Blockchain Games 101 Precompiled Contracts

What is a precompiled contract?

A precompiled contract is a compromise method used in the EVM to provide more complex library functions (usually used for encryption, hashing, and other complex operations). It can also be understood as a special type of contract that is not suitable for writing opcodes. They are suitable for simple but frequently invoked contracts, or contracts with fixed logic but requiring heavy computation. Precompiled contracts are implemented in the node client code and run very fast because they do not require the EVM. They also have lower costs for developers compared to using functions directly executed in the EVM.

In the code below, you can see that the run function in evm.go contract has two branches: the first branch instantiates the index parameters through precompiled indexing to specify the precompiled contract, and the second branch calls evm if it is not a precompiled contract.

// run runs the given contract and takes care of running precompiles with a fallback to the byte code interpreter.
func run(evm *EVM, contract *Contract, input []byte, readOnly bool) ([]byte, error) {
   if contract.CodeAddr != nil {
      precompiles := PrecompiledContractsHomestead
      if evm.ChainConfig().IsByzantium(evm.BlockNumber) {
         precompiles = PrecompiledContractsByzantium
      }
      if p := precompiles[*contract.CodeAddr]; p != nil {
         return RunPrecompiledContract(p, input, contract)
      }
   }
   for _, interpreter := range evm.interpreters {
      if interpreter.CanRun(contract.Code) {
         if evm.interpreter != interpreter {
            // Ensure that the interpreter pointer is set back
            // to its current value upon return.
            defer func(i Interpreter) {
               evm.interpreter = i
            }(evm.interpreter)
            evm.interpreter = interpreter
         }
         return interpreter.Run(contract, input, readOnly)
      }
   }
   return nil, ErrNoCompatibleInterpreter
}

If represented graphically, the specific logic is as shown in the following diagram:

What are the bottlenecks of precompiled contracts?

Currently, Ethereum has eight precompiled contracts:

  1. ECRecover – Recover the corresponding address through signature
  2. SHA256 – Calculate SHA256 hash
  3. RIPEMD160 – Calculate RIPEMD160 hash
  4. Identity – Return the original value of the input data
  5. ModExp – Perform modular exponentiation
  6. ECAdd – Elliptic curve point addition
  7. ECMul – Elliptic curve point multiplication
  8. ECPairing – Pairing operation, verify elliptic curve points

It can be seen that the first four precompiled contracts provide basic encryption functions such as signatures and hashes, while the fifth to eighth ones provide elliptic curve operations related to zk-snarks.

So, why does Ethereum only support eight precompiled contracts? Aren’t precompiled contracts supposed to reduce gas consumption? And why not directly incorporate ECS (the framework for blockchain games) into Ethereum’s precompiled contracts?

Actually, there are three main reasons:

1. Excessive reliance on precompiled contracts will reduce the decentralization of the entire platform:

First, the code of the precompiled contract needs to be integrated into the client node code, which increases the complexity of the client. Second, verification nodes may filter out the computation of precompiled contracts for security reasons, so most of the requests for precompiled contracts are completed by full nodes. Currently, there are only 4,000-6,000 Ethereum full nodes globally, while there are 500,000 verification nodes, which indeed makes precompiled contracts more centralized compared to non-precompiled contracts.

2. Adding and modifying precompiled contracts require hard fork upgrades, which are not easily flexible and adaptable.

Support for precompiled contracts requires the EIP process. For example, EIP-196 adds two precompiled contracts ECADD() and ECMUL() on the alt_bn128 curve. EIP-197 adds the pairing LianGuaiiring function on the alt_bn128 curve. These are basically to support privacy on Ethereum, and the entire EIP process is lengthy and rigorous, so waiting for EIP to be approved is not a practical issue.

3. It is difficult to interact and combine precompiled contracts, with poor scalability.

This point is self-explanatory and intuitive.

What role does precompiled contracts play in full-chain games?

Precompiled contracts bypass the EVM and are executed directly by nodes, which can improve computational efficiency but also reduce the decentralization of the entire chain. Placing the core logic of high-frequency used games in precompiled contracts can optimize the performance of such games. Different game types have different key logics. Therefore, for a dedicated chain for a certain type of game, the precompiled contract design can be highly optimized for the needs of that type of game. In the iterative process of the game, the most efficient combination of precompiled contracts will also be gradually optimized.

Like what you're reading? Subscribe to our top stories.

We will continue to update Gambling Chain; if you have any questions or suggestions, please contact us!

Follow us on Twitter, Facebook, YouTube, and TikTok.

Share:

Was this article helpful?

93 out of 132 found this helpful

Gambling Chain Logo
Industry
Digital Asset Investment
Location
Real world, Metaverse and Network.
Goals
Build Daos that bring Decentralized finance to more and more persons Who love Web3.
Type
Website and other Media Daos

Products used

GC Wallet

Send targeted currencies to the right people at the right time.