Skip to content

Commit

Permalink
test - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rian committed Jun 8, 2024
1 parent dfb0930 commit 4e4a11d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
17 changes: 15 additions & 2 deletions core/trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,12 @@ func BuildTrie(leftProofPath, rightProofPath []StorageNode, keys, values []*felt
}

// merge proof paths
for i := range min(len(leftProofPath), len(rightProofPath)) { //
for i := range min(len(leftProofPath), len(rightProofPath)) {
// Can't store nil keys so stop merging
if leftProofPath[i].node.Left == nil || leftProofPath[i].node.Right == nil ||
rightProofPath[i].node.Left == nil || rightProofPath[i].node.Right == nil {
break
}
if leftProofPath[i].key.Equal(rightProofPath[i].key) {
leftProofPath[i].node.Right = rightProofPath[i].node.Right
rightProofPath[i].node.Left = leftProofPath[i].node.Left
Expand All @@ -463,14 +468,22 @@ func BuildTrie(leftProofPath, rightProofPath []StorageNode, keys, values []*felt
}

for _, sNode := range leftProofPath {
// Can't store nil keys (reached end of line for proof)
if sNode.node.Left == nil || sNode.node.Right == nil {
break
}
_, err := tempTrie.PutInner(sNode.key, sNode.node)
if err != nil {
return nil, err
}
}

for _, sNode := range rightProofPath {
_, err := tempTrie.PutInner(sNode.key, sNode.node) // Todo: incorrectly sets the proof key
// Can't store nil keys (reached end of line for proof)
if sNode.node.Left == nil || sNode.node.Right == nil {
break
}
_, err := tempTrie.PutInner(sNode.key, sNode.node)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion core/trie/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,15 +866,17 @@ func TestVerifyRangeProof(t *testing.T) {
rightProof, err := trie.GetProof(proofKeys[1], tri) // Looks correct in terms of binary and edges
require.NoError(t, err)

// Todo: remove, just included for inspection/testing
leftProofPath, err := trie.ProofToPath(leftProof, proofKeys[0], proofValues[0], crypto.Pedersen) // correct
require.NoError(t, err)
rightProofPath, err := trie.ProofToPath(rightProof, proofKeys[1], proofValues[1], crypto.Pedersen) // gives the correct structure
require.NoError(t, err)
fmt.Println(leftProofPath, rightProofPath)

proofs := [2][]trie.ProofNode{leftProof, rightProof}
rootCommitment, err := tri.Root()
require.NoError(t, err)
verif, err := trie.VerifyRangeProof(rootCommitment, keys, values, proofKeys, proofValues, proofs, crypto.Pedersen)
verif, err := trie.VerifyRangeProof(rootCommitment, keys, values, proofKeys, proofValues, proofs, crypto.Pedersen) // todo: panics
require.NoError(t, err)
require.True(t, verif)
})
Expand Down
2 changes: 0 additions & 2 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,6 @@ func (t *Trie) insertOrUpdateValue(nodeKey *Key, node *Node, nodes []StorageNode
newParent.Value = t.hash(node.LeftHash, rightChild.Hash(&rightPath, t.hash))
} else if node.RightHash != nil {
newParent.Value = t.hash(leftChild.Hash(&leftPath, t.hash), node.RightHash)
} else {
return errors.New("proof node has neither right/left child hash")
}
} else {
newParent.Value = t.hash(leftChild.Hash(&leftPath, t.hash), rightChild.Hash(&rightPath, t.hash))
Expand Down

0 comments on commit 4e4a11d

Please sign in to comment.