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