From 444f06f45672a9725707ad124ddf3b1d90b00cf8 Mon Sep 17 00:00:00 2001 From: Shramee Srivastav Date: Sat, 9 Mar 2024 13:54:22 +0530 Subject: [PATCH 1/8] bn254: faster e6 MulBy01 --- std/algebra/emulated/fields_bn254/e6.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/std/algebra/emulated/fields_bn254/e6.go b/std/algebra/emulated/fields_bn254/e6.go index 4330fa49fb..3ffb11dd0b 100644 --- a/std/algebra/emulated/fields_bn254/e6.go +++ b/std/algebra/emulated/fields_bn254/e6.go @@ -196,9 +196,10 @@ func (e Ext6) MulBy01(z *E6, c0, c1 *E2) *E6 { t0 = e.Ext2.Sub(t0, b) t0 = e.Ext2.MulByNonResidue(t0) t0 = e.Ext2.Add(t0, a) - tmp = e.Ext2.Add(&z.B0, &z.B2) - t2 := e.Ext2.Mul(c0, tmp) - t2 = e.Ext2.Sub(t2, a) + // for t2, schoolbook is faster than karatsuba + // c2 = a0b2 + a1b1 + a2b0, + // c2 = a2b0 + b ∵ b2 = 0, b = a1b1 + t2 := e.Ext2.Mul(&z.B2, c0) t2 = e.Ext2.Add(t2, b) t1 := e.Ext2.Add(c0, c1) tmp = e.Ext2.Add(&z.B0, &z.B1) From 859ee922379b40a20230a16e7ce73bb2eb92b7e8 Mon Sep 17 00:00:00 2001 From: Shramee Srivastav Date: Sat, 9 Mar 2024 14:00:20 +0530 Subject: [PATCH 2/8] bls12381: faster e6 MulBy01 --- std/algebra/emulated/fields_bls12381/e6.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/std/algebra/emulated/fields_bls12381/e6.go b/std/algebra/emulated/fields_bls12381/e6.go index 85e522eeb4..406c317c0a 100644 --- a/std/algebra/emulated/fields_bls12381/e6.go +++ b/std/algebra/emulated/fields_bls12381/e6.go @@ -216,9 +216,10 @@ func (e Ext6) MulBy01(z *E6, c0, c1 *E2) *E6 { t0 = e.Ext2.Sub(t0, b) t0 = e.Ext2.MulByNonResidue(t0) t0 = e.Ext2.Add(t0, a) - tmp = e.Ext2.Add(&z.B0, &z.B2) - t2 := e.Ext2.Mul(c0, tmp) - t2 = e.Ext2.Sub(t2, a) + // for t2, schoolbook is faster than karatsuba + // c2 = a0b2 + a1b1 + a2b0, + // c2 = a2b0 + b ∵ b2 = 0, b = a1b1 + t2 := e.Ext2.Mul(&z.B2, c0) t2 = e.Ext2.Add(t2, b) t1 := e.Ext2.Add(c0, c1) tmp = e.Ext2.Add(&z.B0, &z.B1) From c2a37fdfddcbfd275e61c622f064eb01841d9bc0 Mon Sep 17 00:00:00 2001 From: Shramee Srivastav Date: Sat, 9 Mar 2024 14:02:57 +0530 Subject: [PATCH 3/8] bw6761: faster e3 MulBy01 --- std/algebra/emulated/fields_bw6761/e3.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/std/algebra/emulated/fields_bw6761/e3.go b/std/algebra/emulated/fields_bw6761/e3.go index 63fc125a9b..df30d521d8 100644 --- a/std/algebra/emulated/fields_bw6761/e3.go +++ b/std/algebra/emulated/fields_bw6761/e3.go @@ -154,9 +154,10 @@ func (e Ext3) MulBy01(z *E3, c0, c1 *baseEl) *E3 { t0 = e.fp.MulConst(t0, big.NewInt(4)) t0 = e.fp.Add(t0, a) - tmp = e.fp.Add(&z.A0, &z.A2) - t2 := e.fp.Mul(c0, tmp) - t2 = e.fp.Sub(t2, a) + // for t2, schoolbook is faster than karatsuba + // c2 = a0b2 + a1b1 + a2b0, + // c2 = a2b0 + b ∵ b2 = 0, b = a1b1 + t2 := e.fp.Mul(&z.A2, c0) t2 = e.fp.Add(t2, b) t1 := e.fp.Add(c0, c1) From 4eff191475b0ff120bbebb1bd0a8e4059f68f622 Mon Sep 17 00:00:00 2001 From: Shramee Srivastav Date: Sat, 9 Mar 2024 14:20:19 +0530 Subject: [PATCH 4/8] bls12377: test e6 MulBy01 --- std/algebra/native/fields_bls12377/e6_test.go | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/std/algebra/native/fields_bls12377/e6_test.go b/std/algebra/native/fields_bls12377/e6_test.go index 1165fab114..60e849605e 100644 --- a/std/algebra/native/fields_bls12377/e6_test.go +++ b/std/algebra/native/fields_bls12377/e6_test.go @@ -122,6 +122,42 @@ func TestMulFp6(t *testing.T) { assert.CheckCircuit(&circuit, test.WithValidAssignment(&witness), test.WithCurves(ecc.BW6_761)) } +type fp6MulBy01 struct { + A E6 + C0, C1 E2 + C E6 `gnark:",public"` +} + +func (circuit *fp6MulBy01) Define(api frontend.API) error { + expected := circuit.A + expected.MulBy01(api, circuit.C0, circuit.C1) + expected.AssertIsEqual(api, circuit.C) + + return nil +} + +func TestMulFp6By01(t *testing.T) { + + var circuit, witness fp6MulBy01 + // witness values + var a, c bls12377.E6 + var C0, C1 bls12377.E2 + _, _ = a.SetRandom() + _, _ = C0.SetRandom() + _, _ = C1.SetRandom() + c.Set(&a) + c.MulBy01(&C0, &C1) + + witness.A.Assign(&a) + witness.C0.Assign(&C0) + witness.C1.Assign(&C1) + 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"` From 1818bce3b5e0ce5fdd6348695fa172f1e8bf98bb Mon Sep 17 00:00:00 2001 From: Shramee Srivastav Date: Sat, 9 Mar 2024 14:21:10 +0530 Subject: [PATCH 5/8] bls12377: faster e6 MulBy01 --- std/algebra/native/fields_bls12377/e6.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/std/algebra/native/fields_bls12377/e6.go b/std/algebra/native/fields_bls12377/e6.go index 5fba0cae66..61af7dbd96 100644 --- a/std/algebra/native/fields_bls12377/e6.go +++ b/std/algebra/native/fields_bls12377/e6.go @@ -261,9 +261,10 @@ func (e *E6) MulBy01(api frontend.API, c0, c1 E2) *E6 { t0.MulByNonResidue(api, t0) t0.Add(api, t0, a) - tmp.Add(api, e.B0, e.B2) - t2.Mul(api, c0, tmp) - t2.Sub(api, t2, a) + // for t2, schoolbook is faster than karatsuba + // c2 = a0b2 + a1b1 + a2b0, + // c2 = a2b0 + b ∵ b2 = 0, b = a1b1 + t2.Mul(api, e.B2, c0) t2.Add(api, t2, b) t1.Add(api, c0, c1) From 1ba12f62d68d353ae71834beeec96b8cb2bda4a7 Mon Sep 17 00:00:00 2001 From: Shramee Srivastav Date: Sat, 9 Mar 2024 14:30:27 +0530 Subject: [PATCH 6/8] bls24315: test e12 MulBy01 --- .../native/fields_bls24315/e12_test.go | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/std/algebra/native/fields_bls24315/e12_test.go b/std/algebra/native/fields_bls24315/e12_test.go index 6e41b82d77..8768000aeb 100644 --- a/std/algebra/native/fields_bls24315/e12_test.go +++ b/std/algebra/native/fields_bls24315/e12_test.go @@ -122,6 +122,42 @@ func TestMulFp12(t *testing.T) { assert.CheckCircuit(&circuit, test.WithValidAssignment(&witness), test.WithCurves(ecc.BW6_633)) } +type fp12MulBy01 struct { + A E12 + C0, C1 E4 + C E12 `gnark:",public"` +} + +func (circuit *fp12MulBy01) Define(api frontend.API) error { + expected := circuit.A + expected.MulBy01(api, circuit.C0, circuit.C1) + expected.AssertIsEqual(api, circuit.C) + + return nil +} + +func TestMulFp12By01(t *testing.T) { + + var circuit, witness fp12MulBy01 + // witness values + var a, c bls24315.E12 + var C0, C1 bls24315.E4 + _, _ = a.SetRandom() + _, _ = C0.SetRandom() + _, _ = C1.SetRandom() + c.Set(&a) + c.MulBy01(&C0, &C1) + + witness.A.Assign(&a) + witness.C0.Assign(&C0) + witness.C1.Assign(&C1) + witness.C.Assign(&c) + + assert := test.NewAssert(t) + assert.CheckCircuit(&circuit, test.WithValidAssignment(&witness), test.WithCurves(ecc.BW6_633)) + +} + type fp12MulByNonResidue struct { A E12 C E12 `gnark:",public"` From 39cfb2242dc8b7ff46eacc6d09aff8d3edb3b395 Mon Sep 17 00:00:00 2001 From: Shramee Srivastav Date: Sat, 9 Mar 2024 14:30:34 +0530 Subject: [PATCH 7/8] bls24315: faster e12 MulBy01 --- std/algebra/native/fields_bls24315/e12.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/std/algebra/native/fields_bls24315/e12.go b/std/algebra/native/fields_bls24315/e12.go index a65ac76118..2c9897c338 100644 --- a/std/algebra/native/fields_bls24315/e12.go +++ b/std/algebra/native/fields_bls24315/e12.go @@ -243,9 +243,10 @@ func (e *E12) MulBy01(api frontend.API, c0, c1 E4) *E12 { t0.MulByNonResidue(api, t0) t0.Add(api, t0, a) - tmp.Add(api, e.C0, e.C2) - t2.Mul(api, c0, tmp) - t2.Sub(api, t2, a) + // for t2, schoolbook is faster than karatsuba + // c2 = a0b2 + a1b1 + a2b0, + // c2 = a2b0 + b ∵ b2 = 0, b = a1b1 + t2.Mul(api, e.C2, c0) t2.Add(api, t2, b) t1.Add(api, c0, c1) From 09a3327d87e95d19e1f1c6eadf9ee4498add5a41 Mon Sep 17 00:00:00 2001 From: Shramee Srivastav Date: Sat, 9 Mar 2024 23:41:44 +0530 Subject: [PATCH 8/8] chore update stats --- internal/stats/latest.stats | Bin 2246 -> 2246 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/internal/stats/latest.stats b/internal/stats/latest.stats index 83c9387fa9278436954906eaa56dfe162ce7eaa5..dded511de49d9d926d101e5ac1e03effbfdd87c8 100644 GIT binary patch delta 155 zcmX>mcua6Y=HzVl_{kI4=1iW%w0v?ahasPZ7ULhr_`Qo5z+mzT_PvvLv#LllF#dZ4 z65#*~GyY}qZvqoQ?!-(ZUXM+Te;04h0827VmSkNtxte9cmcua6Y=43Z!Bi`IgjDHv3>0khZ$wjR5Ca+?jKlwPz+Q})5%O?x4Et%ZGq0eiP z#`rh)V+>T)Cbp!>>MRQ;w=*x8%+9PLDa82ikq}Uh11!(@mnHZOm|&PJ#6Dx