From 5bca65d77e9ffadd7c0c7371c055c9c2498f8a78 Mon Sep 17 00:00:00 2001 From: Dmitriy Khaustov aka xDimon Date: Sun, 14 Jan 2024 01:08:40 +0300 Subject: [PATCH 1/2] feature: elliptic curves functions Signed-off-by: Dmitriy Khaustov aka xDimon --- cmake/Hunter/config.cmake | 7 + cmake/dependencies.cmake | 3 + core/crypto/CMakeLists.txt | 45 +++-- core/crypto/elliptic_curves.hpp | 87 +++++++++ .../elliptic_curves/elliptic_curves_impl.cpp | 83 +++++++++ .../elliptic_curves/elliptic_curves_impl.hpp | 42 +++++ core/host_api/CMakeLists.txt | 1 + core/host_api/host_api.hpp | 95 +++++++++- core/host_api/impl/CMakeLists.txt | 9 + .../impl/elliptic_curves_extension.cpp | 166 ++++++++++++++++++ .../impl/elliptic_curves_extension.hpp | 74 ++++++++ core/host_api/impl/host_api_factory_impl.cpp | 4 + core/host_api/impl/host_api_factory_impl.hpp | 6 + core/host_api/impl/host_api_impl.cpp | 54 ++++++ core/host_api/impl/host_api_impl.hpp | 42 ++++- core/injector/CMakeLists.txt | 1 + core/injector/application_injector.cpp | 2 + .../binaryen/runtime_external_interface.cpp | 14 ++ .../wavm/intrinsics/intrinsic_functions.cpp | 72 ++++++++ test/core/runtime/runtime_test_base.hpp | 3 + .../runtime/wavm/wavm_module_init_test.cpp | 31 ++-- test/external-project-test/src/main.cpp | 3 + test/mock/core/host_api/host_api_mock.hpp | 38 ++++ 23 files changed, 836 insertions(+), 46 deletions(-) create mode 100644 core/crypto/elliptic_curves.hpp create mode 100644 core/crypto/elliptic_curves/elliptic_curves_impl.cpp create mode 100644 core/crypto/elliptic_curves/elliptic_curves_impl.hpp create mode 100644 core/host_api/impl/elliptic_curves_extension.cpp create mode 100644 core/host_api/impl/elliptic_curves_extension.hpp diff --git a/cmake/Hunter/config.cmake b/cmake/Hunter/config.cmake index 2717bf24a7..05d164e96c 100644 --- a/cmake/Hunter/config.cmake +++ b/cmake/Hunter/config.cmake @@ -97,6 +97,13 @@ hunter_config( KEEP_PACKAGE_SOURCES ) +hunter_config( + arkworks_crust + URL https://github.com/qdrvm/arkworks-crust/archive/843300a0ef85777761a4c1acc0acf158e986c6ca.tar.gz + SHA1 ec88913a9d2de264c5ce9f5bbe13d9ec7d135ce3 + KEEP_PACKAGE_SOURCES +) + # Fix for Apple clang (or clang from brew) of versions 15 and higher if (APPLE AND (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND CMAKE_CXX_COMPILER_VERSION GREATER_EQUAL "15.0.0") hunter_config( diff --git a/cmake/dependencies.cmake b/cmake/dependencies.cmake index e8b47e925a..d4a1234240 100644 --- a/cmake/dependencies.cmake +++ b/cmake/dependencies.cmake @@ -50,6 +50,9 @@ find_package(erasure_coding_crust CONFIG REQUIRED) hunter_add_package(schnorrkel_crust) find_package(schnorrkel_crust CONFIG REQUIRED) +hunter_add_package(arkworks_crust) +find_package(arkworks_crust CONFIG REQUIRED) + hunter_add_package(jsonrpc-lean) find_package(jsonrpc-lean REQUIRED) diff --git a/core/crypto/CMakeLists.txt b/core/crypto/CMakeLists.txt index c078630aa4..ffc1b7a5d9 100644 --- a/core/crypto/CMakeLists.txt +++ b/core/crypto/CMakeLists.txt @@ -6,102 +6,111 @@ 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_install(sr25519_types) add_library(ed25519_types ed25519_types.cpp ed25519_types.hpp - ) +) target_link_libraries(ed25519_types blob scale::scale schnorrkel_crust::schnorrkel_crust - ) +) 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_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(ecdsa_provider ecdsa/ecdsa_provider_impl.cpp ecdsa_types.cpp - ) +) target_link_libraries(ecdsa_provider hasher logger secp256k1_provider - ) +) 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 secp256k1/secp256k1_provider_impl.cpp - ) +) target_link_libraries(secp256k1_provider PUBLIC OpenSSL::Crypto PUBLIC OpenSSL::SSL blob secp256k1::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_library(elliptic_curves + elliptic_curves/elliptic_curves_impl.cpp +) +target_link_libraries(elliptic_curves + logger + arkworks_crust::arkworks_crust +) +kagome_install(elliptic_curves) + add_subdirectory(bip39) add_subdirectory(blake2) add_subdirectory(crypto_store) diff --git a/core/crypto/elliptic_curves.hpp b/core/crypto/elliptic_curves.hpp new file mode 100644 index 0000000000..3ce0546c49 --- /dev/null +++ b/core/crypto/elliptic_curves.hpp @@ -0,0 +1,87 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "common/blob.hpp" +#include "common/buffer_view.hpp" + +namespace kagome::crypto { + + class EllipticCurves { + public: + virtual ~EllipticCurves() = default; + + /** + * Pairing multi Miller loop for BLS12-381. + * @param a + * ArkScale>> + * @param b + * ArkScale>> + * @return ArkScale>> + */ + virtual outcome::result bls12_381_multi_miller_loop( + common::BufferView a, common::BufferView b) const = 0; + + /** + * Pairing final exponentiation for BLS12-381. + * @param f ArkScale>> + * @return ArkScale>> + */ + virtual outcome::result bls12_381_final_exponentiation( + common::BufferView f) const = 0; + + /** + * Projective multiplication on G1 for BLS12-381. + * @param base ArkScaleProjective + * @param scalar ArkScale<&[u64]> + * @return ArkScaleProjective + */ + virtual outcome::result bls12_381_mul_projective_g1( + common::BufferView base, common::BufferView scalar) const = 0; + + /** + * Projective multiplication on G2 for BLS12-381. + * @param base ArkScaleProjective + * @param scalar ArkScale<&[u64]> + * @return ArkScaleProjective + */ + virtual outcome::result bls12_381_mul_projective_g2( + common::BufferView base, common::BufferView scalar) const = 0; + + /** + * Multi scalar multiplication on G1 for BLS12-381. + * @param bases ArkScale<&[ark_bls12_381::G1Affine]> + * @param scalars ArkScale<&[ark_bls12_381::Fr]> + * @return ArkScaleProjective + */ + virtual outcome::result bls12_381_msm_g1( + common::BufferView bases, common::BufferView scalars) const = 0; + + /** + * Multi scalar multiplication on G2 for BLS12-381. + * @param bases ArkScale<&[ark_bls12_381::G2Affine]> + * @param scalars ArkScale<&[ark_bls12_381::Fr]> + * @return ArkScaleProjective + */ + virtual outcome::result bls12_381_msm_g2( + common::BufferView bases, common::BufferView scalars) const = 0; + + /** + * Short Weierstrass projective multiplication for + * Ed-on-BLS12-381-Bandersnatch. + * @param base + * ArkScaleProjective + * @param scalar ArkScale<&[u64]> + * @return + * ArkScaleProjective + */ + virtual outcome::result + ed_on_bls12_381_bandersnatch_sw_mul_projective( + common::BufferView base, common::BufferView scalar) const = 0; + }; + +} // namespace kagome::crypto diff --git a/core/crypto/elliptic_curves/elliptic_curves_impl.cpp b/core/crypto/elliptic_curves/elliptic_curves_impl.cpp new file mode 100644 index 0000000000..54c64d3065 --- /dev/null +++ b/core/crypto/elliptic_curves/elliptic_curves_impl.cpp @@ -0,0 +1,83 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "crypto/elliptic_curves/elliptic_curves_impl.hpp" + +#include "common/buffer.hpp" +#include "common/buffer_view.hpp" + +#include + +OUTCOME_CPP_DEFINE_CATEGORY(kagome::crypto, EllipticCurvesError, e) { + using E = decltype(e); + switch (e) { + case E::ARKWORKS_RETURN_ERROR: + return "Arkworks function call returned error"; + } + return "unknown error (kagome::crypto::EllipticCurvesError)"; +} + +namespace kagome::crypto { + + namespace { + ::BytesVec convert(common::BufferView view) { + return {.data = const_cast(view.data()), .size = view.size()}; + } + outcome::result convert(::Result res) { + if (res.tag == ::RESULT_OK) { + // TODO avoid coping to runtime + common::Buffer buf(res.ok.data, res.ok.data + res.ok.size); + ::AWCR_deallocate_bytesvec(&res.ok); + return buf; + } + return EllipticCurvesError::ARKWORKS_RETURN_ERROR; + } + } // namespace + + outcome::result + EllipticCurvesImpl::bls12_381_multi_miller_loop(common::BufferView a, + common::BufferView b) const { + return convert(::bls12_381_multi_miller_loop(convert(a), convert(b))); + } + + outcome::result + EllipticCurvesImpl::bls12_381_final_exponentiation( + common::BufferView f) const { + return convert(::bls12_381_final_exponentiation(convert(f))); + } + + outcome::result + EllipticCurvesImpl::bls12_381_mul_projective_g1( + common::BufferView base, common::BufferView scalar) const { + return convert( + ::bls12_381_mul_projective_g1(convert(base), convert(scalar))); + } + + outcome::result + EllipticCurvesImpl::bls12_381_mul_projective_g2( + common::BufferView base, common::BufferView scalar) const { + return convert( + ::bls12_381_mul_projective_g2(convert(base), convert(scalar))); + } + + outcome::result EllipticCurvesImpl::bls12_381_msm_g1( + common::BufferView bases, common::BufferView scalars) const { + return convert(::bls12_381_msm_g1(convert(bases), convert(scalars))); + } + + outcome::result EllipticCurvesImpl::bls12_381_msm_g2( + common::BufferView bases, common::BufferView scalars) const { + return convert(::bls12_381_msm_g2(convert(bases), convert(scalars))); + } + + outcome::result + EllipticCurvesImpl::ed_on_bls12_381_bandersnatch_sw_mul_projective( + common::BufferView base, common::BufferView scalar) const { + return convert(::ed_on_bls12_381_bandersnatch_sw_mul_projective( + convert(base), convert(scalar))); + } + +} // namespace kagome::crypto diff --git a/core/crypto/elliptic_curves/elliptic_curves_impl.hpp b/core/crypto/elliptic_curves/elliptic_curves_impl.hpp new file mode 100644 index 0000000000..f190e96f46 --- /dev/null +++ b/core/crypto/elliptic_curves/elliptic_curves_impl.hpp @@ -0,0 +1,42 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "crypto/elliptic_curves.hpp" + +namespace kagome::crypto { + + class EllipticCurvesImpl : public EllipticCurves { + public: + outcome::result bls12_381_multi_miller_loop( + common::BufferView a, common::BufferView b) const override; + + outcome::result bls12_381_final_exponentiation( + common::BufferView f) const override; + + outcome::result bls12_381_mul_projective_g1( + common::BufferView base, common::BufferView scalar) const override; + + outcome::result bls12_381_mul_projective_g2( + common::BufferView base, common::BufferView scalar) const override; + + outcome::result bls12_381_msm_g1( + common::BufferView bases, common::BufferView scalars) const override; + + outcome::result bls12_381_msm_g2( + common::BufferView bases, common::BufferView scalars) const override; + + outcome::result + ed_on_bls12_381_bandersnatch_sw_mul_projective( + common::BufferView base, common::BufferView scalar) const override; + }; + + enum class EllipticCurvesError { ARKWORKS_RETURN_ERROR = 1 }; + +} // namespace kagome::crypto + +OUTCOME_HPP_DECLARE_ERROR(kagome::crypto, EllipticCurvesError); diff --git a/core/host_api/CMakeLists.txt b/core/host_api/CMakeLists.txt index 657d350789..4a0f4bcc7d 100644 --- a/core/host_api/CMakeLists.txt +++ b/core/host_api/CMakeLists.txt @@ -17,5 +17,6 @@ target_link_libraries(host_api storage_extension child_storage_extension offchain_extension + elliptic_curves_extension ) kagome_install(host_api) diff --git a/core/host_api/host_api.hpp b/core/host_api/host_api.hpp index c266143074..1bd012dc42 100644 --- a/core/host_api/host_api.hpp +++ b/core/host_api/host_api.hpp @@ -86,7 +86,7 @@ namespace kagome::host_api { * @param prefix memory span containing prefix * @param limit of entries to be removed */ - virtual runtime::WasmSpan ext_storage_clear_prefix_version_2( + [[nodiscard]] virtual runtime::WasmSpan ext_storage_clear_prefix_version_2( runtime::WasmSpan prefix, runtime::WasmSpan limit) = 0; /** @@ -211,7 +211,8 @@ namespace kagome::host_api { /** * Get host max log level */ - virtual runtime::WasmEnum ext_logging_max_level_version_1() = 0; + [[nodiscard]] virtual runtime::WasmEnum + ext_logging_max_level_version_1() = 0; // ------------------------ Cryptographic extensions ----------------------- @@ -579,7 +580,8 @@ namespace kagome::host_api { runtime::WasmSpan deadline) = 0; /// @copydoc OffchainExtension::ext_offchain_http_response_headers_version_1 - virtual runtime::WasmSpan ext_offchain_http_response_headers_version_1( + [[nodiscard]] virtual runtime::WasmSpan + ext_offchain_http_response_headers_version_1( runtime::WasmI32 request_id) = 0; /// @copydoc @@ -687,7 +689,8 @@ namespace kagome::host_api { * @param prefix a pointer-size indicating the prefix * @return pointer to number of records removed */ - virtual runtime::WasmSpan ext_default_child_storage_clear_prefix_version_2( + [[nodiscard]] virtual runtime::WasmSpan + ext_default_child_storage_clear_prefix_version_2( runtime::WasmSpan child_storage_key, runtime::WasmSpan prefix, runtime::WasmSpan limit) = 0; @@ -709,7 +712,8 @@ namespace kagome::host_api { * number of bytes written into the value_out buffer. Returns None if the * entry does not exists. */ - virtual runtime::WasmSpan ext_default_child_storage_read_version_1( + [[nodiscard]] virtual runtime::WasmSpan + ext_default_child_storage_read_version_1( runtime::WasmSpan child_storage_key, runtime::WasmSpan key, runtime::WasmSpan value_out, @@ -722,7 +726,7 @@ namespace kagome::host_api { * @return a boolean equal to true if the key does exist, false if * otherwise. */ - virtual int32_t ext_default_child_storage_exists_version_1( + [[nodiscard]] virtual int32_t ext_default_child_storage_exists_version_1( runtime::WasmSpan child_storage_key, runtime::WasmSpan key) const = 0; /** @@ -738,10 +742,87 @@ namespace kagome::host_api { * @param limit is an optional number of records allowed to remove * @return pointer to int32 with a number of records removed */ - virtual runtime::WasmSpan ext_default_child_storage_storage_kill_version_3( + [[nodiscard]] virtual runtime::WasmSpan + ext_default_child_storage_storage_kill_version_3( runtime::WasmSpan child_storage_key, runtime::WasmSpan limit) = 0; virtual void ext_panic_handler_abort_on_panic_version_1( runtime::WasmSpan message) = 0; + + // ---------------------------- Elliptic Curves ---------------------------- + + /** + * Pairing multi Miller loop for BLS12-381. + * @param a + * ArkScale>> + * @param b + * ArkScale>> + * @return ArkScale>> + */ + [[nodiscard]] virtual runtime::WasmSpan + ext_elliptic_curves_bls12_381_multi_miller_loop_version_1( + runtime::WasmSpan a, runtime::WasmSpan b) const = 0; + + /** + * Pairing final exponentiation for BLS12-381. + * @param f ArkScale>> + * @return ArkScale>> + */ + [[nodiscard]] virtual runtime::WasmSpan + ext_elliptic_curves_bls12_381_final_exponentiation_version_1( + runtime::WasmSpan f) const = 0; + + /** + * Projective multiplication on G1 for BLS12-381. + * @param base ArkScaleProjective + * @param scalar ArkScale<&[u64]> + * @return ArkScaleProjective + */ + [[nodiscard]] virtual runtime::WasmSpan + ext_elliptic_curves_bls12_381_mul_projective_g1_version_1( + runtime::WasmSpan base, runtime::WasmSpan scalar) const = 0; + + /** + * Projective multiplication on G2 for BLS12-381. + * @param base ArkScaleProjective + * @param scalar ArkScale<&[u64]> + * @return ArkScaleProjective + */ + [[nodiscard]] virtual runtime::WasmSpan + ext_elliptic_curves_bls12_381_mul_projective_g2_version_1( + runtime::WasmSpan base, runtime::WasmSpan scalar) const = 0; + + /** + * Multi scalar multiplication on G1 for BLS12-381. + * @param bases ArkScale<&[ark_bls12_381::G1Affine]> + * @param scalars ArkScale<&[ark_bls12_381::Fr]> + * @return ArkScaleProjective + */ + [[nodiscard]] virtual runtime::WasmSpan + ext_elliptic_curves_bls12_381_msm_g1_version_1( + runtime::WasmSpan bases, runtime::WasmSpan scalars) const = 0; + + /** + * Multi scalar multiplication on G2 for BLS12-381. + * @param bases ArkScale<&[ark_bls12_381::G2Affine]> + * @param scalars ArkScale<&[ark_bls12_381::Fr]> + * @return ArkScaleProjective + */ + [[nodiscard]] virtual runtime::WasmSpan + ext_elliptic_curves_bls12_381_msm_g2_version_1( + runtime::WasmSpan bases, runtime::WasmSpan scalars) const = 0; + + /** + * Short Weierstrass projective multiplication for + * Ed-on-BLS12-381-Bandersnatch. + * @param base + * ArkScaleProjective + * @param scalar ArkScale<&[u64]> + * @return + * ArkScaleProjective + */ + [[nodiscard]] virtual runtime::WasmSpan + ext_elliptic_curves_ed_on_bls12_381_bandersnatch_sw_mul_projective_version_1( + runtime::WasmSpan base, runtime::WasmSpan scalar) const = 0; }; } // namespace kagome::host_api diff --git a/core/host_api/impl/CMakeLists.txt b/core/host_api/impl/CMakeLists.txt index faf4712887..bc9c62886d 100644 --- a/core/host_api/impl/CMakeLists.txt +++ b/core/host_api/impl/CMakeLists.txt @@ -28,6 +28,15 @@ target_link_libraries(crypto_extension ) kagome_install(crypto_extension) +add_library(elliptic_curves_extension + elliptic_curves_extension.cpp + ) +target_link_libraries(elliptic_curves_extension + logger + elliptic_curves + ) +kagome_install(elliptic_curves_extension) + add_library(io_extension io_extension.cpp ) diff --git a/core/host_api/impl/elliptic_curves_extension.cpp b/core/host_api/impl/elliptic_curves_extension.cpp new file mode 100644 index 0000000000..762689b332 --- /dev/null +++ b/core/host_api/impl/elliptic_curves_extension.cpp @@ -0,0 +1,166 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "host_api/impl/elliptic_curves_extension.hpp" + +#include "crypto/elliptic_curves.hpp" +#include "log/trace_macros.hpp" +#include "runtime/ptr_size.hpp" + +namespace { + template + void throw_with_error(const kagome::log::Logger &logger, + const Format &format, + Args &&...args) { + auto msg = fmt::vformat(format, + fmt::make_format_args(std::forward(args)...)); + logger->error(msg); + throw std::runtime_error(msg); + } +} // namespace + +namespace kagome::host_api { + + EllipticCurvesExtension::EllipticCurvesExtension( + std::shared_ptr memory_provider, + std::shared_ptr elliptic_curves) + : logger_(log::createLogger("EllipticCurvesExtension", + "ecliptic_curves_extension")), + memory_provider_(std::move(memory_provider)), + elliptic_curves_(std::move(elliptic_curves)) { + BOOST_ASSERT(memory_provider_ != nullptr); + BOOST_ASSERT(elliptic_curves_ != nullptr); + } + + runtime::WasmSpan EllipticCurvesExtension:: + ext_elliptic_curves_bls12_381_multi_miller_loop_version_1( + runtime::WasmSpan a, runtime::WasmSpan b) const { + auto [a_addr, a_len] = runtime::PtrSize(a); + const auto &a_buf = getMemory().loadN(a_addr, a_len); + auto [b_addr, b_len] = runtime::PtrSize(b); + const auto &b_buf = getMemory().loadN(b_addr, b_len); + + auto res = elliptic_curves_->bls12_381_multi_miller_loop(a_buf, b_buf); + if (not res) { + throw_with_error( + logger_, "error 'bls12_381_multi_miller_loop' call: {}", res.error()); + } + SL_TRACE_FUNC_CALL(logger_, res.value(), a_buf, b_buf); + + return getMemory().storeBuffer(res.value()); + } + + runtime::WasmSpan EllipticCurvesExtension:: + ext_elliptic_curves_bls12_381_final_exponentiation_version_1( + runtime::WasmSpan f) const { + auto [addr, len] = runtime::PtrSize(f); + const auto &buf = getMemory().loadN(addr, len); + + auto res = elliptic_curves_->bls12_381_final_exponentiation(buf); + if (not res) { + throw_with_error(logger_, + "error 'bls12_381_final_exponentiation' call: {}", + res.error()); + } + SL_TRACE_FUNC_CALL(logger_, res.value(), buf, buf); + + return getMemory().storeBuffer(res.value()); + } + + runtime::WasmSpan EllipticCurvesExtension:: + ext_elliptic_curves_bls12_381_mul_projective_g1_version_1( + runtime::WasmSpan base_span, runtime::WasmSpan scalar_span) const { + auto [base_addr, base_len] = runtime::PtrSize(base_span); + const auto &base = getMemory().loadN(base_addr, base_len); + auto [scalar_addr, scalar_len] = runtime::PtrSize(scalar_span); + const auto &scalar = getMemory().loadN(scalar_addr, scalar_len); + + auto res = elliptic_curves_->bls12_381_mul_projective_g1(base, scalar); + if (not res) { + throw_with_error( + logger_, "error 'bls12_381_mul_projective_g1' call: {}", res.error()); + } + SL_TRACE_FUNC_CALL(logger_, res.value(), base, scalar); + + return getMemory().storeBuffer(res.value()); + } + + runtime::WasmSpan EllipticCurvesExtension:: + ext_elliptic_curves_bls12_381_mul_projective_g2_version_1( + runtime::WasmSpan base_span, runtime::WasmSpan scalar_span) const { + auto [base_addr, base_len] = runtime::PtrSize(base_span); + const auto &base = getMemory().loadN(base_addr, base_len); + auto [scalar_addr, scalar_len] = runtime::PtrSize(scalar_span); + const auto &scalar = getMemory().loadN(scalar_addr, scalar_len); + + auto res = elliptic_curves_->bls12_381_mul_projective_g2(base, scalar); + if (not res) { + throw_with_error( + logger_, "error 'bls12_381_mul_projective_g2' call: {}", res.error()); + } + SL_TRACE_FUNC_CALL(logger_, res.value(), base, scalar); + + return getMemory().storeBuffer(res.value()); + } + + runtime::WasmSpan + EllipticCurvesExtension::ext_elliptic_curves_bls12_381_msm_g1_version_1( + runtime::WasmSpan bases_span, runtime::WasmSpan scalars_span) const { + auto [bases_addr, bases_len] = runtime::PtrSize(bases_span); + const auto &bases = getMemory().loadN(bases_addr, bases_len); + auto [scalars_addr, scalars_len] = runtime::PtrSize(scalars_span); + const auto &scalars = getMemory().loadN(scalars_addr, scalars_len); + + auto res = elliptic_curves_->bls12_381_msm_g1(bases, scalars); + if (not res) { + throw_with_error( + logger_, "error 'bls12_381_msm_g1' call: {}", res.error()); + } + SL_TRACE_FUNC_CALL(logger_, res.value(), bases, scalars); + + return getMemory().storeBuffer(res.value()); + } + + runtime::WasmSpan + EllipticCurvesExtension::ext_elliptic_curves_bls12_381_msm_g2_version_1( + runtime::WasmSpan bases_span, runtime::WasmSpan scalars_span) const { + auto [bases_addr, bases_len] = runtime::PtrSize(bases_span); + const auto &bases = getMemory().loadN(bases_addr, bases_len); + auto [scalars_addr, scalars_len] = runtime::PtrSize(scalars_span); + const auto &scalars = getMemory().loadN(scalars_addr, scalars_len); + + auto res = elliptic_curves_->bls12_381_msm_g2(bases, scalars); + if (not res) { + throw_with_error( + logger_, "error 'bls12_381_msm_g2' call: {}", res.error()); + } + SL_TRACE_FUNC_CALL(logger_, res.value(), bases, scalars); + + return getMemory().storeBuffer(res.value()); + } + + runtime::WasmSpan EllipticCurvesExtension:: + ext_elliptic_curves_ed_on_bls12_381_bandersnatch_sw_mul_projective_version_1( + runtime::WasmSpan data1, runtime::WasmSpan data2) const { + auto [addr1, len1] = runtime::PtrSize(data1); + const auto &buf1 = getMemory().loadN(addr1, len1); + auto [addr2, len2] = runtime::PtrSize(data2); + const auto &buf2 = getMemory().loadN(addr2, len2); + + auto res = elliptic_curves_->ed_on_bls12_381_bandersnatch_sw_mul_projective( + buf1, buf2); + if (not res) { + throw_with_error( + logger_, + "error 'ed_on_bls12_381_bandersnatch_sw_mul_projective' call: {}", + res.error()); + } + SL_TRACE_FUNC_CALL(logger_, res.value(), buf1, buf2); + + return getMemory().storeBuffer(res.value()); + } + +} // namespace kagome::host_api diff --git a/core/host_api/impl/elliptic_curves_extension.hpp b/core/host_api/impl/elliptic_curves_extension.hpp new file mode 100644 index 0000000000..05b22ed5e6 --- /dev/null +++ b/core/host_api/impl/elliptic_curves_extension.hpp @@ -0,0 +1,74 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include + +#include "log/logger.hpp" +#include "runtime/memory_provider.hpp" +#include "runtime/types.hpp" + +namespace kagome::crypto { + class EllipticCurves; +} + +namespace kagome::host_api { + /** + * Implements extension functions related to ecliptic curves + */ + class EllipticCurvesExtension { + public: + EllipticCurvesExtension( + std::shared_ptr memory_provider, + std::shared_ptr elliptic_curves); + + // -------------------- bls12_381 methods -------------------- + + /// @see HostApi::ext_elliptic_curves_bls12_381_multi_miller_loop_version_1 + runtime::WasmSpan ext_elliptic_curves_bls12_381_multi_miller_loop_version_1( + runtime::WasmSpan a, runtime::WasmSpan b) const; + + /// @see + /// HostApi::ext_elliptic_curves_bls12_381_final_exponentiation_version_1 + runtime::WasmSpan + ext_elliptic_curves_bls12_381_final_exponentiation_version_1( + runtime::WasmSpan f) const; + + /// @see HostApi::ext_elliptic_curves_bls12_381_mul_projective_g1_version_1 + runtime::WasmSpan ext_elliptic_curves_bls12_381_mul_projective_g1_version_1( + runtime::WasmSpan base, runtime::WasmSpan scalar) const; + + /// @see HostApi::ext_elliptic_curves_bls12_381_mul_projective_g2_version_1 + runtime::WasmSpan ext_elliptic_curves_bls12_381_mul_projective_g2_version_1( + runtime::WasmSpan base, runtime::WasmSpan scalar) const; + + /// @see HostApi::ext_elliptic_curves_bls12_381_msm_g1_version_1 + runtime::WasmSpan ext_elliptic_curves_bls12_381_msm_g1_version_1( + runtime::WasmSpan bases, runtime::WasmSpan scalars) const; + + /// @see HostApi::ext_elliptic_curves_bls12_381_msm_g2_version_1 + runtime::WasmSpan ext_elliptic_curves_bls12_381_msm_g2_version_1( + runtime::WasmSpan bases, runtime::WasmSpan scalars) const; + + /// @see + /// HostApi::ext_elliptic_curves_ed_on_bls12_381_bandersnatch_sw_mul_projective_version_1 + runtime::WasmSpan + ext_elliptic_curves_ed_on_bls12_381_bandersnatch_sw_mul_projective_version_1( + runtime::WasmSpan base, runtime::WasmSpan scalar) const; + + private: + runtime::Memory &getMemory() const { + return memory_provider_->getCurrentMemory()->get(); + } + + log::Logger logger_; + std::shared_ptr memory_provider_; + std::shared_ptr elliptic_curves_; + }; +} // namespace kagome::host_api diff --git a/core/host_api/impl/host_api_factory_impl.cpp b/core/host_api/impl/host_api_factory_impl.cpp index da5ae5c9e4..3049b1dae5 100644 --- a/core/host_api/impl/host_api_factory_impl.cpp +++ b/core/host_api/impl/host_api_factory_impl.cpp @@ -16,6 +16,7 @@ namespace kagome::host_api { std::shared_ptr ecdsa_provider, std::shared_ptr ed25519_provider, std::shared_ptr secp256k1_provider, + std::shared_ptr elliptic_curves, std::shared_ptr hasher, std::shared_ptr crypto_store, std::shared_ptr @@ -26,6 +27,7 @@ namespace kagome::host_api { ecdsa_provider_(std::move(ecdsa_provider)), ed25519_provider_(std::move(ed25519_provider)), secp256k1_provider_(std::move(secp256k1_provider)), + elliptic_curves_(std::move(elliptic_curves)), hasher_(std::move(hasher)), crypto_store_(std::move(crypto_store)), offchain_persistent_storage_(std::move(offchain_persistent_storage)), @@ -33,6 +35,7 @@ namespace kagome::host_api { BOOST_ASSERT(sr25519_provider_ != nullptr); BOOST_ASSERT(ed25519_provider_ != nullptr); BOOST_ASSERT(secp256k1_provider_ != nullptr); + BOOST_ASSERT(elliptic_curves_ != nullptr); BOOST_ASSERT(hasher_ != nullptr); BOOST_ASSERT(crypto_store_ != nullptr); BOOST_ASSERT(offchain_persistent_storage_ != nullptr); @@ -51,6 +54,7 @@ namespace kagome::host_api { ecdsa_provider_, ed25519_provider_, secp256k1_provider_, + elliptic_curves_, hasher_, crypto_store_, offchain_persistent_storage_, diff --git a/core/host_api/impl/host_api_factory_impl.hpp b/core/host_api/impl/host_api_factory_impl.hpp index 44b245cd95..01cfa501f9 100644 --- a/core/host_api/impl/host_api_factory_impl.hpp +++ b/core/host_api/impl/host_api_factory_impl.hpp @@ -16,6 +16,10 @@ #include "crypto/sr25519_provider.hpp" #include "host_api/impl/offchain_extension.hpp" +namespace kagome::crypto { + class EllipticCurves; +} + namespace kagome::offchain { class OffchainPersistentStorage; class OffchainWorkerPool; @@ -33,6 +37,7 @@ namespace kagome::host_api { std::shared_ptr ecdsa_provider, std::shared_ptr ed25519_provider, std::shared_ptr secp256k1_provider, + std::shared_ptr elliptic_curves, std::shared_ptr hasher, std::shared_ptr crypto_store, std::shared_ptr @@ -51,6 +56,7 @@ namespace kagome::host_api { std::shared_ptr ecdsa_provider_; std::shared_ptr ed25519_provider_; std::shared_ptr secp256k1_provider_; + std::shared_ptr elliptic_curves_; std::shared_ptr hasher_; std::shared_ptr crypto_store_; std::shared_ptr diff --git a/core/host_api/impl/host_api_impl.cpp b/core/host_api/impl/host_api_impl.cpp index c6a94c228a..a68918bb10 100644 --- a/core/host_api/impl/host_api_impl.cpp +++ b/core/host_api/impl/host_api_impl.cpp @@ -27,6 +27,7 @@ namespace kagome::host_api { std::shared_ptr ecdsa_provider, std::shared_ptr ed25519_provider, std::shared_ptr secp256k1_provider, + std::shared_ptr elliptic_curves, std::shared_ptr hasher, std::shared_ptr crypto_store, std::shared_ptr @@ -47,6 +48,7 @@ namespace kagome::host_api { std::move(secp256k1_provider), hasher, std::move(crypto_store)), + elliptic_curves_ext_(memory_provider_, std::move(elliptic_curves)), io_ext_(memory_provider_), memory_ext_(memory_provider_), misc_ext_{DEFAULT_CHAIN_ID, @@ -585,4 +587,56 @@ namespace kagome::host_api { throw std::runtime_error{msg}; } + // ---------------------------- Elliptic Curves ---------------------------- + + runtime::WasmSpan + HostApiImpl::ext_elliptic_curves_bls12_381_multi_miller_loop_version_1( + runtime::WasmSpan a, runtime::WasmSpan b) const { + return elliptic_curves_ext_ + .ext_elliptic_curves_bls12_381_multi_miller_loop_version_1(a, b); + } + + runtime::WasmSpan + HostApiImpl::ext_elliptic_curves_bls12_381_final_exponentiation_version_1( + runtime::WasmSpan f) const { + return elliptic_curves_ext_ + .ext_elliptic_curves_bls12_381_final_exponentiation_version_1(f); + } + + runtime::WasmSpan + HostApiImpl::ext_elliptic_curves_bls12_381_mul_projective_g1_version_1( + runtime::WasmSpan base, runtime::WasmSpan scalar) const { + return elliptic_curves_ext_ + .ext_elliptic_curves_bls12_381_mul_projective_g1_version_1(base, + scalar); + } + + runtime::WasmSpan + HostApiImpl::ext_elliptic_curves_bls12_381_mul_projective_g2_version_1( + runtime::WasmSpan base, runtime::WasmSpan scalar) const { + return elliptic_curves_ext_ + .ext_elliptic_curves_bls12_381_mul_projective_g2_version_1(base, + scalar); + } + + runtime::WasmSpan HostApiImpl::ext_elliptic_curves_bls12_381_msm_g1_version_1( + runtime::WasmSpan bases, runtime::WasmSpan scalars) const { + return elliptic_curves_ext_.ext_elliptic_curves_bls12_381_msm_g1_version_1( + bases, scalars); + } + + runtime::WasmSpan HostApiImpl::ext_elliptic_curves_bls12_381_msm_g2_version_1( + runtime::WasmSpan bases, runtime::WasmSpan scalars) const { + return elliptic_curves_ext_.ext_elliptic_curves_bls12_381_msm_g2_version_1( + bases, scalars); + } + + runtime::WasmSpan HostApiImpl:: + ext_elliptic_curves_ed_on_bls12_381_bandersnatch_sw_mul_projective_version_1( + runtime::WasmSpan base, runtime::WasmSpan scalar) const { + return elliptic_curves_ext_ + .ext_elliptic_curves_ed_on_bls12_381_bandersnatch_sw_mul_projective_version_1( + base, scalar); + } + } // namespace kagome::host_api diff --git a/core/host_api/impl/host_api_impl.hpp b/core/host_api/impl/host_api_impl.hpp index 185e0005a2..a8bf4b3f75 100644 --- a/core/host_api/impl/host_api_impl.hpp +++ b/core/host_api/impl/host_api_impl.hpp @@ -10,6 +10,7 @@ #include "host_api/impl/child_storage_extension.hpp" #include "host_api/impl/crypto_extension.hpp" +#include "host_api/impl/elliptic_curves_extension.hpp" #include "host_api/impl/io_extension.hpp" #include "host_api/impl/memory_extension.hpp" #include "host_api/impl/misc_extension.hpp" @@ -42,6 +43,7 @@ namespace kagome::host_api { std::shared_ptr ecdsa_provider, std::shared_ptr ed25519_provider, std::shared_ptr secp256k1_provider, + std::shared_ptr elliptic_curves, std::shared_ptr hasher, std::shared_ptr crypto_store, std::shared_ptr @@ -52,7 +54,7 @@ namespace kagome::host_api { void reset() override; - // ------------------------ Storage extensions v1 ------------------------ + // ------------------------ Storage extensions ------------------------ runtime::WasmSpan ext_storage_read_version_1( runtime::WasmSpan key, @@ -103,13 +105,13 @@ namespace kagome::host_api { runtime::WasmPointer ext_trie_blake2_256_ordered_root_version_2( runtime::WasmSpan values_data, runtime::WasmI32 state_version) override; - // ------------------------Memory extensions v1------------------------- + // ------------------------ Memory extensions ------------------------- runtime::WasmPointer ext_allocator_malloc_version_1( runtime::WasmSize size) override; void ext_allocator_free_version_1(runtime::WasmPointer ptr) override; - // -------------------------Crypto extensions v1--------------------- + // ------------------------- Crypto extensions --------------------- void ext_crypto_start_batch_verify_version_1() override; @@ -343,25 +345,50 @@ namespace kagome::host_api { runtime::WasmSpan prefix, runtime::WasmSpan limit) override; - virtual runtime::WasmSpan ext_default_child_storage_read_version_1( + runtime::WasmSpan ext_default_child_storage_read_version_1( runtime::WasmSpan child_storage_key, runtime::WasmSpan key, runtime::WasmSpan value_out, runtime::WasmOffset offset) const override; - virtual int32_t ext_default_child_storage_exists_version_1( + int32_t ext_default_child_storage_exists_version_1( runtime::WasmSpan child_storage_key, runtime::WasmSpan key) const override; - virtual void ext_default_child_storage_storage_kill_version_1( + void ext_default_child_storage_storage_kill_version_1( runtime::WasmSpan child_storage_key) override; - virtual runtime::WasmSpan ext_default_child_storage_storage_kill_version_3( + runtime::WasmSpan ext_default_child_storage_storage_kill_version_3( runtime::WasmSpan child_storage_key, runtime::WasmSpan limit) override; virtual void ext_panic_handler_abort_on_panic_version_1( runtime::WasmSpan message) override; + // ---------------------------- Elliptic Curves ---------------------------- + + runtime::WasmSpan ext_elliptic_curves_bls12_381_multi_miller_loop_version_1( + runtime::WasmSpan a, runtime::WasmSpan b) const override; + + runtime::WasmSpan + ext_elliptic_curves_bls12_381_final_exponentiation_version_1( + runtime::WasmSpan f) const override; + + runtime::WasmSpan ext_elliptic_curves_bls12_381_mul_projective_g1_version_1( + runtime::WasmSpan base, runtime::WasmSpan scalar) const override; + + runtime::WasmSpan ext_elliptic_curves_bls12_381_mul_projective_g2_version_1( + runtime::WasmSpan base, runtime::WasmSpan scalar) const override; + + runtime::WasmSpan ext_elliptic_curves_bls12_381_msm_g1_version_1( + runtime::WasmSpan bases, runtime::WasmSpan scalars) const override; + + runtime::WasmSpan ext_elliptic_curves_bls12_381_msm_g2_version_1( + runtime::WasmSpan bases, runtime::WasmSpan scalars) const override; + + runtime::WasmSpan + ext_elliptic_curves_ed_on_bls12_381_bandersnatch_sw_mul_projective_version_1( + runtime::WasmSpan base, runtime::WasmSpan scalar) const override; + private: static constexpr uint64_t DEFAULT_CHAIN_ID = 42; @@ -369,6 +396,7 @@ namespace kagome::host_api { std::shared_ptr storage_provider_; CryptoExtension crypto_ext_; + EllipticCurvesExtension elliptic_curves_ext_; IOExtension io_ext_; MemoryExtension memory_ext_; MiscExtension misc_ext_; diff --git a/core/injector/CMakeLists.txt b/core/injector/CMakeLists.txt index 4a23aec877..4f31773552 100644 --- a/core/injector/CMakeLists.txt +++ b/core/injector/CMakeLists.txt @@ -36,6 +36,7 @@ target_link_libraries(application_injector dispute_coordinator ecdsa_provider ed25519_provider + elliptic_curves executor fd_limit filesystem diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index d2f0982e44..9007448e51 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -82,6 +82,7 @@ #include "crypto/crypto_store/session_keys.hpp" #include "crypto/ecdsa/ecdsa_provider_impl.hpp" #include "crypto/ed25519/ed25519_provider_impl.hpp" +#include "crypto/elliptic_curves/elliptic_curves_impl.hpp" #include "crypto/hasher/hasher_impl.hpp" #include "crypto/pbkdf2/impl/pbkdf2_provider_impl.hpp" #include "crypto/random_generator/boost_generator.hpp" @@ -871,6 +872,7 @@ namespace { di::bind.template to(), di::bind.template to(), di::bind.template to(), + di::bind.template to(), // user-defined overrides... std::forward(args)...); diff --git a/core/runtime/binaryen/runtime_external_interface.cpp b/core/runtime/binaryen/runtime_external_interface.cpp index 5e83b047ac..22c76bedec 100644 --- a/core/runtime/binaryen/runtime_external_interface.cpp +++ b/core/runtime/binaryen/runtime_external_interface.cpp @@ -244,6 +244,20 @@ namespace kagome::runtime::binaryen { REGISTER_HOST_API_FUNC(ext_offchain_index_set_version_1); REGISTER_HOST_API_FUNC(ext_offchain_index_clear_version_1); + // ---------------------------- Elliptic Curves ---------------------------- + + REGISTER_HOST_API_FUNC( + ext_elliptic_curves_bls12_381_final_exponentiation_version_1); + REGISTER_HOST_API_FUNC(ext_elliptic_curves_bls12_381_msm_g1_version_1); + REGISTER_HOST_API_FUNC( + ext_elliptic_curves_bls12_381_mul_projective_g1_version_1); + REGISTER_HOST_API_FUNC( + ext_elliptic_curves_bls12_381_mul_projective_g2_version_1); + REGISTER_HOST_API_FUNC( + ext_elliptic_curves_bls12_381_multi_miller_loop_version_1); + REGISTER_HOST_API_FUNC( + ext_elliptic_curves_ed_on_bls12_381_bandersnatch_sw_mul_projective_version_1); + REGISTER_HOST_API_FUNC(ext_panic_handler_abort_on_panic_version_1); } diff --git a/core/runtime/wavm/intrinsics/intrinsic_functions.cpp b/core/runtime/wavm/intrinsics/intrinsic_functions.cpp index de4fd549b1..a16d78f5ab 100644 --- a/core/runtime/wavm/intrinsics/intrinsic_functions.cpp +++ b/core/runtime/wavm/intrinsics/intrinsic_functions.cpp @@ -677,6 +677,69 @@ namespace kagome::runtime::wavm { return peekHostApi()->ext_logging_max_level_version_1(); } + WAVM_DEFINE_INTRINSIC_FUNCTION( + WAVM::I64, + ext_elliptic_curves_bls12_381_multi_miller_loop_version_1, + WAVM::I64 a, + WAVM::I64 b) { + return peekHostApi() + ->ext_elliptic_curves_bls12_381_multi_miller_loop_version_1(a, b); + } + + WAVM_DEFINE_INTRINSIC_FUNCTION( + WAVM::I64, + ext_elliptic_curves_bls12_381_final_exponentiation_version_1, + WAVM::I64 f) { + return peekHostApi() + ->ext_elliptic_curves_bls12_381_final_exponentiation_version_1(f); + } + + WAVM_DEFINE_INTRINSIC_FUNCTION( + WAVM::I64, + ext_elliptic_curves_bls12_381_mul_projective_g1_version_1, + WAVM::I64 base, + WAVM::I64 scalar) { + return peekHostApi() + ->ext_elliptic_curves_bls12_381_mul_projective_g1_version_1(base, + scalar); + } + + WAVM_DEFINE_INTRINSIC_FUNCTION( + WAVM::I64, + ext_elliptic_curves_bls12_381_mul_projective_g2_version_1, + WAVM::I64 base, + WAVM::I64 scalar) { + return peekHostApi() + ->ext_elliptic_curves_bls12_381_mul_projective_g2_version_1(base, + scalar); + } + + WAVM_DEFINE_INTRINSIC_FUNCTION(WAVM::I64, + ext_elliptic_curves_bls12_381_msm_g1_version_1, + WAVM::I64 bases, + WAVM::I64 scalars) { + return peekHostApi()->ext_elliptic_curves_bls12_381_msm_g1_version_1( + bases, scalars); + } + + WAVM_DEFINE_INTRINSIC_FUNCTION(WAVM::I64, + ext_elliptic_curves_bls12_381_msm_g2_version_1, + WAVM::I64 bases, + WAVM::I64 scalars) { + return peekHostApi()->ext_elliptic_curves_bls12_381_msm_g2_version_1( + bases, scalars); + } + + WAVM_DEFINE_INTRINSIC_FUNCTION( + WAVM::I64, + ext_elliptic_curves_ed_on_bls12_381_bandersnatch_sw_mul_projective_version_1, + WAVM::I64 base, + WAVM::I64 scalar) { + return peekHostApi() + ->ext_elliptic_curves_ed_on_bls12_381_bandersnatch_sw_mul_projective_version_1( + base, scalar); + } + WAVM_DEFINE_INTRINSIC_FUNCTION_STUB(void, ext_sandbox_instance_teardown_version_1, WAVM::I32) @@ -927,6 +990,15 @@ namespace kagome::runtime::wavm { REGISTER_HOST_INTRINSIC( , ext_panic_handler_abort_on_panic_version_1, I64) + // ---------------------------- Elliptic Curves ---------------------------- + + REGISTER_HOST_INTRINSIC(I64, ext_elliptic_curves_bls12_381_final_exponentiation_version_1, I64) + REGISTER_HOST_INTRINSIC(I64, ext_elliptic_curves_bls12_381_msm_g1_version_1, I64, I64) + REGISTER_HOST_INTRINSIC(I64, ext_elliptic_curves_bls12_381_mul_projective_g1_version_1, I64, I64) + REGISTER_HOST_INTRINSIC(I64, ext_elliptic_curves_bls12_381_mul_projective_g2_version_1, I64, I64) + REGISTER_HOST_INTRINSIC(I64, ext_elliptic_curves_bls12_381_multi_miller_loop_version_1, I64, I64) + REGISTER_HOST_INTRINSIC(I64, ext_elliptic_curves_ed_on_bls12_381_bandersnatch_sw_mul_projective_version_1, I64, I64) + // clang-format on } diff --git a/test/core/runtime/runtime_test_base.hpp b/test/core/runtime/runtime_test_base.hpp index 6bb3995738..293763e077 100644 --- a/test/core/runtime/runtime_test_base.hpp +++ b/test/core/runtime/runtime_test_base.hpp @@ -15,6 +15,7 @@ #include "crypto/crypto_store/crypto_store_impl.hpp" #include "crypto/ecdsa/ecdsa_provider_impl.hpp" #include "crypto/ed25519/ed25519_provider_impl.hpp" +#include "crypto/elliptic_curves/elliptic_curves_impl.hpp" #include "crypto/hasher/hasher_impl.hpp" #include "crypto/pbkdf2/impl/pbkdf2_provider_impl.hpp" #include "crypto/random_generator/boost_generator.hpp" @@ -77,6 +78,7 @@ class RuntimeTestBase : public ::testing::Test { auto ed25519_provider = std::make_shared(hasher_); auto secp256k1_provider = std::make_shared(); + auto elliptic_curves = std::make_shared(); auto pbkdf2_provider = std::make_shared(); auto bip39_provider = std::make_shared(pbkdf2_provider, hasher_); @@ -100,6 +102,7 @@ class RuntimeTestBase : public ::testing::Test { ecdsa_provider, ed25519_provider, secp256k1_provider, + elliptic_curves, hasher_, crypto_store, offchain_storage_, diff --git a/test/core/runtime/wavm/wavm_module_init_test.cpp b/test/core/runtime/wavm/wavm_module_init_test.cpp index 5c9f33b02d..e045f50e67 100644 --- a/test/core/runtime/wavm/wavm_module_init_test.cpp +++ b/test/core/runtime/wavm/wavm_module_init_test.cpp @@ -9,20 +9,20 @@ #include #include #include - -#include "blockchain/impl/block_header_repository_impl.hpp" //header_repo -#include "crypto/bip39/impl/bip39_provider_impl.hpp" //bip39_provider -#include "crypto/crypto_store/crypto_store_impl.hpp" //crypto_store -#include "crypto/ecdsa/ecdsa_provider_impl.hpp" //ecdsa_provider -#include "crypto/ed25519/ed25519_provider_impl.hpp" //ed25519_provider -#include "crypto/hasher/hasher_impl.hpp" //hasher -#include "crypto/pbkdf2/impl/pbkdf2_provider_impl.hpp" //pbkdf2_provider -#include "crypto/secp256k1/secp256k1_provider_impl.hpp" //secp256k1_provider -#include "crypto/sr25519/sr25519_provider_impl.hpp" //sr25519_provider -#include "host_api/impl/host_api_factory_impl.hpp" // host_api_factory -#include "offchain/impl/offchain_persistent_storage.hpp" //offchain_persistent_store -#include "offchain/impl/offchain_worker_pool_impl.hpp" //offchain_worker_pool -#include "runtime/common/runtime_properties_cache_impl.hpp" // cache +#include "blockchain/impl/block_header_repository_impl.hpp" +#include "crypto/bip39/impl/bip39_provider_impl.hpp" +#include "crypto/crypto_store/crypto_store_impl.hpp" +#include "crypto/ecdsa/ecdsa_provider_impl.hpp" +#include "crypto/ed25519/ed25519_provider_impl.hpp" +#include "crypto/elliptic_curves/elliptic_curves_impl.hpp" +#include "crypto/hasher/hasher_impl.hpp" +#include "crypto/pbkdf2/impl/pbkdf2_provider_impl.hpp" +#include "crypto/secp256k1/secp256k1_provider_impl.hpp" +#include "crypto/sr25519/sr25519_provider_impl.hpp" +#include "host_api/impl/host_api_factory_impl.hpp" +#include "offchain/impl/offchain_persistent_storage.hpp" +#include "offchain/impl/offchain_worker_pool_impl.hpp" +#include "runtime/common/runtime_properties_cache_impl.hpp" #include "runtime/executor.hpp" #include "runtime/memory_provider.hpp" #include "runtime/module.hpp" // smc @@ -88,6 +88,8 @@ class WavmModuleInitTest : public ::testing::TestWithParam { std::make_shared(hasher); auto secp256k1_provider = std::make_shared(); + auto elliptic_curves = + std::shared_ptr(); auto pbkdf2_provider = std::make_shared(); auto bip39_provider = std::make_shared( @@ -129,6 +131,7 @@ class WavmModuleInitTest : public ::testing::TestWithParam { ecdsa_provider, ed25519_provider, secp256k1_provider, + elliptic_curves, hasher, crypto_store, offchain_persistent_storage, diff --git a/test/external-project-test/src/main.cpp b/test/external-project-test/src/main.cpp index 4487bf3fae..5ea9a97647 100644 --- a/test/external-project-test/src/main.cpp +++ b/test/external-project-test/src/main.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -172,6 +173,7 @@ int main() { std::make_shared(ed25519_provider); auto sr_suite = std::make_shared(sr25519_provider); + auto elliptic_curves = std::make_shared(); std::shared_ptr key_fs = kagome::crypto::KeyFileStorage::createAt("/tmp/kagome_tmp_key_storage") .value(); @@ -194,6 +196,7 @@ int main() { ecdsa_provider, ed25519_provider, secp256k1_provider, + elliptic_curves, hasher, crypto_store, offchain_persistent_storage, diff --git a/test/mock/core/host_api/host_api_mock.hpp b/test/mock/core/host_api/host_api_mock.hpp index af3439f5bd..5cbb4477f9 100644 --- a/test/mock/core/host_api/host_api_mock.hpp +++ b/test/mock/core/host_api/host_api_mock.hpp @@ -495,6 +495,44 @@ namespace kagome::host_api { ext_panic_handler_abort_on_panic_version_1, (runtime::WasmSpan), (override)); + + // ---------------------------- Elliptic Curves ---------------------------- + + MOCK_METHOD(runtime::WasmSpan, + ext_elliptic_curves_bls12_381_multi_miller_loop_version_1, + (runtime::WasmSpan a, runtime::WasmSpan b), + (const, override)); + + MOCK_METHOD(runtime::WasmSpan, + ext_elliptic_curves_bls12_381_final_exponentiation_version_1, + (runtime::WasmSpan f), + (const, override)); + + MOCK_METHOD(runtime::WasmSpan, + ext_elliptic_curves_bls12_381_mul_projective_g1_version_1, + (runtime::WasmSpan base, runtime::WasmSpan scalar), + (const, override)); + + MOCK_METHOD(runtime::WasmSpan, + ext_elliptic_curves_bls12_381_mul_projective_g2_version_1, + (runtime::WasmSpan base, runtime::WasmSpan scalar), + (const, override)); + + MOCK_METHOD(runtime::WasmSpan, + ext_elliptic_curves_bls12_381_msm_g1_version_1, + (runtime::WasmSpan bases, runtime::WasmSpan scalars), + (const, override)); + + MOCK_METHOD(runtime::WasmSpan, + ext_elliptic_curves_bls12_381_msm_g2_version_1, + (runtime::WasmSpan bases, runtime::WasmSpan scalars), + (const, override)); + + MOCK_METHOD( + runtime::WasmSpan, + ext_elliptic_curves_ed_on_bls12_381_bandersnatch_sw_mul_projective_version_1, + (runtime::WasmSpan base, runtime::WasmSpan scalar), + (const, override)); }; } // namespace kagome::host_api From 8afaf2c0c2866b545c2c42341a9cbe700a669828 Mon Sep 17 00:00:00 2001 From: Dmitriy Khaustov aka xDimon Date: Mon, 8 Jul 2024 13:53:54 +0300 Subject: [PATCH 2/2] hotfix Signed-off-by: Dmitriy Khaustov aka xDimon --- core/parachain/validator/impl/parachain_processor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/parachain/validator/impl/parachain_processor.cpp b/core/parachain/validator/impl/parachain_processor.cpp index d9a73468fd..aa200b0bc1 100644 --- a/core/parachain/validator/impl/parachain_processor.cpp +++ b/core/parachain/validator/impl/parachain_processor.cpp @@ -2115,7 +2115,7 @@ namespace kagome::parachain { return Error::CLUSTER_TRACKER_ERROR; } OUTCOME_TRY(check_statement_signature( - session, session_info.validators, relay_parent, statement); + session, session_info.validators, relay_parent, statement)); cluster_tracker.note_received( cluster_sender_index,