Skip to content

Commit 6fa25ca

Browse files
committed
refactor!: remove Sign()
1 parent cfe51e8 commit 6fa25ca

File tree

2 files changed

+8
-36
lines changed

2 files changed

+8
-36
lines changed

libevm/precompiles/p256verify/p256verify.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package p256verify
2121
import (
2222
"crypto/ecdsa"
2323
"crypto/elliptic"
24-
"crypto/rand"
2524
"math/big"
2625

2726
"github.com/ava-labs/libevm/params"
@@ -99,18 +98,6 @@ func (in *input) bigWord(i index) *big.Int {
9998
return new(big.Int).SetBytes(in.word(i))
10099
}
101100

102-
// Sign signs `hash` with the private key, using [rand.Reader] as the first
103-
// argument to [ecdsa.Sign] and assuming that the private key is for the
104-
// [elliptic.P256] curve. The returned signature payload is constructed with
105-
// [Pack], which can therefore be passed directly to the precompile.
106-
func Sign(priv *ecdsa.PrivateKey, hash [32]byte) ([]byte, error) {
107-
r, s, err := ecdsa.Sign(rand.Reader, priv, hash[:])
108-
if err != nil {
109-
return nil, err
110-
}
111-
return Pack(hash, r, s, &priv.PublicKey), nil
112-
}
113-
114101
// Pack packs the arguments into a byte slice compatible with [Precompile.Run].
115102
// It does NOT perform any validation on its inputs and therefore may panic if,
116103
// for example, a [big.Int] with >256 bits is received. Keys and signatures

libevm/precompiles/p256verify/p256verify_test.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ type testCase struct {
6161
wantSuccess bool
6262
}
6363

64+
func signAndPack(tb testing.TB, priv *ecdsa.PrivateKey, hash [32]byte) []byte {
65+
tb.Helper()
66+
r, s, err := ecdsa.Sign(rand.Reader, priv, hash[:])
67+
require.NoError(tb, err, "ecdsa.Sign()")
68+
return Pack(hash, r, s, &priv.PublicKey)
69+
}
70+
6471
func TestPrecompile(t *testing.T) {
6572
assert.Equal(t, params.P256VerifyGas, Precompile{}.RequiredGas(nil), "RequiredGas()")
6673

@@ -100,8 +107,7 @@ func TestPrecompile(t *testing.T) {
100107
_, err := rand.Read(toSign[:])
101108
require.NoErrorf(t, err, "crypto/rand.Read(%T)", toSign)
102109

103-
in, err := Sign(priv, toSign)
104-
require.NoErrorf(t, err, "Sign([P256 key], %#x)", toSign)
110+
in := signAndPack(t, priv, toSign)
105111
tests = append(tests, testCase{
106112
name: "fuzz_valid",
107113
in: in,
@@ -256,24 +262,3 @@ func TestViaEVM(t *testing.T) {
256262
require.NoError(t, err)
257263
assert.Equal(t, []byte{31: 1}, got)
258264
}
259-
260-
// A badCurve will cause [ecdsa.Sign] to return an error due to a zero
261-
// parameter.
262-
type badCurve struct {
263-
elliptic.Curve
264-
}
265-
266-
func (badCurve) Params() *elliptic.CurveParams {
267-
return &elliptic.CurveParams{
268-
N: big.NewInt(0),
269-
}
270-
}
271-
272-
func TestSignPropagatesError(t *testing.T) {
273-
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
274-
require.NoError(t, err, "ecdsa.GenerateKey()")
275-
priv.Curve = badCurve{}
276-
277-
_, err = Sign(priv, [32]byte{})
278-
require.ErrorContains(t, err, "zero", "Sign([private key with zero-param curve])")
279-
}

0 commit comments

Comments
 (0)