Skip to content

Commit

Permalink
Add kzg tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Sep 25, 2024
1 parent 496e620 commit e40bd1a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down
3 changes: 2 additions & 1 deletion src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -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 += \
Expand Down
67 changes: 67 additions & 0 deletions src/test/qtumtests/kzg_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <boost/test/unit_test.hpp>
#include <test/util/setup_common.h>
#include <evmc/evmc.hpp>
#include <evmone_precompiles/kzg.hpp>
#include <evmone_precompiles/sha256.hpp>
#include <intx/intx.hpp>
#include <span>

using namespace evmc::literals;
using namespace evmone::crypto;

namespace KzgTest{

consteval auto operator""_u384(const char* s)
{
return intx::from_string<intx::uint384>(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<const std::byte> input) noexcept
{
std::array<std::byte, 32> 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<uint8_t(&)[sizeof(c)]>(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()

}

0 comments on commit e40bd1a

Please sign in to comment.