How can we implement a new opcode in the Ethereum Virtual Machine. For eg : I want to implement an opcode logic such that the opcode number of this operation is 0x21. The EVM-implementation I am using is in go-lang.

2 Answers 2


I can only give you a general guide on how to do this:

1) Modify core/vm/instructions.go to add your instruction code, something like:

func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    x, y := stack.pop(), stack.pop()
    stack.push(math.U256(x.Add(x, y)))


    return nil, nil

2) Modify core/vm/gas_table.go to add the gas cost for your instruction, something like this:

func gasPush(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
    return GasFastestStep, nil

3) Modify core/vm/jump_table.go to add your new instruction opcode, something like this:

    ADD: {
        execute:       opAdd,
        gasCost:       constGasFunc(GasFastestStep),
        validateStack: makeStackFunc(2, 1),
        valid:         true,

4) Modify core/vm/opcodes.go to add your new opcode.

That should be it. This is for geth 1.7.2, I didn't look at 1.8 yet so can't tell.

  • I had done this steps earlier as well but I had missed the 2nd step. Thank you so much.
    – V.Agarwal
    Commented Jun 26, 2018 at 3:16
  • Can you also guide me on how to check whether the modification I had done is correct
    – V.Agarwal
    Commented Jun 26, 2018 at 3:29
  • that's easy, process the whole blockchain from block 0 until today, if you get the same block.Root() at the end then you didn't break Ethereum's code. After that you can test your instruction for go panics
    – Nulik
    Commented Jun 26, 2018 at 12:28

Well, it will be difficult to provide a copy&paste solution for this. However, for orientation:

You'll first have to at least understand how the code in this geth module works: https://github.com/ethereum/go-ethereum/tree/master/core/vm

opcodes.go / interpreter.go / evm.go are good entry points to start with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.