ethereum.spurious_dragon.vm.instructions.keccakethereum.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:
32
    """
33
    Pushes to the stack the Keccak-256 hash of a region of memory.
34
35
    This also expands the memory, in case the memory is insufficient to
36
    access the data's memory location.
37
38
    Parameters
39
    ----------
40
    evm :
41
        The current EVM frame.
42
43
    """
44
    # STACK
45
    memory_start_index = pop(evm.stack)
46
    size = pop(evm.stack)
47
48
    # GAS
49
    words = ceil32(Uint(size)) // Uint(32)
50
    word_gas_cost = GAS_KECCAK256_WORD * words
51
    extend_memory = calculate_gas_extend_memory(
52
        evm.memory, [(memory_start_index, size)]
53
    )
54
    charge_gas(evm, GAS_KECCAK256 + word_gas_cost + extend_memory.cost)
55
56
    # OPERATION
57
    evm.memory += b"\x00" * extend_memory.expand_by
58
    data = memory_read_bytes(evm.memory, memory_start_index, size)
59
    hash = keccak256(data)
60
61
    push(evm.stack, U256.from_be_bytes(hash))
62
63
    # PROGRAM COUNTER
64
    evm.pc += Uint(1)