From 41fc310dd7bc3e02cb82203877cb6bf413e56955 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Wed, 1 Feb 2017 22:36:51 +0100 Subject: [PATCH] core/vm: implements big int expmod precompile - eip#198 --- core/vm/contracts.go | 80 ++++++++++++++++++++++++++++++++++++++------ params/config.go | 23 ++++++++++--- params/util.go | 3 ++ 3 files changed, 91 insertions(+), 15 deletions(-) diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 593b6ca55c..c8ea5c4b63 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -17,6 +17,8 @@ package vm import ( + "math/big" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/logger" @@ -28,8 +30,8 @@ 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 - Run(input []byte) []byte // Run runs the precompiled contract + 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 @@ -38,11 +40,12 @@ var PrecompiledContracts = map[common.Address]PrecompiledContract{ 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) @@ -55,7 +58,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 } @@ -94,8 +97,8 @@ type sha256 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 *sha256) RequiredGas(inputSize int) uint64 { - return uint64(inputSize+31)/32*params.Sha256WordGas + params.Sha256Gas +func (c *sha256) RequiredGas(input []byte) uint64 { + return uint64(len(input)+31)/32*params.Sha256WordGas + params.Sha256Gas } func (c *sha256) Run(in []byte) []byte { return crypto.Sha256(in) @@ -108,8 +111,8 @@ type ripemd160 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 *ripemd160) RequiredGas(inputSize int) uint64 { - return uint64(inputSize+31)/32*params.Ripemd160WordGas + params.Ripemd160Gas +func (c *ripemd160) RequiredGas(input []byte) uint64 { + return uint64(len(input)+31)/32*params.Ripemd160WordGas + params.Ripemd160Gas } func (c *ripemd160) Run(in []byte) []byte { return common.LeftPadBytes(crypto.Ripemd160(in), 32) @@ -122,9 +125,66 @@ 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 } + +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() +} diff --git a/params/config.go b/params/config.go index 8c285781e9..1cfafb3d53 100644 --- a/params/config.go +++ b/params/config.go @@ -33,6 +33,7 @@ var MainnetChainConfig = &ChainConfig{ EIP150Hash: MainNetHomesteadGasRepriceHash, EIP155Block: MainNetSpuriousDragon, EIP158Block: MainNetSpuriousDragon, + EIP198Block: MainNetMetropolis, } // TestnetChainConfig is the chain parameters to run a node on the test network. @@ -45,6 +46,7 @@ var TestnetChainConfig = &ChainConfig{ EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"), EIP155Block: big.NewInt(10), EIP158Block: big.NewInt(10), + EIP198Block: TestNetMetropolis, } // ChainConfig is the core config which determines the blockchain settings. @@ -65,11 +67,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 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, @@ -77,11 +81,12 @@ func (c *ChainConfig) String() string { c.EIP150Block, c.EIP155Block, c.EIP158Block, + c.EIP198Block, ) } var ( - TestChainConfig = &ChainConfig{big.NewInt(1), new(big.Int), new(big.Int), true, new(big.Int), common.Hash{}, new(big.Int), new(big.Int)} + TestChainConfig = &ChainConfig{big.NewInt(1), new(big.Int), new(big.Int), true, new(big.Int), common.Hash{}, new(big.Int), new(big.Int), nil} TestRules = TestChainConfig.Rules(new(big.Int)) ) @@ -135,16 +140,24 @@ func (c *ChainConfig) IsEIP158(num *big.Int) bool { } +func (c *ChainConfig) IsEIP198(num *big.Int) bool { + if c.EIP198Block == nil || num == nil { + return false + } + return num.Cmp(c.EIP198Block) >= 0 + +} + // 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 bool + 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)} + 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)} } diff --git a/params/util.go b/params/util.go index 546ebb35c3..149dab4292 100644 --- a/params/util.go +++ b/params/util.go @@ -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) // main net default chain ID )