-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathquery.go
224 lines (205 loc) · 4.43 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package circuits
import (
"math/big"
"github.com/iden3/go-merkletree-sql/v2"
"github.com/pkg/errors"
)
// List of available operators.
const (
NOOP int = iota // No operation, skip query verification in circuit
EQ
LT
GT
IN
NIN
NE
LTE
GTE
BETWEEN
NONBETWEEN
EXISTS
SD = 16
NULLIFY = 17
)
// QueryOperators represents operators for atomic circuits
var QueryOperators = map[string]int{
"$noop": NOOP,
"$eq": EQ,
"$lt": LT,
"$gt": GT,
"$in": IN,
"$nin": NIN,
"$ne": NE,
"$lte": LTE,
"$gte": GTE,
"$between": BETWEEN,
"$nonbetween": NONBETWEEN,
"$exists": EXISTS,
"$sd": SD,
"$nullify": NULLIFY,
}
// Comparer value.
type Comparer interface {
Compare(int) (bool, error)
}
// Scalar uses for compare two scalar value.
type Scalar struct {
x, y *big.Int
}
// NewScalar create `Scalar` comparer.
func NewScalar(x, y *big.Int) *Scalar {
return &Scalar{x, y}
}
// Compare x with y by target QueryOperators.
// Scalar compare support: $eq, $lt, $gt, $ne, $lte, $gte type.
func (s *Scalar) Compare(t int) (bool, error) {
compare := s.x.Cmp(s.y)
switch t {
case EQ:
return compare == 0, nil
case LT:
return compare == -1, nil
case GT:
return compare == 1, nil
case NE:
return compare != 0, nil
case LTE:
return compare < 1, nil
case GTE:
return compare > -1, nil
}
return false, errors.New("unknown compare type for scalar")
}
// Vector uses for find/not find x scalar type in y vector type.
type Vector struct {
x *big.Int
y []*big.Int
}
// NewVector create Vector.
func NewVector(x *big.Int, y []*big.Int) *Vector {
return &Vector{x, y}
}
// Compare find/not find x in y by type.
// Vector compare support: $in, $nin, $between
func (v *Vector) Compare(t int) (bool, error) {
switch t {
case IN:
if len(v.y) == 0 {
return false, nil
}
for _, i := range v.y {
if v.x.Cmp(i) == 0 {
return true, nil
}
}
return false, nil
case NIN:
if len(v.y) == 0 {
return true, nil
}
for _, i := range v.y {
if v.x.Cmp(i) == 0 {
return false, nil
}
}
return true, nil
case BETWEEN:
if len(v.y) < 2 {
return false, nil
}
if v.x.Cmp(v.y[0]) >= 0 && v.x.Cmp(v.y[1]) <= 0 {
return true, nil
}
return false, nil
case NONBETWEEN:
if len(v.y) < 2 {
return false, nil
}
if !(v.x.Cmp(v.y[0]) >= 0 && v.x.Cmp(v.y[1]) <= 0) {
return true, nil
}
return false, nil
}
return false, errors.New("unknown compare type for vector")
}
// FactoryComparer depends on input data will return right comparer.
func FactoryComparer(x *big.Int, y []*big.Int, t int) (Comparer, error) {
var cmp Comparer
switch t {
case EQ, LT, GT, NE:
if len(y) != 1 {
return nil, errors.New("currently we support only one value for scalar comparison")
}
cmp = NewScalar(x, y[0])
case IN, NIN:
cmp = NewVector(x, y)
default:
return nil, errors.New("unknown compare type")
}
return cmp, nil
}
// Query represents basic request to claim field with MTP and without
type Query struct {
Operator int
Values []*big.Int
SlotIndex int
ValueProof *ValueProof
}
// Validate value size for operator
func (q Query) ValidateValueArraySize(maxArrSize int) error {
oneArrLengthOps := []int{EQ, LT, GT, NE, LTE, GTE, EXISTS}
twoArrLengthOps := []int{BETWEEN, NONBETWEEN}
maxArrLengthOps := []int{IN, NIN}
arrSize := len(q.Values)
if contains(oneArrLengthOps, q.Operator) {
if arrSize != 1 {
return errors.New(ErrorInvalidValuesArrSize)
} else {
return nil
}
}
if contains(twoArrLengthOps, q.Operator) {
if arrSize != 2 {
return errors.New(ErrorInvalidValuesArrSize)
} else {
return nil
}
}
if contains(maxArrLengthOps, q.Operator) {
if arrSize == 0 || arrSize > maxArrSize {
return errors.New(ErrorInvalidValuesArrSize)
} else {
return nil
}
}
if arrSize != 0 {
return errors.New(ErrorInvalidValuesArrSize)
}
return nil
}
func (q Query) validate() error {
for i := range q.Values {
if q.Values[i] == nil {
return errors.New(ErrorEmptyQueryValue)
}
}
return nil
}
// ValueProof represents a Merkle Proof for a value stored as MT
type ValueProof struct {
Path *big.Int
Value *big.Int
MTP *merkletree.Proof
}
func (q ValueProof) validate() error {
if q.Path == nil {
return errors.New(ErrorEmptyJsonLDQueryPath)
}
if q.Value == nil {
return errors.New(ErrorEmptyJsonLDQueryValue)
}
if q.MTP == nil {
return errors.New(ErrorEmptyJsonLDQueryProof)
}
return nil
}