链接:https://eth.wiki/en/fundamentals/rlp
RLP(递归长度前缀)的目的是对二进制数据的任意嵌套数组进行编码,而RLP是在以太坊中序列化对象的主要编码方法。RLP的唯一目的是对结构进行编码。编码特定的数据类型(例如,字符串,浮点数)由更高阶的协议决定;但是正RLP整数必须以大尾数二进制形式表示,且不包含前导零(因此使整数值零等于空字节数组)。具有前导零的反序列化正整数必须视为无效。字符串长度的整数表示形式以及有效载荷中的整数也必须采用这种方式进行编码。其他信息可以在以太坊黄皮书附录B中找到。
如果希望使用RLP对字典进行编码,则建议的两种规范形式是按字典顺序将[[k1,v1],[k2,v2] ...]与键结合使用,或使用更高级别的Patricia树以太坊进行编码
定义:
RLP编码功能接收一个项目。一个项目定义如下:
- 字符串(即字节数组)是一项 项目
- 列表是一个项目
例如,一个空字符串是一个项目,包含单词“ cat”的字符串,包含任意数量的字符串的列表以及更复杂的数据结构(例如,["cat",["puppy","cow"],"horse",[[]],"pig",[""],"sheep"]。
RLP编码定义如下:
- 对于值在[0x00,0x7f]范围内的单个字节,该字节是其自己的RLP编码
- 否则,如果字符串的长度为0-55个字节,则RLP编码由一个值为0x80的单个字节加上该字符串的长度()组成。因此,第一个字节的范围是[0x80,0xb7]。55的16进制是0x37。
- 如果字符串的长度超过55个字节,则RLP编码由一个值为0xb7的字节加上二进制形式的字符串长度的以字节为单位的长度(也即长度的长度,存储字符串长度数据的字节数),其后为字符串的长度,然后为字符串.例如,长度为1024的字符串将被编码为\ xb9 \ x04 \ x00,后跟该字符串。因此,第一个字节的范围是[0xb8,0xbf]。例中字符串长度为1024,16进制为\ x04 \ x00。1024的二进制是10位,转换成字节是2位。所以0xb7+2.
- 如果列表的总有效载荷(即,其所有项目均经过RLP编码的总长度)为0-55字节,RLP编码由一个值为0xc0的字节组成,再加上列表的长度,然后是各项的RLP编码的串联。因此,第一个字节的范围是[0xc0,0xf7]
- 如果列表的总有效载荷长于55个字节,则RLP编码由一个值为0xf7的字节加上二进制形式的有效载荷长度的字节长度组成(与第三点一样,长度的长度)。其次是有效载荷的长度,然后是项目的RLP编码的串联。因此,第一个字节的范围是[0xf8,0xff]。
代码形式如下:
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:
BL = to_binary(L)
return chr(len(BL) + offset + 55) + BL
else:
raise Exception("input too long")
def to_binary(x):
if x == 0:
return ''
else:
return to_binary(int(x / 256)) + chr(x % 256)
例:
The string “dog” = [ 0x83, ‘d’, ‘o’, ‘g’ ]
The list [ “cat”, “dog” ] = [ 0xc8, 0x83, 'c', 'a', 't', 0x83, 'd', 'o', 'g' ](6元素,所以6个字节,然后两个rpl编码,一共8个字节)
The empty string (‘null’) = [ 0x80 ]
The empty list = [ 0xc0 ]
The integer 0 = [ 0x80 ]
The encoded integer 0 (’\x00’) = [ 0x00 ]
The encoded integer 15 (’\x0f’) = [ 0x0f ]
The encoded integer 1024 (’\x04\x00’) = [ 0x82, 0x04, 0x00 ]
The set theoretical representation of three, [ [], [[]], [ [], [[]] ] ] = [ 0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0 ]
The string “Lorem ipsum dolor sit amet, consectetur adipisicing elit” = [ 0xb8, 0x38, 'L', 'o', 'r', 'e', 'm', ' ', ... , 'e', 'l', 'i', 't' ]
RLP解码
根据RLP编码的规则和过程,将RLP解码的输入视为二进制数据数组,过程如下:
- 根据输入数据的第一个字节(即前缀),解码数据类型,实际数据的长度和偏移量;
- 根据数据的类型和偏移量,对数据进行相应的解码;
- 继续解码其余输入;
其中,解码数据类型和偏移量的规则如下:
- 如果第一个字节(即前缀)的范围是[0x00,0x7f],并且该字符串恰好是第一个字节本身,则该数据为字符串。
- 如果第一个字节的范围是[0x80,0xb7],则数据是一个字符串,并且长度等于第一个字节减去0x80,位于第一个字节之后
- 如果第一个字节的范围是[0xb8,0xbf],并且字符串长度的长度(以字节为单位)等于第一个字节减去0xb7),则该数据为字符串。随后的多个字节是字符串长度(这个多个就是字符串长度的长度,也即相减之后的结果),字符串。
- 如果第一个字节的范围是[0xc0,0xf7],并且列表的所有项的RLP编码的串联(总有效载荷等于第一个字节减去0xc0)跟在第一个字节之后,则该数据为列表。
- 如果第一个字节的范围是[0xf8,0xff],并且列表的总有效载荷的长度等于第一个字节减去0xf7,列表中所有项目的RLP编码的串联遵循列表的总有效载荷,则该数据为列表
在代码中:
def rlp_decode(input):
if len(input) == 0:
return
output = ''
(offset, dataLen, type) = decode_length(input)
if type is str:
output = instantiate_str(substr(input, offset, dataLen))
elif type is list:
output = instantiate_list(substr(input, offset, dataLen))
output + rlp_decode(substr(input, offset + dataLen))
return output
def decode_length(input):
length = len(input)
if length == 0:
raise Exception("input is null")
prefix = ord(input[0])
if prefix <= 0x7f:
return (0, 1, str)
elif prefix <= 0xb7 and length > prefix - 0x80:
strLen = prefix - 0x80
return (1, strLen, str)
elif prefix <= 0xbf and length > prefix - 0xb7 and length > prefix - 0xb7 + to_integer(substr(input, 1, prefix - 0xb7)):
lenOfStrLen = prefix - 0xb7
strLen = to_integer(substr(input, 1, lenOfStrLen))
return (1 + lenOfStrLen, strLen, str)
elif prefix <= 0xf7 and length > prefix - 0xc0:
listLen = prefix - 0xc0;
return (1, listLen, list)
elif prefix <= 0xff and length > prefix - 0xf7 and length > prefix - 0xf7 + to_integer(substr(input, 1, prefix - 0xf7)):
lenOfListLen = prefix - 0xf7
listLen = to_integer(substr(input, 1, lenOfListLen))
return (1 + lenOfListLen, listLen, list)
else:
raise Exception("input don't conform RLP encoding form")
def to_integer(b):
length = len(b)
if length == 0:
raise Exception("input is null")
elif length == 1:
return ord(b[0])
else:
return ord(substr(b, -1)) + to_integer(substr(b, 0, -1)) * 256