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/.gitignore b/.gitignore index 20b2df6..a9e6e96 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,7 @@ *.out *.app +# IDE auxiliaries +.idea + /build diff --git a/CMakeLists.txt b/CMakeLists.txt index 484c4c5..3330c42 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,12 +19,14 @@ 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) +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_library(headers INTERFACE) @@ -32,5 +39,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/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/python/asn1.py b/python/asn1.py index 5530dd2..9addc45 100755 --- a/python/asn1.py +++ b/python/asn1.py @@ -216,6 +216,15 @@ def asn_member(t): ], ) ty.scale += 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"]] @@ -225,6 +234,15 @@ def asn_member(t): *(" %s," % c_dash(x[0]) for x in t["values"]), "};", ] + 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)" % ( @@ -244,6 +262,9 @@ def asn_member(t): ty.diff = c_diff( NS, ty, ["DIFF_M(%s);" % 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)) diff --git a/src/jam/bandersnatch.hpp b/src/jam/bandersnatch.hpp new file mode 100644 index 0000000..3ed483a --- /dev/null +++ b/src/jam/bandersnatch.hpp @@ -0,0 +1,94 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include + +#include +#include + +namespace jam::bandersnatch { + using Output = qtils::BytesN; + using Public = qtils::BytesN; + using RingCommitment = qtils::BytesN; + using RingSignature = qtils::BytesN; + + inline std::optional output(qtils::BytesIn signature) { + Output output; + if (not jam_bandersnatch_output(signature.data(), output.data())) { + return std::nullopt; + } + return output; + } + + struct RingVerifier; + + struct Ring { + explicit Ring(uint32_t count) + : count{count}, ffi{jam_bandersnatch_ring_new(count), Dtor{}} {} + + std::optional commitment( + std::span pks) const { + if (pks.size() != count) { + return std::nullopt; + } + RingCommitment commitment; + static_assert(sizeof(Public) % alignof(Public) == 0); + jam_bandersnatch_ring_commitment(ffi.get(), + pks[0].data(), + pks.size() * sizeof(Public), + commitment.data()); + return commitment; + } + + RingVerifier verifier(const RingCommitment &commitment) const; + + struct Dtor { + void operator()(JamBandersnatchRing *ptr) const { + jam_bandersnatch_ring_drop(ptr); + } + }; + + uint32_t count; + std::unique_ptr ffi; + }; + + struct RingVerifier { + RingVerifier(const Ring &ring, const RingCommitment &commitment) + : ffi{ + jam_bandersnatch_ring_verifier_new( + ring.ffi.get(), commitment.data()), + Dtor{}, + } {} + + std::optional verify( + qtils::BytesIn input, const RingSignature &signature) const { + Output output; + if (not jam_bandersnatch_ring_verifier_verify(ffi.get(), + input.data(), + input.size(), + signature.data(), + output.data())) { + return std::nullopt; + } + return output; + } + + struct Dtor { + void operator()(JamBandersnatchRingVerifier *ptr) const { + jam_bandersnatch_ring_verifier_drop(ptr); + } + }; + + std::unique_ptr ffi; + }; + + RingVerifier Ring::verifier(const RingCommitment &commitment) const { + return RingVerifier{*this, commitment}; + } +} // namespace jam::bandersnatch 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/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/asn1.cmake b/test-vectors/asn1.cmake index 3d796ed..14dc383 100644 --- a/test-vectors/asn1.cmake +++ b/test-vectors/asn1.cmake @@ -10,7 +10,7 @@ function(asn1 name) 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) + 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} @@ -30,7 +30,21 @@ function(asn1 name) 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 index 2a7f982..25ae65d 100644 --- a/test-vectors/common.hpp +++ b/test-vectors/common.hpp @@ -6,6 +6,11 @@ #pragma once +#include + +#include + +#include #include /** @@ -13,8 +18,24 @@ */ namespace jam { - // [GP 0.4.3 3.7.2] - // https://github.com/gavofyork/graypaper/blob/v0.4.3/text/notation.tex#L132 + template + qtils::BytesN first_bytes(qtils::BytesIn bytes) { + if (bytes.size() < N) { + throw std::logic_error("first_bytes"); + } + qtils::BytesN first; + memcpy(first.data(), bytes.data(), N); + return first; + } + + // [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()]; + } + + // [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) { @@ -24,8 +45,39 @@ namespace jam { return xy; } - // [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.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.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; + boost::endian::endian_store( + out.data(), x); + return out; + } + + // [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.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); + } + + // [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/config-types-scale.hpp b/test-vectors/config-types-scale.hpp index 4dc92d2..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 { @@ -73,7 +74,7 @@ namespace jam { void decodeConfig(scale::ScaleDecoderStream &s, ConfigVec &v, const auto &config) { - auto n = config.get(ConfigField{}); + auto n = v.configSize(config); v.v.resize(0); v.v.reserve(n); for (size_t i = 0; i < n; ++i) { diff --git a/test-vectors/config-types.hpp b/test-vectors/config-types.hpp index 1bcd962..01cc9a0 100644 --- a/test-vectors/config-types.hpp +++ b/test-vectors/config-types.hpp @@ -13,6 +13,10 @@ namespace jam { struct ConfigVec { std::vector v; + static size_t configSize(const auto &config) { + return config.get(ConfigField{}); + } + bool operator==(const ConfigVec &) const = default; }; } // namespace jam diff --git a/test-vectors/diff.hpp b/test-vectors/diff.hpp index 12b20fe..2d62e37 100644 --- a/test-vectors/diff.hpp +++ b/test-vectors/diff.hpp @@ -7,6 +7,7 @@ #pragma once #include + #include /** @@ -63,6 +64,10 @@ void diff_m(Indent indent, const T &v1, const T &v2, std::string_view name) { diff(~indent, v1, v2); } +DIFF_F(uint32_t) { + fmt::println("{}{} != {}", indent, v1, v2); +} + template DIFF_F(jam::ConfigVec) { diff(indent, v1.v, v2.v); @@ -115,3 +120,32 @@ DIFF_F(qtils::BytesIn) { 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/history/CMakeLists.txt b/test-vectors/history/CMakeLists.txt index 79a88a5..87dbe28 100644 --- a/test-vectors/history/CMakeLists.txt +++ b/test-vectors/history/CMakeLists.txt @@ -5,14 +5,3 @@ # asn1(history history) - -add_executable(test_vectors_history_test - history.test.cpp -) -target_compile_definitions(test_vectors_history_test PRIVATE PROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}") -target_link_libraries(test_vectors_history_test - fmt::fmt - headers - test_vectors_history_types -) -add_test(test_vectors_history_test test_vectors_history_test) diff --git a/test-vectors/history/history.hpp b/test-vectors/history/history.hpp index 346ed34..bbcafc1 100644 --- a/test-vectors/history/history.hpp +++ b/test-vectors/history/history.hpp @@ -7,6 +7,7 @@ #pragma once #include + #include #include @@ -16,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]) { @@ -40,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; diff --git a/test-vectors/history/history.test.cpp b/test-vectors/history/history.test.cpp index d73f9d6..4ccaf3e 100644 --- a/test-vectors/history/history.test.cpp +++ b/test-vectors/history/history.test.cpp @@ -8,11 +8,7 @@ #include #include -using jam::test_vectors_history::Vectors; - -namespace types = jam::test_vectors_history; - -bool ok = true; +GTEST_VECTORS(jam::test_vectors_history::Vectors); /** * Check history state transition against test vectors. @@ -20,28 +16,17 @@ bool ok = true; * @when transition with `input` * @then get expected `post_state` and `output` */ -void test_transition() { - Vectors vectors; - 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::history::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"); - } +TEST_P(Test, Transition) { + auto testcase = vectors.read(path); + auto [state, output] = + jam::history::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"); } -} - -int main() { - test_transition(); - fmt::println("ok"); - return 0; } diff --git a/test-vectors/history/types.test.cpp b/test-vectors/history/types.test.cpp index 32e9b89..681d5b4 100644 --- a/test-vectors/history/types.test.cpp +++ b/test-vectors/history/types.test.cpp @@ -6,11 +6,9 @@ #include -using jam::test_vectors::test_reencode; -using jam::test_vectors_history::Vectors; +GTEST_VECTORS(jam::test_vectors_history::Vectors); -int main() { - test_reencode(); - fmt::println("ok"); - return 0; -} +/** + * Check python generated scale encoding/decoding against test vectors. + */ +GTEST_VECTORS_REENCODE; diff --git a/test-vectors/history/vectors.hpp b/test-vectors/history/vectors.hpp index be0f2e5..8caef0b 100644 --- a/test-vectors/history/vectors.hpp +++ b/test-vectors/history/vectors.hpp @@ -14,5 +14,9 @@ namespace jam::test_vectors_history { Vectors() : VectorsT{Config{}} { this->list("history/data"); } + + static std::vector> vectors() { + return {std::make_shared()}; + } }; } // namespace jam::test_vectors_history diff --git a/test-vectors/safrole/CMakeLists.txt b/test-vectors/safrole/CMakeLists.txt index 6385f02..ea94dcf 100644 --- a/test-vectors/safrole/CMakeLists.txt +++ b/test-vectors/safrole/CMakeLists.txt @@ -5,3 +5,7 @@ # asn1(safrole safrole tiny full) +target_link_libraries(test_vectors_safrole_test + jam_crust::jam_crust + PkgConfig::libb2 +) diff --git a/test-vectors/safrole/safrole.hpp b/test-vectors/safrole/safrole.hpp new file mode 100644 index 0000000..9d37834 --- /dev/null +++ b/test-vectors/safrole/safrole.hpp @@ -0,0 +1,311 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include + +#include +#include +#include + +namespace jam::safrole { + namespace types = test_vectors_safrole; + using BandersnatchSignature = decltype(types::TicketEnvelope::signature); + + // [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); + } + + struct TicketBodyLess { + bool operator()( + const types::TicketBody &l, const types::TicketBody &r) const { + return l.id < r.id; + } + }; + + using GammaA = decltype(types::State::gamma_a); + using GammaZ = decltype(types::State::gamma_z); + using BandersnatchKeys = decltype(types::EpochMark::validators); + + inline auto &ring_ctx(const types::Config &config) { + static std::unordered_map map; + auto &n = config.validators_count; + auto it = map.find(n); + if (it == map.end()) { + it = map.emplace(n, bandersnatch::Ring{n}).first; + } + return it->second; + } + + // [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 + + // 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; + + inline BandersnatchKeys bandersnatch_keys( + const types::ValidatorsData &validators) { + BandersnatchKeys keys; + for (auto &validator : validators.v) { + keys.v.emplace_back(validator.bandersnatch); + } + return keys; + } + + // [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.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, + qtils::BytesIn input, + const BandersnatchSignature &signature) { + return ring_ctx(config).verifier(gamma_z).verify(input, signature); + } + + // [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} {} + }; + + /** + * Given state and input, derive next state and output. + */ + inline std::pair transition( + 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] = + 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}); + }; + + // 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}; + 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] + // 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"); + } + if (m_tick >= Y and not E_T.empty()) { + return std::make_pair(state, types::Output{Error::unexpected_ticket}); + } + + // [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) { + k_tick.v.emplace_back( + std::ranges::find(offenders_tick, validator.ed25519) + != offenders_tick.end() + ? types::ValidatorData{} + : validator); + } + return k_tick; + }; + // [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, + gamma_k, + kappa, + mathcal_O(config, bandersnatch_keys(gamma_tick_k)), + }; + }(phi(iota)) + : std::tuple{gamma_k, kappa, lambda, gamma_z}; + + // [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.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.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); + } + const auto m = frown(X_T, doubleplus(eta_tick_2, r)); + const auto y = bandersnatch(config, gamma_z, m, p); + if (not y) { + return error(Error::bad_ticket_proof); + } + // [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.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.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; + } else { + 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()) { + return error(Error::duplicate_ticket); + } + } + if (gamma_tick_a.size() > E) { + gamma_tick_a.resize(E); + } + + // [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) { + throw std::logic_error{"Z"}; + } + auto it1 = s.begin(), it2 = std::prev(s.end()); + auto odd = true; + for (uint32_t i = 0; i < tickets.configSize(config); ++i) { + tickets.v.emplace_back(odd ? *it1 : *it2); + if (odd) { + ++it1; + } else { + --it2; + } + odd = not odd; + } + return tickets; + }; + // [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; + for (uint32_t i = 0; i < E; ++i) { + keys.v.emplace_back(circlearrowleft( + k.v, de(first_bytes<4>(mathcal_H(frown(r, mathcal_E<4>(i)))))) + .bandersnatch); + } + return keys; + }; + // [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)} + : 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, + // TODO(turuslan): #3, wait for test vectors + .iota = iota, + .gamma_a = gamma_tick_a, + .gamma_s = gamma_tick_s, + .gamma_z = gamma_tick_z, + }, + types::Output{types::OutputMarks{ + // [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.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)}) + : 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..43810fb --- /dev/null +++ b/test-vectors/safrole/safrole.test.cpp @@ -0,0 +1,32 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +GTEST_VECTORS(jam::test_vectors_safrole::Vectors); + +/** + * Check safrole state transition against test vectors. + * @given `pre_state` + * @when transition with `input` + * @then get expected `post_state` and `output` + */ +TEST_P(Test, Transition) { + auto testcase = vectors.read(path); + auto [state, output] = jam::safrole::transition( + vectors.config, 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"); + } +} diff --git a/test-vectors/safrole/types.test.cpp b/test-vectors/safrole/types.test.cpp index 256aace..7b57341 100644 --- a/test-vectors/safrole/types.test.cpp +++ b/test-vectors/safrole/types.test.cpp @@ -6,12 +6,9 @@ #include -using jam::test_vectors::test_reencode; -using jam::test_vectors_safrole::Vectors; +GTEST_VECTORS(jam::test_vectors_safrole::Vectors); -int main() { - test_reencode>(); - test_reencode>(); - fmt::println("ok"); - return 0; -} +/** + * Check python generated scale encoding/decoding against test vectors. + */ +GTEST_VECTORS_REENCODE; diff --git a/test-vectors/safrole/vectors.hpp b/test-vectors/safrole/vectors.hpp index 8ebe447..b30d549 100644 --- a/test-vectors/safrole/vectors.hpp +++ b/test-vectors/safrole/vectors.hpp @@ -10,12 +10,20 @@ #include namespace jam::test_vectors_safrole { - template struct Vectors : test_vectors::VectorsT { - static constexpr std::string_view type = is_full ? "full" : "tiny"; + std::string_view type; - Vectors() : VectorsT{is_full ? config_full : config_tiny} { + explicit Vectors(bool is_full) + : VectorsT{is_full ? config_full : config_tiny}, + type{is_full ? "full" : "tiny"} { this->list(std::filesystem::path{"safrole"} / type); } + + static std::vector> vectors() { + return { + std::make_shared(false), + std::make_shared(true), + }; + } }; } // namespace jam::test_vectors_safrole diff --git a/test-vectors/vectors.hpp b/test-vectors/vectors.hpp index aea195c..4066da3 100644 --- a/test-vectors/vectors.hpp +++ b/test-vectors/vectors.hpp @@ -6,15 +6,37 @@ #pragma once +#include + #include +#include #include #include -#include /** * Common functions for test vectors */ +#define GTEST_VECTORS(T) \ + struct Test : jam::test_vectors::TestT {}; \ + INSTANTIATE_TEST_SUITE_P(Test, Test, testing::ValuesIn([] { \ + std::vector, std::filesystem::path>> params; \ + for (auto &vectors : T::vectors()) { \ + for (auto &path : vectors->paths) { \ + params.emplace_back(vectors, path); \ + } \ + } \ + return params; \ + }())); + +#define GTEST_VECTORS_REENCODE \ + TEST_P(Test, Reencode) { \ + auto expected = vectors.readRaw(path); \ + auto decoded = vectors.decode(expected); \ + auto reencoded = scale::encode(decoded).value(); \ + EXPECT_EQ(reencoded, expected); \ + } + namespace jam::test_vectors { inline const std::filesystem::path dir = std::filesystem::path{PROJECT_SOURCE_DIR} / "test-vectors/jamtestvectors"; @@ -42,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()); @@ -68,19 +91,10 @@ namespace jam::test_vectors { } }; - /** - * Check python generated scale encoding/decoding against test vectors. - */ template - void test_reencode(const T &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"}; - } - } - } + struct TestT : testing::TestWithParam< + std::pair, std::filesystem::path>> { + const T &vectors = *this->GetParam().first; + const std::filesystem::path &path = this->GetParam().second; + }; } // namespace jam::test_vectors diff --git a/vcpkg-overlay/kagome-crates/portfile.cmake b/vcpkg-overlay/kagome-crates/portfile.cmake new file mode 100644 index 0000000..28b6d1f --- /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 77f9e098a66e6c622e52f415b296ffa14e63603a + SHA512 6c3fddddf5be0f3ed8e5912d82fc0c97ac1f65d402e0c773149983b43b694d1d4b3b4bdc7af923bd3d2f94d53737f35d996275682bb366842870999b5b243138 +) +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 3eaec59..bfdb1ba 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -3,6 +3,11 @@ "version": "0.0.1", "dependencies": [ "fmt", + "kagome-crates", + "libb2", "scale" - ] + ], + "features": { + "test": { "description": "Test", "dependencies": ["gtest"]} + } }