Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 24 additions & 8 deletions driver/gurvy/bls12381/bls12-381.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
adecaro marked this conversation as resolved.
fr.BigInt(&rv.Int)
return rv
}

Expand Down Expand Up @@ -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}
Comment thread
adecaro marked this conversation as resolved.
res := &Zr{Modulus: c.Modulus}
e.BigInt(&res.Int)
return res
}

func (c *Curve) HashToZr(data []byte) driver.Zr {
Expand Down Expand Up @@ -656,7 +668,6 @@ 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))

Expand All @@ -665,8 +676,13 @@ func (c *Curve) MultiScalarMul(a []driver.G1, b []driver.Zr) driver.G1 {
scalars[i].SetBigInt(&b[i].(*Zr).Int)
}

_, _ = 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 {
Expand Down
4 changes: 4 additions & 0 deletions math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
14 changes: 7 additions & 7 deletions math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down