forked from Consensys/gnark-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2.go
296 lines (251 loc) · 6.09 KB
/
e2.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
// Copyright 2020 ConsenSys Software Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by consensys/gnark-crypto DO NOT EDIT
package fptower
import (
"github.com/consensys/gnark-crypto/ecc/bls12-377/fp"
"math/big"
)
// E2 is a degree two finite field extension of fp.Element
type E2 struct {
A0, A1 fp.Element
}
// Equal returns true if z equals x, false otherwise
func (z *E2) Equal(x *E2) bool {
return z.A0.Equal(&x.A0) && z.A1.Equal(&x.A1)
}
// Cmp compares (lexicographic order) z and x and returns:
//
// -1 if z < x
// 0 if z == x
// +1 if z > x
//
func (z *E2) Cmp(x *E2) int {
if a1 := z.A1.Cmp(&x.A1); a1 != 0 {
return a1
}
return z.A0.Cmp(&x.A0)
}
// LexicographicallyLargest returns true if this element is strictly lexicographically
// larger than its negation, false otherwise
func (z *E2) LexicographicallyLargest() bool {
// adapted from github.com/zkcrypto/bls12_381
if z.A1.IsZero() {
return z.A0.LexicographicallyLargest()
}
return z.A1.LexicographicallyLargest()
}
// SetString sets a E2 element from strings
func (z *E2) SetString(s1, s2 string) *E2 {
z.A0.SetString(s1)
z.A1.SetString(s2)
return z
}
// SetZero sets an E2 elmt to zero
func (z *E2) SetZero() *E2 {
z.A0.SetZero()
z.A1.SetZero()
return z
}
// Set sets an E2 from x
func (z *E2) Set(x *E2) *E2 {
z.A0 = x.A0
z.A1 = x.A1
return z
}
// SetOne sets z to 1 in Montgomery form and returns z
func (z *E2) SetOne() *E2 {
z.A0.SetOne()
z.A1.SetZero()
return z
}
// SetRandom sets a0 and a1 to random values
func (z *E2) SetRandom() (*E2, error) {
if _, err := z.A0.SetRandom(); err != nil {
return nil, err
}
if _, err := z.A1.SetRandom(); err != nil {
return nil, err
}
return z, nil
}
// IsZero returns true if the two elements are equal, false otherwise
func (z *E2) IsZero() bool {
return z.A0.IsZero() && z.A1.IsZero()
}
// Add adds two elements of E2
func (z *E2) Add(x, y *E2) *E2 {
addE2(z, x, y)
return z
}
// Sub two elements of E2
func (z *E2) Sub(x, y *E2) *E2 {
subE2(z, x, y)
return z
}
// Double doubles an E2 element
func (z *E2) Double(x *E2) *E2 {
doubleE2(z, x)
return z
}
// Neg negates an E2 element
func (z *E2) Neg(x *E2) *E2 {
negE2(z, x)
return z
}
// String implements Stringer interface for fancy printing
func (z *E2) String() string {
return z.A0.String() + "+" + z.A1.String() + "*u"
}
// ToMont converts to mont form
func (z *E2) ToMont() *E2 {
z.A0.ToMont()
z.A1.ToMont()
return z
}
// FromMont converts from mont form
func (z *E2) FromMont() *E2 {
z.A0.FromMont()
z.A1.FromMont()
return z
}
// MulByElement multiplies an element in E2 by an element in fp
func (z *E2) MulByElement(x *E2, y *fp.Element) *E2 {
var yCopy fp.Element
yCopy.Set(y)
z.A0.Mul(&x.A0, &yCopy)
z.A1.Mul(&x.A1, &yCopy)
return z
}
// Conjugate conjugates an element in E2
func (z *E2) Conjugate(x *E2) *E2 {
z.A0 = x.A0
z.A1.Neg(&x.A1)
return z
}
// Halve sets z = z / 2
func (z *E2) Halve() {
z.A0.Halve()
z.A1.Halve()
}
// Legendre returns the Legendre symbol of z
func (z *E2) Legendre() int {
var n fp.Element
z.norm(&n)
return n.Legendre()
}
// Exp sets z=xᵏ (mod q²) and returns it
func (z *E2) Exp(x E2, k *big.Int) *E2 {
if k.IsUint64() && k.Uint64() == 0 {
return z.SetOne()
}
e := k
if k.Sign() == -1 {
// negative k, we invert
// if k < 0: xᵏ (mod q²) == (x⁻¹)ᵏ (mod q²)
x.Inverse(&x)
// we negate k in a temp big.Int since
// Int.Bit(_) of k and -k is different
e = bigIntPool.Get().(*big.Int)
defer bigIntPool.Put(e)
e.Neg(k)
}
z.SetOne()
b := e.Bytes()
for i := 0; i < len(b); i++ {
w := b[i]
for j := 0; j < 8; j++ {
z.Square(z)
if (w & (0b10000000 >> j)) != 0 {
z.Mul(z, &x)
}
}
}
return z
}
// Sqrt sets z to the square root of and returns z
// The function does not test wether the square root
// exists or not, it's up to the caller to call
// Legendre beforehand.
// cf https://eprint.iacr.org/2012/685.pdf (algo 10)
func (z *E2) Sqrt(x *E2) *E2 {
// precomputation
var b, c, d, e, f, x0 E2
var _b, o fp.Element
// c must be a non square (works for p=1 mod 12 hence 1 mod 4, only bls377 has such a p currently)
c.A1.SetOne()
q := fp.Modulus()
var exp, one big.Int
one.SetUint64(1)
exp.Set(q).Sub(&exp, &one).Rsh(&exp, 1)
d.Exp(c, &exp)
e.Mul(&d, &c).Inverse(&e)
f.Mul(&d, &c).Square(&f)
// computation
exp.Rsh(&exp, 1)
b.Exp(*x, &exp)
b.norm(&_b)
o.SetOne()
if _b.Equal(&o) {
x0.Square(&b).Mul(&x0, x)
_b.Set(&x0.A0).Sqrt(&_b)
z.Conjugate(&b).MulByElement(z, &_b)
return z
}
x0.Square(&b).Mul(&x0, x).Mul(&x0, &f)
_b.Set(&x0.A0).Sqrt(&_b)
z.Conjugate(&b).MulByElement(z, &_b).Mul(z, &e)
return z
}
// BatchInvertE2 returns a new slice with every element inverted.
// Uses Montgomery batch inversion trick
//
// if a[i] == 0, returns result[i] = a[i]
func BatchInvertE2(a []E2) []E2 {
res := make([]E2, len(a))
if len(a) == 0 {
return res
}
zeroes := make([]bool, len(a))
var accumulator E2
accumulator.SetOne()
for i := 0; i < len(a); i++ {
if a[i].IsZero() {
zeroes[i] = true
continue
}
res[i].Set(&accumulator)
accumulator.Mul(&accumulator, &a[i])
}
accumulator.Inverse(&accumulator)
for i := len(a) - 1; i >= 0; i-- {
if zeroes[i] {
continue
}
res[i].Mul(&res[i], &accumulator)
accumulator.Mul(&accumulator, &a[i])
}
return res
}
func (z *E2) Select(cond int, caseZ *E2, caseNz *E2) *E2 {
//Might be able to save a nanosecond or two by an aggregate implementation
z.A0.Select(cond, &caseZ.A0, &caseNz.A0)
z.A1.Select(cond, &caseZ.A1, &caseNz.A1)
return z
}
func (z *E2) Div(x *E2, y *E2) *E2 {
var r E2
r.Inverse(y).Mul(x, &r)
return z.Set(&r)
}