Skip to content

Commit 0703ef6

Browse files
crypto/secp256k1: fix undefined behavior in BitCurve.Add (#22621)
This commit changes the behavior of BitCurve.Add to be more inline with btcd. It fixes two different bugs: 1) When adding a point at infinity to another point, the other point should be returned. While this is undefined behavior, it is better to be more inline with the go standard library. Thus (0,0) + (a, b) = (a,b) 2) Adding the same point to itself produced the point at infinity. This is incorrect, now doubleJacobian is used to correctly calculate it. Thus (a,b) + (a,b) == 2* (a,b) and not (0,0) anymore. The change also adds a differential fuzzer for Add, testing it against btcd. Co-authored-by: Felix Lange <[email protected]>
1 parent d836ad1 commit 0703ef6

File tree

8 files changed

+143
-42
lines changed

8 files changed

+143
-42
lines changed

crypto/secp256k1/curve.go

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,8 @@ package secp256k1
3535
import (
3636
"crypto/elliptic"
3737
"math/big"
38-
"unsafe"
3938
)
4039

41-
/*
42-
#include "libsecp256k1/include/secp256k1.h"
43-
extern int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, const unsigned char *point, const unsigned char *scalar);
44-
*/
45-
import "C"
46-
4740
const (
4841
// number of bits in a big.Word
4942
wordBits = 32 << (uint64(^big.Word(0)) >> 63)
@@ -133,7 +126,18 @@ func (BitCurve *BitCurve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.
133126

134127
// Add returns the sum of (x1,y1) and (x2,y2)
135128
func (BitCurve *BitCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
129+
// If one point is at infinity, return the other point.
130+
// Adding the point at infinity to any point will preserve the other point.
131+
if x1.Sign() == 0 && y1.Sign() == 0 {
132+
return x2, y2
133+
}
134+
if x2.Sign() == 0 && y2.Sign() == 0 {
135+
return x1, y1
136+
}
136137
z := new(big.Int).SetInt64(1)
138+
if x1.Cmp(x2) == 0 && y1.Cmp(y2) == 0 {
139+
return BitCurve.affineFromJacobian(BitCurve.doubleJacobian(x1, y1, z))
140+
}
137141
return BitCurve.affineFromJacobian(BitCurve.addJacobian(x1, y1, z, x2, y2, z))
138142
}
139143

@@ -242,41 +246,6 @@ func (BitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int,
242246
return x3, y3, z3
243247
}
244248

245-
func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) {
246-
// Ensure scalar is exactly 32 bytes. We pad always, even if
247-
// scalar is 32 bytes long, to avoid a timing side channel.
248-
if len(scalar) > 32 {
249-
panic("can't handle scalars > 256 bits")
250-
}
251-
// NOTE: potential timing issue
252-
padded := make([]byte, 32)
253-
copy(padded[32-len(scalar):], scalar)
254-
scalar = padded
255-
256-
// Do the multiplication in C, updating point.
257-
point := make([]byte, 64)
258-
readBits(Bx, point[:32])
259-
readBits(By, point[32:])
260-
261-
pointPtr := (*C.uchar)(unsafe.Pointer(&point[0]))
262-
scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0]))
263-
res := C.secp256k1_ext_scalar_mul(context, pointPtr, scalarPtr)
264-
265-
// Unpack the result and clear temporaries.
266-
x := new(big.Int).SetBytes(point[:32])
267-
y := new(big.Int).SetBytes(point[32:])
268-
for i := range point {
269-
point[i] = 0
270-
}
271-
for i := range padded {
272-
scalar[i] = 0
273-
}
274-
if res != 1 {
275-
return nil, nil
276-
}
277-
return x, y
278-
}
279-
280249
// ScalarBaseMult returns k*G, where G is the base point of the group and k is
281250
// an integer in big-endian form.
282251
func (BitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {

crypto/secp256k1/panic_cb.go

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

5+
// +build !gofuzz cgo
6+
57
package secp256k1
68

79
import "C"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2015 Jeffrey Wilcke, Felix Lange, Gustav Simonsson. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be found in
3+
// the LICENSE file.
4+
5+
// +build !gofuzz cgo
6+
7+
package secp256k1
8+
9+
import (
10+
"math/big"
11+
"unsafe"
12+
)
13+
14+
/*
15+
16+
#include "libsecp256k1/include/secp256k1.h"
17+
18+
extern int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, const unsigned char *point, const unsigned char *scalar);
19+
20+
*/
21+
import "C"
22+
23+
func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) {
24+
// Ensure scalar is exactly 32 bytes. We pad always, even if
25+
// scalar is 32 bytes long, to avoid a timing side channel.
26+
if len(scalar) > 32 {
27+
panic("can't handle scalars > 256 bits")
28+
}
29+
// NOTE: potential timing issue
30+
padded := make([]byte, 32)
31+
copy(padded[32-len(scalar):], scalar)
32+
scalar = padded
33+
34+
// Do the multiplication in C, updating point.
35+
point := make([]byte, 64)
36+
readBits(Bx, point[:32])
37+
readBits(By, point[32:])
38+
39+
pointPtr := (*C.uchar)(unsafe.Pointer(&point[0]))
40+
scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0]))
41+
res := C.secp256k1_ext_scalar_mul(context, pointPtr, scalarPtr)
42+
43+
// Unpack the result and clear temporaries.
44+
x := new(big.Int).SetBytes(point[:32])
45+
y := new(big.Int).SetBytes(point[32:])
46+
for i := range point {
47+
point[i] = 0
48+
}
49+
for i := range padded {
50+
scalar[i] = 0
51+
}
52+
if res != 1 {
53+
return nil, nil
54+
}
55+
return x, y
56+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2015 Jeffrey Wilcke, Felix Lange, Gustav Simonsson. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be found in
3+
// the LICENSE file.
4+
5+
// +build gofuzz !cgo
6+
7+
package secp256k1
8+
9+
import "math/big"
10+
11+
func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) {
12+
panic("ScalarMult is not available when secp256k1 is built without cgo")
13+
}

crypto/secp256k1/secp256.go

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

5+
// +build !gofuzz cgo
6+
57
// Package secp256k1 wraps the bitcoin secp256k1 C library.
68
package secp256k1
79

oss-fuzz.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ compile_fuzzer tests/fuzzers/stacktrie Fuzz fuzzStackTrie
102102
compile_fuzzer tests/fuzzers/difficulty Fuzz fuzzDifficulty
103103
compile_fuzzer tests/fuzzers/abi Fuzz fuzzAbi
104104
compile_fuzzer tests/fuzzers/les Fuzz fuzzLes
105+
compile_fuzzer tests/fuzzers/secp265k1 Fuzz fuzzSecp256k1
105106
compile_fuzzer tests/fuzzers/vflux FuzzClientPool fuzzClientPool
106107

107108
compile_fuzzer tests/fuzzers/bls12381 FuzzG1Add fuzz_g1_add
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2021 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 +gofuzz
18+
19+
package secp256k1
20+
21+
import (
22+
"fmt"
23+
24+
"github.com/btcsuite/btcd/btcec"
25+
"github.com/ethereum/go-ethereum/crypto/secp256k1"
26+
fuzz "github.com/google/gofuzz"
27+
)
28+
29+
func Fuzz(input []byte) int {
30+
var (
31+
fuzzer = fuzz.NewFromGoFuzz(input)
32+
curveA = secp256k1.S256()
33+
curveB = btcec.S256()
34+
dataP1 []byte
35+
dataP2 []byte
36+
)
37+
// first point
38+
fuzzer.Fuzz(&dataP1)
39+
x1, y1 := curveB.ScalarBaseMult(dataP1)
40+
// second point
41+
fuzzer.Fuzz(&dataP2)
42+
x2, y2 := curveB.ScalarBaseMult(dataP2)
43+
resAX, resAY := curveA.Add(x1, y1, x2, y2)
44+
resBX, resBY := curveB.Add(x1, y1, x2, y2)
45+
if resAX.Cmp(resBX) != 0 || resAY.Cmp(resBY) != 0 {
46+
fmt.Printf("%s %s %s %s\n", x1, y1, x2, y2)
47+
panic(fmt.Sprintf("Addition failed: geth: %s %s btcd: %s %s", resAX, resAY, resBX, resBY))
48+
}
49+
return 0
50+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package secp256k1
2+
3+
import "testing"
4+
5+
func TestFuzzer(t *testing.T) {
6+
test := "00000000N0000000/R00000000000000000U0000S0000000mkhP000000000000000U"
7+
Fuzz([]byte(test))
8+
}

0 commit comments

Comments
 (0)