RLP encoding

RLP (Recursive Length Prefix, recursive length prefix encoding) is a main encoding method for object serialization in Ethereum, and its purpose is to encode any sequence of nested binary data.

The purpose of RLP is only to encode some data structures, and specific atomic data types like string, int, and float are left to higher order encoding protocols. In Ethereum, integers must be encoded in big-endian format without leading zeros (so integer zeros are an empty array).

If you want to encode a dictionary, it is recommended to use two standard encoding formats - one is to organize the dictionary by the lexicographical order of keys [[k1,v1],[k2,v2]...], the other is to use in Ethereum The high-rise Patricia Tree.

definition:

RLP encoding accepts an item. Item is defined as follows:

  • A string (eg, byte array) is an item
  • Item's list is an item

For example, an empty string is an item, and the same word "cat" is an item. A list containing any number of strings (for example, ["cat",["puppy","cow"],"horse",[[]],"pig",[""],"sheep"]) is also an item .

RLP encoding is defined as follows:

  • For a single-byte (ascii table-defined character) with a value in the range [0x00, 0x7f], its RLP encoding is itself
  • Otherwise, if the length of a string is 0-55 bytes, then his RLP encoding is to add a byte at the beginning of the string, and the value of this byte is \x80 plus the length of the string, ie [\x80, \xb7] .
  • If the length of a string is greater than 55, then the RLP encoding is to add a byte at the beginning of the string, the value of this byte is equal to \xb7 plus the length of the binary encoding of the length of the string, followed by the length of the string. For example, a string with a length of 1024 bytes has a length of 1024=\x04\x00 and a length of 2 bytes, so the value of the RLP encoding header byte is \xb7+\x02=\xb9, followed by \x04\x00. The RLP encoding of String is \xb9\x04\x00(string). The range of the first byte is [0xb8,0xbf]
  • If the total payload of a list (should be the sum of the encoded lengths of all items it contains) has a length of 0-55, then the list's RLP encoding adds a byte before the concatenation of its item's RLP encoding, this The value of the byte is 0xc0 plus the length of the list (the length of the item after the RLP encoding is concatenated to remember). For example RLP(["cat", "dog"]) = [ 0xc8, 0x83, 'c', 'a', 't', 0x83, 'd', 'o', 'g' ]. So the range of header bytes is [0xc0, 0xf7]
  • If the length of the payload of a list is greater than 55, its RLP encoding is the concatenation of the RLP encoding of the item of the list, preceded by a byte representing the payload length, and preceded by a byte length of the binary representation of the payload length.

The python code is as follows:

#!/usr/bin/env python
#encoding=utf-8
# Author: xuyuzhuang - [email protected]
# Last modified: 2016-06-06 22:16
# Filename: rlp_encoding.py
# Description: Recursive Length Prefix

def rlp_encode(input_):
    if isinstance(input_, str):
        if len(input_) == 1 and ord(input_) < 0x80: return input_
        else : return encode_length(len(input_),0x80) + input_
    elif isinstance(input_, list):
        output = ''
        for item in input_: output += rlp_encode(item)
        return encode_length(len(output),0xc0) + output

def encode_length(L,offset):
    if L < 56:
        return chr(L + offset)
    elif L < 256**8: #(2**8)**8
        BL = to_binary(L)
        return chr(len(BL) + offset + 55) + BL
    else:
        raise Exception("input to long!")

def to_binary(x):
    if x == 0:
        return ''
    else:
        return to_binary(int(x/256))+chr(x%256)

def my_print(string):
    for i in string:
        if ord(i) > 32 and ord(i) < 127:
            print i,
        else:
            print hex(ord(i)),

if __name__ == "__main__":
    #string = "dog"
    string =  ["cat", "dog"]
    #string = "Power is the pain
    my_print(rlp_encode(string))

 

For "dog", the output is

For ["cat", "dog"], the output is

"Love the pain itself

 

 

Reference: https://github.com/ethereum/wiki/wiki/RLP

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326266401&siteId=291194637