Ethereum Virtual Machine (EVM) Keccak Instructions

Introduction

Implementations of the EVM keccak instructions.

Module Contents

Functions

keccak

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

Module Details

keccak

keccak(evm: ethereum.muir_glacier.vm.Evm)None

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:
    # STACK
    memory_start_index = pop(evm.stack)
    size = pop(evm.stack)

    # GAS
    words = ceil32(Uint(size)) // 32
    word_gas_cost = GAS_KECCAK256_WORD * words
    extend_memory = calculate_gas_extend_memory(
        evm.memory, [(memory_start_index, size)]
    )
    charge_gas(evm, GAS_KECCAK256 + word_gas_cost + extend_memory.cost)

    # OPERATION
    evm.memory += b"\x00" * extend_memory.expand_by
    data = memory_read_bytes(evm.memory, memory_start_index, size)
    hash = keccak256(data)

    push(evm.stack, U256.from_be_bytes(hash))

    # PROGRAM COUNTER
    evm.pc += 1