ref-fvm icon indicating copy to clipboard operation
ref-fvm copied to clipboard

EVM runtime: handle Ethereum precompiles

Open raulk opened this issue 3 years ago • 3 comments

Current situation

  • Docs on precompiled contracts: https://www.evm.codes/precompiled
  • evmodin (implementation that fvm-evm is currently based on) is agnostic of precompiles.
  • Akula (Ethereum client using evmodin, built by the same dev) handles them at the client level: https://github.com/akula-bft/akula/blob/c5694cda658880898953187ba52d6249e37527ad/src/execution/evmglue.rs#L303-L330
  • evmodin just relays the CALL to the client, who then realises that it’s a static address and handles the native call.

FEVM solution

In FEVM, we will need to handle the precompile inside the EVM actor by:

  1. Identifying that it's a call to a precompiled contract.
  2. Parsing the params, like revm does here for ecrecover (for example): https://github.com/bluealloy/revm/blob/main/crates/revm_precompiles/src/secp256k1.rs#L62.
  3. Calling the appropriate syscall (hash, ecrecover, etc.) -- or performing the work in Wasm if able (e.g. maybe for the identity precompile)
  4. Forming the return value
  5. Applying the right Ethereum gas charge
  6. Handling errors

Precompiled contracts to support

We don't need to honour the Ethereum precompile history. We can introduce all precompiles that exist today.

  • [ ] Support Ethereum precompile 0x01: ecrecover
    • Requires #627
  • [ ] Support Ethereum precompile 0x02: sha256
    • Requires extending the hash syscall to add support for 256
  • [ ] Support Ethereum precompile 0x03: ripemd160
    • Requires the above + adding support for ripemd160 in multihash
  • [ ] Support Ethereum precompile 0x04: identity
    • Can likely be resolved inside Wasm as we use bulk_memory?
  • [ ] Support Ethereum precompile 0x05: modexp
    • TBD
  • [ ] Support Ethereum precompile 0x06: ecAdd
    • TBD
  • [ ] Support Ethereum precompile 0x07: ecMul
    • TBD
  • [ ] Support Ethereum precompile 0x08: ecPairing
    • TBD
  • [ ] Support Ethereum precompile 0x09: blake2f
    • Can likely use the existing hash syscall as-is.

raulk avatar Aug 08 '22 13:08 raulk

https://github.com/bluealloy/revm has support for precompiles, although they live in a different crate, and we'd have to reimplement their logic so they call a syscall. We have two options:

  1. Implement all of this in our fork of evmodin
  2. Switch to revm (and possibly only implement the logic, but not the glue code?)

@Stebalien -- thoughts?

raulk avatar Aug 08 '22 13:08 raulk

I think we're just going to have to implement them ourselves. We wouldn't get much from REVM because we usually want to lower to FVM syscalls.

Support Ethereum precompile 0x06: ecAdd

(and friends)

Will probably use something like https://github.com/paritytech/bn. At least for the pairing part.

Support Ethereum precompile 0x05: modexp

We'll have to benchmark this. We can use num, but the question is whether it will be faster inside wasm or outside.

Stebalien avatar Aug 08 '22 21:08 Stebalien

I agree with steb

karim-agha avatar Aug 09 '22 02:08 karim-agha

https://github.com/filecoin-project/builtin-actors/pull/564 implements all precompiles, unit tests are to be done before this tracking issue is closed however

mriise avatar Sep 02 '22 17:09 mriise

Tracking unit tests in https://github.com/filecoin-project/ref-fvm/issues/842, so we can close this issue for the Selenium release.

raulk avatar Sep 06 '22 09:09 raulk