Skip to content

Commit 3e21adc

Browse files
committed
crypto/bn256: fix issues caused by Go 1.11
1 parent 2cdf6ee commit 3e21adc

18 files changed

Lines changed: 311 additions & 17 deletions

File tree

‎crypto/bn256/cloudflare/gfp_amd64.s‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ TEXT ·gfpMul(SB),0,$160-24
110110
MOVQ b+16(FP), SI
111111

112112
// Jump to a slightly different implementation if MULX isn't supported.
113-
CMPB runtime·support_bmi2(SB), $0
113+
CMPB ·hasBMI2(SB), $0
114114
JE nobmi2Mul
115115

116116
mulBMI2(0(DI),8(DI),16(DI),24(DI), 0(SI))

‎crypto/bn256/cloudflare/gfp_decl.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ package bn256
55
// This file contains forward declarations for the architecture-specific
66
// assembly implementations of these functions, provided that they exist.
77

8+
import (
9+
"golang.org/x/sys/cpu"
10+
)
11+
12+
//nolint:varcheck
13+
var hasBMI2 = cpu.X86.HasBMI2
14+
815
// go:noescape
916
func gfpNeg(c, a *gfP)
1017

‎crypto/bn256/google/bn256.go‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// Package bn256 implements a particular bilinear group at the 128-bit security level.
5+
// Package bn256 implements a particular bilinear group.
66
//
77
// Bilinear groups are the basis of many of the new cryptographic protocols
88
// that have been proposed over the past decade. They consist of a triplet of
@@ -14,6 +14,10 @@
1414
// Barreto-Naehrig curve as described in
1515
// http://cryptojedi.org/papers/dclxvi-20100714.pdf. Its output is compatible
1616
// with the implementation described in that paper.
17+
//
18+
// (This package previously claimed to operate at a 128-bit security level.
19+
// However, recent improvements in attacks mean that is no longer true. See
20+
// https://moderncrypto.org/mail-archive/curves/2016/000740.html.)
1721
package bn256
1822

1923
import (
@@ -50,8 +54,8 @@ func RandomG1(r io.Reader) (*big.Int, *G1, error) {
5054
return k, new(G1).ScalarBaseMult(k), nil
5155
}
5256

53-
func (g *G1) String() string {
54-
return "bn256.G1" + g.p.String()
57+
func (e *G1) String() string {
58+
return "bn256.G1" + e.p.String()
5559
}
5660

5761
// CurvePoints returns p's curve points in big integer
@@ -98,15 +102,19 @@ func (e *G1) Neg(a *G1) *G1 {
98102
}
99103

100104
// Marshal converts n to a byte slice.
101-
func (n *G1) Marshal() []byte {
102-
n.p.MakeAffine(nil)
103-
104-
xBytes := new(big.Int).Mod(n.p.x, P).Bytes()
105-
yBytes := new(big.Int).Mod(n.p.y, P).Bytes()
106-
105+
func (e *G1) Marshal() []byte {
107106
// Each value is a 256-bit number.
108107
const numBytes = 256 / 8
109108

109+
if e.p.IsInfinity() {
110+
return make([]byte, numBytes*2)
111+
}
112+
113+
e.p.MakeAffine(nil)
114+
115+
xBytes := new(big.Int).Mod(e.p.x, P).Bytes()
116+
yBytes := new(big.Int).Mod(e.p.y, P).Bytes()
117+
110118
ret := make([]byte, numBytes*2)
111119
copy(ret[1*numBytes-len(xBytes):], xBytes)
112120
copy(ret[2*numBytes-len(yBytes):], yBytes)
@@ -175,8 +183,8 @@ func RandomG2(r io.Reader) (*big.Int, *G2, error) {
175183
return k, new(G2).ScalarBaseMult(k), nil
176184
}
177185

178-
func (g *G2) String() string {
179-
return "bn256.G2" + g.p.String()
186+
func (e *G2) String() string {
187+
return "bn256.G2" + e.p.String()
180188
}
181189

182190
// CurvePoints returns the curve points of p which includes the real
@@ -216,16 +224,20 @@ func (e *G2) Add(a, b *G2) *G2 {
216224

217225
// Marshal converts n into a byte slice.
218226
func (n *G2) Marshal() []byte {
227+
// Each value is a 256-bit number.
228+
const numBytes = 256 / 8
229+
230+
if n.p.IsInfinity() {
231+
return make([]byte, numBytes*4)
232+
}
233+
219234
n.p.MakeAffine(nil)
220235

221236
xxBytes := new(big.Int).Mod(n.p.x.x, P).Bytes()
222237
xyBytes := new(big.Int).Mod(n.p.x.y, P).Bytes()
223238
yxBytes := new(big.Int).Mod(n.p.y.x, P).Bytes()
224239
yyBytes := new(big.Int).Mod(n.p.y.y, P).Bytes()
225240

226-
// Each value is a 256-bit number.
227-
const numBytes = 256 / 8
228-
229241
ret := make([]byte, numBytes*4)
230242
copy(ret[1*numBytes-len(xxBytes):], xxBytes)
231243
copy(ret[2*numBytes-len(xyBytes):], xyBytes)

‎crypto/bn256/google/curve.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,19 @@ func (c *curvePoint) Mul(a *curvePoint, scalar *big.Int, pool *bnPool) *curvePoi
245245
return c
246246
}
247247

248+
// MakeAffine converts c to affine form and returns c. If c is ∞, then it sets
249+
// c to 0 : 1 : 0.
248250
func (c *curvePoint) MakeAffine(pool *bnPool) *curvePoint {
249251
if words := c.z.Bits(); len(words) == 1 && words[0] == 1 {
250252
return c
251253
}
252-
254+
if c.IsInfinity() {
255+
c.x.SetInt64(0)
256+
c.y.SetInt64(1)
257+
c.z.SetInt64(0)
258+
c.t.SetInt64(0)
259+
return c
260+
}
253261
zInv := pool.Get().ModInverse(c.z, P)
254262
t := pool.Get().Mul(c.y, zInv)
255263
t.Mod(t, P)

‎crypto/bn256/google/twist.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,19 @@ func (c *twistPoint) Mul(a *twistPoint, scalar *big.Int, pool *bnPool) *twistPoi
225225
return c
226226
}
227227

228+
// MakeAffine converts c to affine form and returns c. If c is ∞, then it sets
229+
// c to 0 : 1 : 0.
228230
func (c *twistPoint) MakeAffine(pool *bnPool) *twistPoint {
229231
if c.z.IsOne() {
230232
return c
231233
}
232-
234+
if c.IsInfinity() {
235+
c.x.SetZero()
236+
c.y.SetOne()
237+
c.z.SetZero()
238+
c.t.SetZero()
239+
return c
240+
}
233241
zInv := newGFp2(pool).Invert(c.z, pool)
234242
t := newGFp2(pool).Mul(c.y, zInv, pool)
235243
zInv2 := newGFp2(pool).Square(zInv, pool)

‎vendor/golang.org/x/sys/cpu/cpu.go‎

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/golang.org/x/sys/cpu/cpu_arm.go‎

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/golang.org/x/sys/cpu/cpu_arm64.go‎

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/golang.org/x/sys/cpu/cpu_gc_x86.go‎

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/golang.org/x/sys/cpu/cpu_gccgo.c‎

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)