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