Skip to content

Commit 40a7714

Browse files
committed
test: signing propagates errors
1 parent 44d7e43 commit 40a7714

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

libevm/precompiles/p256verify/p256verify.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"crypto/ecdsa"
2323
"crypto/elliptic"
2424
"crypto/rand"
25+
"io"
2526
"math/big"
2627
)
2728

@@ -87,7 +88,13 @@ func (in *input) bigWord(index int) *big.Int {
8788
// argument to [ecdsa.Sign]. It returns a signature payload constructed with
8889
// [Pack], which can therefore be passed directly to the precompile.
8990
func Sign(priv *ecdsa.PrivateKey, hash [32]byte) ([]byte, error) {
90-
r, s, err := ecdsa.Sign(rand.Reader, priv, hash[:])
91+
return signWithRandReader(rand.Reader, priv, hash)
92+
}
93+
94+
// signWithRandReader is abstracted for testing purposes only and MUST NOT be
95+
// used directly. Always use [Sign].
96+
func signWithRandReader(rand io.Reader, priv *ecdsa.PrivateKey, hash [32]byte) ([]byte, error) {
97+
r, s, err := ecdsa.Sign(rand, priv, hash[:])
9198
if err != nil {
9299
return nil, err
93100
}

libevm/precompiles/p256verify/p256verify_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"encoding/asn1"
2525
"encoding/hex"
2626
"encoding/json"
27+
"errors"
2728
"fmt"
2829
"math/big"
2930
"slices"
@@ -44,6 +45,8 @@ import (
4445
_ "embed"
4546
)
4647

48+
var _ vm.PrecompiledContract = Precompile{}
49+
4750
// ulerdoganTestCase is the test case from
4851
// https://github.com/ulerdogan/go-ethereum/blob/cec0b058115282168c5afc5197de3f6b5479dc4a/core/vm/testdata/precompiles/p256Verify.json,
4952
// copied under LGPL. See the respective commit for copyright and license
@@ -254,3 +257,22 @@ func TestViaEVM(t *testing.T) {
254257
require.NoError(t, err)
255258
assert.Equal(t, []byte{31: 1}, got)
256259
}
260+
261+
type brokenReader struct {
262+
err error
263+
}
264+
265+
func (r brokenReader) Read([]byte) (int, error) {
266+
return 0, r.err
267+
}
268+
269+
func TestSignPropagatesReaderError(t *testing.T) {
270+
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
271+
require.NoError(t, err, "ecdsa.GenerateKey()")
272+
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")
278+
}

0 commit comments

Comments
 (0)