Skip to content

Commit

Permalink
core/vm: Rename SHA3 instruction to KECCAK256 (#5890)
Browse files Browse the repository at this point in the history
Cherry-pick ethereum/go-ethereum#23976

Co-authored-by: Alex Beregszaszi <alex@rtfs.hu>
  • Loading branch information
yperbasis and axic authored Oct 28, 2022
1 parent f72cfb2 commit f46300e
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/trace_adhoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func (ot *OeTracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost
}
switch ot.lastOp {
case vm.CALLDATALOAD, vm.SLOAD, vm.MLOAD, vm.CALLDATASIZE, vm.LT, vm.GT, vm.DIV, vm.SDIV, vm.SAR, vm.AND, vm.EQ, vm.CALLVALUE, vm.ISZERO,
vm.ADD, vm.EXP, vm.CALLER, vm.SHA3, vm.SUB, vm.ADDRESS, vm.GAS, vm.MUL, vm.RETURNDATASIZE, vm.NOT, vm.SHR, vm.SHL,
vm.ADD, vm.EXP, vm.CALLER, vm.KECCAK256, vm.SUB, vm.ADDRESS, vm.GAS, vm.MUL, vm.RETURNDATASIZE, vm.NOT, vm.SHR, vm.SHL,
vm.EXTCODESIZE, vm.SLT, vm.OR, vm.NUMBER, vm.PC, vm.TIMESTAMP, vm.BALANCE, vm.SELFBALANCE, vm.MULMOD, vm.ADDMOD, vm.BASEFEE,
vm.BLOCKHASH, vm.BYTE, vm.XOR, vm.ORIGIN, vm.CODESIZE, vm.MOD, vm.SIGNEXTEND, vm.GASLIMIT, vm.DIFFICULTY, vm.SGT, vm.GASPRICE,
vm.MSIZE, vm.EXTCODEHASH, vm.SMOD, vm.CHAINID, vm.COINBASE:
Expand Down
2 changes: 1 addition & 1 deletion core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *uint2

// 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.
// DESCRIBED: docs/programmers_guide/guide.md#nonce
func (evm *EVM) Create2(caller ContractRef, code []byte, gas uint64, endowment *uint256.Int, salt *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
Expand Down
6 changes: 3 additions & 3 deletions core/vm/gas_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func makeGasLog(n uint64) gasFunc {
}
}

func gasSha3(evm *EVM, contract *Contract, stack *stack.Stack, mem *Memory, memorySize uint64) (uint64, error) {
func gasKeccak256(evm *EVM, contract *Contract, stack *stack.Stack, mem *Memory, memorySize uint64) (uint64, error) {
gas, err := memoryGasCost(mem, memorySize)
if err != nil {
return 0, err
Expand All @@ -261,7 +261,7 @@ func gasSha3(evm *EVM, contract *Contract, stack *stack.Stack, mem *Memory, memo
if overflow {
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
}
if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
Expand Down Expand Up @@ -295,7 +295,7 @@ func gasCreate2(evm *EVM, contract *Contract, stack *stack.Stack, mem *Memory, m
if overflow {
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
}
if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
Expand Down
2 changes: 1 addition & 1 deletion core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func opSAR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte
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()
data := scope.Memory.GetPtr(offset.Uint64(), size.Uint64())

Expand Down
4 changes: 2 additions & 2 deletions core/vm/instructions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ func BenchmarkOpMstore(bench *testing.B) {
}
}

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

Expand Down
10 changes: 5 additions & 5 deletions core/vm/jump_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,15 +443,15 @@ func newFrontierInstructionSet() JumpTable {
numPop: 2,
numPush: 1,
},
SHA3: {
execute: opSha3,
constantGas: params.Sha3Gas,
dynamicGas: gasSha3,
KECCAK256: {
execute: opKeccak256,
constantGas: params.Keccak256Gas,
dynamicGas: gasKeccak256,
minStack: minStack(2, 1),
maxStack: maxStack(2, 1),
numPop: 2,
numPush: 1,
memorySize: memorySha3,
memorySize: memoryKeccak256,
},
ADDRESS: {
execute: opAddress,
Expand Down
2 changes: 1 addition & 1 deletion core/vm/memory_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package vm

import "github.com/ledgerwatch/erigon/core/vm/stack"

func memorySha3(stack *stack.Stack) (uint64, bool) {
func memoryKeccak256(stack *stack.Stack) (uint64, bool) {
return calcMemSize64(stack.Back(0), stack.Back(1))
}

Expand Down
6 changes: 3 additions & 3 deletions core/vm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const (
SHR
SAR

SHA3 OpCode = 0x20
KECCAK256 OpCode = 0x20
)

// 0x30 range - closure state.
Expand Down Expand Up @@ -253,7 +253,7 @@ var opCodeToString = map[OpCode]string{
MULMOD: "MULMOD",

// 0x20 range - crypto.
SHA3: "SHA3",
KECCAK256: "KECCAK256",

// 0x30 range - closure state.
ADDRESS: "ADDRESS",
Expand Down Expand Up @@ -426,7 +426,7 @@ var stringToOp = map[string]OpCode{
"SAR": SAR,
"ADDMOD": ADDMOD,
"MULMOD": MULMOD,
"SHA3": SHA3,
"KECCAK256": KECCAK256,
"ADDRESS": ADDRESS,
"BALANCE": BALANCE,
"ORIGIN": ORIGIN,
Expand Down
4 changes: 2 additions & 2 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const (
LogDataGas uint64 = 8 // Per byte in a LOG* operation's data.
CallStipend uint64 = 2300 // Free gas given at beginning of call.

Sha3Gas uint64 = 30 // Once per SHA3 operation.
Sha3WordGas uint64 = 6 // Once per word of the SHA3 operation's data.
Keccak256Gas uint64 = 30 // Once per KECCAK256 operation.
Keccak256WordGas uint64 = 6 // Once per word of the KECCAK256 operation's data.

SstoreSetGas uint64 = 20000 // Once per SLOAD operation.
SstoreResetGas uint64 = 5000 // Once per SSTORE operation if the zeroness changes from zero.
Expand Down

0 comments on commit f46300e

Please sign in to comment.