forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_ops.go
348 lines (319 loc) · 11.1 KB
/
field_ops.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package emulated
import (
"errors"
"fmt"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/selector"
)
// Div computes a/b and returns it. It uses [DivHint] as a hint function.
func (f *Field[T]) Div(a, b *Element[T]) *Element[T] {
return f.reduceAndOp(f.div, f.divPreCond, a, b)
}
func (f *Field[T]) divPreCond(a, b *Element[T]) (nextOverflow uint, err error) {
mulOf, err := f.mulPreCond(&Element[T]{Limbs: make([]frontend.Variable, f.fParams.NbLimbs()), overflow: 0}, b)
if err != nil {
return mulOf, err
}
return f.subPreCond(a, &Element[T]{overflow: mulOf})
}
func (f *Field[T]) div(a, b *Element[T], _ uint) *Element[T] {
// omit width assertion as for a is done in AssertIsEqual and for b is done in Mul below
if !f.fParams.IsPrime() {
// TODO shouldn't we still try to do a classic int div in a hint, constraint the result, and let it fail?
// that would enable things like uint32 div ?
panic("modulus not a prime")
}
div, err := f.computeDivisionHint(a.Limbs, b.Limbs)
if err != nil {
panic(fmt.Sprintf("compute division: %v", err))
}
e := f.packLimbs(div, true)
res := f.Mul(e, b)
f.AssertIsEqual(res, a)
return e
}
// Inverse compute 1/a and returns it. It uses [InverseHint].
func (f *Field[T]) Inverse(a *Element[T]) *Element[T] {
return f.reduceAndOp(f.inverse, f.inversePreCond, a, nil)
}
func (f *Field[T]) inversePreCond(a, _ *Element[T]) (nextOverflow uint, err error) {
mulOf, err := f.mulPreCond(a, &Element[T]{Limbs: make([]frontend.Variable, f.fParams.NbLimbs()), overflow: 0}) // order is important, we want that reduce left side
if err != nil {
return mulOf, err
}
return f.subPreCond(&Element[T]{overflow: 0}, &Element[T]{overflow: mulOf})
}
func (f *Field[T]) inverse(a, _ *Element[T], _ uint) *Element[T] {
// omit width assertion as is done in Mul below
if !f.fParams.IsPrime() {
panic("modulus not a prime")
}
k, err := f.computeInverseHint(a.Limbs)
if err != nil {
panic(fmt.Sprintf("compute inverse: %v", err))
}
e := f.packLimbs(k, true)
res := f.Mul(e, a)
one := f.One()
f.AssertIsEqual(res, one)
return e
}
// Sqrt computes square root of a and returns it. It uses [SqrtHint].
func (f *Field[T]) Sqrt(a *Element[T]) *Element[T] {
return f.reduceAndOp(f.sqrt, f.sqrtPreCond, a, nil)
}
func (f *Field[T]) sqrtPreCond(a, _ *Element[T]) (nextOverflow uint, err error) {
mulOf, err := f.mulPreCond(a, a)
if err != nil {
return mulOf, err
}
return f.subPreCond(a, &Element[T]{overflow: mulOf})
}
func (f *Field[T]) sqrt(a, _ *Element[T], _ uint) *Element[T] {
// omit width assertion as is done in Mul below
if !f.fParams.IsPrime() {
panic("modulus not a prime")
}
res, err := f.NewHint(SqrtHint, 1, a)
if err != nil {
panic(fmt.Sprintf("compute sqrt: %v", err))
}
_a := f.Mul(res[0], res[0])
f.AssertIsEqual(_a, a)
return res[0]
}
// Add computes a+b and returns it. If the result wouldn't fit into Element, then
// first reduces the inputs (larger first) and tries again. Doesn't mutate
// inputs.
func (f *Field[T]) Add(a, b *Element[T]) *Element[T] {
return f.reduceAndOp(f.add, f.addPreCond, a, b)
}
func (f *Field[T]) addPreCond(a, b *Element[T]) (nextOverflow uint, err error) {
reduceRight := a.overflow < b.overflow
nextOverflow = max(a.overflow, b.overflow) + 1
if nextOverflow > f.maxOverflow() {
err = overflowError{op: "add", nextOverflow: nextOverflow, maxOverflow: f.maxOverflow(), reduceRight: reduceRight}
}
return
}
func (f *Field[T]) add(a, b *Element[T], nextOverflow uint) *Element[T] {
ba, aConst := f.constantValue(a)
bb, bConst := f.constantValue(b)
if aConst && bConst {
ba.Add(ba, bb).Mod(ba, f.fParams.Modulus())
return newConstElement[T](ba)
}
nbLimbs := max(len(a.Limbs), len(b.Limbs))
limbs := make([]frontend.Variable, nbLimbs)
for i := range limbs {
limbs[i] = 0
if i < len(a.Limbs) {
limbs[i] = f.api.Add(limbs[i], a.Limbs[i])
}
if i < len(b.Limbs) {
limbs[i] = f.api.Add(limbs[i], b.Limbs[i])
}
}
return f.newInternalElement(limbs, nextOverflow)
}
// Reduce reduces a modulo the field order and returns it.
func (f *Field[T]) Reduce(a *Element[T]) *Element[T] {
f.enforceWidthConditional(a)
if a.overflow == 0 {
// fast path - already reduced, omit reduction.
return a
}
// sanity check
if _, aConst := f.constantValue(a); aConst {
panic("trying to reduce a constant, which happen to have an overflow flag set")
}
// slow path - use hint to reduce value
return f.mulMod(a, f.One(), 0)
}
// Sub subtracts b from a and returns it. Reduces locally if wouldn't fit into
// Element. Doesn't mutate inputs.
func (f *Field[T]) Sub(a, b *Element[T]) *Element[T] {
return f.reduceAndOp(f.sub, f.subPreCond, a, b)
}
// subReduce returns a-b and returns it. Contrary to [Field[T].Sub] method this
// method does not reduce the inputs if the result would overflow. This method
// is currently only used as a subroutine in [Field[T].Reduce] method to avoid
// infinite recursion when we are working exactly on the overflow limits.
func (f *Field[T]) subNoReduce(a, b *Element[T]) *Element[T] {
nextOverflow, _ := f.subPreCond(a, b)
// we ignore error as it only indicates if we should reduce or not. But we
// are in non-reducing version of sub.
return f.sub(a, b, nextOverflow)
}
func (f *Field[T]) subPreCond(a, b *Element[T]) (nextOverflow uint, err error) {
reduceRight := a.overflow < (b.overflow + 1)
nextOverflow = max(b.overflow+1, a.overflow) + 1
if nextOverflow > f.maxOverflow() {
err = overflowError{op: "sub", nextOverflow: nextOverflow, maxOverflow: f.maxOverflow(), reduceRight: reduceRight}
}
return
}
func (f *Field[T]) sub(a, b *Element[T], nextOverflow uint) *Element[T] {
ba, aConst := f.constantValue(a)
bb, bConst := f.constantValue(b)
if aConst && bConst {
ba.Sub(ba, bb).Mod(ba, f.fParams.Modulus())
return newConstElement[T](ba)
}
// first we have to compute padding to ensure that the subtraction does not
// underflow.
nbLimbs := max(len(a.Limbs), len(b.Limbs))
limbs := make([]frontend.Variable, nbLimbs)
padLimbs := subPadding[T](b.overflow, uint(nbLimbs))
for i := range limbs {
limbs[i] = padLimbs[i]
if i < len(a.Limbs) {
limbs[i] = f.api.Add(limbs[i], a.Limbs[i])
}
if i < len(b.Limbs) {
limbs[i] = f.api.Sub(limbs[i], b.Limbs[i])
}
}
return f.newInternalElement(limbs, nextOverflow)
}
func (f *Field[T]) Neg(a *Element[T]) *Element[T] {
return f.Sub(f.Zero(), a)
}
// Select sets e to a if selector == 1 and to b otherwise. Sets the number of
// limbs and overflow of the result to be the maximum of the limb lengths and
// overflows. If the inputs are strongly unbalanced, then it would better to
// reduce the result after the operation.
func (f *Field[T]) Select(selector frontend.Variable, a, b *Element[T]) *Element[T] {
f.enforceWidthConditional(a)
f.enforceWidthConditional(b)
overflow := max(a.overflow, b.overflow)
nbLimbs := max(len(a.Limbs), len(b.Limbs))
e := f.newInternalElement(make([]frontend.Variable, nbLimbs), overflow)
normalize := func(limbs []frontend.Variable) []frontend.Variable {
if len(limbs) < nbLimbs {
tail := make([]frontend.Variable, nbLimbs-len(limbs))
for i := range tail {
tail[i] = 0
}
return append(limbs, tail...)
}
return limbs
}
aNormLimbs := normalize(a.Limbs)
bNormLimbs := normalize(b.Limbs)
for i := range e.Limbs {
e.Limbs[i] = f.api.Select(selector, aNormLimbs[i], bNormLimbs[i])
}
return e
}
// Lookup2 performs two-bit lookup between a, b, c, d based on lookup bits b1
// and b2 such that:
// - if b0=0 and b1=0, sets to a,
// - if b0=1 and b1=0, sets to b,
// - if b0=0 and b1=1, sets to c,
// - if b0=1 and b1=1, sets to d.
//
// The number of the limbs and overflow in the result is the maximum of the
// inputs'. If the inputs are very unbalanced, then reduce the result.
func (f *Field[T]) Lookup2(b0, b1 frontend.Variable, a, b, c, d *Element[T]) *Element[T] {
f.enforceWidthConditional(a)
f.enforceWidthConditional(b)
f.enforceWidthConditional(c)
f.enforceWidthConditional(d)
overflow := max(a.overflow, b.overflow, c.overflow, d.overflow)
nbLimbs := max(len(a.Limbs), len(b.Limbs), len(c.Limbs), len(d.Limbs))
e := f.newInternalElement(make([]frontend.Variable, nbLimbs), overflow)
normalize := func(limbs []frontend.Variable) []frontend.Variable {
if len(limbs) < nbLimbs {
tail := make([]frontend.Variable, nbLimbs-len(limbs))
for i := range tail {
tail[i] = 0
}
return append(limbs, tail...)
}
return limbs
}
aNormLimbs := normalize(a.Limbs)
bNormLimbs := normalize(b.Limbs)
cNormLimbs := normalize(c.Limbs)
dNormLimbs := normalize(d.Limbs)
for i := range a.Limbs {
e.Limbs[i] = f.api.Lookup2(b0, b1, aNormLimbs[i], bNormLimbs[i], cNormLimbs[i], dNormLimbs[i])
}
return e
}
// Mux selects element inputs[sel] and returns it. The number of the limbs and
// overflow in the result is the maximum of the inputs'. If the inputs are very
// unbalanced, then reduce the inputs before calling the method. It is most
// efficient for power of two lengths of the inputs, but works for any
// number of inputs.
func (f *Field[T]) Mux(sel frontend.Variable, inputs ...*Element[T]) *Element[T] {
if len(inputs) == 0 {
return nil
}
nbInputs := len(inputs)
overflow := uint(0)
nbLimbs := 0
for i := range inputs {
f.enforceWidthConditional(inputs[i])
if inputs[i].overflow > overflow {
overflow = inputs[i].overflow
}
if len(inputs[i].Limbs) > nbLimbs {
nbLimbs = len(inputs[i].Limbs)
}
}
normalize := func(limbs []frontend.Variable) []frontend.Variable {
if len(limbs) < nbLimbs {
tail := make([]frontend.Variable, nbLimbs-len(limbs))
for i := range tail {
tail[i] = 0
}
return append(limbs, tail...)
}
return limbs
}
normLimbs := make([][]frontend.Variable, nbInputs)
for i := range inputs {
normLimbs[i] = normalize(inputs[i].Limbs)
}
normLimbsTransposed := make([][]frontend.Variable, nbLimbs)
for i := range normLimbsTransposed {
normLimbsTransposed[i] = make([]frontend.Variable, nbInputs)
for j := range normLimbsTransposed[i] {
normLimbsTransposed[i][j] = normLimbs[j][i]
}
}
e := f.newInternalElement(make([]frontend.Variable, nbLimbs), overflow)
for i := range inputs[0].Limbs {
e.Limbs[i] = selector.Mux(f.api, sel, normLimbsTransposed[i]...)
}
return e
}
// reduceAndOp applies op on the inputs. If the pre-condition check preCond
// errs, then first reduces the input arguments. The reduction is done
// one-by-one with the element with highest overflow reduced first.
func (f *Field[T]) reduceAndOp(op func(*Element[T], *Element[T], uint) *Element[T], preCond func(*Element[T], *Element[T]) (uint, error), a, b *Element[T]) *Element[T] {
f.enforceWidthConditional(a)
f.enforceWidthConditional(b)
var nextOverflow uint
var err error
var target overflowError
for nextOverflow, err = preCond(a, b); errors.As(err, &target); nextOverflow, err = preCond(a, b) {
if !target.reduceRight {
a = f.Reduce(a)
} else {
b = f.Reduce(b)
}
}
return op(a, b, nextOverflow)
}
type overflowError struct {
op string
nextOverflow uint
maxOverflow uint
reduceRight bool
}
func (e overflowError) Error() string {
return fmt.Sprintf("op %s overflow %d exceeds max %d", e.op, e.nextOverflow, e.maxOverflow)
}