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