Skip to content

Commit

Permalink
add right proof, all left key case
Browse files Browse the repository at this point in the history
  • Loading branch information
rian committed Jun 4, 2024
1 parent 73d7809 commit 79c9bcb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
7 changes: 3 additions & 4 deletions core/trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ func VerifyProof(root, keyFelt, value *felt.Felt, proofs []ProofNode, hash hashF
// If the trie is constructed incorrectly then the root will have an incorrect key(len,path), and value,
// and therefore it's hash will be incorrect.
// ref: https://github.com/ethereum/go-ethereum/blob/v1.14.3/trie/proof.go#L484
func VerifyRangeProof(root *felt.Felt, keys, values []*felt.Felt, proofKeys,
proofValues [2]*felt.Felt, proofs [2][]ProofNode, hash hashFunc,
func VerifyRangeProof(root *felt.Felt, keys, values []*felt.Felt, proofKeys, proofValues [2]*felt.Felt,
proofs [2][]ProofNode, hash hashFunc,
) (bool, error) {
// Step 0: checks
if len(keys) != len(values) {
Expand Down Expand Up @@ -243,7 +243,7 @@ func VerifyRangeProof(root *felt.Felt, keys, values []*felt.Felt, proofKeys,

if proofs[1] != nil {
if !VerifyProof(root, proofKeys[1], proofValues[1], proofs[1], hash) {
return false, fmt.Errorf("invalid proof for key %x", proofKeys[1].String())
return false, fmt.Errorf("invalid proof for key %x", proofKeys[0].String())

Check warning on line 246 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L246

Added line #L246 was not covered by tests
}

lastProofPath, err = ProofToPath(proofs[1], proofKeys[1], hash)
Expand All @@ -268,7 +268,6 @@ func VerifyRangeProof(root *felt.Felt, keys, values []*felt.Felt, proofKeys,
}

return true, nil

}

func ensureMonotonicIncreasing(proofKeys [2]*felt.Felt, keys []*felt.Felt) error {
Expand Down
21 changes: 21 additions & 0 deletions core/trie/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,4 +665,25 @@ func TestVerifyRangeProof(t *testing.T) {
require.NoError(t, err)
require.True(t, verif)
})

t.Run("right proof, all left keys", func(t *testing.T) {
// Node (edge path 249)
// / \
// Node (binary) 0x6 (leaf)
// / \
// 0x4 0x5 (leaf, leaf)
tri := build3KeyTrie(t)
keys := []*felt.Felt{new(felt.Felt).SetUint64(0), new(felt.Felt).SetUint64(1)}
values := []*felt.Felt{new(felt.Felt).SetUint64(4), new(felt.Felt).SetUint64(5)}
proofKeys := [2]*felt.Felt{nil, new(felt.Felt).SetUint64(2)}
proofValues := [2]*felt.Felt{nil, new(felt.Felt).SetUint64(6)}
rightProof, err := trie.GetProof(proofKeys[1], tri)
require.NoError(t, err)
proofs := [2][]trie.ProofNode{nil, rightProof}
rootCommitment, err := tri.Root()
require.NoError(t, err)
verif, err := trie.VerifyRangeProof(rootCommitment, keys, values, proofKeys, proofValues, proofs, crypto.Pedersen)
require.NoError(t, err)
require.True(t, verif)
})
}

0 comments on commit 79c9bcb

Please sign in to comment.