-
Notifications
You must be signed in to change notification settings - Fork 31
/
fr.go
344 lines (306 loc) · 8.46 KB
/
fr.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
package bls
import (
"crypto/rand"
"fmt"
"hash"
"io"
)
// FR is an element in a field.
type FR struct {
n *FRRepr
}
// RFieldModulus is the modulus of the R field.
var RFieldModulus, _ = FRReprFromString("52435875175126190479447740508185965837690552500527637822603658699938581184513", 10)
// IsValid checks if the element is valid.
func (f *FR) IsValid() bool {
return f.n.Cmp(RFieldModulus) < 0
}
func (f *FR) reduceAssign() {
if !f.IsValid() {
f.n.SubNoBorrow(RFieldModulus)
}
}
// Copy copies an FR element.
func (f *FR) Copy() *FR {
return &FR{f.n.Copy()}
}
// FRR is 2**256 % r used for moving numbers into Montgomery form.
var FRR, _ = FRReprFromString("10920338887063814464675503992315976177888879664585288394250266608035967270910", 10)
// FRR2 is R^2 % r.
var FRR2, _ = FRReprFromString("3294906474794265442129797520630710739278575682199800681788903916070560242797", 10)
// FRReprToFR gets a pointer to a FR given a pointer
// to an FRRepr
func FRReprToFR(o *FRRepr) *FR {
r := &FR{n: o}
if r.IsValid() {
r.MulAssign(&FR{FRR2})
return r
}
return nil
}
// AddAssign multiplies a field element by this one.
func (f *FR) AddAssign(other *FR) {
f.n.AddNoCarry(other.n)
f.reduceAssign()
}
const montInvFR = uint64(0xfffffffeffffffff)
func (f *FR) montReduce(r0 uint64, r1 uint64, r2 uint64, r3 uint64, r4 uint64, r5 uint64, r6 uint64, r7 uint64) {
k := r0 * montInvFR
_, carry := MACWithCarry(r0, k, RFieldModulus[0], 0)
r1, carry = MACWithCarry(r1, k, RFieldModulus[1], carry)
r2, carry = MACWithCarry(r2, k, RFieldModulus[2], carry)
r3, carry = MACWithCarry(r3, k, RFieldModulus[3], carry)
r4, carry = AddWithCarry(r4, 0, carry)
carry2 := carry
k = r1 * montInvFR
_, carry = MACWithCarry(r1, k, RFieldModulus[0], 0)
r2, carry = MACWithCarry(r2, k, RFieldModulus[1], carry)
r3, carry = MACWithCarry(r3, k, RFieldModulus[2], carry)
r4, carry = MACWithCarry(r4, k, RFieldModulus[3], carry)
r5, carry = AddWithCarry(r5, carry2, carry)
carry2 = carry
k = r2 * montInvFR
_, carry = MACWithCarry(r2, k, RFieldModulus[0], 0)
r3, carry = MACWithCarry(r3, k, RFieldModulus[1], carry)
r4, carry = MACWithCarry(r4, k, RFieldModulus[2], carry)
r5, carry = MACWithCarry(r5, k, RFieldModulus[3], carry)
r6, carry = AddWithCarry(r6, carry2, carry)
carry2 = carry
k = r3 * montInvFR
_, carry = MACWithCarry(r3, k, RFieldModulus[0], 0)
r4, carry = MACWithCarry(r4, k, RFieldModulus[1], carry)
r5, carry = MACWithCarry(r5, k, RFieldModulus[2], carry)
r6, carry = MACWithCarry(r6, k, RFieldModulus[3], carry)
r7, _ = AddWithCarry(r7, carry2, carry)
f.n[0] = r4
f.n[1] = r5
f.n[2] = r6
f.n[3] = r7
f.reduceAssign()
}
// MulAssign multiplies a field element by this one.
func (f FR) MulAssign(other *FR) {
r0, carry := MACWithCarry(0, f.n[0], other.n[0], 0)
r1, carry := MACWithCarry(0, f.n[0], other.n[1], carry)
r2, carry := MACWithCarry(0, f.n[0], other.n[2], carry)
r3, carry := MACWithCarry(0, f.n[0], other.n[3], carry)
r4 := carry
r1, carry = MACWithCarry(r1, f.n[1], other.n[0], 0)
r2, carry = MACWithCarry(r2, f.n[1], other.n[1], carry)
r3, carry = MACWithCarry(r3, f.n[1], other.n[2], carry)
r4, carry = MACWithCarry(r4, f.n[1], other.n[3], carry)
r5 := carry
r2, carry = MACWithCarry(r2, f.n[2], other.n[0], 0)
r3, carry = MACWithCarry(r3, f.n[2], other.n[1], carry)
r4, carry = MACWithCarry(r4, f.n[2], other.n[2], carry)
r5, carry = MACWithCarry(r5, f.n[2], other.n[3], carry)
r6 := carry
r3, carry = MACWithCarry(r3, f.n[3], other.n[0], 0)
r4, carry = MACWithCarry(r4, f.n[3], other.n[1], carry)
r5, carry = MACWithCarry(r5, f.n[3], other.n[2], carry)
r6, carry = MACWithCarry(r6, f.n[3], other.n[3], carry)
r7 := carry
f.montReduce(r0, r1, r2, r3, r4, r5, r6, r7)
}
// SubAssign subtracts a field element from this one.
func (f *FR) SubAssign(other *FR) {
if other.n.Cmp(f.n) > 0 {
f.n.AddNoCarry(RFieldModulus)
}
f.n.SubNoBorrow(other.n)
}
var frOne = NewFRRepr(1)
var frZero = NewFRRepr(0)
var bigOneFR = FRReprToFR(frOne)
var bigZeroFR = FRReprToFR(frZero)
// Exp raises the element to a specific power.
func (f *FR) Exp(n *FRRepr) *FR {
nCopy := n.Copy()
fi := f.Copy()
fNew := bigOneFR.Copy()
for nCopy.Cmp(frZero) != 0 {
if nCopy.IsOdd() {
fNew.MulAssign(fi)
}
fi.SquareAssign()
nCopy.Rsh(1)
}
return fNew
}
// Equals checks equality of two field elements.
func (f FR) Equals(other *FR) bool {
return f.n.Equals(other.n)
}
// NegAssign gets the negative value of the field element mod RFieldModulus.
func (f *FR) NegAssign() {
if !f.IsZero() {
tmp := RFieldModulus.Copy()
tmp.SubNoBorrow(f.n)
f.n = tmp
}
}
func (f FR) String() string {
return fmt.Sprintf("FR(0x%s)", f.ToRepr().String())
}
// Cmp compares this field element to another.
func (f FR) Cmp(other *FR) int {
return f.ToRepr().Cmp(other.ToRepr())
}
// DoubleAssign doubles the element
func (f *FR) DoubleAssign() {
f.n.Mul2()
f.reduceAssign()
}
// IsZero checks if the field element is zero.
func (f FR) IsZero() bool {
return f.n.Cmp(frZero) == 0
}
// SquareAssign squares a field element.
func (f *FR) SquareAssign() {
r1, carry := MACWithCarry(0, f.n[0], f.n[1], 0)
r2, carry := MACWithCarry(0, f.n[0], f.n[2], carry)
r3, carry := MACWithCarry(0, f.n[0], f.n[3], carry)
r4 := carry
r3, carry = MACWithCarry(r3, f.n[1], f.n[2], 0)
r4, carry = MACWithCarry(r4, f.n[1], f.n[3], carry)
r5 := carry
r5, carry = MACWithCarry(r5, f.n[2], f.n[3], 0)
r6 := carry
r7 := r6 >> 63
r6 = (r6 << 1) | (r5 >> 63)
r5 = (r5 << 1) | (r4 >> 63)
r4 = (r4 << 1) | (r3 >> 63)
r3 = (r3 << 1) | (r2 >> 63)
r2 = (r2 << 1) | (r1 >> 63)
r1 = r1 << 1
carry = 0
r0, carry := MACWithCarry(0, f.n[0], f.n[0], carry)
r1, carry = AddWithCarry(r1, 0, carry)
r2, carry = MACWithCarry(r2, f.n[1], f.n[1], carry)
r3, carry = AddWithCarry(r3, 0, carry)
r4, carry = MACWithCarry(r4, f.n[2], f.n[2], carry)
r5, carry = AddWithCarry(r5, 0, carry)
r6, carry = MACWithCarry(r6, f.n[3], f.n[3], carry)
r7, carry = AddWithCarry(r7, 0, carry)
f.montReduce(r0, r1, r2, r3, r4, r5, r6, r7)
}
// Sqrt calculates the square root of the field element.
func (f FR) Sqrt() *FR {
// TODO: fixme
return FRReprToFR(frOne)
}
// Inverse finds the inverse of the field element.
func (f FR) Inverse() *FR {
if f.IsZero() {
return nil
}
u := f.n.Copy()
v := RFieldModulus.Copy()
b := &FR{FRR2.Copy()}
c := bigZeroFR.Copy()
one := &FRRepr{1, 0, 0, 0}
for u.Cmp(one) != 0 && v.Cmp(one) != 0 {
for u.IsEven() {
u.Div2()
if b.n.IsEven() {
b.n.Div2()
} else {
b.n.AddNoCarry(RFieldModulus)
b.n.Div2()
}
}
for v.IsEven() {
v.Div2()
if c.n.IsEven() {
c.n.Div2()
} else {
c.n.AddNoCarry(RFieldModulus)
c.n.Div2()
}
}
if u.Cmp(v) >= 0 {
u.SubNoBorrow(v)
b.SubAssign(c)
} else {
v.SubNoBorrow(u)
c.SubAssign(b)
}
}
if u.Equals(one) {
return b
}
return c
}
// Parity checks if the point is greater than the point negated.
func (f FR) Parity() bool {
neg := f.Copy()
neg.NegAssign()
return f.Cmp(neg) > 0
}
// MulBits multiplies the number by a big number.
func (f FR) MulBits(b *FRRepr) *FR {
res := bigZeroFR.Copy()
for i := uint(0); i < b.BitLen(); i++ {
res.DoubleAssign()
if b.Bit(i) {
res.AddAssign(&f)
}
}
return res
}
// MulBytes multiplies the number by some bytes.
func (f FR) MulBytes(b []byte) *FR {
res := bigZeroFR.Copy()
for i := uint(0); i < uint(len(b)*8); i++ {
res.DoubleAssign()
if b[i/8]&(1<<(i%8)) != 0 {
res.AddAssign(&f)
}
}
return res
}
// HashFR calculates a new FR2 value based on a hash.
func HashFR(hasher hash.Hash) *FR {
digest := hasher.Sum(nil)
return bigOneFR.MulBytes(digest)
}
var rMinus1Over2, _ = FRReprFromString("26217937587563095239723870254092982918845276250263818911301829349969290592256", 10)
// Legendre gets the legendre symbol of the element.
func (f *FR) Legendre() LegendreSymbol {
o := f.Exp(rMinus1Over2)
if o.IsZero() {
return LegendreZero
} else if o.Equals(bigOneFR) {
return LegendreQuadraticResidue
} else {
return LegendreQuadraticNonResidue
}
}
// ToRepr gets the 256-bit representation of the field element.
func (f *FR) ToRepr() *FRRepr {
out := f.Copy()
out.montReduce(
f.n[0],
f.n[1],
f.n[2],
f.n[3],
0,
0,
0,
0,
)
return out.n
}
// Bytes gets the representation of the FR in bytes.
func (f *FR) Bytes() [32]byte {
return f.ToRepr().Bytes()
}
// RandFR generates a random FR element.
func RandFR(reader io.Reader) (*FR, error) {
r, err := rand.Int(reader, RFieldModulus.ToBig())
if err != nil {
return nil, err
}
b, _ := FRReprFromBigInt(r)
return FRReprToFR(b), nil
}