Skip to content

Commit 80faa68

Browse files
authored
Test-Vector 'Disputes' (#12)
1 parent 945138b commit 80faa68

21 files changed

+797
-73
lines changed

.clang-format

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
BasedOnStyle: Google
88

9-
AlignAfterOpenBracket: DontAlign
9+
AlignAfterOpenBracket: Align
1010
AlignOperands: AlignAfterOperator
1111
AllowShortFunctionsOnASingleLine: Empty
1212
BinPackArguments: false

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ find_package(Boost CONFIG REQUIRED)
2828
find_package(fmt CONFIG REQUIRED)
2929
find_package(jam_crust CONFIG REQUIRED)
3030
find_package(scale CONFIG REQUIRED)
31+
find_package(schnorrkel_crust CONFIG REQUIRED)
3132

3233
add_library(headers INTERFACE)
3334
target_include_directories(headers INTERFACE

python/asn1.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ def asn_sequence_of(t):
170170
def asn_member(t):
171171
if t["type"] == "OCTET STRING":
172172
t = dict(type="SEQUENCE OF", element=dict(type="U8"), size=t["size"])
173+
if t["type"] == "BOOLEAN":
174+
return "bool"
173175
if t["type"] == "SEQUENCE OF":
174176
r = asn_sequence_of(t)
175177
elif t["type"] in asn_types:
@@ -190,16 +192,22 @@ def asn_member(t):
190192
enum_trait = []
191193
for tname, t in asn_types.items():
192194
ty = types[tname]
195+
if tname == "BOOLEAN":
196+
ty.decl = c_using(tname, "bool")
197+
continue
193198
if tname == "U8":
194199
ty.decl = c_using(tname, "uint8_t")
195200
continue
201+
if tname == "U16":
202+
ty.decl = c_using(tname, "uint16_t")
203+
continue
196204
if tname == "U32":
197205
ty.decl = c_using(tname, "uint32_t")
198206
continue
199207
if t["type"] == "NULL":
200208
t = dict(type="SEQUENCE", members=[])
201209
if t["type"] == "CHOICE":
202-
if tname == "MmrPeak":
210+
if tname == "MmrPeak" or tname == "AvailabilityAssignmentItem": # TODO(#14): Need to make in universal
203211
assert [x["name"] for x in t["members"]] == ["none", "some"]
204212
ty.decl = c_using(
205213
tname, "std::optional<%s>" % asn_member(t["members"][1])
@@ -348,6 +356,19 @@ def safrole():
348356
)
349357
g.write("safrole")
350358

359+
def disputes():
360+
g = Gen(
361+
"jam::test_vectors_disputes",
362+
["validators-count", "core-count", "epoch-length", "validators-super-majority"],
363+
asn_file("disputes/disputes"),
364+
"DisputesModule",
365+
[
366+
(name, parse_const(asn_file("disputes/%s" % name), "Constants"))
367+
for name in ["tiny", "full"]
368+
],
369+
)
370+
g.write("disputes")
371+
351372

352373
def history():
353374
g = Gen(
@@ -362,4 +383,4 @@ def history():
362383

363384
if __name__ == "__main__":
364385
for arg in sys.argv[1:]:
365-
dict(safrole=safrole, history=history)[arg]()
386+
dict(safrole=safrole, history=history, disputes=disputes)[arg]()

src/TODO_qtils/bytes_std_hash.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright Quadrivium LLC
3+
* All Rights Reserved
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <qtils/bytes.hpp>
10+
#include <qtils/bytestr.hpp>
11+
12+
namespace qtils {
13+
struct BytesStdHash {
14+
std::size_t operator()(qtils::BytesIn bytes) const {
15+
return std::hash<std::string_view>{}(qtils::byte2str(bytes));
16+
}
17+
};
18+
} // namespace qtils
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright Quadrivium LLC
3+
* All Rights Reserved
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
#include <algorithm>
9+
10+
namespace qtils::cxx23::ranges {
11+
auto contains(auto &&r, const auto &v) {
12+
auto end = std::ranges::end(r);
13+
return std::find(std::ranges::begin(r), end, v) != end;
14+
}
15+
16+
auto contains_if(auto &&r, const auto &f) {
17+
auto end = std::ranges::end(r);
18+
return std::find_if(std::ranges::begin(r), end, f) != end;
19+
}
20+
} // namespace qtils::cxx23::ranges

src/jam/ed25519.hpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright Quadrivium LLC
3+
* All Rights Reserved
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <schnorrkel_crust.h>
8+
#include <qtils/bytes.hpp>
9+
10+
namespace jam::ed25519 {
11+
using Secret = qtils::BytesN<ED25519_SECRET_KEY_LENGTH>;
12+
using Public = qtils::BytesN<ED25519_PUBLIC_KEY_LENGTH>;
13+
using KeyPair = qtils::BytesN<ED25519_KEYPAIR_LENGTH>;
14+
using Signature = qtils::BytesN<ED25519_SIGNATURE_LENGTH>;
15+
using Message = qtils::BytesIn;
16+
17+
inline std::optional<Signature> sign(const KeyPair &keypair,
18+
Message message) {
19+
Signature sig;
20+
auto res = ed25519_sign(
21+
sig.data(), keypair.data(), message.data(), message.size_bytes());
22+
if (res != ED25519_RESULT_OK) {
23+
return std::nullopt;
24+
}
25+
return sig;
26+
}
27+
28+
inline bool verify(const Signature &signature,
29+
Message message,
30+
const Public &public_key) {
31+
auto res = ed25519_verify(signature.data(),
32+
public_key.data(),
33+
message.data(),
34+
message.size_bytes());
35+
return res == ED25519_RESULT_OK;
36+
}
37+
} // namespace jam::ed25519

test-vectors/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ include(asn1.cmake)
1313

1414
add_subdirectory(history)
1515
add_subdirectory(safrole)
16+
add_subdirectory(disputes)

test-vectors/common.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#pragma once
88

9+
#include <cstring>
910
#include <stdexcept>
1011

1112
#include <boost/endian/conversion.hpp>
@@ -37,8 +38,8 @@ namespace jam {
3738
// [GP 0.4.5 3.7.2]
3839
// https://github.com/gavofyork/graypaper/blob/v0.4.5/text/notation.tex#L132
3940
template <size_t X, size_t Y>
40-
qtils::BytesN<X + Y> frown(
41-
const qtils::BytesN<X> &x, const qtils::BytesN<Y> &y) {
41+
qtils::BytesN<X + Y> frown(const qtils::BytesN<X> &x,
42+
const qtils::BytesN<Y> &y) {
4243
qtils::BytesN<X + Y> xy;
4344
memcpy(xy.data(), x.data(), X);
4445
memcpy(xy.data() + X, y.data(), Y);

test-vectors/disputes/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/types.hpp
2+
/types.scale.hpp
3+
/types.diff.hpp

test-vectors/disputes/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright Quadrivium LLC
3+
# All Rights Reserved
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
asn1(disputes disputes tiny full)
8+
target_link_libraries(test_vectors_disputes_test
9+
jam_crust::jam_crust
10+
PkgConfig::libb2
11+
schnorrkel_crust::schnorrkel_crust
12+
)

0 commit comments

Comments
 (0)