From 9a327850c7648ef980ab16b630dc720b8ea8f64f Mon Sep 17 00:00:00 2001 From: Angelo De Caro Date: Sat, 31 Jan 2026 20:24:00 +0100 Subject: [PATCH 1/4] better mem Signed-off-by: Angelo De Caro --- driver/gurvy/bls12381/bls12-381.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/driver/gurvy/bls12381/bls12-381.go b/driver/gurvy/bls12381/bls12-381.go index 4b706cb..0e2156f 100644 --- a/driver/gurvy/bls12381/bls12-381.go +++ b/driver/gurvy/bls12381/bls12-381.go @@ -656,17 +656,24 @@ func (c *Curve) ModAdd2(a1, b1, c1, m driver.Zr) { } func (c *Curve) MultiScalarMul(a []driver.G1, b []driver.Zr) driver.G1 { - var result bls12381.G1Affine affinePoints := make([]bls12381.G1Affine, len(a)) - scalars := make([]fr.Element, len(b)) + scalars := make([]fr.Element, 0, len(b)) for i := range len(a) { affinePoints[i] = a[i].(*G1).G1Affine - scalars[i].SetBigInt(&b[i].(*Zr).Int) + + s := *frElements.Get() + s.SetBigInt(&b[i].(*Zr).Int) + scalars = append(scalars, s) } - _, _ = result.MultiExp(affinePoints, scalars, ecc.MultiExpConfig{}) - return &G1{G1Affine: result} + first := G1Jacs.Get() + defer G1Jacs.Put(first) + _, _ = first.MultiExp(affinePoints, scalars, ecc.MultiExpConfig{}) + + gc := &G1{} + gc.G1Affine.FromJacobian(first) + return gc } type BBSCurve struct { From 53287e4578a73e6bb773e75d980e19b7e5598a56 Mon Sep 17 00:00:00 2001 From: Angelo De Caro Date: Mon, 2 Feb 2026 09:27:21 +0100 Subject: [PATCH 2/4] mul opmitzation Signed-off-by: Angelo De Caro --- driver/gurvy/bls12381/bls12-381.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/driver/gurvy/bls12381/bls12-381.go b/driver/gurvy/bls12381/bls12-381.go index 0e2156f..ecca872 100644 --- a/driver/gurvy/bls12381/bls12-381.go +++ b/driver/gurvy/bls12381/bls12-381.go @@ -57,10 +57,18 @@ func (b *Zr) Minus(a driver.Zr) driver.Zr { return rv } -func (b *Zr) Mul(a driver.Zr) driver.Zr { +func (b *Zr) Mul(x driver.Zr) driver.Zr { + fr := frElements.Get() + defer frElements.Put(fr) + frX := frElements.Get() + defer frElements.Put(frX) + + fr.SetBigInt(&b.Int) + frX.SetBigInt(&x.(*Zr).Int) + fr.Mul(fr, frX) + rv := &Zr{Modulus: b.Modulus} - rv.Int.Mul(&b.Int, &a.(*Zr).Int) - rv.Int.Mod(&rv.Int, &b.Modulus) + fr.BigInt(&rv.Int) return rv } @@ -505,12 +513,16 @@ func (c *Curve) NewZrFromUint64(i uint64) driver.Zr { } func (c *Curve) NewRandomZr(rng io.Reader) driver.Zr { - bi, err := rand.Int(rng, &c.Modulus) + e := frElements.Get() + defer frElements.Put(e) + e, err := e.SetRandom() if err != nil { panic(err) } - return &Zr{Int: *bi, Modulus: c.Modulus} + res := &Zr{Modulus: c.Modulus} + e.BigInt(&res.Int) + return res } func (c *Curve) HashToZr(data []byte) driver.Zr { @@ -657,14 +669,11 @@ func (c *Curve) ModAdd2(a1, b1, c1, m driver.Zr) { func (c *Curve) MultiScalarMul(a []driver.G1, b []driver.Zr) driver.G1 { affinePoints := make([]bls12381.G1Affine, len(a)) - scalars := make([]fr.Element, 0, len(b)) + scalars := make([]fr.Element, len(b)) for i := range len(a) { affinePoints[i] = a[i].(*G1).G1Affine - - s := *frElements.Get() - s.SetBigInt(&b[i].(*Zr).Int) - scalars = append(scalars, s) + scalars[i].SetBigInt(&b[i].(*Zr).Int) } first := G1Jacs.Get() From 4e3d98dc4541f77ba7e82c6d8d3c83f85a6880c4 Mon Sep 17 00:00:00 2001 From: Angelo De Caro Date: Mon, 2 Feb 2026 12:33:32 +0100 Subject: [PATCH 3/4] expose curve's id Signed-off-by: Angelo De Caro --- math.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/math.go b/math.go index a7f7422..6732830 100644 --- a/math.go +++ b/math.go @@ -478,6 +478,10 @@ func NewCurve( } } +func (c *Curve) ID() CurveID { + return c.curveID +} + func (c *Curve) Rand() (io.Reader, error) { return c.c.Rand() } From 85177508bcbcf0eecd3784e96f018dc137495e96 Mon Sep 17 00:00:00 2001 From: Angelo De Caro Date: Mon, 2 Feb 2026 15:04:20 +0100 Subject: [PATCH 4/4] test update Signed-off-by: Angelo De Caro --- math_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/math_test.go b/math_test.go index da68647..317391c 100644 --- a/math_test.go +++ b/math_test.go @@ -45,10 +45,10 @@ func TestCurveId(t *testing.T) { func runCurveIdTest(t *testing.T, c *Curve, rng io.Reader) { r := c.NewRandomZr(rng) - assert.Equal(t, r.CurveID(), c.curveID) - assert.Equal(t, c.GenG1.Mul(r).CurveID(), c.curveID) - assert.Equal(t, c.GenG2.Mul(r).CurveID(), c.curveID) - assert.Equal(t, c.GenGt.Exp(r).CurveID(), c.curveID) + assert.Equal(t, r.CurveID(), c.ID()) + assert.Equal(t, c.GenG1.Mul(r).CurveID(), c.ID()) + assert.Equal(t, c.GenG2.Mul(r).CurveID(), c.ID()) + assert.Equal(t, c.GenGt.Exp(r).CurveID(), c.ID()) } var r *Zr @@ -257,9 +257,9 @@ var expectedModuli = []string{ } func runG1Test(t *testing.T, c *Curve) { - assert.Equal(t, expectedG1Gens[c.curveID], c.GenG1.String()) + assert.Equal(t, expectedG1Gens[c.ID()], c.GenG1.String()) - assert.Equal(t, expectedModuli[c.curveID], c.GroupOrder.String(), fmt.Sprintf("failed with curve %T", c.c)) + assert.Equal(t, expectedModuli[c.ID()], c.GroupOrder.String(), fmt.Sprintf("failed with curve %T", c.c)) g1copy := c.NewG1() g1copy.Clone(c.GenG1) @@ -362,7 +362,7 @@ func runG2Test(t *testing.T, c *Curve) { assert.Len(t, p.Bytes(), c.G2ByteSize) assert.Len(t, p.Compressed(), c.CompressedG2ByteSize) - if c.curveID != FP256BN_AMCL && c.curveID != FP256BN_AMCL_MIRACL { + if c.ID() != FP256BN_AMCL && c.ID() != FP256BN_AMCL_MIRACL { GS := c.HashToG2([]byte("Amazing Grace (how sweet the sound)")) assert.Len(t, GS.Bytes(), c.G2ByteSize)