forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
201 lines (166 loc) · 5.27 KB
/
key.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
package merkletree
import (
"math"
"math/big"
"github.com/ethereum/go-ethereum/common"
poseidon "github.com/iden3/go-iden3-crypto/goldenposeidon"
)
// Key stores key of the leaf
type Key [32]byte
const (
// HashPoseidonAllZeroes represents the poseidon hash for an input with all
// bits set to zero.
HashPoseidonAllZeroes = "0xc71603f33a1144ca7953db0ab48808f4c4055e3364a246c33c18a9786cb0b359"
)
// keyEthAddr is the common code for all the keys related to ethereum addresses.
func keyEthAddr(ethAddr common.Address, leafType leafType, key1Capacity [4]uint64) ([]byte, error) {
ethAddrBI := new(big.Int).SetBytes(ethAddr.Bytes())
ethAddrArr := scalar2fea(ethAddrBI)
key1 := [8]uint64{
ethAddrArr[0],
ethAddrArr[1],
ethAddrArr[2],
ethAddrArr[3],
ethAddrArr[4],
0,
uint64(leafType),
0,
}
result, err := poseidon.Hash(key1, key1Capacity)
if err != nil {
return nil, err
}
return h4ToFilledByteSlice(result[:]), nil
}
func defaultCapIn() ([4]uint64, error) {
capIn, err := StringToh4(HashPoseidonAllZeroes)
if err != nil {
return [4]uint64{}, err
}
return [4]uint64{capIn[0], capIn[1], capIn[2], capIn[3]}, nil
}
// KeyEthAddrBalance returns the key of balance leaf:
// hk0: H([0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0])
// key: H([ethAddr[0:4], ethAddr[4:8], ethAddr[8:12], ethAddr[12:16], ethAddr[16:20], 0, 0, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
func KeyEthAddrBalance(ethAddr common.Address) ([]byte, error) {
capIn, err := defaultCapIn()
if err != nil {
return nil, err
}
return keyEthAddr(ethAddr, LeafTypeBalance, capIn)
}
// KeyEthAddrNonce returns the key of nonce leaf:
// hk0: H([0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0])
// key: H([ethAddr[0:4], ethAddr[4:8], ethAddr[8:12], ethAddr[12:16], ethAddr[16:20], 0, 1, 0], [hk0[0], hk0[1], hk0[2], hk0[3]]
func KeyEthAddrNonce(ethAddr common.Address) ([]byte, error) {
capIn, err := defaultCapIn()
if err != nil {
return nil, err
}
return keyEthAddr(ethAddr, LeafTypeNonce, capIn)
}
// KeyContractCode returns the key of contract code leaf:
// hk0: H([0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0])
// key: H([ethAddr[0:4], ethAddr[4:8], ethAddr[8:12], ethAddr[12:16], ethAddr[16:20], 0, 2, 0], [hk0[0], hk0[1], hk0[2], hk0[3]]
func KeyContractCode(ethAddr common.Address) ([]byte, error) {
capIn, err := defaultCapIn()
if err != nil {
return nil, err
}
return keyEthAddr(ethAddr, LeafTypeCode, capIn)
}
// KeyContractStorage returns the key of contract storage position leaf:
// hk0: H([stoPos[0:4], stoPos[4:8], stoPos[8:12], stoPos[12:16], stoPos[16:20], stoPos[20:24], stoPos[24:28], stoPos[28:32], [0, 0, 0, 0])
// key: H([ethAddr[0:4], ethAddr[4:8], ethAddr[8:12], ethAddr[12:16], ethAddr[16:20], 0, 3, 0], [hk0[0], hk0[1], hk0[2], hk0[3])
func KeyContractStorage(ethAddr common.Address, storagePos []byte) ([]byte, error) {
storageBI := new(big.Int).SetBytes(storagePos)
storageArr := scalar2fea(storageBI)
hk0, err := poseidon.Hash([8]uint64{
storageArr[0],
storageArr[1],
storageArr[2],
storageArr[3],
storageArr[4],
storageArr[5],
storageArr[6],
storageArr[7],
}, [4]uint64{})
if err != nil {
return nil, err
}
return keyEthAddr(ethAddr, LeafTypeStorage, hk0)
}
// hashContractBytecode computes the bytecode hash in order to add it to the
// state-tree.
func hashContractBytecode(code []byte) ([]uint64, error) {
const (
bytecodeElementsHash = 8
bytecodeBytesElement = 7
maxBytesToAdd = bytecodeElementsHash * bytecodeBytesElement
)
// add 0x01
code = append(code, 0x01) // nolint:gomnd
// add padding
for len(code)%(56) != 0 { // nolint:gomnd
code = append(code, 0x00) // nolint:gomnd
}
code[len(code)-1] = code[len(code)-1] | 0x80 // nolint:gomnd
numHashes := int(math.Ceil(float64(len(code)) / float64(maxBytesToAdd)))
tmpHash := [4]uint64{}
var err error
bytesPointer := 0
for i := 0; i < numHashes; i++ {
elementsToHash := [12]uint64{}
for j := 0; j < 4; j++ {
elementsToHash[j] = tmpHash[j]
}
subsetBytecode := code[bytesPointer : int(math.Min(float64(len(code)-1), float64(bytesPointer+maxBytesToAdd)))+1]
bytesPointer += maxBytesToAdd
tmpElem := [7]byte{}
counter := 0
index := 4
for j := 0; j < maxBytesToAdd; j++ {
byteToAdd := []byte{0}
if j < len(subsetBytecode) {
byteToAdd = subsetBytecode[j : j+1]
}
tmpElem[bytecodeBytesElement-1-counter] = byteToAdd[0]
counter++
if counter == bytecodeBytesElement {
elementsToHash[index] = new(big.Int).SetBytes(tmpElem[:]).Uint64()
index++
tmpElem = [7]byte{}
counter = 0
}
}
tmpHash, err = poseidon.Hash([8]uint64{
elementsToHash[4],
elementsToHash[5],
elementsToHash[6],
elementsToHash[7],
elementsToHash[8],
elementsToHash[9],
elementsToHash[10],
elementsToHash[11],
}, [4]uint64{
elementsToHash[0],
elementsToHash[1],
elementsToHash[2],
elementsToHash[3],
})
if err != nil {
return nil, err
}
}
return tmpHash[:], nil
}
// KeyCodeLength returns the key of code length leaf:
// hk0: H([0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0])
// key: H([ethAddr[0:4], ethAddr[4:8], ethAddr[8:12], ethAddr[12:16], ethAddr[16:20], 0, 4, 0], [hk0[0], hk0[1], hk0[2], hk0[3]]
func KeyCodeLength(ethAddr common.Address) ([]byte, error) {
capIn, err := defaultCapIn()
if err != nil {
return nil, err
}
return keyEthAddr(ethAddr, LeafTypeSCLength, capIn)
}