Skip to content

Commit 0d6db3e

Browse files
committed
test: induce Sign() error with malformed curve
1 parent 8afdb91 commit 0d6db3e

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

libevm/precompiles/p256verify/p256verify.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"crypto/ecdsa"
2323
"crypto/elliptic"
2424
"crypto/rand"
25-
"io"
2625
"math/big"
2726

2827
"github.com/ava-labs/libevm/params"
@@ -89,16 +88,11 @@ func (in *input) bigWord(index int) *big.Int {
8988
}
9089

9190
// Sign signs `hash` with the private key, using [rand.Reader] as the first
92-
// argument to [ecdsa.Sign]. It returns a signature payload constructed with
91+
// argument to [ecdsa.Sign] and assuming that the private key is for the
92+
// [elliptic.P256] curve. The returned signature payload is constructed with
9393
// [Pack], which can therefore be passed directly to the precompile.
9494
func Sign(priv *ecdsa.PrivateKey, hash [32]byte) ([]byte, error) {
95-
return signWithRandReader(rand.Reader, priv, hash)
96-
}
97-
98-
// signWithRandReader is abstracted for testing purposes only and MUST NOT be
99-
// used directly. Always use [Sign].
100-
func signWithRandReader(rand io.Reader, priv *ecdsa.PrivateKey, hash [32]byte) ([]byte, error) {
101-
r, s, err := ecdsa.Sign(rand, priv, hash[:])
95+
r, s, err := ecdsa.Sign(rand.Reader, priv, hash[:])
10296
if err != nil {
10397
return nil, err
10498
}

libevm/precompiles/p256verify/p256verify_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"encoding/asn1"
2525
"encoding/hex"
2626
"encoding/json"
27-
"errors"
2827
"fmt"
2928
"math/big"
3029
"slices"
@@ -258,21 +257,23 @@ func TestViaEVM(t *testing.T) {
258257
assert.Equal(t, []byte{31: 1}, got)
259258
}
260259

261-
type brokenReader struct {
262-
err error
260+
// A badCurve will cause [ecdsa.Sign] to return an error due to a zero
261+
// parameter.
262+
type badCurve struct {
263+
elliptic.Curve
263264
}
264265

265-
func (r brokenReader) Read([]byte) (int, error) {
266-
return 0, r.err
266+
func (badCurve) Params() *elliptic.CurveParams {
267+
return &elliptic.CurveParams{
268+
N: big.NewInt(0),
269+
}
267270
}
268271

269-
func TestSignPropagatesReaderError(t *testing.T) {
272+
func TestSignPropagatesError(t *testing.T) {
270273
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
271274
require.NoError(t, err, "ecdsa.GenerateKey()")
275+
priv.Curve = badCurve{}
272276

273-
r := brokenReader{
274-
err: errors.New("uh oh"),
275-
}
276-
_, err = signWithRandReader(r, priv, [32]byte{})
277-
require.Equal(t, r.err, err, "Propagate error from io.Reader entropy source")
277+
_, err = Sign(priv, [32]byte{})
278+
require.ErrorContains(t, err, "zero", "Sign([private key with zero-param curve])")
278279
}

0 commit comments

Comments
 (0)