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