Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bandersnatch VRF support #1928

Merged
merged 14 commits into from
Jul 17, 2024
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ if (NOT ($ENV{CI}) OR NOT ($ENV{GITHUB_ACTIONS}))
endif ()
option(EXTERNAL_PROJECT "Build external project" ${_EXTERNAL_PROJECT_DEFAULT})

set(WASM_COMPILER WasmEdge CACHE STRING "WebAssembly compiler built into Kagome: one of [WAVM, WasmEdge]")
set(WASM_COMPILER WAVM CACHE STRING "WebAssembly compiler built into Kagome: one of [WAVM, WasmEdge]")
xDimon marked this conversation as resolved.
Show resolved Hide resolved

if (NOT ${WASM_COMPILER} MATCHES "^(WAVM|WasmEdge)$")
fatal_error("WASM_COMPILER is set to ${WASM_COMPILER} but should be one of [WAVM, WasmEdge]")
Expand Down
9 changes: 9 additions & 0 deletions cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ hunter_config(
CMAKE_ARGS WITH_GFLAGS=OFF
)

hunter_config(
bandersnatch_vrfs_crust
URL https://github.com/qdrvm/bandersnatch-vrfs-crust/archive/refs/heads/draft2.tar.gz
SHA1 8d4512287ff7744b87f222faae768dbaa7f0c77a
CMAKE_ARGS CACHE_BREAK=5
CONFIGURATION_TYPES Debug
xDimon marked this conversation as resolved.
Show resolved Hide resolved
KEEP_PACKAGE_SOURCES
)

if ("${WASM_COMPILER}" STREQUAL "WasmEdge")
hunter_config(
WasmEdge
Expand Down
3 changes: 3 additions & 0 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ find_package(erasure_coding_crust CONFIG REQUIRED)
hunter_add_package(schnorrkel_crust)
find_package(schnorrkel_crust CONFIG REQUIRED)

hunter_add_package(bandersnatch_vrfs_crust)
find_package(bandersnatch_vrfs_crust CONFIG REQUIRED)

hunter_add_package(jsonrpc-lean)
find_package(jsonrpc-lean REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions core/application/chain_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ namespace kagome::application {
virtual const std::vector<libp2p::multi::Multiaddress> &bootNodes()
const = 0;

virtual const std::vector<std::pair<std::string, size_t>> &
telemetryEndpoints() const = 0;
virtual const std::vector<std::pair<std::string, size_t>>
&telemetryEndpoints() const = 0;

virtual const std::string &protocolId() const = 0;

Expand Down
20 changes: 10 additions & 10 deletions core/consensus/grandpa/impl/environment_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,21 +464,21 @@ namespace kagome::consensus::grandpa {
auto key_owner_proof_res = grandpa_api_->generate_key_ownership_proof(
last_finalized.hash, authority_set_id, equivocation.offender());
if (key_owner_proof_res.has_error()) {
SL_WARN(
logger_,
"Round #{}: can't generate key ownership proof for equivocation report: {}",
equivocation.round(),
key_owner_proof_res.error());
SL_WARN(logger_,
"Round #{}: can't generate key ownership proof for equivocation "
"report: {}",
equivocation.round(),
key_owner_proof_res.error());
return key_owner_proof_res.as_failure();
}
const auto &key_owner_proof_opt = key_owner_proof_res.value();

if (not key_owner_proof_opt.has_value()) {
SL_DEBUG(
logger_,
"Round #{}: can't generate key ownership proof for equivocation report: "
"Equivocation offender is not part of the authority set.",
equivocation.round());
SL_DEBUG(logger_,
"Round #{}: can't generate key ownership proof for equivocation "
"report: "
"Equivocation offender is not part of the authority set.",
equivocation.round());
return outcome::success(); // ensure if an error type is right
}
const auto &key_owner_proof = key_owner_proof_opt.value();
Expand Down
62 changes: 45 additions & 17 deletions core/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,80 +13,108 @@ kagome_install(kagome_crypto)

add_library(hasher
hasher/hasher_impl.cpp
)
)
target_link_libraries(hasher
blake2
twox
sha
keccak
)
)
kagome_install(hasher)

add_library(sr25519_types
sr25519_types.cpp
)
)
target_link_libraries(sr25519_types
mp_utils
blob
schnorrkel_crust::schnorrkel_crust
kagome_crypto
)
)
kagome_install(sr25519_types)

add_library(bandersnatch_types
bandersnatch_types.hpp
)
target_link_libraries(bandersnatch_types
mp_utils
blob
schnorrkel_crust::schnorrkel_crust
)
kagome_install(bandersnatch_types)

add_library(ed25519_types
ed25519_types.cpp
ed25519_types.hpp
)
)
target_link_libraries(ed25519_types
blob
scale::scale
schnorrkel_crust::schnorrkel_crust
kagome_crypto
)
)
kagome_install(ed25519_types)

add_library(vrf_provider
vrf/vrf_provider_impl.cpp
)
)
target_link_libraries(vrf_provider
mp_utils
sr25519_types
blob
p2p::p2p_random_generator # generator from libp2p
kagome_crypto
)
)
kagome_install(vrf_provider)

add_library(sr25519_provider
sr25519/sr25519_provider_impl.cpp
)
)
target_link_libraries(sr25519_provider
p2p::p2p_random_generator # generator from libp2p
sr25519_types
)
)
kagome_install(sr25519_provider)

add_library(bandersnatch_provider
bandersnatch/bandersnatch_provider_impl.cpp
)
target_link_libraries(bandersnatch_provider
bandersnatch_types
bandersnatch_vrfs_crust::bandersnatch_vrfs_crust
)
kagome_install(bandersnatch_provider)

add_library(bandersnatch_vrf
bandersnatch/vrf.cpp
)
target_link_libraries(bandersnatch_vrf
bandersnatch_types
bandersnatch_vrfs_crust::bandersnatch_vrfs_crust
)
kagome_install(bandersnatch_vrf)

add_library(ecdsa_provider
ecdsa/ecdsa_provider_impl.cpp
ecdsa_types.cpp
)
)
target_link_libraries(ecdsa_provider
hasher
logger
secp256k1_provider
kagome_crypto
)
)
kagome_install(ecdsa_provider)

add_library(ed25519_provider
ed25519/ed25519_provider_impl.cpp
)
)
target_link_libraries(ed25519_provider
ed25519_types
hasher
logger
schnorrkel_crust::schnorrkel_crust
)
)
kagome_install(ed25519_provider)

add_library(secp256k1_provider
Expand All @@ -99,20 +127,20 @@ target_link_libraries(secp256k1_provider
blob
libsecp256k1::secp256k1
scale::scale
)
)
kagome_install(
secp256k1_provider
)

add_library(pbkdf2_provider
pbkdf2/impl/pbkdf2_provider_impl.cpp
)
)
target_link_libraries(pbkdf2_provider
PUBLIC
OpenSSL::SSL
OpenSSL::Crypto
blob
)
)
kagome_install(pbkdf2_provider)

add_subdirectory(bip39)
Expand Down
56 changes: 56 additions & 0 deletions core/crypto/bandersnatch/bandersnatch_provider_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#include "crypto/bandersnatch/bandersnatch_provider_impl.hpp"

namespace kagome::crypto {

outcome::result<BandersnatchKeypair>
BandersnatchProviderImpl::generateKeypair(
const BandersnatchSeed &seed,
BandersnatchProvider::Junctions junctions) const {
xDimon marked this conversation as resolved.
Show resolved Hide resolved
std::array<uint8_t, constants::bandersnatch::KEYPAIR_SIZE> kp{};
bandersnatch_keypair_from_seed(seed.unsafeBytes().data(), kp.data());

// for (auto &junction : junctions) {
xDimon marked this conversation as resolved.
Show resolved Hide resolved
// decltype(kp) next;
// (junction.hard ? bandersnatch_derive_keypair_hard
// : bandersnatch_derive_keypair_soft)(
// next.data(), kp.data(), junction.cc.data());
// kp = next;
// }

BandersnatchKeypair keypair{
BandersnatchSecretKey::from(SecureCleanGuard{
std::span(kp).subspan<0, constants::bandersnatch::SECRET_SIZE>()}),
BandersnatchPublicKey::fromSpan(
std::span(kp).subspan(constants::bandersnatch::SECRET_SIZE,
constants::bandersnatch::PUBLIC_SIZE))
.value()};
return keypair;
}

outcome::result<BandersnatchSignature> BandersnatchProviderImpl::sign(
const BandersnatchKeypair &keypair, common::BufferView message) const {
BandersnatchSignature signature;

::bandersnatch_sign(keypair.secret_key.unsafeBytes().data(),
message.data(),
message.size(),
signature.data());

return signature;
}

outcome::result<bool> BandersnatchProviderImpl::verify(
const BandersnatchSignature &signature,
common::BufferView message,
const BandersnatchPublicKey &public_key) const {
return ::bandersnatch_verify(
signature.data(), message.data(), message.size(), public_key.data());
}

} // namespace kagome::crypto
29 changes: 29 additions & 0 deletions core/crypto/bandersnatch/bandersnatch_provider_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "crypto/bandersnatch_provider.hpp"
#include "crypto/bandersnatch_types.hpp"

namespace kagome::crypto {

class BandersnatchProviderImpl final : public BandersnatchProvider {
public:
outcome::result<BandersnatchKeypair> generateKeypair(
const BandersnatchSeed &seed, Junctions junctions) const override;

outcome::result<BandersnatchSignature> sign(
const BandersnatchKeypair &keypair,
common::BufferView message) const override;

outcome::result<bool> verify(
const BandersnatchSignature &signature,
common::BufferView message,
const BandersnatchPublicKey &public_key) const override;
};

} // namespace kagome::crypto
Loading