Precompiled Contracts

Precompiled contracts are a compromise used in the EVM to provide more complex library functions (usually used for complex operations such as encryption, hashing, etc.) that are not suitable for writing in opcode. They are applied to contracts that are simple but frequently called, or that are logically fixed but computationally intensive. Precompiled contracts are implemented on the client-side with client code, and because they do not require the EVM, they run fast. It also costs less for developers than using functions that run directly in the EVM.

MAP Pre-compiled contracts

To ease the development of light clients, all kinds of cryptography primitives are supported at the blockchain level and are exposed to EVM via pre-compiled contracts.

MAP Relay Chain will implement the pre-compiled contracts to support:

ecrecover

  • Address 0x0000000000000000000000000000000000000001

    ecrecover implemented as a native contract.

  func (c *ecrecover) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) {
  const ecRecoverInputLength = 128
  
      input = common.RightPadBytes(input, ecRecoverInputLength)
      // "input" is (hash, v, r, s), each 32 bytes
      // but for ecrecover we want (r, s, v)
  
      r := new(big.Int).SetBytes(input[64:96])
      s := new(big.Int).SetBytes(input[96:128])
      v := input[63] - 27
  
      // tighter sig s values input homestead only apply to tx sigs
      if !allZero(input[32:63]) || !crypto.ValidateSignatureValues(v, r, s, false) {
          return nil, nil
      }
      // We must make sure not to modify the 'input', so placing the 'v' along with
      // the signature needs to be done on a new allocation
      sig := make([]byte, 65)
      copy(sig, input[64:128])
      sig[64] = v
      // v needs to be at the end for libsecp256k1
      pubKey, err := crypto.Ecrecover(input[:32], sig)
      // make sure the public key is a valid one
      if err != nil {
          return nil, nil
      }
  
      // the first byte of pubkey is bitcoin heritage
      return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32), nil
  }

sha256hash

  • Address 0x0000000000000000000000000000000000000002

    SHA256 implemented as a native contract.

dataCopy

  • Address 0x0000000000000000000000000000000000000004

  • data copy implemented as a native contract.

bigModExp

  • Address 0x0000000000000000000000000000000000000005

    bigModExp implements a native big integer exponential modular operation.

bn256AddIstanbul

  • Address 0x0000000000000000000000000000000000000006

    bn256Add implements a native elliptic curve point addition conforming to Istanbul consensus rules.

bn256ScalarMulIstanbul

  • Address 0x0000000000000000000000000000000000000007

    bn256ScalarMulIstanbul implements a native elliptic curve scalar multiplication conforming to Istanbul consensus rules.

bn256PairingIstanbul

  • Address 0x0000000000000000000000000000000000000008

    bn256PairingIstanbul implements a pairing pre-compile for the bn256 curve conforming to Istanbul consensus rules.

store

  • Address 0x000068656164657273746F726541646472657373

    execute atlas header store contract

verify

  • Address 0x0000000000747856657269667941646472657373

    RunTxVerify execute atlas tx verify contract

transfer

  • Address 0x00000000000000000000000000000000000000fd

    Native transfer contract to make Atlas Gold ERC20 compatible.

fractionMulExp

  • Address 0x00000000000000000000000000000000000000fc

    computes a * (b ^ exponent) to decimals places of precision, where a and b are fractions

proofOfPossession

  • Address 0x00000000000000000000000000000000000000fb

    verify validator`s address 、publicKey、g1publickey、signature

getValidator

  • Address 0x00000000000000000000000000000000000000fa

    Return the validators that are required to sign the given, possibly unsealed, block number. If this block is the last in an epoch, note that that may mean one or more of those validators may no longer be elected for subsequent blocks. WARNING: Validator set is always constructed from the canonical chain, therefore this precompile is undefined if the engine is aware of a chain with higher total difficulty.

numberValidators

  • Address 0x00000000000000000000000000000000000000f9

    Return the number of validators that are required to sign this current, possibly unsealed, block. If this block is the last in an epoch, note that that may mean one or more of those validators may no longer be elected for subsequent blocks.

epochSize

  • Address 0x00000000000000000000000000000000000000f8

    return the epochSize

blockNumberFromHeader

  • Address 0x00000000000000000000000000000000000000f7

    return the blockNumber from header

hashHeader

  • Address 0x00000000000000000000000000000000000000f6

    return the hashHeader from header

getParentSealBitmap

  • Address 0x00000000000000000000000000000000000000F5

    Return the signer bitmap from the parent seal of a past block in the chain. Requested parent seal must have occurred within 4 epochs of the current block number.

getVerifiedSealBitmap

  • Address 0x00000000000000000000000000000000000000F4

    rerurn the extra.AggregatedSeal.Bitmap from header

ed25519Verify

  • Address 0x00000000000000000000000000000000000000f3

    ed25519Verify implements a native Ed25519 signature verification.

BLS12_G1ADD

  • Address 0x000000000000000000000000000000000000000a

    Implements EIP-2537 G1Add precompile.

    > G1 addition call expects 256 bytes as an input that is interpreted as byte concatenation of two G1 points (128 bytes each).

    > Output is an encoding of addition operation result - single G1 point (128 bytes).

BLS12_G1MUL

  • Address 0x000000000000000000000000000000000000000b

    Implements EIP-2537 G1Mul precompile.

    > G1 multiplication call expects 160 bytes as an input that is interpreted as byte concatenation of encoding of G1 point (128 bytes) and encoding of a scalar value (32 bytes).

    > Output is an encoding of multiplication operation result - single G1 point(128 bytes).

BLS12_G1MULTIEXP

  • Address 0x000000000000000000000000000000000000000c

    Implements EIP-2537 G1MultiExp precompile.

    G1 multiplication call expects 160*k bytes as an input that is interpreted as byte concatenation of k slices each of them being a byte concatenation of encoding of G1 point (128 bytes) and encoding of a scalar value (32 bytes).

    Output is an encoding of multiexponentiation operation result - single G1 point (128 bytes).

BLS12_G2ADD

  • Address 0x000000000000000000000000000000000000000d

    Implements EIP-2537 G2Add precompile.

    > G2 addition call expects 512 bytes as an input that is interpreted as byte concatenation of two G2 points (256 bytes each).

    > Output is an encoding of addition operation result - single G2 point (256 bytes).

BLS12_G2MUL

  • Address 0x000000000000000000000000000000000000000e

    Implements EIP-2537 G2MUL precompile logic.

    > G2 multiplication call expects 288 bytes as an input that is interpreted as byte concatenation of encoding of G2 point (256 bytes) and encoding of a scalar value (32 bytes).

    > Output is an encoding of multiplication operation result - single G2 point (256 bytes).

BLS12_G2MULTIEXP

  • Address 0x000000000000000000000000000000000000000f

    Implements EIP-2537 G2MultiExp precompile logic

    > G2 multiplication call expects 288*k bytes as an input that is interpreted as byte concatenation of k slices each of them being a byte concatenation of encoding of G2 point (256 bytes) and encoding of a scalar value (32 bytes).

    > Output is an encoding of multiexponentiation operation result - single G2 point (256 bytes).

BLS12_PAIRING

  • Address 0x0000000000000000000000000000000000000010

    Implements EIP-2537 Pairing precompile logic.

    > Pairing call expects 384*k bytes as an inputs that is interpreted as byte concatenation of k slices. Each slice has the following structure:

    > - 128 bytes of G1 point encoding

    > - 256 bytes of G2 point encoding

    > Output is a 32 bytes where last single byte is 0x01 if pairing result is equal to multiplicative identity in a pairing target field and 0x00 otherwise

    > (which is equivalent of Big Endian encoding of Solidity values uint256(1) and uin256(0) respectively).

BLS12_MAP_FP_TO_G1

  • Address 0x0000000000000000000000000000000000000011

Implements EIP-2537 Map_To_G1 precompile.

> Field-to-curve call expects 64 bytes an an input that is interpreted as a an element of the base field.

> Output of this call is 128 bytes and is G1 point following respective encoding rules.

BLS12_MAP_FP2_TO_G2

  • Address 0x0000000000000000000000000000000000000012

    Implements EIP-2537 Map_FP2_TO_G2 precompile logic.

    > Field-to-curve call expects 128 bytes an an input that is interpreted as a an element of the quadratic extension field.

    > Output of this call is 256 bytes and is G2 point following respective encoding rules.

Last updated

Was this helpful?