Skip to content

Commit

Permalink
refactor: VerifyMembership and VerifyNonMembership api (#391)
Browse files Browse the repository at this point in the history
* refactor: remove  and

* chore: CHANGELOG

* lint

* chore: changelog

* refactor: remove CombineProofs

* refactor: VerifyMembership and VerifyNonMembership api

* refactor: reset api

* Update go/ics23.go

* refactor: remove unnecessary code

* lint: remove isLeft and isRight as they are unused now
  • Loading branch information
colin-axner authored Oct 23, 2024
1 parent fa483bc commit 90b4eef
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 62 deletions.
74 changes: 14 additions & 60 deletions go/ics23.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,23 @@ and determine neighbors
*/
package ics23

import (
"bytes"
)

// CommitmentRoot is a byte slice that represents the merkle root of a tree that can be used to validate proofs
type CommitmentRoot []byte

// VerifyMembership returns true iff
// proof is (contains) an ExistenceProof for the given key and value AND
// calculating the root for the ExistenceProof matches the provided CommitmentRoot
// proof is an ExistenceProof for the given key and value AND
// calculating the root for the ExistenceProof matches the provided CommitmentRoot.
func VerifyMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentProof, key []byte, value []byte) bool {
// decompress it before running code (no-op if not compressed)
proof = Decompress(proof)
ep := getExistProofForKey(proof, key)
if proof == nil {
return false
}

ep := proof.GetExist()
if ep == nil {
return false
}
err := ep.Verify(spec, root, key, value)
return err == nil

return ep.Verify(spec, root, key, value) == nil
}

// VerifyNonMembership returns true iff
Expand All @@ -50,58 +48,14 @@ func VerifyMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentPro
// left and right proofs are neighbors (or left/right most if one is nil)
// provided key is between the keys of the two proofs
func VerifyNonMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentProof, key []byte) bool {
// decompress it before running code (no-op if not compressed)
proof = Decompress(proof)
np := getNonExistProofForKey(spec, proof, key)
if np == nil {
return false
}
err := np.Verify(spec, root, key)
return err == nil
}

func getExistProofForKey(proof *CommitmentProof, key []byte) *ExistenceProof {
if proof == nil {
return nil
}

switch p := proof.Proof.(type) {
case *CommitmentProof_Exist:
ep := p.Exist
if bytes.Equal(ep.Key, key) {
return ep
}
case *CommitmentProof_Batch:
for _, sub := range p.Batch.Entries {
if ep := sub.GetExist(); ep != nil && bytes.Equal(ep.Key, key) {
return ep
}
}
return false
}
return nil
}

func getNonExistProofForKey(spec *ProofSpec, proof *CommitmentProof, key []byte) *NonExistenceProof {
switch p := proof.Proof.(type) {
case *CommitmentProof_Nonexist:
np := p.Nonexist
if isLeft(spec, np.Left, key) && isRight(spec, np.Right, key) {
return np
}
case *CommitmentProof_Batch:
for _, sub := range p.Batch.Entries {
if np := sub.GetNonexist(); np != nil && isLeft(spec, np.Left, key) && isRight(spec, np.Right, key) {
return np
}
}
np := proof.GetNonexist()
if np == nil {
return false
}
return nil
}

func isLeft(spec *ProofSpec, left *ExistenceProof, key []byte) bool {
return left == nil || bytes.Compare(keyForComparison(spec, left.Key), keyForComparison(spec, key)) < 0
}

func isRight(spec *ProofSpec, right *ExistenceProof, key []byte) bool {
return right == nil || bytes.Compare(keyForComparison(spec, right.Key), keyForComparison(spec, key)) > 0
return np.Verify(spec, root, key) == nil
}
5 changes: 3 additions & 2 deletions go/vectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestVectors(t *testing.T) {
name := fmt.Sprintf("%s/%s", tc.Dir, tc.Filename)
t.Run(name, func(t *testing.T) {
proof, ref := LoadFile(t, tc.Dir, tc.Filename)

// Test Calculate method
calculatedRoot, err := proof.Calculate()
if err != nil {
Expand All @@ -26,12 +27,12 @@ func TestVectors(t *testing.T) {
// non-existence
valid := VerifyNonMembership(tc.Spec, ref.RootHash, proof, ref.Key)
if !valid {
t.Fatal("Invalid proof")
t.Fatalf("Invalid proof: %v", err)
}
} else {
valid := VerifyMembership(tc.Spec, ref.RootHash, proof, ref.Key, ref.Value)
if !valid {
t.Fatal("Invalid proof")
t.Fatalf("Invalid proof: %v", err)
}
}
})
Expand Down

0 comments on commit 90b4eef

Please sign in to comment.