Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement all precompiled contracts #122

Closed
8 tasks
zah opened this issue Aug 28, 2018 · 10 comments
Closed
8 tasks

Implement all precompiled contracts #122

zah opened this issue Aug 28, 2018 · 10 comments

Comments

@zah
Copy link
Contributor

zah commented Aug 28, 2018

We need implementations and dispatching logic for the following precompiles

Introduced in Homestead:

  • 0x1 ecrecover
  • 0x2 sha256
  • 0x3 ripemd160
  • 0x4 identity

Introduced in Byzantium:

  • 0x5 modexp
  • 0x6 ecadd
  • 0x7 ecmul
  • 0x8 ecpairing

Reference implementations:
https://github.com/ethereum/py-evm/tree/master/eth/precompiles

@cheatfate
Copy link
Contributor

cheatfate commented Aug 28, 2018

  1. sha256 and ripemd160 is pretty easy to implement because nimcrypto has both primitives.
  2. ecrecover is based on secp256k1 curve so we already have everything to implement it.
  3. identity is not so hard too.
  4. modexp can be implemented in stint.

But
ecadd, ecmul and ecpairing is based on BN128 curve, which we don't have.

@mratsim
Copy link
Contributor

mratsim commented Aug 29, 2018

modexp is called powmod in Stint and already available: https://github.com/status-im/nim-stint/blob/master/stint/modular_arithmetic.nim#L113.

For BN128 the fastest ways are in order:

  • reuse Aleth/Cpp-ethereum lib (devcrypto?)
  • Find another C/C++ lib
  • wrap the primitives from Milagro crypto similar to BLS12-381
  • DIY

@mratsim
Copy link
Contributor

mratsim commented Aug 29, 2018

After reviewing on precompiles.

  1. Precompiles live at specified addresses on the blockchain: https://github.com/ethereum/go-ethereum/blob/702b8a7aec97c7ef512335ee44c925bc91c6ae09/core/vm/contracts.go#L40-L60
// PrecompiledContractsHomestead contains the default set of pre-compiled Ethereum
// contracts used in the Frontier and Homestead releases.
var PrecompiledContractsHomestead = map[common.Address]PrecompiledContract{
	common.BytesToAddress([]byte{1}): &ecrecover{},
	common.BytesToAddress([]byte{2}): &sha256hash{},
	common.BytesToAddress([]byte{3}): &ripemd160hash{},
	common.BytesToAddress([]byte{4}): &dataCopy{},
}

// PrecompiledContractsByzantium contains the default set of pre-compiled Ethereum
// contracts used in the Byzantium release.
var PrecompiledContractsByzantium = map[common.Address]PrecompiledContract{
	common.BytesToAddress([]byte{1}): &ecrecover{},
	common.BytesToAddress([]byte{2}): &sha256hash{},
	common.BytesToAddress([]byte{3}): &ripemd160hash{},
	common.BytesToAddress([]byte{4}): &dataCopy{},
	common.BytesToAddress([]byte{5}): &bigModExp{},
	common.BytesToAddress([]byte{6}): &bn256Add{},
	common.BytesToAddress([]byte{7}): &bn256ScalarMul{},
	common.BytesToAddress([]byte{8}): &bn256Pairing{},
}

So 0x0000...0001 for ecrecover for example

  1. CALL opcode must catch calls to those addresses and dispatch to the proper precompile, see https://github.com/ethereum/go-ethereum/blob/c1c003e4ff36c22d67662ca661fc78cde850d401/core/vm/evm.go#L163-L225 and https://github.com/ethereum/go-ethereum/blob/c1c003e4ff36c22d67662ca661fc78cde850d401/core/vm/evm.go#L43-L68

For reference here are some discussions, including BN128 C++ implementation concerns:

  1. They are implemented internally and not through EVM native ops for efficiency and gas costs reasons.

@cheatfate
Copy link
Contributor

If we are limited in time on BN128 i will vote on wrap the primitives from Milagro crypto similar to BLS12-381.

@mratsim
Copy link
Contributor

mratsim commented Aug 29, 2018

After looking into BN128 with @cheatfate the current situation is somewhat strange:

We don't know if we should use BN128 or BN256 (Note that the data will be stored in a 256-bit integer regardless)

Facts:

Also BN128 is not supported in Milagro while BN256 is https://github.com/milagro-crypto/amcl/blob/master/version3/curves.txt#L26

@coffeepots
Copy link
Contributor

ecrecover is implemented already too, but needs testing to make sure it's correct.

@mratsim
Copy link
Contributor

mratsim commented Aug 30, 2018

Useful repo, a fuzzer for Ethereum BN256 implementations: https://github.com/guidovranken/bn256-fuzzing

@cheatfate
Copy link
Contributor

@mratsim
Copy link
Contributor

mratsim commented Sep 18, 2018

Geth uses Cloudflare implementation according to EIP 1108

Still no test vectors though.

@tersec
Copy link
Contributor

tersec commented Sep 20, 2018

To the extent it might help prioritize things, the stPreCompiledContracts2 section of https://github.com/status-im/nimbus/blob/master/GeneralStateTests.md shows which of these are tested by currently running tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants