diff --git a/Makefile b/Makefile index aa93e43e5aa0..8f84d6cfad45 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,8 @@ test: all cd ${PWD}/cmd/geth; go test -test.run TestCustomGenesis # module test $(GORUN) build/ci.go test ./consensus ./core ./eth ./miner ./node ./trie ./rollup/... + # RIP-7212 (secp256r1) precompiled contract test + cd ${PWD}/core/vm; go test -v -run=^TestPrecompiledP256 -bench=^BenchmarkPrecompiledP256 lint: ## Run linters. $(GORUN) build/ci.go lint diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 446638f9a012..d115182d49b3 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -126,6 +126,21 @@ var PrecompiledContractsBernoulli = map[common.Address]PrecompiledContract{ common.BytesToAddress([]byte{9}): &blake2FDisabled{}, } +// PrecompiledContractsEuclidV2 contains the default set of pre-compiled Ethereum +// contracts used in the EuclidV2 release. Same as Bernoulli and adds p256Verify +var PrecompiledContractsEuclidV2 = map[common.Address]PrecompiledContract{ + common.BytesToAddress([]byte{1}): &ecrecover{}, + common.BytesToAddress([]byte{2}): &sha256hash{}, + common.BytesToAddress([]byte{3}): &ripemd160hashDisabled{}, + common.BytesToAddress([]byte{4}): &dataCopy{}, + common.BytesToAddress([]byte{5}): &bigModExp{eip2565: true}, + common.BytesToAddress([]byte{6}): &bn256AddIstanbul{}, + common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{}, + common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{}, + common.BytesToAddress([]byte{9}): &blake2FDisabled{}, + common.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{}, +} + // PrecompiledContractsBLS contains the set of pre-compiled Ethereum // contracts specified in EIP-2537. These are exported for testing purposes. var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{ @@ -140,13 +155,8 @@ var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{ common.BytesToAddress([]byte{18}): &bls12381MapG2{}, } -// PrecompiledContractsP256Verify contains the precompiled Ethereum -// contract specified in EIP-7212/RIP-7212. This is exported for testing purposes. -var PrecompiledContractsP256Verify = map[common.Address]PrecompiledContract{ - common.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{}, -} - var ( + PrecompiledAddressesEuclidV2 []common.Address PrecompiledAddressesBernoulli []common.Address PrecompiledAddressesArchimedes []common.Address PrecompiledAddressesBerlin []common.Address @@ -174,11 +184,16 @@ func init() { for k := range PrecompiledContractsBernoulli { PrecompiledAddressesBernoulli = append(PrecompiledAddressesBernoulli, k) } + for k := range PrecompiledContractsEuclidV2 { + PrecompiledAddressesEuclidV2 = append(PrecompiledAddressesEuclidV2, k) + } } // ActivePrecompiles returns the precompiles enabled with the current configuration. func ActivePrecompiles(rules params.Rules) []common.Address { switch { + case rules.IsEuclidV2: + return PrecompiledAddressesEuclidV2 case rules.IsBernoulli: return PrecompiledAddressesBernoulli case rules.IsArchimedes: diff --git a/core/vm/evm.go b/core/vm/evm.go index b20e0a876fac..b3685c934358 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -46,6 +46,8 @@ type ( func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) { var precompiles map[common.Address]PrecompiledContract switch { + case evm.chainRules.IsEuclidV2: + precompiles = PrecompiledContractsEuclidV2 case evm.chainRules.IsBernoulli: precompiles = PrecompiledContractsBernoulli case evm.chainRules.IsArchimedes: diff --git a/params/config.go b/params/config.go index 21fa4f05f067..bb976eae1e40 100644 --- a/params/config.go +++ b/params/config.go @@ -639,6 +639,7 @@ type ChainConfig struct { DarwinTime *uint64 `json:"darwinTime,omitempty"` // Darwin switch time (nil = no fork, 0 = already on darwin) DarwinV2Time *uint64 `json:"darwinv2Time,omitempty"` // DarwinV2 switch time (nil = no fork, 0 = already on darwinv2) EuclidTime *uint64 `json:"euclidTime,omitempty"` // Euclid switch time (nil = no fork, 0 = already on euclid) + EuclidV2Time *uint64 `json:"euclidv2Time,omitempty"` // EuclidV2 switch time (nil = no fork, 0 = already on euclidv2) // TerminalTotalDifficulty is the amount of total difficulty reached by // the network that triggers the consensus upgrade. @@ -899,21 +900,26 @@ func (c *ChainConfig) IsCurie(num *big.Int) bool { return isForked(c.CurieBlock, num) } -// IsDarwin returns whether num is either equal to the Darwin fork block or greater. +// IsDarwin returns whether time is either equal to the Darwin fork time or greater. func (c *ChainConfig) IsDarwin(now uint64) bool { return isForkedTime(now, c.DarwinTime) } -// IsDarwinV2 returns whether num is either equal to the DarwinV2 fork block or greater. +// IsDarwinV2 returns whether time is either equal to the DarwinV2 fork time or greater. func (c *ChainConfig) IsDarwinV2(now uint64) bool { return isForkedTime(now, c.DarwinV2Time) } -// IsEuclid returns whether num is either equal to the Darwin fork block or greater. +// IsEuclid returns whether time is either equal to the Euclid fork time or greater. func (c *ChainConfig) IsEuclid(now uint64) bool { return isForkedTime(now, c.EuclidTime) } +// IsEuclidV2 returns whether time is either equal to the EuclidV2 fork time or greater. +func (c *ChainConfig) IsEuclidV2(now uint64) bool { + return isForkedTime(now, c.EuclidV2Time) +} + // IsTerminalPoWBlock returns whether the given block is the last block of PoW stage. func (c *ChainConfig) IsTerminalPoWBlock(parentTotalDiff *big.Int, totalDiff *big.Int) bool { if c.TerminalTotalDifficulty == nil { @@ -1126,7 +1132,7 @@ type Rules struct { IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool IsBerlin, IsLondon, IsArchimedes, IsShanghai bool - IsBernoulli, IsCurie, IsDarwin, IsEuclid bool + IsBernoulli, IsCurie, IsDarwin, IsEuclid, IsEuclidV2 bool } // Rules ensures c's ChainID is not nil. @@ -1153,5 +1159,6 @@ func (c *ChainConfig) Rules(num *big.Int, time uint64) Rules { IsCurie: c.IsCurie(num), IsDarwin: c.IsDarwin(time), IsEuclid: c.IsEuclid(time), + IsEuclidV2: c.IsEuclidV2(time), } }