Ethereum currently has 9 precompiled contracts available at addresses 0x01-0x09, none of which are state-changing.
But is that a VM requirement? Is it possible to implement a new precompile that does have state?
The existing precompiled contracts in Ethereum primarily handle specific computational tasks optimized for performance, and they have been stateless/pure mainly for design reasons, not because of inherent EVM limitations.
Here's a breakdown:
Nature of Precompiled Contracts: The existing precompiled contracts are designed to perform certain computational tasks efficiently, such as elliptic curve operations and specific hashing functions. These operations are computationally intensive when executed within the EVM, but they can be optimized when implemented in native code, which is the primary reason for having them as precompiled contracts.
State Management in Ethereum: State in Ethereum is maintained through a key-value store known as the "state trie." Any entity with an address, be it an externally owned account (EOA) or a contract, can have associated state. Theoretically, there's nothing stopping a precompiled contract from having an associated state within this structure, though it's also possible for a precompile to have its state managed externally.
Use Cases: For any proposal introducing state to precompiled contracts, there should be a compelling use case. One can argue that standard contracts are already apt for state management, and introducing state to precompiled contracts might complicate the system without clear benefits.
Examples from EVM Tools and EVM Compatible Networks: Foundry is an example of a framework that uses a stateful precompiled contract albeit for a local environment i.e. the Vm CheatCodes Precompile Contract which exposes numerous
view/stateful functions via it's solidity interface to efficiently compute something(e.g. addr(uint256 privateKey)), view or manipulate the state of it's local testing environment. There are also live EVM compatible networks that implement stateful precompiled contracts such as Moonbeam's Collective Precompile or Hedera's HTS Precompile