go-snark/bn128/bn128.go
bn128/bn128.goRaw
1package bn128
2
3import (
4 "errors"
5 "math/big"
6
7 "github.com/arnaucube/go-snark/fields"
8)
9
10type Bn128 struct {
11 Q *big.Int
12 R *big.Int
13 Gg1 [2]*big.Int
14 Gg2 [2][2]*big.Int
15 NonResidueFq2 *big.Int
16 NonResidueFq6 [2]*big.Int
17 Fq1 fields.Fq
18 Fq2 fields.Fq2
19 Fq6 fields.Fq6
20 Fq12 fields.Fq12
21 G1 G1
22 G2 G2
23 LoopCount *big.Int
24 LoopCountNeg bool
25
26 TwoInv *big.Int
27 CoefB *big.Int
28 TwistCoefB [2]*big.Int
29 Twist [2]*big.Int
30 FrobeniusCoeffsC11 *big.Int
31 TwistMulByQX [2]*big.Int
32 TwistMulByQY [2]*big.Int
33 FinalExp *big.Int
34}
35
36func NewBn128() (Bn128, error) {
37 var b Bn128
38 q, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10)
39 if !ok {
40 return b, errors.New("err with q")
41 }
42 b.Q = q
43
44 r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
45 if !ok {
46 return b, errors.New("err with r")
47 }
48 b.R = r
49
50 b.Gg1 = [2]*big.Int{
51 big.NewInt(int64(1)),
52 big.NewInt(int64(2)),
53 }
54
55 g2_00, ok := new(big.Int).SetString("10857046999023057135944570762232829481370756359578518086990519993285655852781", 10)
56 if !ok {
57 return b, errors.New("err with g2_00")
58 }
59 g2_01, ok := new(big.Int).SetString("11559732032986387107991004021392285783925812861821192530917403151452391805634", 10)
60 if !ok {
61 return b, errors.New("err with g2_00")
62 }
63 g2_10, ok := new(big.Int).SetString("8495653923123431417604973247489272438418190587263600148770280649306958101930", 10)
64 if !ok {
65 return b, errors.New("err with g2_00")
66 }
67 g2_11, ok := new(big.Int).SetString("4082367875863433681332203403145435568316851327593401208105741076214120093531", 10)
68 if !ok {
69 return b, errors.New("err with g2_00")
70 }
71
72 b.Gg2 = [2][2]*big.Int{
73 [2]*big.Int{
74 g2_00,
75 g2_01,
76 },
77 [2]*big.Int{
78 g2_10,
79 g2_11,
80 },
81 }
82
83 b.Fq1 = fields.NewFq(q)
84 b.NonResidueFq2, ok = new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) // i
85 if !ok {
86 return b, errors.New("err with nonResidueFq2")
87 }
88 b.NonResidueFq6 = [2]*big.Int{
89 big.NewInt(int64(9)),
90 big.NewInt(int64(1)),
91 }
92
93 b.Fq2 = fields.NewFq2(b.Fq1, b.NonResidueFq2)
94 b.Fq6 = fields.NewFq6(b.Fq2, b.NonResidueFq6)
95 b.Fq12 = fields.NewFq12(b.Fq6, b.Fq2, b.NonResidueFq6)
96
97 b.G1 = NewG1(b.Fq1, b.Gg1)
98 b.G2 = NewG2(b.Fq2, b.Gg2)
99
100 err := b.preparePairing()
101 if err != nil {
102 return b, err
103 }
104
105 return b, nil
106}
107
108func NewFqR() (fields.Fq, error){
109 r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
110 if !ok {
111 return fields.Fq{}, errors.New("err parsing R")
112 }
113 fqR := fields.NewFq(r)
114 return fqR, nil
115}
116
117func (bn128 *Bn128) preparePairing() error {
118 var ok bool
119 bn128.LoopCount, ok = new(big.Int).SetString("29793968203157093288", 10)
120 if !ok {
121 return errors.New("err with LoopCount from string")
122 }
123
124 bn128.LoopCountNeg = false
125
126 bn128.TwoInv = bn128.Fq1.Inverse(big.NewInt(int64(2)))
127
128 bn128.CoefB = big.NewInt(int64(3))
129 bn128.Twist = [2]*big.Int{
130 big.NewInt(int64(9)),
131 big.NewInt(int64(1)),
132 }
133 bn128.TwistCoefB = bn128.Fq2.MulScalar(bn128.Fq2.Inverse(bn128.Twist), bn128.CoefB)
134
135 bn128.FrobeniusCoeffsC11, ok = new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10)
136 if !ok {
137 return errors.New("error parsing frobeniusCoeffsC11")
138 }
139
140 a, ok := new(big.Int).SetString("21575463638280843010398324269430826099269044274347216827212613867836435027261", 10)
141 if !ok {
142 return errors.New("error parsing a")
143 }
144 b, ok := new(big.Int).SetString("10307601595873709700152284273816112264069230130616436755625194854815875713954", 10)
145 if !ok {
146 return errors.New("error parsing b")
147 }
148 bn128.TwistMulByQX = [2]*big.Int{
149 a,
150 b,
151 }
152
153 a, ok = new(big.Int).SetString("2821565182194536844548159561693502659359617185244120367078079554186484126554", 10)
154 if !ok {
155 return errors.New("error parsing a")
156 }
157 b, ok = new(big.Int).SetString("3505843767911556378687030309984248845540243509899259641013678093033130930403", 10)
158 if !ok {
159 return errors.New("error parsing b")
160 }
161 bn128.TwistMulByQY = [2]*big.Int{
162 a,
163 b,
164 }
165
166 bn128.FinalExp, ok = new(big.Int).SetString("552484233613224096312617126783173147097382103762957654188882734314196910839907541213974502761540629817009608548654680343627701153829446747810907373256841551006201639677726139946029199968412598804882391702273019083653272047566316584365559776493027495458238373902875937659943504873220554161550525926302303331747463515644711876653177129578303191095900909191624817826566688241804408081892785725967931714097716709526092261278071952560171111444072049229123565057483750161460024353346284167282452756217662335528813519139808291170539072125381230815729071544861602750936964829313608137325426383735122175229541155376346436093930287402089517426973178917569713384748081827255472576937471496195752727188261435633271238710131736096299798168852925540549342330775279877006784354801422249722573783561685179618816480037695005515426162362431072245638324744480", 10)
167 if !ok {
168 return errors.New("error parsing finalExp")
169 }
170
171 return nil
172
173}
174
175func (bn128 Bn128) Pairing(p1 [3]*big.Int, p2 [3][2]*big.Int) ([2][3][2]*big.Int, error) {
176 pre1 := bn128.PreComputeG1(p1)
177 pre2, err := bn128.PreComputeG2(p2)
178 if err != nil {
179 return [2][3][2]*big.Int{}, err
180 }
181
182 r1 := bn128.MillerLoop(pre1, pre2)
183 res := bn128.FinalExponentiation(r1)
184 return res, nil
185}
186
187type AteG1Precomp struct {
188 Px *big.Int
189 Py *big.Int
190}
191
192func (bn128 Bn128) PreComputeG1(p [3]*big.Int) AteG1Precomp {
193 pCopy := bn128.G1.Affine(p)
194 res := AteG1Precomp{
195 Px: pCopy[0],
196 Py: pCopy[1],
197 }
198 return res
199}
200
201type EllCoeffs struct {
202 Ell0 [2]*big.Int
203 EllVW [2]*big.Int
204 EllVV [2]*big.Int
205}
206type AteG2Precomp struct {
207 Qx [2]*big.Int
208 Qy [2]*big.Int
209 Coeffs []EllCoeffs
210}
211
212func (bn128 Bn128) PreComputeG2(p [3][2]*big.Int) (AteG2Precomp, error) {
213 qCopy := bn128.G2.Affine(p)
214 res := AteG2Precomp{
215 qCopy[0],
216 qCopy[1],
217 []EllCoeffs{},
218 }
219 r := [3][2]*big.Int{
220 bn128.Fq2.Copy(qCopy[0]),
221 bn128.Fq2.Copy(qCopy[1]),
222 bn128.Fq2.One(),
223 }
224 var c EllCoeffs
225 for i := bn128.LoopCount.BitLen() - 2; i >= 0; i-- {
226 bit := bn128.LoopCount.Bit(i)
227
228 c, r = bn128.DoublingStep(r)
229 res.Coeffs = append(res.Coeffs, c)
230 if bit == 1 {
231 c, r = bn128.MixedAdditionStep(qCopy, r)
232 res.Coeffs = append(res.Coeffs, c)
233 }
234 }
235
236 q1 := bn128.G2.Affine(bn128.G2MulByQ(qCopy))
237 if !bn128.Fq2.Equal(q1[2], bn128.Fq2.One()) {
238 return res, errors.New("q1[2] != Fq2.One")
239 }
240 q2 := bn128.G2.Affine(bn128.G2MulByQ(q1))
241 if !bn128.Fq2.Equal(q2[2], bn128.Fq2.One()) {
242 return res, errors.New("q2[2] != Fq2.One")
243 }
244
245 if bn128.LoopCountNeg {
246 r[1] = bn128.Fq2.Neg(r[1])
247 }
248 q2[1] = bn128.Fq2.Neg(q2[1])
249
250 c, r = bn128.MixedAdditionStep(q1, r)
251 res.Coeffs = append(res.Coeffs, c)
252
253 c, r = bn128.MixedAdditionStep(q2, r)
254 res.Coeffs = append(res.Coeffs, c)
255
256 return res, nil
257}
258
259func (bn128 Bn128) DoublingStep(current [3][2]*big.Int) (EllCoeffs, [3][2]*big.Int) {
260 x := current[0]
261 y := current[1]
262 z := current[2]
263
264 a := bn128.Fq2.MulScalar(bn128.Fq2.Mul(x, y), bn128.TwoInv)
265 b := bn128.Fq2.Square(y)
266 c := bn128.Fq2.Square(z)
267 d := bn128.Fq2.Add(c, bn128.Fq2.Add(c, c))
268 e := bn128.Fq2.Mul(bn128.TwistCoefB, d)
269 f := bn128.Fq2.Add(e, bn128.Fq2.Add(e, e))
270 g := bn128.Fq2.MulScalar(bn128.Fq2.Add(b, f), bn128.TwoInv)
271 h := bn128.Fq2.Sub(
272 bn128.Fq2.Square(bn128.Fq2.Add(y, z)),
273 bn128.Fq2.Add(b, c))
274 i := bn128.Fq2.Sub(e, b)
275 j := bn128.Fq2.Square(x)
276 eSqr := bn128.Fq2.Square(e)
277 current[0] = bn128.Fq2.Mul(a, bn128.Fq2.Sub(b, f))
278 current[1] = bn128.Fq2.Sub(bn128.Fq2.Sub(bn128.Fq2.Square(g), eSqr),
279 bn128.Fq2.Add(eSqr, eSqr))
280 current[2] = bn128.Fq2.Mul(b, h)
281 res := EllCoeffs{
282 Ell0: bn128.Fq2.Mul(i, bn128.Twist),
283 EllVW: bn128.Fq2.Neg(h),
284 EllVV: bn128.Fq2.Add(j, bn128.Fq2.Add(j, j)),
285 }
286
287 return res, current
288}
289
290func (bn128 Bn128) MixedAdditionStep(base, current [3][2]*big.Int) (EllCoeffs, [3][2]*big.Int) {
291 x1 := current[0]
292 y1 := current[1]
293 z1 := current[2]
294 x2 := base[0]
295 y2 := base[1]
296
297 d := bn128.Fq2.Sub(x1, bn128.Fq2.Mul(x2, z1))
298 e := bn128.Fq2.Sub(y1, bn128.Fq2.Mul(y2, z1))
299 f := bn128.Fq2.Square(d)
300 g := bn128.Fq2.Square(e)
301 h := bn128.Fq2.Mul(d, f)
302 i := bn128.Fq2.Mul(x1, f)
303 j := bn128.Fq2.Sub(
304 bn128.Fq2.Add(h, bn128.Fq2.Mul(z1, g)),
305 bn128.Fq2.Add(i, i))
306
307 current[0] = bn128.Fq2.Mul(d, j)
308 current[1] = bn128.Fq2.Sub(
309 bn128.Fq2.Mul(e, bn128.Fq2.Sub(i, j)),
310 bn128.Fq2.Mul(h, y1))
311 current[2] = bn128.Fq2.Mul(z1, h)
312
313 coef := EllCoeffs{
314 Ell0: bn128.Fq2.Mul(
315 bn128.Twist,
316 bn128.Fq2.Sub(
317 bn128.Fq2.Mul(e, x2),
318 bn128.Fq2.Mul(d, y2))),
319 EllVW: d,
320 EllVV: bn128.Fq2.Neg(e),
321 }
322 return coef, current
323}
324func (bn128 Bn128) G2MulByQ(p [3][2]*big.Int) [3][2]*big.Int {
325 fmx := [2]*big.Int{
326 p[0][0],
327 bn128.Fq1.Mul(p[0][1], bn128.Fq1.Copy(bn128.FrobeniusCoeffsC11)),
328 }
329 fmy := [2]*big.Int{
330 p[1][0],
331 bn128.Fq1.Mul(p[1][1], bn128.Fq1.Copy(bn128.FrobeniusCoeffsC11)),
332 }
333 fmz := [2]*big.Int{
334 p[2][0],
335 bn128.Fq1.Mul(p[2][1], bn128.Fq1.Copy(bn128.FrobeniusCoeffsC11)),
336 }
337
338 return [3][2]*big.Int{
339 bn128.Fq2.Mul(bn128.TwistMulByQX, fmx),
340 bn128.Fq2.Mul(bn128.TwistMulByQY, fmy),
341 fmz,
342 }
343}
344
345func (bn128 Bn128) MillerLoop(pre1 AteG1Precomp, pre2 AteG2Precomp) [2][3][2]*big.Int {
346 // https://cryptojedi.org/papers/dclxvi-20100714.pdf
347 // https://eprint.iacr.org/2008/096.pdf
348
349 idx := 0
350 var c EllCoeffs
351 f := bn128.Fq12.One()
352
353 for i := bn128.LoopCount.BitLen() - 2; i >= 0; i-- {
354 bit := bn128.LoopCount.Bit(i)
355
356 c = pre2.Coeffs[idx]
357 idx++
358 f = bn128.Fq12.Square(f)
359
360 f = bn128.MulBy024(f,
361 c.Ell0,
362 bn128.Fq2.MulScalar(c.EllVW, pre1.Py),
363 bn128.Fq2.MulScalar(c.EllVV, pre1.Px))
364
365 if bit == 1 {
366 c = pre2.Coeffs[idx]
367 idx++
368 f = bn128.MulBy024(
369 f,
370 c.Ell0,
371 bn128.Fq2.MulScalar(c.EllVW, pre1.Py),
372 bn128.Fq2.MulScalar(c.EllVV, pre1.Px))
373 }
374 }
375 if bn128.LoopCountNeg {
376 f = bn128.Fq12.Inverse(f)
377 }
378
379 c = pre2.Coeffs[idx]
380 idx++
381 f = bn128.MulBy024(
382 f,
383 c.Ell0,
384 bn128.Fq2.MulScalar(c.EllVW, pre1.Py),
385 bn128.Fq2.MulScalar(c.EllVV, pre1.Px))
386
387 c = pre2.Coeffs[idx]
388 idx++
389
390 f = bn128.MulBy024(
391 f,
392 c.Ell0,
393 bn128.Fq2.MulScalar(c.EllVW, pre1.Py),
394 bn128.Fq2.MulScalar(c.EllVV, pre1.Px))
395
396 return f
397}
398
399func (bn128 Bn128) MulBy024(a [2][3][2]*big.Int, ell0, ellVW, ellVV [2]*big.Int) [2][3][2]*big.Int {
400 b := [2][3][2]*big.Int{
401 [3][2]*big.Int{
402 ell0,
403 bn128.Fq2.Zero(),
404 ellVV,
405 },
406 [3][2]*big.Int{
407 bn128.Fq2.Zero(),
408 ellVW,
409 bn128.Fq2.Zero(),
410 },
411 }
412 return bn128.Fq12.Mul(a, b)
413}
414
415func (bn128 Bn128) FinalExponentiation(r [2][3][2]*big.Int) [2][3][2]*big.Int {
416 res := bn128.Fq12.Exp(r, bn128.FinalExp)
417 return res
418}