Skip to content

Commit 1852350

Browse files
committed
Instantiate a Zr directly from uint64
Signed-off-by: Alessandro Sorniotti <aso@zurich.ibm.com>
1 parent 7fdf255 commit 1852350

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

driver/amcl/fp256bn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (b *fp256bnG1) String() string {
235235
}
236236

237237
func (e *fp256bnG1) Neg() {
238-
res := e.Mul(NewFp256bn().NewZrFromInt(-1))
238+
res := e.Mul(NewFp256bn().NewZrFromInt64(-1))
239239
e.ECP = res.(*fp256bnG1).ECP
240240
}
241241

driver/common/curve.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,14 @@ func (c *CurveBase) NewZrFromBytes(b []byte) driver.Zr {
6161
return res
6262
}
6363

64-
func (c *CurveBase) NewZrFromInt(i int64) driver.Zr {
64+
func (c *CurveBase) NewZrFromInt64(i int64) driver.Zr {
6565
return &BaseZr{Int: *big.NewInt(i), Modulus: c.Modulus}
6666
}
6767

68+
func (c *CurveBase) NewZrFromUint64(i uint64) driver.Zr {
69+
return &BaseZr{Int: *new(big.Int).SetUint64(i), Modulus: c.Modulus}
70+
}
71+
6872
func (c *CurveBase) NewRandomZr(rng io.Reader) driver.Zr {
6973
bi, err := rand.Int(rng, &c.Modulus)
7074
if err != nil {

driver/math.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ type Curve interface {
2929
NewG1() G1
3030
NewG2() G2
3131
NewZrFromBytes(b []byte) Zr
32-
NewZrFromInt(i int64) Zr
32+
NewZrFromInt64(i int64) Zr
33+
NewZrFromUint64(i uint64) Zr
3334
NewG1FromBytes(b []byte) G1
3435
NewG1FromCompressed(b []byte) G1
3536
NewG2FromBytes(b []byte) G2

math.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,11 @@ func (c *Curve) NewGtFromBytes(b []byte) (p *Gt, err error) {
480480
}
481481

482482
func (c *Curve) NewZrFromInt(i int64) *Zr {
483-
return &Zr{zr: c.c.NewZrFromInt(i), curveID: c.curveID}
483+
return &Zr{zr: c.c.NewZrFromInt64(i), curveID: c.curveID}
484+
}
485+
486+
func (c *Curve) NewZrFromUint64(i uint64) *Zr {
487+
return &Zr{zr: c.c.NewZrFromUint64(i), curveID: c.curveID}
484488
}
485489

486490
func (c *Curve) NewG2() *G2 {

math_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ func runZrTest(t *testing.T, c *Curve) {
124124
rng, err := c.Rand()
125125
assert.NoError(t, err)
126126

127+
maxint64 := c.NewZrFromInt(math.MaxInt64)
128+
maxuint64 := c.NewZrFromUint64(math.MaxUint64)
129+
assert.Equal(t, fmt.Sprintf("%x", math.MaxInt64), maxint64.String())
130+
assert.Equal(t, fmt.Sprintf("%x", uint64(math.MaxUint64)), maxuint64.String())
131+
127132
// serialising and deserialising negative numbers
128133
rr := c.NewRandomZr(rng)
129134
rr1 := rr.Copy()

0 commit comments

Comments
 (0)