Skip to content

Commit c34cc6f

Browse files
authored
Merge pull request #524 from Consensys/refactor/bls12-MillerLoopFixed
refactor(bls12-381, bls12-377): MillerLoopFixed corresponds to gnark
2 parents 20fab15 + ffaaea7 commit c34cc6f

File tree

2 files changed

+100
-8
lines changed

2 files changed

+100
-8
lines changed

ecc/bls12-377/pairing.go

+48-3
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,10 @@ func PrecomputeLines(Q G2Affine) (PrecomputedLines [2][len(LoopCounter) - 1]Line
384384
accQ.Set(&Q)
385385

386386
for i := len(LoopCounter) - 2; i >= 0; i-- {
387-
accQ.doubleStep(&PrecomputedLines[0][i])
388387
if LoopCounter[i] == 0 {
389-
continue
388+
accQ.doubleStep(&PrecomputedLines[0][i])
390389
} else {
391-
accQ.addStep(&PrecomputedLines[1][i], &Q)
390+
accQ.doubleAndAddStep(&PrecomputedLines[0][i], &PrecomputedLines[1][i], &Q)
392391
}
393392
}
394393
return PrecomputedLines
@@ -573,3 +572,49 @@ func (p *G2Affine) addStep(evaluations *LineEvaluationAff, a *G2Affine) {
573572
p.X.Set(&xr)
574573
p.Y.Set(&yr)
575574
}
575+
576+
func (p *G2Affine) doubleAndAddStep(evaluations1, evaluations2 *LineEvaluationAff, a *G2Affine) {
577+
var n, d, l1, x3, l2, x4, y4 fptower.E2
578+
579+
// compute λ1 = (y2-y1)/(x2-x1)
580+
n.Sub(&p.Y, &a.Y)
581+
d.Sub(&p.X, &a.X)
582+
l1.Div(&n, &d)
583+
584+
// compute x3 =λ1²-x1-x2
585+
x3.Square(&l1)
586+
x3.Sub(&x3, &p.X)
587+
x3.Sub(&x3, &a.X)
588+
589+
// omit y3 computation
590+
591+
// compute line1
592+
evaluations1.R0.Set(&l1)
593+
evaluations1.R1.Mul(&l1, &p.X)
594+
evaluations1.R1.Sub(&evaluations1.R1, &p.Y)
595+
596+
// compute λ2 = -λ1-2y1/(x3-x1)
597+
n.Double(&p.Y)
598+
d.Sub(&x3, &p.X)
599+
l2.Div(&n, &d)
600+
l2.Add(&l2, &l1)
601+
l2.Neg(&l2)
602+
603+
// compute x4 = λ2²-x1-x3
604+
x4.Square(&l2)
605+
x4.Sub(&x4, &p.X)
606+
x4.Sub(&x4, &x3)
607+
608+
// compute y4 = λ2(x1 - x4)-y1
609+
y4.Sub(&p.X, &x4)
610+
y4.Mul(&l2, &y4)
611+
y4.Sub(&y4, &p.Y)
612+
613+
// compute line2
614+
evaluations2.R0.Set(&l2)
615+
evaluations2.R1.Mul(&l2, &p.X)
616+
evaluations2.R1.Sub(&evaluations2.R1, &p.Y)
617+
618+
p.X.Set(&x4)
619+
p.Y.Set(&y4)
620+
}

ecc/bls12-381/pairing.go

+52-5
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,15 @@ func PairingCheckFixedQ(P []G1Affine, lines [][2][len(LoopCounter) - 1]LineEvalu
386386
func PrecomputeLines(Q G2Affine) (PrecomputedLines [2][len(LoopCounter) - 1]LineEvaluationAff) {
387387
var accQ G2Affine
388388
accQ.Set(&Q)
389-
390389
n := len(LoopCounter)
391-
for i := n - 2; i >= 0; i-- {
392-
accQ.doubleStep(&PrecomputedLines[0][i])
390+
// i = n - 2
391+
accQ.doubleStep(&PrecomputedLines[0][n-2])
392+
accQ.addStep(&PrecomputedLines[1][n-2], &Q)
393+
for i := n - 3; i >= 0; i-- {
393394
if LoopCounter[i] == 0 {
394-
continue
395+
accQ.doubleStep(&PrecomputedLines[0][i])
395396
} else {
396-
accQ.addStep(&PrecomputedLines[1][i], &Q)
397+
accQ.doubleAndAddStep(&PrecomputedLines[0][i], &PrecomputedLines[1][i], &Q)
397398
}
398399
}
399400
return PrecomputedLines
@@ -541,3 +542,49 @@ func (p *G2Affine) addStep(evaluations *LineEvaluationAff, a *G2Affine) {
541542
p.X.Set(&xr)
542543
p.Y.Set(&yr)
543544
}
545+
546+
func (p *G2Affine) doubleAndAddStep(evaluations1, evaluations2 *LineEvaluationAff, a *G2Affine) {
547+
var n, d, l1, x3, l2, x4, y4 fptower.E2
548+
549+
// compute λ1 = (y2-y1)/(x2-x1)
550+
n.Sub(&p.Y, &a.Y)
551+
d.Sub(&p.X, &a.X)
552+
l1.Div(&n, &d)
553+
554+
// compute x3 =λ1²-x1-x2
555+
x3.Square(&l1)
556+
x3.Sub(&x3, &p.X)
557+
x3.Sub(&x3, &a.X)
558+
559+
// omit y3 computation
560+
561+
// compute line1
562+
evaluations1.R0.Set(&l1)
563+
evaluations1.R1.Mul(&l1, &p.X)
564+
evaluations1.R1.Sub(&evaluations1.R1, &p.Y)
565+
566+
// compute λ2 = -λ1-2y1/(x3-x1)
567+
n.Double(&p.Y)
568+
d.Sub(&x3, &p.X)
569+
l2.Div(&n, &d)
570+
l2.Add(&l2, &l1)
571+
l2.Neg(&l2)
572+
573+
// compute x4 = λ2²-x1-x3
574+
x4.Square(&l2)
575+
x4.Sub(&x4, &p.X)
576+
x4.Sub(&x4, &x3)
577+
578+
// compute y4 = λ2(x1 - x4)-y1
579+
y4.Sub(&p.X, &x4)
580+
y4.Mul(&l2, &y4)
581+
y4.Sub(&y4, &p.Y)
582+
583+
// compute line2
584+
evaluations2.R0.Set(&l2)
585+
evaluations2.R1.Mul(&l2, &p.X)
586+
evaluations2.R1.Sub(&evaluations2.R1, &p.Y)
587+
588+
p.X.Set(&x4)
589+
p.Y.Set(&y4)
590+
}

0 commit comments

Comments
 (0)