ethereum.forks.spurious_dragon.vm.instructions.keccakethereum.forks.byzantium.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:
26
    <snip>
38
    # STACK
39
    memory_start_index = pop(evm.stack)
40
    size = pop(evm.stack)
41
42
    # GAS
43
    words = ceil32(Uint(size)) // Uint(32)
44
    word_gas_cost = GasCosts.OPCODE_KECCAK256_PER_WORD * words
45
    extend_memory = calculate_gas_extend_memory(
46
        evm.memory, [(memory_start_index, size)]
47
    )
48
    charge_gas(
49
        evm,
50
        GasCosts.OPCODE_KECCAK256_BASE + word_gas_cost + extend_memory.cost,
51
    )
52
53
    # OPERATION
54
    evm.memory += b"\x00" * extend_memory.expand_by
55
    data = memory_read_bytes(evm.memory, memory_start_index, size)
56
    hashed = keccak256(data)
57
58
    push(evm.stack, U256.from_be_bytes(hashed))
59
60
    # PROGRAM COUNTER
61
    evm.pc += Uint(1)