Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@
*.out
*.app

# IDE auxiliaries
.idea

/build
15 changes: 13 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 ()
25 changes: 25 additions & 0 deletions CODESTYLE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
### `#include` group order

```c++
// std
#include <algorithm>
#include <memory>
#include <optional>
#include <set>
#include <stdexcept>
#include <unordered_map>
#include <vector>

// vcpkg
#include <blake2.h>
#include <boost/...>
#include <fmt/...>
#include <gtest/...>
#include <jam_crust.h>
#include <qtils/...>
#include <scale/...>

// project
#include <jam/...>
#include <test-vectors/...>
```
21 changes: 21 additions & 0 deletions python/asn1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]
Expand All @@ -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)"
% (
Expand All @@ -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))

Expand Down
94 changes: 94 additions & 0 deletions src/jam/bandersnatch.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <memory>
#include <optional>

#include <jam_crust.h>
#include <qtils/bytes.hpp>

namespace jam::bandersnatch {
using Output = qtils::BytesN<JAM_BANDERSNATCH_OUTPUT>;
using Public = qtils::BytesN<JAM_BANDERSNATCH_PUBLIC>;
using RingCommitment = qtils::BytesN<JAM_BANDERSNATCH_RING_COMMITMENT>;
using RingSignature = qtils::BytesN<JAM_BANDERSNATCH_RING_SIGNATURE>;

inline std::optional<Output> 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<RingCommitment> commitment(
std::span<const Public> 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<JamBandersnatchRing, Dtor> ffi;
};

struct RingVerifier {
RingVerifier(const Ring &ring, const RingCommitment &commitment)
: ffi{
jam_bandersnatch_ring_verifier_new(
ring.ffi.get(), commitment.data()),
Dtor{},
} {}

std::optional<Output> 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<JamBandersnatchRingVerifier, Dtor> ffi;
};

RingVerifier Ring::verifier(const RingCommitment &commitment) const {
return RingVerifier{*this, commitment};
}
} // namespace jam::bandersnatch
33 changes: 33 additions & 0 deletions src/jam/blake.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <blake2.h>
#include <qtils/bytes.hpp>

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
2 changes: 1 addition & 1 deletion src/jam/keccak.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace jam {
void update(uint8_t byte) {
int j = blockOff >> 3;
state[j % 5][j / 5] ^= static_cast<uint64_t>(byte)
<< ((blockOff & 7) << 3);
<< ((blockOff & 7) << 3);
blockOff++;
if (blockOff == BLOCK_SIZE) {
absorb();
Expand Down
16 changes: 15 additions & 1 deletion test-vectors/asn1.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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()
60 changes: 56 additions & 4 deletions test-vectors/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,36 @@

#pragma once

#include <stdexcept>

#include <boost/endian/conversion.hpp>

#include <jam/blake.hpp>
#include <jam/keccak.hpp>

/**
* Common functions used in tests
*/

namespace jam {
// [GP 0.4.3 3.7.2]
// https://github.com/gavofyork/graypaper/blob/v0.4.3/text/notation.tex#L132
template <size_t N>
qtils::BytesN<N> first_bytes(qtils::BytesIn bytes) {
if (bytes.size() < N) {
throw std::logic_error("first_bytes");
}
qtils::BytesN<N> 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 <size_t X, size_t Y>
qtils::BytesN<X + Y> frown(
const qtils::BytesN<X> &x, const qtils::BytesN<Y> &y) {
Expand All @@ -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 <size_t X>
qtils::BytesN<X + 1> doubleplus(const qtils::BytesN<X> &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 <size_t N>
qtils::BytesN<N> mathcal_E(uint64_t x) {
qtils::BytesN<N> out;
boost::endian::endian_store<uint64_t, N, boost::endian::order::little>(
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 <size_t N>
auto de(qtils::BytesN<N> x) {
return boost::endian::
endian_load<uint64_t, N, boost::endian::order::little>(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);
}
Expand Down
Loading