@@ -23,26 +23,23 @@ and determine neighbors
23
23
*/
24
24
package ics23
25
25
26
- import (
27
- "bytes"
28
- "fmt"
29
- )
30
-
31
26
// CommitmentRoot is a byte slice that represents the merkle root of a tree that can be used to validate proofs
32
27
type CommitmentRoot []byte
33
28
34
29
// VerifyMembership returns true iff
35
- // proof is (contains) an ExistenceProof for the given key and value AND
36
- // 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.
37
32
func VerifyMembership (spec * ProofSpec , root CommitmentRoot , proof * CommitmentProof , key []byte , value []byte ) bool {
38
- // decompress it before running code (no-op if not compressed)
39
- proof = Decompress (proof )
40
- ep := getExistProofForKey (proof , key )
33
+ if proof == nil {
34
+ return false
35
+ }
36
+
37
+ ep := proof .GetExist ()
41
38
if ep == nil {
42
39
return false
43
40
}
44
- err := ep . Verify ( spec , root , key , value )
45
- return err == nil
41
+
42
+ return ep . Verify ( spec , root , key , value ) == nil
46
43
}
47
44
48
45
// VerifyNonMembership returns true iff
@@ -51,129 +48,14 @@ func VerifyMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentPro
51
48
// left and right proofs are neighbors (or left/right most if one is nil)
52
49
// provided key is between the keys of the two proofs
53
50
func VerifyNonMembership (spec * ProofSpec , root CommitmentRoot , proof * CommitmentProof , key []byte ) bool {
54
- // decompress it before running code (no-op if not compressed)
55
- proof = Decompress (proof )
56
- np := getNonExistProofForKey (spec , proof , key )
57
- if np == nil {
58
- return false
59
- }
60
- err := np .Verify (spec , root , key )
61
- return err == nil
62
- }
63
-
64
- // BatchVerifyMembership will ensure all items are also proven by the CommitmentProof (which should be a BatchProof,
65
- // unless there is one item, when a ExistenceProof may work)
66
- func BatchVerifyMembership (spec * ProofSpec , root CommitmentRoot , proof * CommitmentProof , items map [string ][]byte ) bool {
67
- // decompress it before running code (no-op if not compressed) - once for batch
68
- proof = Decompress (proof )
69
- for k , v := range items {
70
- valid := VerifyMembership (spec , root , proof , []byte (k ), v )
71
- if ! valid {
72
- return false
73
- }
74
- }
75
- return true
76
- }
77
-
78
- // BatchVerifyNonMembership will ensure all items are also proven to not be in the Commitment by the CommitmentProof
79
- // (which should be a BatchProof, unless there is one item, when a NonExistenceProof may work)
80
- func BatchVerifyNonMembership (spec * ProofSpec , root CommitmentRoot , proof * CommitmentProof , keys [][]byte ) bool {
81
- // decompress it before running code (no-op if not compressed) - once for batch
82
- proof = Decompress (proof )
83
- for _ , k := range keys {
84
- valid := VerifyNonMembership (spec , root , proof , k )
85
- if ! valid {
86
- return false
87
- }
88
- }
89
- return true
90
- }
91
-
92
- // CombineProofs takes a number of commitment proofs (simple or batch) and
93
- // converts them into a batch and compresses them.
94
- //
95
- // This is designed for proof generation libraries to create efficient batches
96
- func CombineProofs (proofs []* CommitmentProof ) (* CommitmentProof , error ) {
97
- var entries []* BatchEntry
98
-
99
- for _ , proof := range proofs {
100
- if ex := proof .GetExist (); ex != nil {
101
- entry := & BatchEntry {
102
- Proof : & BatchEntry_Exist {
103
- Exist : ex ,
104
- },
105
- }
106
- entries = append (entries , entry )
107
- } else if non := proof .GetNonexist (); non != nil {
108
- entry := & BatchEntry {
109
- Proof : & BatchEntry_Nonexist {
110
- Nonexist : non ,
111
- },
112
- }
113
- entries = append (entries , entry )
114
- } else if batch := proof .GetBatch (); batch != nil {
115
- entries = append (entries , batch .Entries ... )
116
- } else if comp := proof .GetCompressed (); comp != nil {
117
- decomp := Decompress (proof )
118
- entries = append (entries , decomp .GetBatch ().Entries ... )
119
- } else {
120
- return nil , fmt .Errorf ("proof neither exist or nonexist: %#v" , proof .GetProof ())
121
- }
122
- }
123
-
124
- batch := & CommitmentProof {
125
- Proof : & CommitmentProof_Batch {
126
- Batch : & BatchProof {
127
- Entries : entries ,
128
- },
129
- },
130
- }
131
-
132
- return Compress (batch ), nil
133
- }
134
-
135
- func getExistProofForKey (proof * CommitmentProof , key []byte ) * ExistenceProof {
136
51
if proof == nil {
137
- return nil
138
- }
139
-
140
- switch p := proof .Proof .(type ) {
141
- case * CommitmentProof_Exist :
142
- ep := p .Exist
143
- if bytes .Equal (ep .Key , key ) {
144
- return ep
145
- }
146
- case * CommitmentProof_Batch :
147
- for _ , sub := range p .Batch .Entries {
148
- if ep := sub .GetExist (); ep != nil && bytes .Equal (ep .Key , key ) {
149
- return ep
150
- }
151
- }
52
+ return false
152
53
}
153
- return nil
154
- }
155
54
156
- func getNonExistProofForKey (spec * ProofSpec , proof * CommitmentProof , key []byte ) * NonExistenceProof {
157
- switch p := proof .Proof .(type ) {
158
- case * CommitmentProof_Nonexist :
159
- np := p .Nonexist
160
- if isLeft (spec , np .Left , key ) && isRight (spec , np .Right , key ) {
161
- return np
162
- }
163
- case * CommitmentProof_Batch :
164
- for _ , sub := range p .Batch .Entries {
165
- if np := sub .GetNonexist (); np != nil && isLeft (spec , np .Left , key ) && isRight (spec , np .Right , key ) {
166
- return np
167
- }
168
- }
55
+ np := proof .GetNonexist ()
56
+ if np == nil {
57
+ return false
169
58
}
170
- return nil
171
- }
172
-
173
- func isLeft (spec * ProofSpec , left * ExistenceProof , key []byte ) bool {
174
- return left == nil || bytes .Compare (keyForComparison (spec , left .Key ), keyForComparison (spec , key )) < 0
175
- }
176
59
177
- func isRight (spec * ProofSpec , right * ExistenceProof , key []byte ) bool {
178
- return right == nil || bytes .Compare (keyForComparison (spec , right .Key ), keyForComparison (spec , key )) > 0
60
+ return np .Verify (spec , root , key ) == nil
179
61
}
0 commit comments