Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: faster final exponentiation (hard part) for BW6 curves #459

Merged
merged 6 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 1 addition & 72 deletions ecc/bw6-633/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,30 +208,12 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
// precomputations
pProj0 := make([]g1Proj, n)
p1 := make([]G1Affine, n)
pProj01 := make([]g1Proj, n) // P0+P1
pProj10 := make([]g1Proj, n) // P0-P1
l01 := make([]lineEvaluation, n)
l10 := make([]lineEvaluation, n)
for k := 0; k < n; k++ {
p1[k].Y.Set(&p0[k].Y)
p1[k].X.Mul(&p0[k].X, &thirdRootOneG1)
p0[k].Neg(&p0[k])
pProj0[k].FromAffine(&p0[k])

// l_{p0,p1}(q)
pProj01[k].Set(&pProj0[k])
pProj01[k].addMixedStep(&l01[k], &p1[k])
l01[k].r1.Mul(&l01[k].r1, &q[k].X)
l01[k].r0.Mul(&l01[k].r0, &q[k].Y)

// l_{-p0,p1}(q)
pProj10[k].Neg(&pProj0[k])
pProj10[k].addMixedStep(&l10[k], &p1[k])
l10[k].r1.Mul(&l10[k].r1, &q[k].X)
l10[k].r0.Mul(&l10[k].r0, &q[k].Y)
}
p01 := BatchProjectiveToAffineG1(pProj01)
p10 := BatchProjectiveToAffineG1(pProj10)

// f_{a0+λ*a1,P}(Q)
var result GT
Expand Down Expand Up @@ -299,20 +281,7 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
l0.r0.Mul(&l0.r0, &q[k].Y)

switch j {
case -4:
tmp.Neg(&p01[k])
// pProj0[k] ← pProj0[k]-p01[k] and
// l the line ℓ passing pProj0[k] and -p01[k]
pProj0[k].addMixedStep(&l, &tmp)
// line evaluation at Q[k]
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
// ℓ × ℓ
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l01[k].r0, &l01[k].r1, &l01[k].r2)
// ℓ × res
result.MulBy034(&l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
// cases -4, -2, 2, 4 do not occur given the static loopCounters
case -3:
tmp.Neg(&p1[k])
// pProj0[k] ← pProj0[k]-p1[k] and
Expand All @@ -325,19 +294,6 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
case -2:
// pProj0[k] ← pProj0[k]+p10[k] and
// l the line ℓ passing pProj0[k] and p10[k]
pProj0[k].addMixedStep(&l, &p10[k])
// line evaluation at Q[k]
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
// ℓ × ℓ
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l01[k].r0, &l01[k].r1, &l01[k].r2)
// ℓ × res
result.MulBy034(&l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
case -1:
tmp.Neg(&p0[k])
// pProj0[k] ← pProj0[k]-p0[k] and
Expand All @@ -364,20 +320,6 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
case 2:
tmp.Neg(&p10[k])
// pProj0[k] ← pProj0[k]-p10[k] and
// l the line ℓ passing pProj0[k] and -p10[k]
pProj0[k].addMixedStep(&l, &tmp)
// line evaluation at Q[k]
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
// ℓ × ℓ
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l01[k].r0, &l01[k].r1, &l01[k].r2)
// ℓ × res
result.MulBy034(&l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
case 3:
// pProj0[k] ← pProj0[k]+p1[k] and
// l the line ℓ passing pProj0[k] and p1[k]
Expand All @@ -389,19 +331,6 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
case 4:
// pProj0[k] ← pProj0[k]+p01[k] and
// l the line ℓ passing pProj0[k] and p01[k]
pProj0[k].addMixedStep(&l, &p01[k])
// line evaluation at Q[k]
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
// ℓ × ℓ
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l01[k].r0, &l01[k].r1, &l01[k].r2)
// ℓ × res
result.MulBy034(&l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
default:
return GT{}, errors.New("invalid loopCounter")
}
Expand Down
72 changes: 0 additions & 72 deletions ecc/bw6-756/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,29 +189,11 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
// precomputations
pProj1 := make([]g1Proj, n)
p1 := make([]G1Affine, n)
pProj01 := make([]g1Proj, n) // P0+P1
pProj10 := make([]g1Proj, n) // P0-P1
l01 := make([]lineEvaluation, n)
l10 := make([]lineEvaluation, n)
for k := 0; k < n; k++ {
p1[k].Y.Neg(&p0[k].Y)
p1[k].X.Mul(&p0[k].X, &thirdRootOneG2)
pProj1[k].FromAffine(&p1[k])

// l_{p0,p1}(q)
pProj01[k].Set(&pProj1[k])
pProj01[k].addMixedStep(&l01[k], &p0[k])
l01[k].r1.Mul(&l01[k].r1, &q[k].X)
l01[k].r0.Mul(&l01[k].r0, &q[k].Y)

// l_{p0,-p1}(q)
pProj10[k].Neg(&pProj1[k])
pProj10[k].addMixedStep(&l10[k], &p0[k])
l10[k].r1.Mul(&l10[k].r1, &q[k].X)
l10[k].r0.Mul(&l10[k].r0, &q[k].Y)
}
p01 := BatchProjectiveToAffineG1(pProj01)
p10 := BatchProjectiveToAffineG1(pProj10)

// f_{a0+λ*a1,P}(Q)
var result GT
Expand Down Expand Up @@ -279,20 +261,6 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
l0.r0.Mul(&l0.r0, &q[k].Y)

switch j {
case -4:
tmp.Neg(&p01[k])
// pProj1[k] ← pProj1[k]-p01[k] and
// l the line ℓ passing pProj1[k] and -p01[k]
pProj1[k].addMixedStep(&l, &tmp)
// line evaluation at Q[k]
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
// ℓ × ℓ
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l01[k].r0, &l01[k].r1, &l01[k].r2)
// ℓ × res
result.MulBy034(&l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
case -3:
tmp.Neg(&p1[k])
// pProj1[k] ← pProj1[k]-p1[k] and
Expand All @@ -305,19 +273,6 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
case -2:
// pProj1[k] ← pProj1[k]+p10[k] and
// l the line ℓ passing pProj1[k] and p10[k]
pProj1[k].addMixedStep(&l, &p10[k])
// line evaluation at Q[k]
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
// ℓ × ℓ
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l01[k].r0, &l01[k].r1, &l01[k].r2)
// ℓ × res
result.MulBy034(&l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
case -1:
tmp.Neg(&p0[k])
// pProj1[k] ← pProj1[k]-p0[k] and
Expand All @@ -344,20 +299,6 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
case 2:
tmp.Neg(&p10[k])
// pProj1[k] ← pProj1[k]-p10[k] and
// l the line ℓ passing pProj1[k] and -p10[k]
pProj1[k].addMixedStep(&l, &tmp)
// line evaluation at Q[k]
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
// ℓ × ℓ
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l01[k].r0, &l01[k].r1, &l01[k].r2)
// ℓ × res
result.MulBy034(&l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
case 3:
// pProj1[k] ← pProj1[k]+p1[k] and
// l the line ℓ passing pProj1[k] and p1[k]
Expand All @@ -369,19 +310,6 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
case 4:
// pProj1[k] ← pProj1[k]+p01[k] and
// l the line ℓ passing pProj1[k] and p01[k]
pProj1[k].addMixedStep(&l, &p01[k])
// line evaluation at Q[k]
l.r1.Mul(&l.r1, &q[k].X)
l.r0.Mul(&l.r0, &q[k].Y)
// ℓ × ℓ
prodLines = fptower.Mul034By034(&l.r0, &l.r1, &l.r2, &l01[k].r0, &l01[k].r1, &l01[k].r2)
// ℓ × res
result.MulBy034(&l0.r0, &l0.r1, &l0.r2)
// (ℓ × ℓ) × res
result.MulBy01234(&prodLines)
default:
return GT{}, errors.New("invalid loopCounter")
}
Expand Down
125 changes: 93 additions & 32 deletions ecc/bw6-761/internal/fptower/e6_pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ func (z *E6) nSquareCompressed(n int) {
}
}

// Expt set z to x^t in E6 and return z
func (z *E6) Expt(x *E6) *E6 {
// const tAbsVal uint64 = 9586122913090633729
// tAbsVal in binary: 1000010100001000110000000000000000000000000000000000000000000001
// drop the low 46 bits (all 0 except the least significant bit): 100001010000100011 = 136227
// Shortest addition chains can be found at https://wwwhomes.uni-bielefeld.de/achim/addition_chain.html
// ExptMinus1 set z to x^(t-1) in E6 and return z
// t-1 = 91893752504881257682351033800651177983
func (z *E6) ExptMinus1(x *E6) *E6 {

var result, x33 E6

// a shortest addition chain for 136227
result.Set(x)
result.nSquare(5)
result.Mul(&result, x)
Expand All @@ -34,55 +29,121 @@ func (z *E6) Expt(x *E6) *E6 {
result.Mul(&result, x)
result.CyclotomicSquare(&result)
result.Mul(&result, x)

// the remaining 46 bits
result.nSquareCompressed(46)
result.DecompressKarabina(&result)
result.Mul(&result, x)

z.Set(&result)

return z
}

// Expc2 set z to x^c2 in E6 and return z
// ht, hy = 13, 9
// c1 = ht+hy = 22 (10110)
func (z *E6) Expc2(x *E6) *E6 {
// ExptMinus1Square set z to x^{(t-1)²} in E6 and return z
// (t-1)² = 91893752504881257682351033800651177984
func (z *E6) ExptMinus1Square(x *E6) *E6 {
var result, t0, t1, t2 E6

result.Set(x)
result.CyclotomicSquare(&result)
t0.Mul(x, &result)
t1.CyclotomicSquare(&t0)
t0.Mul(&t0, &t1)
result.Mul(&result, &t0)
t1.Mul(&t1, &result)
t0.Mul(&t0, &t1)
t2.CyclotomicSquare(&t0)
t2.Mul(&t1, &t2)
t0.Mul(&t0, &t2)
t2.nSquare(7)
t1.Mul(&t1, &t2)
t1.nSquare(11)
t1.Mul(&t0, &t1)
t1.nSquare(9)
t0.Mul(&t0, &t1)
t0.CyclotomicSquare(&t0)
result.Mul(&result, &t0)
result.nSquareCompressed(92)
result.DecompressKarabina(&result)
z.Set(&result)

return z
}

// Expt set z to x^t in E6 and return z
// t = 91893752504881257682351033800651177984
func (z *E6) Expt(x *E6) *E6 {
var result E6

result.CyclotomicSquare(x)
result.CyclotomicSquare(&result)
result.ExptMinus1(x)
result.Mul(&result, x)
z.Set(&result)

return z
}

// ExptPlus1 set z to x^(t+1) in E6 and return z
// t+1 = 91893752504881257682351033800651177985
func (z *E6) ExptPlus1(x *E6) *E6 {
var result, t E6

result.ExptMinus1(x)
t.CyclotomicSquare(x)
result.Mul(&result, &t)
z.Set(&result)

return z
}

// ExptMinus1Div3 set z to x^(t-1)/3 in E6 and return z
// (t-1)/3 = 3195374304363544576
func (z *E6) ExptMinus1Div3(x *E6) *E6 {
var result, t0 E6

result.Set(x)
result.CyclotomicSquare(&result)
result.Mul(&result, x)
result.CyclotomicSquare(&result)

t0.Mul(&result, x)
t0.CyclotomicSquare(&t0)
result.Mul(&result, &t0)
t0.Set(&result)
t0.nSquare(7)
result.Mul(&result, &t0)
result.nSquare(5)
result.Mul(&result, x)
result.nSquareCompressed(46)
result.DecompressKarabina(&result)
z.Set(&result)

return z
}

// Expc1 set z to x^c1 in E6 and return z
// Expc1 set z to z^c1 in E6 and return z
// ht, hy = 13, 9
// c1 = ht**2+3*hy**2 = 412 (110011100)
// c1 = (ht+hy)/2 = 11
func (z *E6) Expc1(x *E6) *E6 {

var result E6
var result, t0 E6

result.CyclotomicSquare(x)
result.Mul(&result, x)
result.CyclotomicSquare(&result)
result.CyclotomicSquare(&result)
result.CyclotomicSquare(&result)
result.Mul(&result, x)
result.CyclotomicSquare(&result)
t0.Mul(x, &result)
t0.CyclotomicSquare(&t0)
result.Mul(&result, &t0)
z.Set(&result)

return z
}

// Expx2 set z to z^c2 in E6 and return z
// ht, hy = 13, 9
// c2 = (ht**2+3*hy**2)/4 = 103
func (z *E6) Expc2(x *E6) *E6 {
var result, t0 E6

result.CyclotomicSquare(x)
result.Mul(&result, x)
t0.Set(&result)
t0.nSquare(4)
result.Mul(&result, &t0)
result.CyclotomicSquare(&result)
result.Mul(&result, x)
result.CyclotomicSquare(&result)
result.CyclotomicSquare(&result)

z.Set(&result)

return z
Expand Down
Loading