github.com/consensys/gnark-crypto@v0.14.0/ecc/bw6-633/g2.go (about)

     1  // Copyright 2020 Consensys Software Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Code generated by consensys/gnark-crypto DO NOT EDIT
    16  
    17  package bw6633
    18  
    19  import (
    20  	"crypto/rand"
    21  	"github.com/consensys/gnark-crypto/ecc"
    22  	"github.com/consensys/gnark-crypto/ecc/bw6-633/fp"
    23  	"github.com/consensys/gnark-crypto/ecc/bw6-633/fr"
    24  	"github.com/consensys/gnark-crypto/internal/parallel"
    25  	"math/big"
    26  	"runtime"
    27  )
    28  
    29  // G2Affine is a point in affine coordinates (x,y)
    30  type G2Affine struct {
    31  	X, Y fp.Element
    32  }
    33  
    34  // G2Jac is a point in Jacobian coordinates (x=X/Z², y=Y/Z³)
    35  type G2Jac struct {
    36  	X, Y, Z fp.Element
    37  }
    38  
    39  // g2JacExtended is a point in extended Jacobian coordinates (x=X/ZZ, y=Y/ZZZ, ZZ³=ZZZ²)
    40  type g2JacExtended struct {
    41  	X, Y, ZZ, ZZZ fp.Element
    42  }
    43  
    44  // g2Proj point in projective coordinates
    45  type g2Proj struct {
    46  	x, y, z fp.Element
    47  }
    48  
    49  // -------------------------------------------------------------------------------------------------
    50  // Affine coordinates
    51  
    52  // Set sets p to a in affine coordinates.
    53  func (p *G2Affine) Set(a *G2Affine) *G2Affine {
    54  	p.X, p.Y = a.X, a.Y
    55  	return p
    56  }
    57  
    58  // setInfinity sets p to the infinity point, which is encoded as (0,0).
    59  // N.B.: (0,0) is never on the curve for j=0 curves (Y²=X³+B).
    60  func (p *G2Affine) setInfinity() *G2Affine {
    61  	p.X.SetZero()
    62  	p.Y.SetZero()
    63  	return p
    64  }
    65  
    66  // ScalarMultiplication computes and returns p = [s]a
    67  // where p and a are affine points.
    68  func (p *G2Affine) ScalarMultiplication(a *G2Affine, s *big.Int) *G2Affine {
    69  	var _p G2Jac
    70  	_p.FromAffine(a)
    71  	_p.mulGLV(&_p, s)
    72  	p.FromJacobian(&_p)
    73  	return p
    74  }
    75  
    76  // ScalarMultiplicationBase computes and returns p = [s]g
    77  // where g is the affine point generating the prime subgroup.
    78  func (p *G2Affine) ScalarMultiplicationBase(s *big.Int) *G2Affine {
    79  	var _p G2Jac
    80  	_p.mulGLV(&g2Gen, s)
    81  	p.FromJacobian(&_p)
    82  	return p
    83  }
    84  
    85  // Add adds two points in affine coordinates.
    86  // It uses the Jacobian addition with a.Z=b.Z=1 and converts the result to affine coordinates.
    87  //
    88  // https://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-mmadd-2007-bl
    89  func (p *G2Affine) Add(a, b *G2Affine) *G2Affine {
    90  	var q G2Jac
    91  	// a is infinity, return b
    92  	if a.IsInfinity() {
    93  		p.Set(b)
    94  		return p
    95  	}
    96  	// b is infinity, return a
    97  	if b.IsInfinity() {
    98  		p.Set(a)
    99  		return p
   100  	}
   101  	if a.X.Equal(&b.X) {
   102  		// if b == a, we double instead
   103  		if a.Y.Equal(&b.Y) {
   104  			q.DoubleMixed(a)
   105  			return p.FromJacobian(&q)
   106  		} else {
   107  			// if b == -a, we return 0
   108  			return p.setInfinity()
   109  		}
   110  	}
   111  	var H, HH, I, J, r, V fp.Element
   112  	H.Sub(&b.X, &a.X)
   113  	HH.Square(&H)
   114  	I.Double(&HH).Double(&I)
   115  	J.Mul(&H, &I)
   116  	r.Sub(&b.Y, &a.Y)
   117  	r.Double(&r)
   118  	V.Mul(&a.X, &I)
   119  	q.X.Square(&r).
   120  		Sub(&q.X, &J).
   121  		Sub(&q.X, &V).
   122  		Sub(&q.X, &V)
   123  	q.Y.Sub(&V, &q.X).
   124  		Mul(&q.Y, &r)
   125  	J.Mul(&a.Y, &J).Double(&J)
   126  	q.Y.Sub(&q.Y, &J)
   127  	q.Z.Double(&H)
   128  
   129  	return p.FromJacobian(&q)
   130  }
   131  
   132  // Double doubles a point in affine coordinates.
   133  // It converts the point to Jacobian coordinates, doubles it using Jacobian
   134  // addition with a.Z=1, and converts it back to affine coordinates.
   135  //
   136  // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-mdbl-2007-bl
   137  func (p *G2Affine) Double(a *G2Affine) *G2Affine {
   138  	var q G2Jac
   139  	q.FromAffine(a)
   140  	q.DoubleMixed(a)
   141  	p.FromJacobian(&q)
   142  	return p
   143  }
   144  
   145  // Sub subtracts two points in affine coordinates.
   146  // It uses a similar approach to Add, but negates the second point before adding.
   147  func (p *G2Affine) Sub(a, b *G2Affine) *G2Affine {
   148  	var bneg G2Affine
   149  	bneg.Neg(b)
   150  	p.Add(a, &bneg)
   151  	return p
   152  }
   153  
   154  // Equal tests if two points in affine coordinates are equal.
   155  func (p *G2Affine) Equal(a *G2Affine) bool {
   156  	return p.X.Equal(&a.X) && p.Y.Equal(&a.Y)
   157  }
   158  
   159  // Neg sets p to the affine negative point -a = (a.X, -a.Y).
   160  func (p *G2Affine) Neg(a *G2Affine) *G2Affine {
   161  	p.X = a.X
   162  	p.Y.Neg(&a.Y)
   163  	return p
   164  }
   165  
   166  // FromJacobian converts a point p1 from Jacobian to affine coordinates.
   167  func (p *G2Affine) FromJacobian(p1 *G2Jac) *G2Affine {
   168  
   169  	var a, b fp.Element
   170  
   171  	if p1.Z.IsZero() {
   172  		p.X.SetZero()
   173  		p.Y.SetZero()
   174  		return p
   175  	}
   176  
   177  	a.Inverse(&p1.Z)
   178  	b.Square(&a)
   179  	p.X.Mul(&p1.X, &b)
   180  	p.Y.Mul(&p1.Y, &b).Mul(&p.Y, &a)
   181  
   182  	return p
   183  }
   184  
   185  // String returns the string representation E(x,y) of the affine point p or "O" if it is infinity.
   186  func (p *G2Affine) String() string {
   187  	if p.IsInfinity() {
   188  		return "O"
   189  	}
   190  	return "E([" + p.X.String() + "," + p.Y.String() + "])"
   191  }
   192  
   193  // IsInfinity checks if the affine point p is infinity, which is encoded as (0,0).
   194  // N.B.: (0,0) is never on the curve for j=0 curves (Y²=X³+B).
   195  func (p *G2Affine) IsInfinity() bool {
   196  	return p.X.IsZero() && p.Y.IsZero()
   197  }
   198  
   199  // IsOnCurve returns true if the affine point p in on the curve.
   200  func (p *G2Affine) IsOnCurve() bool {
   201  	var point G2Jac
   202  	point.FromAffine(p)
   203  	return point.IsOnCurve() // call this function to handle infinity point
   204  }
   205  
   206  // IsInSubGroup returns true if the affine point p is in the correct subgroup, false otherwise.
   207  func (p *G2Affine) IsInSubGroup() bool {
   208  	var _p G2Jac
   209  	_p.FromAffine(p)
   210  	return _p.IsInSubGroup()
   211  }
   212  
   213  // -------------------------------------------------------------------------------------------------
   214  // Jacobian coordinates
   215  
   216  // Set sets p to a in Jacobian coordinates.
   217  func (p *G2Jac) Set(q *G2Jac) *G2Jac {
   218  	p.X, p.Y, p.Z = q.X, q.Y, q.Z
   219  	return p
   220  }
   221  
   222  // Equal tests if two points in Jacobian coordinates are equal.
   223  func (p *G2Jac) Equal(q *G2Jac) bool {
   224  	// If one point is infinity, the other must also be infinity.
   225  	if p.Z.IsZero() {
   226  		return q.Z.IsZero()
   227  	}
   228  	// If the other point is infinity, return false since we can't
   229  	// the following checks would be incorrect.
   230  	if q.Z.IsZero() {
   231  		return false
   232  	}
   233  
   234  	var pZSquare, aZSquare fp.Element
   235  	pZSquare.Square(&p.Z)
   236  	aZSquare.Square(&q.Z)
   237  
   238  	var lhs, rhs fp.Element
   239  	lhs.Mul(&p.X, &aZSquare)
   240  	rhs.Mul(&q.X, &pZSquare)
   241  	if !lhs.Equal(&rhs) {
   242  		return false
   243  	}
   244  	lhs.Mul(&p.Y, &aZSquare).Mul(&lhs, &q.Z)
   245  	rhs.Mul(&q.Y, &pZSquare).Mul(&rhs, &p.Z)
   246  
   247  	return lhs.Equal(&rhs)
   248  }
   249  
   250  // Neg sets p to the Jacobian negative point -q = (q.X, -q.Y, q.Z).
   251  func (p *G2Jac) Neg(q *G2Jac) *G2Jac {
   252  	*p = *q
   253  	p.Y.Neg(&q.Y)
   254  	return p
   255  }
   256  
   257  // AddAssign sets p to p+a in Jacobian coordinates.
   258  //
   259  // https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl
   260  func (p *G2Jac) AddAssign(q *G2Jac) *G2Jac {
   261  
   262  	// p is infinity, return q
   263  	if p.Z.IsZero() {
   264  		p.Set(q)
   265  		return p
   266  	}
   267  
   268  	// q is infinity, return p
   269  	if q.Z.IsZero() {
   270  		return p
   271  	}
   272  
   273  	var Z1Z1, Z2Z2, U1, U2, S1, S2, H, I, J, r, V fp.Element
   274  	Z1Z1.Square(&q.Z)
   275  	Z2Z2.Square(&p.Z)
   276  	U1.Mul(&q.X, &Z2Z2)
   277  	U2.Mul(&p.X, &Z1Z1)
   278  	S1.Mul(&q.Y, &p.Z).
   279  		Mul(&S1, &Z2Z2)
   280  	S2.Mul(&p.Y, &q.Z).
   281  		Mul(&S2, &Z1Z1)
   282  
   283  	// if p == q, we double instead
   284  	if U1.Equal(&U2) && S1.Equal(&S2) {
   285  		return p.DoubleAssign()
   286  	}
   287  
   288  	H.Sub(&U2, &U1)
   289  	I.Double(&H).
   290  		Square(&I)
   291  	J.Mul(&H, &I)
   292  	r.Sub(&S2, &S1).Double(&r)
   293  	V.Mul(&U1, &I)
   294  	p.X.Square(&r).
   295  		Sub(&p.X, &J).
   296  		Sub(&p.X, &V).
   297  		Sub(&p.X, &V)
   298  	p.Y.Sub(&V, &p.X).
   299  		Mul(&p.Y, &r)
   300  	S1.Mul(&S1, &J).Double(&S1)
   301  	p.Y.Sub(&p.Y, &S1)
   302  	p.Z.Add(&p.Z, &q.Z)
   303  	p.Z.Square(&p.Z).
   304  		Sub(&p.Z, &Z1Z1).
   305  		Sub(&p.Z, &Z2Z2).
   306  		Mul(&p.Z, &H)
   307  
   308  	return p
   309  }
   310  
   311  // SubAssign sets p to p-a in Jacobian coordinates.
   312  // It uses a similar approach to AddAssign, but negates the point a before adding.
   313  func (p *G2Jac) SubAssign(q *G2Jac) *G2Jac {
   314  	var tmp G2Jac
   315  	tmp.Set(q)
   316  	tmp.Y.Neg(&tmp.Y)
   317  	p.AddAssign(&tmp)
   318  	return p
   319  }
   320  
   321  // Double sets p to [2]q in Jacobian coordinates.
   322  //
   323  // https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2007-bl
   324  func (p *G2Jac) DoubleMixed(a *G2Affine) *G2Jac {
   325  	var XX, YY, YYYY, S, M, T fp.Element
   326  	XX.Square(&a.X)
   327  	YY.Square(&a.Y)
   328  	YYYY.Square(&YY)
   329  	S.Add(&a.X, &YY).
   330  		Square(&S).
   331  		Sub(&S, &XX).
   332  		Sub(&S, &YYYY).
   333  		Double(&S)
   334  	M.Double(&XX).
   335  		Add(&M, &XX) // -> + A, but A=0 here
   336  	T.Square(&M).
   337  		Sub(&T, &S).
   338  		Sub(&T, &S)
   339  	p.X.Set(&T)
   340  	p.Y.Sub(&S, &T).
   341  		Mul(&p.Y, &M)
   342  	YYYY.Double(&YYYY).
   343  		Double(&YYYY).
   344  		Double(&YYYY)
   345  	p.Y.Sub(&p.Y, &YYYY)
   346  	p.Z.Double(&a.Y)
   347  
   348  	return p
   349  }
   350  
   351  // AddMixed sets p to p+a in Jacobian coordinates, where a.Z = 1.
   352  //
   353  // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl
   354  func (p *G2Jac) AddMixed(a *G2Affine) *G2Jac {
   355  
   356  	//if a is infinity return p
   357  	if a.IsInfinity() {
   358  		return p
   359  	}
   360  	// p is infinity, return a
   361  	if p.Z.IsZero() {
   362  		p.X = a.X
   363  		p.Y = a.Y
   364  		p.Z.SetOne()
   365  		return p
   366  	}
   367  
   368  	var Z1Z1, U2, S2, H, HH, I, J, r, V fp.Element
   369  	Z1Z1.Square(&p.Z)
   370  	U2.Mul(&a.X, &Z1Z1)
   371  	S2.Mul(&a.Y, &p.Z).
   372  		Mul(&S2, &Z1Z1)
   373  
   374  	// if p == a, we double instead
   375  	if U2.Equal(&p.X) && S2.Equal(&p.Y) {
   376  		return p.DoubleMixed(a)
   377  	}
   378  
   379  	H.Sub(&U2, &p.X)
   380  	HH.Square(&H)
   381  	I.Double(&HH).Double(&I)
   382  	J.Mul(&H, &I)
   383  	r.Sub(&S2, &p.Y).Double(&r)
   384  	V.Mul(&p.X, &I)
   385  	p.X.Square(&r).
   386  		Sub(&p.X, &J).
   387  		Sub(&p.X, &V).
   388  		Sub(&p.X, &V)
   389  	J.Mul(&J, &p.Y).Double(&J)
   390  	p.Y.Sub(&V, &p.X).
   391  		Mul(&p.Y, &r)
   392  	p.Y.Sub(&p.Y, &J)
   393  	p.Z.Add(&p.Z, &H)
   394  	p.Z.Square(&p.Z).
   395  		Sub(&p.Z, &Z1Z1).
   396  		Sub(&p.Z, &HH)
   397  
   398  	return p
   399  }
   400  
   401  // Double sets p to [2]q in Jacobian coordinates.
   402  //
   403  // https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2007-bl
   404  func (p *G2Jac) Double(q *G2Jac) *G2Jac {
   405  	p.Set(q)
   406  	p.DoubleAssign()
   407  	return p
   408  }
   409  
   410  // DoubleAssign doubles p in Jacobian coordinates.
   411  //
   412  // https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2007-bl
   413  func (p *G2Jac) DoubleAssign() *G2Jac {
   414  
   415  	var XX, YY, YYYY, ZZ, S, M, T fp.Element
   416  
   417  	XX.Square(&p.X)
   418  	YY.Square(&p.Y)
   419  	YYYY.Square(&YY)
   420  	ZZ.Square(&p.Z)
   421  	S.Add(&p.X, &YY)
   422  	S.Square(&S).
   423  		Sub(&S, &XX).
   424  		Sub(&S, &YYYY).
   425  		Double(&S)
   426  	M.Double(&XX).Add(&M, &XX)
   427  	p.Z.Add(&p.Z, &p.Y).
   428  		Square(&p.Z).
   429  		Sub(&p.Z, &YY).
   430  		Sub(&p.Z, &ZZ)
   431  	T.Square(&M)
   432  	p.X = T
   433  	T.Double(&S)
   434  	p.X.Sub(&p.X, &T)
   435  	p.Y.Sub(&S, &p.X).
   436  		Mul(&p.Y, &M)
   437  	YYYY.Double(&YYYY).Double(&YYYY).Double(&YYYY)
   438  	p.Y.Sub(&p.Y, &YYYY)
   439  
   440  	return p
   441  }
   442  
   443  // ScalarMultiplication computes and returns p = [s]a
   444  // where p and a are Jacobian points.
   445  // using the GLV technique.
   446  // see https://www.iacr.org/archive/crypto2001/21390189.pdf
   447  func (p *G2Jac) ScalarMultiplication(q *G2Jac, s *big.Int) *G2Jac {
   448  	return p.mulGLV(q, s)
   449  }
   450  
   451  // ScalarMultiplicationBase computes and returns p = [s]g
   452  // where g is the prime subgroup generator.
   453  func (p *G2Jac) ScalarMultiplicationBase(s *big.Int) *G2Jac {
   454  	return p.mulGLV(&g2Gen, s)
   455  
   456  }
   457  
   458  // String converts p to affine coordinates and returns its string representation E(x,y) or "O" if it is infinity.
   459  func (p *G2Jac) String() string {
   460  	_p := G2Affine{}
   461  	_p.FromJacobian(p)
   462  	return _p.String()
   463  }
   464  
   465  // FromAffine converts a point a from affine to Jacobian coordinates.
   466  func (p *G2Jac) FromAffine(a *G2Affine) *G2Jac {
   467  	if a.IsInfinity() {
   468  		p.Z.SetZero()
   469  		p.X.SetOne()
   470  		p.Y.SetOne()
   471  		return p
   472  	}
   473  	p.Z.SetOne()
   474  	p.X.Set(&a.X)
   475  	p.Y.Set(&a.Y)
   476  	return p
   477  }
   478  
   479  // IsOnCurve returns true if the Jacobian point p in on the curve.
   480  func (p *G2Jac) IsOnCurve() bool {
   481  	var left, right, tmp, ZZ fp.Element
   482  	left.Square(&p.Y)
   483  	right.Square(&p.X).Mul(&right, &p.X)
   484  	ZZ.Square(&p.Z)
   485  	tmp.Square(&ZZ).Mul(&tmp, &ZZ)
   486  	// Mul tmp by bTwistCurveCoeff=8
   487  	tmp.Double(&tmp).Double(&tmp).Double(&tmp)
   488  	right.Add(&right, &tmp)
   489  	return left.Equal(&right)
   490  }
   491  
   492  // IsInSubGroup returns true if p is on the r-torsion, false otherwise.
   493  
   494  // 3r P = (x+1)ϕ(P) + (-x^5 + x⁴ + x)P
   495  func (p *G2Jac) IsInSubGroup() bool {
   496  
   497  	var uP, u4P, u5P, q, r G2Jac
   498  	uP.ScalarMultiplication(p, &xGen)
   499  	u4P.ScalarMultiplication(&uP, &xGen).
   500  		ScalarMultiplication(&u4P, &xGen).
   501  		ScalarMultiplication(&u4P, &xGen)
   502  	u5P.ScalarMultiplication(&u4P, &xGen)
   503  	q.Set(p).SubAssign(&uP)
   504  	r.phi(&q).SubAssign(&uP).
   505  		AddAssign(&u4P).
   506  		AddAssign(&u5P)
   507  
   508  	return r.IsOnCurve() && r.Z.IsZero()
   509  }
   510  
   511  // mulWindowed computes the 2-bits windowed double-and-add scalar
   512  // multiplication p=[s]q in Jacobian coordinates.
   513  func (p *G2Jac) mulWindowed(q *G2Jac, s *big.Int) *G2Jac {
   514  
   515  	var res G2Jac
   516  	var ops [3]G2Jac
   517  
   518  	ops[0].Set(q)
   519  	if s.Sign() == -1 {
   520  		ops[0].Neg(&ops[0])
   521  	}
   522  	res.Set(&g2Infinity)
   523  	ops[1].Double(&ops[0])
   524  	ops[2].Set(&ops[0]).AddAssign(&ops[1])
   525  
   526  	b := s.Bytes()
   527  	for i := range b {
   528  		w := b[i]
   529  		mask := byte(0xc0)
   530  		for j := 0; j < 4; j++ {
   531  			res.DoubleAssign().DoubleAssign()
   532  			c := (w & mask) >> (6 - 2*j)
   533  			if c != 0 {
   534  				res.AddAssign(&ops[c-1])
   535  			}
   536  			mask = mask >> 2
   537  		}
   538  	}
   539  	p.Set(&res)
   540  
   541  	return p
   542  
   543  }
   544  
   545  // phi sets p to ϕ(a) where ϕ: (x,y) → (w x,y),
   546  // where w is a third root of unity.
   547  func (p *G2Jac) phi(q *G2Jac) *G2Jac {
   548  	p.Set(q)
   549  	p.X.Mul(&p.X, &thirdRootOneG2)
   550  	return p
   551  }
   552  
   553  // mulGLV computes the scalar multiplication using a windowed-GLV method
   554  //
   555  // see https://www.iacr.org/archive/crypto2001/21390189.pdf
   556  func (p *G2Jac) mulGLV(q *G2Jac, s *big.Int) *G2Jac {
   557  
   558  	var table [15]G2Jac
   559  	var res G2Jac
   560  	var k1, k2 fr.Element
   561  
   562  	res.Set(&g2Infinity)
   563  
   564  	// table[b3b2b1b0-1] = b3b2 ⋅ ϕ(q) + b1b0*q
   565  	table[0].Set(q)
   566  	table[3].phi(q)
   567  
   568  	// split the scalar, modifies ±q, ϕ(q) accordingly
   569  	k := ecc.SplitScalar(s, &glvBasis)
   570  
   571  	if k[0].Sign() == -1 {
   572  		k[0].Neg(&k[0])
   573  		table[0].Neg(&table[0])
   574  	}
   575  	if k[1].Sign() == -1 {
   576  		k[1].Neg(&k[1])
   577  		table[3].Neg(&table[3])
   578  	}
   579  
   580  	// precompute table (2 bits sliding window)
   581  	// table[b3b2b1b0-1] = b3b2 ⋅ ϕ(q) + b1b0 ⋅ q if b3b2b1b0 != 0
   582  	table[1].Double(&table[0])
   583  	table[2].Set(&table[1]).AddAssign(&table[0])
   584  	table[4].Set(&table[3]).AddAssign(&table[0])
   585  	table[5].Set(&table[3]).AddAssign(&table[1])
   586  	table[6].Set(&table[3]).AddAssign(&table[2])
   587  	table[7].Double(&table[3])
   588  	table[8].Set(&table[7]).AddAssign(&table[0])
   589  	table[9].Set(&table[7]).AddAssign(&table[1])
   590  	table[10].Set(&table[7]).AddAssign(&table[2])
   591  	table[11].Set(&table[7]).AddAssign(&table[3])
   592  	table[12].Set(&table[11]).AddAssign(&table[0])
   593  	table[13].Set(&table[11]).AddAssign(&table[1])
   594  	table[14].Set(&table[11]).AddAssign(&table[2])
   595  
   596  	// bounds on the lattice base vectors guarantee that k1, k2 are len(r)/2 or len(r)/2+1 bits long max
   597  	// this is because we use a probabilistic scalar decomposition that replaces a division by a right-shift
   598  	k1 = k1.SetBigInt(&k[0]).Bits()
   599  	k2 = k2.SetBigInt(&k[1]).Bits()
   600  
   601  	// we don't target constant-timeness so we check first if we increase the bounds or not
   602  	maxBit := k1.BitLen()
   603  	if k2.BitLen() > maxBit {
   604  		maxBit = k2.BitLen()
   605  	}
   606  	hiWordIndex := (maxBit - 1) / 64
   607  
   608  	// loop starts from len(k1)/2 or len(k1)/2+1 due to the bounds
   609  	for i := hiWordIndex; i >= 0; i-- {
   610  		mask := uint64(3) << 62
   611  		for j := 0; j < 32; j++ {
   612  			res.Double(&res).Double(&res)
   613  			b1 := (k1[i] & mask) >> (62 - 2*j)
   614  			b2 := (k2[i] & mask) >> (62 - 2*j)
   615  			if b1|b2 != 0 {
   616  				s := (b2<<2 | b1)
   617  				res.AddAssign(&table[s-1])
   618  			}
   619  			mask = mask >> 2
   620  		}
   621  	}
   622  
   623  	p.Set(&res)
   624  	return p
   625  }
   626  
   627  // ClearCofactor maps a point in curve to r-torsion
   628  func (p *G2Affine) ClearCofactor(a *G2Affine) *G2Affine {
   629  	var _p G2Jac
   630  	_p.FromAffine(a)
   631  	_p.ClearCofactor(&_p)
   632  	p.FromJacobian(&_p)
   633  	return p
   634  }
   635  
   636  // ClearCofactor maps a point in curve to r-torsion
   637  func (p *G2Jac) ClearCofactor(q *G2Jac) *G2Jac {
   638  	var uP, u2P, u3P, u4P, u5P, xP, vP, wP, L0, L1, tmp G2Jac
   639  	var ht, d1, d3 big.Int
   640  	ht.SetInt64(7) // negative
   641  	d1.SetInt64(13)
   642  	d3.SetInt64(5) // negative
   643  
   644  	uP.ScalarMultiplication(q, &xGen) // negative
   645  	u2P.ScalarMultiplication(&uP, &xGen)
   646  	u3P.ScalarMultiplication(&u2P, &xGen) // negative
   647  	u4P.ScalarMultiplication(&u3P, &xGen)
   648  	u5P.ScalarMultiplication(&u4P, &xGen) // negative
   649  	vP.Set(&u2P).AddAssign(&uP).
   650  		AddAssign(&u3P).
   651  		Double(&vP).
   652  		AddAssign(&u4P).
   653  		AddAssign(q)
   654  	wP.Set(&uP).SubAssign(&u4P).SubAssign(&u5P)
   655  	xP.Set(q).AddAssign(&vP)
   656  	L0.Set(&uP).SubAssign(q).ScalarMultiplication(&L0, &d1)
   657  	tmp.ScalarMultiplication(&xP, &d3)
   658  	L0.AddAssign(&tmp)
   659  	tmp.ScalarMultiplication(q, &ht) // negative
   660  	L0.SubAssign(&tmp)
   661  	L1.ScalarMultiplication(&wP, &d1)
   662  	tmp.ScalarMultiplication(&vP, &ht)
   663  	L1.AddAssign(&tmp)
   664  	tmp.ScalarMultiplication(q, &d3)
   665  	L1.AddAssign(&tmp)
   666  
   667  	p.phi(&L1).AddAssign(&L0)
   668  
   669  	return p
   670  
   671  }
   672  
   673  // -------------------------------------------------------------------------------------------------
   674  // extended Jacobian coordinates
   675  
   676  // Set sets p to a in extended Jacobian coordinates.
   677  func (p *g2JacExtended) Set(q *g2JacExtended) *g2JacExtended {
   678  	p.X, p.Y, p.ZZ, p.ZZZ = q.X, q.Y, q.ZZ, q.ZZZ
   679  	return p
   680  }
   681  
   682  // setInfinity sets p to the infinity point (1,1,0,0).
   683  func (p *g2JacExtended) setInfinity() *g2JacExtended {
   684  	p.X.SetOne()
   685  	p.Y.SetOne()
   686  	p.ZZ = fp.Element{}
   687  	p.ZZZ = fp.Element{}
   688  	return p
   689  }
   690  
   691  // IsInfinity checks if the p is infinity, i.e. p.ZZ=0.
   692  func (p *g2JacExtended) IsInfinity() bool {
   693  	return p.ZZ.IsZero()
   694  }
   695  
   696  // fromJacExtended converts an extended Jacobian point to an affine point.
   697  func (p *G2Affine) fromJacExtended(q *g2JacExtended) *G2Affine {
   698  	if q.ZZ.IsZero() {
   699  		p.X = fp.Element{}
   700  		p.Y = fp.Element{}
   701  		return p
   702  	}
   703  	p.X.Inverse(&q.ZZ).Mul(&p.X, &q.X)
   704  	p.Y.Inverse(&q.ZZZ).Mul(&p.Y, &q.Y)
   705  	return p
   706  }
   707  
   708  // fromJacExtended converts an extended Jacobian point to a Jacobian point.
   709  func (p *G2Jac) fromJacExtended(q *g2JacExtended) *G2Jac {
   710  	if q.ZZ.IsZero() {
   711  		p.Set(&g2Infinity)
   712  		return p
   713  	}
   714  	p.X.Mul(&q.ZZ, &q.X).Mul(&p.X, &q.ZZ)
   715  	p.Y.Mul(&q.ZZZ, &q.Y).Mul(&p.Y, &q.ZZZ)
   716  	p.Z.Set(&q.ZZZ)
   717  	return p
   718  }
   719  
   720  // unsafeFromJacExtended converts an extended Jacobian point, distinct from Infinity, to a Jacobian point.
   721  func (p *G2Jac) unsafeFromJacExtended(q *g2JacExtended) *G2Jac {
   722  	p.X.Square(&q.ZZ).Mul(&p.X, &q.X)
   723  	p.Y.Square(&q.ZZZ).Mul(&p.Y, &q.Y)
   724  	p.Z = q.ZZZ
   725  	return p
   726  }
   727  
   728  // add sets p to p+q in extended Jacobian coordinates.
   729  //
   730  // https://www.hyperelliptic.org/EFD/g1p/auto-shortw-xyzz.html#addition-add-2008-s
   731  func (p *g2JacExtended) add(q *g2JacExtended) *g2JacExtended {
   732  	//if q is infinity return p
   733  	if q.ZZ.IsZero() {
   734  		return p
   735  	}
   736  	// p is infinity, return q
   737  	if p.ZZ.IsZero() {
   738  		p.Set(q)
   739  		return p
   740  	}
   741  
   742  	var A, B, U1, U2, S1, S2 fp.Element
   743  
   744  	// p2: q, p1: p
   745  	U2.Mul(&q.X, &p.ZZ)
   746  	U1.Mul(&p.X, &q.ZZ)
   747  	A.Sub(&U2, &U1)
   748  	S2.Mul(&q.Y, &p.ZZZ)
   749  	S1.Mul(&p.Y, &q.ZZZ)
   750  	B.Sub(&S2, &S1)
   751  
   752  	if A.IsZero() {
   753  		if B.IsZero() {
   754  			return p.double(q)
   755  
   756  		}
   757  		p.ZZ = fp.Element{}
   758  		p.ZZZ = fp.Element{}
   759  		return p
   760  	}
   761  
   762  	var P, R, PP, PPP, Q, V fp.Element
   763  	P.Sub(&U2, &U1)
   764  	R.Sub(&S2, &S1)
   765  	PP.Square(&P)
   766  	PPP.Mul(&P, &PP)
   767  	Q.Mul(&U1, &PP)
   768  	V.Mul(&S1, &PPP)
   769  
   770  	p.X.Square(&R).
   771  		Sub(&p.X, &PPP).
   772  		Sub(&p.X, &Q).
   773  		Sub(&p.X, &Q)
   774  	p.Y.Sub(&Q, &p.X).
   775  		Mul(&p.Y, &R).
   776  		Sub(&p.Y, &V)
   777  	p.ZZ.Mul(&p.ZZ, &q.ZZ).
   778  		Mul(&p.ZZ, &PP)
   779  	p.ZZZ.Mul(&p.ZZZ, &q.ZZZ).
   780  		Mul(&p.ZZZ, &PPP)
   781  
   782  	return p
   783  }
   784  
   785  // double sets p to [2]q in Jacobian extended coordinates.
   786  //
   787  // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-xyzz.html#doubling-dbl-2008-s-1
   788  // N.B.: since we consider any point on Z=0 as the point at infinity
   789  // this doubling formula works for infinity points as well.
   790  func (p *g2JacExtended) double(q *g2JacExtended) *g2JacExtended {
   791  	var U, V, W, S, XX, M fp.Element
   792  
   793  	U.Double(&q.Y)
   794  	V.Square(&U)
   795  	W.Mul(&U, &V)
   796  	S.Mul(&q.X, &V)
   797  	XX.Square(&q.X)
   798  	M.Double(&XX).
   799  		Add(&M, &XX) // -> + A, but A=0 here
   800  	U.Mul(&W, &q.Y)
   801  
   802  	p.X.Square(&M).
   803  		Sub(&p.X, &S).
   804  		Sub(&p.X, &S)
   805  	p.Y.Sub(&S, &p.X).
   806  		Mul(&p.Y, &M).
   807  		Sub(&p.Y, &U)
   808  	p.ZZ.Mul(&V, &q.ZZ)
   809  	p.ZZZ.Mul(&W, &q.ZZZ)
   810  
   811  	return p
   812  }
   813  
   814  // addMixed sets p to p+q in extended Jacobian coordinates, where a.ZZ=1.
   815  //
   816  // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-xyzz.html#addition-madd-2008-s
   817  func (p *g2JacExtended) addMixed(a *G2Affine) *g2JacExtended {
   818  
   819  	//if a is infinity return p
   820  	if a.IsInfinity() {
   821  		return p
   822  	}
   823  	// p is infinity, return a
   824  	if p.ZZ.IsZero() {
   825  		p.X = a.X
   826  		p.Y = a.Y
   827  		p.ZZ.SetOne()
   828  		p.ZZZ.SetOne()
   829  		return p
   830  	}
   831  
   832  	var P, R fp.Element
   833  
   834  	// p2: a, p1: p
   835  	P.Mul(&a.X, &p.ZZ)
   836  	P.Sub(&P, &p.X)
   837  
   838  	R.Mul(&a.Y, &p.ZZZ)
   839  	R.Sub(&R, &p.Y)
   840  
   841  	if P.IsZero() {
   842  		if R.IsZero() {
   843  			return p.doubleMixed(a)
   844  
   845  		}
   846  		p.ZZ = fp.Element{}
   847  		p.ZZZ = fp.Element{}
   848  		return p
   849  	}
   850  
   851  	var PP, PPP, Q, Q2, RR, X3, Y3 fp.Element
   852  
   853  	PP.Square(&P)
   854  	PPP.Mul(&P, &PP)
   855  	Q.Mul(&p.X, &PP)
   856  	RR.Square(&R)
   857  	X3.Sub(&RR, &PPP)
   858  	Q2.Double(&Q)
   859  	p.X.Sub(&X3, &Q2)
   860  	Y3.Sub(&Q, &p.X).Mul(&Y3, &R)
   861  	R.Mul(&p.Y, &PPP)
   862  	p.Y.Sub(&Y3, &R)
   863  	p.ZZ.Mul(&p.ZZ, &PP)
   864  	p.ZZZ.Mul(&p.ZZZ, &PPP)
   865  
   866  	return p
   867  
   868  }
   869  
   870  // subMixed works the same as addMixed, but negates a.Y.
   871  //
   872  // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-xyzz.html#addition-madd-2008-s
   873  func (p *g2JacExtended) subMixed(a *G2Affine) *g2JacExtended {
   874  
   875  	//if a is infinity return p
   876  	if a.IsInfinity() {
   877  		return p
   878  	}
   879  	// p is infinity, return a
   880  	if p.ZZ.IsZero() {
   881  		p.X = a.X
   882  		p.Y.Neg(&a.Y)
   883  		p.ZZ.SetOne()
   884  		p.ZZZ.SetOne()
   885  		return p
   886  	}
   887  
   888  	var P, R fp.Element
   889  
   890  	// p2: a, p1: p
   891  	P.Mul(&a.X, &p.ZZ)
   892  	P.Sub(&P, &p.X)
   893  
   894  	R.Mul(&a.Y, &p.ZZZ)
   895  	R.Neg(&R)
   896  	R.Sub(&R, &p.Y)
   897  
   898  	if P.IsZero() {
   899  		if R.IsZero() {
   900  			return p.doubleNegMixed(a)
   901  
   902  		}
   903  		p.ZZ = fp.Element{}
   904  		p.ZZZ = fp.Element{}
   905  		return p
   906  	}
   907  
   908  	var PP, PPP, Q, Q2, RR, X3, Y3 fp.Element
   909  
   910  	PP.Square(&P)
   911  	PPP.Mul(&P, &PP)
   912  	Q.Mul(&p.X, &PP)
   913  	RR.Square(&R)
   914  	X3.Sub(&RR, &PPP)
   915  	Q2.Double(&Q)
   916  	p.X.Sub(&X3, &Q2)
   917  	Y3.Sub(&Q, &p.X).Mul(&Y3, &R)
   918  	R.Mul(&p.Y, &PPP)
   919  	p.Y.Sub(&Y3, &R)
   920  	p.ZZ.Mul(&p.ZZ, &PP)
   921  	p.ZZZ.Mul(&p.ZZZ, &PPP)
   922  
   923  	return p
   924  
   925  }
   926  
   927  // doubleNegMixed works the same as double, but negates q.Y.
   928  func (p *g2JacExtended) doubleNegMixed(a *G2Affine) *g2JacExtended {
   929  
   930  	var U, V, W, S, XX, M, S2, L fp.Element
   931  
   932  	U.Double(&a.Y)
   933  	U.Neg(&U)
   934  	V.Square(&U)
   935  	W.Mul(&U, &V)
   936  	S.Mul(&a.X, &V)
   937  	XX.Square(&a.X)
   938  	M.Double(&XX).
   939  		Add(&M, &XX) // -> + A, but A=0 here
   940  	S2.Double(&S)
   941  	L.Mul(&W, &a.Y)
   942  
   943  	p.X.Square(&M).
   944  		Sub(&p.X, &S2)
   945  	p.Y.Sub(&S, &p.X).
   946  		Mul(&p.Y, &M).
   947  		Add(&p.Y, &L)
   948  	p.ZZ.Set(&V)
   949  	p.ZZZ.Set(&W)
   950  
   951  	return p
   952  }
   953  
   954  // doubleMixed sets p to [2]a in Jacobian extended coordinates, where a.ZZ=1.
   955  //
   956  // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-xyzz.html#doubling-dbl-2008-s-1
   957  func (p *g2JacExtended) doubleMixed(a *G2Affine) *g2JacExtended {
   958  
   959  	var U, V, W, S, XX, M, S2, L fp.Element
   960  
   961  	U.Double(&a.Y)
   962  	V.Square(&U)
   963  	W.Mul(&U, &V)
   964  	S.Mul(&a.X, &V)
   965  	XX.Square(&a.X)
   966  	M.Double(&XX).
   967  		Add(&M, &XX) // -> + A, but A=0 here
   968  	S2.Double(&S)
   969  	L.Mul(&W, &a.Y)
   970  
   971  	p.X.Square(&M).
   972  		Sub(&p.X, &S2)
   973  	p.Y.Sub(&S, &p.X).
   974  		Mul(&p.Y, &M).
   975  		Sub(&p.Y, &L)
   976  	p.ZZ.Set(&V)
   977  	p.ZZZ.Set(&W)
   978  
   979  	return p
   980  }
   981  
   982  // -------------------------------------------------------------------------------------------------
   983  // Homogenous projective coordinates
   984  
   985  // Set sets p to a in projective coordinates.
   986  func (p *g2Proj) Set(q *g2Proj) *g2Proj {
   987  	p.x, p.y, p.z = q.x, q.y, q.z
   988  	return p
   989  }
   990  
   991  // Neg sets p to the projective negative point -q = (q.X, -q.Y).
   992  func (p *g2Proj) Neg(q *g2Proj) *g2Proj {
   993  	*p = *q
   994  	p.y.Neg(&q.y)
   995  	return p
   996  }
   997  
   998  // FromAffine converts q in affine to p in projective coordinates.
   999  func (p *g2Proj) FromAffine(a *G2Affine) *g2Proj {
  1000  	if a.X.IsZero() && a.Y.IsZero() {
  1001  		p.z.SetZero()
  1002  		p.x.SetOne()
  1003  		p.y.SetOne()
  1004  		return p
  1005  	}
  1006  	p.z.SetOne()
  1007  	p.x.Set(&a.X)
  1008  	p.y.Set(&a.Y)
  1009  	return p
  1010  }
  1011  
  1012  // BatchScalarMultiplicationG2 multiplies the same base by all scalars
  1013  // and return resulting points in affine coordinates
  1014  // uses a simple windowed-NAF-like multiplication algorithm.
  1015  func BatchScalarMultiplicationG2(base *G2Affine, scalars []fr.Element) []G2Affine {
  1016  	// approximate cost in group ops is
  1017  	// cost = 2^{c-1} + n(scalar.nbBits+nbChunks)
  1018  
  1019  	nbPoints := uint64(len(scalars))
  1020  	min := ^uint64(0)
  1021  	bestC := 0
  1022  	for c := 2; c <= 16; c++ {
  1023  		cost := uint64(1 << (c - 1)) // pre compute the table
  1024  		nbChunks := computeNbChunks(uint64(c))
  1025  		cost += nbPoints * (uint64(c) + 1) * nbChunks // doublings + point add
  1026  		if cost < min {
  1027  			min = cost
  1028  			bestC = c
  1029  		}
  1030  	}
  1031  	c := uint64(bestC) // window size
  1032  	nbChunks := int(computeNbChunks(c))
  1033  
  1034  	// last window may be slightly larger than c; in which case we need to compute one
  1035  	// extra element in the baseTable
  1036  	maxC := lastC(c)
  1037  	if c > maxC {
  1038  		maxC = c
  1039  	}
  1040  
  1041  	// precompute all powers of base for our window
  1042  	// note here that if performance is critical, we can implement as in the msmX methods
  1043  	// this allocation to be on the stack
  1044  	baseTable := make([]G2Jac, (1 << (maxC - 1)))
  1045  	baseTable[0].FromAffine(base)
  1046  	for i := 1; i < len(baseTable); i++ {
  1047  		baseTable[i] = baseTable[i-1]
  1048  		baseTable[i].AddMixed(base)
  1049  	}
  1050  	toReturn := make([]G2Affine, len(scalars))
  1051  
  1052  	// partition the scalars into digits
  1053  	digits, _ := partitionScalars(scalars, c, runtime.NumCPU())
  1054  
  1055  	// for each digit, take value in the base table, double it c time, voilà.
  1056  	parallel.Execute(len(scalars), func(start, end int) {
  1057  		var p G2Jac
  1058  		for i := start; i < end; i++ {
  1059  			p.Set(&g2Infinity)
  1060  			for chunk := nbChunks - 1; chunk >= 0; chunk-- {
  1061  				if chunk != nbChunks-1 {
  1062  					for j := uint64(0); j < c; j++ {
  1063  						p.DoubleAssign()
  1064  					}
  1065  				}
  1066  				offset := chunk * len(scalars)
  1067  				digit := digits[i+offset]
  1068  
  1069  				if digit == 0 {
  1070  					continue
  1071  				}
  1072  
  1073  				// if msbWindow bit is set, we need to subtract
  1074  				if digit&1 == 0 {
  1075  					// add
  1076  					p.AddAssign(&baseTable[(digit>>1)-1])
  1077  				} else {
  1078  					// sub
  1079  					t := baseTable[digit>>1]
  1080  					t.Neg(&t)
  1081  					p.AddAssign(&t)
  1082  				}
  1083  			}
  1084  
  1085  			// set our result point
  1086  			toReturn[i].FromJacobian(&p)
  1087  
  1088  		}
  1089  	})
  1090  	return toReturn
  1091  }
  1092  
  1093  // batchAddG2Affine adds affine points using the Montgomery batch inversion trick.
  1094  // Special cases (doubling, infinity) must be filtered out before this call.
  1095  func batchAddG2Affine[TP pG2Affine, TPP ppG2Affine, TC cG2Affine](R *TPP, P *TP, batchSize int) {
  1096  	var lambda, lambdain TC
  1097  
  1098  	// add part
  1099  	for j := 0; j < batchSize; j++ {
  1100  		lambdain[j].Sub(&(*P)[j].X, &(*R)[j].X)
  1101  	}
  1102  
  1103  	// invert denominator using montgomery batch invert technique
  1104  	{
  1105  		var accumulator fp.Element
  1106  		lambda[0].SetOne()
  1107  		accumulator.Set(&lambdain[0])
  1108  
  1109  		for i := 1; i < batchSize; i++ {
  1110  			lambda[i] = accumulator
  1111  			accumulator.Mul(&accumulator, &lambdain[i])
  1112  		}
  1113  
  1114  		accumulator.Inverse(&accumulator)
  1115  
  1116  		for i := batchSize - 1; i > 0; i-- {
  1117  			lambda[i].Mul(&lambda[i], &accumulator)
  1118  			accumulator.Mul(&accumulator, &lambdain[i])
  1119  		}
  1120  		lambda[0].Set(&accumulator)
  1121  	}
  1122  
  1123  	var d fp.Element
  1124  	var rr G2Affine
  1125  
  1126  	// add part
  1127  	for j := 0; j < batchSize; j++ {
  1128  		// computa lambda
  1129  		d.Sub(&(*P)[j].Y, &(*R)[j].Y)
  1130  		lambda[j].Mul(&lambda[j], &d)
  1131  
  1132  		// compute X, Y
  1133  		rr.X.Square(&lambda[j])
  1134  		rr.X.Sub(&rr.X, &(*R)[j].X)
  1135  		rr.X.Sub(&rr.X, &(*P)[j].X)
  1136  		d.Sub(&(*R)[j].X, &rr.X)
  1137  		rr.Y.Mul(&lambda[j], &d)
  1138  		rr.Y.Sub(&rr.Y, &(*R)[j].Y)
  1139  		(*R)[j].Set(&rr)
  1140  	}
  1141  }
  1142  
  1143  // RandomOnG2 produces a random point in G2
  1144  // using standard map-to-curve methods, which means the relative discrete log
  1145  // of the generated point with respect to the canonical generator is not known.
  1146  func RandomOnG2() (G2Affine, error) {
  1147  	if gBytes, err := randomFrSizedBytes(); err != nil {
  1148  		return G2Affine{}, err
  1149  	} else {
  1150  		return HashToG2(gBytes, []byte("random on g2"))
  1151  	}
  1152  }
  1153  
  1154  func randomFrSizedBytes() ([]byte, error) {
  1155  	res := make([]byte, fr.Bytes)
  1156  	_, err := rand.Read(res)
  1157  	return res, err
  1158  }