Skip to content

Commit 6635ad9

Browse files
SadPencilFiloSottile
authored andcommitted
bn256: fix String methods when g.p == nil
Previously, when g.p == nil, String() crashed. In other method like Add(), a point with g.p == nil is treated as an identity element. Besides, the following code is the only way to get an identity element outside the library: g := bn256.G1{}. In this situation, g.p == nil. For example, the following code will crash: package main import ( "fmt" "golang.org/x/crypto/bn256" ) func main() { g := bn256.G1{} fmt.Println(g.String()) } Change-Id: Ied6f7c8197e7b79b0913c000a9cf1cf68f8188a9 GitHub-Last-Rev: 2324648 GitHub-Pull-Request: #75 Reviewed-on: https://go-review.googlesource.com/c/163118 TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> Run-TryBot: Filippo Valsorda <[email protected]>
1 parent a573983 commit 6635ad9

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Diff for: bn256/bn256.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func RandomG1(r io.Reader) (*big.Int, *G1, error) {
5656
}
5757

5858
func (e *G1) String() string {
59+
if e.p == nil {
60+
return "bn256.G1" + newCurvePoint(nil).String()
61+
}
5962
return "bn256.G1" + e.p.String()
6063
}
6164

@@ -178,6 +181,9 @@ func RandomG2(r io.Reader) (*big.Int, *G2, error) {
178181
}
179182

180183
func (e *G2) String() string {
184+
if e.p == nil {
185+
return "bn256.G2" + newTwistPoint(nil).String()
186+
}
181187
return "bn256.G2" + e.p.String()
182188
}
183189

@@ -281,8 +287,11 @@ type GT struct {
281287
p *gfP12
282288
}
283289

284-
func (g *GT) String() string {
285-
return "bn256.GT" + g.p.String()
290+
func (e *GT) String() string {
291+
if e.p == nil {
292+
return "bn256.GT" + newGFp12(nil).String()
293+
}
294+
return "bn256.GT" + e.p.String()
286295
}
287296

288297
// ScalarMult sets e to a*k and then returns e.

0 commit comments

Comments
 (0)