Skip to content

Commit b7ee69d

Browse files
committed
test: add gopter compatibility test against reference implementation
1 parent f0f347b commit b7ee69d

File tree

9 files changed

+562
-0
lines changed

9 files changed

+562
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2020-2025 Consensys Software Inc.
2+
// Licensed under the Apache License, Version 2.0. See the LICENSE file for details.
3+
4+
package bls12377
5+
6+
import (
7+
"github.com/consensys/gnark-crypto/ecc/bls12-377/internal/fptower"
8+
)
9+
10+
// doubleAndAddStepRef is the reference (pre-optimization) implementation
11+
// of the doubleAndAddStep function. It computes 2P+Q using two field inversions.
12+
//
13+
// This version uses the standard chord-tangent method:
14+
// - λ1 = (y2-y1)/(x2-x1) for P + Q
15+
// - λ2 = -λ1 - 2y1/(x3-x1) for doubling and adding back
16+
//
17+
// The optimized version uses the Eisenträger-Lauter-Montgomery formula
18+
// (https://eprint.iacr.org/2003/257) which computes both slopes with a single
19+
// field inversion via Montgomery's batch inversion trick.
20+
func doubleAndAddStepRef(p *G2Affine, evaluations1, evaluations2 *LineEvaluationAff, a *G2Affine) {
21+
var n, d, l1, x3, l2, x4, y4 fptower.E2
22+
23+
// compute λ1 = (y2-y1)/(x2-x1)
24+
n.Sub(&p.Y, &a.Y)
25+
d.Sub(&p.X, &a.X)
26+
l1.Div(&n, &d)
27+
28+
// compute x3 =λ1²-x1-x2
29+
x3.Square(&l1)
30+
x3.Sub(&x3, &p.X)
31+
x3.Sub(&x3, &a.X)
32+
33+
// omit y3 computation
34+
35+
// compute line1
36+
evaluations1.R0.Set(&l1)
37+
evaluations1.R1.Mul(&l1, &p.X)
38+
evaluations1.R1.Sub(&evaluations1.R1, &p.Y)
39+
40+
// compute λ2 = -λ1-2y1/(x3-x1)
41+
n.Double(&p.Y)
42+
d.Sub(&x3, &p.X)
43+
l2.Div(&n, &d)
44+
l2.Add(&l2, &l1)
45+
l2.Neg(&l2)
46+
47+
// compute x4 = λ2²-x1-x3
48+
x4.Square(&l2)
49+
x4.Sub(&x4, &p.X)
50+
x4.Sub(&x4, &x3)
51+
52+
// compute y4 = λ2(x1 - x4)-y1
53+
y4.Sub(&p.X, &x4)
54+
y4.Mul(&l2, &y4)
55+
y4.Sub(&y4, &p.Y)
56+
57+
// compute line2
58+
evaluations2.R0.Set(&l2)
59+
evaluations2.R1.Mul(&l2, &p.X)
60+
evaluations2.R1.Sub(&evaluations2.R1, &p.Y)
61+
62+
p.X.Set(&x4)
63+
p.Y.Set(&y4)
64+
}

ecc/bls12-377/pairing_test.go

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2020-2025 Consensys Software Inc.
2+
// Licensed under the Apache License, Version 2.0. See the LICENSE file for details.
3+
4+
package bls12381
5+
6+
import (
7+
"github.com/consensys/gnark-crypto/ecc/bls12-381/internal/fptower"
8+
)
9+
10+
// doubleAndAddStepRef is the reference (pre-optimization) implementation
11+
// of the doubleAndAddStep function. It computes 2P+Q using two field inversions.
12+
//
13+
// This version uses the standard chord-tangent method:
14+
// - λ1 = (y2-y1)/(x2-x1) for P + Q
15+
// - λ2 = -λ1 - 2y1/(x3-x1) for doubling and adding back
16+
//
17+
// The optimized version uses the Eisenträger-Lauter-Montgomery formula
18+
// (https://eprint.iacr.org/2003/257) which computes both slopes with a single
19+
// field inversion via Montgomery's batch inversion trick.
20+
func doubleAndAddStepRef(p *G2Affine, evaluations1, evaluations2 *LineEvaluationAff, a *G2Affine) {
21+
var n, d, l1, x3, l2, x4, y4 fptower.E2
22+
23+
// compute λ1 = (y2-y1)/(x2-x1)
24+
n.Sub(&p.Y, &a.Y)
25+
d.Sub(&p.X, &a.X)
26+
l1.Div(&n, &d)
27+
28+
// compute x3 =λ1²-x1-x2
29+
x3.Square(&l1)
30+
x3.Sub(&x3, &p.X)
31+
x3.Sub(&x3, &a.X)
32+
33+
// omit y3 computation
34+
35+
// compute line1
36+
evaluations1.R0.Set(&l1)
37+
evaluations1.R1.Mul(&l1, &p.X)
38+
evaluations1.R1.Sub(&evaluations1.R1, &p.Y)
39+
40+
// compute λ2 = -λ1-2y1/(x3-x1)
41+
n.Double(&p.Y)
42+
d.Sub(&x3, &p.X)
43+
l2.Div(&n, &d)
44+
l2.Add(&l2, &l1)
45+
l2.Neg(&l2)
46+
47+
// compute x4 = λ2²-x1-x3
48+
x4.Square(&l2)
49+
x4.Sub(&x4, &p.X)
50+
x4.Sub(&x4, &x3)
51+
52+
// compute y4 = λ2(x1 - x4)-y1
53+
y4.Sub(&p.X, &x4)
54+
y4.Mul(&l2, &y4)
55+
y4.Sub(&y4, &p.Y)
56+
57+
// compute line2
58+
evaluations2.R0.Set(&l2)
59+
evaluations2.R1.Mul(&l2, &p.X)
60+
evaluations2.R1.Sub(&evaluations2.R1, &p.Y)
61+
62+
p.X.Set(&x4)
63+
p.Y.Set(&y4)
64+
}

ecc/bls12-381/pairing_test.go

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2020-2025 Consensys Software Inc.
2+
// Licensed under the Apache License, Version 2.0. See the LICENSE file for details.
3+
4+
package bn254
5+
6+
import (
7+
"github.com/consensys/gnark-crypto/ecc/bn254/internal/fptower"
8+
)
9+
10+
// doubleAndAddStepRef is the reference (pre-optimization) implementation
11+
// of the doubleAndAddStep function. It computes 2P+Q using two field inversions.
12+
//
13+
// This version uses the standard chord-tangent method:
14+
// - λ1 = (y2-y1)/(x2-x1) for P + Q
15+
// - λ2 = -λ1 - 2y1/(x3-x1) for doubling and adding back
16+
//
17+
// The optimized version uses the Eisenträger-Lauter-Montgomery formula
18+
// (https://eprint.iacr.org/2003/257) which computes both slopes with a single
19+
// field inversion via Montgomery's batch inversion trick.
20+
func doubleAndAddStepRef(p *G2Affine, evaluations1, evaluations2 *LineEvaluationAff, a *G2Affine) {
21+
var n, d, l1, x3, l2, x4, y4 fptower.E2
22+
23+
// compute λ1 = (y2-y1)/(x2-x1)
24+
n.Sub(&p.Y, &a.Y)
25+
d.Sub(&p.X, &a.X)
26+
l1.Div(&n, &d)
27+
28+
// compute x3 =λ1²-x1-x2
29+
x3.Square(&l1)
30+
x3.Sub(&x3, &p.X)
31+
x3.Sub(&x3, &a.X)
32+
33+
// omit y3 computation
34+
35+
// compute line1
36+
evaluations1.R0.Set(&l1)
37+
evaluations1.R1.Mul(&l1, &p.X)
38+
evaluations1.R1.Sub(&evaluations1.R1, &p.Y)
39+
40+
// compute λ2 = -λ1-2y1/(x3-x1)
41+
n.Double(&p.Y)
42+
d.Sub(&x3, &p.X)
43+
l2.Div(&n, &d)
44+
l2.Add(&l2, &l1)
45+
l2.Neg(&l2)
46+
47+
// compute x4 = λ2²-x1-x3
48+
x4.Square(&l2)
49+
x4.Sub(&x4, &p.X)
50+
x4.Sub(&x4, &x3)
51+
52+
// compute y4 = λ2(x1 - x4)-y1
53+
y4.Sub(&p.X, &x4)
54+
y4.Mul(&l2, &y4)
55+
y4.Sub(&y4, &p.Y)
56+
57+
// compute line2
58+
evaluations2.R0.Set(&l2)
59+
evaluations2.R1.Mul(&l2, &p.X)
60+
evaluations2.R1.Sub(&evaluations2.R1, &p.Y)
61+
62+
p.X.Set(&x4)
63+
p.Y.Set(&y4)
64+
}

ecc/bn254/pairing_test.go

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)