Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(opcodes/eip5656/mcopy): add mCopy opcode, EIP-5656 #12

Draft
wants to merge 1 commit into
base: v20.0.0-exrp
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions x/evm/core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,38 @@ func opMstore8(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]
return nil, nil
}

// opMcopy implements EIP-5656 MCOPY operation
//
// Stack Input:
// [ length | top ] -- number of bytes to copy
// [ srcOffset | second ] -- source memory offset
// [ destOffset | third ] -- destination memory offset
//
// Operation Flow:
// 1. Pop parameters from stack:
// - destination offset
// - source offset
// - length
// 2. Convert stack items to uint64
// 3. Copy memory from source to destination
func opMcopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var (
destOffset = scope.Stack.Pop()
srcOffset = scope.Stack.Pop()
length = scope.Stack.Pop()
)

// Convert stack items to integers
destOffsetInt := destOffset.Uint64()
srcOffsetInt := srcOffset.Uint64()
lengthInt := length.Uint64()

// Copy memory
scope.Memory.Set(destOffsetInt, lengthInt, scope.Memory.GetPtr(int64(srcOffsetInt), int64(lengthInt)))

return nil, nil
}

func opSload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
loc := scope.Stack.Peek()
hash := common.Hash(loc.Bytes32())
Expand Down
25 changes: 25 additions & 0 deletions x/evm/core/vm/instructions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,31 @@ func TestOpMstore(t *testing.T) {
}
}

func TestOpMcopy(t *testing.T) {
var (
env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
stack, err = NewStack()
mem = NewMemory()
evmInterpreter = NewEVMInterpreter(env, env.Config)
pc = uint64(0)
)
require.NoError(t, err)

env.interpreter = evmInterpreter
mem.Resize(100)
mem.Set(10, 3, []byte{0x01, 0x02, 0x03})

stack.Push(new(uint256.Int).SetUint64(3)) // length
stack.Push(new(uint256.Int).SetUint64(10)) // srcOffset
stack.Push(new(uint256.Int).SetUint64(20)) // destOffset

opMcopy(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})

expected := []byte{0x01, 0x02, 0x03}
actual := mem.GetCopy(20, 3)
require.Equal(t, expected, actual)
}

func BenchmarkOpMstore(bench *testing.B) {
var (
env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
Expand Down
8 changes: 8 additions & 0 deletions x/evm/core/vm/jump_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,14 @@ func newFrontierInstructionSet() JumpTable {
minStack: minStack(2, 0),
maxStack: maxStack(2, 0),
},
MCOPY: {
execute: opMcopy,
constantGas: GasFastestStep,
dynamicGas: gasMStore,
minStack: minStack(3, 0),
maxStack: maxStack(3, 0),
memorySize: memoryMStore,
},
SLOAD: {
execute: opSload,
constantGas: params.SloadGasFrontier,
Expand Down
3 changes: 3 additions & 0 deletions x/evm/core/vm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const (
MSIZE OpCode = 0x59
GAS OpCode = 0x5a
JUMPDEST OpCode = 0x5b
MCOPY OpCode = 0x5e
PUSH0 OpCode = 0x5f
)

Expand Down Expand Up @@ -301,6 +302,7 @@ var opCodeToString = map[OpCode]string{
MSIZE: "MSIZE",
GAS: "GAS",
JUMPDEST: "JUMPDEST",
MCOPY: "MCOPY",
PUSH0: "PUSH0",

// 0x60 range - push.
Expand Down Expand Up @@ -465,6 +467,7 @@ var stringToOp = map[string]OpCode{
"MSIZE": MSIZE,
"GAS": GAS,
"JUMPDEST": JUMPDEST,
"MCOPY": MCOPY,
"PUSH0": PUSH0,
"PUSH1": PUSH1,
"PUSH2": PUSH2,
Expand Down