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

refactor: VerifyMembership and VerifyNonMembership api #391

Merged
merged 12 commits into from
Oct 23, 2024
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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checked in ep.Verify

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checked in np.Verify

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
Loading