Skip to content

Commit ede59c5

Browse files
armfazhbwesterb
authored andcommitted
Implement Granger-Scott faster squaring in the cyclotomic subgroup.
1 parent c48866b commit ede59c5

6 files changed

Lines changed: 56 additions & 6 deletions

File tree

ecc/bls12381/ff/cyclo6.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,44 @@ func (z Cyclo6) IsEqual(x *Cyclo6) int { return (Fp12)(z).IsEqual((*Fp12)(x))
88
func (z Cyclo6) IsIdentity() int { i := &Fp12{}; i.SetOne(); return z.IsEqual((*Cyclo6)(i)) }
99
func (z *Cyclo6) Frob(x *Cyclo6) { (*Fp12)(z).Frob((*Fp12)(x)) }
1010
func (z *Cyclo6) Mul(x, y *Cyclo6) { (*Fp12)(z).Mul((*Fp12)(x), (*Fp12)(y)) }
11-
func (z *Cyclo6) Sqr(x *Cyclo6) { (*Fp12)(z).Sqr((*Fp12)(x)) }
1211
func (z *Cyclo6) Inv(x *Cyclo6) { *z = *x; z[1].Neg() }
1312
func (z *Cyclo6) exp(x *Cyclo6, n []byte) { (*Fp12)(z).Exp((*Fp12)(x), n) }
13+
func (z *Cyclo6) Sqr(x *Cyclo6) {
14+
// Method of Granger-Scott.
15+
// Page 7 of "Faster Squaring in the Cyclotomic Subgroup of Sixth Degree Extensions"
16+
// https://www.iacr.org/archive/pkc2010/60560212/60560212.pdf
17+
var xx, zz Fp12Cubic
18+
xx.FromFp12((*Fp12)(x))
19+
20+
a, b, c := &xx[0], &xx[1], &xx[2]
21+
z0, z1, z2 := &zz[0], &zz[1], &zz[2]
22+
23+
var aa, bb, cc, tt Fp4
24+
aa.Sqr(a)
25+
bb.Sqr(b)
26+
cc.Sqr(c)
27+
cc.mulT(&cc)
28+
29+
z0.Add(&aa, &aa)
30+
z0.Add(z0, &aa)
31+
tt.Add(a, a)
32+
tt.Cjg()
33+
z0.Sub(z0, &tt)
34+
35+
z1.Add(&cc, &cc)
36+
z1.Add(z1, &cc)
37+
tt.Add(b, b)
38+
tt.Cjg()
39+
z1.Add(z1, &tt)
40+
41+
z2.Add(&bb, &bb)
42+
z2.Add(z2, &bb)
43+
tt.Add(c, c)
44+
tt.Cjg()
45+
z2.Sub(z2, &tt)
46+
47+
(*Fp12)(z).FromFp12Cubic(&zz)
48+
}
1449

1550
// PowToX computes z = x^paramX, where paramX is the parameter of the BLS curve.
1651
func (z *Cyclo6) PowToX(x *Cyclo6) {

ecc/bls12381/ff/cyclo6_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ func TestCyclo6(t *testing.T) {
8282
}
8383
}
8484
})
85+
t.Run("sqr_sqrfasr", func(t *testing.T) {
86+
var want, got Cyclo6
87+
for i := 0; i < testTimes; i++ {
88+
x := randomCyclo6(t)
89+
90+
// Specialized square in cyclotomic vs Generic Square in Fp12
91+
got.Sqr(x)
92+
(*Fp12)(&want).Sqr((*Fp12)(x))
93+
if got.IsEqual(&want) == 0 {
94+
test.ReportError(t, got, want, x)
95+
}
96+
}
97+
})
8598

8699
t.Run("invFp12_vs_invCyclo6", func(t *testing.T) {
87100
var want, got Fp12

ecc/bls12381/ff/fp12cubic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (z *Fp12Cubic) FromFp12(x *Fp12) {
2929
z[2][1] = x[1][2] // w^5
3030
}
3131

32-
func (z *Fp12) FromFp12Alt(x *Fp12Cubic) {
32+
func (z *Fp12) FromFp12Cubic(x *Fp12Cubic) {
3333
z[0][0] = x[0][0] // w^0
3434
z[1][0] = x[1][0] // w^1
3535
z[0][1] = x[2][0] // w^2

ecc/bls12381/ff/fp12cubic_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestFP12CubicAdd(t *testing.T) {
1717
yalt.FromFp12(y)
1818
zalt.Add(&xalt, &yalt)
1919
z.Add(x, y)
20-
zcmp.FromFp12Alt(&zalt)
20+
zcmp.FromFp12Cubic(&zalt)
2121
if z.IsEqual(&zcmp) == 0 {
2222
test.ReportError(t, z, zcmp, x, y)
2323
}
@@ -35,7 +35,7 @@ func TestFP12CubicMul(t *testing.T) {
3535
yalt.FromFp12(y)
3636
zalt.Mul(&xalt, &yalt)
3737
z.Mul(x, y)
38-
zcmp.FromFp12Alt(&zalt)
38+
zcmp.FromFp12Cubic(&zalt)
3939
if z.IsEqual(&zcmp) == 0 {
4040
test.ReportError(t, z, zcmp, x, y)
4141
}
@@ -51,7 +51,7 @@ func TestFP12AltSqr(t *testing.T) {
5151
xalt.FromFp12(x)
5252
zalt.Sqr(&xalt)
5353
z.Sqr(x)
54-
zcmp.FromFp12Alt(&zalt)
54+
zcmp.FromFp12Cubic(&zalt)
5555
if z.IsEqual(&zcmp) == 0 {
5656
test.ReportError(t, z, zcmp, x)
5757
}

ecc/bls12381/ff/fp4.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func (z *Fp4) IsEqual(x *Fp4) int {
2323
return z[0].IsEqual(&x[0]) & z[1].IsEqual(&x[1])
2424
}
2525

26+
func (z *Fp4) Cjg() { z[1].Neg() }
27+
2628
func (z *Fp4) Neg() {
2729
z[0].Neg()
2830
z[1].Neg()

ecc/bls12381/pair.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func miller(f *ff.Fp12, P *G1, Q *G2) {
3232
acc.MulLine(acc, g)
3333
}
3434
}
35-
f.FromFp12Alt(acc)
35+
f.FromFp12Cubic(acc)
3636
f.Cjg() // inverts f as paramX is negative.
3737
}
3838

0 commit comments

Comments
 (0)