Skip to content

Commit 1ec8741

Browse files
authored
feat: ethtest.UNSAFEDeterministicPrivateKey() (#244)
## Why this should be merged The ability to deterministically generate private keys (and corresponding EOAs) is useful in testing. ## How this works A `[]byte` seed is written to a Keccak state, which is rejection-sampled to find a scalar `<` the order of `S256` to use as the private key. ## How this was tested Unit test that demonstrates (a) determinism of generation; and (b) correct computation of the public key as proven by ECDSA recovery using the standard tx-sender functionality.
1 parent bb1e421 commit 1ec8741

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

libevm/ethtest/devkey.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package ethtest
18+
19+
import (
20+
"crypto/ecdsa"
21+
"crypto/elliptic"
22+
"math/big"
23+
"testing"
24+
25+
"github.com/stretchr/testify/require"
26+
27+
"github.com/ava-labs/libevm/crypto"
28+
)
29+
30+
// UNSAFEDeterministicPrivateKey returns a new [crypto.S256] private key,
31+
// deterministically generated from the `seed`.
32+
func UNSAFEDeterministicPrivateKey(tb testing.TB, seed []byte) *ecdsa.PrivateKey {
33+
tb.Helper()
34+
35+
curve := crypto.S256()
36+
d := privateKeyScalar(tb, seed, curve)
37+
38+
x, y := curve.ScalarBaseMult(d.Bytes())
39+
return &ecdsa.PrivateKey{
40+
D: d,
41+
PublicKey: ecdsa.PublicKey{
42+
X: x,
43+
Y: y,
44+
Curve: curve,
45+
},
46+
}
47+
}
48+
49+
func privateKeyScalar(tb testing.TB, seed []byte, curve elliptic.Curve) *big.Int {
50+
tb.Helper()
51+
52+
s := crypto.NewKeccakState()
53+
_, err := s.Write(seed)
54+
require.NoError(tb, err, "%T.Write()", s)
55+
56+
buf := make([]byte, 32)
57+
for {
58+
_, err := s.Read(buf)
59+
require.NoError(tb, err, "%T.Read()", s)
60+
d := new(big.Int).SetBytes(buf)
61+
62+
if isZero := d.Sign() == 0; !isZero && d.Cmp(curve.Params().N) == -1 {
63+
return d
64+
}
65+
}
66+
}

libevm/ethtest/devkey_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package ethtest
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
24+
"github.com/ava-labs/libevm/common"
25+
"github.com/ava-labs/libevm/core/types"
26+
"github.com/ava-labs/libevm/crypto"
27+
"github.com/ava-labs/libevm/params"
28+
)
29+
30+
func TestDeterministicPrivateKey(t *testing.T) {
31+
tests := []struct {
32+
seed []byte
33+
// Specific values are random, but we lock them in to ensure
34+
// deterministic generation.
35+
want common.Address
36+
}{
37+
{
38+
seed: nil,
39+
want: common.HexToAddress("0x9cce34F7aB185c7ABA1b7C8140d620B4BDA941d6"),
40+
},
41+
{
42+
seed: []byte{0},
43+
want: common.HexToAddress("0xa385D2E939787Af0B304512b2b6d56364F1722FA"),
44+
},
45+
{
46+
seed: []byte{1},
47+
want: common.HexToAddress("0x3Eea25034397B249a3eD8614BB4d0533e5b03594"),
48+
},
49+
}
50+
51+
signer := types.LatestSigner(params.MergedTestChainConfig)
52+
53+
for _, tt := range tests {
54+
t.Run("", func(t *testing.T) {
55+
key := UNSAFEDeterministicPrivateKey(t, tt.seed)
56+
57+
t.Run("address_from_pubkey", func(t *testing.T) {
58+
got := crypto.PubkeyToAddress(key.PublicKey)
59+
require.Equal(t, tt.want, got, "crypto.PubKeyToAddress(UNSAFEDeterministicPrivateKey())")
60+
})
61+
62+
t.Run("address_via_sender_recovery", func(t *testing.T) {
63+
got, err := types.Sender(
64+
signer,
65+
types.MustSignNewTx(key, signer, &types.LegacyTx{}),
66+
)
67+
require.NoError(t, err, "types.Sender(...)")
68+
require.Equal(t, tt.want, got, "types.Sender(..., types.MustSignNewTx(UNSAFEDeterministicPrivateKey(), ....))")
69+
})
70+
})
71+
}
72+
}

0 commit comments

Comments
 (0)