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