From 8578317cca900f0ff30c80eb5d61a7e4aaf4993e Mon Sep 17 00:00:00 2001 From: turuslan Date: Thu, 15 Aug 2024 18:28:28 +0500 Subject: [PATCH 01/14] safrole test Signed-off-by: turuslan --- CMakeLists.txt | 4 + test-vectors/safrole/CMakeLists.txt | 12 + test-vectors/safrole/safrole.hpp | 333 +++++++++++++++++++++ test-vectors/safrole/safrole.test.cpp | 229 ++++++++++++++ vcpkg-overlay/kagome-crates/portfile.cmake | 11 + vcpkg-overlay/kagome-crates/vcpkg.json | 8 + vcpkg.json | 2 + 7 files changed, 599 insertions(+) create mode 100644 test-vectors/safrole/safrole.hpp create mode 100644 test-vectors/safrole/safrole.test.cpp create mode 100644 vcpkg-overlay/kagome-crates/portfile.cmake create mode 100644 vcpkg-overlay/kagome-crates/vcpkg.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ecad11..ba8debf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,8 +18,12 @@ option(TESTING "Build and run test suite" ON) find_package(Python3 REQUIRED) +find_package(PkgConfig REQUIRED) +pkg_check_modules(libb2 REQUIRED IMPORTED_TARGET GLOBAL libb2) + find_package(Boost CONFIG REQUIRED) find_package(fmt CONFIG REQUIRED) +find_package(jam_crust CONFIG REQUIRED) find_package(scale CONFIG REQUIRED) add_subdirectory(src) diff --git a/test-vectors/safrole/CMakeLists.txt b/test-vectors/safrole/CMakeLists.txt index c382151..2c50f19 100644 --- a/test-vectors/safrole/CMakeLists.txt +++ b/test-vectors/safrole/CMakeLists.txt @@ -29,3 +29,15 @@ target_link_libraries(test_vectors_safrole_types_test test_vectors_safrole_types ) add_test(NAME test_vectors_safrole_types_test COMMAND $) + +add_executable(test_vectors_safrole_test + safrole.test.cpp +) +target_compile_definitions(test_vectors_safrole_test PRIVATE PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}") +target_link_libraries(test_vectors_safrole_test + fmt::fmt + jam_crust::jam_crust + PkgConfig::libb2 + test_vectors_safrole_types +) +add_test(NAME test_vectors_safrole_test COMMAND $) diff --git a/test-vectors/safrole/safrole.hpp b/test-vectors/safrole/safrole.hpp new file mode 100644 index 0000000..fc49e35 --- /dev/null +++ b/test-vectors/safrole/safrole.hpp @@ -0,0 +1,333 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include +#include + +#include "types.hpp" + +struct Blake { + using Hash = qtils::BytesN<32>; + blake2b_state state; + Blake() { + blake2b_init(&state, sizeof(Hash)); + } + Blake &update(qtils::BytesIn input) { + blake2b_update(&state, input.data(), input.size()); + return *this; + } + Hash hash() const { + Hash hash; + auto state2 = state; + blake2b_final(&state2, hash.data(), sizeof(Hash)); + return hash; + } + static Hash hash(qtils::BytesIn input) { + return Blake{}.update(input).hash(); + } +}; + +namespace jam::safrole { + using types = test_vectors_safrole::tiny; + using GammaA = decltype(types::State::gamma_a); + using GammaS = decltype(types::State::gamma_s); + using GammaZ = decltype(types::State::gamma_z); + using BandersnatchSignature = decltype(types::TicketEnvelope::signature); + using BandersnatchKeys = decltype(types::EpochMark::validators); + + inline auto *ring_ctx() { + static auto ctx = jam_ring_new(types::validators_count); + return ctx; + } + + struct TicketBodyLess { + bool operator()( + const types::TicketBody &l, const types::TicketBody &r) const { + return l.id < r.id; + } + }; + + // [JAM:1.4.5] + // clang-format off + const qtils::BytesN<15> X_T = {'j','a','m','_','t','i','c','k','e','t','_','s','e','a','l'}; + // clang-format on + + // [JAM:1.4.4] + constexpr auto E = types::epoch_length; + + // [JAM:1.4.4] + constexpr uint32_t N = 2; + // [JAM:1.4.4] + constexpr uint32_t K = 16; + + // [JAM:1.4.4] + constexpr uint32_t Y = E * 5 / 6; + + template + qtils::BytesN first_bytes(qtils::BytesIn bytes) { + if (bytes.size() < N) { + throw std::logic_error("de"); + } + qtils::BytesN first; + memcpy(first.data(), bytes.data(), N); + return first; + } + + BandersnatchKeys bandersnatch_keys(const types::ValidatorsData &validators) { + BandersnatchKeys keys; + for (size_t i = 0; i < keys.size(); ++i) { + keys[i] = validators[i].bandersnatch; + } + return keys; + } + + // [JAM:3.7.2] + template + qtils::BytesN frown( + const qtils::BytesN &x, const qtils::BytesN &y) { + qtils::BytesN xy; + memcpy(xy.data(), x.data(), X); + memcpy(xy.data() + X, y.data(), Y); + return xy; + } + + template + qtils::BytesN doubleplus(const qtils::BytesN &x, uint8_t i) { + return frown(x, std::array{i}); + } + + // [JAM:273] + template + qtils::BytesN mathcal_E(uint64_t x) { + qtils::BytesN out; + boost::endian::endian_store( + out.data(), x); + return out; + } + // [JAM:273] + template + auto de(qtils::BytesN x) { + return boost::endian:: + endian_load(x.data()); + } + + // [JAM:3.8.1] + inline types::OpaqueHash mathcal_H(qtils::BytesIn m) { + return Blake::hash(m); + } + + inline GammaZ mathcal_O(const BandersnatchKeys &pks) { + if (sizeof(pks) != pks.size() * sizeof(pks[0])) { + // BUG: static_assert + throw std::logic_error{"mathcal_O"}; + } + GammaZ out; + if (not jam_ring_commitment( + ring_ctx(), pks[0].data(), sizeof(pks), out.data())) { + throw std::logic_error("mathcal_O"); + } + return out; + } + + // [JAM:310] + // [JAM:313] + std::optional banderout( + const BandersnatchSignature &signature) { + types::OpaqueHash out; + if (not jam_vrf_output(signature.data(), out.data())) { + return std::nullopt; + } + return out; + } + + // [JAM:310] + // [JAM:313] + inline std::optional bandersnatch(const GammaZ &gamma_z, + qtils::BytesIn input, + const BandersnatchSignature &signature) { + types::OpaqueHash out; + std::unique_ptr + verifier{ + jam_ring_verifier_new(ring_ctx(), gamma_z.data()), + &jam_ring_verifier_drop, + }; + if (not jam_ring_verifier_verify(verifier.get(), + input.data(), + input.size(), + signature.data(), + out.data())) { + return std::nullopt; + } + return out; + } + + auto &circlearrowleft(const auto &s, auto i) { + return s[i % s.size()]; + } + + struct Epoch { + uint32_t epoch, phase; + Epoch(uint32_t slot) : epoch{slot / E}, phase{slot % E} {} + }; + + inline std::pair transition( + const types::State &state, const types::Input &input) { + const auto &[H_t, banderout_H_v, E_T] = input; + const auto + &[tau, eta, lambda, kappa, gamma_k, iota, gamma_a, gamma_s, gamma_z] = + state; + const auto &[eta_0, eta_1, eta_2, eta_3] = eta; + using Error = types::CustomErrorCode; + const auto error = [&](Error error) { + return std::make_pair(state, types::Output{error}); + }; + + if (H_t <= state.tau) { + return error(Error::bad_slot); + } + + // [JAM:46] + const auto tau_tick = H_t; + + // [JAM:47] + const Epoch epoch{state.tau}, epoch_tick{tau_tick}; + const auto &[e, m] = epoch; + const auto &[e_tick, m_tick] = epoch_tick; + const auto change_epoch = e_tick != e; + + // [JAM:75] + if (E_T.size() > K) { + throw std::logic_error("not covered by test vectors"); + } + if (m_tick >= Y and not E_T.empty()) { + return std::make_pair(state, types::Output{Error::unexpected_ticket}); + } + + // [JAM:59] + // TODO: offenders + const auto phi = [](const types::ValidatorsData &k) { return k; }; + // [JAM:58] + const auto [gamma_tick_k, kappa_tick, lambda_tick, gamma_tick_z] = change_epoch ? + [&](const types::ValidatorsData&gamma_tick_k) { + return std::tuple{gamma_tick_k, gamma_k, kappa, mathcal_O(bandersnatch_keys(gamma_tick_k))}; + }(phi(iota)) : + std::tuple{gamma_k,kappa,lambda,gamma_z}; + + // [JAM:67] + const auto eta_tick_0 = mathcal_H(frown(eta_0, banderout_H_v)); + + // [JAM:68] + const auto [eta_tick_1, eta_tick_2, eta_tick_3] = change_epoch + ? std::tuple{eta_0, eta_1, eta_2} + : std::tuple{eta_1, eta_2, eta_3}; + + std::vector n; + for (auto &[r, p] : E_T) { + // [JAM:74] + if (r >= N) { + return error(Error::bad_ticket_attempt); + } + const auto m = frown(X_T, doubleplus(eta_tick_2, r)); + const auto y = bandersnatch(gamma_z, m, p); + if (not y) { + return error(Error::bad_ticket_proof); + } + // [JAM:76] + n.emplace_back(types::TicketBody{*y, r}); + } + // [JAM:77] + if (not std::is_sorted(n.begin(), n.end(), TicketBodyLess{})) { + return error(Error::bad_ticket_order); + } + + // [JAM:79] + std::vector gamma_tick_a; + if (change_epoch) { + gamma_tick_a = n; + } else { + std::set_union(n.begin(), + n.end(), + gamma_a.begin(), + gamma_a.end(), + std::back_inserter(gamma_tick_a), + TicketBodyLess{}); + // [JAM:78] + if (gamma_tick_a.size() != gamma_a.size() + n.size()) { + return error(Error::duplicate_ticket); + } + } + if (gamma_tick_a.size() > E) { + gamma_tick_a.resize(E); + } + + // [JAM:70] + const auto Z = [](const GammaA &s) { + types::TicketsBodies tickets; + if (s.size() != E) { + throw std::logic_error{"Z"}; + } + auto it1 = s.begin(), it2 = std::prev(s.end()); + auto odd = true; + for (auto &ticket : tickets) { + ticket = odd ? *it1 : *it2; + if (odd) { + ++it1; + } else { + --it2; + } + odd = not odd; + } + return tickets; + }; + // [JAM:71] + const auto F = [](const types::OpaqueHash &r, + const types::ValidatorsData &k) { + types::EpochKeys keys; + for (uint32_t i = 0; i < E; ++i) { + keys[i] = circlearrowleft( + k, de(first_bytes<4>(mathcal_H(frown(r, mathcal_E<4>(i)))))) + .bandersnatch; + } + return keys; + }; + // [JAM:69] + const auto gamma_tick_s = e_tick == e + 1 && m >= Y && gamma_a.size() == E + ? types::TicketsOrKeys{Z(gamma_a)} + : e_tick == e ? gamma_s + : types::TicketsOrKeys{F(eta_tick_2, kappa_tick)}; + + types::Output output; + return { + types::State{ + .tau = tau_tick, + .eta = {eta_tick_0, eta_tick_1, eta_tick_2, eta_tick_3}, + .lambda = lambda_tick, + .kappa = kappa_tick, + .gamma_k = gamma_tick_k, + .iota = iota, // TODO + .gamma_a = gamma_tick_a, + .gamma_s = gamma_tick_s, + .gamma_z = gamma_tick_z, + }, + types::Output{types::OutputMarks{ + // [JAM:72] + .epoch_mark = change_epoch ? std::make_optional(types::EpochMark{ + eta_tick_1, bandersnatch_keys(gamma_tick_k)}) + : std::nullopt, + + // [JAM:73] + .tickets_mark = + e_tick == e && m < Y && Y <= m_tick && gamma_a.size() == E + ? std::make_optional(types::TicketsMark{Z(gamma_a)}) + : std::nullopt, + }}, + }; + } +} // namespace jam::safrole diff --git a/test-vectors/safrole/safrole.test.cpp b/test-vectors/safrole/safrole.test.cpp new file mode 100644 index 0000000..9a1184f --- /dev/null +++ b/test-vectors/safrole/safrole.test.cpp @@ -0,0 +1,229 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "safrole.hpp" +#include "vectors.hpp" + +using jam::test_vectors_safrole::Vectors; +using _types = jam::test_vectors_safrole::tiny; + +auto ok = true; + +struct Indent { + int indent = 0; + Indent operator~() const { + return Indent{indent + 1}; + } +}; +template <> +struct fmt::formatter { + static constexpr auto parse(format_parse_context &ctx) { + return ctx.begin(); + } + auto format(const Indent &v, format_context &ctx) const { + auto out = ctx.out(); + for (int i = 0; i < v.indent; ++i) { + out = fmt::detail::write(out, " "); + } + return out; + } +}; + +#define DIFF_F(T) void diff(Indent indent, const T &v1, const T &v2) +DIFF_F(uint32_t); +DIFF_F(_types::CustomErrorCode); +DIFF_F(_types::EpochMark); +DIFF_F(_types::TicketEnvelope); +DIFF_F(_types::TicketBody); +DIFF_F(_types::OutputMarks); +DIFF_F(_types::Output); +DIFF_F(_types::ValidatorData); +DIFF_F(_types::TicketsOrKeys); +DIFF_F(_types::State); + +enum class color { red = 31, green = 32 }; +template +void _diff(color color, fmt::format_string f, T &&...a) { + fmt::print("\x1b[0;{}m", (int)color); + fmt::print(f, static_cast(a)...); + fmt::println("\x1b[0;0m"); +} +template +void diff1(fmt::format_string f, T &&...a) { + _diff(color::red, f, static_cast(a)...); +} +template +void diff2(fmt::format_string f, T &&...a) { + _diff(color::green, f, static_cast(a)...); +} + +#define DIFF_M(m) diff_m(indent, v1.m, v2.m, #m) +template +void diff_m(Indent indent, const T &v1, const T &v2, std::string_view name) { + if (v1 == v2) { + return; + } + fmt::println("{}{}", indent, name); + diff(~indent, v1, v2); +} +template +void diff(Indent indent, + const boost::variant &v1, + const boost::variant &v2, + const std::vector &names) { + if (v1 == v2) { + return; + } + if (v1.which() != v2.which()) { + fmt::println("{}{} != {}", indent, names[v1.which()], names[v2.which()]); + return; + } + fmt::println("{}{}", indent, names[v1.which()]); + if (v1.which() == 0) { + diff(~indent, boost::get(v1), boost::get(v2)); + } else { + diff(~indent, boost::get(v1), boost::get(v2)); + } +} +template +void diff( + Indent indent, const std::optional &v1, const std::optional &v2) { + if (v1 == v2) { + return; + } + std::string_view o[] = {"None", "Some"}; + auto i1 = v1 ? 1 : 0, i2 = v2 ? 1 : 0; + if (i1 != i2) { + fmt::println("{}{} != {}", indent, o[i1], o[i2]); + return; + } + fmt::println("{}{}", indent, o[i1]); + if (v1) { + diff(~indent, *v1, *v2); + } +} +template + requires( + requires(T v) { std::span{v}; } + and not requires(T v) { qtils::BytesIn{v}; }) +void diff(Indent indent, const T &_v1, const T &_v2) { + if (_v1 == _v2) { + return; + } + std::span v1{_v1}, v2{_v2}; + if (v1.size() != v2.size()) { + fmt::println("{}{} != {}", indent, v1.size(), v2.size()); + return; + } + fmt::println("{}{}", indent, v1.size()); + for (size_t i = 0; i < v1.size(); ++i) { + if (v1[i] == v2[i]) { + continue; + } + fmt::println("{}[{}]", indent, i); + diff(~indent, v1[i], v2[i]); + } +} +DIFF_F(qtils::BytesIn) { + if (v1.size() == v2.size() && memcmp(v1.data(), v2.data(), v1.size()) == 0) { + return; + } + diff1("{}- {:x}", indent, v1); + diff2("{}+ {:x}", indent, v2); +} +DIFF_F(uint32_t) { + fmt::println("{}{} != {}", indent, v1, v2); +} +DIFF_F(_types::CustomErrorCode) { + std::string_view o[] = { + "bad_slot", + "unexpected_ticket", + "bad_ticket_order", + "bad_ticket_proof", + "bad_ticket_attempt", + "reserved", + "duplicate_ticket", + }; + fmt::println("{}{} != {}", indent, o[(int)v1], o[(int)v2]); +} + +DIFF_F(_types::EpochMark) { + DIFF_M(entropy); + DIFF_M(validators); +} +DIFF_F(_types::TicketEnvelope) { + DIFF_M(attempt); + DIFF_M(signature); +} +DIFF_F(_types::TicketBody) { + DIFF_M(id); + DIFF_M(attempt); +} +DIFF_F(_types::OutputMarks) { + DIFF_M(epoch_mark); + DIFF_M(tickets_mark); +} +DIFF_F(_types::Output) { + diff(indent, v1.v, v2.v, {"ok", "err"}); +} +DIFF_F(_types::ValidatorData) { + DIFF_M(bandersnatch); + DIFF_M(ed25519); + DIFF_M(bls); + DIFF_M(metadata); +} +DIFF_F(_types::TicketsOrKeys) { + diff(indent, v1.v, v2.v, {"tickets", "keys"}); +} +DIFF_F(_types::State) { + DIFF_M(tau); + DIFF_M(eta); + DIFF_M(lambda); + DIFF_M(kappa); + DIFF_M(gamma_k); + DIFF_M(iota); + DIFF_M(gamma_a); + DIFF_M(gamma_s); + DIFF_M(gamma_z); +} + +/** + * Check safrole state transition against test vectors. + */ +// template +void test_transition() { + constexpr auto full = false; + Vectors vectors; + using types = decltype(vectors)::types; + for (auto &path : vectors.paths) { + auto s = path.native(); + fmt::println("{}.json", s.substr(0, s.size() - 6)); + auto testcase = vectors.read(path); + auto [state, output] = + jam::safrole::transition(testcase->pre_state, testcase->input); + Indent indent{1}; + if (state != testcase->post_state) { + ok = false; + diff_m(indent, state, testcase->post_state, "state"); + } + if (output != testcase->output) { + ok = false; + diff_m(indent, output, testcase->output, "output"); + } + } +} + +int main() { + test_transition(); + // test_transition(); + // test_transition(); + if (ok) { + fmt::println("ok"); + } + return 0; +} diff --git a/vcpkg-overlay/kagome-crates/portfile.cmake b/vcpkg-overlay/kagome-crates/portfile.cmake new file mode 100644 index 0000000..60c6399 --- /dev/null +++ b/vcpkg-overlay/kagome-crates/portfile.cmake @@ -0,0 +1,11 @@ +vcpkg_check_linkage(ONLY_STATIC_LIBRARY) +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO qdrvm/kagome-crates + REF 0688fec85a521eb8d19acb88fdfbb1ca33961538 + SHA512 565c785993835a23c89f6bf178cb3e9d11deedc19a910458dc8db237b1319018a4a99e7cae546c8faa0ed24413ed36df0521c329ae78598544b6144c44f8a3b0 +) +vcpkg_cmake_configure(SOURCE_PATH "${SOURCE_PATH}") +vcpkg_cmake_install() +vcpkg_cmake_config_fixup(PACKAGE_NAME "kagome-crates") +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") diff --git a/vcpkg-overlay/kagome-crates/vcpkg.json b/vcpkg-overlay/kagome-crates/vcpkg.json new file mode 100644 index 0000000..6104ca7 --- /dev/null +++ b/vcpkg-overlay/kagome-crates/vcpkg.json @@ -0,0 +1,8 @@ +{ + "name": "kagome-crates", + "version": "1.0.2", + "dependencies": [ + { "name": "vcpkg-cmake", "host": true }, + { "name": "vcpkg-cmake-config", "host": true } + ] +} diff --git a/vcpkg.json b/vcpkg.json index d044aa4..e5ff010 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -2,6 +2,8 @@ "name": "cpp-jam", "version": "0.0.1", "dependencies": [ + "kagome-crates", + "libb2", "scale", { "name": "fmt", From e02a56e1ca9709c9a4e9938320c91a56b21bf22a Mon Sep 17 00:00:00 2001 From: turuslan Date: Mon, 23 Sep 2024 12:17:54 +0500 Subject: [PATCH 02/14] generic Signed-off-by: turuslan --- test-vectors/safrole/safrole.hpp | 168 ++++++++++++++------------ test-vectors/safrole/safrole.test.cpp | 63 +++++----- 2 files changed, 126 insertions(+), 105 deletions(-) diff --git a/test-vectors/safrole/safrole.hpp b/test-vectors/safrole/safrole.hpp index fc49e35..3147e8d 100644 --- a/test-vectors/safrole/safrole.hpp +++ b/test-vectors/safrole/safrole.hpp @@ -35,40 +35,15 @@ struct Blake { }; namespace jam::safrole { - using types = test_vectors_safrole::tiny; - using GammaA = decltype(types::State::gamma_a); - using GammaS = decltype(types::State::gamma_s); - using GammaZ = decltype(types::State::gamma_z); - using BandersnatchSignature = decltype(types::TicketEnvelope::signature); - using BandersnatchKeys = decltype(types::EpochMark::validators); - - inline auto *ring_ctx() { - static auto ctx = jam_ring_new(types::validators_count); - return ctx; - } + namespace generic = jam::test_vectors_safrole::generic; + using BandersnatchSignature = decltype(generic::TicketEnvelope::signature); - struct TicketBodyLess { - bool operator()( - const types::TicketBody &l, const types::TicketBody &r) const { - return l.id < r.id; - } - }; + template + struct Generic; - // [JAM:1.4.5] - // clang-format off - const qtils::BytesN<15> X_T = {'j','a','m','_','t','i','c','k','e','t','_','s','e','a','l'}; - // clang-format on - - // [JAM:1.4.4] - constexpr auto E = types::epoch_length; - - // [JAM:1.4.4] - constexpr uint32_t N = 2; - // [JAM:1.4.4] - constexpr uint32_t K = 16; - - // [JAM:1.4.4] - constexpr uint32_t Y = E * 5 / 6; + auto &circlearrowleft(const auto &s, auto i) { + return s[i % s.size()]; + } template qtils::BytesN first_bytes(qtils::BytesIn bytes) { @@ -80,14 +55,6 @@ namespace jam::safrole { return first; } - BandersnatchKeys bandersnatch_keys(const types::ValidatorsData &validators) { - BandersnatchKeys keys; - for (size_t i = 0; i < keys.size(); ++i) { - keys[i] = validators[i].bandersnatch; - } - return keys; - } - // [JAM:3.7.2] template qtils::BytesN frown( @@ -111,6 +78,7 @@ namespace jam::safrole { out.data(), x); return out; } + // [JAM:273] template auto de(qtils::BytesN x) { @@ -119,11 +87,66 @@ namespace jam::safrole { } // [JAM:3.8.1] - inline types::OpaqueHash mathcal_H(qtils::BytesIn m) { + inline generic::OpaqueHash mathcal_H(qtils::BytesIn m) { return Blake::hash(m); } - inline GammaZ mathcal_O(const BandersnatchKeys &pks) { + // [JAM:310] + // [JAM:313] + inline std::optional banderout( + const BandersnatchSignature &signature) { + generic::OpaqueHash out; + if (not jam_vrf_output(signature.data(), out.data())) { + return std::nullopt; + } + return out; + } + + struct TicketBodyLess { + bool operator()( + const generic::TicketBody &l, const generic::TicketBody &r) const { + return l.id < r.id; + } + }; +} // namespace jam::safrole + +template +struct jam::safrole::Generic { + using GammaA = decltype(types::State::gamma_a); + using GammaZ = decltype(types::State::gamma_z); + using BandersnatchKeys = decltype(types::EpochMark::validators); + + static auto *ring_ctx() { + static auto ctx = jam_ring_new(types::validators_count); + return ctx; + } + + // [JAM:1.4.5] + // clang-format off + static constexpr qtils::BytesN<15> X_T = {'j','a','m','_','t','i','c','k','e','t','_','s','e','a','l'}; + // clang-format on + + // [JAM:1.4.4] + static constexpr auto E = types::epoch_length; + + // [JAM:1.4.4] + static constexpr uint32_t N = 2; + // [JAM:1.4.4] + static constexpr uint32_t K = 16; + + // [JAM:1.4.4] + static constexpr uint32_t Y = E * 5 / 6; + + static BandersnatchKeys bandersnatch_keys( + const types::ValidatorsData &validators) { + BandersnatchKeys keys; + for (size_t i = 0; i < keys.size(); ++i) { + keys[i] = validators[i].bandersnatch; + } + return keys; + } + + static GammaZ mathcal_O(const BandersnatchKeys &pks) { if (sizeof(pks) != pks.size() * sizeof(pks[0])) { // BUG: static_assert throw std::logic_error{"mathcal_O"}; @@ -138,21 +161,11 @@ namespace jam::safrole { // [JAM:310] // [JAM:313] - std::optional banderout( - const BandersnatchSignature &signature) { - types::OpaqueHash out; - if (not jam_vrf_output(signature.data(), out.data())) { - return std::nullopt; - } - return out; - } - - // [JAM:310] - // [JAM:313] - inline std::optional bandersnatch(const GammaZ &gamma_z, + static std::optional bandersnatch( + const GammaZ &gamma_z, qtils::BytesIn input, const BandersnatchSignature &signature) { - types::OpaqueHash out; + typename types::OpaqueHash out; std::unique_ptr verifier{ jam_ring_verifier_new(ring_ctx(), gamma_z.data()), @@ -168,16 +181,12 @@ namespace jam::safrole { return out; } - auto &circlearrowleft(const auto &s, auto i) { - return s[i % s.size()]; - } - struct Epoch { uint32_t epoch, phase; Epoch(uint32_t slot) : epoch{slot / E}, phase{slot % E} {} }; - inline std::pair transition( + static std::pair transition( const types::State &state, const types::Input &input) { const auto &[H_t, banderout_H_v, E_T] = input; const auto @@ -186,7 +195,7 @@ namespace jam::safrole { const auto &[eta_0, eta_1, eta_2, eta_3] = eta; using Error = types::CustomErrorCode; const auto error = [&](Error error) { - return std::make_pair(state, types::Output{error}); + return std::make_pair(state, typename types::Output{error}); }; if (H_t <= state.tau) { @@ -207,7 +216,8 @@ namespace jam::safrole { throw std::logic_error("not covered by test vectors"); } if (m_tick >= Y and not E_T.empty()) { - return std::make_pair(state, types::Output{Error::unexpected_ticket}); + return std::make_pair( + state, typename types::Output{Error::unexpected_ticket}); } // [JAM:59] @@ -228,7 +238,7 @@ namespace jam::safrole { ? std::tuple{eta_0, eta_1, eta_2} : std::tuple{eta_1, eta_2, eta_3}; - std::vector n; + std::vector n; for (auto &[r, p] : E_T) { // [JAM:74] if (r >= N) { @@ -240,7 +250,7 @@ namespace jam::safrole { return error(Error::bad_ticket_proof); } // [JAM:76] - n.emplace_back(types::TicketBody{*y, r}); + n.emplace_back(typename types::TicketBody{*y, r}); } // [JAM:77] if (not std::is_sorted(n.begin(), n.end(), TicketBodyLess{})) { @@ -248,7 +258,7 @@ namespace jam::safrole { } // [JAM:79] - std::vector gamma_tick_a; + std::vector gamma_tick_a; if (change_epoch) { gamma_tick_a = n; } else { @@ -269,7 +279,7 @@ namespace jam::safrole { // [JAM:70] const auto Z = [](const GammaA &s) { - types::TicketsBodies tickets; + typename types::TicketsBodies tickets; if (s.size() != E) { throw std::logic_error{"Z"}; } @@ -289,7 +299,7 @@ namespace jam::safrole { // [JAM:71] const auto F = [](const types::OpaqueHash &r, const types::ValidatorsData &k) { - types::EpochKeys keys; + typename types::EpochKeys keys; for (uint32_t i = 0; i < E; ++i) { keys[i] = circlearrowleft( k, de(first_bytes<4>(mathcal_H(frown(r, mathcal_E<4>(i)))))) @@ -299,13 +309,14 @@ namespace jam::safrole { }; // [JAM:69] const auto gamma_tick_s = e_tick == e + 1 && m >= Y && gamma_a.size() == E - ? types::TicketsOrKeys{Z(gamma_a)} - : e_tick == e ? gamma_s - : types::TicketsOrKeys{F(eta_tick_2, kappa_tick)}; + ? typename types::TicketsOrKeys{Z(gamma_a)} + : e_tick == e + ? gamma_s + : typename types::TicketsOrKeys{F(eta_tick_2, kappa_tick)}; - types::Output output; + typename types::Output output; return { - types::State{ + typename types::State{ .tau = tau_tick, .eta = {eta_tick_0, eta_tick_1, eta_tick_2, eta_tick_3}, .lambda = lambda_tick, @@ -316,18 +327,19 @@ namespace jam::safrole { .gamma_s = gamma_tick_s, .gamma_z = gamma_tick_z, }, - types::Output{types::OutputMarks{ + typename types::Output{typename types::OutputMarks{ // [JAM:72] - .epoch_mark = change_epoch ? std::make_optional(types::EpochMark{ - eta_tick_1, bandersnatch_keys(gamma_tick_k)}) - : std::nullopt, + .epoch_mark = change_epoch + ? std::make_optional(typename types::EpochMark{ + eta_tick_1, bandersnatch_keys(gamma_tick_k)}) + : std::nullopt, // [JAM:73] .tickets_mark = e_tick == e && m < Y && Y <= m_tick && gamma_a.size() == E - ? std::make_optional(types::TicketsMark{Z(gamma_a)}) + ? std::make_optional(typename types::TicketsMark{Z(gamma_a)}) : std::nullopt, }}, }; } -} // namespace jam::safrole +}; diff --git a/test-vectors/safrole/safrole.test.cpp b/test-vectors/safrole/safrole.test.cpp index 9a1184f..69e7200 100644 --- a/test-vectors/safrole/safrole.test.cpp +++ b/test-vectors/safrole/safrole.test.cpp @@ -10,7 +10,7 @@ #include "vectors.hpp" using jam::test_vectors_safrole::Vectors; -using _types = jam::test_vectors_safrole::tiny; +namespace generic = jam::test_vectors_safrole::generic; auto ok = true; @@ -34,17 +34,23 @@ struct fmt::formatter { } }; -#define DIFF_F(T) void diff(Indent indent, const T &v1, const T &v2) +#define DIFF_F(...) \ + void diff(Indent indent, const __VA_ARGS__ &v1, const __VA_ARGS__ &v2) DIFF_F(uint32_t); -DIFF_F(_types::CustomErrorCode); -DIFF_F(_types::EpochMark); -DIFF_F(_types::TicketEnvelope); -DIFF_F(_types::TicketBody); -DIFF_F(_types::OutputMarks); -DIFF_F(_types::Output); -DIFF_F(_types::ValidatorData); -DIFF_F(_types::TicketsOrKeys); -DIFF_F(_types::State); +DIFF_F(generic::CustomErrorCode); +template +DIFF_F(generic::EpochMark); +DIFF_F(generic::TicketEnvelope); +DIFF_F(generic::TicketBody); +template +DIFF_F(generic::OutputMarks); +template +DIFF_F(generic::Output); +DIFF_F(generic::ValidatorData); +template +DIFF_F(generic::TicketsOrKeys); +template +DIFF_F(generic::State); enum class color { red = 31, green = 32 }; template @@ -139,7 +145,7 @@ DIFF_F(qtils::BytesIn) { DIFF_F(uint32_t) { fmt::println("{}{} != {}", indent, v1, v2); } -DIFF_F(_types::CustomErrorCode) { +DIFF_F(generic::CustomErrorCode) { std::string_view o[] = { "bad_slot", "unexpected_ticket", @@ -152,35 +158,40 @@ DIFF_F(_types::CustomErrorCode) { fmt::println("{}{} != {}", indent, o[(int)v1], o[(int)v2]); } -DIFF_F(_types::EpochMark) { +template +DIFF_F(generic::EpochMark) { DIFF_M(entropy); DIFF_M(validators); } -DIFF_F(_types::TicketEnvelope) { +DIFF_F(generic::TicketEnvelope) { DIFF_M(attempt); DIFF_M(signature); } -DIFF_F(_types::TicketBody) { +DIFF_F(generic::TicketBody) { DIFF_M(id); DIFF_M(attempt); } -DIFF_F(_types::OutputMarks) { +template +DIFF_F(generic::OutputMarks) { DIFF_M(epoch_mark); DIFF_M(tickets_mark); } -DIFF_F(_types::Output) { +template +DIFF_F(generic::Output) { diff(indent, v1.v, v2.v, {"ok", "err"}); } -DIFF_F(_types::ValidatorData) { +DIFF_F(generic::ValidatorData) { DIFF_M(bandersnatch); DIFF_M(ed25519); DIFF_M(bls); DIFF_M(metadata); } -DIFF_F(_types::TicketsOrKeys) { +template +DIFF_F(generic::TicketsOrKeys) { diff(indent, v1.v, v2.v, {"tickets", "keys"}); } -DIFF_F(_types::State) { +template +DIFF_F(generic::State) { DIFF_M(tau); DIFF_M(eta); DIFF_M(lambda); @@ -195,17 +206,16 @@ DIFF_F(_types::State) { /** * Check safrole state transition against test vectors. */ -// template +template void test_transition() { - constexpr auto full = false; Vectors vectors; using types = decltype(vectors)::types; for (auto &path : vectors.paths) { auto s = path.native(); fmt::println("{}.json", s.substr(0, s.size() - 6)); auto testcase = vectors.read(path); - auto [state, output] = - jam::safrole::transition(testcase->pre_state, testcase->input); + auto [state, output] = jam::safrole::Generic::transition( + testcase->pre_state, testcase->input); Indent indent{1}; if (state != testcase->post_state) { ok = false; @@ -219,9 +229,8 @@ void test_transition() { } int main() { - test_transition(); - // test_transition(); - // test_transition(); + test_transition(); + test_transition(); if (ok) { fmt::println("ok"); } From e07c5d472b72490adf97b7af0177cd6dfa118e63 Mon Sep 17 00:00:00 2001 From: turuslan Date: Mon, 23 Sep 2024 18:44:58 +0500 Subject: [PATCH 03/14] gtest Signed-off-by: turuslan --- CMakeLists.txt | 11 +++++-- test-vectors/safrole/CMakeLists.txt | 6 ++-- test-vectors/safrole/safrole.test.cpp | 45 +++++++++------------------ test-vectors/safrole/types.test.cpp | 28 +++++------------ test-vectors/safrole/vectors.hpp | 24 ++++++++++++++ vcpkg.json | 12 +++---- 6 files changed, 64 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba8debf..4e0c3dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,11 @@ cmake_minimum_required(VERSION 3.25) +option(TESTING "Build and run test suite" ON) +if(TESTING) + list(APPEND VCPKG_MANIFEST_FEATURES test) +endif() + project(cpp-jam VERSION 0.0.1 LANGUAGES C CXX @@ -14,8 +19,6 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) -option(TESTING "Build and run test suite" ON) - find_package(Python3 REQUIRED) find_package(PkgConfig REQUIRED) @@ -30,5 +33,9 @@ add_subdirectory(src) if(TESTING) enable_testing() + + find_package(GTest CONFIG REQUIRED) + set(GTEST_DEPS GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main) + add_subdirectory(test-vectors) endif () diff --git a/test-vectors/safrole/CMakeLists.txt b/test-vectors/safrole/CMakeLists.txt index 2c50f19..cf1d0e3 100644 --- a/test-vectors/safrole/CMakeLists.txt +++ b/test-vectors/safrole/CMakeLists.txt @@ -26,9 +26,10 @@ add_executable(test_vectors_safrole_types_test target_compile_definitions(test_vectors_safrole_types_test PRIVATE PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}") target_link_libraries(test_vectors_safrole_types_test fmt::fmt + ${GTEST_DEPS} test_vectors_safrole_types ) -add_test(NAME test_vectors_safrole_types_test COMMAND $) +add_test(test_vectors_safrole_types_test test_vectors_safrole_types_test) add_executable(test_vectors_safrole_test safrole.test.cpp @@ -36,8 +37,9 @@ add_executable(test_vectors_safrole_test target_compile_definitions(test_vectors_safrole_test PRIVATE PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}") target_link_libraries(test_vectors_safrole_test fmt::fmt + ${GTEST_DEPS} jam_crust::jam_crust PkgConfig::libb2 test_vectors_safrole_types ) -add_test(NAME test_vectors_safrole_test COMMAND $) +add_test(test_vectors_safrole_test test_vectors_safrole_test) diff --git a/test-vectors/safrole/safrole.test.cpp b/test-vectors/safrole/safrole.test.cpp index 69e7200..17c84c3 100644 --- a/test-vectors/safrole/safrole.test.cpp +++ b/test-vectors/safrole/safrole.test.cpp @@ -4,16 +4,14 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include #include "safrole.hpp" #include "vectors.hpp" -using jam::test_vectors_safrole::Vectors; namespace generic = jam::test_vectors_safrole::generic; -auto ok = true; - struct Indent { int indent = 0; Indent operator~() const { @@ -206,33 +204,18 @@ DIFF_F(generic::State) { /** * Check safrole state transition against test vectors. */ -template -void test_transition() { - Vectors vectors; - using types = decltype(vectors)::types; - for (auto &path : vectors.paths) { - auto s = path.native(); - fmt::println("{}.json", s.substr(0, s.size() - 6)); - auto testcase = vectors.read(path); - auto [state, output] = jam::safrole::Generic::transition( - testcase->pre_state, testcase->input); - Indent indent{1}; - if (state != testcase->post_state) { - ok = false; - diff_m(indent, state, testcase->post_state, "state"); - } - if (output != testcase->output) { - ok = false; - diff_m(indent, output, testcase->output, "output"); - } - } -} - -int main() { - test_transition(); - test_transition(); - if (ok) { - fmt::println("ok"); +GTEST_VECTORS { + using types = Vectors::types; + auto testcase = Vectors::read(path); + auto [state, output] = jam::safrole::Generic::transition( + testcase->pre_state, testcase->input); + Indent indent{1}; + EXPECT_EQ(state, testcase->post_state); + if (state != testcase->post_state) { + diff_m(indent, state, testcase->post_state, "state"); + } + EXPECT_EQ(output, testcase->output); + if (output != testcase->output) { + diff_m(indent, output, testcase->output, "output"); } - return 0; } diff --git a/test-vectors/safrole/types.test.cpp b/test-vectors/safrole/types.test.cpp index 466d72f..bc4730b 100644 --- a/test-vectors/safrole/types.test.cpp +++ b/test-vectors/safrole/types.test.cpp @@ -4,30 +4,16 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "vectors.hpp" +#include -using jam::test_vectors_safrole::Vectors; +#include "vectors.hpp" /** * Check python generated scale encoding/decoding against test vectors. */ -template -void test_reencode() { - Vectors vectors; - for (auto &path : vectors.paths) { - fmt::println("{}", path.native()); - auto expected = vectors.readRaw(path); - auto decoded = vectors.decode(expected); - auto reencoded = scale::encode(*decoded).value(); - if (reencoded != expected) { - throw std::logic_error{"reencoded"}; - } - } -} - -int main() { - test_reencode(); - test_reencode(); - fmt::println("ok"); - return 0; +GTEST_VECTORS { + auto expected = Vectors::readRaw(path); + auto decoded = Vectors::decode(expected); + auto reencoded = scale::encode(*decoded).value(); + EXPECT_EQ(reencoded, expected); } diff --git a/test-vectors/safrole/vectors.hpp b/test-vectors/safrole/vectors.hpp index 3f5b731..700d754 100644 --- a/test-vectors/safrole/vectors.hpp +++ b/test-vectors/safrole/vectors.hpp @@ -9,6 +9,30 @@ #include "types.scale.hpp" +#define GTEST_VECTORS \ + template \ + struct GtestVectors { \ + using Vectors = jam::test_vectors_safrole::Vectors; \ + void test(const std::filesystem::path &path); \ + }; \ + struct Test : testing::TestWithParam> {}; \ + TEST_P(Test, Test) { GetParam()(); } \ + INSTANTIATE_TEST_SUITE_P(Test, Test, testing::ValuesIn([] { \ + std::vector> params; \ + auto f = [&] { \ + using Vectors = jam::test_vectors_safrole::Vectors; \ + Vectors vectors; \ + for (auto &path : vectors.paths) { \ + params.emplace_back([path] { GtestVectors{}.test(path); }); \ + } \ + }; \ + f.operator()(); \ + f.operator()(); \ + return params; \ + }())); \ + template \ + void GtestVectors::test(const std::filesystem::path &path) + namespace jam::test_vectors_safrole { template struct Vectors { diff --git a/vcpkg.json b/vcpkg.json index e5ff010..bfdb1ba 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -2,12 +2,12 @@ "name": "cpp-jam", "version": "0.0.1", "dependencies": [ + "fmt", "kagome-crates", "libb2", - "scale", - { - "name": "fmt", - "version>=": "10.2.1#2" - } - ] + "scale" + ], + "features": { + "test": { "description": "Test", "dependencies": ["gtest"]} + } } From 221490f323de6470d2c0de1ceda6876e59ee3284 Mon Sep 17 00:00:00 2001 From: turuslan Date: Mon, 23 Sep 2024 18:57:38 +0500 Subject: [PATCH 04/14] comment Signed-off-by: turuslan --- test-vectors/safrole/safrole.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test-vectors/safrole/safrole.hpp b/test-vectors/safrole/safrole.hpp index 3147e8d..0331427 100644 --- a/test-vectors/safrole/safrole.hpp +++ b/test-vectors/safrole/safrole.hpp @@ -186,6 +186,9 @@ struct jam::safrole::Generic { Epoch(uint32_t slot) : epoch{slot / E}, phase{slot % E} {} }; + /** + * Given state and input, derive next state and output. + */ static std::pair transition( const types::State &state, const types::Input &input) { const auto &[H_t, banderout_H_v, E_T] = input; From 1e3344bc927f99dfeb00e4748b5312920d84a492 Mon Sep 17 00:00:00 2001 From: turuslan Date: Mon, 23 Sep 2024 21:39:48 +0500 Subject: [PATCH 05/14] graypaper references Signed-off-by: turuslan --- test-vectors/safrole/safrole.hpp | 70 +++++++++++++++++--------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/test-vectors/safrole/safrole.hpp b/test-vectors/safrole/safrole.hpp index 0331427..cda0cfd 100644 --- a/test-vectors/safrole/safrole.hpp +++ b/test-vectors/safrole/safrole.hpp @@ -41,6 +41,7 @@ namespace jam::safrole { template struct Generic; + // [GP 0.3.6 3.7] auto &circlearrowleft(const auto &s, auto i) { return s[i % s.size()]; } @@ -55,7 +56,7 @@ namespace jam::safrole { return first; } - // [JAM:3.7.2] + // [GP 0.3.6 3.7.2] template qtils::BytesN frown( const qtils::BytesN &x, const qtils::BytesN &y) { @@ -65,12 +66,13 @@ namespace jam::safrole { return xy; } + // [GP 0.3.6 3.7.2] template qtils::BytesN doubleplus(const qtils::BytesN &x, uint8_t i) { return frown(x, std::array{i}); } - // [JAM:273] + // [GP 0.3.6 C.1.2 271] template qtils::BytesN mathcal_E(uint64_t x) { qtils::BytesN out; @@ -79,20 +81,20 @@ namespace jam::safrole { return out; } - // [JAM:273] + // [GP 0.3.6 C.1.2 271] template auto de(qtils::BytesN x) { return boost::endian:: endian_load(x.data()); } - // [JAM:3.8.1] + // [GP 0.3.6 3.8.1] inline generic::OpaqueHash mathcal_H(qtils::BytesIn m) { return Blake::hash(m); } - // [JAM:310] - // [JAM:313] + // [GP 0.3.6 G 307] + // [GP 0.3.6 G 310] inline std::optional banderout( const BandersnatchSignature &signature) { generic::OpaqueHash out; @@ -121,20 +123,20 @@ struct jam::safrole::Generic { return ctx; } - // [JAM:1.4.5] + // [GP 0.3.6 I.4.5] // clang-format off static constexpr qtils::BytesN<15> X_T = {'j','a','m','_','t','i','c','k','e','t','_','s','e','a','l'}; // clang-format on - // [JAM:1.4.4] + // [GP 0.3.6 I.4.4] static constexpr auto E = types::epoch_length; - // [JAM:1.4.4] + // [GP 0.3.6 I.4.4] static constexpr uint32_t N = 2; - // [JAM:1.4.4] + // [GP 0.3.6 I.4.4] static constexpr uint32_t K = 16; - // [JAM:1.4.4] + // [GP 0.3.6 I.4.4] static constexpr uint32_t Y = E * 5 / 6; static BandersnatchKeys bandersnatch_keys( @@ -146,6 +148,7 @@ struct jam::safrole::Generic { return keys; } + // [GP 0.3.6 G 308] static GammaZ mathcal_O(const BandersnatchKeys &pks) { if (sizeof(pks) != pks.size() * sizeof(pks[0])) { // BUG: static_assert @@ -159,8 +162,7 @@ struct jam::safrole::Generic { return out; } - // [JAM:310] - // [JAM:313] + // [GP 0.3.6 G 309] static std::optional bandersnatch( const GammaZ &gamma_z, qtils::BytesIn input, @@ -181,6 +183,7 @@ struct jam::safrole::Generic { return out; } + // [GP 0.3.6 6.1 46] struct Epoch { uint32_t epoch, phase; Epoch(uint32_t slot) : epoch{slot / E}, phase{slot % E} {} @@ -201,20 +204,21 @@ struct jam::safrole::Generic { return std::make_pair(state, typename types::Output{error}); }; + // [GP 0.3.6 5 41] if (H_t <= state.tau) { return error(Error::bad_slot); } - // [JAM:46] + // [GP 0.3.6 6.1 45] const auto tau_tick = H_t; - // [JAM:47] + // [GP 0.3.6 6.1 46] const Epoch epoch{state.tau}, epoch_tick{tau_tick}; const auto &[e, m] = epoch; const auto &[e_tick, m_tick] = epoch_tick; const auto change_epoch = e_tick != e; - // [JAM:75] + // [GP 0.3.6 6.7 74] if (E_T.size() > K) { throw std::logic_error("not covered by test vectors"); } @@ -223,27 +227,27 @@ struct jam::safrole::Generic { state, typename types::Output{Error::unexpected_ticket}); } - // [JAM:59] - // TODO: offenders + // [GP 0.3.6 6.3 58] + // TODO(turuslan): #3, wait for test vectors const auto phi = [](const types::ValidatorsData &k) { return k; }; - // [JAM:58] + // [GP 0.3.6 6.3 57] const auto [gamma_tick_k, kappa_tick, lambda_tick, gamma_tick_z] = change_epoch ? [&](const types::ValidatorsData&gamma_tick_k) { return std::tuple{gamma_tick_k, gamma_k, kappa, mathcal_O(bandersnatch_keys(gamma_tick_k))}; }(phi(iota)) : std::tuple{gamma_k,kappa,lambda,gamma_z}; - // [JAM:67] + // [GP 0.3.6 6.4 66] const auto eta_tick_0 = mathcal_H(frown(eta_0, banderout_H_v)); - // [JAM:68] + // [GP 0.3.6 6.4 67] const auto [eta_tick_1, eta_tick_2, eta_tick_3] = change_epoch ? std::tuple{eta_0, eta_1, eta_2} : std::tuple{eta_1, eta_2, eta_3}; std::vector n; for (auto &[r, p] : E_T) { - // [JAM:74] + // [GP 0.3.6 6.7 73] if (r >= N) { return error(Error::bad_ticket_attempt); } @@ -252,15 +256,15 @@ struct jam::safrole::Generic { if (not y) { return error(Error::bad_ticket_proof); } - // [JAM:76] + // [GP 0.3.6 6.7 75] n.emplace_back(typename types::TicketBody{*y, r}); } - // [JAM:77] + // [GP 0.3.6 6.7 76] if (not std::is_sorted(n.begin(), n.end(), TicketBodyLess{})) { return error(Error::bad_ticket_order); } - // [JAM:79] + // [GP 0.3.6 6.7 78] std::vector gamma_tick_a; if (change_epoch) { gamma_tick_a = n; @@ -271,7 +275,7 @@ struct jam::safrole::Generic { gamma_a.end(), std::back_inserter(gamma_tick_a), TicketBodyLess{}); - // [JAM:78] + // [GP 0.3.6 6.7 77] if (gamma_tick_a.size() != gamma_a.size() + n.size()) { return error(Error::duplicate_ticket); } @@ -280,7 +284,7 @@ struct jam::safrole::Generic { gamma_tick_a.resize(E); } - // [JAM:70] + // [GP 0.3.6 6.5 69] const auto Z = [](const GammaA &s) { typename types::TicketsBodies tickets; if (s.size() != E) { @@ -299,7 +303,7 @@ struct jam::safrole::Generic { } return tickets; }; - // [JAM:71] + // [GP 0.3.6 6.5 70] const auto F = [](const types::OpaqueHash &r, const types::ValidatorsData &k) { typename types::EpochKeys keys; @@ -310,7 +314,7 @@ struct jam::safrole::Generic { } return keys; }; - // [JAM:69] + // [GP 0.3.6 6.5 68] const auto gamma_tick_s = e_tick == e + 1 && m >= Y && gamma_a.size() == E ? typename types::TicketsOrKeys{Z(gamma_a)} : e_tick == e @@ -325,19 +329,19 @@ struct jam::safrole::Generic { .lambda = lambda_tick, .kappa = kappa_tick, .gamma_k = gamma_tick_k, - .iota = iota, // TODO + // TODO(turuslan): #3, wait for test vectors + .iota = iota, .gamma_a = gamma_tick_a, .gamma_s = gamma_tick_s, .gamma_z = gamma_tick_z, }, typename types::Output{typename types::OutputMarks{ - // [JAM:72] + // [GP 0.3.6 6.6 71] .epoch_mark = change_epoch ? std::make_optional(typename types::EpochMark{ eta_tick_1, bandersnatch_keys(gamma_tick_k)}) : std::nullopt, - - // [JAM:73] + // [GP 0.3.6 6.6 72] .tickets_mark = e_tick == e && m < Y && Y <= m_tick && gamma_a.size() == E ? std::make_optional(typename types::TicketsMark{Z(gamma_a)}) From 4df0e5e7f0e50e8edc5d1c84627cd4b1c534aca1 Mon Sep 17 00:00:00 2001 From: turuslan Date: Wed, 25 Sep 2024 16:13:14 +0500 Subject: [PATCH 06/14] simplify, deduplicate Signed-off-by: turuslan --- CMakeLists.txt | 6 + python/asn1.py | 188 +++++++++++++++++-------- src/jam/blake.hpp | 33 +++++ test-vectors/CMakeLists.txt | 2 + test-vectors/asn1.cmake | 49 +++++++ test-vectors/common.hpp | 64 +++++++++ test-vectors/diff.hpp | 139 +++++++++++++++++++ test-vectors/safrole/.gitignore | 1 + test-vectors/safrole/CMakeLists.txt | 36 +---- test-vectors/safrole/safrole.hpp | 77 +--------- test-vectors/safrole/safrole.test.cpp | 193 +------------------------- 11 files changed, 428 insertions(+), 360 deletions(-) create mode 100644 src/jam/blake.hpp create mode 100644 test-vectors/asn1.cmake create mode 100644 test-vectors/common.hpp create mode 100644 test-vectors/diff.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e0c3dc..3330c42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,12 @@ find_package(fmt CONFIG REQUIRED) find_package(jam_crust CONFIG REQUIRED) find_package(scale CONFIG REQUIRED) +add_library(headers INTERFACE) +target_include_directories(headers INTERFACE + $ + $ +) + add_subdirectory(src) if(TESTING) diff --git a/python/asn1.py b/python/asn1.py index 6a70a7d..1bb9c21 100755 --- a/python/asn1.py +++ b/python/asn1.py @@ -2,26 +2,24 @@ import asn1tools import os.path +import sys + DIR = os.path.dirname(__file__) TEST_VECTORS_DIR = os.path.join(DIR, "../test-vectors") OUTPUT_NAME = "types" -OUTPUT_DIR = os.path.join(TEST_VECTORS_DIR, "safrole") -SAFROLE_DIR = os.path.join(TEST_VECTORS_DIR, "jamtestvectors/safrole") -ARGS = ["validators-count", "epoch-length"] -NS = "jam::test_vectors_safrole" def flatten(aaa: list[list]): return [a for aa in aaa for a in aa] -def safrole_file(name: str): - return os.path.join(SAFROLE_DIR, name + ".asn") +def asn_file(name: str): + return os.path.join(TEST_VECTORS_DIR, "jamtestvectors", name + ".asn") -def write(ext: str, lines: list[str]): - with open(os.path.join(OUTPUT_DIR, OUTPUT_NAME + ext), "w") as f: +def write(path: str, lines: list[str]): + with open(path, "w") as f: f.writelines(line + "\n" for line in lines) @@ -33,7 +31,7 @@ def c_struct(name: str, members): return [ "struct %s {" % name, *((" %s %s;" % (t, k) for k, t in members)), - " bool operator==(const %s &)const = default;" % name, + " bool operator==(const %s &) const = default;" % name, "};", ] @@ -54,11 +52,14 @@ def __init__(self, name: str): self.args: list[str] = [] self.decl: list[str] = [] self.scale: list[str] = [] + self.diff: list[str] = [] def c_tdecl(self): if not self.args: return [] - return ["template<%s>" % ", ".join("uint32_t %s" % c_dash(x) for x in self.args)] + return [ + "template<%s>" % ", ".join("uint32_t %s" % c_dash(x) for x in self.args) + ] def c_targs(self): if not self.args: @@ -98,7 +99,7 @@ def deep(k: str): return deps1, deps2 -def asn_args(types: dict, deps2: dict[str, set[str]]): +def asn_args(ARGS: list[str], types: dict, deps2: dict[str, set[str]]): args = { k: { s[0] @@ -138,9 +139,18 @@ def c_scale_struct(ty: Type, members: list[str]): ) -def parse_types(path: str): +def c_diff(NS: str, ty: Type, lines: list[str]): + return [ + *ty.c_tdecl(), + "DIFF_F(%s::%s) {" % (NS, ty.c_tname()), + *indent(lines), + "}", + ] + + +def parse_types(NS: str, ARGS: list[str], path: str, key: str): def asn_sequence_of(t): - (size,) = t["size"] + (size,) = t.get("size", (None,)) fixed = isinstance(size, (int, str)) T = t["element"]["type"] assert T in asn_types @@ -153,6 +163,8 @@ def asn_sequence_of(t): return "std::vector<%s>" % T def asn_member(t): + if t["type"] == "OCTET STRING": + t = dict(type="SEQUENCE OF", element=dict(type="U8"), size=t["size"]) if t["type"] == "SEQUENCE OF": r = asn_sequence_of(t) elif t["type"] in asn_types: @@ -163,10 +175,12 @@ def asn_member(t): return "std::optional<%s>" % r return r - asn_types: dict = asn1tools.parse_files([path])["SafroleModule"]["types"] + asn_types: dict = asn1tools.parse_files([path])[key]["types"] + if "U8" not in asn_types: + asn_types["U8"] = dict(type="INTEGER") deps1, deps2 = asn_deps(asn_types) types = {tname: Type(tname) for tname in asn_types} - for tname, args in asn_args(asn_types, deps2).items(): + for tname, args in asn_args(ARGS, asn_types, deps2).items(): types[tname].args = args enum_trait = [] for tname, t in asn_types.items(): @@ -177,7 +191,15 @@ def asn_member(t): if tname == "U32": ty.decl = c_using(tname, "uint32_t") continue + if t["type"] == "NULL": + t = dict(type="SEQUENCE", members=[]) if t["type"] == "CHOICE": + if tname == "MmrPeak": + assert [x["name"] for x in t["members"]] == ["none", "some"] + ty.decl = c_using( + tname, "std::optional<%s>" % asn_member(t["members"][1]) + ) + continue ty.decl = c_struct( tname, [ @@ -189,6 +211,15 @@ def asn_member(t): ], ) ty.scale.extend(c_scale_struct(ty, ["v"])) + ty.diff = c_diff( + NS, + ty, + [ + "diff(indent, v1.v, v2.v, {", + *(' "%s",' % c_dash(x["name"]) for x in t["members"]), + "});", + ], + ) continue if t["type"] == "ENUMERATED": values = [x[1] for x in t["values"]] @@ -198,14 +229,22 @@ def asn_member(t): *(" %s," % c_dash(x[0]) for x in t["values"]), "};", ] - ns = "%s::generic" % NS + ty.diff = c_diff( + NS, + ty, + [ + "diff(indent, v1, v2, {", + *(' "%s",' % c_dash(x[0]) for x in t["values"]), + "});", + ], + ) enum_trait.append( "SCALE_DEFINE_ENUM_VALUE_LIST(%s, %s, %s)" % ( - ns, + NS, ty.name, ", ".join( - "%s::%s::%s" % (ns, ty.name, c_dash(x[0])) for x in t["values"] + "%s::%s::%s" % (NS, ty.name, c_dash(x[0])) for x in t["values"] ), ) ) @@ -217,6 +256,9 @@ def asn_member(t): ty.scale.extend( c_scale_struct(ty, [c_dash(x["name"]) for x in t["members"]]) ) + ty.diff = c_diff( + NS, ty, ["DIFF_M(%s);" % c_dash(x["name"]) for x in t["members"]] + ) continue ty.decl = c_using(tname, asn_member(t)) @@ -224,49 +266,81 @@ def asn_member(t): return [types[k] for k in order], enum_trait -def parse_const(path: str): - values: dict = asn1tools.parse_files([path])["SafroleConstants"]["values"] +def parse_const(path: str, key: str): + values: dict = asn1tools.parse_files([path])[key]["values"] assert all(v["type"] == "INTEGER" for v in values.values()) return {k: v["value"] for k, v in values.items()} -types, enum_trait = parse_types(safrole_file("safrole")) - +class Gen: + def __init__(self, NS: str, ARGS: list[str], path: str, key: str): + self.types, self.enum_trait = parse_types(NS, ARGS, path, key) + self.g_types = flatten([*ty.c_tdecl(), *ty.decl] for ty in self.types) + self.g_types = ["namespace %s {" % NS, *indent(self.g_types), "}"] + self.g_types = [ + "#pragma once", + "#include ", + "#include ", + *self.g_types, + ] + self.g_scale = flatten(ty.scale for ty in self.types) + self.g_scale = ["namespace %s {" % NS, *indent(self.g_scale), "}"] + self.g_scale = [ + "#pragma once", + "#include ", + '#include "%s.hpp"' % OUTPUT_NAME, + *self.g_scale, + *self.enum_trait, + ] + self.g_diff = flatten(ty.diff for ty in self.types) + self.g_diff = [ + "#pragma once", + '#include "../diff.hpp"', + '#include "%s.hpp"' % OUTPUT_NAME, + *self.g_diff, + ] -g_types = flatten([*ty.c_tdecl(), *ty.decl] for ty in types) -g_types = ["namespace %s::generic {" % NS, *indent(g_types), "}"] -for name in ["tiny", "full"]: - args = parse_const(safrole_file(name)) - g_args = [ - *["static constexpr uint32_t %s = %s;" % (c_dash(a), args[a]) for a in ARGS], - *flatten( - c_using(ty.name, "%s::generic::%s" % (NS, ty.c_tname())) for ty in types - ), - ] - g_args = ["struct %s {" % name, *indent(g_args), "};"] - g_types.extend( - [ - "namespace %s {" % NS, - *indent(g_args), - "}", + def write(self, name: str): + prefix = os.path.join(TEST_VECTORS_DIR, name, OUTPUT_NAME) + write(prefix + ".hpp", self.g_types) + write(prefix + ".scale.hpp", self.g_scale) + write(prefix + ".diff.hpp", self.g_diff) + + +def safrole(): + NS = "jam::test_vectors_safrole" + ARGS = ["validators-count", "epoch-length"] + g = Gen("%s::generic" % NS, ARGS, asn_file("safrole/safrole"), "SafroleModule") + for name in ["tiny", "full"]: + args = parse_const(asn_file("safrole/%s" % name), "SafroleConstants") + g_args = [ + *[ + "static constexpr uint32_t %s = %s;" % (c_dash(a), args[a]) + for a in ARGS + ], + *flatten( + c_using(ty.name, "%s::generic::%s" % (NS, ty.c_tname())) + for ty in g.types + ), ] + g_args = ["struct %s {" % name, *indent(g_args), "};"] + g.g_types.extend( + [ + "namespace %s {" % NS, + *indent(g_args), + "}", + ] + ) + g.write("safrole") + + +def history(): + g = Gen( + "jam::test_vectors_history", [], asn_file("history/history"), "HistoryModule" ) -g_types = [ - "#pragma once", - "#include ", - "#include ", - *g_types, -] - -g_scale = flatten(ty.scale for ty in types) -g_scale = ["namespace %s::generic {" % NS, *indent(g_scale), "}"] -g_scale = [ - "#pragma once", - "#include ", - '#include "%s.hpp"' % OUTPUT_NAME, - *g_scale, - *enum_trait, -] - -write(".hpp", g_types) -write(".scale.hpp", g_scale) + g.write("history") + + +if __name__ == "__main__": + for arg in sys.argv[1:]: + dict(safrole=safrole, history=history)[arg]() diff --git a/src/jam/blake.hpp b/src/jam/blake.hpp new file mode 100644 index 0000000..c0ad000 --- /dev/null +++ b/src/jam/blake.hpp @@ -0,0 +1,33 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include + +namespace jam { + struct Blake { + using Hash = qtils::BytesN<32>; + blake2b_state state; + Blake() { + blake2b_init(&state, sizeof(Hash)); + } + Blake &update(qtils::BytesIn input) { + blake2b_update(&state, input.data(), input.size()); + return *this; + } + Hash hash() const { + Hash hash; + auto state2 = state; + blake2b_final(&state2, hash.data(), sizeof(Hash)); + return hash; + } + static Hash hash(qtils::BytesIn input) { + return Blake{}.update(input).hash(); + } + }; +} // namespace jam diff --git a/test-vectors/CMakeLists.txt b/test-vectors/CMakeLists.txt index bf96af0..6a83feb 100644 --- a/test-vectors/CMakeLists.txt +++ b/test-vectors/CMakeLists.txt @@ -4,4 +4,6 @@ # SPDX-License-Identifier: Apache-2.0 # +include(asn1.cmake) + add_subdirectory(safrole) diff --git a/test-vectors/asn1.cmake b/test-vectors/asn1.cmake new file mode 100644 index 0000000..4dda878 --- /dev/null +++ b/test-vectors/asn1.cmake @@ -0,0 +1,49 @@ +# +# Copyright Quadrivium LLC +# All Rights Reserved +# SPDX-License-Identifier: Apache-2.0 +# + +function(asn1 name) + set(ASN_DIR ${PROJECT_SOURCE_DIR}/test-vectors/jamtestvectors/${name}) + foreach(x IN LISTS ARGN) + list(APPEND ASN_FILES ${ASN_DIR}/${x}.asn) + endforeach() + set(OUTPUT_PREFIX ${PROJECT_SOURCE_DIR}/test-vectors/${name}/types) + set(OUTPUT ${OUTPUT_PREFIX}.hpp ${OUTPUT_PREFIX}.scale.hpp ${OUTPUT_PREFIX}.diff.hpp) + set(ASN1_PY ${PROJECT_SOURCE_DIR}/python/asn1.py) + add_custom_command( + OUTPUT ${OUTPUT} + COMMAND ${Python3_EXECUTABLE} ${ASN1_PY} ${name} + DEPENDS ${ASN1_PY} ${ASN_FILES} + ) + + add_library(test_vectors_${name}_types INTERFACE ${OUTPUT}) + target_link_libraries(test_vectors_${name}_types INTERFACE + scale::scale + ) + + add_executable(test_vectors_${name}_types_test + types.test.cpp + ) + target_compile_definitions(test_vectors_${name}_types_test PRIVATE PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}") + target_link_libraries(test_vectors_${name}_types_test + fmt::fmt + ${GTEST_DEPS} + test_vectors_${name}_types + ) + add_test(test_vectors_${name}_types_test test_vectors_${name}_types_test) + + add_executable(test_vectors_${name}_test + ${name}.test.cpp + ) + target_compile_definitions(test_vectors_${name}_test PRIVATE PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}") + target_link_libraries(test_vectors_${name}_test + fmt::fmt + ${GTEST_DEPS} + headers + test_vectors_${name}_types + ) + add_test(test_vectors_${name}_test test_vectors_${name}_test) + +endfunction() diff --git a/test-vectors/common.hpp b/test-vectors/common.hpp new file mode 100644 index 0000000..1c814e5 --- /dev/null +++ b/test-vectors/common.hpp @@ -0,0 +1,64 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include + +namespace jam { + template + qtils::BytesN first_bytes(qtils::BytesIn bytes) { + if (bytes.size() < N) { + throw std::logic_error("de"); + } + qtils::BytesN first; + memcpy(first.data(), bytes.data(), N); + return first; + } + + // [GP 0.3.6 3.7] + auto &circlearrowleft(const auto &s, auto i) { + return s[i % s.size()]; + } + + // [GP 0.3.6 3.7.2] + template + qtils::BytesN frown( + const qtils::BytesN &x, const qtils::BytesN &y) { + qtils::BytesN xy; + memcpy(xy.data(), x.data(), X); + memcpy(xy.data() + X, y.data(), Y); + return xy; + } + + // [GP 0.3.6 3.7.2] + template + qtils::BytesN doubleplus(const qtils::BytesN &x, uint8_t i) { + return frown(x, std::array{i}); + } + + // [GP 0.3.6 C.1.2 271] + template + qtils::BytesN mathcal_E(uint64_t x) { + qtils::BytesN out; + boost::endian::endian_store( + out.data(), x); + return out; + } + + // [GP 0.3.6 C.1.2 271] + template + auto de(qtils::BytesN x) { + return boost::endian:: + endian_load(x.data()); + } + + // [GP 0.3.6 3.8.1] + inline auto mathcal_H(qtils::BytesIn m) { + return Blake::hash(m); + } +} // namespace jam diff --git a/test-vectors/diff.hpp b/test-vectors/diff.hpp new file mode 100644 index 0000000..5fbd0a3 --- /dev/null +++ b/test-vectors/diff.hpp @@ -0,0 +1,139 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +struct Indent { + int indent = 0; + Indent operator~() const { + return Indent{indent + 1}; + } +}; +template <> +struct fmt::formatter { + static constexpr auto parse(format_parse_context &ctx) { + return ctx.begin(); + } + auto format(const Indent &v, format_context &ctx) const { + auto out = ctx.out(); + for (int i = 0; i < v.indent; ++i) { + out = fmt::detail::write(out, " "); + } + return out; + } +}; + +#define DIFF_F(...) \ + void diff(Indent indent, const __VA_ARGS__ &v1, const __VA_ARGS__ &v2) +#define DIFF_M(m) diff_m(indent, v1.m, v2.m, #m) + +enum class color { red = 31, green = 32 }; +template +void _diff(color color, fmt::format_string f, T &&...a) { + fmt::print("\x1b[0;{}m", (int)color); + fmt::print(f, static_cast(a)...); + fmt::println("\x1b[0;0m"); +} +template +void diff1(fmt::format_string f, T &&...a) { + _diff(color::red, f, static_cast(a)...); +} +template +void diff2(fmt::format_string f, T &&...a) { + _diff(color::green, f, static_cast(a)...); +} + +template +void diff_m(Indent indent, const T &v1, const T &v2, std::string_view name) { + if (v1 == v2) { + return; + } + fmt::println("{}{}", indent, name); + diff(~indent, v1, v2); +} + +DIFF_F(uint32_t) { + fmt::println("{}{} != {}", indent, v1, v2); +} + +template +DIFF_F(std::optional) { + if (v1 == v2) { + return; + } + std::string_view o[] = {"None", "Some"}; + auto i1 = v1 ? 1 : 0, i2 = v2 ? 1 : 0; + if (i1 != i2) { + fmt::println("{}{} != {}", indent, o[i1], o[i2]); + return; + } + fmt::println("{}{}", indent, o[i1]); + if (v1) { + diff(~indent, *v1, *v2); + } +} + +template + requires( + requires(T v) { std::span{v}; } + and not requires(T v) { qtils::BytesIn{v}; }) +void diff(Indent indent, const T &_v1, const T &_v2) { + if (_v1 == _v2) { + return; + } + std::span v1{_v1}, v2{_v2}; + if (v1.size() != v2.size()) { + fmt::println("{}{} != {}", indent, v1.size(), v2.size()); + return; + } + fmt::println("{}{}", indent, v1.size()); + for (size_t i = 0; i < v1.size(); ++i) { + if (v1[i] == v2[i]) { + continue; + } + fmt::println("{}[{}]", indent, i); + diff(~indent, v1[i], v2[i]); + } +} + +DIFF_F(qtils::BytesIn) { + if (v1.size() == v2.size() && memcmp(v1.data(), v2.data(), v1.size()) == 0) { + return; + } + diff1("{}- {:x}", indent, v1); + diff2("{}+ {:x}", indent, v2); +} + +template + requires(std::is_enum_v) +void diff(Indent indent, + const E &v1, + const E &v2, + const std::vector &names) { + fmt::println("{}{} != {}", indent, names[(int)v1], names[(int)v2]); +} + +template +void diff(Indent indent, + const boost::variant &v1, + const boost::variant &v2, + const std::vector &names) { + if (v1 == v2) { + return; + } + if (v1.which() != v2.which()) { + fmt::println("{}{} != {}", indent, names[v1.which()], names[v2.which()]); + return; + } + fmt::println("{}{}", indent, names[v1.which()]); + if (v1.which() == 0) { + diff(~indent, boost::get(v1), boost::get(v2)); + } else { + diff(~indent, boost::get(v1), boost::get(v2)); + } +} diff --git a/test-vectors/safrole/.gitignore b/test-vectors/safrole/.gitignore index cb56060..4fc5df1 100644 --- a/test-vectors/safrole/.gitignore +++ b/test-vectors/safrole/.gitignore @@ -1,2 +1,3 @@ /types.hpp /types.scale.hpp +/types.diff.hpp diff --git a/test-vectors/safrole/CMakeLists.txt b/test-vectors/safrole/CMakeLists.txt index cf1d0e3..ea94dcf 100644 --- a/test-vectors/safrole/CMakeLists.txt +++ b/test-vectors/safrole/CMakeLists.txt @@ -4,42 +4,8 @@ # SPDX-License-Identifier: Apache-2.0 # -set(SAFROLE_ASN_DIR ${PROJECT_SOURCE_DIR}/test-vectors/jamtestvectors/safrole) -set(SAFROLE_ASN ${SAFROLE_ASN_DIR}/safrole.asn ${SAFROLE_ASN_DIR}/tiny.asn ${SAFROLE_ASN_DIR}/full.asn) -set(SAFROLE_OUTPUT_PREFIX ${PROJECT_SOURCE_DIR}/test-vectors/safrole/types) -set(SAFROLE_OUTPUT ${SAFROLE_OUTPUT_PREFIX}.hpp ${SAFROLE_OUTPUT_PREFIX}.scale.hpp) -set(SAFROLE_PY ${PROJECT_SOURCE_DIR}/python/asn1.py) -add_custom_command( - OUTPUT ${SAFROLE_OUTPUT} - COMMAND ${Python3_EXECUTABLE} ${SAFROLE_PY} - DEPENDS ${SAFROLE_PY} ${SAFROLE_ASN} -) - -add_library(test_vectors_safrole_types INTERFACE ${SAFROLE_OUTPUT}) -target_link_libraries(test_vectors_safrole_types INTERFACE - scale::scale -) - -add_executable(test_vectors_safrole_types_test - types.test.cpp -) -target_compile_definitions(test_vectors_safrole_types_test PRIVATE PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}") -target_link_libraries(test_vectors_safrole_types_test - fmt::fmt - ${GTEST_DEPS} - test_vectors_safrole_types -) -add_test(test_vectors_safrole_types_test test_vectors_safrole_types_test) - -add_executable(test_vectors_safrole_test - safrole.test.cpp -) -target_compile_definitions(test_vectors_safrole_test PRIVATE PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}") +asn1(safrole safrole tiny full) target_link_libraries(test_vectors_safrole_test - fmt::fmt - ${GTEST_DEPS} jam_crust::jam_crust PkgConfig::libb2 - test_vectors_safrole_types ) -add_test(test_vectors_safrole_test test_vectors_safrole_test) diff --git a/test-vectors/safrole/safrole.hpp b/test-vectors/safrole/safrole.hpp index cda0cfd..cdcf5b8 100644 --- a/test-vectors/safrole/safrole.hpp +++ b/test-vectors/safrole/safrole.hpp @@ -6,34 +6,11 @@ #pragma once -#include #include -#include -#include +#include "../common.hpp" #include "types.hpp" -struct Blake { - using Hash = qtils::BytesN<32>; - blake2b_state state; - Blake() { - blake2b_init(&state, sizeof(Hash)); - } - Blake &update(qtils::BytesIn input) { - blake2b_update(&state, input.data(), input.size()); - return *this; - } - Hash hash() const { - Hash hash; - auto state2 = state; - blake2b_final(&state2, hash.data(), sizeof(Hash)); - return hash; - } - static Hash hash(qtils::BytesIn input) { - return Blake{}.update(input).hash(); - } -}; - namespace jam::safrole { namespace generic = jam::test_vectors_safrole::generic; using BandersnatchSignature = decltype(generic::TicketEnvelope::signature); @@ -41,58 +18,6 @@ namespace jam::safrole { template struct Generic; - // [GP 0.3.6 3.7] - auto &circlearrowleft(const auto &s, auto i) { - return s[i % s.size()]; - } - - template - qtils::BytesN first_bytes(qtils::BytesIn bytes) { - if (bytes.size() < N) { - throw std::logic_error("de"); - } - qtils::BytesN first; - memcpy(first.data(), bytes.data(), N); - return first; - } - - // [GP 0.3.6 3.7.2] - template - qtils::BytesN frown( - const qtils::BytesN &x, const qtils::BytesN &y) { - qtils::BytesN xy; - memcpy(xy.data(), x.data(), X); - memcpy(xy.data() + X, y.data(), Y); - return xy; - } - - // [GP 0.3.6 3.7.2] - template - qtils::BytesN doubleplus(const qtils::BytesN &x, uint8_t i) { - return frown(x, std::array{i}); - } - - // [GP 0.3.6 C.1.2 271] - template - qtils::BytesN mathcal_E(uint64_t x) { - qtils::BytesN out; - boost::endian::endian_store( - out.data(), x); - return out; - } - - // [GP 0.3.6 C.1.2 271] - template - auto de(qtils::BytesN x) { - return boost::endian:: - endian_load(x.data()); - } - - // [GP 0.3.6 3.8.1] - inline generic::OpaqueHash mathcal_H(qtils::BytesIn m) { - return Blake::hash(m); - } - // [GP 0.3.6 G 307] // [GP 0.3.6 G 310] inline std::optional banderout( diff --git a/test-vectors/safrole/safrole.test.cpp b/test-vectors/safrole/safrole.test.cpp index 17c84c3..da28437 100644 --- a/test-vectors/safrole/safrole.test.cpp +++ b/test-vectors/safrole/safrole.test.cpp @@ -5,202 +5,11 @@ */ #include -#include #include "safrole.hpp" +#include "types.diff.hpp" #include "vectors.hpp" -namespace generic = jam::test_vectors_safrole::generic; - -struct Indent { - int indent = 0; - Indent operator~() const { - return Indent{indent + 1}; - } -}; -template <> -struct fmt::formatter { - static constexpr auto parse(format_parse_context &ctx) { - return ctx.begin(); - } - auto format(const Indent &v, format_context &ctx) const { - auto out = ctx.out(); - for (int i = 0; i < v.indent; ++i) { - out = fmt::detail::write(out, " "); - } - return out; - } -}; - -#define DIFF_F(...) \ - void diff(Indent indent, const __VA_ARGS__ &v1, const __VA_ARGS__ &v2) -DIFF_F(uint32_t); -DIFF_F(generic::CustomErrorCode); -template -DIFF_F(generic::EpochMark); -DIFF_F(generic::TicketEnvelope); -DIFF_F(generic::TicketBody); -template -DIFF_F(generic::OutputMarks); -template -DIFF_F(generic::Output); -DIFF_F(generic::ValidatorData); -template -DIFF_F(generic::TicketsOrKeys); -template -DIFF_F(generic::State); - -enum class color { red = 31, green = 32 }; -template -void _diff(color color, fmt::format_string f, T &&...a) { - fmt::print("\x1b[0;{}m", (int)color); - fmt::print(f, static_cast(a)...); - fmt::println("\x1b[0;0m"); -} -template -void diff1(fmt::format_string f, T &&...a) { - _diff(color::red, f, static_cast(a)...); -} -template -void diff2(fmt::format_string f, T &&...a) { - _diff(color::green, f, static_cast(a)...); -} - -#define DIFF_M(m) diff_m(indent, v1.m, v2.m, #m) -template -void diff_m(Indent indent, const T &v1, const T &v2, std::string_view name) { - if (v1 == v2) { - return; - } - fmt::println("{}{}", indent, name); - diff(~indent, v1, v2); -} -template -void diff(Indent indent, - const boost::variant &v1, - const boost::variant &v2, - const std::vector &names) { - if (v1 == v2) { - return; - } - if (v1.which() != v2.which()) { - fmt::println("{}{} != {}", indent, names[v1.which()], names[v2.which()]); - return; - } - fmt::println("{}{}", indent, names[v1.which()]); - if (v1.which() == 0) { - diff(~indent, boost::get(v1), boost::get(v2)); - } else { - diff(~indent, boost::get(v1), boost::get(v2)); - } -} -template -void diff( - Indent indent, const std::optional &v1, const std::optional &v2) { - if (v1 == v2) { - return; - } - std::string_view o[] = {"None", "Some"}; - auto i1 = v1 ? 1 : 0, i2 = v2 ? 1 : 0; - if (i1 != i2) { - fmt::println("{}{} != {}", indent, o[i1], o[i2]); - return; - } - fmt::println("{}{}", indent, o[i1]); - if (v1) { - diff(~indent, *v1, *v2); - } -} -template - requires( - requires(T v) { std::span{v}; } - and not requires(T v) { qtils::BytesIn{v}; }) -void diff(Indent indent, const T &_v1, const T &_v2) { - if (_v1 == _v2) { - return; - } - std::span v1{_v1}, v2{_v2}; - if (v1.size() != v2.size()) { - fmt::println("{}{} != {}", indent, v1.size(), v2.size()); - return; - } - fmt::println("{}{}", indent, v1.size()); - for (size_t i = 0; i < v1.size(); ++i) { - if (v1[i] == v2[i]) { - continue; - } - fmt::println("{}[{}]", indent, i); - diff(~indent, v1[i], v2[i]); - } -} -DIFF_F(qtils::BytesIn) { - if (v1.size() == v2.size() && memcmp(v1.data(), v2.data(), v1.size()) == 0) { - return; - } - diff1("{}- {:x}", indent, v1); - diff2("{}+ {:x}", indent, v2); -} -DIFF_F(uint32_t) { - fmt::println("{}{} != {}", indent, v1, v2); -} -DIFF_F(generic::CustomErrorCode) { - std::string_view o[] = { - "bad_slot", - "unexpected_ticket", - "bad_ticket_order", - "bad_ticket_proof", - "bad_ticket_attempt", - "reserved", - "duplicate_ticket", - }; - fmt::println("{}{} != {}", indent, o[(int)v1], o[(int)v2]); -} - -template -DIFF_F(generic::EpochMark) { - DIFF_M(entropy); - DIFF_M(validators); -} -DIFF_F(generic::TicketEnvelope) { - DIFF_M(attempt); - DIFF_M(signature); -} -DIFF_F(generic::TicketBody) { - DIFF_M(id); - DIFF_M(attempt); -} -template -DIFF_F(generic::OutputMarks) { - DIFF_M(epoch_mark); - DIFF_M(tickets_mark); -} -template -DIFF_F(generic::Output) { - diff(indent, v1.v, v2.v, {"ok", "err"}); -} -DIFF_F(generic::ValidatorData) { - DIFF_M(bandersnatch); - DIFF_M(ed25519); - DIFF_M(bls); - DIFF_M(metadata); -} -template -DIFF_F(generic::TicketsOrKeys) { - diff(indent, v1.v, v2.v, {"tickets", "keys"}); -} -template -DIFF_F(generic::State) { - DIFF_M(tau); - DIFF_M(eta); - DIFF_M(lambda); - DIFF_M(kappa); - DIFF_M(gamma_k); - DIFF_M(iota); - DIFF_M(gamma_a); - DIFF_M(gamma_s); - DIFF_M(gamma_z); -} - /** * Check safrole state transition against test vectors. */ From d4a24fb3b3684c0d97435f5c867dc5338d2cac1a Mon Sep 17 00:00:00 2001 From: Dmitriy Khaustov aka xDimon Date: Wed, 6 Nov 2024 11:00:43 +0800 Subject: [PATCH 07/14] fix: add missed includes --- src/jam/bandersnatch.hpp | 4 +++- test-vectors/common.hpp | 2 ++ test-vectors/safrole/safrole.hpp | 17 +++++++++++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/jam/bandersnatch.hpp b/src/jam/bandersnatch.hpp index 502329b..3ed483a 100644 --- a/src/jam/bandersnatch.hpp +++ b/src/jam/bandersnatch.hpp @@ -6,8 +6,10 @@ #pragma once -#include +#include #include + +#include #include namespace jam::bandersnatch { diff --git a/test-vectors/common.hpp b/test-vectors/common.hpp index dcba600..35a6e84 100644 --- a/test-vectors/common.hpp +++ b/test-vectors/common.hpp @@ -6,6 +6,8 @@ #pragma once +#include + #include #include #include diff --git a/test-vectors/safrole/safrole.hpp b/test-vectors/safrole/safrole.hpp index 1f41999..50a9325 100644 --- a/test-vectors/safrole/safrole.hpp +++ b/test-vectors/safrole/safrole.hpp @@ -6,10 +6,12 @@ #pragma once +#include +#include + #include #include #include -#include namespace jam::safrole { namespace types = test_vectors_safrole; @@ -140,11 +142,14 @@ namespace jam::safrole { return k_tick; }; // [GP 0.3.6 6.3 57] - const auto [gamma_tick_k, kappa_tick, lambda_tick, gamma_tick_z] = change_epoch ? - [&](const types::ValidatorsData&gamma_tick_k) { - return std::tuple{gamma_tick_k, gamma_k, kappa, mathcal_O(config,bandersnatch_keys(gamma_tick_k))}; - }(phi(iota)) : - std::tuple{gamma_k,kappa,lambda,gamma_z}; + const auto [gamma_tick_k, kappa_tick, lambda_tick, gamma_tick_z] = + change_epoch ? [&](const types::ValidatorsData &gamma_tick_k) { + return std::tuple{gamma_tick_k, + gamma_k, + kappa, + mathcal_O(config, bandersnatch_keys(gamma_tick_k))}; + }(phi(iota)) + : std::tuple{gamma_k, kappa, lambda, gamma_z}; // [GP 0.3.6 6.4 66] const auto eta_tick_0 = mathcal_H(frown(eta_0, banderout_H_v)); From 701c53c3bce13b08205b68703137fc76c13a0336 Mon Sep 17 00:00:00 2001 From: Dmitriy Khaustov aka xDimon Date: Wed, 6 Nov 2024 11:03:42 +0800 Subject: [PATCH 08/14] git: update .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 20b2df6..a9e6e96 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,7 @@ *.out *.app +# IDE auxiliaries +.idea + /build From f709759af6de330b1cb12f87d695d777fa92f538 Mon Sep 17 00:00:00 2001 From: turuslan Date: Wed, 6 Nov 2024 08:22:54 +0500 Subject: [PATCH 09/14] clang-format doesn't work Signed-off-by: turuslan --- test-vectors/safrole/safrole.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test-vectors/safrole/safrole.hpp b/test-vectors/safrole/safrole.hpp index 50a9325..c719d03 100644 --- a/test-vectors/safrole/safrole.hpp +++ b/test-vectors/safrole/safrole.hpp @@ -147,7 +147,8 @@ namespace jam::safrole { return std::tuple{gamma_tick_k, gamma_k, kappa, - mathcal_O(config, bandersnatch_keys(gamma_tick_k))}; + mathcal_O(config, bandersnatch_keys(gamma_tick_k)), + }; }(phi(iota)) : std::tuple{gamma_k, kappa, lambda, gamma_z}; From ee55c5f0ce4df5adca80625b2b8ae9dff5e68ca1 Mon Sep 17 00:00:00 2001 From: turuslan Date: Wed, 6 Nov 2024 08:54:48 +0500 Subject: [PATCH 10/14] include group order Signed-off-by: turuslan --- CODESTYLE.md | 25 +++++++++++++++++++++++++ test-vectors/common.hpp | 1 + test-vectors/config-types-scale.hpp | 1 + test-vectors/diff.hpp | 1 + test-vectors/history/history.hpp | 1 + test-vectors/vectors.hpp | 3 ++- 6 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 CODESTYLE.md diff --git a/CODESTYLE.md b/CODESTYLE.md new file mode 100644 index 0000000..4427a7b --- /dev/null +++ b/CODESTYLE.md @@ -0,0 +1,25 @@ +### `#include` group order + +```c++ +// std +#include +#include +#include +#include +#include +#include +#include + +// vcpkg +#include +#include +#include +#include +#include +#include +#include + +// project +#include +#include +``` diff --git a/test-vectors/common.hpp b/test-vectors/common.hpp index 35a6e84..aff884f 100644 --- a/test-vectors/common.hpp +++ b/test-vectors/common.hpp @@ -9,6 +9,7 @@ #include #include + #include #include diff --git a/test-vectors/config-types-scale.hpp b/test-vectors/config-types-scale.hpp index faec3c4..ca253d6 100644 --- a/test-vectors/config-types-scale.hpp +++ b/test-vectors/config-types-scale.hpp @@ -7,6 +7,7 @@ #pragma once #include + #include namespace scale { diff --git a/test-vectors/diff.hpp b/test-vectors/diff.hpp index 2a677eb..2d62e37 100644 --- a/test-vectors/diff.hpp +++ b/test-vectors/diff.hpp @@ -7,6 +7,7 @@ #pragma once #include + #include /** diff --git a/test-vectors/history/history.hpp b/test-vectors/history/history.hpp index 346ed34..a8329b4 100644 --- a/test-vectors/history/history.hpp +++ b/test-vectors/history/history.hpp @@ -7,6 +7,7 @@ #pragma once #include + #include #include diff --git a/test-vectors/vectors.hpp b/test-vectors/vectors.hpp index f979f22..551c66b 100644 --- a/test-vectors/vectors.hpp +++ b/test-vectors/vectors.hpp @@ -6,11 +6,12 @@ #pragma once +#include + #include #include #include #include -#include /** * Common functions for test vectors From 97911766bd9c65db5ced21244a6f0307e76e5a2b Mon Sep 17 00:00:00 2001 From: turuslan Date: Wed, 6 Nov 2024 09:59:04 +0500 Subject: [PATCH 11/14] clang-format Signed-off-by: turuslan --- .clang-format | 10 ++++++++-- src/jam/keccak.hpp | 2 +- test-vectors/safrole/safrole.hpp | 15 ++++++++------- test-vectors/vectors.hpp | 3 ++- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/.clang-format b/.clang-format index 5c1d881..f6751be 100644 --- a/.clang-format +++ b/.clang-format @@ -7,12 +7,11 @@ BasedOnStyle: Google AlignAfterOpenBracket: DontAlign -AlignOperands: DontAlign +AlignOperands: AlignAfterOperator AllowShortFunctionsOnASingleLine: Empty BinPackArguments: false BinPackParameters: false BreakBeforeBinaryOperators: NonAssignment -BreakStringLiterals: false DerivePointerAlignment: false FixNamespaceComments: true IncludeBlocks: Preserve @@ -23,5 +22,12 @@ PointerAlignment: Right QualifierAlignment: Custom QualifierOrder: ['inline', 'static', 'constexpr', 'const', 'volatile', 'type', 'restrict'] ReferenceAlignment: Right +ReflowComments: true ShortNamespaceLines: 0 SortIncludes: CaseSensitive + +# use "// clang-format off" + "// clang-format on" +BreakStringLiterals: true + +# comment if not supported +InsertNewlineAtEOF: true diff --git a/src/jam/keccak.hpp b/src/jam/keccak.hpp index d5e3e5c..028f14a 100644 --- a/src/jam/keccak.hpp +++ b/src/jam/keccak.hpp @@ -89,7 +89,7 @@ namespace jam { void update(uint8_t byte) { int j = blockOff >> 3; state[j % 5][j / 5] ^= static_cast(byte) - << ((blockOff & 7) << 3); + << ((blockOff & 7) << 3); blockOff++; if (blockOff == BLOCK_SIZE) { absorb(); diff --git a/test-vectors/safrole/safrole.hpp b/test-vectors/safrole/safrole.hpp index c719d03..689b9f9 100644 --- a/test-vectors/safrole/safrole.hpp +++ b/test-vectors/safrole/safrole.hpp @@ -156,9 +156,9 @@ namespace jam::safrole { const auto eta_tick_0 = mathcal_H(frown(eta_0, banderout_H_v)); // [GP 0.3.6 6.4 67] - const auto [eta_tick_1, eta_tick_2, eta_tick_3] = change_epoch - ? std::tuple{eta_0, eta_1, eta_2} - : std::tuple{eta_1, eta_2, eta_3}; + const auto [eta_tick_1, eta_tick_2, eta_tick_3] = + change_epoch ? std::tuple{eta_0, eta_1, eta_2} + : std::tuple{eta_1, eta_2, eta_3}; std::vector n; for (auto &[r, p] : E_T) { @@ -230,8 +230,9 @@ namespace jam::safrole { return keys; }; // [GP 0.3.6 6.5 68] - const auto gamma_tick_s = e_tick == e + 1 && m >= Y && gamma_a.size() == E - ? types::TicketsOrKeys{Z(gamma_a)} + const auto gamma_tick_s = + e_tick == e + 1 && m >= Y && gamma_a.size() == E + ? types::TicketsOrKeys{Z(gamma_a)} : e_tick == e ? gamma_s : types::TicketsOrKeys{F(eta_tick_2, kappa_tick)}; @@ -257,8 +258,8 @@ namespace jam::safrole { // [GP 0.3.6 6.6 72] .tickets_mark = e_tick == e && m < Y && Y <= m_tick && gamma_a.size() == E - ? std::make_optional(types::TicketsMark{Z(gamma_a)}) - : std::nullopt, + ? std::make_optional(types::TicketsMark{Z(gamma_a)}) + : std::nullopt, }}, }; } diff --git a/test-vectors/vectors.hpp b/test-vectors/vectors.hpp index 551c66b..4066da3 100644 --- a/test-vectors/vectors.hpp +++ b/test-vectors/vectors.hpp @@ -64,7 +64,8 @@ namespace jam::test_vectors { for (auto &[path, ok] : path_ok) { if (not ok) { fmt::println( - "{}:{} warning: {} is missing, but files with other extensions are available", + "{}:{} warning: {} is missing, but files with other extensions " + "are available", __FILE__, __LINE__, path.native()); From b680e6259e6ea97076b76968bdcd355045451d87 Mon Sep 17 00:00:00 2001 From: turuslan Date: Fri, 15 Nov 2024 11:18:00 +0500 Subject: [PATCH 12/14] graypaper references Signed-off-by: turuslan --- test-vectors/common.hpp | 8 ++++---- test-vectors/history/history.hpp | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/test-vectors/common.hpp b/test-vectors/common.hpp index aff884f..5032e5e 100644 --- a/test-vectors/common.hpp +++ b/test-vectors/common.hpp @@ -33,8 +33,8 @@ namespace jam { return s[i % s.size()]; } - // [GP 0.4.3 3.7.2] - // https://github.com/gavofyork/graypaper/blob/v0.4.3/text/notation.tex#L132 + // [GP 0.4.5 3.7.2] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/notation.tex#L132 template qtils::BytesN frown( const qtils::BytesN &x, const qtils::BytesN &y) { @@ -71,8 +71,8 @@ namespace jam { return Blake::hash(m); } - // [GP 0.4.3 3.8.1] - // https://github.com/gavofyork/graypaper/blob/v0.4.3/text/notation.tex#L153 + // [GP 0.4.5 3.8.1] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/notation.tex#L153 inline auto mathcal_H_K(qtils::BytesIn m) { return Keccak::hash(m); } diff --git a/test-vectors/history/history.hpp b/test-vectors/history/history.hpp index a8329b4..bbcafc1 100644 --- a/test-vectors/history/history.hpp +++ b/test-vectors/history/history.hpp @@ -17,12 +17,12 @@ namespace jam::history { /** * The size of recent history, in blocks */ - // [GP 0.4.3 I.4.4] - // https://github.com/gavofyork/graypaper/blob/v0.4.3/text/definitions.tex#L266 + // [GP 0.4.5 I.4.4] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/definitions.tex#L266 constexpr uint32_t H = 8; - // [GP 0.4.3 E.2 327] - // https://github.com/gavofyork/graypaper/blob/v0.4.3/text/merklization.tex#L205 + // [GP 0.4.5 E.2 333] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/merklization.tex#L205 inline types::Mmr mathcal_A(types::Mmr r, types::Hash l) { for (size_t n = 0; n < r.peaks.size(); ++n) { if (not r.peaks[n]) { @@ -41,18 +41,18 @@ namespace jam::history { */ inline std::pair transition( const types::State &state, const types::Input &input) { - // [GP 0.4.3 7 84] - // https://github.com/gavofyork/graypaper/blob/v0.4.3/text/recent_history.tex#L32 + // [GP 0.4.5 7 84] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/recent_history.tex#L32 std::vector beta_tick(state.beta.size() >= H ? std::next(state.beta.begin()) : state.beta.begin(), state.beta.end()); - // [GP 0.4.3 7 82] - // https://github.com/gavofyork/graypaper/blob/v0.4.3/text/recent_history.tex#L12 + // [GP 0.4.5 7 82] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/recent_history.tex#L12 if (not beta_tick.empty()) { beta_tick.back().state_root = input.parent_state_root; } - // [GP 0.4.3 7 83] - // https://github.com/gavofyork/graypaper/blob/v0.4.3/text/recent_history.tex#L20 + // [GP 0.4.5 7 83] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/recent_history.tex#L20 types::Mmr mmr; if (not state.beta.empty()) { mmr = state.beta.back().mmr; From 2cca4ee3dea26b572efd1895ee4e057618e19f56 Mon Sep 17 00:00:00 2001 From: turuslan Date: Fri, 15 Nov 2024 11:57:09 +0500 Subject: [PATCH 13/14] graypaper references Signed-off-by: turuslan --- test-vectors/common.hpp | 15 ++++-- test-vectors/safrole/safrole.hpp | 84 +++++++++++++++++++++----------- 2 files changed, 66 insertions(+), 33 deletions(-) diff --git a/test-vectors/common.hpp b/test-vectors/common.hpp index 5032e5e..25ae65d 100644 --- a/test-vectors/common.hpp +++ b/test-vectors/common.hpp @@ -28,7 +28,8 @@ namespace jam { return first; } - // [GP 0.3.6 3.7] + // [GP 0.4.5 3.7] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/notation.tex#L116 auto &circlearrowleft(const auto &s, auto i) { return s[i % s.size()]; } @@ -44,13 +45,15 @@ namespace jam { return xy; } - // [GP 0.3.6 3.7.2] + // [GP 0.4.5 3.7.2] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/notation.tex#L132 template qtils::BytesN doubleplus(const qtils::BytesN &x, uint8_t i) { return frown(x, std::array{i}); } - // [GP 0.3.6 C.1.2 271] + // [GP 0.4.5 C.1.2 300] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/serialization.tex#L29 template qtils::BytesN mathcal_E(uint64_t x) { qtils::BytesN out; @@ -59,14 +62,16 @@ namespace jam { return out; } - // [GP 0.3.6 C.1.2 271] + // [GP 0.4.5 C.1.2 300] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/serialization.tex#L29 template auto de(qtils::BytesN x) { return boost::endian:: endian_load(x.data()); } - // [GP 0.3.6 3.8.1] + // [GP 0.4.5 3.8.1] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/notation.tex#L153 inline auto mathcal_H(qtils::BytesIn m) { return Blake::hash(m); } diff --git a/test-vectors/safrole/safrole.hpp b/test-vectors/safrole/safrole.hpp index 689b9f9..ce7d36b 100644 --- a/test-vectors/safrole/safrole.hpp +++ b/test-vectors/safrole/safrole.hpp @@ -17,8 +17,10 @@ namespace jam::safrole { namespace types = test_vectors_safrole; using BandersnatchSignature = decltype(types::TicketEnvelope::signature); - // [GP 0.3.6 G 307] - // [GP 0.3.6 G 310] + // [GP 0.4.5 G 339] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/bandersnatch.tex#L9 + // [GP 0.4.5 G 342] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/bandersnatch.tex#L17 inline std::optional banderout( const BandersnatchSignature &signature) { return bandersnatch::output(signature); @@ -45,14 +47,17 @@ namespace jam::safrole { return it->second; } - // [GP 0.3.6 I.4.5] + // [GP 0.4.5 I.4.5] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/definitions.tex#L303 // clang-format off constexpr qtils::BytesN<15> X_T = {'j','a','m','_','t','i','c','k','e','t','_','s','e','a','l'}; // clang-format on - // [GP 0.3.6 I.4.4] + // [GP 0.4.5 I.4.4] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/definitions.tex#L271 constexpr uint32_t N = 2; - // [GP 0.3.6 I.4.4] + // [GP 0.4.5 I.4.4] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/definitions.tex#L269 constexpr uint32_t K = 16; inline BandersnatchKeys bandersnatch_keys( @@ -64,13 +69,15 @@ namespace jam::safrole { return keys; } - // [GP 0.3.6 G 308] + // [GP 0.4.5 G 340] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/bandersnatch.tex#L15 inline GammaZ mathcal_O( const types::Config &config, const BandersnatchKeys &pks) { return ring_ctx(config).commitment(pks.v).value(); } - // [GP 0.3.6 G 309] + // [GP 0.4.5 G 341] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/bandersnatch.tex#L16 inline std::optional bandersnatch( const types::Config &config, const GammaZ &gamma_z, @@ -79,7 +86,8 @@ namespace jam::safrole { return ring_ctx(config).verifier(gamma_z).verify(input, signature); } - // [GP 0.3.6 6.1 46] + // [GP 0.4.5 6.1 47] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L30 struct Epoch { uint32_t epoch, phase; Epoch(uint32_t E, uint32_t slot) : epoch{slot / E}, phase{slot % E} {} @@ -92,9 +100,11 @@ namespace jam::safrole { const types::Config &config, const types::State &state, const types::Input &input) { - // [GP 0.3.6 I.4.4] + // [GP 0.4.5 I.4.4] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/definitions.tex#L260 const auto E = config.epoch_length; - // [GP 0.3.6 I.4.4] + // [GP 0.4.5 I.4.4] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/definitions.tex#L287 const auto Y = E * 5 / 6; const auto &[H_t, banderout_H_v, E_T, offenders_tick] = input; @@ -107,21 +117,25 @@ namespace jam::safrole { return std::make_pair(state, types::Output{error}); }; - // [GP 0.3.6 5 41] + // [GP 0.4.5 5 42] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/header.tex#L29 if (H_t <= state.tau) { return error(Error::bad_slot); } - // [GP 0.3.6 6.1 45] + // [GP 0.4.5 6.1 46] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L25 const auto tau_tick = H_t; - // [GP 0.3.6 6.1 46] + // [GP 0.4.5 6.1 47] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L30 const Epoch epoch{E, state.tau}, epoch_tick{E, tau_tick}; const auto &[e, m] = epoch; const auto &[e_tick, m_tick] = epoch_tick; const auto change_epoch = e_tick != e; - // [GP 0.3.6 6.7 74] + // [GP 0.4.5 6.7 75] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L251 if (E_T.size() > K) { throw std::logic_error("not covered by test vectors"); } @@ -129,7 +143,8 @@ namespace jam::safrole { return std::make_pair(state, types::Output{Error::unexpected_ticket}); } - // [GP 0.3.6 6.3 58] + // [GP 0.4.5 6.3 59] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L101 const auto phi = [&](const types::ValidatorsData &k) { types::ValidatorsData k_tick; for (auto &validator : k.v) { @@ -141,7 +156,8 @@ namespace jam::safrole { } return k_tick; }; - // [GP 0.3.6 6.3 57] + // [GP 0.4.5 6.3 58] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L97 const auto [gamma_tick_k, kappa_tick, lambda_tick, gamma_tick_z] = change_epoch ? [&](const types::ValidatorsData &gamma_tick_k) { return std::tuple{gamma_tick_k, @@ -152,17 +168,20 @@ namespace jam::safrole { }(phi(iota)) : std::tuple{gamma_k, kappa, lambda, gamma_z}; - // [GP 0.3.6 6.4 66] + // [GP 0.4.5 6.4 67] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L147 const auto eta_tick_0 = mathcal_H(frown(eta_0, banderout_H_v)); - // [GP 0.3.6 6.4 67] + // [GP 0.4.5 6.4 68] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L152 const auto [eta_tick_1, eta_tick_2, eta_tick_3] = change_epoch ? std::tuple{eta_0, eta_1, eta_2} : std::tuple{eta_1, eta_2, eta_3}; std::vector n; for (auto &[r, p] : E_T) { - // [GP 0.3.6 6.7 73] + // [GP 0.4.5 6.7 74] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L250 if (r >= N) { return error(Error::bad_ticket_attempt); } @@ -171,15 +190,18 @@ namespace jam::safrole { if (not y) { return error(Error::bad_ticket_proof); } - // [GP 0.3.6 6.7 75] + // [GP 0.4.5 6.7 76] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L259 n.emplace_back(types::TicketBody{*y, r}); } - // [GP 0.3.6 6.7 76] + // [GP 0.4.5 6.7 77] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L264 if (not std::is_sorted(n.begin(), n.end(), TicketBodyLess{})) { return error(Error::bad_ticket_order); } - // [GP 0.3.6 6.7 78] + // [GP 0.4.5 6.7 79] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L271 std::vector gamma_tick_a; if (change_epoch) { gamma_tick_a = n; @@ -190,7 +212,8 @@ namespace jam::safrole { gamma_a.end(), std::back_inserter(gamma_tick_a), TicketBodyLess{}); - // [GP 0.3.6 6.7 77] + // [GP 0.4.5 6.7 78] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L265 if (gamma_tick_a.size() != gamma_a.size() + n.size()) { return error(Error::duplicate_ticket); } @@ -199,7 +222,8 @@ namespace jam::safrole { gamma_tick_a.resize(E); } - // [GP 0.3.6 6.5 69] + // [GP 0.4.5 6.5 70] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L182 const auto Z = [&](const GammaA &s) { types::TicketsBodies tickets; if (s.size() != E) { @@ -218,7 +242,8 @@ namespace jam::safrole { } return tickets; }; - // [GP 0.3.6 6.5 70] + // [GP 0.4.5 6.5 71] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L190 const auto F = [&](const types::OpaqueHash &r, const types::ValidatorsData &k) { types::EpochKeys keys; @@ -229,7 +254,8 @@ namespace jam::safrole { } return keys; }; - // [GP 0.3.6 6.5 68] + // [GP 0.4.5 6.5 69] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L173 const auto gamma_tick_s = e_tick == e + 1 && m >= Y && gamma_a.size() == E ? types::TicketsOrKeys{Z(gamma_a)} @@ -251,11 +277,13 @@ namespace jam::safrole { .gamma_z = gamma_tick_z, }, types::Output{types::OutputMarks{ - // [GP 0.3.6 6.6 71] + // [GP 0.4.5 6.6 72] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L216 .epoch_mark = change_epoch ? std::make_optional(types::EpochMark{ eta_tick_1, bandersnatch_keys(gamma_tick_k)}) : std::nullopt, - // [GP 0.3.6 6.6 72] + // [GP 0.4.5 6.6 73] + // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L224 .tickets_mark = e_tick == e && m < Y && Y <= m_tick && gamma_a.size() == E ? std::make_optional(types::TicketsMark{Z(gamma_a)}) From b5352b2efe1a33d3eb08cf48b490e60c52b62261 Mon Sep 17 00:00:00 2001 From: Dmitriy Khaustov aka xDimon Date: Thu, 21 Nov 2024 16:37:04 +0800 Subject: [PATCH 14/14] minor for safrole --- test-vectors/safrole/safrole.hpp | 33 ++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/test-vectors/safrole/safrole.hpp b/test-vectors/safrole/safrole.hpp index ce7d36b..9d37834 100644 --- a/test-vectors/safrole/safrole.hpp +++ b/test-vectors/safrole/safrole.hpp @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include @@ -53,9 +54,12 @@ namespace jam::safrole { constexpr qtils::BytesN<15> X_T = {'j','a','m','_','t','i','c','k','e','t','_','s','e','a','l'}; // clang-format on + // The number of ticket entries per validator. // [GP 0.4.5 I.4.4] // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/definitions.tex#L271 constexpr uint32_t N = 2; + + // The maximum number of tickets which may be submitted in a single extrinsic. // [GP 0.4.5 I.4.4] // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/definitions.tex#L269 constexpr uint32_t K = 16; @@ -100,13 +104,17 @@ namespace jam::safrole { const types::Config &config, const types::State &state, const types::Input &input) { + /// The length of an epoch in timeslots. // [GP 0.4.5 I.4.4] // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/definitions.tex#L260 const auto E = config.epoch_length; + + /// The number of slots into an epoch at which ticket-submission ends. // [GP 0.4.5 I.4.4] // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/definitions.tex#L287 const auto Y = E * 5 / 6; + /// H_t - a time-slot index const auto &[H_t, banderout_H_v, E_T, offenders_tick] = input; const auto &[tau, eta, lambda, kappa, gamma_k, iota, gamma_a, gamma_s, gamma_z] = @@ -117,21 +125,33 @@ namespace jam::safrole { return std::make_pair(state, types::Output{error}); }; + // A block may only be regarded as valid once the time-slot index Ht is in + // the past. + // It's always strictly greater than that of its parent. // [GP 0.4.5 5 42] // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/header.tex#L29 if (H_t <= state.tau) { return error(Error::bad_slot); } + // The most recent block’s slot index, which we transition to the slot index + // as defined in the block’s header. // [GP 0.4.5 6.1 46] // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L25 const auto tau_tick = H_t; // [GP 0.4.5 6.1 47] // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L30 - const Epoch epoch{E, state.tau}, epoch_tick{E, tau_tick}; + const Epoch epoch{E, state.tau}; + const Epoch epoch_tick{E, tau_tick}; + + // the prior’s epoch index and slot phase index within that epoch const auto &[e, m] = epoch; + + // epoch index and slot phase index are the corresponding values for the + // present block const auto &[e_tick, m_tick] = epoch_tick; + const auto change_epoch = e_tick != e; // [GP 0.4.5 6.7 75] @@ -160,7 +180,8 @@ namespace jam::safrole { // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L97 const auto [gamma_tick_k, kappa_tick, lambda_tick, gamma_tick_z] = change_epoch ? [&](const types::ValidatorsData &gamma_tick_k) { - return std::tuple{gamma_tick_k, + return std::tuple{ + gamma_tick_k, gamma_k, kappa, mathcal_O(config, bandersnatch_keys(gamma_tick_k)), @@ -206,12 +227,8 @@ namespace jam::safrole { if (change_epoch) { gamma_tick_a = n; } else { - std::set_union(n.begin(), - n.end(), - gamma_a.begin(), - gamma_a.end(), - std::back_inserter(gamma_tick_a), - TicketBodyLess{}); + std::ranges::set_union( + n, gamma_a, std::back_inserter(gamma_tick_a), TicketBodyLess{}); // [GP 0.4.5 6.7 78] // https://github.com/gavofyork/graypaper/blob/v0.4.5/text/safrole.tex#L265 if (gamma_tick_a.size() != gamma_a.size() + n.size()) {