Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating verify proof range to handle empty proof keys #1901

Merged
merged 32 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5cd826f
updating verify proof range to handle empty proof keys
Jun 7, 2024
3ecb395
test non set proof key - wip
Jun 7, 2024
d2fe491
wip - proof to Path doesn't work
Jun 7, 2024
2cd3157
store the hashes of children in ProofToPath
Jun 8, 2024
3603e28
ProoftoPath update to handle unset proof key
Jun 8, 2024
d8f5811
test - wip
Jun 8, 2024
40a3d04
test - wip
Jun 9, 2024
fb6c899
test passes! :D
Jun 10, 2024
07e99f5
lint
Jun 10, 2024
c0b933a
tidy
Jun 10, 2024
529aef2
fixing tests that broke wip
Jun 10, 2024
ca360e9
build4keyTrie passes again!
Jun 11, 2024
fdf6af2
fixed more tests
Jun 11, 2024
9f9f2fe
fix test wip
Jun 11, 2024
bad7267
fix more tests
Jun 11, 2024
92a98df
fix test -wip
Jun 11, 2024
727801b
fix tests wip
Jun 11, 2024
b4ae2b6
wip
Jun 11, 2024
a123730
all tests passgit add .
Jun 12, 2024
d249186
lint
Jun 12, 2024
21f9453
tidy
Jun 12, 2024
2d7c6e8
tidy logic
Jun 12, 2024
46c7b8a
tidy logic
Jun 12, 2024
0bb2edc
tidy logic
Jun 12, 2024
ce20e08
comment: update getLeftRightHash
Jun 12, 2024
ec2f917
update ProofToPath comment
Jun 12, 2024
dd21cc0
bubble up getLeftRightHash err
Jun 12, 2024
e4aafd1
Merge branch 'main' into rianhughes/proof-range-test
rianhughes Jun 25, 2024
1004a9e
Merge branch 'main' into rianhughes/proof-range-test
rianhughes Jun 25, 2024
dfbedca
Start using Artifactory for CI/CD in favour of Docker Registry
derrix060 Jun 25, 2024
83f5c08
Merge branch 'main' into rianhughes/proof-range-test
rianhughes Jun 28, 2024
f3b20cd
Merge branch 'main' into rianhughes/proof-range-test
rianhughes Jun 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test non set proof key - wip
  • Loading branch information
rian committed Jun 12, 2024
commit 3ecb39591e7f566714c4d24e1361d020d514d282
12 changes: 10 additions & 2 deletions core/trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func transformNode(tri *Trie, parentKey *Key, sNode StorageNode) (*Edge, *Binary
}

// https://github.com/eqlabs/pathfinder/blob/main/crates/merkle-tree/src/tree.rs#L514
func GetProof(key *Key, tri *Trie) ([]ProofNode, error) {
func GetProof(key *Key, tri *Trie) ([]ProofNode, error) { // todo: Get proof should always be for leafs??
nodesFromRoot, err := tri.nodesFromRoot(key)
if err != nil {
return nil, err
Expand Down Expand Up @@ -171,10 +171,11 @@ func GetProof(key *Key, tri *Trie) ([]ProofNode, error) {
func VerifyProof(root *felt.Felt, key *Key, value *felt.Felt, proofs []ProofNode, hash hashFunc) bool {
expectedHash := root
remainingPath := NewKey(key.len, key.bitset[:])
for _, proofNode := range proofs {
for i, proofNode := range proofs {
if !proofNode.Hash(hash).Equal(expectedHash) {
return false
}

switch {
case proofNode.Binary != nil:
if remainingPath.Test(remainingPath.Len() - 1) {
Expand All @@ -188,6 +189,13 @@ func VerifyProof(root *felt.Felt, key *Key, value *felt.Felt, proofs []ProofNode
if err != nil {
return false
}

// If we are verifying the key doesn't exist, then we should
// update subKey to point in the other direction
if value == nil && i == len(proofs)-1 {
return true // todo: hack for non-set proof nodes
}

if !proofNode.Edge.Path.Equal(subKey) {
return false
}
Expand Down
77 changes: 77 additions & 0 deletions core/trie/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,59 @@ func build3KeyTrie(t *testing.T) *trie.Trie {
return tempTrie
}

func build4KeyTrie(t *testing.T) *trie.Trie {
// Juno
// 248
// / \
// 249 \
// / \ \
// 250 \ \
// / \ \ \
// 0 1 2 4

// Pathfinder (???)
// 0 Edge
// |
// 248 Binary
// / \
// 249 250 Binary Edge ??
// / \ \
// 250 250 \ Binary Edge ??
// / \ \ \
// 0 1 2 4

// Build trie
memdb := pebble.NewMemTest(t)
txn, err := memdb.NewTransaction(true)
require.NoError(t, err)

tempTrie, err := trie.NewTriePedersen(trie.NewStorage(txn, []byte{0}), 251)
require.NoError(t, err)

// Update trie
key1 := new(felt.Felt).SetUint64(0)
key2 := new(felt.Felt).SetUint64(1)
key3 := new(felt.Felt).SetUint64(2)
key5 := new(felt.Felt).SetUint64(4)
value1 := new(felt.Felt).SetUint64(4)
value2 := new(felt.Felt).SetUint64(5)
value3 := new(felt.Felt).SetUint64(6)
value5 := new(felt.Felt).SetUint64(7)

_, err = tempTrie.Put(key1, value1)
require.NoError(t, err)

_, err = tempTrie.Put(key3, value3)
require.NoError(t, err)
_, err = tempTrie.Put(key2, value2)
require.NoError(t, err)
_, err = tempTrie.Put(key5, value5)
require.NoError(t, err)

require.NoError(t, tempTrie.Commit())

return tempTrie
}
func TestGetProof(t *testing.T) {
t.Run("Simple Trie - simple binary", func(t *testing.T) {
tempTrie := buildSimpleTrie(t)
Expand Down Expand Up @@ -784,4 +837,28 @@ func TestVerifyRangeProof(t *testing.T) {
require.NoError(t, err)
require.True(t, verif)
})

t.Run("left proof, all inner keys, right proof with non-set key", func(t *testing.T) {
zeroFeltBytes := new(felt.Felt).SetUint64(0).Bytes()
zeroLeafkey := trie.NewKey(251, zeroFeltBytes[:])

threeFeltBytes := new(felt.Felt).SetUint64(3).Bytes()
threeLeafkey := trie.NewKey(251, threeFeltBytes[:])

tri := build4KeyTrie(t)
keys := []*felt.Felt{new(felt.Felt).SetUint64(1), new(felt.Felt).SetUint64(2)}
values := []*felt.Felt{new(felt.Felt).SetUint64(5), new(felt.Felt).SetUint64(6)}
proofKeys := [2]*trie.Key{&zeroLeafkey, &threeLeafkey}
proofValues := [2]*felt.Felt{new(felt.Felt).SetUint64(4), nil}
leftProof, err := trie.GetProof(proofKeys[0], tri)
require.NoError(t, err)
rightProof, err := trie.GetProof(proofKeys[1], tri)
require.NoError(t, err)
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)
require.NoError(t, err)
require.True(t, verif)
})
}