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

Refactor(BW6-761): use revisited Ate pairing instead of Tate #876

Merged
merged 7 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
185 changes: 72 additions & 113 deletions std/algebra/emulated/fields_bw6761/e6_pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,65 +127,32 @@ func (e Ext6) ExpC2(z *E6) *E6 {
return result
}

// Square034 squares an E6 sparse element of the form
// MulBy014 multiplies z by an E6 sparse element of the form
//
// E6{
// B0: E3{A0: 1, A1: 0, A2: 0},
// B1: E3{A0: c3, A1: c4, A2: 0},
// B0: E3{A0: c0, A1: c1, A2: 0},
// B1: E3{A0: 0, A1: 1, A2: 0},
// }
func (e *Ext6) Square034(x *E6) *E6 {
x = e.Reduce(x)
c0 := &E3{
A0: *e.fp.Sub(&x.B0.A0, &x.B1.A0),
A1: *e.fp.Neg(&x.B1.A1),
A2: *e.fp.Zero(),
}

c3 := &E3{
A0: x.B0.A0,
A1: *e.fp.Neg(&x.B1.A0),
A2: *e.fp.Neg(&x.B1.A1),
}

c2 := &E3{
A0: x.B1.A0,
A1: x.B1.A1,
A2: *e.fp.Zero(),
}
c3 = e.MulBy01(c3, &c0.A0, &c0.A1)
c3 = e.Ext3.Add(c3, c2)

var z E6
z.B1.A0 = *e.fp.Add(&c2.A0, &c2.A0)
z.B1.A1 = *e.fp.Add(&c2.A1, &c2.A1)

z.B0.A0 = c3.A0
z.B0.A1 = *e.fp.Add(&c3.A1, &c2.A0)
z.B0.A2 = *e.fp.Add(&c3.A2, &c2.A1)
func (e *Ext6) MulBy014(z *E6, c0, c1 *baseEl) *E6 {
z = e.Reduce(z)

return &z
}
a := e.MulBy01(&z.B0, c0, c1)

// MulBy034 multiplies z by an E6 sparse element of the form
//
// E6{
// B0: E3{A0: 1, A1: 0, A2: 0},
// B1: E3{A0: c3, A1: c4, A2: 0},
// }
func (e *Ext6) MulBy034(z *E6, c3, c4 *baseEl) *E6 {
var b E3
// Mul by E3{0, 1, 0}
b.A0 = *mulFpByNonResidue(e.fp, &z.B1.A2)
b.A2 = z.B1.A1
b.A1 = z.B1.A0

z = e.Reduce(z)
a := z.B0
b := e.MulBy01(&z.B1, c3, c4)
c3 = e.fp.Add(e.fp.One(), c3)
d := e.Ext3.Add(&z.B0, &z.B1)
d = e.MulBy01(d, c3, c4)
one := e.fp.One()
d := e.fp.Add(c1, one)

zC1 := e.Ext3.Add(&a, b)
zC1 = e.Ext3.Neg(zC1)
zC1 = e.Ext3.Add(zC1, d)
zC0 := e.Ext3.MulByNonResidue(b)
zC0 = e.Ext3.Add(zC0, &a)
zC1 := e.Ext3.Add(&z.B1, &z.B0)
zC1 = e.Ext3.MulBy01(zC1, c0, d)
zC1 = e.Ext3.Sub(zC1, a)
zC1 = e.Ext3.Sub(zC1, &b)
zC0 := e.Ext3.MulByNonResidue(&b)
zC0 = e.Ext3.Add(zC0, a)

return &E6{
B0: *zC0,
Expand All @@ -196,85 +163,77 @@ func (e *Ext6) MulBy034(z *E6, c3, c4 *baseEl) *E6 {
// multiplies two E6 sparse element of the form:
//
// E6{
// C0: E6{B0: 1, B1: 0, B2: 0},
// C1: E6{B0: c3, B1: c4, B2: 0},
// B0: E3{A0: c0, A1: c1, A2: 0},
// B1: E3{A0: 0, A1: 1, A2: 0},
// }
//
// and
//
// E6{
// C0: E6{B0: 1, B1: 0, B2: 0},
// C1: E6{B0: d3, B1: d4, B2: 0},
// B0: E3{A0: d0, A1: d1, A2: 0},
// B1: E3{A0: 0, A1: 1, A2: 0},
// }
func (e *Ext6) Mul034By034(d3, d4, c3, c4 *baseEl) [5]*baseEl {
x3 := e.fp.Mul(c3, d3)
x4 := e.fp.Mul(c4, d4)
x04 := e.fp.Add(c4, d4)
x03 := e.fp.Add(c3, d3)
tmp := e.fp.Add(c3, c4)
x34 := e.fp.Add(d3, d4)
x34 = e.fp.Mul(x34, tmp)
x34 = e.fp.Sub(x34, x3)
x34 = e.fp.Sub(x34, x4)

zC0B0 := mulFpByNonResidue(e.fp, x4)
zC0B0 = e.fp.Add(zC0B0, e.fp.One())
zC0B1 := x3
zC0B2 := x34
zC1B0 := x03
zC1B1 := x04

return [5]*baseEl{zC0B0, zC0B1, zC0B2, zC1B0, zC1B1}
func (e Ext6) Mul014By014(d0, d1, c0, c1 *baseEl) [5]*baseEl {
one := e.fp.One()
x0 := e.fp.Mul(c0, d0)
x1 := e.fp.Mul(c1, d1)
tmp := e.fp.Add(c0, one)
x04 := e.fp.Add(d0, one)
x04 = e.fp.Mul(x04, tmp)
x04 = e.fp.Sub(x04, x0)
x04 = e.fp.Sub(x04, one)
tmp = e.fp.Add(c0, c1)
x01 := e.fp.Add(d0, d1)
x01 = e.fp.Mul(x01, tmp)
x01 = e.fp.Sub(x01, x0)
x01 = e.fp.Sub(x01, x1)
tmp = e.fp.Add(c1, one)
x14 := e.fp.Add(d1, one)
x14 = e.fp.Mul(x14, tmp)
x14 = e.fp.Sub(x14, x1)
x14 = e.fp.Sub(x14, one)

zC0B0 := e.fp.Add(one, one)
zC0B0 = e.fp.Add(zC0B0, zC0B0)
zC0B0 = e.fp.Neg(zC0B0)

zC0B0 = e.fp.Add(zC0B0, x0)

return [5]*baseEl{zC0B0, x01, x1, x04, x14}
}

// MulBy01234 multiplies z by an E6 sparse element of the form
// Mul01245By014 multiplies two E6 sparse element of the form
//
// E6{
// C0: E3{A0: c0, A1: c1, A2: c2},
// C1: E3{A0: c3, A1: c4, A2: 0},
// C0: E3{B0: x0, B1: x1, B2: x2},
// C1: E3{B0: 0, B1: x4, B2: x5},
// }
func (e *Ext6) MulBy01234(z *E6, x [5]*baseEl) *E6 {
c0 := &E3{A0: *x[0], A1: *x[1], A2: *x[2]}
c1 := &E3{A0: *x[3], A1: *x[4], A2: *e.fp.Zero()}
a := e.Ext3.Add(&z.B0, &z.B1)
b := e.Ext3.Add(c0, c1)
a = e.Ext3.Mul(a, b)
b = e.Ext3.Mul(&z.B0, c0)
c := e.Ext3.MulBy01(&z.B1, x[3], x[4])
z1 := e.Ext3.Sub(a, b)
z1 = e.Ext3.Sub(z1, c)
z0 := e.Ext3.MulByNonResidue(c)
z0 = e.Ext3.Add(z0, b)
return &E6{
B0: *z0,
B1: *z1,
}
}

// multiplies two E6 sparse element of the form:
//
// E6{
// C0: E2{A0: x0, A1: x1, A2: x2},
// C1: E2{A0: x3, A1: x4, A2: 0},
// }
//
// and
// and
//
// E6{
// C0: E3{A0: 1, A1: 0, A2: 0},
// C1: E3{A0: z3, A1: z4, A2: 0},
// C0: E3{B0: d0, B1: d1, B2: 0},
// C1: E3{B0: 0, B1: 1, B2: 0},
// }
func (e *Ext6) Mul01234By034(x [5]*baseEl, z3, z4 *baseEl) *E6 {
func (e *Ext6) Mul01245By014(x [5]*baseEl, d0, d1 *baseEl) *E6 {
zero := e.fp.Zero()
c0 := &E3{A0: *x[0], A1: *x[1], A2: *x[2]}
c1 := &E3{A0: *x[3], A1: *x[4], A2: *e.fp.Zero()}
a := e.Ext3.Add(e.Ext3.One(), &E3{A0: *z3, A1: *z4, A2: *e.fp.Zero()})
b := e.Ext3.Add(c0, c1)
a = e.Ext3.Mul(a, b)
c := e.Ext3.Mul01By01(z3, z4, x[3], x[4])
z1 := e.Ext3.Sub(a, c0)
b := &E3{
A0: *x[0],
A1: *e.fp.Add(x[1], x[3]),
A2: *e.fp.Add(x[2], x[4]),
}
a := e.Ext3.MulBy01(b, d0, e.fp.Add(d1, e.fp.One()))
b = e.Ext3.MulBy01(c0, d0, d1)
c := &E3{
A0: *mulFpByNonResidue(e.fp, x[4]),
A1: *zero,
A2: *x[3],
}
z1 := e.Ext3.Sub(a, b)
z1 = e.Ext3.Sub(z1, c)
z0 := e.Ext3.MulByNonResidue(c)
z0 = e.Ext3.Add(z0, c0)
z0 = e.Ext3.Add(z0, b)
return &E6{
B0: *z0,
B1: *z1,
Expand Down
Loading