Skip to content

Commit 90b4eef

Browse files
authored
refactor: VerifyMembership and VerifyNonMembership api (#391)
* 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
1 parent fa483bc commit 90b4eef

File tree

2 files changed

+17
-62
lines changed

2 files changed

+17
-62
lines changed

go/ics23.go

+14-60
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,23 @@ and determine neighbors
2323
*/
2424
package ics23
2525

26-
import (
27-
"bytes"
28-
)
29-
3026
// CommitmentRoot is a byte slice that represents the merkle root of a tree that can be used to validate proofs
3127
type CommitmentRoot []byte
3228

3329
// VerifyMembership returns true iff
34-
// proof is (contains) an ExistenceProof for the given key and value AND
35-
// calculating the root for the ExistenceProof matches the provided CommitmentRoot
30+
// proof is an ExistenceProof for the given key and value AND
31+
// calculating the root for the ExistenceProof matches the provided CommitmentRoot.
3632
func VerifyMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentProof, key []byte, value []byte) bool {
37-
// decompress it before running code (no-op if not compressed)
38-
proof = Decompress(proof)
39-
ep := getExistProofForKey(proof, key)
33+
if proof == nil {
34+
return false
35+
}
36+
37+
ep := proof.GetExist()
4038
if ep == nil {
4139
return false
4240
}
43-
err := ep.Verify(spec, root, key, value)
44-
return err == nil
41+
42+
return ep.Verify(spec, root, key, value) == nil
4543
}
4644

4745
// VerifyNonMembership returns true iff
@@ -50,58 +48,14 @@ func VerifyMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentPro
5048
// left and right proofs are neighbors (or left/right most if one is nil)
5149
// provided key is between the keys of the two proofs
5250
func VerifyNonMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentProof, key []byte) bool {
53-
// decompress it before running code (no-op if not compressed)
54-
proof = Decompress(proof)
55-
np := getNonExistProofForKey(spec, proof, key)
56-
if np == nil {
57-
return false
58-
}
59-
err := np.Verify(spec, root, key)
60-
return err == nil
61-
}
62-
63-
func getExistProofForKey(proof *CommitmentProof, key []byte) *ExistenceProof {
6451
if proof == nil {
65-
return nil
66-
}
67-
68-
switch p := proof.Proof.(type) {
69-
case *CommitmentProof_Exist:
70-
ep := p.Exist
71-
if bytes.Equal(ep.Key, key) {
72-
return ep
73-
}
74-
case *CommitmentProof_Batch:
75-
for _, sub := range p.Batch.Entries {
76-
if ep := sub.GetExist(); ep != nil && bytes.Equal(ep.Key, key) {
77-
return ep
78-
}
79-
}
52+
return false
8053
}
81-
return nil
82-
}
8354

84-
func getNonExistProofForKey(spec *ProofSpec, proof *CommitmentProof, key []byte) *NonExistenceProof {
85-
switch p := proof.Proof.(type) {
86-
case *CommitmentProof_Nonexist:
87-
np := p.Nonexist
88-
if isLeft(spec, np.Left, key) && isRight(spec, np.Right, key) {
89-
return np
90-
}
91-
case *CommitmentProof_Batch:
92-
for _, sub := range p.Batch.Entries {
93-
if np := sub.GetNonexist(); np != nil && isLeft(spec, np.Left, key) && isRight(spec, np.Right, key) {
94-
return np
95-
}
96-
}
55+
np := proof.GetNonexist()
56+
if np == nil {
57+
return false
9758
}
98-
return nil
99-
}
100-
101-
func isLeft(spec *ProofSpec, left *ExistenceProof, key []byte) bool {
102-
return left == nil || bytes.Compare(keyForComparison(spec, left.Key), keyForComparison(spec, key)) < 0
103-
}
10459

105-
func isRight(spec *ProofSpec, right *ExistenceProof, key []byte) bool {
106-
return right == nil || bytes.Compare(keyForComparison(spec, right.Key), keyForComparison(spec, key)) > 0
60+
return np.Verify(spec, root, key) == nil
10761
}

go/vectors_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func TestVectors(t *testing.T) {
1313
name := fmt.Sprintf("%s/%s", tc.Dir, tc.Filename)
1414
t.Run(name, func(t *testing.T) {
1515
proof, ref := LoadFile(t, tc.Dir, tc.Filename)
16+
1617
// Test Calculate method
1718
calculatedRoot, err := proof.Calculate()
1819
if err != nil {
@@ -26,12 +27,12 @@ func TestVectors(t *testing.T) {
2627
// non-existence
2728
valid := VerifyNonMembership(tc.Spec, ref.RootHash, proof, ref.Key)
2829
if !valid {
29-
t.Fatal("Invalid proof")
30+
t.Fatalf("Invalid proof: %v", err)
3031
}
3132
} else {
3233
valid := VerifyMembership(tc.Spec, ref.RootHash, proof, ref.Key, ref.Value)
3334
if !valid {
34-
t.Fatal("Invalid proof")
35+
t.Fatalf("Invalid proof: %v", err)
3536
}
3637
}
3738
})

0 commit comments

Comments
 (0)