core/vm: Rename SHA3 instruction to KECCAK256 (#23976)

This was proposed in 2016, Solidity uses this since 2017, and evmone and other VMs use the keccak256 name. This brings geth in line with those.
pull/24022/head
Alex Beregszaszi 3 years ago committed by GitHub
parent 1fa91729f2
commit a69d4b273d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      core/vm/evm.go
  2. 6
      core/vm/gas_table.go
  3. 2
      core/vm/instructions.go
  4. 4
      core/vm/instructions_test.go
  5. 10
      core/vm/jump_table.go
  6. 2
      core/vm/memory_table.go
  7. 6
      core/vm/opcodes.go
  8. 4
      params/protocol_params.go

@ -518,7 +518,7 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I
// Create2 creates a new contract using code as deployment code. // Create2 creates a new contract using code as deployment code.
// //
// The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:] // The different between Create2 with Create is Create2 uses keccak256(0xff ++ msg.sender ++ salt ++ keccak256(init_code))[12:]
// instead of the usual sender-and-nonce-hash as the address where the contract is initialized at. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
func (evm *EVM) Create2(caller ContractRef, code []byte, gas uint64, endowment *big.Int, salt *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) { func (evm *EVM) Create2(caller ContractRef, code []byte, gas uint64, endowment *big.Int, salt *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
codeAndHash := &codeAndHash{code: code} codeAndHash := &codeAndHash{code: code}

@ -247,7 +247,7 @@ func makeGasLog(n uint64) gasFunc {
} }
} }
func gasSha3(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { func gasKeccak256(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
gas, err := memoryGasCost(mem, memorySize) gas, err := memoryGasCost(mem, memorySize)
if err != nil { if err != nil {
return 0, err return 0, err
@ -256,7 +256,7 @@ func gasSha3(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize
if overflow { if overflow {
return 0, ErrGasUintOverflow return 0, ErrGasUintOverflow
} }
if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow { if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Keccak256WordGas); overflow {
return 0, ErrGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, wordGas); overflow { if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
@ -290,7 +290,7 @@ func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memoryS
if overflow { if overflow {
return 0, ErrGasUintOverflow return 0, ErrGasUintOverflow
} }
if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow { if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Keccak256WordGas); overflow {
return 0, ErrGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, wordGas); overflow { if gas, overflow = math.SafeAdd(gas, wordGas); overflow {

@ -231,7 +231,7 @@ func opSAR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte
return nil, nil return nil, nil
} }
func opSha3(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { func opKeccak256(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
offset, size := scope.Stack.pop(), scope.Stack.peek() offset, size := scope.Stack.pop(), scope.Stack.peek()
data := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64())) data := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))

@ -558,7 +558,7 @@ func BenchmarkOpMstore(bench *testing.B) {
} }
} }
func BenchmarkOpSHA3(bench *testing.B) { func BenchmarkOpKeccak256(bench *testing.B) {
var ( var (
env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
stack = newstack() stack = newstack()
@ -573,7 +573,7 @@ func BenchmarkOpSHA3(bench *testing.B) {
bench.ResetTimer() bench.ResetTimer()
for i := 0; i < bench.N; i++ { for i := 0; i < bench.N; i++ {
stack.pushN(*uint256.NewInt(32), *start) stack.pushN(*uint256.NewInt(32), *start)
opSha3(&pc, evmInterpreter, &ScopeContext{mem, stack, nil}) opKeccak256(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
} }
} }

@ -342,13 +342,13 @@ func newFrontierInstructionSet() JumpTable {
minStack: minStack(2, 1), minStack: minStack(2, 1),
maxStack: maxStack(2, 1), maxStack: maxStack(2, 1),
}, },
SHA3: { KECCAK256: {
execute: opSha3, execute: opKeccak256,
constantGas: params.Sha3Gas, constantGas: params.Keccak256Gas,
dynamicGas: gasSha3, dynamicGas: gasKeccak256,
minStack: minStack(2, 1), minStack: minStack(2, 1),
maxStack: maxStack(2, 1), maxStack: maxStack(2, 1),
memorySize: memorySha3, memorySize: memoryKeccak256,
}, },
ADDRESS: { ADDRESS: {
execute: opAddress, execute: opAddress,

@ -16,7 +16,7 @@
package vm package vm
func memorySha3(stack *Stack) (uint64, bool) { func memoryKeccak256(stack *Stack) (uint64, bool) {
return calcMemSize64(stack.Back(0), stack.Back(1)) return calcMemSize64(stack.Back(0), stack.Back(1))
} }

@ -70,7 +70,7 @@ const (
SHR OpCode = 0x1c SHR OpCode = 0x1c
SAR OpCode = 0x1d SAR OpCode = 0x1d
SHA3 OpCode = 0x20 KECCAK256 OpCode = 0x20
) )
// 0x30 range - closure state. // 0x30 range - closure state.
@ -254,7 +254,7 @@ var opCodeToString = map[OpCode]string{
MULMOD: "MULMOD", MULMOD: "MULMOD",
// 0x20 range - crypto. // 0x20 range - crypto.
SHA3: "SHA3", KECCAK256: "KECCAK256",
// 0x30 range - closure state. // 0x30 range - closure state.
ADDRESS: "ADDRESS", ADDRESS: "ADDRESS",
@ -422,7 +422,7 @@ var stringToOp = map[string]OpCode{
"SAR": SAR, "SAR": SAR,
"ADDMOD": ADDMOD, "ADDMOD": ADDMOD,
"MULMOD": MULMOD, "MULMOD": MULMOD,
"SHA3": SHA3, "KECCAK256": KECCAK256,
"ADDRESS": ADDRESS, "ADDRESS": ADDRESS,
"BALANCE": BALANCE, "BALANCE": BALANCE,
"ORIGIN": ORIGIN, "ORIGIN": ORIGIN,

@ -35,8 +35,8 @@ const (
LogDataGas uint64 = 8 // Per byte in a LOG* operation's data. LogDataGas uint64 = 8 // Per byte in a LOG* operation's data.
CallStipend uint64 = 2300 // Free gas given at beginning of call. CallStipend uint64 = 2300 // Free gas given at beginning of call.
Sha3Gas uint64 = 30 // Once per SHA3 operation. Keccak256Gas uint64 = 30 // Once per KECCAK256 operation.
Sha3WordGas uint64 = 6 // Once per word of the SHA3 operation's data. Keccak256WordGas uint64 = 6 // Once per word of the KECCAK256 operation's data.
SstoreSetGas uint64 = 20000 // Once per SSTORE operation. SstoreSetGas uint64 = 20000 // Once per SSTORE operation.
SstoreResetGas uint64 = 5000 // Once per SSTORE operation if the zeroness changes from zero. SstoreResetGas uint64 = 5000 // Once per SSTORE operation if the zeroness changes from zero.

Loading…
Cancel
Save