github.com/consensys/gnark-crypto@v0.14.0/ecc/bw6-761/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 bw6761 18 19 import ( 20 "crypto/rand" 21 "github.com/consensys/gnark-crypto/ecc" 22 "github.com/consensys/gnark-crypto/ecc/bw6-761/fp" 23 "github.com/consensys/gnark-crypto/ecc/bw6-761/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=4 487 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 // Z[r,0]+Z[-lambdaG2Affine, 1] is the kernel 495 // of (u,v)->u+lambdaG2Affinev mod r. Expressing r, lambdaG2Affine as 496 // polynomials in x, a short vector of this Zmodule is 497 // (x+1), (x³-x²+1). So we check that (x+1)p+(x³-x²+1)ϕ(p) 498 // is the infinity. 499 func (p *G2Jac) IsInSubGroup() bool { 500 501 var res, phip G2Jac 502 phip.phi(p) 503 res.ScalarMultiplication(&phip, &xGen). 504 SubAssign(&phip). 505 ScalarMultiplication(&res, &xGen). 506 ScalarMultiplication(&res, &xGen). 507 AddAssign(&phip) 508 509 phip.ScalarMultiplication(p, &xGen).AddAssign(p).AddAssign(&res) 510 511 return phip.IsOnCurve() && phip.Z.IsZero() 512 513 } 514 515 // mulWindowed computes the 2-bits windowed double-and-add scalar 516 // multiplication p=[s]q in Jacobian coordinates. 517 func (p *G2Jac) mulWindowed(q *G2Jac, s *big.Int) *G2Jac { 518 519 var res G2Jac 520 var ops [3]G2Jac 521 522 ops[0].Set(q) 523 if s.Sign() == -1 { 524 ops[0].Neg(&ops[0]) 525 } 526 res.Set(&g2Infinity) 527 ops[1].Double(&ops[0]) 528 ops[2].Set(&ops[0]).AddAssign(&ops[1]) 529 530 b := s.Bytes() 531 for i := range b { 532 w := b[i] 533 mask := byte(0xc0) 534 for j := 0; j < 4; j++ { 535 res.DoubleAssign().DoubleAssign() 536 c := (w & mask) >> (6 - 2*j) 537 if c != 0 { 538 res.AddAssign(&ops[c-1]) 539 } 540 mask = mask >> 2 541 } 542 } 543 p.Set(&res) 544 545 return p 546 547 } 548 549 // phi sets p to ϕ(a) where ϕ: (x,y) → (w x,y), 550 // where w is a third root of unity. 551 func (p *G2Jac) phi(q *G2Jac) *G2Jac { 552 p.Set(q) 553 p.X.Mul(&p.X, &thirdRootOneG2) 554 return p 555 } 556 557 // mulGLV computes the scalar multiplication using a windowed-GLV method 558 // 559 // see https://www.iacr.org/archive/crypto2001/21390189.pdf 560 func (p *G2Jac) mulGLV(q *G2Jac, s *big.Int) *G2Jac { 561 562 var table [15]G2Jac 563 var res G2Jac 564 var k1, k2 fr.Element 565 566 res.Set(&g2Infinity) 567 568 // table[b3b2b1b0-1] = b3b2 ⋅ ϕ(q) + b1b0*q 569 table[0].Set(q) 570 table[3].phi(q) 571 572 // split the scalar, modifies ±q, ϕ(q) accordingly 573 k := ecc.SplitScalar(s, &glvBasis) 574 575 if k[0].Sign() == -1 { 576 k[0].Neg(&k[0]) 577 table[0].Neg(&table[0]) 578 } 579 if k[1].Sign() == -1 { 580 k[1].Neg(&k[1]) 581 table[3].Neg(&table[3]) 582 } 583 584 // precompute table (2 bits sliding window) 585 // table[b3b2b1b0-1] = b3b2 ⋅ ϕ(q) + b1b0 ⋅ q if b3b2b1b0 != 0 586 table[1].Double(&table[0]) 587 table[2].Set(&table[1]).AddAssign(&table[0]) 588 table[4].Set(&table[3]).AddAssign(&table[0]) 589 table[5].Set(&table[3]).AddAssign(&table[1]) 590 table[6].Set(&table[3]).AddAssign(&table[2]) 591 table[7].Double(&table[3]) 592 table[8].Set(&table[7]).AddAssign(&table[0]) 593 table[9].Set(&table[7]).AddAssign(&table[1]) 594 table[10].Set(&table[7]).AddAssign(&table[2]) 595 table[11].Set(&table[7]).AddAssign(&table[3]) 596 table[12].Set(&table[11]).AddAssign(&table[0]) 597 table[13].Set(&table[11]).AddAssign(&table[1]) 598 table[14].Set(&table[11]).AddAssign(&table[2]) 599 600 // bounds on the lattice base vectors guarantee that k1, k2 are len(r)/2 or len(r)/2+1 bits long max 601 // this is because we use a probabilistic scalar decomposition that replaces a division by a right-shift 602 k1 = k1.SetBigInt(&k[0]).Bits() 603 k2 = k2.SetBigInt(&k[1]).Bits() 604 605 // we don't target constant-timeness so we check first if we increase the bounds or not 606 maxBit := k1.BitLen() 607 if k2.BitLen() > maxBit { 608 maxBit = k2.BitLen() 609 } 610 hiWordIndex := (maxBit - 1) / 64 611 612 // loop starts from len(k1)/2 or len(k1)/2+1 due to the bounds 613 for i := hiWordIndex; i >= 0; i-- { 614 mask := uint64(3) << 62 615 for j := 0; j < 32; j++ { 616 res.Double(&res).Double(&res) 617 b1 := (k1[i] & mask) >> (62 - 2*j) 618 b2 := (k2[i] & mask) >> (62 - 2*j) 619 if b1|b2 != 0 { 620 s := (b2<<2 | b1) 621 res.AddAssign(&table[s-1]) 622 } 623 mask = mask >> 2 624 } 625 } 626 627 p.Set(&res) 628 return p 629 } 630 631 // ClearCofactor maps a point in curve to r-torsion 632 func (p *G2Affine) ClearCofactor(a *G2Affine) *G2Affine { 633 var _p G2Jac 634 _p.FromAffine(a) 635 _p.ClearCofactor(&_p) 636 p.FromJacobian(&_p) 637 return p 638 } 639 640 // ClearCofactor maps a point in curve to r-torsion 641 func (p *G2Jac) ClearCofactor(q *G2Jac) *G2Jac { 642 var points [4]G2Jac 643 points[0].Set(q) 644 points[1].ScalarMultiplication(q, &xGen) 645 points[2].ScalarMultiplication(&points[1], &xGen) 646 points[3].ScalarMultiplication(&points[2], &xGen) 647 648 var scalars [7]big.Int 649 scalars[0].SetInt64(103) 650 scalars[1].SetInt64(83) 651 scalars[2].SetInt64(143) 652 scalars[3].SetInt64(27) 653 654 scalars[4].SetInt64(7) 655 scalars[5].SetInt64(117) 656 scalars[6].SetInt64(109) 657 658 var p1, p2, tmp G2Jac 659 p1.ScalarMultiplication(&points[3], &scalars[0]) 660 tmp.ScalarMultiplication(&points[2], &scalars[1]).Neg(&tmp) 661 p1.AddAssign(&tmp) 662 tmp.ScalarMultiplication(&points[1], &scalars[2]).Neg(&tmp) 663 p1.AddAssign(&tmp) 664 tmp.ScalarMultiplication(&points[0], &scalars[3]) 665 p1.AddAssign(&tmp) 666 667 p2.ScalarMultiplication(&points[2], &scalars[4]) 668 tmp.ScalarMultiplication(&points[1], &scalars[5]).Neg(&tmp) 669 p2.AddAssign(&tmp) 670 tmp.ScalarMultiplication(&points[0], &scalars[6]).Neg(&tmp) 671 p2.AddAssign(&tmp) 672 p2.phi(&p2).phi(&p2) 673 674 p.Set(&p1).AddAssign(&p2) 675 676 return p 677 678 } 679 680 // ------------------------------------------------------------------------------------------------- 681 // extended Jacobian coordinates 682 683 // Set sets p to a in extended Jacobian coordinates. 684 func (p *g2JacExtended) Set(q *g2JacExtended) *g2JacExtended { 685 p.X, p.Y, p.ZZ, p.ZZZ = q.X, q.Y, q.ZZ, q.ZZZ 686 return p 687 } 688 689 // setInfinity sets p to the infinity point (1,1,0,0). 690 func (p *g2JacExtended) setInfinity() *g2JacExtended { 691 p.X.SetOne() 692 p.Y.SetOne() 693 p.ZZ = fp.Element{} 694 p.ZZZ = fp.Element{} 695 return p 696 } 697 698 // IsInfinity checks if the p is infinity, i.e. p.ZZ=0. 699 func (p *g2JacExtended) IsInfinity() bool { 700 return p.ZZ.IsZero() 701 } 702 703 // fromJacExtended converts an extended Jacobian point to an affine point. 704 func (p *G2Affine) fromJacExtended(q *g2JacExtended) *G2Affine { 705 if q.ZZ.IsZero() { 706 p.X = fp.Element{} 707 p.Y = fp.Element{} 708 return p 709 } 710 p.X.Inverse(&q.ZZ).Mul(&p.X, &q.X) 711 p.Y.Inverse(&q.ZZZ).Mul(&p.Y, &q.Y) 712 return p 713 } 714 715 // fromJacExtended converts an extended Jacobian point to a Jacobian point. 716 func (p *G2Jac) fromJacExtended(q *g2JacExtended) *G2Jac { 717 if q.ZZ.IsZero() { 718 p.Set(&g2Infinity) 719 return p 720 } 721 p.X.Mul(&q.ZZ, &q.X).Mul(&p.X, &q.ZZ) 722 p.Y.Mul(&q.ZZZ, &q.Y).Mul(&p.Y, &q.ZZZ) 723 p.Z.Set(&q.ZZZ) 724 return p 725 } 726 727 // unsafeFromJacExtended converts an extended Jacobian point, distinct from Infinity, to a Jacobian point. 728 func (p *G2Jac) unsafeFromJacExtended(q *g2JacExtended) *G2Jac { 729 p.X.Square(&q.ZZ).Mul(&p.X, &q.X) 730 p.Y.Square(&q.ZZZ).Mul(&p.Y, &q.Y) 731 p.Z = q.ZZZ 732 return p 733 } 734 735 // add sets p to p+q in extended Jacobian coordinates. 736 // 737 // https://www.hyperelliptic.org/EFD/g1p/auto-shortw-xyzz.html#addition-add-2008-s 738 func (p *g2JacExtended) add(q *g2JacExtended) *g2JacExtended { 739 //if q is infinity return p 740 if q.ZZ.IsZero() { 741 return p 742 } 743 // p is infinity, return q 744 if p.ZZ.IsZero() { 745 p.Set(q) 746 return p 747 } 748 749 var A, B, U1, U2, S1, S2 fp.Element 750 751 // p2: q, p1: p 752 U2.Mul(&q.X, &p.ZZ) 753 U1.Mul(&p.X, &q.ZZ) 754 A.Sub(&U2, &U1) 755 S2.Mul(&q.Y, &p.ZZZ) 756 S1.Mul(&p.Y, &q.ZZZ) 757 B.Sub(&S2, &S1) 758 759 if A.IsZero() { 760 if B.IsZero() { 761 return p.double(q) 762 763 } 764 p.ZZ = fp.Element{} 765 p.ZZZ = fp.Element{} 766 return p 767 } 768 769 var P, R, PP, PPP, Q, V fp.Element 770 P.Sub(&U2, &U1) 771 R.Sub(&S2, &S1) 772 PP.Square(&P) 773 PPP.Mul(&P, &PP) 774 Q.Mul(&U1, &PP) 775 V.Mul(&S1, &PPP) 776 777 p.X.Square(&R). 778 Sub(&p.X, &PPP). 779 Sub(&p.X, &Q). 780 Sub(&p.X, &Q) 781 p.Y.Sub(&Q, &p.X). 782 Mul(&p.Y, &R). 783 Sub(&p.Y, &V) 784 p.ZZ.Mul(&p.ZZ, &q.ZZ). 785 Mul(&p.ZZ, &PP) 786 p.ZZZ.Mul(&p.ZZZ, &q.ZZZ). 787 Mul(&p.ZZZ, &PPP) 788 789 return p 790 } 791 792 // double sets p to [2]q in Jacobian extended coordinates. 793 // 794 // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-xyzz.html#doubling-dbl-2008-s-1 795 // N.B.: since we consider any point on Z=0 as the point at infinity 796 // this doubling formula works for infinity points as well. 797 func (p *g2JacExtended) double(q *g2JacExtended) *g2JacExtended { 798 var U, V, W, S, XX, M fp.Element 799 800 U.Double(&q.Y) 801 V.Square(&U) 802 W.Mul(&U, &V) 803 S.Mul(&q.X, &V) 804 XX.Square(&q.X) 805 M.Double(&XX). 806 Add(&M, &XX) // -> + A, but A=0 here 807 U.Mul(&W, &q.Y) 808 809 p.X.Square(&M). 810 Sub(&p.X, &S). 811 Sub(&p.X, &S) 812 p.Y.Sub(&S, &p.X). 813 Mul(&p.Y, &M). 814 Sub(&p.Y, &U) 815 p.ZZ.Mul(&V, &q.ZZ) 816 p.ZZZ.Mul(&W, &q.ZZZ) 817 818 return p 819 } 820 821 // addMixed sets p to p+q in extended Jacobian coordinates, where a.ZZ=1. 822 // 823 // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-xyzz.html#addition-madd-2008-s 824 func (p *g2JacExtended) addMixed(a *G2Affine) *g2JacExtended { 825 826 //if a is infinity return p 827 if a.IsInfinity() { 828 return p 829 } 830 // p is infinity, return a 831 if p.ZZ.IsZero() { 832 p.X = a.X 833 p.Y = a.Y 834 p.ZZ.SetOne() 835 p.ZZZ.SetOne() 836 return p 837 } 838 839 var P, R fp.Element 840 841 // p2: a, p1: p 842 P.Mul(&a.X, &p.ZZ) 843 P.Sub(&P, &p.X) 844 845 R.Mul(&a.Y, &p.ZZZ) 846 R.Sub(&R, &p.Y) 847 848 if P.IsZero() { 849 if R.IsZero() { 850 return p.doubleMixed(a) 851 852 } 853 p.ZZ = fp.Element{} 854 p.ZZZ = fp.Element{} 855 return p 856 } 857 858 var PP, PPP, Q, Q2, RR, X3, Y3 fp.Element 859 860 PP.Square(&P) 861 PPP.Mul(&P, &PP) 862 Q.Mul(&p.X, &PP) 863 RR.Square(&R) 864 X3.Sub(&RR, &PPP) 865 Q2.Double(&Q) 866 p.X.Sub(&X3, &Q2) 867 Y3.Sub(&Q, &p.X).Mul(&Y3, &R) 868 R.Mul(&p.Y, &PPP) 869 p.Y.Sub(&Y3, &R) 870 p.ZZ.Mul(&p.ZZ, &PP) 871 p.ZZZ.Mul(&p.ZZZ, &PPP) 872 873 return p 874 875 } 876 877 // subMixed works the same as addMixed, but negates a.Y. 878 // 879 // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-xyzz.html#addition-madd-2008-s 880 func (p *g2JacExtended) subMixed(a *G2Affine) *g2JacExtended { 881 882 //if a is infinity return p 883 if a.IsInfinity() { 884 return p 885 } 886 // p is infinity, return a 887 if p.ZZ.IsZero() { 888 p.X = a.X 889 p.Y.Neg(&a.Y) 890 p.ZZ.SetOne() 891 p.ZZZ.SetOne() 892 return p 893 } 894 895 var P, R fp.Element 896 897 // p2: a, p1: p 898 P.Mul(&a.X, &p.ZZ) 899 P.Sub(&P, &p.X) 900 901 R.Mul(&a.Y, &p.ZZZ) 902 R.Neg(&R) 903 R.Sub(&R, &p.Y) 904 905 if P.IsZero() { 906 if R.IsZero() { 907 return p.doubleNegMixed(a) 908 909 } 910 p.ZZ = fp.Element{} 911 p.ZZZ = fp.Element{} 912 return p 913 } 914 915 var PP, PPP, Q, Q2, RR, X3, Y3 fp.Element 916 917 PP.Square(&P) 918 PPP.Mul(&P, &PP) 919 Q.Mul(&p.X, &PP) 920 RR.Square(&R) 921 X3.Sub(&RR, &PPP) 922 Q2.Double(&Q) 923 p.X.Sub(&X3, &Q2) 924 Y3.Sub(&Q, &p.X).Mul(&Y3, &R) 925 R.Mul(&p.Y, &PPP) 926 p.Y.Sub(&Y3, &R) 927 p.ZZ.Mul(&p.ZZ, &PP) 928 p.ZZZ.Mul(&p.ZZZ, &PPP) 929 930 return p 931 932 } 933 934 // doubleNegMixed works the same as double, but negates q.Y. 935 func (p *g2JacExtended) doubleNegMixed(a *G2Affine) *g2JacExtended { 936 937 var U, V, W, S, XX, M, S2, L fp.Element 938 939 U.Double(&a.Y) 940 U.Neg(&U) 941 V.Square(&U) 942 W.Mul(&U, &V) 943 S.Mul(&a.X, &V) 944 XX.Square(&a.X) 945 M.Double(&XX). 946 Add(&M, &XX) // -> + A, but A=0 here 947 S2.Double(&S) 948 L.Mul(&W, &a.Y) 949 950 p.X.Square(&M). 951 Sub(&p.X, &S2) 952 p.Y.Sub(&S, &p.X). 953 Mul(&p.Y, &M). 954 Add(&p.Y, &L) 955 p.ZZ.Set(&V) 956 p.ZZZ.Set(&W) 957 958 return p 959 } 960 961 // doubleMixed sets p to [2]a in Jacobian extended coordinates, where a.ZZ=1. 962 // 963 // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-xyzz.html#doubling-dbl-2008-s-1 964 func (p *g2JacExtended) doubleMixed(a *G2Affine) *g2JacExtended { 965 966 var U, V, W, S, XX, M, S2, L fp.Element 967 968 U.Double(&a.Y) 969 V.Square(&U) 970 W.Mul(&U, &V) 971 S.Mul(&a.X, &V) 972 XX.Square(&a.X) 973 M.Double(&XX). 974 Add(&M, &XX) // -> + A, but A=0 here 975 S2.Double(&S) 976 L.Mul(&W, &a.Y) 977 978 p.X.Square(&M). 979 Sub(&p.X, &S2) 980 p.Y.Sub(&S, &p.X). 981 Mul(&p.Y, &M). 982 Sub(&p.Y, &L) 983 p.ZZ.Set(&V) 984 p.ZZZ.Set(&W) 985 986 return p 987 } 988 989 // ------------------------------------------------------------------------------------------------- 990 // Homogenous projective coordinates 991 992 // Set sets p to a in projective coordinates. 993 func (p *g2Proj) Set(q *g2Proj) *g2Proj { 994 p.x, p.y, p.z = q.x, q.y, q.z 995 return p 996 } 997 998 // Neg sets p to the projective negative point -q = (q.X, -q.Y). 999 func (p *g2Proj) Neg(q *g2Proj) *g2Proj { 1000 *p = *q 1001 p.y.Neg(&q.y) 1002 return p 1003 } 1004 1005 // FromAffine converts q in affine to p in projective coordinates. 1006 func (p *g2Proj) FromAffine(a *G2Affine) *g2Proj { 1007 if a.X.IsZero() && a.Y.IsZero() { 1008 p.z.SetZero() 1009 p.x.SetOne() 1010 p.y.SetOne() 1011 return p 1012 } 1013 p.z.SetOne() 1014 p.x.Set(&a.X) 1015 p.y.Set(&a.Y) 1016 return p 1017 } 1018 1019 // BatchScalarMultiplicationG2 multiplies the same base by all scalars 1020 // and return resulting points in affine coordinates 1021 // uses a simple windowed-NAF-like multiplication algorithm. 1022 func BatchScalarMultiplicationG2(base *G2Affine, scalars []fr.Element) []G2Affine { 1023 // approximate cost in group ops is 1024 // cost = 2^{c-1} + n(scalar.nbBits+nbChunks) 1025 1026 nbPoints := uint64(len(scalars)) 1027 min := ^uint64(0) 1028 bestC := 0 1029 for c := 2; c <= 16; c++ { 1030 cost := uint64(1 << (c - 1)) // pre compute the table 1031 nbChunks := computeNbChunks(uint64(c)) 1032 cost += nbPoints * (uint64(c) + 1) * nbChunks // doublings + point add 1033 if cost < min { 1034 min = cost 1035 bestC = c 1036 } 1037 } 1038 c := uint64(bestC) // window size 1039 nbChunks := int(computeNbChunks(c)) 1040 1041 // last window may be slightly larger than c; in which case we need to compute one 1042 // extra element in the baseTable 1043 maxC := lastC(c) 1044 if c > maxC { 1045 maxC = c 1046 } 1047 1048 // precompute all powers of base for our window 1049 // note here that if performance is critical, we can implement as in the msmX methods 1050 // this allocation to be on the stack 1051 baseTable := make([]G2Jac, (1 << (maxC - 1))) 1052 baseTable[0].FromAffine(base) 1053 for i := 1; i < len(baseTable); i++ { 1054 baseTable[i] = baseTable[i-1] 1055 baseTable[i].AddMixed(base) 1056 } 1057 toReturn := make([]G2Affine, len(scalars)) 1058 1059 // partition the scalars into digits 1060 digits, _ := partitionScalars(scalars, c, runtime.NumCPU()) 1061 1062 // for each digit, take value in the base table, double it c time, voilà. 1063 parallel.Execute(len(scalars), func(start, end int) { 1064 var p G2Jac 1065 for i := start; i < end; i++ { 1066 p.Set(&g2Infinity) 1067 for chunk := nbChunks - 1; chunk >= 0; chunk-- { 1068 if chunk != nbChunks-1 { 1069 for j := uint64(0); j < c; j++ { 1070 p.DoubleAssign() 1071 } 1072 } 1073 offset := chunk * len(scalars) 1074 digit := digits[i+offset] 1075 1076 if digit == 0 { 1077 continue 1078 } 1079 1080 // if msbWindow bit is set, we need to subtract 1081 if digit&1 == 0 { 1082 // add 1083 p.AddAssign(&baseTable[(digit>>1)-1]) 1084 } else { 1085 // sub 1086 t := baseTable[digit>>1] 1087 t.Neg(&t) 1088 p.AddAssign(&t) 1089 } 1090 } 1091 1092 // set our result point 1093 toReturn[i].FromJacobian(&p) 1094 1095 } 1096 }) 1097 return toReturn 1098 } 1099 1100 // batchAddG2Affine adds affine points using the Montgomery batch inversion trick. 1101 // Special cases (doubling, infinity) must be filtered out before this call. 1102 func batchAddG2Affine[TP pG2Affine, TPP ppG2Affine, TC cG2Affine](R *TPP, P *TP, batchSize int) { 1103 var lambda, lambdain TC 1104 1105 // add part 1106 for j := 0; j < batchSize; j++ { 1107 lambdain[j].Sub(&(*P)[j].X, &(*R)[j].X) 1108 } 1109 1110 // invert denominator using montgomery batch invert technique 1111 { 1112 var accumulator fp.Element 1113 lambda[0].SetOne() 1114 accumulator.Set(&lambdain[0]) 1115 1116 for i := 1; i < batchSize; i++ { 1117 lambda[i] = accumulator 1118 accumulator.Mul(&accumulator, &lambdain[i]) 1119 } 1120 1121 accumulator.Inverse(&accumulator) 1122 1123 for i := batchSize - 1; i > 0; i-- { 1124 lambda[i].Mul(&lambda[i], &accumulator) 1125 accumulator.Mul(&accumulator, &lambdain[i]) 1126 } 1127 lambda[0].Set(&accumulator) 1128 } 1129 1130 var d fp.Element 1131 var rr G2Affine 1132 1133 // add part 1134 for j := 0; j < batchSize; j++ { 1135 // computa lambda 1136 d.Sub(&(*P)[j].Y, &(*R)[j].Y) 1137 lambda[j].Mul(&lambda[j], &d) 1138 1139 // compute X, Y 1140 rr.X.Square(&lambda[j]) 1141 rr.X.Sub(&rr.X, &(*R)[j].X) 1142 rr.X.Sub(&rr.X, &(*P)[j].X) 1143 d.Sub(&(*R)[j].X, &rr.X) 1144 rr.Y.Mul(&lambda[j], &d) 1145 rr.Y.Sub(&rr.Y, &(*R)[j].Y) 1146 (*R)[j].Set(&rr) 1147 } 1148 } 1149 1150 // RandomOnG2 produces a random point in G2 1151 // using standard map-to-curve methods, which means the relative discrete log 1152 // of the generated point with respect to the canonical generator is not known. 1153 func RandomOnG2() (G2Affine, error) { 1154 if gBytes, err := randomFrSizedBytes(); err != nil { 1155 return G2Affine{}, err 1156 } else { 1157 return HashToG2(gBytes, []byte("random on g2")) 1158 } 1159 } 1160 1161 func randomFrSizedBytes() ([]byte, error) { 1162 res := make([]byte, fr.Bytes) 1163 _, err := rand.Read(res) 1164 return res, err 1165 }