From 81d273c29dd97391c5c069ee19cce4578b905e9f Mon Sep 17 00:00:00 2001 From: Andrei Maiboroda Date: Mon, 29 Jul 2019 18:34:53 +0200 Subject: [PATCH] Unit tests for EC arithmetics cost change --- libethashseal/genesis/test/istanbulTest.cpp | 4 +- .../genesis/test/istanbulTransitionTest.cpp | 4 +- test/unittests/libethcore/PrecompiledTest.cpp | 68 +++++++++++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/libethashseal/genesis/test/istanbulTest.cpp b/libethashseal/genesis/test/istanbulTest.cpp index 738b0b987f1..816a5bb3075 100644 --- a/libethashseal/genesis/test/istanbulTest.cpp +++ b/libethashseal/genesis/test/istanbulTest.cpp @@ -46,8 +46,8 @@ R"E( "0000000000000000000000000000000000000003": { "precompiled": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } }, "0000000000000000000000000000000000000004": { "precompiled": { "name": "identity", "linear": { "base": 15, "word": 3 } } }, "0000000000000000000000000000000000000005": { "precompiled": { "name": "modexp" } }, - "0000000000000000000000000000000000000006": { "precompiled": { "name": "alt_bn128_G1_add", "linear": { "base": 500, "word": 0 } } }, - "0000000000000000000000000000000000000007": { "precompiled": { "name": "alt_bn128_G1_mul", "linear": { "base": 40000, "word": 0 } } }, + "0000000000000000000000000000000000000006": { "precompiled": { "name": "alt_bn128_G1_add" } }, + "0000000000000000000000000000000000000007": { "precompiled": { "name": "alt_bn128_G1_mul" } }, "0000000000000000000000000000000000000008": { "precompiled": { "name": "alt_bn128_pairing_product" } } } } diff --git a/libethashseal/genesis/test/istanbulTransitionTest.cpp b/libethashseal/genesis/test/istanbulTransitionTest.cpp index ad5597f6cfa..1a865e76e6c 100644 --- a/libethashseal/genesis/test/istanbulTransitionTest.cpp +++ b/libethashseal/genesis/test/istanbulTransitionTest.cpp @@ -46,8 +46,8 @@ R"E( "0000000000000000000000000000000000000003": { "precompiled": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } }, "0000000000000000000000000000000000000004": { "precompiled": { "name": "identity", "linear": { "base": 15, "word": 3 } } }, "0000000000000000000000000000000000000005": { "precompiled": { "name": "modexp" } }, - "0000000000000000000000000000000000000006": { "precompiled": { "name": "alt_bn128_G1_add", "linear": { "base": 500, "word": 0 } } }, - "0000000000000000000000000000000000000007": { "precompiled": { "name": "alt_bn128_G1_mul", "linear": { "base": 40000, "word": 0 } } }, + "0000000000000000000000000000000000000006": { "precompiled": { "name": "alt_bn128_G1_add" } }, + "0000000000000000000000000000000000000007": { "precompiled": { "name": "alt_bn128_G1_mul" } }, "0000000000000000000000000000000000000008": { "precompiled": { "name": "alt_bn128_pairing_product"} } } } diff --git a/test/unittests/libethcore/PrecompiledTest.cpp b/test/unittests/libethcore/PrecompiledTest.cpp index fb88e05dc98..c59f970e15e 100644 --- a/test/unittests/libethcore/PrecompiledTest.cpp +++ b/test/unittests/libethcore/PrecompiledTest.cpp @@ -717,4 +717,72 @@ BOOST_AUTO_TEST_CASE(bench_bn256Pairing, *ut::label("bench")) benchmarkPrecompiled("alt_bn128_pairing_product", tests, 1000); } +BOOST_AUTO_TEST_CASE(ecaddCostBeforeIstanbul) +{ + PrecompiledPricer cost = PrecompiledRegistrar::pricer("alt_bn128_G1_add"); + + ChainParams chainParams{genesisInfo(eth::Network::IstanbulTransitionTest)}; + + auto res = cost({}, chainParams, 1); + + BOOST_REQUIRE_EQUAL(static_cast(res), 500); +} + +BOOST_AUTO_TEST_CASE(ecaddCostIstanbul) +{ + PrecompiledPricer cost = PrecompiledRegistrar::pricer("alt_bn128_G1_add"); + + ChainParams chainParams{genesisInfo(eth::Network::IstanbulTransitionTest)}; + + auto res = cost({}, chainParams, 2); + + BOOST_REQUIRE_EQUAL(static_cast(res), 150); +} + +BOOST_AUTO_TEST_CASE(ecmulBeforeIstanbul) +{ + PrecompiledPricer cost = PrecompiledRegistrar::pricer("alt_bn128_G1_mul"); + + ChainParams chainParams{genesisInfo(eth::Network::IstanbulTransitionTest)}; + + auto res = cost({}, chainParams, 1); + + BOOST_REQUIRE_EQUAL(static_cast(res), 40000); +} + +BOOST_AUTO_TEST_CASE(ecmulCostIstanbul) +{ + PrecompiledPricer cost = PrecompiledRegistrar::pricer("alt_bn128_G1_mul"); + + ChainParams chainParams{genesisInfo(eth::Network::IstanbulTransitionTest)}; + + auto res = cost({}, chainParams, 2); + + BOOST_REQUIRE_EQUAL(static_cast(res), 6000); +} + +BOOST_AUTO_TEST_CASE(ecpairingCost) +{ + PrecompiledPricer cost = PrecompiledRegistrar::pricer("alt_bn128_pairing_product"); + + ChainParams chainParams{genesisInfo(eth::Network::IstanbulTransitionTest)}; + + bytes in{fromHex( + "0x1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f593034dd2920f673e204fee281" + "1c678745fc819b55d3e9d294e45c9b03a76aef41209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b" + "4a452003a35bf704bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a416782bb8324af6cf" + "c93537a2ad1a445cfd0ca2a71acd7ac41fadbf933c2a51be344d120a2a4cf30c1bf9845f20c6fe39e07ea2cce6" + "1f0c9bb048165fe5e4de877550111e129f1cf1097710d41c4ac70fcdfa5ba2023c6ff1cbeac322de49d1b6df7c" + "2032c61a830e3c17286de9462bf242fca2883585b93870a73853face6a6bf411198e9393920d483a7260bfb731" + "fb5d25f1aa493335a9e71297e485b7aef312c21800deef121f1e76426a00665e5c4479674322d4f75edadd46de" + "bd5cd992f6ed090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b12c85ea5db8c6d" + "eb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa")}; + + auto costBeforeIstanbul = cost(ref(in), chainParams, 1); + BOOST_CHECK_EQUAL(static_cast(costBeforeIstanbul), in.size() / 192 * 80000 + 100000); + + auto costIstanbul = cost(ref(in), chainParams, 2); + BOOST_CHECK_EQUAL(static_cast(costIstanbul), in.size() / 192 * 34000 + 45000); +} + BOOST_AUTO_TEST_SUITE_END()