Skip to content

Commit

Permalink
dirty
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Nov 4, 2024
1 parent ebc4a94 commit 4231156
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tree/arbo/circomproofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package arbo
import (
"bytes"
"encoding/json"
"fmt"
"slices"
)

Expand Down Expand Up @@ -107,3 +108,16 @@ func (cvp CircomVerifierProof) CalculateProofNodes(hashFunc HashFunction) ([][]b
}
return CalculateProofNodes(hashFunc, cvp.Key, cvp.Value, packedSiblings)
}

// CheckProof verifies the given proof. The proof verification depends on the
// HashFunction passed as parameter.
func (cvp CircomVerifierProof) CheckProof(hashFunc HashFunction) (bool, error) {
hashes, err := cvp.CalculateProofNodes(hashFunc)
if err != nil {
return false, err
}
if !bytes.Equal(hashes[0], cvp.Root) {
return false, fmt.Errorf("calculated root doesn't match expected root")
}
return true, nil
}
47 changes: 47 additions & 0 deletions tree/arbo/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package arbo
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"math"
"slices"
Expand Down Expand Up @@ -200,3 +201,49 @@ func CalculateProofNodes(hashFunc HashFunction, k, v, packedSiblings []byte) ([]
slices.Reverse(hashes)
return hashes, nil
}

// CheckProofBatch verifies a batch of N proofs pairs (old and new). The proof verification depends on the
// HashFunction passed as parameter.
func CheckProofBatch(hashFunc HashFunction, old, new []CircomVerifierProof) (bool, error) {
newBranch := make(map[string]int)
newSiblings := make(map[string]int)

if len(old) != len(new) {
return false, fmt.Errorf("batch of proofs incomplete")
}

for i := range old {
// Check all old proofs are valid
if valid, err := old[i].CheckProof(hashFunc); !valid {
return false, err
}

// Map all new branches
nodes, err := new[i].CalculateProofNodes(hashFunc)
if err != nil {
return false, err
}
// and check they are valid
if !bytes.Equal(new[i].Root, nodes[0]) {
return false, fmt.Errorf("root doesn't match")
}

for level, hash := range nodes {
newBranch[hex.EncodeToString(hash)] = level
fmt.Printf("newBranch(%d): %x\n", level, hash) // debug
}

for level := range new[i].Siblings {
if !slices.Equal(old[i].Siblings[level], new[i].Siblings[level]) {
newSiblings[hex.EncodeToString(new[i].Siblings[level])] = level + 1
}
}
}
for hash, level := range newSiblings {
if newBranch[hash] != newSiblings[hash] {
return false, fmt.Errorf("sibling %s (at level %d) changed but there's no proof why", hash, level)
}
}

return bytes.Equal(hashes[0], root), nil

Check failure on line 248 in tree/arbo/proof.go

View workflow job for this annotation

GitHub Actions / job_go_checks

undefined: hashes

Check failure on line 248 in tree/arbo/proof.go

View workflow job for this annotation

GitHub Actions / job_go_checks

undefined: root

Check failure on line 248 in tree/arbo/proof.go

View workflow job for this annotation

GitHub Actions / job_go_test

undefined: hashes

Check failure on line 248 in tree/arbo/proof.go

View workflow job for this annotation

GitHub Actions / job_go_test

undefined: root
}

0 comments on commit 4231156

Please sign in to comment.