Skip to content

Commit c532c4d

Browse files
committed
Reduce the number of allocations
Signed-off-by: Alessandro Sorniotti <aso@zurich.ibm.com>
1 parent 4a731ed commit c532c4d

File tree

8 files changed

+485
-429
lines changed

8 files changed

+485
-429
lines changed

driver/amcl/fp256bn.go

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ import (
2121
/*********************************************************************/
2222

2323
type fp256bnGt struct {
24-
*FP256BN.FP12
24+
FP256BN.FP12
2525
}
2626

2727
func (a *fp256bnGt) Exp(x driver.Zr) driver.Gt {
28-
return &fp256bnGt{a.FP12.Pow(bigToMiraclBIGCore(x.(*common.BaseZr).Int))}
28+
return &fp256bnGt{*a.FP12.Pow(bigToMiraclBIGCore(&x.(*common.BaseZr).Int))}
2929
}
3030

3131
func (a *fp256bnGt) Equals(b driver.Gt) bool {
32-
return a.FP12.Equals(b.(*fp256bnGt).FP12)
32+
return a.FP12.Equals(&b.(*fp256bnGt).FP12)
3333
}
3434

3535
func (a *fp256bnGt) IsUnity() bool {
@@ -41,7 +41,7 @@ func (a *fp256bnGt) Inverse() {
4141
}
4242

4343
func (a *fp256bnGt) Mul(b driver.Gt) {
44-
a.FP12.Mul(b.(*fp256bnGt).FP12)
44+
a.FP12.Mul(&b.(*fp256bnGt).FP12)
4545
}
4646

4747
func (b *fp256bnGt) ToString() string {
@@ -57,37 +57,37 @@ func (b *fp256bnGt) Bytes() []byte {
5757
/*********************************************************************/
5858

5959
func NewFp256bn() *Fp256bn {
60-
return &Fp256bn{&common.CurveBase{Modulus: &modulusBig}}
60+
return &Fp256bn{common.CurveBase{Modulus: modulusBig}}
6161
}
6262

6363
type Fp256bn struct {
64-
*common.CurveBase
64+
common.CurveBase
6565
}
6666

6767
func (*Fp256bn) Pairing(a driver.G2, b driver.G1) driver.Gt {
68-
return &fp256bnGt{FP256BN.Ate(a.(*fp256bnG2).ECP2, b.(*fp256bnG1).ECP)}
68+
return &fp256bnGt{*FP256BN.Ate(&a.(*fp256bnG2).ECP2, &b.(*fp256bnG1).ECP)}
6969
}
7070

7171
func (*Fp256bn) Pairing2(p2a, p2b driver.G2, p1a, p1b driver.G1) driver.Gt {
72-
return &fp256bnGt{FP256BN.Ate2(p2a.(*fp256bnG2).ECP2, p1a.(*fp256bnG1).ECP, p2b.(*fp256bnG2).ECP2, p1b.(*fp256bnG1).ECP)}
72+
return &fp256bnGt{*FP256BN.Ate2(&p2a.(*fp256bnG2).ECP2, &p1a.(*fp256bnG1).ECP, &p2b.(*fp256bnG2).ECP2, &p1b.(*fp256bnG1).ECP)}
7373
}
7474

7575
func (*Fp256bn) FExp(e driver.Gt) driver.Gt {
76-
return &fp256bnGt{FP256BN.Fexp(e.(*fp256bnGt).FP12)}
76+
return &fp256bnGt{*FP256BN.Fexp(&e.(*fp256bnGt).FP12)}
7777
}
7878

7979
func (*Fp256bn) GenG1() driver.G1 {
80-
return &fp256bnG1{FP256BN.NewECPbigs(FP256BN.NewBIGints(FP256BN.CURVE_Gx), FP256BN.NewBIGints(FP256BN.CURVE_Gy))}
80+
return &fp256bnG1{*FP256BN.NewECPbigs(FP256BN.NewBIGints(FP256BN.CURVE_Gx), FP256BN.NewBIGints(FP256BN.CURVE_Gy))}
8181
}
8282

8383
func (*Fp256bn) GenG2() driver.G2 {
84-
return &fp256bnG2{FP256BN.NewECP2fp2s(
84+
return &fp256bnG2{*FP256BN.NewECP2fp2s(
8585
FP256BN.NewFP2bigs(FP256BN.NewBIGints(FP256BN.CURVE_Pxa), FP256BN.NewBIGints(FP256BN.CURVE_Pxb)),
8686
FP256BN.NewFP2bigs(FP256BN.NewBIGints(FP256BN.CURVE_Pya), FP256BN.NewBIGints(FP256BN.CURVE_Pyb)))}
8787
}
8888

8989
func (p *Fp256bn) GenGt() driver.Gt {
90-
return &fp256bnGt{FP256BN.Fexp(FP256BN.Ate(p.GenG2().(*fp256bnG2).ECP2, p.GenG1().(*fp256bnG1).ECP))}
90+
return &fp256bnGt{*FP256BN.Fexp(FP256BN.Ate(&p.GenG2().(*fp256bnG2).ECP2, &p.GenG1().(*fp256bnG1).ECP))}
9191
}
9292

9393
func (p *Fp256bn) CoordinateByteSize() int {
@@ -115,11 +115,11 @@ func (p *Fp256bn) ScalarByteSize() int {
115115
}
116116

117117
func (p *Fp256bn) NewG1() driver.G1 {
118-
return &fp256bnG1{FP256BN.NewECP()}
118+
return &fp256bnG1{*FP256BN.NewECP()}
119119
}
120120

121121
func (p *Fp256bn) NewG2() driver.G2 {
122-
return &fp256bnG2{FP256BN.NewECP2()}
122+
return &fp256bnG2{*FP256BN.NewECP2()}
123123
}
124124

125125
func bigToMiraclBIGCore(bi *big.Int) *FP256BN.BIG {
@@ -137,65 +137,65 @@ func bigToMiraclBIGCore(bi *big.Int) *FP256BN.BIG {
137137
}
138138

139139
func (p *Fp256bn) NewG1FromBytes(b []byte) driver.G1 {
140-
return &fp256bnG1{FP256BN.ECP_fromBytes(b)}
140+
return &fp256bnG1{*FP256BN.ECP_fromBytes(b)}
141141
}
142142

143143
func (p *Fp256bn) NewG2FromBytes(b []byte) driver.G2 {
144-
return &fp256bnG2{FP256BN.ECP2_fromBytes(b)}
144+
return &fp256bnG2{*FP256BN.ECP2_fromBytes(b)}
145145
}
146146

147147
func (p *Fp256bn) NewG1FromCompressed(b []byte) driver.G1 {
148-
return &fp256bnG1{FP256BN.ECP_fromBytes(b)}
148+
return &fp256bnG1{*FP256BN.ECP_fromBytes(b)}
149149
}
150150

151151
func (p *Fp256bn) NewG2FromCompressed(b []byte) driver.G2 {
152-
return &fp256bnG2{FP256BN.ECP2_fromBytes(b)}
152+
return &fp256bnG2{*FP256BN.ECP2_fromBytes(b)}
153153
}
154154

155155
func (p *Fp256bn) NewGtFromBytes(b []byte) driver.Gt {
156-
return &fp256bnGt{FP256BN.FP12_fromBytes(b)}
156+
return &fp256bnGt{*FP256BN.FP12_fromBytes(b)}
157157
}
158158

159159
func (p *Fp256bn) HashToG1(data []byte) driver.G1 {
160-
return &fp256bnG1{FP256BN.Bls_hash(string(data))}
160+
return &fp256bnG1{*FP256BN.Bls_hash(string(data))}
161161
}
162162

163163
func (p *Fp256bn) HashToG1WithDomain(data, domain []byte) driver.G1 {
164164
mac := hmac.New(sha256.New, domain)
165165
mac.Write(data)
166-
return &fp256bnG1{FP256BN.Bls_hash(string(mac.Sum(nil)))}
166+
return &fp256bnG1{*FP256BN.Bls_hash(string(mac.Sum(nil)))}
167167
}
168168

169169
/*********************************************************************/
170170

171171
type fp256bnG1 struct {
172-
*FP256BN.ECP
172+
FP256BN.ECP
173173
}
174174

175175
func (e *fp256bnG1) Clone(a driver.G1) {
176-
e.ECP.Copy(a.(*fp256bnG1).ECP)
176+
e.ECP.Copy(&a.(*fp256bnG1).ECP)
177177
}
178178

179179
func (e *fp256bnG1) Copy() driver.G1 {
180180
c := FP256BN.NewECP()
181-
c.Copy(e.ECP)
182-
return &fp256bnG1{c}
181+
c.Copy(&e.ECP)
182+
return &fp256bnG1{*c}
183183
}
184184

185185
func (e *fp256bnG1) Add(a driver.G1) {
186-
e.ECP.Add(a.(*fp256bnG1).ECP)
186+
e.ECP.Add(&a.(*fp256bnG1).ECP)
187187
}
188188

189189
func (e *fp256bnG1) Mul(a driver.Zr) driver.G1 {
190-
return &fp256bnG1{FP256BN.G1mul(e.ECP, bigToMiraclBIGCore(a.(*common.BaseZr).Int))}
190+
return &fp256bnG1{*FP256BN.G1mul(&e.ECP, bigToMiraclBIGCore(&a.(*common.BaseZr).Int))}
191191
}
192192

193193
func (e *fp256bnG1) Mul2(ee driver.Zr, Q driver.G1, f driver.Zr) driver.G1 {
194-
return &fp256bnG1{e.ECP.Mul2(bigToMiraclBIGCore(ee.(*common.BaseZr).Int), Q.(*fp256bnG1).ECP, bigToMiraclBIGCore(f.(*common.BaseZr).Int))}
194+
return &fp256bnG1{*e.ECP.Mul2(bigToMiraclBIGCore(&ee.(*common.BaseZr).Int), &Q.(*fp256bnG1).ECP, bigToMiraclBIGCore(&f.(*common.BaseZr).Int))}
195195
}
196196

197197
func (e *fp256bnG1) Equals(a driver.G1) bool {
198-
return e.ECP.Equals(a.(*fp256bnG1).ECP)
198+
return e.ECP.Equals(&a.(*fp256bnG1).ECP)
199199
}
200200

201201
func (e *fp256bnG1) IsInfinity() bool {
@@ -215,7 +215,7 @@ func (e *fp256bnG1) Compressed() []byte {
215215
}
216216

217217
func (e *fp256bnG1) Sub(a driver.G1) {
218-
e.ECP.Sub(a.(*fp256bnG1).ECP)
218+
e.ECP.Sub(&a.(*fp256bnG1).ECP)
219219
}
220220

221221
var g1StrRegexp *regexp.Regexp = regexp.MustCompile(`^\(([0-9a-f]+),([0-9a-f]+)\)$`)
@@ -234,33 +234,33 @@ func (e *fp256bnG1) Neg() {
234234
/*********************************************************************/
235235

236236
type fp256bnG2 struct {
237-
*FP256BN.ECP2
237+
FP256BN.ECP2
238238
}
239239

240240
func (e *fp256bnG2) Equals(a driver.G2) bool {
241-
return e.ECP2.Equals(a.(*fp256bnG2).ECP2)
241+
return e.ECP2.Equals(&a.(*fp256bnG2).ECP2)
242242
}
243243

244244
func (e *fp256bnG2) Clone(a driver.G2) {
245-
e.ECP2.Copy(a.(*fp256bnG2).ECP2)
245+
e.ECP2.Copy(&a.(*fp256bnG2).ECP2)
246246
}
247247

248248
func (e *fp256bnG2) Copy() driver.G2 {
249249
c := FP256BN.NewECP2()
250-
c.Copy(e.ECP2)
251-
return &fp256bnG2{c}
250+
c.Copy(&e.ECP2)
251+
return &fp256bnG2{*c}
252252
}
253253

254254
func (e *fp256bnG2) Add(a driver.G2) {
255-
e.ECP2.Add(a.(*fp256bnG2).ECP2)
255+
e.ECP2.Add(&a.(*fp256bnG2).ECP2)
256256
}
257257

258258
func (e *fp256bnG2) Sub(a driver.G2) {
259-
e.ECP2.Sub(a.(*fp256bnG2).ECP2)
259+
e.ECP2.Sub(&a.(*fp256bnG2).ECP2)
260260
}
261261

262262
func (e *fp256bnG2) Mul(a driver.Zr) driver.G2 {
263-
return &fp256bnG2{e.ECP2.Mul(bigToMiraclBIGCore(a.(*common.BaseZr).Int))}
263+
return &fp256bnG2{*e.ECP2.Mul(bigToMiraclBIGCore(&a.(*common.BaseZr).Int))}
264264
}
265265

266266
func (e *fp256bnG2) Affine() {

driver/amcl/fp256bn_miracl.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ func init() {
2525
/*********************************************************************/
2626

2727
type fp256bnMiraclGt struct {
28-
*FP256BN.FP12
28+
FP256BN.FP12
2929
}
3030

3131
func (a *fp256bnMiraclGt) Exp(x driver.Zr) driver.Gt {
32-
return &fp256bnMiraclGt{a.FP12.Pow(bigToMiraclBIG(x.(*common.BaseZr).Int))}
32+
return &fp256bnMiraclGt{*a.FP12.Pow(bigToMiraclBIG(&x.(*common.BaseZr).Int))}
3333
}
3434

3535
func (a *fp256bnMiraclGt) Equals(b driver.Gt) bool {
36-
return a.FP12.Equals(b.(*fp256bnMiraclGt).FP12)
36+
return a.FP12.Equals(&b.(*fp256bnMiraclGt).FP12)
3737
}
3838

3939
func (a *fp256bnMiraclGt) IsUnity() bool {
@@ -45,7 +45,7 @@ func (a *fp256bnMiraclGt) Inverse() {
4545
}
4646

4747
func (a *fp256bnMiraclGt) Mul(b driver.Gt) {
48-
a.FP12.Mul(b.(*fp256bnMiraclGt).FP12)
48+
a.FP12.Mul(&b.(*fp256bnMiraclGt).FP12)
4949
}
5050

5151
func (b *fp256bnMiraclGt) ToString() string {
@@ -61,27 +61,27 @@ func (b *fp256bnMiraclGt) Bytes() []byte {
6161
/*********************************************************************/
6262

6363
func NewFp256Miraclbn() *Fp256Miraclbn {
64-
return &Fp256Miraclbn{&common.CurveBase{Modulus: &modulusBig}}
64+
return &Fp256Miraclbn{common.CurveBase{Modulus: modulusBig}}
6565
}
6666

6767
type Fp256Miraclbn struct {
68-
*common.CurveBase
68+
common.CurveBase
6969
}
7070

7171
func (*Fp256Miraclbn) Pairing(a driver.G2, b driver.G1) driver.Gt {
72-
return &fp256bnMiraclGt{FP256BN.Ate(a.(*fp256bnMiraclG2).ECP2, b.(*fp256bnMiraclG1).ECP)}
72+
return &fp256bnMiraclGt{*FP256BN.Ate(a.(*fp256bnMiraclG2).ECP2, &b.(*fp256bnMiraclG1).ECP)}
7373
}
7474

7575
func (*Fp256Miraclbn) Pairing2(p2a, p2b driver.G2, p1a, p1b driver.G1) driver.Gt {
76-
return &fp256bnMiraclGt{FP256BN.Ate2(p2a.(*fp256bnMiraclG2).ECP2, p1a.(*fp256bnMiraclG1).ECP, p2b.(*fp256bnMiraclG2).ECP2, p1b.(*fp256bnMiraclG1).ECP)}
76+
return &fp256bnMiraclGt{*FP256BN.Ate2(p2a.(*fp256bnMiraclG2).ECP2, &p1a.(*fp256bnMiraclG1).ECP, p2b.(*fp256bnMiraclG2).ECP2, &p1b.(*fp256bnMiraclG1).ECP)}
7777
}
7878

7979
func (*Fp256Miraclbn) FExp(e driver.Gt) driver.Gt {
80-
return &fp256bnMiraclGt{FP256BN.Fexp(e.(*fp256bnMiraclGt).FP12)}
80+
return &fp256bnMiraclGt{*FP256BN.Fexp(&e.(*fp256bnMiraclGt).FP12)}
8181
}
8282

8383
func (*Fp256Miraclbn) GenG1() driver.G1 {
84-
return &fp256bnMiraclG1{FP256BN.NewECPbigs(FP256BN.NewBIGints(FP256BN.CURVE_Gx), FP256BN.NewBIGints(FP256BN.CURVE_Gy))}
84+
return &fp256bnMiraclG1{*FP256BN.NewECPbigs(FP256BN.NewBIGints(FP256BN.CURVE_Gx), FP256BN.NewBIGints(FP256BN.CURVE_Gy))}
8585
}
8686

8787
func (*Fp256Miraclbn) GenG2() driver.G2 {
@@ -91,7 +91,7 @@ func (*Fp256Miraclbn) GenG2() driver.G2 {
9191
}
9292

9393
func (p *Fp256Miraclbn) GenGt() driver.Gt {
94-
return &fp256bnMiraclGt{FP256BN.Fexp(FP256BN.Ate(p.GenG2().(*fp256bnMiraclG2).ECP2, p.GenG1().(*fp256bnMiraclG1).ECP))}
94+
return &fp256bnMiraclGt{*FP256BN.Fexp(FP256BN.Ate(p.GenG2().(*fp256bnMiraclG2).ECP2, &p.GenG1().(*fp256bnMiraclG1).ECP))}
9595
}
9696

9797
func (p *Fp256Miraclbn) CoordinateByteSize() int {
@@ -119,7 +119,7 @@ func (p *Fp256Miraclbn) ScalarByteSize() int {
119119
}
120120

121121
func (p *Fp256Miraclbn) NewG1() driver.G1 {
122-
return &fp256bnMiraclG1{FP256BN.NewECP()}
122+
return &fp256bnMiraclG1{*FP256BN.NewECP()}
123123
}
124124

125125
func (p *Fp256Miraclbn) NewG2() driver.G2 {
@@ -141,63 +141,63 @@ func bigToMiraclBIG(bi *big.Int) *FP256BN.BIG {
141141
}
142142

143143
func (p *Fp256Miraclbn) NewG1FromBytes(b []byte) driver.G1 {
144-
return &fp256bnMiraclG1{FP256BN.ECP_fromBytes(b)}
144+
return &fp256bnMiraclG1{*FP256BN.ECP_fromBytes(b)}
145145
}
146146

147147
func (p *Fp256Miraclbn) NewG2FromBytes(b []byte) driver.G2 {
148148
return &fp256bnMiraclG2{FP256BN.ECP2_fromBytes(b)}
149149
}
150150

151151
func (p *Fp256Miraclbn) NewG1FromCompressed(b []byte) driver.G1 {
152-
return &fp256bnMiraclG1{FP256BN.ECP_fromBytes(b)}
152+
return &fp256bnMiraclG1{*FP256BN.ECP_fromBytes(b)}
153153
}
154154

155155
func (p *Fp256Miraclbn) NewG2FromCompressed(b []byte) driver.G2 {
156156
return &fp256bnMiraclG2{FP256BN.ECP2_fromBytes(b)}
157157
}
158158

159159
func (p *Fp256Miraclbn) NewGtFromBytes(b []byte) driver.Gt {
160-
return &fp256bnMiraclGt{FP256BN.FP12_fromBytes(b)}
160+
return &fp256bnMiraclGt{*FP256BN.FP12_fromBytes(b)}
161161
}
162162

163163
func (p *Fp256Miraclbn) HashToG1(data []byte) driver.G1 {
164-
return &fp256bnMiraclG1{bls_hash_to_point_miracl(data, []byte{})}
164+
return &fp256bnMiraclG1{*bls_hash_to_point_miracl(data, []byte{})}
165165
}
166166

167167
func (p *Fp256Miraclbn) HashToG1WithDomain(data, domain []byte) driver.G1 {
168-
return &fp256bnMiraclG1{bls_hash_to_point_miracl(data, domain)}
168+
return &fp256bnMiraclG1{*bls_hash_to_point_miracl(data, domain)}
169169
}
170170

171171
/*********************************************************************/
172172

173173
type fp256bnMiraclG1 struct {
174-
*FP256BN.ECP
174+
FP256BN.ECP
175175
}
176176

177177
func (e *fp256bnMiraclG1) Clone(a driver.G1) {
178-
e.ECP.Copy(a.(*fp256bnMiraclG1).ECP)
178+
e.ECP.Copy(&a.(*fp256bnMiraclG1).ECP)
179179
}
180180

181181
func (e *fp256bnMiraclG1) Copy() driver.G1 {
182182
c := FP256BN.NewECP()
183-
c.Copy(e.ECP)
184-
return &fp256bnMiraclG1{c}
183+
c.Copy(&e.ECP)
184+
return &fp256bnMiraclG1{*c}
185185
}
186186

187187
func (e *fp256bnMiraclG1) Add(a driver.G1) {
188-
e.ECP.Add(a.(*fp256bnMiraclG1).ECP)
188+
e.ECP.Add(&a.(*fp256bnMiraclG1).ECP)
189189
}
190190

191191
func (e *fp256bnMiraclG1) Mul(a driver.Zr) driver.G1 {
192-
return &fp256bnMiraclG1{FP256BN.G1mul(e.ECP, bigToMiraclBIG(a.(*common.BaseZr).Int))}
192+
return &fp256bnMiraclG1{*FP256BN.G1mul(&e.ECP, bigToMiraclBIG(&a.(*common.BaseZr).Int))}
193193
}
194194

195195
func (e *fp256bnMiraclG1) Mul2(ee driver.Zr, Q driver.G1, f driver.Zr) driver.G1 {
196-
return &fp256bnMiraclG1{e.ECP.Mul2(bigToMiraclBIG(ee.(*common.BaseZr).Int), Q.(*fp256bnMiraclG1).ECP, bigToMiraclBIG(f.(*common.BaseZr).Int))}
196+
return &fp256bnMiraclG1{*e.ECP.Mul2(bigToMiraclBIG(&ee.(*common.BaseZr).Int), &Q.(*fp256bnMiraclG1).ECP, bigToMiraclBIG(&f.(*common.BaseZr).Int))}
197197
}
198198

199199
func (e *fp256bnMiraclG1) Equals(a driver.G1) bool {
200-
return e.ECP.Equals(a.(*fp256bnMiraclG1).ECP)
200+
return e.ECP.Equals(&a.(*fp256bnMiraclG1).ECP)
201201
}
202202

203203
func (e *fp256bnMiraclG1) IsInfinity() bool {
@@ -217,7 +217,7 @@ func (e *fp256bnMiraclG1) Compressed() []byte {
217217
}
218218

219219
func (e *fp256bnMiraclG1) Sub(a driver.G1) {
220-
e.ECP.Sub(a.(*fp256bnMiraclG1).ECP)
220+
e.ECP.Sub(&a.(*fp256bnMiraclG1).ECP)
221221
}
222222

223223
func (b *fp256bnMiraclG1) String() string {
@@ -259,7 +259,7 @@ func (e *fp256bnMiraclG2) Sub(a driver.G2) {
259259
}
260260

261261
func (e *fp256bnMiraclG2) Mul(a driver.Zr) driver.G2 {
262-
return &fp256bnMiraclG2{e.ECP2.Mul(bigToMiraclBIG(a.(*common.BaseZr).Int))}
262+
return &fp256bnMiraclG2{e.ECP2.Mul(bigToMiraclBIG(&a.(*common.BaseZr).Int))}
263263
}
264264

265265
func (e *fp256bnMiraclG2) Affine() {

0 commit comments

Comments
 (0)