Skip to content

Commit

Permalink
add testcases for eth_shang precompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony committed Mar 19, 2024
1 parent d9b0b85 commit 3f3de17
Show file tree
Hide file tree
Showing 6 changed files with 409 additions and 40 deletions.
91 changes: 52 additions & 39 deletions vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,39 +49,19 @@ 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]PrecompiledContract{
common.BytesToAddress([]byte{1}): &safe_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{},
common.BytesToAddress([]byte{9}): &blake2F{},
}

// EIP-152 test vectors
var blake2FMalformedInputTests = []precompiledFailureTest{
{
Input: "",
ExpectedError: errBlake2FInvalidInputLength.Error(),
Name: "vector 0: empty input",
},
{
Input: "00000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001",
ExpectedError: errBlake2FInvalidInputLength.Error(),
Name: "vector 1: less than 213 bytes input",
},
{
Input: "000000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001",
ExpectedError: errBlake2FInvalidInputLength.Error(),
Name: "vector 2: more than 213 bytes input",
},
{
Input: "0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000002",
ExpectedError: errBlake2FInvalidFinalFlag.Error(),
Name: "vector 3: malformed final block indicator flag",
},
common.BytesToAddress([]byte{1}): &safe_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}): &bn256Add{eip1108: false},
common.BytesToAddress([]byte{0xf6}): &bn256Add{eip1108: true},
common.BytesToAddress([]byte{7}): &bn256ScalarMul{eip1108: false},
common.BytesToAddress([]byte{0xf7}): &bn256ScalarMul{eip1108: true},
common.BytesToAddress([]byte{8}): &bn256Pairing{eip1108: false},
common.BytesToAddress([]byte{0xf8}): &bn256Pairing{eip1108: true},
common.BytesToAddress([]byte{9}): &blake2F{},
}

func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
Expand Down Expand Up @@ -236,10 +216,16 @@ func BenchmarkPrecompiledIdentity(bench *testing.B) {
func TestPrecompiledModExp(t *testing.T) { testJson("modexp", "05", t) }
func BenchmarkPrecompiledModExp(b *testing.B) { benchJson("modexp", "05", b) }

func TestPrecompiledModExpEip2565(t *testing.T) { testJson("modexp_eip2565", "f5", t) }
func BenchmarkPrecompiledModExpEip2565(b *testing.B) { benchJson("modexp_eip2565", "f5", b) }

// Tests the sample inputs from the elliptic curve addition EIP 213.
func TestPrecompiledBn256Add(t *testing.T) { testJson("bn256Add", "06", t) }
func BenchmarkPrecompiledBn256Add(b *testing.B) { benchJson("bn256Add", "06", b) }

func TestPrecompiledBn256AddEip1108(t *testing.T) { testJson("bn256Add_eip1108", "f6", t) }
func BenchmarkPrecompiledBn256AddEip1108(b *testing.B) { benchJson("bn256Add_eip1108", "f6", b) }

// Tests OOG
func TestPrecompiledModExpOOG(t *testing.T) {
modexpTests, err := loadJson("modexp")
Expand All @@ -255,21 +241,28 @@ func TestPrecompiledModExpOOG(t *testing.T) {
func TestPrecompiledBn256ScalarMul(t *testing.T) { testJson("bn256ScalarMul", "07", t) }
func BenchmarkPrecompiledBn256ScalarMul(b *testing.B) { benchJson("bn256ScalarMul", "07", b) }

func TestPrecompiledBn256ScalarMulEip1108(t *testing.T) { testJson("bn256ScalarMul_eip1108", "f7", t) }
func BenchmarkPrecompiledBn256ScalarMulEip1108(b *testing.B) {
benchJson("bn256ScalarMul_eip1108", "f7", b)
}

// Tests the sample inputs from the elliptic curve pairing check EIP 197.
func TestPrecompiledBn256Pairing(t *testing.T) { testJson("bn256Pairing", "08", t) }
func BenchmarkPrecompiledBn256Pairing(b *testing.B) { benchJson("bn256Pairing", "08", b) }

func TestPrecompiledBn256PairingEip1108(t *testing.T) { testJson("bn256Pairing_eip1108", "f8", t) }
func BenchmarkPrecompiledBn256PairingEip1108(b *testing.B) {
benchJson("bn256Pairing_eip1108", "f8", b)
}

func TestPrecompiledBlake2F(t *testing.T) { testJson("blake2F", "09", t) }
func BenchmarkPrecompiledBlake2F(b *testing.B) { benchJson("blake2F", "09", b) }

func TestPrecompileBlake2FMalformedInput(t *testing.T) {
for _, test := range blake2FMalformedInputTests {
testPrecompiledFailure("09", test, t)
}
}

func TestPrecompiledEcrecover(t *testing.T) { testJson("ecRecover", "01", t) }

// Failure tests
func TestPrecompiledBlake2FFailure(t *testing.T) { testJsonFail("blake2F", "09", t) }

func testJson(name, addr string, t *testing.T) {
tests, err := loadJson(name)
if err != nil {
Expand All @@ -280,6 +273,16 @@ func testJson(name, addr string, t *testing.T) {
}
}

func testJsonFail(name, addr string, t *testing.T) {
tests, err := loadJsonFail(name)
if err != nil {
t.Fatal(err)
}
for _, test := range tests {
testPrecompiledFailure(addr, test, t)
}
}

func benchJson(name, addr string, b *testing.B) {
tests, err := loadJson(name)
if err != nil {
Expand All @@ -300,6 +303,16 @@ func loadJson(name string) ([]precompiledTest, error) {
return testcases, err
}

func loadJsonFail(name string) ([]precompiledFailureTest, error) {
data, err := os.ReadFile(fmt.Sprintf("testdata/precompiles/fail-%v.json", name))
if err != nil {
return nil, err
}
var testcases []precompiledFailureTest
err = json.Unmarshal(data, &testcases)
return testcases, err
}

func TestAsDelegate(t *testing.T) {
// Mock addresses
parentCallerAddress := common.HexToAddress("0x01")
Expand Down
2 changes: 1 addition & 1 deletion vm/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
)

func GetNewInterpreter(jumpTable JumpTable) *Interpreter {
func GetNewInterpreter(jumpTable *JumpTable) *Interpreter {
statedb := NoopStateDB{}

evmConfig := Config{
Expand Down
114 changes: 114 additions & 0 deletions vm/testdata/precompiles/bn256Add_eip1108.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
[
{
"Input": "18b18acfb4c2c30276db5411368e7185b311dd124691610c5d3b74034e093dc9063c909c4720840cb5134cb9f59fa749755796819658d32efc0d288198f3726607c2b7f58a84bd6145f00c9c2bc0bb1a187f20ff2c92963a88019e7c6a014eed06614e20c147e940f2d70da3f74c9a17df361706a4485c742bd6788478fa17d7",
"Expected": "2243525c5efd4b9c3d3c45ac0ca3fe4dd85e830a4ce6b65fa1eeaee202839703301d1d33be6da8e509df21cc35964723180eed7532537db9ae5e7d48f195c915",
"Name": "chfast1",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "2243525c5efd4b9c3d3c45ac0ca3fe4dd85e830a4ce6b65fa1eeaee202839703301d1d33be6da8e509df21cc35964723180eed7532537db9ae5e7d48f195c91518b18acfb4c2c30276db5411368e7185b311dd124691610c5d3b74034e093dc9063c909c4720840cb5134cb9f59fa749755796819658d32efc0d288198f37266",
"Expected": "2bd3e6d0f3b142924f5ca7b49ce5b9d54c4703d7ae5648e61d02268b1a0a9fb721611ce0a6af85915e2f1d70300909ce2e49dfad4a4619c8390cae66cefdb204",
"Name": "chfast2",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Name": "cdetrio1",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Name": "cdetrio2",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Name": "cdetrio3",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "",
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Name": "cdetrio4",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Name": "cdetrio5",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Expected": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Name": "cdetrio6",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Name": "cdetrio7",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Expected": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Name": "cdetrio8",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Gas": 150,
"Name": "cdetrio9",
"NoBenchmark": false
},
{
"Input": "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Gas": 150,
"Name": "cdetrio10",
"NoBenchmark": false
},
{
"Input": "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Expected": "030644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd315ed738c0e0a7c92e7845f96b2ae9c0a68a6a449e3538fc7ff3ebf7a5a18a2c4",
"Name": "cdetrio11",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "030644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd315ed738c0e0a7c92e7845f96b2ae9c0a68a6a449e3538fc7ff3ebf7a5a18a2c4",
"Name": "cdetrio12",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "17c139df0efee0f766bc0204762b774362e4ded88953a39ce849a8a7fa163fa901e0559bacb160664764a357af8a9fe70baa9258e0b959273ffc5718c6d4cc7c039730ea8dff1254c0fee9c0ea777d29a9c710b7e616683f194f18c43b43b869073a5ffcc6fc7a28c30723d6e58ce577356982d65b833a5a5c15bf9024b43d98",
"Expected": "15bf2bb17880144b5d1cd2b1f46eff9d617bffd1ca57c37fb5a49bd84e53cf66049c797f9ce0d17083deb32b5e36f2ea2a212ee036598dd7624c168993d1355f",
"Name": "cdetrio13",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "17c139df0efee0f766bc0204762b774362e4ded88953a39ce849a8a7fa163fa901e0559bacb160664764a357af8a9fe70baa9258e0b959273ffc5718c6d4cc7c17c139df0efee0f766bc0204762b774362e4ded88953a39ce849a8a7fa163fa92e83f8d734803fc370eba25ed1f6b8768bd6d83887b87165fc2434fe11a830cb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Name": "cdetrio14",
"Gas": 150,
"NoBenchmark": false
}
]
Loading

0 comments on commit 3f3de17

Please sign in to comment.