Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fjord] Secp256r1 curve support (RIP-7212) #176

Merged
merged 11 commits into from
May 27, 2024
7 changes: 7 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ var (
Name: "override.ecotone",
Usage: "Manually specify the Optimism Ecotone fork time, overriding the bundled setting",
}
OverrideOptimismFjordFlag = flags.BigFlag{
Name: "override.fjord",
Usage: "Manually specify the Optimism Ecotone fork time, overriding the bundled setting",
}
// Ethash settings
EthashCachesInMemoryFlag = cli.IntFlag{
Name: "ethash.cachesinmem",
Expand Down Expand Up @@ -2006,6 +2010,9 @@ func SetEthConfig(ctx *cli.Context, nodeConfig *nodecfg.Config, cfg *ethconfig.C
"cancun", overrideCancunTime.String(), "ecotone", overrideOptimismEcotoneTime.String())
}
}
if ctx.IsSet(OverrideOptimismFjordFlag.Name) {
cfg.OverrideOptimismFjordTime = flags.GlobalBig(ctx, OverrideOptimismFjordFlag.Name)
}
if ctx.IsSet(InternalConsensusFlag.Name) && clparams.EmbeddedSupported(cfg.NetworkID) {
cfg.InternalCL = ctx.Bool(InternalConsensusFlag.Name)
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/misc/create2deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestEnsureCreate2Deployer(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := chain.Config{
ChainID: big.NewInt(params.OPGoerliChainID),
ChainID: big.NewInt(params.OPMainnetChainID),
Optimism: &chain.OptimismConfig{},
CanyonTime: big.NewInt(int64(canyonTime)),
}
Expand Down
6 changes: 3 additions & 3 deletions core/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestGenesisBlockHashes(t *testing.T) {
t.Fatal(err)
}
defer tx.Rollback()
_, block, err := core.WriteGenesisBlock(tx, genesis, nil, nil, nil, nil, nil, "", logger)
_, block, err := core.WriteGenesisBlock(tx, genesis, nil, nil, nil, nil, nil, nil, "", logger)
require.NoError(t, err)
expect := params.GenesisHashByChainName(network)
require.NotNil(t, expect, network)
Expand Down Expand Up @@ -83,13 +83,13 @@ func TestCommitGenesisIdempotency(t *testing.T) {
defer tx.Rollback()

genesis := core.GenesisBlockByChainName(networkname.MainnetChainName)
_, _, err = core.WriteGenesisBlock(tx, genesis, nil, nil, nil, nil, nil, "", logger)
_, _, err = core.WriteGenesisBlock(tx, genesis, nil, nil, nil, nil, nil, nil, "", logger)
require.NoError(t, err)
seq, err := tx.ReadSequence(kv.EthTx)
require.NoError(t, err)
require.Equal(t, uint64(2), seq)

_, _, err = core.WriteGenesisBlock(tx, genesis, nil, nil, nil, nil, nil, "", logger)
_, _, err = core.WriteGenesisBlock(tx, genesis, nil, nil, nil, nil, nil, nil, "", logger)
require.NoError(t, err)
seq, err = tx.ReadSequence(kv.EthTx)
require.NoError(t, err)
Expand Down
13 changes: 7 additions & 6 deletions core/genesis_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ import (
//
// The returned chain configuration is never nil.
func CommitGenesisBlock(db kv.RwDB, genesis *types.Genesis, tmpDir string, logger log.Logger) (*chain.Config, *types.Block, error) {
return CommitGenesisBlockWithOverride(db, genesis, nil, nil, nil, nil, nil, tmpDir, logger)
return CommitGenesisBlockWithOverride(db, genesis, nil, nil, nil, nil, nil, nil, tmpDir, logger)
}

func CommitGenesisBlockWithOverride(db kv.RwDB, genesis *types.Genesis, overrideCancunTime, overrideShanghaiTime, overrideOptimismCanyonTime, overrideOptimismEcotoneTime, overridePragueTime *big.Int, tmpDir string, logger log.Logger) (*chain.Config, *types.Block, error) {
func CommitGenesisBlockWithOverride(db kv.RwDB, genesis *types.Genesis, overrideCancunTime, overrideShanghaiTime, overrideOptimismCanyonTime, overrideOptimismEcotoneTime, overrideOptimismFjordTime, overridePragueTime *big.Int, tmpDir string, logger log.Logger) (*chain.Config, *types.Block, error) {
tx, err := db.BeginRw(context.Background())
if err != nil {
return nil, nil, err
}
defer tx.Rollback()
c, b, err := WriteGenesisBlock(tx, genesis, overrideCancunTime, overrideShanghaiTime, overrideOptimismCanyonTime, overrideOptimismEcotoneTime, overridePragueTime, tmpDir, logger)
c, b, err := WriteGenesisBlock(tx, genesis, overrideCancunTime, overrideShanghaiTime, overrideOptimismCanyonTime, overrideOptimismEcotoneTime, overrideOptimismFjordTime, overridePragueTime, tmpDir, logger)
if err != nil {
return c, b, err
}
Expand All @@ -88,7 +88,7 @@ func CommitGenesisBlockWithOverride(db kv.RwDB, genesis *types.Genesis, override
return c, b, nil
}

func WriteGenesisBlock(tx kv.RwTx, genesis *types.Genesis, overrideCancunTime, overrideShanghaiTime, overrideOptimismCanyonTime, overrideOptimismEcotoneTime, overridePragueTime *big.Int, tmpDir string, logger log.Logger) (*chain.Config, *types.Block, error) {
func WriteGenesisBlock(tx kv.RwTx, genesis *types.Genesis, overrideCancunTime, overrideShanghaiTime, overrideOptimismCanyonTime, overrideOptimismEcotoneTime, overrideOptimismFjordTime, overridePragueTime *big.Int, tmpDir string, logger log.Logger) (*chain.Config, *types.Block, error) {
var storedBlock *types.Block
if genesis != nil && genesis.Config == nil {
return params.AllProtocolChanges, nil, types.ErrGenesisNoConfig
Expand Down Expand Up @@ -135,6 +135,9 @@ func WriteGenesisBlock(tx kv.RwTx, genesis *types.Genesis, overrideCancunTime, o
"cancun", overrideCancunTime.String(), "ecotone", overrideOptimismEcotoneTime.String())
}
}
if config.IsOptimism() && overrideOptimismFjordTime != nil {
config.FjordTime = overrideOptimismFjordTime
}
}

if (storedHash == libcommon.Hash{}) {
Expand Down Expand Up @@ -836,8 +839,6 @@ func loadOPStackGenesisByChainName(name string) (*types.Genesis, error) {
switch opStackChainCfg.ChainID {
case params.OPMainnetChainID:
expectedHash = params.OPMainnetGenesisHash
case params.OPGoerliChainID:
expectedHash = params.OPGoerliGenesisHash
default:
return nil, fmt.Errorf("unknown stateless genesis definition for chain %d", opStackChainCfg.ChainID)
}
Expand Down
22 changes: 22 additions & 0 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ var PrecompiledContractsCancun = map[libcommon.Address]PrecompiledContract{
libcommon.BytesToAddress([]byte{0x0a}): &pointEvaluation{},
}

// PrecompiledContractsFjord contains the default set of pre-compiled Ethereum
// contracts used in the Fjord release.
var PrecompiledContractsFjord = map[libcommon.Address]PrecompiledContract{
libcommon.BytesToAddress([]byte{1}): &ecrecover{},
libcommon.BytesToAddress([]byte{2}): &sha256hash{},
libcommon.BytesToAddress([]byte{3}): &ripemd160hash{},
libcommon.BytesToAddress([]byte{4}): &dataCopy{},
libcommon.BytesToAddress([]byte{5}): &bigModExp{eip2565: true},
libcommon.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
libcommon.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
libcommon.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
libcommon.BytesToAddress([]byte{9}): &blake2F{},
libcommon.BytesToAddress([]byte{0x0a}): &pointEvaluation{},
libcommon.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{},
}

var PrecompiledContractsNapoli = map[libcommon.Address]PrecompiledContract{
libcommon.BytesToAddress([]byte{0x01}): &ecrecover{},
libcommon.BytesToAddress([]byte{0x02}): &sha256hash{},
Expand Down Expand Up @@ -151,6 +167,7 @@ var PrecompiledContractsPrague = map[libcommon.Address]PrecompiledContract{
}

var (
PrecompiledAddressesFjord []libcommon.Address
PrecompiledAddressesPrague []libcommon.Address
PrecompiledAddressesNapoli []libcommon.Address
PrecompiledAddressesCancun []libcommon.Address
Expand All @@ -176,6 +193,9 @@ func init() {
for k := range PrecompiledContractsCancun {
PrecompiledAddressesCancun = append(PrecompiledAddressesCancun, k)
}
for k := range PrecompiledContractsFjord {
PrecompiledAddressesFjord = append(PrecompiledAddressesFjord, k)
}
for k := range PrecompiledContractsNapoli {
PrecompiledAddressesNapoli = append(PrecompiledAddressesNapoli, k)
}
Expand All @@ -187,6 +207,8 @@ func init() {
// ActivePrecompiles returns the precompiles enabled with the current configuration.
func ActivePrecompiles(rules *chain.Rules) []libcommon.Address {
switch {
case rules.IsOptimismFjord:
return PrecompiledAddressesFjord
case rules.IsPrague:
return PrecompiledAddressesPrague
case rules.IsNapoli:
Expand Down
3 changes: 3 additions & 0 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,5 +415,8 @@ func BenchmarkPrecompiledP256Verify(b *testing.B) {
func TestPrecompiledP256Verify(t *testing.T) {
t.Parallel()

// test case from Polygon Napoli erigon
testJson("p256Verify", "100", t)
// test case from OP Stack Fjord geth
testJson("p256Verify2", "100", t)
}
2 changes: 2 additions & 0 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var emptyCodeHash = crypto.Keccak256Hash(nil)
func (evm *EVM) precompile(addr libcommon.Address) (PrecompiledContract, bool) {
var precompiles map[libcommon.Address]PrecompiledContract
switch {
case evm.chainRules.IsOptimismFjord:
precompiles = PrecompiledContractsFjord
case evm.chainRules.IsPrague:
precompiles = PrecompiledContractsPrague
case evm.chainRules.IsNapoli:
Expand Down
Loading
Loading