Misc#

This one was hard to classify

Precompiles#

Precompiles in Ethereum are programs that the EVM can execute directly. They are currently 9 of them.

Implementing these functions in EVM bytecode would be very gas inefficient. So it was decided to include them in the EVM itself.

title

source

Opcode vs Precompile#

The sha3 could have easily been a precompile. Weather an operation should be a opcode or a precomiple is a matter of debate in the Ethereum community.

Hash Function#

A hash function is one of the most important cryptographic primitives. It has the following characteristics:

  • fixed sized: every input (also called message) creates a hash value of fixed size.

  • deterministic: the same input will produce the same output every time.

  • one-way: its practically infeasible to invert.

  • chaotic: if only one bit changes the whole hash changes in a toatlly chaotic and random way.

Note that we are using the Python built-in hash function, so we do not have to import any external library.

The EVM uses the Keccak-256 function.

def sha3(evm):
    offset, size = evm.stack.pop(), evm.stack.pop()
    value = evm.memory.access(offset, size)
    evm.stack.push(hash(str(value)))

    evm.pc += 1

    # calculate gas
    minimum_word_size = (size + 31) / 32
    dynamic_gas = 6 * minimum_word_size # TODO: + memory_expansion_cost
    evm.gas_dec(30 + dynamic_gas)