forked from ledgerwatch/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stark_stats.go
264 lines (240 loc) · 6.63 KB
/
stark_stats.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
package trie
import (
"fmt"
"io"
"math/bits"
"sort"
"github.com/holiman/uint256"
libcommon "github.com/nebojsa94/erigon/erigon-lib/common"
len2 "github.com/nebojsa94/erigon/erigon-lib/common/length"
"github.com/nebojsa94/erigon/core/types/accounts"
"github.com/nebojsa94/erigon/turbo/rlphacks"
)
type StarkStatsBuilder struct {
keccakCounter int // Number of Keccak invocations
perInputSize map[int]int // Number of invocation for certain size of input
sizeStack []int // Stack of input sizes
}
func NewStarkStatsBuilder() *StarkStatsBuilder {
return &StarkStatsBuilder{
keccakCounter: 0,
perInputSize: make(map[int]int),
}
}
func (hb *StarkStatsBuilder) leafHash(length int, keyHex []byte, val rlphacks.RlpSerializable) error {
key := keyHex[len(keyHex)-length:]
var compactLen int
var kp, kl int
if hasTerm(key) {
compactLen = (len(key)-1)/2 + 1
} else {
compactLen = len(key)/2 + 1
}
if compactLen > 1 {
kp = 1
kl = compactLen
} else {
kl = 1
}
totalLen := kp + kl + val.DoubleRLPLen()
var lenPrefix [4]byte
pt := rlphacks.GenerateStructLen(lenPrefix[:], totalLen)
inputSize := totalLen + pt
if inputSize > len2.Hash {
inputSize = 32
}
hb.sizeStack = append(hb.sizeStack, inputSize)
return nil
}
func (hb *StarkStatsBuilder) leaf(length int, keyHex []byte, val rlphacks.RlpSerializable) error {
return hb.leafHash(length, keyHex, val)
}
func (hb *StarkStatsBuilder) extensionHash(key []byte) error {
var kp, kl int
var compactLen int
if hasTerm(key) {
compactLen = (len(key)-1)/2 + 1
} else {
compactLen = len(key)/2 + 1
}
if compactLen > 1 {
kp = 1
kl = compactLen
} else {
kl = 1
}
totalLen := kp + kl + 33
var lenPrefix [4]byte
pt := rlphacks.GenerateStructLen(lenPrefix[:], totalLen)
inputSize := pt + totalLen
hb.keccakCounter++
hb.perInputSize[inputSize]++
hb.sizeStack[len(hb.sizeStack)-1] = 32
return nil
}
func (hb *StarkStatsBuilder) extension(key []byte) error {
return hb.extensionHash(key)
}
func (hb *StarkStatsBuilder) branchHash(set uint16) error {
digits := bits.OnesCount16(set)
inputSizes := hb.sizeStack[len(hb.sizeStack)-digits:]
totalLen := 17 // These are 17 length prefixes
var i int
for digit := uint(0); digit < 16; digit++ {
if ((1 << digit) & set) != 0 {
totalLen += inputSizes[i]
i++
}
}
var lenPrefix [4]byte
pt := rlphacks.GenerateStructLen(lenPrefix[:], totalLen)
inputSize := pt + totalLen
hb.keccakCounter++
hb.perInputSize[inputSize]++
hb.sizeStack = hb.sizeStack[:len(hb.sizeStack)-digits+1]
hb.sizeStack[len(hb.sizeStack)-1] = 32
return nil
}
func (hb *StarkStatsBuilder) branch(set uint16) error {
return hb.branchHash(set)
}
func (hb *StarkStatsBuilder) hash(_ libcommon.Hash) {
hb.sizeStack = append(hb.sizeStack, 32)
}
func (hb *StarkStatsBuilder) code(_ []byte) libcommon.Hash {
hb.sizeStack = append(hb.sizeStack, 32)
return libcommon.Hash{}
}
func (hb *StarkStatsBuilder) accountLeafHash(length int, keyHex []byte, _ uint64, balance *uint256.Int, nonce uint64, fieldSet uint32) (err error) {
key := keyHex[len(keyHex)-length:]
var acc accounts.Account
acc.Root = EmptyRoot
acc.CodeHash = EmptyCodeHash
acc.Nonce = nonce
acc.Balance.Set(balance)
acc.Initialised = true
if fieldSet&uint32(4) == 0 && fieldSet&uint32(8) == 0 {
// In this case we can precompute the hash of the entire account leaf
hb.sizeStack = append(hb.sizeStack, 32)
} else {
if fieldSet&uint32(4) != 0 {
hb.sizeStack = hb.sizeStack[:len(hb.sizeStack)-1]
}
if fieldSet&uint32(8) != 0 {
hb.sizeStack = hb.sizeStack[:len(hb.sizeStack)-1]
}
}
var kp, kl int
var compactLen int
if hasTerm(key) {
compactLen = (len(key)-1)/2 + 1
} else {
compactLen = len(key)/2 + 1
}
if compactLen > 1 {
kp = 1
kl = compactLen
} else {
kl = 1
}
valLen := acc.EncodingLengthForHashing()
valBuf := make([]byte, valLen)
acc.EncodeForHashing(valBuf)
val := rlphacks.RlpEncodedBytes(valBuf)
totalLen := kp + kl + val.DoubleRLPLen()
var lenPrefix [4]byte
pt := rlphacks.GenerateStructLen(lenPrefix[:], totalLen)
inputSize := pt + totalLen
hb.keccakCounter++
hb.perInputSize[inputSize]++
hb.sizeStack = append(hb.sizeStack, 32)
return nil
}
func (hb *StarkStatsBuilder) accountLeaf(length int, keyHex []byte, storageSize uint64, balance *uint256.Int, nonce uint64, _ uint64, fieldSet uint32) (err error) {
return hb.accountLeafHash(length, keyHex, storageSize, balance, nonce, fieldSet)
}
func (hb *StarkStatsBuilder) emptyRoot() {
hb.sizeStack = append(hb.sizeStack, 32)
}
// StarkStats collects Keccak256 stats from the witness and write them into the file
func StarkStats(witness *Witness, w io.Writer, trace bool) error {
hb := NewStarkStatsBuilder()
for _, operator := range witness.Operators {
switch op := operator.(type) {
case *OperatorLeafValue:
if trace {
fmt.Printf("LEAF ")
}
keyHex := op.Key
val := op.Value
if err := hb.leaf(len(op.Key), keyHex, rlphacks.RlpSerializableBytes(val)); err != nil {
return err
}
case *OperatorExtension:
if trace {
fmt.Printf("EXTENSION ")
}
if err := hb.extension(op.Key); err != nil {
return err
}
case *OperatorBranch:
if trace {
fmt.Printf("BRANCH ")
}
if err := hb.branch(uint16(op.Mask)); err != nil {
return err
}
case *OperatorHash:
if trace {
fmt.Printf("HASH ")
}
hb.hash(op.Hash)
case *OperatorCode:
if trace {
fmt.Printf("CODE ")
}
hb.code(op.Code)
case *OperatorLeafAccount:
if trace {
fmt.Printf("ACCOUNTLEAF(code=%v storage=%v) ", op.HasCode, op.HasStorage)
}
balance := uint256.NewInt(0)
balance.SetBytes(op.Balance.Bytes())
nonce := op.Nonce
// FIXME: probably not needed, fix hb.accountLeaf
fieldSet := uint32(3)
if op.HasCode && op.HasStorage {
fieldSet = 15
}
// Incarnation is always needed for a hashbuilder.
// but it is just our implementation detail needed for contract self-destruction support with our
// db structure. Stateless clients don't access the DB so we can just pass 0 here.
incarnation := uint64(0)
if err := hb.accountLeaf(len(op.Key), op.Key, 0, balance, nonce, incarnation, fieldSet); err != nil {
return err
}
case *OperatorEmptyRoot:
if trace {
fmt.Printf("EMPTYROOT ")
}
hb.emptyRoot()
default:
return fmt.Errorf("unknown operand type: %T", operator)
}
}
if trace {
fmt.Printf("\n")
}
inputSizes := make([]int, len(hb.perInputSize))
i := 0
for inputSize := range hb.perInputSize {
inputSizes[i] = inputSize
i++
}
sort.Ints(inputSizes)
fmt.Fprintf(w, "%d\n", hb.keccakCounter)
for _, inputSize := range inputSizes {
fmt.Fprintf(w, "%d %d\n", inputSize, hb.perInputSize[inputSize])
}
return nil
}