forked from Consensys/gnark
/
e2.go
264 lines (204 loc) · 5.57 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
/*
Copyright © 2020 ConsenSys
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.
*/
package fields_bls24315
import (
"math/big"
bls24315 "github.com/consensys/gnark-crypto/ecc/bls24-315"
"github.com/consensys/gnark-crypto/ecc/bw6-633/fr"
"github.com/consensys/gnark/constraint/solver"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/internal/utils"
)
// E2 element in a quadratic extension
type E2 struct {
A0, A1 frontend.Variable
}
// SetZero returns a newly allocated element equal to 0
func (e *E2) SetZero() *E2 {
e.A0 = 0
e.A1 = 0
return e
}
// SetOne returns a newly allocated element equal to 1
func (e *E2) SetOne() *E2 {
e.A0 = 1
e.A1 = 0
return e
}
// IsZero returns 1 if the element is equal to 0 and 0 otherwise
func (e *E2) IsZero(api frontend.API) frontend.Variable {
return api.And(api.IsZero(e.A0), api.IsZero(e.A1))
}
func (e *E2) assign(e1 []frontend.Variable) {
e.A0 = e1[0]
e.A1 = e1[1]
}
// Neg negates a e2 elmt
func (e *E2) Neg(api frontend.API, e1 E2) *E2 {
e.A0 = api.Sub(0, e1.A0)
e.A1 = api.Sub(0, e1.A1)
return e
}
// Add e2 elmts
func (e *E2) Add(api frontend.API, e1, e2 E2) *E2 {
e.A0 = api.Add(e1.A0, e2.A0)
e.A1 = api.Add(e1.A1, e2.A1)
return e
}
// Double e2 elmt
func (e *E2) Double(api frontend.API, e1 E2) *E2 {
e.A0 = api.Add(e1.A0, e1.A0)
e.A1 = api.Add(e1.A1, e1.A1)
return e
}
// Sub e2 elmts
func (e *E2) Sub(api frontend.API, e1, e2 E2) *E2 {
e.A0 = api.Sub(e1.A0, e2.A0)
e.A1 = api.Sub(e1.A1, e2.A1)
return e
}
// Mul e2 elmts: 5C
func (e *E2) Mul(api frontend.API, e1, e2 E2) *E2 {
// 1C
l1 := api.Add(e1.A0, e1.A1)
l2 := api.Add(e2.A0, e2.A1)
u := api.Mul(l1, l2)
// 2C
ac := api.Mul(e1.A0, e2.A0)
bd := api.Mul(e1.A1, e2.A1)
// 1C
l31 := api.Add(ac, bd)
e.A1 = api.Sub(u, l31)
// 1C
buSquare := utils.FromInterface(ext.uSquare)
l41 := api.Mul(bd, buSquare)
e.A0 = api.Add(ac, l41)
return e
}
// Square e2 elt
func (e *E2) Square(api frontend.API, x E2) *E2 {
//Algorithm 22 from https://eprint.iacr.org/2010/354.pdf
c0 := api.Sub(x.A0, x.A1)
buSquare := utils.FromInterface(ext.uSquare)
c3 := api.Mul(x.A1, buSquare)
c3 = api.Sub(x.A0, c3)
c2 := api.Mul(x.A0, x.A1)
c0 = api.Mul(c0, c3)
c0 = api.Add(c0, c2)
e.A1 = api.Add(c2, c2)
c2 = api.Mul(c2, buSquare)
e.A0 = api.Add(c0, c2)
return e
}
// MulByFp multiplies an fp2 elmt by an fp elmt
func (e *E2) MulByFp(api frontend.API, e1 E2, c interface{}) *E2 {
e.A0 = api.Mul(e1.A0, c)
e.A1 = api.Mul(e1.A1, c)
return e
}
// MulByNonResidue multiplies an fp2 elmt by the imaginary elmt
// ext.uSquare is the square of the imaginary root
func (e *E2) MulByNonResidue(api frontend.API, e1 E2) *E2 {
e.A0, e.A1 = e1.A1, e1.A0
e.A0 = api.Mul(e.A0, ext.uSquare)
return e
}
// Conjugate conjugation of an e2 elmt
func (e *E2) Conjugate(api frontend.API, e1 E2) *E2 {
e.A0 = e1.A0
e.A1 = api.Sub(0, e1.A1)
return e
}
var DivE2Hint = func(_ *big.Int, inputs []*big.Int, res []*big.Int) error {
var a, b, c bls24315.E2
a.A0.SetBigInt(inputs[0])
a.A1.SetBigInt(inputs[1])
b.A0.SetBigInt(inputs[2])
b.A1.SetBigInt(inputs[3])
c.Inverse(&b).Mul(&c, &a)
c.A0.BigInt(res[0])
c.A1.BigInt(res[1])
return nil
}
func init() {
solver.RegisterHint(DivE2Hint)
}
// DivUnchecked e2 elmts
func (e *E2) DivUnchecked(api frontend.API, e1, e2 E2) *E2 {
res, err := api.NewHint(DivE2Hint, 2, e1.A0, e1.A1, e2.A0, e2.A1)
if err != nil {
// err is non-nil only for invalid number of inputs
panic(err)
}
var e3 E2
e3.assign(res[:2])
// e1 == e3 * e2
e3.Mul(api, e3, e2)
e3.AssertIsEqual(api, e1)
e.assign(res[:2])
return e
}
var InverseE2Hint = func(_ *big.Int, inputs []*big.Int, res []*big.Int) error {
var a, c bls24315.E2
a.A0.SetBigInt(inputs[0])
a.A1.SetBigInt(inputs[1])
c.Inverse(&a)
c.A0.BigInt(res[0])
c.A1.BigInt(res[1])
return nil
}
func init() {
solver.RegisterHint(InverseE2Hint)
}
// Inverse e2 elmts
func (e *E2) Inverse(api frontend.API, e1 E2) *E2 {
res, err := api.NewHint(InverseE2Hint, 2, e1.A0, e1.A1)
if err != nil {
// err is non-nil only for invalid number of inputs
panic(err)
}
var e3, one E2
e3.assign(res[:2])
one.SetOne()
// 1 == e3 * e1
e3.Mul(api, e3, e1)
e3.AssertIsEqual(api, one)
e.assign(res[:2])
return e
}
// Assign a value to self (witness assignment)
func (e *E2) Assign(a *bls24315.E2) {
e.A0 = (fr.Element)(a.A0)
e.A1 = (fr.Element)(a.A1)
}
// AssertIsEqual constraint self to be equal to other into the given constraint system
func (e *E2) AssertIsEqual(api frontend.API, other E2) {
api.AssertIsEqual(e.A0, other.A0)
api.AssertIsEqual(e.A1, other.A1)
}
// Select sets e to r1 if b=1, r2 otherwise
func (e *E2) Select(api frontend.API, b frontend.Variable, r1, r2 E2) *E2 {
e.A0 = api.Select(b, r1.A0, r2.A0)
e.A1 = api.Select(b, r1.A1, r2.A1)
return e
}
// Lookup2 implements two-bit lookup. It returns:
// - r1 if b1=0 and b2=0,
// - r2 if b1=0 and b2=1,
// - r3 if b1=1 and b2=0,
// - r3 if b1=1 and b2=1.
func (e *E2) Lookup2(api frontend.API, b1, b2 frontend.Variable, r1, r2, r3, r4 E2) *E2 {
e.A0 = api.Lookup2(b1, b2, r1.A0, r2.A0, r3.A0, r4.A0)
e.A1 = api.Lookup2(b1, b2, r1.A1, r2.A1, r3.A1, r4.A1)
return e
}