Skip to content

Commit

Permalink
arbo: add CalculateProofNodes
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Oct 29, 2024
1 parent d969541 commit ebc4a94
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
18 changes: 18 additions & 0 deletions tree/arbo/circomproofs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package arbo

import (
"bytes"
"encoding/json"
"slices"
)

// CircomVerifierProof contains the needed data to check a Circom Verifier Proof
Expand Down Expand Up @@ -89,3 +91,19 @@ func (t *Tree) GenerateCircomVerifierProof(k []byte) (*CircomVerifierProof, erro

return &cp, nil
}

// CalculateProofNodes calculates the chain of hashes in the path of the proof.
// In the returned list, first item is the root, and last item is the hash of the leaf.
func (cvp CircomVerifierProof) CalculateProofNodes(hashFunc HashFunction) ([][]byte, error) {
paddedSiblings := slices.Clone(cvp.Siblings)
for k, v := range paddedSiblings {
if bytes.Equal(v, []byte{0}) {
paddedSiblings[k] = make([]byte, hashFunc.Len())
}
}
packedSiblings, err := PackSiblings(hashFunc, paddedSiblings)
if err != nil {
return nil, err
}
return CalculateProofNodes(hashFunc, cvp.Key, cvp.Value, packedSiblings)
}
22 changes: 18 additions & 4 deletions tree/arbo/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,31 @@ func bytesToBitmap(b []byte) []bool {
// CheckProof verifies the given proof. The proof verification depends on the
// HashFunction passed as parameter.
func CheckProof(hashFunc HashFunction, k, v, root, packedSiblings []byte) (bool, error) {
siblings, err := UnpackSiblings(hashFunc, packedSiblings)
hashes, err := CalculateProofNodes(hashFunc, k, v, packedSiblings)
if err != nil {
return false, err
}
return bytes.Equal(hashes[0], root), nil
}

// CalculateProofNodes calculates the chain of hashes in the path of the given proof.
// In the returned list, first item is the root, and last item is the hash of the leaf.
func CalculateProofNodes(hashFunc HashFunction, k, v, packedSiblings []byte) ([][]byte, error) {
siblings, err := UnpackSiblings(hashFunc, packedSiblings)
if err != nil {
return nil, err
}

keyPath := make([]byte, int(math.Ceil(float64(len(siblings))/float64(8))))
copy(keyPath, k)

key, _, err := newLeafValue(hashFunc, k, v)
if err != nil {
return false, err
return nil, err
}

hashes := [][]byte{key}

path := getPath(len(siblings), keyPath)
for i, sibling := range slices.Backward(siblings) {
if path[i] {
Expand All @@ -181,8 +193,10 @@ func CheckProof(hashFunc HashFunction, k, v, root, packedSiblings []byte) (bool,
key, _, err = newIntermediate(hashFunc, key, sibling)
}
if err != nil {
return false, err
return nil, err
}
hashes = append(hashes, key)
}
return bytes.Equal(key, root), nil
slices.Reverse(hashes)
return hashes, nil
}

0 comments on commit ebc4a94

Please sign in to comment.