Skip to content

Commit

Permalink
feat: rm precompiles (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaspitz authored Apr 23, 2024
1 parent 5359b90 commit 1669243
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 236 deletions.
126 changes: 57 additions & 69 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,93 +44,81 @@ type PrecompiledContract interface {

// PrecompiledContractsHomestead contains the default set of pre-compiled Ethereum
// contracts used in the Frontier and Homestead releases.
var PrecompiledContractsHomestead = map[common.Address]PrecompiledContractWithCtx{
common.BytesToAddress([]byte{1}): precompileWrapper{&ecrecover{}},
common.BytesToAddress([]byte{2}): precompileWrapper{&sha256hash{}},
common.BytesToAddress([]byte{3}): precompileWrapper{&ripemd160hash{}},
common.BytesToAddress([]byte{4}): precompileWrapper{&dataCopy{}},
common.BytesToAddress([]byte{0x89}): &mint{},
common.BytesToAddress([]byte{0x90}): &burn{},
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]PrecompiledContractWithCtx{
common.BytesToAddress([]byte{1}): precompileWrapper{&ecrecover{}},
common.BytesToAddress([]byte{2}): precompileWrapper{&sha256hash{}},
common.BytesToAddress([]byte{3}): precompileWrapper{&ripemd160hash{}},
common.BytesToAddress([]byte{4}): precompileWrapper{&dataCopy{}},
common.BytesToAddress([]byte{5}): precompileWrapper{&bigModExp{eip2565: false}},
common.BytesToAddress([]byte{6}): precompileWrapper{&bn256AddByzantium{}},
common.BytesToAddress([]byte{7}): precompileWrapper{&bn256ScalarMulByzantium{}},
common.BytesToAddress([]byte{8}): precompileWrapper{&bn256PairingByzantium{}},
common.BytesToAddress([]byte{0x89}): &mint{},
common.BytesToAddress([]byte{0x90}): &burn{},
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{eip2565: false},
common.BytesToAddress([]byte{6}): &bn256AddByzantium{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulByzantium{},
common.BytesToAddress([]byte{8}): &bn256PairingByzantium{},
}

// PrecompiledContractsIstanbul contains the default set of pre-compiled Ethereum
// contracts used in the Istanbul release.
var PrecompiledContractsIstanbul = map[common.Address]PrecompiledContractWithCtx{
common.BytesToAddress([]byte{1}): precompileWrapper{&ecrecover{}},
common.BytesToAddress([]byte{2}): precompileWrapper{&sha256hash{}},
common.BytesToAddress([]byte{3}): precompileWrapper{&ripemd160hash{}},
common.BytesToAddress([]byte{4}): precompileWrapper{&dataCopy{}},
common.BytesToAddress([]byte{5}): precompileWrapper{&bigModExp{eip2565: false}},
common.BytesToAddress([]byte{6}): precompileWrapper{&bn256AddIstanbul{}},
common.BytesToAddress([]byte{7}): precompileWrapper{&bn256ScalarMulIstanbul{}},
common.BytesToAddress([]byte{8}): precompileWrapper{&bn256PairingIstanbul{}},
common.BytesToAddress([]byte{9}): precompileWrapper{&blake2F{}},
common.BytesToAddress([]byte{0x89}): &mint{},
common.BytesToAddress([]byte{0x90}): &burn{},
var PrecompiledContractsIstanbul = 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{eip2565: false},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
}

// PrecompiledContractsBerlin contains the default set of pre-compiled Ethereum
// contracts used in the Berlin release.
var PrecompiledContractsBerlin = map[common.Address]PrecompiledContractWithCtx{
common.BytesToAddress([]byte{1}): precompileWrapper{&ecrecover{}},
common.BytesToAddress([]byte{2}): precompileWrapper{&sha256hash{}},
common.BytesToAddress([]byte{3}): precompileWrapper{&ripemd160hash{}},
common.BytesToAddress([]byte{4}): precompileWrapper{&dataCopy{}},
common.BytesToAddress([]byte{5}): precompileWrapper{&bigModExp{eip2565: true}},
common.BytesToAddress([]byte{6}): precompileWrapper{&bn256AddIstanbul{}},
common.BytesToAddress([]byte{7}): precompileWrapper{&bn256ScalarMulIstanbul{}},
common.BytesToAddress([]byte{8}): precompileWrapper{&bn256PairingIstanbul{}},
common.BytesToAddress([]byte{9}): precompileWrapper{&blake2F{}},
common.BytesToAddress([]byte{0x89}): &mint{},
common.BytesToAddress([]byte{0x90}): &burn{},
var PrecompiledContractsBerlin = 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{eip2565: true},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
}

// PrecompiledContractsCancun contains the default set of pre-compiled Ethereum
// contracts used in the Cancun release.
var PrecompiledContractsCancun = map[common.Address]PrecompiledContractWithCtx{
common.BytesToAddress([]byte{1}): precompileWrapper{&ecrecover{}},
common.BytesToAddress([]byte{2}): precompileWrapper{&sha256hash{}},
common.BytesToAddress([]byte{3}): precompileWrapper{&ripemd160hash{}},
common.BytesToAddress([]byte{4}): precompileWrapper{&dataCopy{}},
common.BytesToAddress([]byte{5}): precompileWrapper{&bigModExp{eip2565: true}},
common.BytesToAddress([]byte{6}): precompileWrapper{&bn256AddIstanbul{}},
common.BytesToAddress([]byte{7}): precompileWrapper{&bn256ScalarMulIstanbul{}},
common.BytesToAddress([]byte{8}): precompileWrapper{&bn256PairingIstanbul{}},
common.BytesToAddress([]byte{9}): precompileWrapper{&blake2F{}},
common.BytesToAddress([]byte{0x0a}): precompileWrapper{&kzgPointEvaluation{}},
common.BytesToAddress([]byte{0x89}): &mint{},
common.BytesToAddress([]byte{0x90}): &burn{},
var PrecompiledContractsCancun = 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{eip2565: true},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},
}

// PrecompiledContractsBLS contains the set of pre-compiled Ethereum
// contracts specified in EIP-2537. These are exported for testing purposes.
var PrecompiledContractsBLS = map[common.Address]PrecompiledContractWithCtx{
common.BytesToAddress([]byte{10}): precompileWrapper{&bls12381G1Add{}},
common.BytesToAddress([]byte{11}): precompileWrapper{&bls12381G1Mul{}},
common.BytesToAddress([]byte{12}): precompileWrapper{&bls12381G1MultiExp{}},
common.BytesToAddress([]byte{13}): precompileWrapper{&bls12381G2Add{}},
common.BytesToAddress([]byte{14}): precompileWrapper{&bls12381G2Mul{}},
common.BytesToAddress([]byte{15}): precompileWrapper{&bls12381G2MultiExp{}},
common.BytesToAddress([]byte{16}): precompileWrapper{&bls12381Pairing{}},
common.BytesToAddress([]byte{17}): precompileWrapper{&bls12381MapG1{}},
common.BytesToAddress([]byte{18}): precompileWrapper{&bls12381MapG2{}},
common.BytesToAddress([]byte{0x89}): &mint{},
common.BytesToAddress([]byte{0x90}): &burn{},
var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{10}): &bls12381G1Add{},
common.BytesToAddress([]byte{11}): &bls12381G1Mul{},
common.BytesToAddress([]byte{12}): &bls12381G1MultiExp{},
common.BytesToAddress([]byte{13}): &bls12381G2Add{},
common.BytesToAddress([]byte{14}): &bls12381G2Mul{},
common.BytesToAddress([]byte{15}): &bls12381G2MultiExp{},
common.BytesToAddress([]byte{16}): &bls12381Pairing{},
common.BytesToAddress([]byte{17}): &bls12381MapG1{},
common.BytesToAddress([]byte{18}): &bls12381MapG2{},
}

var (
Expand Down Expand Up @@ -180,13 +168,13 @@ func ActivePrecompiles(rules params.Rules) []common.Address {
// - the returned bytes,
// - the _remaining_ gas,
// - any error that occurred
func RunPrecompiledContract(p PrecompiledContractWithCtx, input []byte, suppliedGas uint64, ctx *precompileContext) (ret []byte, remainingGas uint64, err error) {
func RunPrecompiledContract(p PrecompiledContract, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) {
gasCost := p.RequiredGas(input)
if suppliedGas < gasCost {
return nil, 0, ErrOutOfGas
}
gasLeft := suppliedGas - gasCost
output, err := p.Run(input, ctx)
output, err := p.Run(input)
return output, gasLeft, err
}

Expand Down
2 changes: 1 addition & 1 deletion core/vm/contracts_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func FuzzPrecompiledContracts(f *testing.F) {
}
inWant := string(input)

RunPrecompiledContract(p, input, gas, NewContext(common.HexToAddress("1337"), mockEVM))
RunPrecompiledContract(p, input, gas)
if inHave := string(input); inWant != inHave {
t.Errorf("Precompiled %v modified input data", a)
}
Expand Down
52 changes: 26 additions & 26 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,28 @@ type precompiledFailureTest struct {

// allPrecompiles does not map to the actual set of precompiles, as it also contains
// repriced versions of precompiles at certain slots
var allPrecompiles = map[common.Address]PrecompiledContractWithCtx{
common.BytesToAddress([]byte{1}): precompileWrapper{&ecrecover{}},
common.BytesToAddress([]byte{2}): precompileWrapper{&sha256hash{}},
common.BytesToAddress([]byte{3}): precompileWrapper{&ripemd160hash{}},
common.BytesToAddress([]byte{4}): precompileWrapper{&dataCopy{}},
common.BytesToAddress([]byte{5}): precompileWrapper{&bigModExp{eip2565: false}},
common.BytesToAddress([]byte{0xf5}): precompileWrapper{&bigModExp{eip2565: true}},
common.BytesToAddress([]byte{6}): precompileWrapper{&bn256AddIstanbul{}},
common.BytesToAddress([]byte{7}): precompileWrapper{&bn256ScalarMulIstanbul{}},
common.BytesToAddress([]byte{8}): precompileWrapper{&bn256PairingIstanbul{}},
common.BytesToAddress([]byte{9}): precompileWrapper{&blake2F{}},
common.BytesToAddress([]byte{0x0a}): precompileWrapper{&kzgPointEvaluation{}},

common.BytesToAddress([]byte{0x0f, 0x0a}): precompileWrapper{&bls12381G1Add{}},
common.BytesToAddress([]byte{0x0f, 0x0b}): precompileWrapper{&bls12381G1Mul{}},
common.BytesToAddress([]byte{0x0f, 0x0c}): precompileWrapper{&bls12381G1MultiExp{}},
common.BytesToAddress([]byte{0x0f, 0x0d}): precompileWrapper{&bls12381G2Add{}},
common.BytesToAddress([]byte{0x0f, 0x0e}): precompileWrapper{&bls12381G2Mul{}},
common.BytesToAddress([]byte{0x0f, 0x0f}): precompileWrapper{&bls12381G2MultiExp{}},
common.BytesToAddress([]byte{0x0f, 0x10}): precompileWrapper{&bls12381Pairing{}},
common.BytesToAddress([]byte{0x0f, 0x11}): precompileWrapper{&bls12381MapG1{}},
common.BytesToAddress([]byte{0x0f, 0x12}): precompileWrapper{&bls12381MapG2{}},
var allPrecompiles = 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{eip2565: false},
common.BytesToAddress([]byte{0xf5}): &bigModExp{eip2565: true},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},

common.BytesToAddress([]byte{0x0f, 0x0a}): &bls12381G1Add{},
common.BytesToAddress([]byte{0x0f, 0x0b}): &bls12381G1Mul{},
common.BytesToAddress([]byte{0x0f, 0x0c}): &bls12381G1MultiExp{},
common.BytesToAddress([]byte{0x0f, 0x0d}): &bls12381G2Add{},
common.BytesToAddress([]byte{0x0f, 0x0e}): &bls12381G2Mul{},
common.BytesToAddress([]byte{0x0f, 0x0f}): &bls12381G2MultiExp{},
common.BytesToAddress([]byte{0x0f, 0x10}): &bls12381Pairing{},
common.BytesToAddress([]byte{0x0f, 0x11}): &bls12381MapG1{},
common.BytesToAddress([]byte{0x0f, 0x12}): &bls12381MapG2{},
}

// EIP-152 test vectors
Expand Down Expand Up @@ -98,7 +98,7 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
in := common.Hex2Bytes(test.Input)
gas := p.RequiredGas(in)
t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
res, _, err := RunPrecompiledContract(p, in, gas, NewContext(common.HexToAddress("1337"), mockEVM))
res, _, err := RunPrecompiledContract(p, in, gas)
if err != nil {
t.Error(err)
} else if common.Bytes2Hex(res) != test.Expected {
Expand All @@ -121,7 +121,7 @@ func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) {
gas := p.RequiredGas(in) - 1

t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
_, _, err := RunPrecompiledContract(p, in, gas, NewContext(common.HexToAddress("1337"), mockEVM))
_, _, err := RunPrecompiledContract(p, in, gas)
if err.Error() != "out of gas" {
t.Errorf("Expected error [out of gas], got [%v]", err)
}
Expand All @@ -138,7 +138,7 @@ func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing
in := common.Hex2Bytes(test.Input)
gas := p.RequiredGas(in)
t.Run(test.Name, func(t *testing.T) {
_, _, err := RunPrecompiledContract(p, in, gas, NewContext(common.HexToAddress("1337"), mockEVM))
_, _, err := RunPrecompiledContract(p, in, gas)
if err.Error() != test.ExpectedError {
t.Errorf("Expected error [%v], got [%v]", test.ExpectedError, err)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
copy(data, in)
res, _, err = RunPrecompiledContract(p, data, reqGas, NewContext(common.HexToAddress("1337"), mockEVM))
res, _, err = RunPrecompiledContract(p, data, reqGas)
}
bench.StopTimer()
elapsed := uint64(time.Since(start))
Expand Down
134 changes: 0 additions & 134 deletions core/vm/contracts_with_ctx.go

This file was deleted.

Loading

0 comments on commit 1669243

Please sign in to comment.