Skip to content

Commit bd6879a

Browse files
authored
core/vm, crypto/bn256: switch over to cloudflare library (#16203)
* core/vm, crypto/bn256: switch over to cloudflare library * crypto/bn256: unmarshal constraint + start pure go impl * crypto/bn256: combo cloudflare and google lib * travis: drop 386 test job
1 parent 223fe3f commit bd6879a

33 files changed

+2799
-58
lines changed

core/vm/contracts.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -251,41 +251,22 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) {
251251
return common.LeftPadBytes(base.Exp(base, exp, mod).Bytes(), int(modLen)), nil
252252
}
253253

254-
var (
255-
// errNotOnCurve is returned if a point being unmarshalled as a bn256 elliptic
256-
// curve point is not on the curve.
257-
errNotOnCurve = errors.New("point not on elliptic curve")
258-
259-
// errInvalidCurvePoint is returned if a point being unmarshalled as a bn256
260-
// elliptic curve point is invalid.
261-
errInvalidCurvePoint = errors.New("invalid elliptic curve point")
262-
)
263-
264254
// newCurvePoint unmarshals a binary blob into a bn256 elliptic curve point,
265255
// returning it, or an error if the point is invalid.
266256
func newCurvePoint(blob []byte) (*bn256.G1, error) {
267-
p, onCurve := new(bn256.G1).Unmarshal(blob)
268-
if !onCurve {
269-
return nil, errNotOnCurve
270-
}
271-
gx, gy, _, _ := p.CurvePoints()
272-
if gx.Cmp(bn256.P) >= 0 || gy.Cmp(bn256.P) >= 0 {
273-
return nil, errInvalidCurvePoint
257+
p := new(bn256.G1)
258+
if _, err := p.Unmarshal(blob); err != nil {
259+
return nil, err
274260
}
275261
return p, nil
276262
}
277263

278264
// newTwistPoint unmarshals a binary blob into a bn256 elliptic curve point,
279265
// returning it, or an error if the point is invalid.
280266
func newTwistPoint(blob []byte) (*bn256.G2, error) {
281-
p, onCurve := new(bn256.G2).Unmarshal(blob)
282-
if !onCurve {
283-
return nil, errNotOnCurve
284-
}
285-
x2, y2, _, _ := p.CurvePoints()
286-
if x2.Real().Cmp(bn256.P) >= 0 || x2.Imag().Cmp(bn256.P) >= 0 ||
287-
y2.Real().Cmp(bn256.P) >= 0 || y2.Imag().Cmp(bn256.P) >= 0 {
288-
return nil, errInvalidCurvePoint
267+
p := new(bn256.G2)
268+
if _, err := p.Unmarshal(blob); err != nil {
269+
return nil, err
289270
}
290271
return p, nil
291272
}

crypto/bn256/bn256_amd64.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2018 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// +build amd64,!appengine,!gccgo
18+
19+
// Package bn256 implements the Optimal Ate pairing over a 256-bit Barreto-Naehrig curve.
20+
package bn256
21+
22+
import (
23+
"math/big"
24+
25+
"github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
26+
)
27+
28+
// G1 is an abstract cyclic group. The zero value is suitable for use as the
29+
// output of an operation, but cannot be used as an input.
30+
type G1 struct {
31+
bn256.G1
32+
}
33+
34+
// Add sets e to a+b and then returns e.
35+
func (e *G1) Add(a, b *G1) *G1 {
36+
e.G1.Add(&a.G1, &b.G1)
37+
return e
38+
}
39+
40+
// ScalarMult sets e to a*k and then returns e.
41+
func (e *G1) ScalarMult(a *G1, k *big.Int) *G1 {
42+
e.G1.ScalarMult(&a.G1, k)
43+
return e
44+
}
45+
46+
// G2 is an abstract cyclic group. The zero value is suitable for use as the
47+
// output of an operation, but cannot be used as an input.
48+
type G2 struct {
49+
bn256.G2
50+
}
51+
52+
// PairingCheck calculates the Optimal Ate pairing for a set of points.
53+
func PairingCheck(a []*G1, b []*G2) bool {
54+
as := make([]*bn256.G1, len(a))
55+
for i, p := range a {
56+
as[i] = &p.G1
57+
}
58+
bs := make([]*bn256.G2, len(b))
59+
for i, p := range b {
60+
bs[i] = &p.G2
61+
}
62+
return bn256.PairingCheck(as, bs)
63+
}

crypto/bn256/bn256_other.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2018 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// +build !amd64 appengine gccgo
18+
19+
// Package bn256 implements the Optimal Ate pairing over a 256-bit Barreto-Naehrig curve.
20+
package bn256
21+
22+
import (
23+
"math/big"
24+
25+
"github.com/ethereum/go-ethereum/crypto/bn256/google"
26+
)
27+
28+
// G1 is an abstract cyclic group. The zero value is suitable for use as the
29+
// output of an operation, but cannot be used as an input.
30+
type G1 struct {
31+
bn256.G1
32+
}
33+
34+
// Add sets e to a+b and then returns e.
35+
func (e *G1) Add(a, b *G1) *G1 {
36+
e.G1.Add(&a.G1, &b.G1)
37+
return e
38+
}
39+
40+
// ScalarMult sets e to a*k and then returns e.
41+
func (e *G1) ScalarMult(a *G1, k *big.Int) *G1 {
42+
e.G1.ScalarMult(&a.G1, k)
43+
return e
44+
}
45+
46+
// G2 is an abstract cyclic group. The zero value is suitable for use as the
47+
// output of an operation, but cannot be used as an input.
48+
type G2 struct {
49+
bn256.G2
50+
}
51+
52+
// PairingCheck calculates the Optimal Ate pairing for a set of points.
53+
func PairingCheck(a []*G1, b []*G2) bool {
54+
as := make([]*bn256.G1, len(a))
55+
for i, p := range a {
56+
as[i] = &p.G1
57+
}
58+
bs := make([]*bn256.G2, len(b))
59+
for i, p := range b {
60+
bs[i] = &p.G2
61+
}
62+
return bn256.PairingCheck(as, bs)
63+
}

0 commit comments

Comments
 (0)