13
13
)
14
14
15
15
type ProofNode interface {
16
- Hash (hash hashFunc ) * felt.Felt
16
+ Hash (hash HashFunc ) * felt.Felt
17
17
Len () uint8
18
18
PrettyPrint ()
19
19
}
@@ -23,7 +23,7 @@ type Binary struct {
23
23
RightHash * felt.Felt
24
24
}
25
25
26
- func (b * Binary ) Hash (hash hashFunc ) * felt.Felt {
26
+ func (b * Binary ) Hash (hash HashFunc ) * felt.Felt {
27
27
return hash (b .LeftHash , b .RightHash )
28
28
}
29
29
@@ -42,7 +42,7 @@ type Edge struct {
42
42
Path * Key // path from parent to child
43
43
}
44
44
45
- func (e * Edge ) Hash (hash hashFunc ) * felt.Felt {
45
+ func (e * Edge ) Hash (hash HashFunc ) * felt.Felt {
46
46
length := make ([]byte , len (e .Path .bitset ))
47
47
length [len (e .Path .bitset )- 1 ] = e .Path .len
48
48
pathFelt := e .Path .Felt ()
@@ -54,6 +54,11 @@ func (e *Edge) Len() uint8 {
54
54
return e .Path .Len ()
55
55
}
56
56
57
+ func (e * Edge ) PathInt () uint64 {
58
+ f := e .Path .Felt ()
59
+ return f .Uint64 ()
60
+ }
61
+
57
62
func (e * Edge ) PrettyPrint () {
58
63
fmt .Printf (" Edge:\n " )
59
64
fmt .Printf (" Child: %v\n " , e .Child )
@@ -199,7 +204,7 @@ func traverseNodes(currNode ProofNode, path *[]ProofNode, nodeHashes map[felt.Fe
199
204
// merges paths in the specified order [commonNodes..., leftNodes..., rightNodes...]
200
205
// ordering of the merged path is not important
201
206
// since SplitProofPath can discover the left and right paths using the merged path and the rootHash
202
- func MergeProofPaths (leftPath , rightPath []ProofNode , hash hashFunc ) ([]ProofNode , * felt.Felt , error ) {
207
+ func MergeProofPaths (leftPath , rightPath []ProofNode , hash HashFunc ) ([]ProofNode , * felt.Felt , error ) {
203
208
merged := []ProofNode {}
204
209
minLen := min (len (leftPath ), len (rightPath ))
205
210
@@ -236,7 +241,7 @@ func MergeProofPaths(leftPath, rightPath []ProofNode, hash hashFunc) ([]ProofNod
236
241
// SplitProofPath splits the merged proof path into two paths (left and right), which were merged before
237
242
// it first validates that the merged path is not circular, the split happens at most once and rootHash exists
238
243
// then calls traverseNodes to split the path to left and right paths
239
- func SplitProofPath (mergedPath []ProofNode , rootHash * felt.Felt , hash hashFunc ) ([]ProofNode , []ProofNode , error ) {
244
+ func SplitProofPath (mergedPath []ProofNode , rootHash * felt.Felt , hash HashFunc ) ([]ProofNode , []ProofNode , error ) {
240
245
commonPath := []ProofNode {}
241
246
leftPath := []ProofNode {}
242
247
rightPath := []ProofNode {}
@@ -316,7 +321,7 @@ func GetProof(key *Key, tri *Trie) ([]ProofNode, error) {
316
321
317
322
// verifyProof checks if `leafPath` leads from `root` to `leafHash` along the `proofNodes`
318
323
// https://github.com/eqlabs/pathfinder/blob/main/crates/merkle-tree/src/tree.rs#L2006
319
- func VerifyProof (root * felt.Felt , key * Key , value * felt.Felt , proofs []ProofNode , hash hashFunc ) bool {
324
+ func VerifyProof (root * felt.Felt , key * Key , value * felt.Felt , proofs []ProofNode , hash HashFunc ) bool {
320
325
expectedHash := root
321
326
remainingPath := NewKey (key .len , key .bitset [:])
322
327
for i , proofNode := range proofs {
@@ -345,7 +350,7 @@ func VerifyProof(root *felt.Felt, key *Key, value *felt.Felt, proofs []ProofNode
345
350
return true
346
351
}
347
352
348
- if ! proofNode .Path .Equal (subKey ) {
353
+ if ! proofNode .Path .Equal (subKey ) && ! subKey . Equal ( & Key {}) {
349
354
return false
350
355
}
351
356
expectedHash = proofNode .Child
@@ -363,7 +368,7 @@ func VerifyProof(root *felt.Felt, key *Key, value *felt.Felt, proofs []ProofNode
363
368
// and therefore it's hash won't match the expected root.
364
369
// ref: https://github.com/ethereum/go-ethereum/blob/v1.14.3/trie/proof.go#L484
365
370
func VerifyRangeProof (root * felt.Felt , keys , values []* felt.Felt , proofKeys [2 ]* Key , proofValues [2 ]* felt.Felt ,
366
- proofs [2 ][]ProofNode , hash hashFunc ,
371
+ proofs [2 ][]ProofNode , hash HashFunc ,
367
372
) (bool , error ) {
368
373
// Step 0: checks
369
374
if len (keys ) != len (values ) {
@@ -440,7 +445,7 @@ func ensureMonotonicIncreasing(proofKeys [2]*Key, keys []*felt.Felt) error {
440
445
}
441
446
442
447
// compressNode determines if the node needs compressed, and if so, the len needed to arrive at the next key
443
- func compressNode (idx int , proofNodes []ProofNode , hashF hashFunc ) (int , uint8 , error ) {
448
+ func compressNode (idx int , proofNodes []ProofNode , hashF HashFunc ) (int , uint8 , error ) {
444
449
parent := proofNodes [idx ]
445
450
446
451
if idx == len (proofNodes )- 1 {
@@ -474,7 +479,7 @@ func compressNode(idx int, proofNodes []ProofNode, hashF hashFunc) (int, uint8,
474
479
}
475
480
476
481
func assignChild (i , compressedParent int , parentNode * Node ,
477
- nilKey , leafKey , parentKey * Key , proofNodes []ProofNode , hashF hashFunc ,
482
+ nilKey , leafKey , parentKey * Key , proofNodes []ProofNode , hashF HashFunc ,
478
483
) (* Key , error ) {
479
484
childInd := i + compressedParent + 1
480
485
childKey , err := getChildKey (childInd , parentKey , leafKey , nilKey , proofNodes , hashF )
@@ -494,7 +499,7 @@ func assignChild(i, compressedParent int, parentNode *Node,
494
499
// ProofToPath returns a set of storage nodes from the root to the end of the proof path.
495
500
// The storage nodes will have the hashes of the children, but only the key of the child
496
501
// along the path outlined by the proof.
497
- func ProofToPath (proofNodes []ProofNode , leafKey * Key , hashF hashFunc ) ([]StorageNode , error ) {
502
+ func ProofToPath (proofNodes []ProofNode , leafKey * Key , hashF HashFunc ) ([]StorageNode , error ) {
498
503
pathNodes := []StorageNode {}
499
504
500
505
// Child keys that can't be derived are set to nilKey, so that we can store the node
@@ -552,7 +557,7 @@ func ProofToPath(proofNodes []ProofNode, leafKey *Key, hashF hashFunc) ([]Storag
552
557
return pathNodes , nil
553
558
}
554
559
555
- func skipNode (pNode ProofNode , pathNodes []StorageNode , hashF hashFunc ) bool {
560
+ func skipNode (pNode ProofNode , pathNodes []StorageNode , hashF HashFunc ) bool {
556
561
lastNode := pathNodes [len (pathNodes )- 1 ].node
557
562
noLeftMatch , noRightMatch := false , false
558
563
if lastNode .LeftHash != nil && ! pNode .Hash (hashF ).Equal (lastNode .LeftHash ) {
@@ -607,7 +612,7 @@ func getParentKey(idx int, compressedParentOffset uint8, leafKey *Key,
607
612
return crntKey , err
608
613
}
609
614
610
- func getChildKey (childIdx int , crntKey , leafKey , nilKey * Key , proofNodes []ProofNode , hashF hashFunc ) (* Key , error ) {
615
+ func getChildKey (childIdx int , crntKey , leafKey , nilKey * Key , proofNodes []ProofNode , hashF HashFunc ) (* Key , error ) {
611
616
if childIdx > len (proofNodes )- 1 {
612
617
return nilKey , nil
613
618
}
0 commit comments