arnaucube
arnaucube/go-snarkPublic Self-hosted repository
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 (bn128 *Bn128) preparePairing() error {
109	var ok bool
110	bn128.LoopCount, ok = new(big.Int).SetString("29793968203157093288", 10)
111	if !ok {
112		return errors.New("err with LoopCount from string")
113	}
114
115	bn128.LoopCountNeg = false
116
117	bn128.TwoInv = bn128.Fq1.Inverse(big.NewInt(int64(2)))
118
119	bn128.CoefB = big.NewInt(int64(3))
120	bn128.Twist = [2]*big.Int{
121		big.NewInt(int64(9)),
122		big.NewInt(int64(1)),
123	}
124	bn128.TwistCoefB = bn128.Fq2.MulScalar(bn128.Fq2.Inverse(bn128.Twist), bn128.CoefB)
125
126	bn128.FrobeniusCoeffsC11, ok = new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10)
127	if !ok {
128		return errors.New("error parsing frobeniusCoeffsC11")
129	}
130
131	a, ok := new(big.Int).SetString("21575463638280843010398324269430826099269044274347216827212613867836435027261", 10)
132	if !ok {
133		return errors.New("error parsing a")
134	}
135	b, ok := new(big.Int).SetString("10307601595873709700152284273816112264069230130616436755625194854815875713954", 10)
136	if !ok {
137		return errors.New("error parsing b")
138	}
139	bn128.TwistMulByQX = [2]*big.Int{
140		a,
141		b,
142	}
143
144	a, ok = new(big.Int).SetString("2821565182194536844548159561693502659359617185244120367078079554186484126554", 10)
145	if !ok {
146		return errors.New("error parsing a")
147	}
148	b, ok = new(big.Int).SetString("3505843767911556378687030309984248845540243509899259641013678093033130930403", 10)
149	if !ok {
150		return errors.New("error parsing b")
151	}
152	bn128.TwistMulByQY = [2]*big.Int{
153		a,
154		b,
155	}
156
157	bn128.FinalExp, ok = new(big.Int).SetString("552484233613224096312617126783173147097382103762957654188882734314196910839907541213974502761540629817009608548654680343627701153829446747810907373256841551006201639677726139946029199968412598804882391702273019083653272047566316584365559776493027495458238373902875937659943504873220554161550525926302303331747463515644711876653177129578303191095900909191624817826566688241804408081892785725967931714097716709526092261278071952560171111444072049229123565057483750161460024353346284167282452756217662335528813519139808291170539072125381230815729071544861602750936964829313608137325426383735122175229541155376346436093930287402089517426973178917569713384748081827255472576937471496195752727188261435633271238710131736096299798168852925540549342330775279877006784354801422249722573783561685179618816480037695005515426162362431072245638324744480", 10)
158	if !ok {
159		return errors.New("error parsing finalExp")
160	}
161
162	return nil
163
164}
165
166func (bn128 Bn128) Pairing(p1 [3]*big.Int, p2 [3][2]*big.Int) ([2][3][2]*big.Int, error) {
167	pre1 := bn128.PreComputeG1(p1)
168	pre2, err := bn128.PreComputeG2(p2)
169	if err != nil {
170		return [2][3][2]*big.Int{}, err
171	}
172
173	r1 := bn128.MillerLoop(pre1, pre2)
174	res := bn128.FinalExponentiation(r1)
175	return res, nil
176}
177
178type AteG1Precomp struct {
179	Px *big.Int
180	Py *big.Int
181}
182
183func (bn128 Bn128) PreComputeG1(p [3]*big.Int) AteG1Precomp {
184	pCopy := bn128.G1.Affine(p)
185	res := AteG1Precomp{
186		Px: pCopy[0],
187		Py: pCopy[1],
188	}
189	return res
190}
191
192type EllCoeffs struct {
193	Ell0  [2]*big.Int
194	EllVW [2]*big.Int
195	EllVV [2]*big.Int
196}
197type AteG2Precomp struct {
198	Qx     [2]*big.Int
199	Qy     [2]*big.Int
200	Coeffs []EllCoeffs
201}
202
203func (bn128 Bn128) PreComputeG2(p [3][2]*big.Int) (AteG2Precomp, error) {
204	qCopy := bn128.G2.Affine(p)
205	res := AteG2Precomp{
206		qCopy[0],
207		qCopy[1],
208		[]EllCoeffs{},
209	}
210	r := [3][2]*big.Int{
211		bn128.Fq2.Copy(qCopy[0]),
212		bn128.Fq2.Copy(qCopy[1]),
213		bn128.Fq2.One(),
214	}
215	var c EllCoeffs
216	for i := bn128.LoopCount.BitLen() - 2; i >= 0; i-- {
217		bit := bn128.LoopCount.Bit(i)
218
219		c, r = bn128.DoublingStep(r)
220		res.Coeffs = append(res.Coeffs, c)
221		if bit == 1 {
222			c, r = bn128.MixedAdditionStep(qCopy, r)
223			res.Coeffs = append(res.Coeffs, c)
224		}
225	}
226
227	q1 := bn128.G2.Affine(bn128.G2MulByQ(qCopy))
228	if !bn128.Fq2.Equal(q1[2], bn128.Fq2.One()) {
229		return res, errors.New("q1[2] != Fq2.One")
230	}
231	q2 := bn128.G2.Affine(bn128.G2MulByQ(q1))
232	if !bn128.Fq2.Equal(q2[2], bn128.Fq2.One()) {
233		return res, errors.New("q2[2] != Fq2.One")
234	}
235
236	if bn128.LoopCountNeg {
237		r[1] = bn128.Fq2.Neg(r[1])
238	}
239	q2[1] = bn128.Fq2.Neg(q2[1])
240
241	c, r = bn128.MixedAdditionStep(q1, r)
242	res.Coeffs = append(res.Coeffs, c)
243
244	c, r = bn128.MixedAdditionStep(q2, r)
245	res.Coeffs = append(res.Coeffs, c)
246
247	return res, nil
248}
249
250func (bn128 Bn128) DoublingStep(current [3][2]*big.Int) (EllCoeffs, [3][2]*big.Int) {
251	x := current[0]
252	y := current[1]
253	z := current[2]
254
255	a := bn128.Fq2.MulScalar(bn128.Fq2.Mul(x, y), bn128.TwoInv)
256	b := bn128.Fq2.Square(y)
257	c := bn128.Fq2.Square(z)
258	d := bn128.Fq2.Add(c, bn128.Fq2.Add(c, c))
259	e := bn128.Fq2.Mul(bn128.TwistCoefB, d)
260	f := bn128.Fq2.Add(e, bn128.Fq2.Add(e, e))
261	g := bn128.Fq2.MulScalar(bn128.Fq2.Add(b, f), bn128.TwoInv)
262	h := bn128.Fq2.Sub(
263		bn128.Fq2.Square(bn128.Fq2.Add(y, z)),
264		bn128.Fq2.Add(b, c))
265	i := bn128.Fq2.Sub(e, b)
266	j := bn128.Fq2.Square(x)
267	eSqr := bn128.Fq2.Square(e)
268	current[0] = bn128.Fq2.Mul(a, bn128.Fq2.Sub(b, f))
269	current[1] = bn128.Fq2.Sub(bn128.Fq2.Sub(bn128.Fq2.Square(g), eSqr),
270		bn128.Fq2.Add(eSqr, eSqr))
271	current[2] = bn128.Fq2.Mul(b, h)
272	res := EllCoeffs{
273		Ell0:  bn128.Fq2.Mul(i, bn128.Twist),
274		EllVW: bn128.Fq2.Neg(h),
275		EllVV: bn128.Fq2.Add(j, bn128.Fq2.Add(j, j)),
276	}
277
278	return res, current
279}
280
281func (bn128 Bn128) MixedAdditionStep(base, current [3][2]*big.Int) (EllCoeffs, [3][2]*big.Int) {
282	x1 := current[0]
283	y1 := current[1]
284	z1 := current[2]
285	x2 := base[0]
286	y2 := base[1]
287
288	d := bn128.Fq2.Sub(x1, bn128.Fq2.Mul(x2, z1))
289	e := bn128.Fq2.Sub(y1, bn128.Fq2.Mul(y2, z1))
290	f := bn128.Fq2.Square(d)
291	g := bn128.Fq2.Square(e)
292	h := bn128.Fq2.Mul(d, f)
293	i := bn128.Fq2.Mul(x1, f)
294	j := bn128.Fq2.Sub(
295		bn128.Fq2.Add(h, bn128.Fq2.Mul(z1, g)),
296		bn128.Fq2.Add(i, i))
297
298	current[0] = bn128.Fq2.Mul(d, j)
299	current[1] = bn128.Fq2.Sub(
300		bn128.Fq2.Mul(e, bn128.Fq2.Sub(i, j)),
301		bn128.Fq2.Mul(h, y1))
302	current[2] = bn128.Fq2.Mul(z1, h)
303
304	coef := EllCoeffs{
305		Ell0: bn128.Fq2.Mul(
306			bn128.Twist,
307			bn128.Fq2.Sub(
308				bn128.Fq2.Mul(e, x2),
309				bn128.Fq2.Mul(d, y2))),
310		EllVW: d,
311		EllVV: bn128.Fq2.Neg(e),
312	}
313	return coef, current
314}
315func (bn128 Bn128) G2MulByQ(p [3][2]*big.Int) [3][2]*big.Int {
316	fmx := [2]*big.Int{
317		p[0][0],
318		bn128.Fq1.Mul(p[0][1], bn128.Fq1.Copy(bn128.FrobeniusCoeffsC11)),
319	}
320	fmy := [2]*big.Int{
321		p[1][0],
322		bn128.Fq1.Mul(p[1][1], bn128.Fq1.Copy(bn128.FrobeniusCoeffsC11)),
323	}
324	fmz := [2]*big.Int{
325		p[2][0],
326		bn128.Fq1.Mul(p[2][1], bn128.Fq1.Copy(bn128.FrobeniusCoeffsC11)),
327	}
328
329	return [3][2]*big.Int{
330		bn128.Fq2.Mul(bn128.TwistMulByQX, fmx),
331		bn128.Fq2.Mul(bn128.TwistMulByQY, fmy),
332		fmz,
333	}
334}
335
336func (bn128 Bn128) MillerLoop(pre1 AteG1Precomp, pre2 AteG2Precomp) [2][3][2]*big.Int {
337	// https://cryptojedi.org/papers/dclxvi-20100714.pdf
338	// https://eprint.iacr.org/2008/096.pdf
339
340	idx := 0
341	var c EllCoeffs
342	f := bn128.Fq12.One()
343
344	for i := bn128.LoopCount.BitLen() - 2; i >= 0; i-- {
345		bit := bn128.LoopCount.Bit(i)
346
347		c = pre2.Coeffs[idx]
348		idx++
349		f = bn128.Fq12.Square(f)
350
351		f = bn128.MulBy024(f,
352			c.Ell0,
353			bn128.Fq2.MulScalar(c.EllVW, pre1.Py),
354			bn128.Fq2.MulScalar(c.EllVV, pre1.Px))
355
356		if bit == 1 {
357			c = pre2.Coeffs[idx]
358			idx++
359			f = bn128.MulBy024(
360				f,
361				c.Ell0,
362				bn128.Fq2.MulScalar(c.EllVW, pre1.Py),
363				bn128.Fq2.MulScalar(c.EllVV, pre1.Px))
364		}
365	}
366	if bn128.LoopCountNeg {
367		f = bn128.Fq12.Inverse(f)
368	}
369
370	c = pre2.Coeffs[idx]
371	idx++
372	f = bn128.MulBy024(
373		f,
374		c.Ell0,
375		bn128.Fq2.MulScalar(c.EllVW, pre1.Py),
376		bn128.Fq2.MulScalar(c.EllVV, pre1.Px))
377
378	c = pre2.Coeffs[idx]
379	idx++
380
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	return f
388}
389
390func (bn128 Bn128) MulBy024(a [2][3][2]*big.Int, ell0, ellVW, ellVV [2]*big.Int) [2][3][2]*big.Int {
391	b := [2][3][2]*big.Int{
392		[3][2]*big.Int{
393			ell0,
394			bn128.Fq2.Zero(),
395			ellVV,
396		},
397		[3][2]*big.Int{
398			bn128.Fq2.Zero(),
399			ellVW,
400			bn128.Fq2.Zero(),
401		},
402	}
403	return bn128.Fq12.Mul(a, b)
404}
405
406func (bn128 Bn128) FinalExponentiation(r [2][3][2]*big.Int) [2][3][2]*big.Int {
407	res := bn128.Fq12.Exp(r, bn128.FinalExp)
408	return res
409}