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
all tests passgit add .
  • Loading branch information
rian committed Jun 12, 2024
commit a1237304d12801e26f113bd7816c7414717070e3
6 changes: 6 additions & 0 deletions core/trie/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func (n *Node) Hash(path *Key, hashFunc hashFunc) *felt.Felt {
return hash.Add(hash, &pathFelt)
}

// Hash calculates the hash of a [Node]
func (n *Node) HashFromParent(parnetKey, nodeKey *Key, hashFunc hashFunc) *felt.Felt {
path := path(nodeKey, parnetKey)
return n.Hash(&path, hashFunc)
}

func (n *Node) WriteTo(buf *bytes.Buffer) (int64, error) {
if n.Value == nil {
return 0, errors.New("cannot marshal node with nil value")
Expand Down
16 changes: 8 additions & 8 deletions core/trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,14 @@ func getLeftRightHash(parentId int, sqshdParentKey *Key, leafKey *Key, proofNode
leftHash = parentBinary.LeftHash
rightHash = parentBinary.RightHash

childID := shiftedParentId + 1
if childID <= len(proofNodes)-1 && proofNodes[childID].Edge != nil {
if leafKey.Test(leafKey.len - sqshdParentKey.len - 1) {
rightHash = proofNodes[childID].Edge.Child
} else {
leftHash = proofNodes[childID].Edge.Child
}
}
// childID := shiftedParentId + 1
// if childID <= len(proofNodes)-1 && proofNodes[childID].Edge != nil {
// if leafKey.Test(leafKey.len - sqshdParentKey.len - 1) {
// rightHash = proofNodes[childID].Edge.Child
// } else {
// leftHash = proofNodes[childID].Edge.Child
// }
// }
return leftHash, rightHash, nil
}

Expand Down
32 changes: 12 additions & 20 deletions core/trie/proof_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package trie_test

import (
"fmt"
"testing"

"github.com/NethermindEth/juno/core/crypto"
Expand Down Expand Up @@ -629,17 +628,17 @@ func TestProofToPath(t *testing.T) {
},
}

leafValue := utils.HexToFelt(t, "0xcc")
siblingValue := utils.HexToFelt(t, "0xdd")

sns, err := trie.ProofToPath(proofNodes, &leafkey, crypto.Pedersen)
require.NoError(t, err)

rootKey := tempTrie.RootKey()

rootNode, err := tempTrie.GetNodeFromKey(rootKey)
require.NoError(t, err)
leftNode, err := tempTrie.GetNodeFromKey(rootNode.Left)
require.NoError(t, err)
require.Equal(t, 1, len(sns))
require.Equal(t, rootKey.Len(), sns[0].Key().Len())
require.Equal(t, leafValue.String(), sns[0].Node().LeftHash.String())
require.Equal(t, leftNode.HashFromParent(rootKey, rootNode.Left, crypto.Pedersen).String(), sns[0].Node().LeftHash.String())
require.NotEqual(t, siblingValue.String(), sns[0].Node().RightHash.String())
})

Expand All @@ -655,21 +654,20 @@ func TestProofToPath(t *testing.T) {
oneLeafValue := new(felt.Felt).SetUint64(5)
twoFeltBytes := new(felt.Felt).SetUint64(2).Bytes()
twoLeafkey := trie.NewKey(251, twoFeltBytes[:])
twoLeafValue := new(felt.Felt).SetUint64(6)
bProofs, err := trie.GetBoundaryProofs(&zeroLeafkey, &twoLeafkey, tri)
require.NoError(t, err)

// Test 1
leftProofPath, err := trie.ProofToPath(bProofs[0], &zeroLeafkey, crypto.Pedersen)
require.Equal(t, 2, len(leftProofPath))
// require.NoError(t, err)
// left, err := tri.GetNodeFromKey(rootNode.Left)
// require.NoError(t, err)
// right, err := tri.GetNodeFromKey(rootNode.Right)
require.NoError(t, err)
left, err := tri.GetNodeFromKey(rootNode.Left)
require.NoError(t, err)
right, err := tri.GetNodeFromKey(rootNode.Right)
require.NoError(t, err)
require.Equal(t, rootKey, leftProofPath[0].Key())
// require.Equal(t, left.Hash(rootNode.Left, crypto.Pedersen).String(), leftProofPath[0].Node().LeftHash.String()) // Todo
// require.Equal(t, right.Hash(rootNode.Right, crypto.Pedersen).String(), leftProofPath[0].Node().RightHash.String()) // Todo
require.Equal(t, left.HashFromParent(rootKey, rootNode.Left, crypto.Pedersen).String(), leftProofPath[0].Node().LeftHash.String())
require.Equal(t, right.HashFromParent(rootKey, rootNode.Right, crypto.Pedersen).String(), leftProofPath[0].Node().RightHash.String())
require.Equal(t, rootNode.Left, leftProofPath[1].Key())
require.Equal(t, zeroLeafValue.String(), leftProofPath[1].Node().LeftHash.String())
require.Equal(t, oneLeafValue.String(), leftProofPath[1].Node().RightHash.String())
Expand All @@ -681,7 +679,7 @@ func TestProofToPath(t *testing.T) {
require.Equal(t, rootKey, rightProofPath[0].Key())
require.NotEqual(t, rootNode.Right, rightProofPath[0].Node().Right)
require.NotEqual(t, uint8(0), rightProofPath[0].Node().Right)
require.Equal(t, twoLeafValue.String(), rightProofPath[0].Node().RightHash.String())
require.Equal(t, right.HashFromParent(rootKey, rootNode.Right, crypto.Pedersen).String(), rightProofPath[0].Node().RightHash.String())
})
}

Expand All @@ -701,8 +699,6 @@ func TestBuildTrie(t *testing.T) {
require.NoError(t, err)
leftNode, err := tri.GetNodeFromKey(rootNode.Left)
require.NoError(t, err)
rightNode, err := tri.GetNodeFromKey(rootNode.Right)
require.NoError(t, err)
leftleftNode, err := tri.GetNodeFromKey(leftNode.Left)
require.NoError(t, err)
leftrightNode, err := tri.GetNodeFromKey(leftNode.Right)
Expand Down Expand Up @@ -744,14 +740,10 @@ func TestBuildTrie(t *testing.T) {
// Assert the leaf nodes have the correct values
require.Equal(t, leftleftNode.Value.String(), builtLeftNode.LeftHash.String(), "should be 0x4")
require.Equal(t, leftrightNode.Value.String(), builtLeftRightNode.Value.String(), "should be 0x5")
require.Equal(t, rightNode.Value.String(), builtRootNode.RightHash.String(), "should be 0x6")

// Given the above two asserts pass, we should be able to reconstruct the correct commitment
// Todo: The original tries right value doesn't seem correct (should be 0x6)
reconstructedRootCommitment, err := builtTrie.Root()
fmt.Println("rootNode.Value.String()", rootNode.Value.String(), rootCommitment.String())
require.NoError(t, err)
require.Equal(t, rootNode.Value.String(), builtRootNode.Value.String(), "rootNode.Value not equal")
require.Equal(t, rootCommitment.String(), reconstructedRootCommitment.String(), "root commitment not equal")
})
}
Expand Down
2 changes: 2 additions & 0 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ func (t *Trie) updateValueIfDirty(key *Key) (*Node, error) {
defer nodePool.Put(rightChild)
rightHash = rightChild.Hash(&rightPath, t.hash)
}
fmt.Println("node.Value", node.Value.String(), key)
node.Value = t.hash(leftHash, rightHash)
fmt.Println("node.Value", node.Value.String(), key)
fmt.Println(key.String(), leftHash.String(), rightHash.String(), node.Value.String()) // Todo: rightHash should be 0x6 on original trie??
if err = t.storage.Put(key, node); err != nil {
return nil, err
Expand Down