Skip to content

Commit

Permalink
Ensure pairing functions don't overwrite the input.
Browse files Browse the repository at this point in the history
  • Loading branch information
armfazh committed Apr 6, 2024
1 parent b4f1578 commit a4b7601
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
10 changes: 6 additions & 4 deletions ecc/bls12381/g1.go
Expand Up @@ -392,7 +392,8 @@ func G1Generator() *G1 {
}

// affinize converts an entire slice to affine at once
func affinize(points []*G1) {
func affinize(points []*G1) (out []G1) {
out = make([]G1, len(points))
if len(points) == 0 {
return
}
Expand All @@ -410,8 +411,9 @@ func affinize(points []*G1) {
zinv.Mul(w, &ws[i])
w.Mul(w, &points[i].z)

points[i].x.Mul(&points[i].x, zinv)
points[i].y.Mul(&points[i].y, zinv)
points[i].z.SetOne()
out[i].x.Mul(&points[i].x, zinv)
out[i].y.Mul(&points[i].y, zinv)
out[i].z.SetOne()
}
return
}
7 changes: 2 additions & 5 deletions ecc/bls12381/g1_test.go
Expand Up @@ -218,17 +218,14 @@ func TestG1Affinize(t *testing.T) {
N := 20
testTimes := 1 << 6
g1 := make([]*G1, N)
g2 := make([]*G1, N)
for i := 0; i < testTimes; i++ {
for j := 0; j < N; j++ {
g1[j] = randomG1(t)
g2[j] = &G1{}
*g2[j] = *g1[j]
}
affinize(g2)
g2 := affinize(g1)
for j := 0; j < N; j++ {
g1[j].toAffine()
if !g1[j].IsEqual(g2[j]) {
if !g1[j].IsEqual(&g2[j]) {
t.Fatal("failure to preserve points")
}
if g2[j].z.IsEqual(&g1[j].z) != 1 {
Expand Down
20 changes: 10 additions & 10 deletions ecc/bls12381/pair.go
Expand Up @@ -4,9 +4,10 @@ import "github.com/cloudflare/circl/ecc/bls12381/ff"

// Pair calculates the ate-pairing of P and Q.
func Pair(P *G1, Q *G2) *Gt {
P.toAffine()
affP := *P
affP.toAffine()
mi := &ff.Fp12{}
miller(mi, P, Q)
miller(mi, &affP, Q)
e := &Gt{}
finalExp(e, mi)
return e
Expand Down Expand Up @@ -82,9 +83,9 @@ func ProdPair(P []*G1, Q []*G2, n []*Scalar) *Gt {
out := new(ff.Fp12)
out.SetOne()

affinize(P)
for i := range P {
miller(mi, P[i], Q[i])
affineP := affinize(P)
for i := range affineP {
miller(mi, &affineP[i], Q[i])
nb, _ := n[i].MarshalBinary()
ei.Exp(mi, nb)
out.Mul(out, ei)
Expand All @@ -105,13 +106,12 @@ func ProdPairFrac(P []*G1, Q []*G2, signs []int) *Gt {
out := new(ff.Fp12)
out.SetOne()

affinize(P)
for i := range P {
g := *P[i]
affineP := affinize(P)
for i := range affineP {
if signs[i] == -1 {
g.Neg()
affineP[i].Neg()
}
miller(mi, &g, Q[i])
miller(mi, &affineP[i], Q[i])
out.Mul(mi, out)
}

Expand Down

0 comments on commit a4b7601

Please sign in to comment.