rlp-lite 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +172 -3
- data/lib/rlp-lite/util.rb +24 -15
- data/lib/rlp-lite/version.rb +1 -1
- data/lib/rlp-lite.rb +8 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ee2acc28b11b60da5de5a0609e09ab49f4b256b8cd18a78b56cc5fbf3608620
|
4
|
+
data.tar.gz: 8cc0a1436872cf91951b860e6302b5ef71369cb7e429037c1ae81f5697eb7acd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d652299872dfa39154cf5799123763acd24d4292e41519409db8d9e77f72d7e4f5ff1b38e1c090138fd2f046048899fda8bb8987f8cb64e1bdf1408af749e882
|
7
|
+
data.tar.gz: 620a9e400cbf5c915ddbc81ae0c8e11e0d6991c4feade7cd5c2d2f1801aa75745bd4ec1490e14ec27c86d058f2d19642dc421de0d6ea52396b5cc519e531a47c
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
# Recursive
|
1
|
+
# Recursive-Length Prefix (RLP) Serialization Lite
|
2
2
|
|
3
3
|
|
4
|
-
rlp-lite - light-weight machinery to serialize / deserialze
|
4
|
+
rlp-lite - light-weight ("lite") machinery to serialize / deserialze using the recursive-length prefix (rlp) scheme
|
5
5
|
|
6
6
|
|
7
7
|
|
@@ -36,7 +36,7 @@ decoded = Rlp.decode( "\xCB\x84ruby\x83rlp\x81\xFF".b )
|
|
36
36
|
|
37
37
|
|
38
38
|
|
39
|
-
Note: All integers get returned (decoded) as big integers in binary buffers (that is, string with binary "ASCII-8BIT" encoding)
|
39
|
+
Note: All integers get returned (decoded) as big-endian integers in binary buffers (that is, string with binary "ASCII-8BIT" encoding)
|
40
40
|
e.g. `"\xFF".b` and not `255`.
|
41
41
|
|
42
42
|
|
@@ -78,6 +78,175 @@ decoded = Rlp.decode( "0xecca846b6579318476616c31ca846b6579328476616c32ca846b657
|
|
78
78
|
```
|
79
79
|
|
80
80
|
|
81
|
+
|
82
|
+
## More About the Recursive-Length Prefix (RLP) Serialization
|
83
|
+
|
84
|
+
via [Recursive-Length Prefix (RLP) Serialization](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/)
|
85
|
+
|
86
|
+
|
87
|
+
The RLP encoding function takes in an item. An item is defined as follows:
|
88
|
+
|
89
|
+
- a string (i.e. byte array) is an item
|
90
|
+
- a list of items is an item
|
91
|
+
|
92
|
+
For example, all of the following are items:
|
93
|
+
|
94
|
+
- an empty string;
|
95
|
+
- the string containing the word "cat";
|
96
|
+
- a list containing any number of strings;
|
97
|
+
- and a more complex data structures like `["cat", ["puppy", "cow"], "horse", [[]], "pig", [""], "sheep"]`.
|
98
|
+
|
99
|
+
Note that in the context of the rest of this page, 'string' means "a certain number of bytes of binary data"; no special encodings are used, and no knowledge about the content of the strings is implied.
|
100
|
+
|
101
|
+
RLP encoding is defined as follows:
|
102
|
+
|
103
|
+
- For a single byte whose value is in the `[0x00, 0x7f]` (decimal `[0, 127]`) range, that byte is its own RLP encoding.
|
104
|
+
- Otherwise, if a string is 0-55 bytes long, the RLP encoding consists of a single byte with value **0x80** (dec. 128) plus the length of the string followed by the string. The range of the first byte is thus `[0x80, 0xb7]` (dec. `[128, 183]`).
|
105
|
+
- If a string is more than 55 bytes long, the RLP encoding consists of a single byte with value **0xb7** (dec. 183) plus the length in bytes of the length of the string in binary form, followed by the length of the string, followed by the string. For example, a 1024 byte long string would be encoded as `\xb9\x04\x00` (dec. `185, 4, 0`) followed by the string. Here, `0xb9` (183 + 2 = 185) as the first byte, followed by the 2 bytes `0x0400` (dec. 1024) that denote the length of the actual string. The range of the first byte is thus `[0xb8, 0xbf]` (dec. `[184, 191]`).
|
106
|
+
- If the total payload of a list (i.e. the combined length of all its items being RLP encoded) is 0-55 bytes long, the RLP encoding consists of a single byte with value **0xc0** plus the length of the list followed by the concatenation of the RLP encodings of the items. The range of the first byte is thus `[0xc0, 0xf7]` (dec. `[192, 247]`).
|
107
|
+
- If the total payload of a list is more than 55 bytes long, the RLP encoding consists of a single byte with value **0xf7** plus the length in bytes of the length of the payload in binary form, followed by the length of the payload, followed by the concatenation of the RLP encodings of the items. The range of the first byte is thus `[0xf8, 0xff]` (dec. `[248, 255]`).
|
108
|
+
|
109
|
+
In code, this is:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
PRIMITIVE_PREFIX_OFFSET = 0x80 # The RLP primitive type offset (dec. 128).
|
113
|
+
LIST_PREFIX_OFFSET = 0xc0 # The RLP array type offset (dec. 192).
|
114
|
+
|
115
|
+
def rlp_encode( input )
|
116
|
+
if input.instance_of?( String )
|
117
|
+
if input.length == 1 && input.ord < PRIMITIVE_PREFIX_OFFSET
|
118
|
+
input
|
119
|
+
else
|
120
|
+
encode_length( input.length, PRIMITIVE_PREFIX_OFFSET ) + input
|
121
|
+
end
|
122
|
+
elsif input.instance_of?( Array )
|
123
|
+
output = ''
|
124
|
+
input.each do |item|
|
125
|
+
output += rlp_encode( item )
|
126
|
+
end
|
127
|
+
encode_length( output.length, LIST_PREFIX_OFFSET ) + output
|
128
|
+
else
|
129
|
+
raise ArgumentError, "type error"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def encode_length( l, offset )
|
134
|
+
if l < 56
|
135
|
+
(l + offset).chr
|
136
|
+
elsif l < 256**8 ## 256**8 = 18446744073709551616
|
137
|
+
bl = to_binary( l )
|
138
|
+
(bl.length + offset + 55).chr + bl
|
139
|
+
else
|
140
|
+
raise ArgumentError, "input too long"
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def to_binary(x)
|
145
|
+
x == 0 ? '' : to_binary( x / 256 ) + (x % 256).chr
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
|
150
|
+
**Examples**
|
151
|
+
|
152
|
+
- the string "dog" = [ 0x83, 'd', 'o', 'g' ]
|
153
|
+
- the list [ "cat", "dog" ] = `[ 0xc8, 0x83, 'c', 'a', 't', 0x83, 'd', 'o', 'g' ]`
|
154
|
+
- the empty string ('null') = `[ 0x80 ]`
|
155
|
+
- the empty list = `[ 0xc0 ]`
|
156
|
+
- the integer 0 = `[ 0x80 ]`
|
157
|
+
- the encoded integer 0 ('\\x00') = `[ 0x00 ]`
|
158
|
+
- the encoded integer 15 ('\\x0f') = `[ 0x0f ]`
|
159
|
+
- the encoded integer 1024 ('\\x04\\x00') = `[ 0x82, 0x04, 0x00 ]`
|
160
|
+
- the [set theoretical representation](http://en.wikipedia.org/wiki/Set-theoretic_definition_of_natural_numbers) of three, `[ [], [[]], [ [], [[]] ] ] = [ 0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0 ]`
|
161
|
+
- the string "Lorem ipsum dolor sit amet, consectetur adipisicing elit" = `[ 0xb8, 0x38, 'L', 'o', 'r', 'e', 'm', ' ', ... , 'e', 'l', 'i', 't' ]`
|
162
|
+
|
163
|
+
|
164
|
+
**RLP decoding**
|
165
|
+
|
166
|
+
According to the rules and process of RLP encoding, the input of RLP decode is regarded as an array of binary data. The RLP decoding process is as follows:
|
167
|
+
|
168
|
+
1. according to the first byte (i.e. prefix) of input data and decoding the data type, the length of the actual data and offset;
|
169
|
+
|
170
|
+
2. according to the type and offset of data, decode the data correspondingly;
|
171
|
+
|
172
|
+
3. continue to decode the rest of the input;
|
173
|
+
|
174
|
+
Among them, the rules of decoding data types and offset is as follows:
|
175
|
+
|
176
|
+
1. the data is a string if the range of the first byte (i.e. prefix) is [0x00, 0x7f], and the string is the first byte itself exactly;
|
177
|
+
|
178
|
+
2. the data is a string if the range of the first byte is [0x80, 0xb7], and the string whose length is equal to the first byte minus 0x80 follows the first byte;
|
179
|
+
|
180
|
+
3. the data is a string if the range of the first byte is [0xb8, 0xbf], and the length of the string whose length in bytes is equal to the first byte minus 0xb7 follows the first byte, and the string follows the length of the string;
|
181
|
+
|
182
|
+
4. the data is a list if the range of the first byte is [0xc0, 0xf7], and the concatenation of the RLP encodings of all items of the list which the total payload is equal to the first byte minus 0xc0 follows the first byte;
|
183
|
+
|
184
|
+
5. the data is a list if the range of the first byte is [0xf8, 0xff], and the total payload of the list whose length is equal to the first byte minus 0xf7 follows the first byte, and the concatenation of the RLP encodings of all items of the list follows the total payload of the list;
|
185
|
+
|
186
|
+
In code, this is:
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
def rlp_decode( input, output=[] )
|
190
|
+
return output[0] if input.length == 0
|
191
|
+
|
192
|
+
offset, dataLen, type = decode_length( input )
|
193
|
+
|
194
|
+
if type == String
|
195
|
+
output << input[ offset, dataLen ]
|
196
|
+
elsif type == Array
|
197
|
+
list = []
|
198
|
+
rlp_decode( input[ offset, dataLen], list )
|
199
|
+
output << list
|
200
|
+
else
|
201
|
+
raise ArgumentError, "type error"
|
202
|
+
end
|
203
|
+
|
204
|
+
rlp_decode( input[ (offset + dataLen)..-1], output )
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
def decode_length( input )
|
209
|
+
length = input.length
|
210
|
+
|
211
|
+
raise ArgumentError, "input is null" if length == 0
|
212
|
+
|
213
|
+
prefix = input[0].ord
|
214
|
+
if prefix <= 0x7f
|
215
|
+
[0, 1, String]
|
216
|
+
elsif prefix <= 0xb7 && length > prefix - 0x80
|
217
|
+
strLen = prefix - 0x80
|
218
|
+
[1, strLen, String]
|
219
|
+
elsif prefix <= 0xbf && length > prefix - 0xb7 && length > prefix - 0xb7 + to_integer( input[1, prefix - 0xb7] )
|
220
|
+
lenOfStrLen = prefix - 0xb7
|
221
|
+
strLen = to_integer( input[1, lenOfStrLen] )
|
222
|
+
[1 + lenOfStrLen, strLen, String]
|
223
|
+
elsif prefix <= 0xf7 && length > prefix - 0xc0
|
224
|
+
listLen = prefix - 0xc0
|
225
|
+
[1, listLen, Array]
|
226
|
+
elsif prefix <= 0xff && length > prefix - 0xf7 && length > prefix - 0xf7 + to_integer( input[1, prefix - 0xf7])
|
227
|
+
lenOfListLen = prefix - 0xf7
|
228
|
+
listLen = to_integer( input[1, lenOfListLen] )
|
229
|
+
[1 + lenOfListLen, listLen, Array]
|
230
|
+
else
|
231
|
+
raise ArgumentError, "input don't conform RLP encoding form"
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
|
236
|
+
def to_integer( b )
|
237
|
+
length = b.length
|
238
|
+
if length == 0
|
239
|
+
raise ArgumentError, "input is null"
|
240
|
+
elsif length == 1
|
241
|
+
b[0].ord
|
242
|
+
else
|
243
|
+
b[-1].ord + to_integer( b[0, length-1] ) * 256
|
244
|
+
end
|
245
|
+
end
|
246
|
+
```
|
247
|
+
|
248
|
+
|
249
|
+
|
81
250
|
## License
|
82
251
|
|
83
252
|
The scripts are dedicated to the public domain.
|
data/lib/rlp-lite/util.rb
CHANGED
@@ -4,15 +4,13 @@ module Util
|
|
4
4
|
extend self
|
5
5
|
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
# Checks if a string is hex-adecimal.
|
7
|
+
# Checks if a string is hex-adecimal (string).
|
10
8
|
#
|
11
9
|
# @param str [String] a string to be checked.
|
12
10
|
# @return [String] a match if true; `nil` if not.
|
13
|
-
def is_hex?(str)
|
11
|
+
def is_hex?( str )
|
14
12
|
return false unless str.is_a?( String )
|
15
|
-
str =
|
13
|
+
str = strip_hex_prefix( str )
|
16
14
|
str.match /\A[0-9a-fA-F]*\z/
|
17
15
|
end
|
18
16
|
|
@@ -20,18 +18,25 @@ module Util
|
|
20
18
|
#
|
21
19
|
# @param hex [String] a prefixed hex-string.
|
22
20
|
# @return [String] an unprefixed hex-string.
|
23
|
-
def
|
24
|
-
|
25
|
-
return hex
|
21
|
+
def strip_hex_prefix(hex)
|
22
|
+
is_prefixed?( hex ) ? hex[2..-1] : hex
|
26
23
|
end
|
24
|
+
alias_method :remove_hex_prefix, :strip_hex_prefix
|
25
|
+
alias_method :strip_0x, :strip_hex_prefix ## add more alias - why? why not?
|
27
26
|
|
28
27
|
# Checks if a string is prefixed with `0x`.
|
29
28
|
#
|
30
29
|
# @param hex [String] a string to be checked.
|
31
30
|
# @return [String] a match if true; `nil` if not.
|
32
31
|
def is_prefixed?(hex)
|
33
|
-
hex.match /\A0x/
|
32
|
+
## was: hex.match /\A0x/
|
33
|
+
## tood/check: add support for (upcase) 0X too - why? why not?
|
34
|
+
hex.start_with?( '0x' ) ||
|
35
|
+
hex.start_with?( '0X' )
|
34
36
|
end
|
37
|
+
alias_method :is_hex_prefixed?, :is_prefixed?
|
38
|
+
alias_method :start_with_0x?, :is_prefixed?
|
39
|
+
|
35
40
|
|
36
41
|
# Packs a hexa-decimal string into a binary string. Also works with
|
37
42
|
# `0x`-prefixed strings.
|
@@ -64,6 +69,7 @@ module Util
|
|
64
69
|
def str_to_bytes(str)
|
65
70
|
is_bytes?(str) ? str : str.b
|
66
71
|
end
|
72
|
+
## todo/check - rename to str_to_binary - why? why not?
|
67
73
|
|
68
74
|
# Checks if a string is a byte-string.
|
69
75
|
#
|
@@ -72,6 +78,7 @@ module Util
|
|
72
78
|
def is_bytes?(str)
|
73
79
|
str && str.instance_of?(String) && str.encoding.name == 'ASCII-8BIT'
|
74
80
|
end
|
81
|
+
## todo/check - rename to is binary? is_binary?
|
75
82
|
|
76
83
|
|
77
84
|
|
@@ -79,10 +86,10 @@ module Util
|
|
79
86
|
#
|
80
87
|
# @param num [Integer] integer to be converted.
|
81
88
|
# @return [String] packed, big-endian integer string.
|
82
|
-
def int_to_big_endian(num)
|
83
|
-
hex = num.to_s(16)
|
89
|
+
def int_to_big_endian( num )
|
90
|
+
hex = num.to_s(16)
|
84
91
|
hex = "0#{hex}" if hex.size.odd?
|
85
|
-
hex_to_bin hex
|
92
|
+
hex_to_bin( hex )
|
86
93
|
end
|
87
94
|
|
88
95
|
|
@@ -91,16 +98,18 @@ module Util
|
|
91
98
|
#
|
92
99
|
# @param str [String] big endian to be converted.
|
93
100
|
# @return [Integer] an unpacked integer number.
|
94
|
-
def big_endian_to_int(str)
|
95
|
-
str.unpack("H*").
|
101
|
+
def big_endian_to_int( str )
|
102
|
+
str.unpack("H*")[0].to_i(16)
|
96
103
|
end
|
97
104
|
|
105
|
+
|
106
|
+
|
98
107
|
# Deserializes big endian data string to integer.
|
99
108
|
#
|
100
109
|
# @param str [String] serialized big endian integer string.
|
101
110
|
# @return [Integer] an deserialized unsigned integer.
|
102
111
|
def deserialize_big_endian_to_int(str)
|
103
|
-
Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/,
|
112
|
+
Sedes.big_endian_int.deserialize str.sub( /\A(\x00)+/, '' )
|
104
113
|
end
|
105
114
|
|
106
115
|
|
data/lib/rlp-lite/version.rb
CHANGED
data/lib/rlp-lite.rb
CHANGED
@@ -21,18 +21,20 @@ module Rlp
|
|
21
21
|
|
22
22
|
## todo/check - use encoding -ascii-8bit for source file or ? - why? why not?
|
23
23
|
## use #b/.b to ensure binary encoding? - why? why not?
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
|
25
|
+
## todo/check: use auto-freeze string literals magic comment - why? why not?
|
26
|
+
BYTE_EMPTY = "".b.freeze # The empty byte is defined as "".
|
27
|
+
BYTE_ZERO = "\x00".b.freeze # The zero byte is 0x00.
|
28
|
+
BYTE_ONE = "\x01".b.freeze # The byte one is 0x01.
|
27
29
|
|
28
30
|
|
29
31
|
SHORT_LENGTH_LIMIT = 56 # The RLP short length limit.
|
30
32
|
LONG_LENGTH_LIMIT = (256 ** 8) # The RLP long length limit.
|
31
33
|
PRIMITIVE_PREFIX_OFFSET = 0x80 # The RLP primitive type offset.
|
32
|
-
LIST_PREFIX_OFFSET
|
34
|
+
LIST_PREFIX_OFFSET = 0xc0 # The RLP array type offset.
|
35
|
+
|
33
36
|
|
34
|
-
# Infinity as constant for convenience.
|
35
|
-
INFINITY = (1.0 / 0.0)
|
37
|
+
INFINITY = (1.0 / 0.0) # Infinity as constant for convenience.
|
36
38
|
|
37
39
|
|
38
40
|
# The Rlp module exposes a variety of exceptions grouped as {RlpException}.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rlp-lite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-11-
|
11
|
+
date: 2022-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdoc
|