Skip to content

Commit

Permalink
Merge pull request #1076 from shramee/faster-fq6-01-01
Browse files Browse the repository at this point in the history
Faster cubic 01 01 mul
  • Loading branch information
yelhousni authored Mar 11, 2024
2 parents 1ed22f7 + 19c7716 commit 2e80e8a
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 28 deletions.
Binary file modified internal/stats/latest.stats
Binary file not shown.
11 changes: 2 additions & 9 deletions std/algebra/emulated/fields_bn254/e6.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,15 @@ func (e Ext6) MulBy01(z *E6, c0, c1 *E2) *E6 {
func (e Ext6) Mul01By01(c0, c1, d0, d1 *E2) *E6 {
a := e.Ext2.Mul(d0, c0)
b := e.Ext2.Mul(d1, c1)
t0 := e.Ext2.Mul(c1, d1)
t0 = e.Ext2.Sub(t0, b)
t0 = e.Ext2.MulByNonResidue(t0)
t0 = e.Ext2.Add(t0, a)
t2 := e.Ext2.Mul(c0, d0)
t2 = e.Ext2.Sub(t2, a)
t2 = e.Ext2.Add(t2, b)
t1 := e.Ext2.Add(c0, c1)
tmp := e.Ext2.Add(d0, d1)
t1 = e.Ext2.Mul(t1, tmp)
t1 = e.Ext2.Sub(t1, a)
t1 = e.Ext2.Sub(t1, b)
return &E6{
B0: *t0,
B0: *a,
B1: *t1,
B2: *t2,
B2: *b,
}
}

Expand Down
48 changes: 48 additions & 0 deletions std/algebra/emulated/fields_bn254/e6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,54 @@ func TestMulFp6By01(t *testing.T) {

}

type e6Mul01By01 struct {
A0, A1 E2
B0, B1 E2
C E6 `gnark:",public"`
}

func (circuit *e6Mul01By01) Define(api frontend.API) error {
e := NewExt6(api)
expected := e.Mul01By01(&circuit.A0, &circuit.A1, &circuit.B0, &circuit.B1)
e.AssertIsEqual(expected, &circuit.C)

return nil
}

func TestMul01By01(t *testing.T) {

// we test our new E3.Mul01By01 against E3.MulBy01
assert := test.NewAssert(t)
// witness values
var a, c bn254.E6
var A0, A1, B0, B1 bn254.E2
_, _ = A0.SetRandom()
_, _ = A1.SetRandom()
_, _ = B0.SetRandom()
_, _ = B1.SetRandom()

// build a 01 sparse E3 with,
// first two elements as A1 and A2,
// and the third as 0
a.B0 = A0
a.B1 = A1
a.B2.SetZero()
c.Set(&a)
c.MulBy01(&B0, &B1)

witness := e6Mul01By01{
A0: FromE2(&A0),
A1: FromE2(&A1),
B0: FromE2(&B0),
B1: FromE2(&B1),
C: FromE6(&c),
}

err := test.IsSolved(&e6Mul01By01{}, &witness, ecc.BN254.ScalarField())
assert.NoError(err)

}

type e6MulBy0 struct {
A E6
C0 E2
Expand Down
11 changes: 2 additions & 9 deletions std/algebra/emulated/fields_bw6761/e3.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,22 +235,15 @@ func (e Ext3) MulBy12(x *E3, b1, b2 *baseEl) *E3 {
func (e Ext3) Mul01By01(c0, c1, d0, d1 *baseEl) *E3 {
a := e.fp.Mul(d0, c0)
b := e.fp.Mul(d1, c1)
t0 := e.fp.Mul(c1, d1)
t0 = e.fp.Sub(b, t0)
t0 = e.fp.MulConst(t0, big.NewInt(4))
t0 = e.fp.Add(t0, a)
t2 := e.fp.Mul(c0, d0)
t2 = e.fp.Sub(t2, a)
t2 = e.fp.Add(t2, b)
t1 := e.fp.Add(c0, c1)
tmp := e.fp.Add(d0, d1)
t1 = e.fp.Mul(t1, tmp)
t1 = e.fp.Sub(t1, a)
t1 = e.fp.Sub(t1, b)
return &E3{
A0: *t0,
A0: *a,
A1: *t1,
A2: *t2,
A2: *b,
}
}

Expand Down
47 changes: 47 additions & 0 deletions std/algebra/emulated/fields_bw6761/e3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,53 @@ func TestMulFp3(t *testing.T) {
assert.NoError(err)
}

type e3Mul01By01 struct {
A0, A1 baseEl
B0, B1 baseEl
C E3
}

func (circuit *e3Mul01By01) Define(api frontend.API) error {
e := NewExt3(api)
expected := e.Mul01By01(&circuit.A0, &circuit.A1, &circuit.B0, &circuit.B1)
e.AssertIsEqual(expected, &circuit.C)

return nil
}

func TestMul01By01(t *testing.T) {

// we test our new E3.Mul01By01 against E3.MulBy01
assert := test.NewAssert(t)
// witness values
var a, c bw6761.E3
var A0, A1, B0, B1 fp.Element
A0.SetRandom()
A1.SetRandom()
B0.SetRandom()
B1.SetRandom()
// build a 01 sparse E3 with,
// first two elements as A1 and A2,
// and the third as 0
a.A0 = A0
a.A1 = A1
a.A2.SetZero()
c.Set(&a)
c.MulBy01(&B0, &B1)

witness := e3Mul01By01{
A0: emulated.ValueOf[emulated.BW6761Fp](A0),
A1: emulated.ValueOf[emulated.BW6761Fp](A1),
B0: emulated.ValueOf[emulated.BW6761Fp](B0),
B1: emulated.ValueOf[emulated.BW6761Fp](B1),
C: FromE3(&c),
}

err := test.IsSolved(&e3Mul01By01{}, &witness, ecc.BN254.ScalarField())
assert.NoError(err)

}

type e3MulByNonResidue struct {
A, B E3
}
Expand Down
13 changes: 3 additions & 10 deletions std/algebra/native/fields_bls12377/e6.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,27 +280,20 @@ func (e *E6) MulBy01(api frontend.API, c0, c1 E2) *E6 {
}

func Mul01By01(api frontend.API, c0, c1, d0, d1 E2) *E6 {
var a, b, t0, t1, t2, tmp E2
var a, b, t1, tmp E2

a.Mul(api, d0, c0)
b.Mul(api, d1, c1)
t0.Mul(api, c1, d1)
t0.Sub(api, t0, b)
t0.MulByNonResidue(api, t0)
t0.Add(api, t0, a)
t2.Mul(api, c0, d0)
t2.Sub(api, t2, a)
t2.Add(api, t2, b)
t1.Add(api, c0, c1)
tmp.Add(api, d0, d1)
t1.Mul(api, t1, tmp)
t1.Sub(api, t1, a)
t1.Sub(api, t1, b)

return &E6{
B0: t0,
B0: a,
B1: t1,
B2: t2,
B2: b,
}
}

Expand Down
44 changes: 44 additions & 0 deletions std/algebra/native/fields_bls12377/e6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,50 @@ func TestMulFp6(t *testing.T) {
assert.CheckCircuit(&circuit, test.WithValidAssignment(&witness), test.WithCurves(ecc.BW6_761))
}

type e6Mul01By01 struct {
A0, A1 E2
B0, B1 E2
C E6 `gnark:",public"`
}

func (circuit *e6Mul01By01) Define(api frontend.API) error {
expected := *Mul01By01(api, circuit.A0, circuit.A1, circuit.B0, circuit.B1)
expected.AssertIsEqual(api, circuit.C)

return nil
}

func TestMul01By01(t *testing.T) {

// we test our new E3.Mul01By01 against E3.MulBy01
var circuit, witness e6Mul01By01
// witness values
var a, c bls12377.E6
var A0, A1, B0, B1 bls12377.E2
_, _ = A0.SetRandom()
_, _ = A1.SetRandom()
_, _ = B0.SetRandom()
_, _ = B1.SetRandom()
// build a 01 sparse E3 with,
// first two elements as A1 and A2,
// and the third as 0
a.B0 = A0
a.B1 = A1
a.B2.SetZero()
c.Set(&a)
c.MulBy01(&B0, &B1)

witness.A0.Assign(&A0)
witness.A1.Assign(&A1)
witness.B0.Assign(&B0)
witness.B1.Assign(&B1)
witness.C.Assign(&c)

assert := test.NewAssert(t)
assert.CheckCircuit(&circuit, test.WithValidAssignment(&witness), test.WithCurves(ecc.BW6_761))

}

type fp6MulByNonResidue struct {
A E6
C E6 `gnark:",public"`
Expand Down

0 comments on commit 2e80e8a

Please sign in to comment.