ethereum.forks.bpo5.vm.instructions.keccakethereum.forks.amsterdam.vm.instructions.keccak

Ethereum Virtual Machine (EVM) Keccak Instructions.

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Implementations of the EVM keccak instructions.

keccak

Pushes to the stack the Keccak-256 hash of a region of memory.

This also expands the memory, in case the memory is insufficient to access the data's memory location.

Parameters

evm : The current EVM frame.

def keccak(​evm: Evm​) -> None:
27
    <snip>
39
    # STACK
40
    memory_start_index = pop(evm.stack)
41
    size = pop(evm.stack)
42
43
    # GAS
44
    words = ceil32(Uint(size)) // Uint(32)
44
    word_gas_cost = GasCosts.OPCODE_KECCAK256_PER_WORD * words
45
    word_gas_cost = ExecutionGas(GasCosts.OPCODE_KECCAK256_PER_WORD * words)
46
    extend_memory = calculate_gas_extend_memory(
47
        evm.memory, [(memory_start_index, size)]
48
    )
49
    charge_gas(
50
        evm,
51
        GasCosts.OPCODE_KECCAK256_BASE + word_gas_cost + extend_memory.cost,
52
    )
53
54
    # OPERATION
55
    evm.memory += b"\x00" * extend_memory.expand_by
56
    data = memory_read_bytes(evm.memory, memory_start_index, size)
57
    hashed = keccak256(data)
58
59
    push(evm.stack, U256.from_be_bytes(hashed))
60
61
    # PROGRAM COUNTER
62
    evm.pc += Uint(1)