@@ -23,25 +23,23 @@ and determine neighbors
23
23
*/
24
24
package ics23
25
25
26
- import (
27
- "bytes"
28
- )
29
-
30
26
// CommitmentRoot is a byte slice that represents the merkle root of a tree that can be used to validate proofs
31
27
type CommitmentRoot []byte
32
28
33
29
// 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.
36
32
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 ()
40
38
if ep == nil {
41
39
return false
42
40
}
43
- err := ep . Verify ( spec , root , key , value )
44
- return err == nil
41
+
42
+ return ep . Verify ( spec , root , key , value ) == nil
45
43
}
46
44
47
45
// VerifyNonMembership returns true iff
@@ -50,58 +48,14 @@ func VerifyMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentPro
50
48
// left and right proofs are neighbors (or left/right most if one is nil)
51
49
// provided key is between the keys of the two proofs
52
50
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 {
64
51
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
80
53
}
81
- return nil
82
- }
83
54
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
97
58
}
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
- }
104
59
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
107
61
}
0 commit comments