From e40bd1a71f9e2d5f3c0fbfe969783ce7250b9010 Mon Sep 17 00:00:00 2001 From: timemarkovqtum Date: Wed, 25 Sep 2024 14:56:33 +0200 Subject: [PATCH] Add kzg tests --- src/Makefile.am | 3 ++ src/Makefile.test.include | 3 +- src/test/qtumtests/kzg_tests.cpp | 67 ++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/test/qtumtests/kzg_tests.cpp diff --git a/src/Makefile.am b/src/Makefile.am index b097e3c5f2..776cfd0341 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -31,6 +31,7 @@ BITCOIN_INCLUDES += -I$(srcdir)/libff BITCOIN_INCLUDES += -I$(srcdir)/blst/bindings BITCOIN_INCLUDES += -I$(srcdir)/evmone/evmc/include BITCOIN_INCLUDES += -I$(srcdir)/evmone/include +BITCOIN_INCLUDES += -I$(srcdir)/evmone/lib BITCOIN_INCLUDES += -I$(srcdir)/eth_client BITCOIN_INCLUDES += -I$(srcdir)/eth_client/utils BITCOIN_INCLUDES += -I$(srcdir)/eth_client/utils/ethash/include @@ -808,6 +809,8 @@ libbitcoin_common_a_SOURCES = \ evmone/lib/evmone_precompiles/bls.hpp \ evmone/lib/evmone_precompiles/kzg.cpp \ evmone/lib/evmone_precompiles/kzg.hpp \ + evmone/lib/evmone_precompiles/sha256.cpp \ + evmone/lib/evmone_precompiles/sha256.hpp \ eth_client/libdevcore/Address.cpp \ eth_client/libdevcore/Address.h \ eth_client/libdevcore/Assertions.h \ diff --git a/src/Makefile.test.include b/src/Makefile.test.include index e892e5b0ff..d74ea6ea0f 100644 --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -199,7 +199,8 @@ BITCOIN_TESTS =\ test/qtumtests/londonfork_tests.cpp \ test/qtumtests/evmone_tests.cpp \ test/qtumtests/shanghaifork_tests.cpp \ - test/qtumtests/cancunfork_tests.cpp + test/qtumtests/cancunfork_tests.cpp \ + test/qtumtests/kzg_tests.cpp if ENABLE_WALLET BITCOIN_TESTS += \ diff --git a/src/test/qtumtests/kzg_tests.cpp b/src/test/qtumtests/kzg_tests.cpp new file mode 100644 index 0000000000..25efc82092 --- /dev/null +++ b/src/test/qtumtests/kzg_tests.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace evmc::literals; +using namespace evmone::crypto; + +namespace KzgTest{ + +consteval auto operator""_u384(const char* s) +{ + return intx::from_string(s); +} + +constexpr auto G1_GENERATOR_X = + 0x17F1D3A73197D7942695638C4FA9AC0FC3688C4F9774B905A14E3A3F171BAC586C55E83FF97A1AEFFB3AF00ADB22C6BB_u384; +constexpr std::byte ZERO32[32]{}; +constexpr std::byte POINT_AT_INFINITY[48]{std::byte{0xC0}}; + +auto versioned_hash(std::span input) noexcept +{ + std::array hash{}; + sha256(hash.data(), input.data(), input.size()); + hash[0] = VERSIONED_HASH_VERSION_KZG; + return hash; +} + +BOOST_FIXTURE_TEST_SUITE(kzg_tests, BasicTestingSetup) + +BOOST_AUTO_TEST_CASE(kzg_verify_proof_hash_invalid){ + const auto r = kzg_verify_proof(ZERO32, ZERO32, ZERO32, POINT_AT_INFINITY, POINT_AT_INFINITY); + BOOST_CHECK(r == false); +} + +BOOST_AUTO_TEST_CASE(kzg_verify_proof_zero){ + // Commit and prove polynomial f(x) = 0. + std::byte z[32]{}; + z[13] = std::byte{17}; // can be any value because f(z) is always 0. + const auto hash = versioned_hash(POINT_AT_INFINITY); + const auto r = kzg_verify_proof(hash.data(), z, ZERO32, POINT_AT_INFINITY, POINT_AT_INFINITY); + BOOST_CHECK(r == true); +} + +BOOST_AUTO_TEST_CASE(kzg_verify_proof_constant){ + // Commit and prove polynomial f(x) = 1. + std::byte z[32]{}; + z[13] = std::byte{17}; // can be any value because f(z) is always 0. + std::byte y[32]{}; + y[31] = std::byte{1}; + + // Commitment for f(x) = 1 is [1]₁, i.e. the G1 generator point. + std::byte c[48]{}; + intx::be::store(reinterpret_cast(c), G1_GENERATOR_X); + c[0] |= std::byte{0x80}; // flag of the point compressed form. + + const auto hash = versioned_hash(c); + const auto r = kzg_verify_proof(hash.data(), z, y, c, POINT_AT_INFINITY); + BOOST_CHECK(r == true); +} + +BOOST_AUTO_TEST_SUITE_END() + +}