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
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
tidy logic
  • Loading branch information
rian committed Jun 12, 2024
commit 2d7c6e8e2fe8283407df8c15fdf1268045070b78
58 changes: 39 additions & 19 deletions core/trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@
proofs := [2][]ProofNode{}
leftProof, err := GetProof(leftBoundary, tri)
if err != nil {
return proofs, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L66

Added line #L66 was not covered by tests
}
rightProof, err := GetProof(rightBoundary, tri)
if err != nil {
return proofs, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L70

Added line #L70 was not covered by tests
}
proofs[0] = leftProof
proofs[1] = rightProof
Expand Down Expand Up @@ -219,18 +219,18 @@
) (bool, error) {
// Step 0: checks
if len(keys) != len(values) {
return false, fmt.Errorf("inconsistent proof data, number of keys: %d, number of values: %d", len(keys), len(values))

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L222

Added line #L222 was not covered by tests
}

// Ensure all keys are monotonic increasing
if err := ensureMonotonicIncreasing(proofKeys, keys); err != nil {
return false, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L227

Added line #L227 was not covered by tests
}

// Ensure the inner values contain no deletions
for _, value := range values {
if value.Equal(&felt.Zero) {
return false, errors.New("range contains deletion")

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L233

Added line #L233 was not covered by tests
}
}

Expand All @@ -240,12 +240,12 @@
for i := 0; i < 2; i++ {
if proofs[i] != nil {
if !VerifyProof(root, proofKeys[i], proofValues[i], proofs[i], hash) {
return false, fmt.Errorf("invalid proof for key %x", proofKeys[i].String())

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L243

Added line #L243 was not covered by tests
}

proofPaths[i], err = ProofToPath(proofs[i], proofKeys[i], hash)
if err != nil {
return false, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L248

Added line #L248 was not covered by tests
}
}
}
Expand All @@ -253,16 +253,16 @@
// Step 2: Build trie from proofPaths and keys
tmpTrie, err := BuildTrie(proofPaths[0], proofPaths[1], keys, values)
if err != nil {
return false, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L256

Added line #L256 was not covered by tests
}

// Verify that the recomputed root hash matches the provided root hash
recomputedRoot, err := tmpTrie.Root()
if err != nil {
return false, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L262

Added line #L262 was not covered by tests
}
if !recomputedRoot.Equal(root) {
return false, errors.New("root hash mismatch")

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L265

Added line #L265 was not covered by tests
}

return true, nil
Expand All @@ -272,19 +272,19 @@
if proofKeys[0] != nil {
leftProofFelt := proofKeys[0].Felt()
if leftProofFelt.Cmp(keys[0]) >= 0 {
return errors.New("range is not monotonically increasing")

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L275

Added line #L275 was not covered by tests
}
}
if proofKeys[1] != nil {
rightProofFelt := proofKeys[1].Felt()
if keys[len(keys)-1].Cmp(&rightProofFelt) >= 0 {
return errors.New("range is not monotonically increasing")

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L281

Added line #L281 was not covered by tests
}
}
if len(keys) >= 2 {
for i := 0; i < len(keys)-1; i++ {
if keys[i].Cmp(keys[i+1]) >= 0 {
return errors.New("range is not monotonically increasing")

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L287

Added line #L287 was not covered by tests
}
}
}
Expand Down Expand Up @@ -317,7 +317,7 @@
} else if parent.Binary.RightHash.Equal(childHash) {
return 1, child.Edge.Path.len, nil
} else {
return 0, 0, errors.New("can't determine the child hash from the parent and child")

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L320

Added line #L320 was not covered by tests
}
}

Expand All @@ -325,17 +325,23 @@
return 0, 1, nil
}

return 0, 1, nil

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L328

Added line #L328 was not covered by tests
}

func assignChild(crntNode *Node, nilKey, childKey *Key, isRight bool) {
if isRight {
func assignChild(i, squishedParent int, crntNode *Node, nilKey, leafKey, crntKey *Key, proofNodes []ProofNode, hashF hashFunc) (*Key, error) {

Check failure on line 331 in core/trie/proof.go

View workflow job for this annotation

GitHub Actions / lint

line is 142 characters (lll)
childInd := i + squishedParent + 1
childKey, err := getChildKey(childInd, crntKey, leafKey, nilKey, proofNodes, hashF)
if err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L335

Added line #L335 was not covered by tests
}
if leafKey.Test(leafKey.len - crntKey.len - 1) {
crntNode.Right = childKey
crntNode.Left = nilKey
} else {
crntNode.Right = nilKey
crntNode.Left = childKey
}
return childKey, nil
}

// ProofToPath returns a set of storage nodes from the root to the end of the proof path.
Expand All @@ -352,32 +358,22 @@
for i, pNode := range proofNodes {
// Keep moving along the path (may need to skip nodes that were compressed into the last path node)
if i != 0 {
lastNode := pathNodes[len(pathNodes)-1].node
noLeftMatch, noRightMatch := false, false
if lastNode.LeftHash != nil && !pNode.Hash(hashF).Equal(lastNode.LeftHash) {
noLeftMatch = true
}
if lastNode.RightHash != nil && !pNode.Hash(hashF).Equal(lastNode.RightHash) {
noRightMatch = true
}
if noLeftMatch && noRightMatch {
if skipNode(pNode, pathNodes, hashF) {
continue
}
}

var crntKey *Key
crntNode := Node{}

height := getHeight(i, pathNodes, proofNodes)

// Set the key of the current node
squishedParent, squishParentOffset, err := shouldSquish(i, proofNodes, hashF)
if err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L372

Added line #L372 was not covered by tests
}
crntKey, err = getCrntKey(height, squishParentOffset, leafKey, pNode)
crntKey, err = getCrntKey(i, squishParentOffset, leafKey, pNode, pathNodes, proofNodes)
if err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L376

Added line #L376 was not covered by tests
}

// Set the value of the current node
Expand All @@ -385,17 +381,14 @@

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

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L384

Added line #L384 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L390

Added line #L390 was not covered by tests
}
childIsRight := leafKey.Test(leafKey.len - crntKey.len - 1)
assignChild(&crntNode, &nilKey, childKey, childIsRight)

// Set the LeftHash and RightHash values
crntNode.LeftHash, crntNode.RightHash = getLeftRightHash(i, proofNodes)
Expand All @@ -410,6 +403,21 @@
return pathNodes, nil
}

func skipNode(pNode ProofNode, pathNodes []StorageNode, hashF hashFunc) bool {
lastNode := pathNodes[len(pathNodes)-1].node
noLeftMatch, noRightMatch := false, false
if lastNode.LeftHash != nil && !pNode.Hash(hashF).Equal(lastNode.LeftHash) {
noLeftMatch = true
}
if lastNode.RightHash != nil && !pNode.Hash(hashF).Equal(lastNode.RightHash) {
noRightMatch = true
}
if noLeftMatch && noRightMatch {
return true
}
return false
}

func getLeftRightHash(parentInd int, proofNodes []ProofNode) (*felt.Felt, *felt.Felt) {
var leftHash, rightHash *felt.Felt
parent := &proofNodes[parentInd]
Expand All @@ -426,9 +434,21 @@
return leftHash, rightHash
}

func getCrntKey(height, squishParentOffset uint8, leafKey *Key, pNode ProofNode) (*Key, error) {
func getCrntKey(idx int, squishParentOffset uint8, leafKey *Key, pNode ProofNode, pathNodes []StorageNode, proofNodes []ProofNode) (*Key, error) {

Check failure on line 437 in core/trie/proof.go

View workflow job for this annotation

GitHub Actions / lint

line is 146 characters (lll)
var crntKey *Key
var err error

var height uint8
if len(pathNodes) > 0 {
if proofNodes[idx].Edge != nil {
height = pathNodes[len(pathNodes)-1].key.len + proofNodes[idx].Edge.Path.len

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L444

Added line #L444 was not covered by tests
} else {
height = pathNodes[len(pathNodes)-1].key.len + 1
}
} else {
height = 0
}

if pNode.Binary != nil {
crntKey, err = leafKey.SubKey(height)
} else {
Expand All @@ -446,7 +466,7 @@
} else {
squishChild, squishChildOffset, err = shouldSquish(childIdx, proofNodes, hashF)
if err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L469

Added line #L469 was not covered by tests
}
}
if crntKey.len+uint8(squishChild)+squishChildOffset == 251 { //nolint:gomnd
Expand All @@ -456,15 +476,15 @@
return key, err
}

func getHeight(idx int, pathNodes []StorageNode, proofNodes []ProofNode) uint8 {

Check failure on line 479 in core/trie/proof.go

View workflow job for this annotation

GitHub Actions / lint

func `getHeight` is unused (unused)
if len(pathNodes) > 0 {
if proofNodes[idx].Edge != nil {
return pathNodes[len(pathNodes)-1].key.len + proofNodes[idx].Edge.Path.len
} else {
return pathNodes[len(pathNodes)-1].key.len + 1

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L479-L484

Added lines #L479 - L484 were not covered by tests
}
} else {
return 0

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L486-L487

Added lines #L486 - L487 were not covered by tests
}
}

Expand All @@ -472,7 +492,7 @@
func BuildTrie(leftProofPath, rightProofPath []StorageNode, keys, values []*felt.Felt) (*Trie, error) { //nolint:gocyclo
tempTrie, err := NewTriePedersen(newMemStorage(), 251) //nolint:gomnd
if err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L495

Added line #L495 was not covered by tests
}

// merge proof paths
Expand All @@ -480,40 +500,40 @@
// 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

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L503

Added line #L503 was not covered by tests
}
if leftProofPath[i].key.Equal(rightProofPath[i].key) {
leftProofPath[i].node.Right = rightProofPath[i].node.Right
rightProofPath[i].node.Left = leftProofPath[i].node.Left
} else {
break

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L509

Added line #L509 was not covered by tests
}
}

for _, sNode := range leftProofPath {
if sNode.node.Left == nil || sNode.node.Right == nil {
break

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L515

Added line #L515 was not covered by tests
}
_, err := tempTrie.PutInner(sNode.key, sNode.node)
if err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L519

Added line #L519 was not covered by tests
}
}

for _, sNode := range rightProofPath {
if sNode.node.Left == nil || sNode.node.Right == nil {
break

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L525

Added line #L525 was not covered by tests
}
_, err := tempTrie.PutInner(sNode.key, sNode.node)
if err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L529

Added line #L529 was not covered by tests
}
}

for i := range len(keys) {
_, err := tempTrie.PutWithProof(keys[i], values[i], leftProofPath, rightProofPath)
if err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L536

Added line #L536 was not covered by tests
}
}
return tempTrie, nil
Expand Down
Loading