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
lint
  • Loading branch information
rian committed Jun 12, 2024
commit d249186d5e2fb7e1bfa76ec2d92c127978ecef70
105 changes: 31 additions & 74 deletions core/trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,6 @@ func VerifyRangeProof(root *felt.Felt, keys, values []*felt.Felt, proofKeys [2]*
if err != nil {
return false, err
}
err = tmpTrie.Commit()
if err != nil {
return false, err
}

// Verify that the recomputed root hash matches the provided root hash
recomputedRoot, err := tmpTrie.Root()
Expand Down Expand Up @@ -304,7 +300,6 @@ func shouldSquish(idx int, proofNodes []ProofNode, hashF hashFunc) (int, uint8,
if parent.Edge != nil {
hack = 1
} else if parent.Binary != nil {

hack = 0
}
return hack, parent.Len(), nil
Expand Down Expand Up @@ -347,7 +342,7 @@ func assignChild(crntNode *Node, nilKey, childKey *Key, isRight bool) {
// ProofToPath returns a set of storage nodes from the root to the end of the proof path.
// It will contain the hashes of the children along the path, but only the key of the children
// along the path. The final node must contain the hash of the leaf if the leaf is set.
// It will not contain the leaf node even if it is set. // Todo
// It will not contain the leaf node even if it is set.
func ProofToPath(proofNodes []ProofNode, leafKey *Key, hashF hashFunc) ([]StorageNode, error) {
pathNodes := []StorageNode{}

Expand Down Expand Up @@ -381,41 +376,32 @@ func ProofToPath(proofNodes []ProofNode, leafKey *Key, hashF hashFunc) ([]Storag
if err != nil {
return nil, err
}
if pNode.Binary != nil {
crntKey, err = leafKey.SubKey(height)
} else {
crntKey, err = leafKey.SubKey(height + squishParentOffset)
}
crntKey, err = getCrntKey(height, squishParentOffset, leafKey, pNode)
if err != nil {
return nil, err
}

// Set the value of the current node
crntNode.Value = pNode.Hash(hashF)

// End of the line
if crntKey.len == 251 {
// End of the line, no children
if crntKey.len == 251 { //nolint:gomnd
break
}

// Set the child key of the current node.
childId := i + squishedParent + 1
childKey, childOffset, err := getChildKey(childId, crntKey, leafKey, &nilKey, proofNodes, hashF)
childInd := i + squishedParent + 1
childKey, err := getChildKey(childInd, crntKey, leafKey, &nilKey, proofNodes, hashF)
if err != nil {
return nil, err
}
childIsRight := leafKey.Test(leafKey.len - crntKey.len - 1)
assignChild(&crntNode, &nilKey, childKey, childIsRight)

pathNodes = append(pathNodes, StorageNode{key: crntKey, node: &crntNode})
// Set the LeftHash and RightHash values
crntNode.LeftHash, crntNode.RightHash = getLeftRightHash(i, proofNodes)

childId += childOffset
leftHash, rightHash, err := getLeftRightHash(i, crntKey, leafKey, proofNodes, hashF)
if err != nil {
return nil, err
}
crntNode.LeftHash = leftHash
crntNode.RightHash = rightHash
pathNodes = append(pathNodes, StorageNode{key: crntKey, node: &crntNode})

// break early
if childKey.len == 0 || childKey.len == 251 {
Expand All @@ -425,66 +411,54 @@ func ProofToPath(proofNodes []ProofNode, leafKey *Key, hashF hashFunc) ([]Storag
return pathNodes, nil
}

func getLeftRightHash(parentId int, sqshdParentKey *Key, leafKey *Key, proofNodes []ProofNode, hashF hashFunc) (*felt.Felt, *felt.Felt, error) {
// Find the binary part of the parent. Use left and right hashes naievely.
// If there is an edge after the binary part, along the path, we use the edge child.
// If there is an edge after the binary part, in the complement path, the hash in that
// direction will be wrong, but it will be corrected later when either merging proofs or
// inserting the actual keys.
func getLeftRightHash(parentInd int, proofNodes []ProofNode) (*felt.Felt, *felt.Felt) {
var leftHash, rightHash *felt.Felt
parent := &proofNodes[parentId]
shiftedParentId := parentId
parent := &proofNodes[parentInd]
shiftedParentInd := parentInd
var parentBinary *Binary
if parent.Binary != nil {
parentBinary = parent.Binary
} else {
shiftedParentId++
parentBinary = proofNodes[shiftedParentId].Binary

shiftedParentInd++
parentBinary = proofNodes[shiftedParentInd].Binary
}
leftHash = parentBinary.LeftHash
rightHash = parentBinary.RightHash
return leftHash, 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
// }
// }
return leftHash, rightHash, nil
func getCrntKey(height, squishParentOffset uint8, leafKey *Key, pNode ProofNode) (*Key, error) {
var crntKey *Key
var err error
if pNode.Binary != nil {
crntKey, err = leafKey.SubKey(height)
} else {
crntKey, err = leafKey.SubKey(height + squishParentOffset)
}
return crntKey, err
}

func getChildKey(childIdx int, crntKey, leafKey, nilKey *Key, proofNodes []ProofNode, hashF hashFunc) (*Key, int, error) {
func getChildKey(childIdx int, crntKey, leafKey, nilKey *Key, proofNodes []ProofNode, hashF hashFunc) (*Key, error) {
var squishChildOffset uint8
var squishChild int
var err error
if childIdx > len(proofNodes)-1 {
return nilKey, 0, nil
return nilKey, nil
} else {
squishChild, squishChildOffset, err = shouldSquish(childIdx, proofNodes, hashF)
if err != nil {
return nil, 0, err
return nil, err
}
}
if crntKey.len+uint8(squishChild)+squishChildOffset == 251 {
return nilKey, squishChild, nil
if crntKey.len+uint8(squishChild)+squishChildOffset == 251 { //nolint:gomnd
return nilKey, nil
}
key, err := leafKey.SubKey(crntKey.len + uint8(squishChild) + squishChildOffset)
return key, squishChild, err
return key, err
}

// getHeight returns the height of the current node, which depends on the previous
// height and whether the current proofnode is edge or binary
func getHeight(idx int, pathNodes []StorageNode, proofNodes []ProofNode) uint8 {
if len(pathNodes) > 0 {
// leftHeight := pathNodes[len(pathNodes)-1].node.Left.len
// rightHeight := pathNodes[len(pathNodes)-1].node.Right.len
// if leftHeight > rightHeight {
// return leftHeight
// }
// return rightHeight
if proofNodes[idx].Edge != nil {
return pathNodes[len(pathNodes)-1].key.len + proofNodes[idx].Edge.Path.len
} else {
Expand Down Expand Up @@ -526,11 +500,6 @@ func BuildTrie(leftProofPath, rightProofPath []StorageNode, keys, values []*felt
return nil, err
}
}
// builtRootKey := tempTrie.RootKey()
// builtRootNode, err := tempTrie.GetNodeFromKey(builtRootKey)
// builtLeftNode, err := tempTrie.GetNodeFromKey(builtRootNode.Left)
// builtRightNode, err := tempTrie.GetNodeFromKey(builtRootNode.Right)
// builtLeftRightNode, err := tempTrie.GetNodeFromKey(builtLeftNode.Right)

for _, sNode := range rightProofPath {
if sNode.node.Left == nil || sNode.node.Right == nil {
Expand All @@ -542,23 +511,11 @@ func BuildTrie(leftProofPath, rightProofPath []StorageNode, keys, values []*felt
}
}

// builtRootKey = tempTrie.RootKey()
// builtRootNode, err = tempTrie.GetNodeFromKey(builtRootKey)
// builtLeftNode, err = tempTrie.GetNodeFromKey(builtRootNode.Left)
// builtRightNode, err := tempTrie.GetNodeFromKey(builtRootNode.Right)
// builtLeftRightNode, err = tempTrie.GetNodeFromKey(builtLeftNode.Right)
for i := range len(keys) {
_, err := tempTrie.PutWithProof(keys[i], values[i], leftProofPath, rightProofPath)
if err != nil {
return nil, err
}
}

// builtRootKey = tempTrie.RootKey()
// builtRootNode, err = tempTrie.GetNodeFromKey(builtRootKey)
// builtLeftNode, err = tempTrie.GetNodeFromKey(builtRootNode.Left)
// builtRightNode, err = tempTrie.GetNodeFromKey(builtRootNode.Right)
// builtLeftRightNode, err = tempTrie.GetNodeFromKey(builtLeftNode.Right)
// fmt.Println(builtRightNode, builtLeftRightNode)
return tempTrie, nil
}
6 changes: 2 additions & 4 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func (t *Trie) setRootKey(newRootKey *Key) {
}

// Todo: update so that the proof nodes are always updated
func (t *Trie) updateValueIfDirty(key *Key) (*Node, error) {
func (t *Trie) updateValueIfDirty(key *Key) (*Node, error) { //nolint:gocyclo
zeroFeltBytes := new(felt.Felt).Bytes()
nilKey := NewKey(0, zeroFeltBytes[:])

Expand All @@ -478,6 +478,7 @@ func (t *Trie) updateValueIfDirty(key *Key) (*Node, error) {
}
}
}

// Update inner proof nodes
if node.Left.Equal(&nilKey) && node.Right.Equal(&nilKey) { // leaf
shouldUpdate = false
Expand Down Expand Up @@ -522,10 +523,7 @@ 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