Skip to content

Commit a4b7601

Browse files
committed
Ensure pairing functions don't overwrite the input.
1 parent b4f1578 commit a4b7601

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

ecc/bls12381/g1.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ func G1Generator() *G1 {
392392
}
393393

394394
// affinize converts an entire slice to affine at once
395-
func affinize(points []*G1) {
395+
func affinize(points []*G1) (out []G1) {
396+
out = make([]G1, len(points))
396397
if len(points) == 0 {
397398
return
398399
}
@@ -410,8 +411,9 @@ func affinize(points []*G1) {
410411
zinv.Mul(w, &ws[i])
411412
w.Mul(w, &points[i].z)
412413

413-
points[i].x.Mul(&points[i].x, zinv)
414-
points[i].y.Mul(&points[i].y, zinv)
415-
points[i].z.SetOne()
414+
out[i].x.Mul(&points[i].x, zinv)
415+
out[i].y.Mul(&points[i].y, zinv)
416+
out[i].z.SetOne()
416417
}
418+
return
417419
}

ecc/bls12381/g1_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,14 @@ func TestG1Affinize(t *testing.T) {
218218
N := 20
219219
testTimes := 1 << 6
220220
g1 := make([]*G1, N)
221-
g2 := make([]*G1, N)
222221
for i := 0; i < testTimes; i++ {
223222
for j := 0; j < N; j++ {
224223
g1[j] = randomG1(t)
225-
g2[j] = &G1{}
226-
*g2[j] = *g1[j]
227224
}
228-
affinize(g2)
225+
g2 := affinize(g1)
229226
for j := 0; j < N; j++ {
230227
g1[j].toAffine()
231-
if !g1[j].IsEqual(g2[j]) {
228+
if !g1[j].IsEqual(&g2[j]) {
232229
t.Fatal("failure to preserve points")
233230
}
234231
if g2[j].z.IsEqual(&g1[j].z) != 1 {

ecc/bls12381/pair.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import "github.com/cloudflare/circl/ecc/bls12381/ff"
44

55
// Pair calculates the ate-pairing of P and Q.
66
func Pair(P *G1, Q *G2) *Gt {
7-
P.toAffine()
7+
affP := *P
8+
affP.toAffine()
89
mi := &ff.Fp12{}
9-
miller(mi, P, Q)
10+
miller(mi, &affP, Q)
1011
e := &Gt{}
1112
finalExp(e, mi)
1213
return e
@@ -82,9 +83,9 @@ func ProdPair(P []*G1, Q []*G2, n []*Scalar) *Gt {
8283
out := new(ff.Fp12)
8384
out.SetOne()
8485

85-
affinize(P)
86-
for i := range P {
87-
miller(mi, P[i], Q[i])
86+
affineP := affinize(P)
87+
for i := range affineP {
88+
miller(mi, &affineP[i], Q[i])
8889
nb, _ := n[i].MarshalBinary()
8990
ei.Exp(mi, nb)
9091
out.Mul(out, ei)
@@ -105,13 +106,12 @@ func ProdPairFrac(P []*G1, Q []*G2, signs []int) *Gt {
105106
out := new(ff.Fp12)
106107
out.SetOne()
107108

108-
affinize(P)
109-
for i := range P {
110-
g := *P[i]
109+
affineP := affinize(P)
110+
for i := range affineP {
111111
if signs[i] == -1 {
112-
g.Neg()
112+
affineP[i].Neg()
113113
}
114-
miller(mi, &g, Q[i])
114+
miller(mi, &affineP[i], Q[i])
115115
out.Mul(mi, out)
116116
}
117117

0 commit comments

Comments
 (0)