forked from ethereum/go-verkle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding.go
142 lines (130 loc) · 4.42 KB
/
encoding.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
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <https://unlicense.org>
package verkle
import (
"errors"
"fmt"
)
var ErrInvalidNodeEncoding = errors.New("invalid node encoding")
var mask = [8]byte{0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1}
func bit(bitlist []byte, nr int) bool {
if len(bitlist)*8 <= nr {
return false
}
return bitlist[nr/8]&mask[nr%8] != 0
}
var serializedPayloadTooShort = errors.New("verkle payload is too short")
func ParseNode(serialized []byte, depth byte, comm []byte) (VerkleNode, error) {
if len(serialized) < 64 {
return nil, serializedPayloadTooShort
}
switch serialized[0] {
case leafRLPType:
return parseLeafNode(serialized, depth)
case internalRLPType:
return CreateInternalNode(serialized[1:33], serialized[33:], depth, comm)
default:
return nil, ErrInvalidNodeEncoding
}
}
func ParseStatelessNode(serialized []byte, depth byte, comm []byte) (VerkleNode, error) {
if len(serialized) < 64 {
return nil, serializedPayloadTooShort
}
switch serialized[0] {
case leafRLPType:
return parseLeafNode(serialized, depth)
case internalRLPType:
return deserializeIntoStateless(serialized[1:33], serialized[33:], depth, comm)
default:
return nil, ErrInvalidNodeEncoding
}
}
func parseLeafNode(serialized []byte, depth byte) (VerkleNode, error) {
var values [NodeWidth][]byte
offset := 64
for i := 0; i < NodeWidth; i++ {
if bit(serialized[32:64], i) {
if offset+32 > len(serialized) {
return nil, fmt.Errorf("verkle payload is too short, need at least %d and only have %d, payload = %x (%w)", offset+32, len(serialized), serialized, serializedPayloadTooShort)
}
values[i] = serialized[offset : offset+32]
offset += 32
}
}
if NodeWidth != len(values) {
return nil, fmt.Errorf("invalid number of nodes in decoded child expected %d, got %d", NodeWidth, len(values))
}
ln := NewLeafNode(serialized[1:32], values[:])
ln.setDepth(depth)
ln.Commit()
return ln, nil
}
func deserializeIntoStateless(bitlist []byte, raw []byte, depth byte, comm []byte) (*StatelessNode, error) {
// GetTreeConfig caches computation result, hence
// this op has low overhead
n := NewStateless()
n.setDepth(depth)
indices := indicesFromBitlist(bitlist)
if len(raw)/32 != len(indices) {
return nil, ErrInvalidNodeEncoding
}
for i, index := range indices {
n.unresolved[byte(index)] = raw[i*32 : (i+1)*32]
}
n.commitment = new(Point)
n.commitment.SetBytesTrusted(comm)
return n, nil
}
func CreateInternalNode(bitlist []byte, raw []byte, depth byte, comm []byte) (*InternalNode, error) {
// GetTreeConfig caches computation result, hence
// this op has low overhead
n := (newInternalNode(depth)).(*InternalNode)
indices := indicesFromBitlist(bitlist)
if len(raw)/32 != len(indices) {
return nil, ErrInvalidNodeEncoding
}
for i, index := range indices {
n.children[index] = &HashedNode{commitment: raw[i*32 : (i+1)*32]}
}
n.commitment = new(Point)
n.commitment.SetBytesTrusted(comm)
return n, nil
}
func indicesFromBitlist(bitlist []byte) []int {
indices := make([]int, 0)
for i, b := range bitlist {
if b == 0 {
continue
}
// the bitmap is little-endian, inside a big-endian byte list
for j := 0; j < 8; j++ {
if b&mask[j%8] != 0 {
indices = append(indices, i*8+j)
}
}
}
return indices
}