0

The RLP specification says the following about integers:

The only purpose of RLP is to encode structure; encoding specific atomic data types (eg. strings, ints, floats) is left up to higher-order protocols; in Ethereum integers must be represented in big endian binary form with no leading zeroes (thus making the integer value zero be equivalent to the empty byte array).

When it says "in Ethereum" does this actually mean in the ETH subprotocol alone, or does this mean in the body of work 'Ethereum' and specifically the RLP protocol? If the latter, this paragraph seems to contradict itself. On the one hand, it is saying that RLP leaves the encoding to higher protocols, but on the other says that RLP demands big endian integers with all leading zeroes skipped.

Where should this encoding be implemented then? In the RLP protocol, or should there be a specific type 'EthereumInteger' distinct from 'integer'?

Perhaps a better question might be, to whom should this sort of question best be addressed and where?

  • The answer below is correct if it includes all the comments. – Sentinel Nov 12 '17 at 22:18
2

RLP deals only in structures made up of bytes (binary data). It doesn't care whether those bytes represent strings, integers, big integers, floating point numbers or whatever. This is in the sentence prior to the one you quoted,

The purpose of RLP (Recursive Length Prefix) is to encode arbitrarily nested arrays of binary data...

On your question:

...but on the other says that RLP demands big endian integers with all leading zeroes skipped.

No, it doesn't say that. It says that in Ethereum integers are represented as big endian with no leading zero bytes.

RLP is not specific to Ethereum (although it was developed for Ethereum AFAIK). The point is that Ethereum is one of the "higher-order protocols" mentioned in this sentence. So it all makes sense:

  • RLP encodes arbitrary binary data and doesn't care what it represents in any higher-level protocol;
  • [implied "As an example of such a higher order protocol..."] Ethereum integers must be represented in big endian binary form with no leading zeroes.

Admittedly the Ethereum part could be omitted and things might be clearer. I think the author just wanted to give an example of such a higher-order protocol for concreteness.

  • Thanks for that but I do not yet know if that is accurate. For example Ethereum integers in method calls are not encoded in this way. This looks more like it is trying to say that the ethereum specific implementation of RLP must encode like this, not the whole body of work Ethereum. And when you say other protocols, do you mean just ETH, or devp2p or shh etc? – Sentinel Nov 12 '17 at 15:37
  • "For example Ethereum integers in method calls are not encoded in this way." - these are not integers, they are ABI byte strings. A smart contract may choose to interpret them as integers, or as something else. This is not RLP-related. RLP is RLP totally independently of Etherem. Look at its sample code in the article; there's nothing Ethereum-specific in it. – benjaminion Nov 12 '17 at 15:43
  • OK thanks for that. I am just trying to learn. Can you point to where it is specified that Ethereum integers are represented as big-endian no-leading-zero bytes? Also in the RLP protocol specification the " length of the string in binary form" is what data type, if not big endian non-leading-zero integer? Eg: this phrase in the RLP spec :"followed by the length of the payload". – Sentinel Nov 12 '17 at 16:37
  • Learning is good :-) The Ethereum Yellow Paper is the canonical reference. For the specific Ethereum implementation of RLP, see Appendix B. At the end of that appendix you can see a statement that (in Ethereum clients) "if an expected fragment is decoded as a scalar and leading zeroes are found in the byte sequence" it is to be considered invalid. For big-endian, see the first sentence of Appendix H. It's only in the RLP encoding of integers in Ethereum that leading zero bytes must be dropped. It's not a general feature of Ethereum per se. – benjaminion Nov 12 '17 at 16:49
  • 1
    I think we have converged: Appendix B is the Ethereum RLP spec, and I agree with your other statements. Anyway, it's been interesting to revisit this. Best of luck with the light client! – benjaminion Nov 12 '17 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.