@@ -45,16 +45,16 @@ type IPAProof struct {
45
45
}
46
46
47
47
type VerkleProof struct {
48
- OtherStems [][31 ]byte `json:"otherStems"`
49
- DepthExtensionPresent []byte `json:"depthExtensionPresent"`
50
- CommitmentsByPath [][32 ]byte `json:"commitmentsByPath"`
51
- D [32 ]byte `json:"d"`
52
- IPAProof * IPAProof `json:"ipa_proof"`
48
+ OtherStems [][StemSize ]byte `json:"otherStems"`
49
+ DepthExtensionPresent []byte `json:"depthExtensionPresent"`
50
+ CommitmentsByPath [][32 ]byte `json:"commitmentsByPath"`
51
+ D [32 ]byte `json:"d"`
52
+ IPAProof * IPAProof `json:"ipa_proof"`
53
53
}
54
54
55
55
func (vp * VerkleProof ) Copy () * VerkleProof {
56
56
ret := & VerkleProof {
57
- OtherStems : make ([][31 ]byte , len (vp .OtherStems )),
57
+ OtherStems : make ([][StemSize ]byte , len (vp .OtherStems )),
58
58
DepthExtensionPresent : make ([]byte , len (vp .DepthExtensionPresent )),
59
59
CommitmentsByPath : make ([][32 ]byte , len (vp .CommitmentsByPath )),
60
60
IPAProof : & IPAProof {},
@@ -77,7 +77,7 @@ type Proof struct {
77
77
Multipoint * ipa.MultiProof // multipoint argument
78
78
ExtStatus []byte // the extension status of each stem
79
79
Cs []* Point // commitments, sorted by their path in the tree
80
- PoaStems [][] byte // stems proving another stem is absent
80
+ PoaStems []Stem // stems proving another stem is absent
81
81
Keys [][]byte
82
82
PreValues [][]byte
83
83
PostValues [][]byte
@@ -92,7 +92,7 @@ type SuffixStateDiff struct {
92
92
type SuffixStateDiffs []SuffixStateDiff
93
93
94
94
type StemStateDiff struct {
95
- Stem [31 ]byte `json:"stem"`
95
+ Stem [StemSize ]byte `json:"stem"`
96
96
SuffixDiffs SuffixStateDiffs `json:"suffixDiffs"`
97
97
}
98
98
@@ -118,15 +118,15 @@ func (sd StateDiff) Copy() StateDiff {
118
118
return ret
119
119
}
120
120
121
- func GetCommitmentsForMultiproof (root VerkleNode , keys [][]byte , resolver NodeResolverFn ) (* ProofElements , []byte , [][] byte , error ) {
121
+ func GetCommitmentsForMultiproof (root VerkleNode , keys [][]byte , resolver NodeResolverFn ) (* ProofElements , []byte , []Stem , error ) {
122
122
sort .Sort (keylist (keys ))
123
123
return root .GetProofItems (keylist (keys ), resolver )
124
124
}
125
125
126
126
// getProofElementsFromTree factors the logic that is used both in the proving and verification methods. It takes a pre-state
127
127
// tree and an optional post-state tree, extracts the proof data from them and returns all the items required to build/verify
128
128
// a proof.
129
- func getProofElementsFromTree (preroot , postroot VerkleNode , keys [][]byte , resolver NodeResolverFn ) (* ProofElements , []byte , [][] byte , [][]byte , error ) {
129
+ func getProofElementsFromTree (preroot , postroot VerkleNode , keys [][]byte , resolver NodeResolverFn ) (* ProofElements , []byte , []Stem , [][]byte , error ) {
130
130
// go-ipa won't accept no key as an input, catch this corner case
131
131
// and return an empty result.
132
132
if len (keys ) == 0 {
@@ -228,7 +228,7 @@ func VerifyVerkleProof(proof *Proof, Cs []*Point, indices []uint8, ys []*Fr, tc
228
228
// * Multipoint proof
229
229
// it also returns the serialized keys and values
230
230
func SerializeProof (proof * Proof ) (* VerkleProof , StateDiff , error ) {
231
- otherstems := make ([][31 ]byte , len (proof .PoaStems ))
231
+ otherstems := make ([][StemSize ]byte , len (proof .PoaStems ))
232
232
for i , stem := range proof .PoaStems {
233
233
copy (otherstems [i ][:], stem )
234
234
}
@@ -251,12 +251,13 @@ func SerializeProof(proof *Proof) (*VerkleProof, StateDiff, error) {
251
251
var stemdiff * StemStateDiff
252
252
var statediff StateDiff
253
253
for i , key := range proof .Keys {
254
- if stemdiff == nil || ! bytes .Equal (stemdiff .Stem [:], key [:31 ]) {
254
+ stem := KeyToStem (key )
255
+ if stemdiff == nil || ! bytes .Equal (stemdiff .Stem [:], stem ) {
255
256
statediff = append (statediff , StemStateDiff {})
256
257
stemdiff = & statediff [len (statediff )- 1 ]
257
- copy (stemdiff .Stem [:], key [: 31 ] )
258
+ copy (stemdiff .Stem [:], stem )
258
259
}
259
- stemdiff .SuffixDiffs = append (stemdiff .SuffixDiffs , SuffixStateDiff {Suffix : key [31 ]})
260
+ stemdiff .SuffixDiffs = append (stemdiff .SuffixDiffs , SuffixStateDiff {Suffix : key [StemSize ]})
260
261
newsd := & stemdiff .SuffixDiffs [len (stemdiff .SuffixDiffs )- 1 ]
261
262
262
263
var valueLen = len (proof .PreValues [i ])
@@ -302,14 +303,15 @@ func SerializeProof(proof *Proof) (*VerkleProof, StateDiff, error) {
302
303
// can be used to rebuild a stateless version of the tree.
303
304
func DeserializeProof (vp * VerkleProof , statediff StateDiff ) (* Proof , error ) {
304
305
var (
305
- poaStems , keys [][]byte
306
+ poaStems []Stem
307
+ keys [][]byte
306
308
prevalues , postvalues [][]byte
307
309
extStatus []byte
308
310
commitments []* Point
309
311
multipoint ipa.MultiProof
310
312
)
311
313
312
- poaStems = make ([][] byte , len (vp .OtherStems ))
314
+ poaStems = make ([]Stem , len (vp .OtherStems ))
313
315
for i , poaStem := range vp .OtherStems {
314
316
poaStems [i ] = make ([]byte , len (poaStem ))
315
317
copy (poaStems [i ], poaStem [:])
@@ -347,8 +349,8 @@ func DeserializeProof(vp *VerkleProof, statediff StateDiff) (*Proof, error) {
347
349
for _ , stemdiff := range statediff {
348
350
for _ , suffixdiff := range stemdiff .SuffixDiffs {
349
351
var k [32 ]byte
350
- copy (k [:31 ], stemdiff .Stem [:])
351
- k [31 ] = suffixdiff .Suffix
352
+ copy (k [:StemSize ], stemdiff .Stem [:])
353
+ k [StemSize ] = suffixdiff .Suffix
352
354
keys = append (keys , k [:])
353
355
if suffixdiff .CurrentValue != nil {
354
356
prevalues = append (prevalues , suffixdiff .CurrentValue [:])
@@ -394,8 +396,9 @@ func PreStateTreeFromProof(proof *Proof, rootC *Point) (VerkleNode, error) { //
394
396
}
395
397
stems := make ([][]byte , 0 , len (proof .Keys ))
396
398
for _ , k := range proof .Keys {
397
- if len (stems ) == 0 || ! bytes .Equal (stems [len (stems )- 1 ], k [:31 ]) {
398
- stems = append (stems , k [:31 ])
399
+ stem := KeyToStem (k )
400
+ if len (stems ) == 0 || ! bytes .Equal (stems [len (stems )- 1 ], stem ) {
401
+ stems = append (stems , stem )
399
402
}
400
403
}
401
404
if len (stems ) != len (proof .ExtStatus ) {
@@ -469,10 +472,10 @@ func PreStateTreeFromProof(proof *Proof, rootC *Point) (VerkleNode, error) { //
469
472
si .values = map [byte ][]byte {}
470
473
si .stem = stems [i ]
471
474
for j , k := range proof .Keys { // TODO: DoS risk, use map or binary search.
472
- if bytes .Equal (k [: 31 ] , si .stem ) {
473
- si .values [k [31 ]] = proof .PreValues [j ]
474
- si .has_c1 = si .has_c1 || (k [31 ] < 128 )
475
- si .has_c2 = si .has_c2 || (k [31 ] >= 128 )
475
+ if bytes .Equal (KeyToStem ( k ) , si .stem ) {
476
+ si.values [k [StemSize ]] = proof .PreValues [j ]
477
+ si .has_c1 = si .has_c1 || (k [StemSize ] < 128 )
478
+ si .has_c2 = si .has_c2 || (k [StemSize ] >= 128 )
476
479
}
477
480
}
478
481
default :
@@ -501,8 +504,8 @@ func PreStateTreeFromProof(proof *Proof, rootC *Point) (VerkleNode, error) { //
501
504
continue
502
505
}
503
506
504
- if bytes .Equal (k [: 31 ] , info [string (p )].stem ) {
505
- values [k [31 ]] = proof .PreValues [i ]
507
+ if bytes .Equal (KeyToStem ( k ) , info [string (p )].stem ) {
508
+ values [k [StemSize ]] = proof .PreValues [i ]
506
509
}
507
510
}
508
511
comms , err = root .CreatePath (p , info [string (p )], comms , values )
@@ -535,8 +538,8 @@ func PostStateTreeFromStateDiff(preroot VerkleNode, statediff StateDiff) (Verkle
535
538
}
536
539
537
540
if overwrites {
538
- var stem [31 ]byte
539
- copy (stem [:31 ], stemstatediff .Stem [:])
541
+ var stem [StemSize ]byte
542
+ copy (stem [:StemSize ], stemstatediff .Stem [:])
540
543
if err := postroot .(* InternalNode ).InsertValuesAtStem (stem [:], values , nil ); err != nil {
541
544
return nil , fmt .Errorf ("error overwriting value in post state: %w" , err )
542
545
}
@@ -547,7 +550,7 @@ func PostStateTreeFromStateDiff(preroot VerkleNode, statediff StateDiff) (Verkle
547
550
return postroot , nil
548
551
}
549
552
550
- type bytesSlice [][] byte
553
+ type bytesSlice []Stem
551
554
552
555
func (x bytesSlice ) Len () int { return len (x ) }
553
556
func (x bytesSlice ) Less (i , j int ) bool { return bytes .Compare (x [i ], x [j ]) < 0 }
0 commit comments