This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
rlp.go
359 lines (317 loc) · 8.96 KB
/
rlp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//
// See https://eth.wiki/fundamentals/rlp
//
package rlp
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
"math/bits"
"reflect"
binary2 "github.com/hyperledger/burrow/binary"
)
type magicOffset uint8
const (
ShortLength = 55
StringOffset magicOffset = 0x80 // 128 - if string length is less than or equal to 55 [inclusive]
SliceOffset magicOffset = 0xC0 // 192 - if slice length is less than or equal to 55 [inclusive]
SmallByte = 0x7f // 247 - value less than or equal is itself [inclusive
)
type Code uint32
const (
ErrUnknown Code = iota
ErrNoInput
ErrInvalid
)
var bigIntType = reflect.TypeOf(&big.Int{})
func (c Code) Error() string {
switch c {
case ErrNoInput:
return "no input"
case ErrInvalid:
return "input not valid RLP encoding"
default:
return "unknown error"
}
}
func Encode(input interface{}) ([]byte, error) {
val := reflect.ValueOf(input)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
return encode(val)
}
func Decode(src []byte, dst interface{}) error {
fields, err := decode(src)
if err != nil {
return err
}
val := reflect.ValueOf(dst)
typ := reflect.TypeOf(dst)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
switch val.Kind() {
case reflect.Slice:
switch typ.Elem().Kind() {
case reflect.Uint8:
out, ok := dst.([]byte)
if !ok {
return fmt.Errorf("cannot decode into type %s", val.Type())
}
found := bytes.Join(fields, []byte(""))
if len(out) < len(found) {
return fmt.Errorf("cannot decode %d bytes into slice of size %d", len(found), len(out))
}
for i, b := range found {
out[i] = b
}
default:
for i := 0; i < val.Len(); i++ {
elem := val.Index(i)
err = decodeField(elem, fields[i])
if err != nil {
return err
}
}
}
case reflect.Struct:
rt := val.Type()
numExportedFields := 0
for i := 0; i < val.NumField(); i++ {
// Skip unexported fields
if rt.Field(i).PkgPath == "" {
err := decodeField(val.Field(i), fields[numExportedFields])
if err != nil {
return err
}
numExportedFields++
}
}
if numExportedFields != len(fields) {
return fmt.Errorf("wrong number of fields; have %d, want %d", len(fields), numExportedFields)
}
default:
return fmt.Errorf("cannot decode into unsupported type %v", reflect.TypeOf(dst))
}
return nil
}
func encodeUint8(input uint8) ([]byte, error) {
if input == 0 {
// yes this makes no sense, but it does seem to be what everyone else does, apparently 'no leading zeroes'.
// It means we cannot store []byte{0} because that is indistinguishable from byte{}
return []byte{uint8(StringOffset)}, nil
} else if input <= SmallByte {
return []byte{input}, nil
} else if input >= uint8(StringOffset) {
return []byte{0x81, input}, nil
}
return []byte{uint8(StringOffset)}, nil
}
func encodeUint64(i uint64) ([]byte, error) {
// Byte-wise ceiling
byteCount := (bits.Len64(i) + 7) / 8
if byteCount == 1 {
return encodeUint8(uint8(i))
}
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(i))
return encodeString(b[8-byteCount:])
}
func encodeBigInt(b *big.Int) ([]byte, error) {
if b.Sign() == -1 {
return nil, fmt.Errorf("cannot RLP encode negative number")
}
if b.IsUint64() {
return encodeUint64(b.Uint64())
}
bs := b.Bytes()
length := encodeLength(len(bs), StringOffset)
return append(length, bs...), nil
}
func encodeLength(n int, offset magicOffset) []byte {
// > if a string is 0-55 bytes long, the RLP encoding consists of a single byte with value 0x80 plus
// > the length of the string followed by the string.
if n <= ShortLength {
return []uint8{uint8(offset) + uint8(n)}
}
i := uint64(n)
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, i)
// Byte-wise ceiling
byteLengthOfLength := (bits.Len64(i) + 7) / 8
// > If a string is more than 55 bytes long, the RLP encoding consists of a single byte with value 0xb7
// > 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
return append([]byte{uint8(offset) + ShortLength + uint8(byteLengthOfLength)}, b[8-byteLengthOfLength:]...)
}
func encodeString(input []byte) ([]byte, error) {
if len(input) == 1 && input[0] <= SmallByte {
return encodeUint8(input[0])
} else {
return append(encodeLength(len(input), StringOffset), input...), nil
}
}
func encodeList(val reflect.Value) ([]byte, error) {
if val.Len() == 0 {
return []byte{uint8(SliceOffset)}, nil
}
out := make([][]byte, 0)
for i := 0; i < val.Len(); i++ {
data, err := encode(val.Index(i))
if err != nil {
return nil, err
}
out = append(out, data)
}
sum := bytes.Join(out, []byte{})
return append(encodeLength(len(sum), SliceOffset), sum...), nil
}
func encodeStruct(val reflect.Value) ([]byte, error) {
out := make([][]byte, 0)
rt := val.Type()
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
// Skip unexported fields
if rt.Field(i).PkgPath == "" {
data, err := encode(field)
if err != nil {
return nil, err
}
out = append(out, data)
}
}
sum := bytes.Join(out, []byte{})
length := encodeLength(len(sum), SliceOffset)
return append(length, sum...), nil
}
func encode(val reflect.Value) ([]byte, error) {
if val.Kind() == reflect.Interface {
val = val.Elem()
}
switch val.Kind() {
case reflect.Ptr:
if !val.Type().AssignableTo(bigIntType) {
return nil, fmt.Errorf("cannot encode pointer type %v", val.Type())
}
return encodeBigInt(val.Interface().(*big.Int))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i := val.Int()
if i < 0 {
return nil, fmt.Errorf("cannot rlp encode negative integer")
}
return encodeUint64(uint64(i))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return encodeUint64(val.Uint())
case reflect.Bool:
if val.Bool() {
return []byte{0x01}, nil
}
return []byte{uint8(StringOffset)}, nil
case reflect.String:
return encodeString([]byte(val.String()))
case reflect.Slice:
switch val.Type().Elem().Kind() {
case reflect.Uint8:
i, err := encodeString(val.Bytes())
return i, err
default:
return encodeList(val)
}
case reflect.Struct:
return encodeStruct(val)
default:
return []byte{uint8(StringOffset)}, nil
}
}
// Split into RLP fields by reading length prefixes and consuming chunks
func decode(in []byte) ([][]byte, error) {
if len(in) == 0 {
return nil, nil
}
offset, length, typ := decodeLength(in)
end := offset + length
if end > uint64(len(in)) {
return nil, fmt.Errorf("read length prefix of %d but there is only %d bytes of unconsumed input",
length, uint64(len(in))-offset)
}
suffix, err := decode(in[end:])
if err != nil {
return nil, err
}
switch typ {
case reflect.String:
return append([][]byte{in[offset:end]}, suffix...), nil
case reflect.Slice:
prefix, err := decode(in[offset:end])
if err != nil {
return nil, err
}
return append(prefix, suffix...), nil
}
return suffix, nil
}
func decodeLength(input []byte) (uint64, uint64, reflect.Kind) {
magicByte := magicOffset(input[0])
switch {
case magicByte <= SmallByte:
// small byte: sufficiently small single byte
return 0, 1, reflect.String
case magicByte <= StringOffset+ShortLength:
// short string: length less than or equal to 55 bytes
length := uint64(magicByte - StringOffset)
return 1, length, reflect.String
case magicByte < SliceOffset:
// long string: length described by magic = 0xb7 + <byte length of length of string>
byteLengthOfLength := magicByte - StringOffset - ShortLength
length := getUint64(input[1:byteLengthOfLength])
offset := uint64(byteLengthOfLength + 1)
return offset, length, reflect.String
case magicByte <= SliceOffset+ShortLength:
// short slice: length less than or equal to 55 bytes
length := uint64(magicByte - SliceOffset)
return 1, length, reflect.Slice
// Note this takes us all the way up to <= 255 so this switch is exhaustive
default:
// long string: length described by magic = 0xf7 + <byte length of length of string>
byteLengthOfLength := magicByte - SliceOffset - ShortLength
length := getUint64(input[1:byteLengthOfLength])
offset := uint64(byteLengthOfLength + 1)
return offset, length, reflect.Slice
}
}
func getUint64(bs []byte) uint64 {
bs = binary2.LeftPadBytes(bs, 8)
return binary.BigEndian.Uint64(bs)
}
func decodeField(val reflect.Value, field []byte) error {
typ := val.Type()
switch val.Kind() {
case reflect.Ptr:
if !typ.AssignableTo(bigIntType) {
return fmt.Errorf("cannot decode into pointer type %v", typ)
}
bi := new(big.Int).SetBytes(field)
val.Set(reflect.ValueOf(bi))
case reflect.String:
val.SetString(string(field))
case reflect.Uint64:
out := make([]byte, 8)
for j := range field {
out[len(out)-(len(field)-j)] = field[j]
}
val.SetUint(binary.BigEndian.Uint64(out))
case reflect.Slice:
if typ.Elem().Kind() != reflect.Uint8 {
// skip
return nil
}
out := make([]byte, len(field))
for i, b := range field {
out[i] = b
}
val.SetBytes(out)
}
return nil
}