Laurent Knauss Software Engineer
ethereum precompiles illustration

The Ethereum Virtual Machine is the runtime environment in which smart contracts are executed on this blockchain. Before a smart contract can be executed, it needs to be compiled from its high-level Solidity (or Vyper if you are more a pythonesque person) language into bytecode, which is the machine-readable language of the EVM.

The process of this compilation is as follows:

  • Lexical analysis
  • Parsing
  • Semantic analysis
  • Code generation

The end result of this compiling process is a binary file containing the bytecode of the smart contract.

After understanding the compilation process transforming smart contracts into EVM bytecode, it is worth mentioning another important aspect of the Ethereum ecosystem: precompiles.

Precompile contracts are part of the internal EVM and are just exposed in a way that Solidity can execute them. Precompiles are bundled in the EVM at specific, fixed addresses & called with determined gas cost. The purpose of those pre-deployed contracts is to provide certain complex operations that are usually too computationally expensive to be executed on-chain like a regular Solidity file would do or require special algorithms.

There exist 9 precompiles in the EVM and most of them are related to cryptographic primitives. Among those 9, here are 4 examples:

  • ECRecover: address 0x1 – Used to recover the public key from a signature and a message. It is used in verifying signatures.
  • SHA256: address 0x2 – Used for computing the SHA-256 hash of a given input.
  • RIPEMD160: address 0x3 – Used for computing the RIPEMD-160 hash of a given input.
  • Identity: address 0x4 – A simple precompile that returns the input unchanged. It is used as a no-op and can be seen as a basic cryptographic operation.

The advantage of precompiles is that you can code them in Golang and render them way cheaper to interact with than if you were to code them directly in Solidity.

    Precompiles & stateful precompiles in EVM-based blockchains | Laurent