core/vm: implements big int expmod precompile - EIP#198

This commit is contained in:
Jeffrey Wilcke 2017-02-01 22:36:51 +01:00
parent 24b9860c1b
commit 0906d61746
5 changed files with 136 additions and 29 deletions

View file

@ -31,11 +31,11 @@ import (
// requires a deterministic gas count based on the input size of the Run method of the
// contract.
type PrecompiledContract interface {
RequiredGas(inputSize int) uint64 // RequiredPrice calculates the contract gas use
RequiredGas(input []byte) uint64 // RequiredPrice calculates the contract gas use
Run(input []byte) []byte // Run runs the precompiled contract
}
// Precompiled contains the default set of ethereum contracts
// PrecompiledContracts contains the default set of ethereum contracts
var PrecompiledContracts = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &ecrecover{},
common.BytesToAddress([]byte{2}): &sha256hash{},
@ -43,9 +43,19 @@ var PrecompiledContracts = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{4}): &dataCopy{},
}
// PrecompiledContractsEIP198 contains the default set of ethereum contracts
// for EIP198.
var PrecompiledContractsEIP198 = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &ecrecover{},
common.BytesToAddress([]byte{2}): &sha256{},
common.BytesToAddress([]byte{3}): &ripemd160{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModexp{},
}
// RunPrecompile runs and evaluate the output of a precompiled contract defined in contracts.go
func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contract) (ret []byte, err error) {
gas := p.RequiredGas(len(input))
gas := p.RequiredGas(input)
if contract.UseGas(gas) {
ret = p.Run(input)
@ -58,7 +68,7 @@ func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contr
// ECRECOVER implemented as a native contract
type ecrecover struct{}
func (c *ecrecover) RequiredGas(inputSize int) uint64 {
func (c *ecrecover) RequiredGas(input []byte) uint64 {
return params.EcrecoverGas
}
@ -97,8 +107,8 @@ type sha256hash struct{}
//
// This method does not require any overflow checking as the input size gas costs
// required for anything significant is so high it's impossible to pay for.
func (c *sha256hash) RequiredGas(inputSize int) uint64 {
return uint64(inputSize+31)/32*params.Sha256WordGas + params.Sha256Gas
func (c *sha256hash) RequiredGas(input []byte) uint64 {
return uint64(len(input)+31)/32*params.Sha256WordGas + params.Sha256Gas
}
func (c *sha256hash) Run(in []byte) []byte {
h := sha256.Sum256(in)
@ -112,8 +122,8 @@ type ripemd160hash struct{}
//
// This method does not require any overflow checking as the input size gas costs
// required for anything significant is so high it's impossible to pay for.
func (c *ripemd160hash) RequiredGas(inputSize int) uint64 {
return uint64(inputSize+31)/32*params.Ripemd160WordGas + params.Ripemd160Gas
func (c *ripemd160hash) RequiredGas(input []byte) uint64 {
return uint64(len(input)+31)/32*params.Ripemd160WordGas + params.Ripemd160Gas
}
func (c *ripemd160hash) Run(in []byte) []byte {
ripemd := ripemd160.New()
@ -128,9 +138,67 @@ type dataCopy struct{}
//
// This method does not require any overflow checking as the input size gas costs
// required for anything significant is so high it's impossible to pay for.
func (c *dataCopy) RequiredGas(inputSize int) uint64 {
return uint64(inputSize+31)/32*params.IdentityWordGas + params.IdentityGas
func (c *dataCopy) RequiredGas(input []byte) uint64 {
return uint64(len(input)+31)/32*params.IdentityWordGas + params.IdentityGas
}
func (c *dataCopy) Run(in []byte) []byte {
return in
}
// bigModexp implements a native big integer exponential modular operation.
type bigModexp struct{}
// RequiredGas returns the gas required to execute the pre-compiled contract.
//
// This method does not require any overflow checking as the input size gas costs
// required for anything significant is so high it's impossible to pay for.
func (c *bigModexp) RequiredGas(input []byte) uint64 {
// TODO reword required gas to have error reporting and convert arithmetic
// to uint64.
if len(input) < 3*32 {
input = append(input, make([]byte, 3*32-len(input))...)
}
var (
baseLen = common.BytesToBig(input[:31])
expLen = common.BigMax(common.BytesToBig(input[32:64]), big.NewInt(1))
modLen = common.BytesToBig(input[65:97])
)
x := new(big.Int).Set(common.BigMax(baseLen, modLen))
x.Mul(x, x)
x.Mul(x, expLen)
x.Div(x, new(big.Int).SetUint64(params.QuadCoeffDiv))
return x.Uint64()
}
func (c *bigModexp) Run(input []byte) []byte {
if len(input) < 3*32 {
input = append(input, make([]byte, 3*32-len(input))...)
}
// why 32-byte? These values won't fit anyway
var (
baseLen = common.BytesToBig(input[:32]).Uint64()
expLen = common.BytesToBig(input[32:64]).Uint64()
modLen = common.BytesToBig(input[64:96]).Uint64()
)
input = input[96:]
if uint64(len(input)) < baseLen {
input = append(input, make([]byte, baseLen-uint64(len(input)))...)
}
base := common.BytesToBig(input[:baseLen])
input = input[baseLen:]
if uint64(len(input)) < expLen {
input = append(input, make([]byte, expLen-uint64(len(input)))...)
}
exp := common.BytesToBig(input[:expLen])
input = input[expLen:]
if uint64(len(input)) < modLen {
input = append(input, make([]byte, modLen-uint64(len(input)))...)
}
mod := common.BytesToBig(input[:modLen])
return base.Exp(base, exp, mod).Bytes()
}

View file

@ -33,6 +33,22 @@ type (
GetHashFunc func(uint64) common.Hash
)
// run runs the given contract and takes care of running precompiles with a fallback to the byte code interpreter.
func run(evm *EVM, contract *Contract, input []byte) ([]byte, error) {
if contract.CodeAddr != nil {
precompiledContracts := PrecompiledContracts
if evm.ChainConfig().IsEIP198(evm.BlockNumber) {
precompiledContracts = PrecompiledContractsEIP198
}
if p := precompiledContracts[*contract.CodeAddr]; p != nil {
return RunPrecompiledContract(p, input, contract)
}
}
return evm.interpreter.Run(contract, input)
}
// Context provides the EVM with auxiliary information. Once provided it shouldn't be modified.
type Context struct {
// CanTransfer returns whether the account contains
@ -134,7 +150,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
contract := NewContract(caller, to, value, gas)
contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
ret, err = evm.interpreter.Run(contract, input)
ret, err = run(evm, contract, input)
// When an error was returned by the EVM or when setting the creation code
// above we revert to the snapshot and consume any gas remaining. Additionally
// when we're in homestead this also counts for code storage gas errors.
@ -175,7 +191,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
contract := NewContract(caller, to, value, gas)
contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
ret, err = evm.interpreter.Run(contract, input)
ret, err = run(evm, contract, input)
if err != nil {
contract.UseGas(contract.Gas)
@ -210,7 +226,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
contract := NewContract(caller, to, nil, gas).AsDelegate()
contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
ret, err = evm.interpreter.Run(contract, input)
ret, err = run(evm, contract, input)
if err != nil {
contract.UseGas(contract.Gas)
@ -253,8 +269,7 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I
contract := NewContract(caller, AccountRef(contractAddr), value, gas)
contract.SetCallCode(&contractAddr, crypto.Keccak256Hash(code), code)
ret, err = evm.interpreter.Run(contract, nil)
ret, err = run(evm, contract, nil)
// check whether the max code size has been exceeded
maxCodeSizeExceeded := len(ret) > params.MaxCodeSize
// if the contract creation ran successfully and no errors were returned

View file

@ -84,12 +84,6 @@ func (evm *Interpreter) Run(contract *Contract, input []byte) (ret []byte, err e
evm.env.depth++
defer func() { evm.env.depth-- }()
if contract.CodeAddr != nil {
if p := PrecompiledContracts[*contract.CodeAddr]; p != nil {
return RunPrecompiledContract(p, input, contract)
}
}
// Don't bother with the execution if there's no code.
if len(contract.Code) == 0 {
return nil, nil

View file

@ -34,9 +34,10 @@ var (
EIP150Hash: MainNetHomesteadGasRepriceHash,
EIP155Block: MainNetSpuriousDragon,
EIP158Block: MainNetSpuriousDragon,
EIP198Block: MainNetMetropolis,
}
// TestnetChainConfig contains the chain parameters to run a node on the ropsten test network.
// TestnetChainConfig is the chain parameters to run a node on the test network.
TestnetChainConfig = &ChainConfig{
ChainId: big.NewInt(3),
HomesteadBlock: big.NewInt(0),
@ -46,19 +47,20 @@ var (
EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
EIP155Block: big.NewInt(10),
EIP158Block: big.NewInt(10),
EIP198Block: TestNetMetropolis,
}
// AllProtocolChanges contains every protocol change (EIPs)
// introduced and accepted by the Ethereum core developers.
// TestChainConfig is like AllProtocolChanges but has chain ID 1.
//
// This configuration is intentionally not using keyed fields.
// This configuration must *always* have all forks enabled, which
// means that all fields must be set at all times. This forces
// anyone adding flags to the config to also have to set these
// fields.
AllProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0)}
TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0)}
AllProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0)}
TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), nil}
TestRules = TestChainConfig.Rules(new(big.Int))
)
// ChainConfig is the core config which determines the blockchain settings.
@ -79,11 +81,13 @@ type ChainConfig struct {
EIP155Block *big.Int `json:"eip155Block"` // EIP155 HF block
EIP158Block *big.Int `json:"eip158Block"` // EIP158 HF block
EIP198Block *big.Int `json:"eip198Block"` // EIP198 HF block
}
// String implements the fmt.Stringer interface.
func (c *ChainConfig) String() string {
return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v}",
return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v EIP198: %v}",
c.ChainId,
c.HomesteadBlock,
c.DAOForkBlock,
@ -91,6 +95,7 @@ func (c *ChainConfig) String() string {
c.EIP150Block,
c.EIP155Block,
c.EIP158Block,
c.EIP198Block,
)
}
@ -200,6 +205,14 @@ func configNumEqual(x, y *big.Int) bool {
return x.Cmp(y) == 0
}
func (c *ChainConfig) IsEIP198(num *big.Int) bool {
if c.EIP198Block == nil || num == nil {
return false
}
return num.Cmp(c.EIP198Block) >= 0
}
// ConfigCompatError is raised if the locally-stored blockchain is initialised with a
// ChainConfig that would alter the past.
type ConfigCompatError struct {
@ -230,3 +243,17 @@ func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatEr
func (err *ConfigCompatError) Error() string {
return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
}
// Rules wraps ChainConfig and is merely syntatic sugar or can be used for functions
// that do not have or require information about the block.
//
// Rules is a one time interface meaning that it shouldn't be used in between transition
// phases.
type Rules struct {
ChainId *big.Int
IsHomestead, IsEIP150, IsEIP155, IsEIP158, IsEIP198 bool
}
func (c *ChainConfig) Rules(num *big.Int) Rules {
return Rules{ChainId: new(big.Int).Set(c.ChainId), IsHomestead: c.IsHomestead(num), IsEIP150: c.IsEIP150(num), IsEIP155: c.IsEIP155(num), IsEIP158: c.IsEIP158(num), IsEIP198: c.IsEIP198(num)}
}

View file

@ -38,6 +38,9 @@ var (
TestNetSpuriousDragon = big.NewInt(10)
MainNetSpuriousDragon = big.NewInt(2675000)
TestNetMetropolis = big.NewInt(11)
MainNetMetropolis = big.NewInt(10000000)
TestNetChainID = big.NewInt(3) // Test net default chain ID
MainNetChainID = big.NewInt(1) // Mainnet default chain ID
MainNetChainID = big.NewInt(1) // main net default chain ID
)