EMC Testnet Documentation: Quick Start Guide
  • EMC TESTNET
    • EMC Testnet Chain Documentation: Quick Start Guide
      • Introduction
      • Requirements
      • Setting Up the Development Environment
      • Testing
      • Conclusion
    • Estimate Gas Costs
      • What is Gas fee?
      • Methods to estimate gas costs
      • Precaution
      • Conclusion
    • EMC chain documents: Mainnet and Testnet
      • EMC mainnet
      • EMC Testnet
      • User Guide
      • Conclusion
    • Cross-chain messaging
      • What is cross-chain messaging?
      • Principle of cross-chain message passing
      • Implement cross-chain messaging
      • Conclusion
    • How to use oracles in applications
      • What is an oracle?
      • Steps to use oracle
      • Precautions
      • Conclusion
    • Precompilation
      • Overview of precompiled contracts
    • Functions of NodeInterface
      • Using NodeInterface
      • Conclusion
    • RPC endpoints and providers
      • Introduction
      • EMC Mainnet RPC endpoint
      • EMC Testnet RPC endpoint
      • User Guide
      • Conclusion
    • Block explorer
      • User Guide
    • Cross-chain bridge
      • Overview
      • Detailed operation rules and principles
  • EMC Airdrop Guidelines
    • EMC Airdrop Guidelines
      • What is EMC Public Testnet?
      • EMC Public Testnet Incentive Program
      • EMC Public Testnet Mining Rules
      • How to seek help if you encounter a problem?
      • How long will the EMC Public Testnet Mining Event last?
    • Wallet Setup
      • Method 1: One-Click Configuration (Recommended)
      • Method 2: Manual Configuration
        • EMC Testnet Network RPC Information
    • Faucets: Claim Test Tokens
    • Testnet Tutorial Guide
      • Windows Tutorial
        • Docker Installation and Setup Tutorial(Window Version)
      • MacOS Tutorial
      • Linux Tutorial
        • Docker Installation and NVIDIA GPU Configuration
        • EMC Client Installation and Operation
      • Windows CLI Tutorial
        • WSL Subsystem Installation
        • Install and Setup Docker Desktop
        • EMC CLI Download and Mining
        • Claim Gas Fee from EMC Community
    • EMC Testnet Blockchain Explorer
Powered by GitBook
On this page
  1. EMC TESTNET
  2. Precompilation

Overview of precompiled contracts

Introduction

The precompiled contract is a specific address contract on the EMC chain, which provides complex computing functions and is directly implemented and optimized by the blockchain client. As a Layer 1 blockchain, EMC Chain's precompiled contracts can efficiently perform specific operations, such as encryption algorithms, hash functions and other mathematical operations, improving developer application performance and efficiency.

What is a precompiled contract?

A precompiled contract is a special smart contract that runs at a specific address and is supported by the underlying implementation of the blockchain. Unlike ordinary smart contracts, precompiled contracts do not need to be interpreted and executed in the EVM, but are directly processed by the client code, so the execution speed is faster and the gas cost is lower.

Precompiled contracts on the EMC chain

The EMC chain inherits Ethereum's precompiled contracts and may add its own precompiled contracts. The following are common precompiled contracts on the EMC chain:

1. ECRecover (0x01): Recover the public key from the signature.

2. SHA256 (0x02): Compute the SHA-256 hash value of the input data.

3. RIPEMD160 (0x03): Compute the RIPEMD-160 hash of the input data.

4. Identity (0x04): Returns a copy of the input data.

5. ModExp (0x05): Perform Modular exponentiation of large numbers.

6. BN256Add (0x06): Addition operation on the BN256 curve.

7. BN256ScalarMul (0x07): Scalar multiplication on BN256 curves.

8. BN256Pairing (0x08): Pairing operation on BN256 curve.

The addresses and functions of these precompiled contracts are consistent with Ethereum on the EMC chain, ensuring compatibility and consistency.

Precompiled contract example

Here is an example of how to call a precompiled contract in a smart contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
contract UsePrecompile {
    
    // Call ECRecover precompiled contract
    function recover(bytes32 hash, bytes memory signature) public pure returns (address) {
        bytes32 r;
        bytes32 s;
        uint8 v;
        
        // Signature format: {r}{s}{v}
        require(signature.length == 65, "invalid signature length");
 
        assembly {
            r := mload(add(signature, 0x20))
            s := mload(add(signature, 0x40))
            v := byte(0, mload(add(signature, 0x60)))
        }
 
        if (v < 27) {
            in += 27;
        }
 
        require(v == 27 || v == 28, "invalid signature version");
 
        return ecrecover(hash, v, r, s);
    }
    
    // Call SHA256 precompiled contract
    function sha256Hash(bytes memory data) public pure returns (bytes32) {
        return sha256(data);
    }
}

Advantages of using precompiled contracts

● Efficiency: The precompiled contract is directly executed by the client code, which is faster.

● Low gas cost: Due to the efficiency of precompiled contracts, the gas fees required for their execution are usually lower than equivalent smart contract implementations.

● Security: Precompiled contracts are implemented and maintained by the blockchain client, reducing the risk of vulnerabilities in the contract.

NodeInterfaceOverview

Introduction

The NodeInterface of the EMC chain provides a set of powerful APIs through which developers can interact with the nodes of the EMC chain. NodeInterface provides a convenient interface for application development and on-chain data access, allowing developers to efficiently build and maintain decentralized applications (dApps).

PreviousPrecompilationNextFunctions of NodeInterface

Last updated 9 months ago