Skip to content

Commit

Permalink
feature: elliptic curves functions
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitriy Khaustov aka xDimon <khaustov.dm@gmail.com>
  • Loading branch information
xDimon committed Jan 13, 2024
1 parent 536a72b commit 5bca65d
Show file tree
Hide file tree
Showing 23 changed files with 836 additions and 46 deletions.
7 changes: 7 additions & 0 deletions cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
45 changes: 27 additions & 18 deletions core/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
87 changes: 87 additions & 0 deletions core/crypto/elliptic_curves.hpp
Original file line number Diff line number Diff line change
@@ -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<Vec<ark_ec::bls12::G1Prepared::<ark_bls12_381::Config>>>
* @param b
* ArkScale<Vec<ark_ec::bls12::G1Prepared::<ark_bls12_381::Config>>>
* @return ArkScale<MillerLoopOutput<Bls12<ark_bls12_381::Config>>>
*/
virtual outcome::result<common::Buffer> bls12_381_multi_miller_loop(
common::BufferView a, common::BufferView b) const = 0;

/**
* Pairing final exponentiation for BLS12-381.
* @param f ArkScale<MillerLoopOutput<Bls12<ark_bls12_381::Config>>>
* @return ArkScale<PairingOutput<Bls12<ark_bls12_381::Config>>>
*/
virtual outcome::result<common::Buffer> bls12_381_final_exponentiation(
common::BufferView f) const = 0;

/**
* Projective multiplication on G1 for BLS12-381.
* @param base ArkScaleProjective<ark_bls12_381::G1Projective>
* @param scalar ArkScale<&[u64]>
* @return ArkScaleProjective<ark_bls12_381::G1Projective>
*/
virtual outcome::result<common::Buffer> bls12_381_mul_projective_g1(
common::BufferView base, common::BufferView scalar) const = 0;

/**
* Projective multiplication on G2 for BLS12-381.
* @param base ArkScaleProjective<ark_bls12_381::G2Projective>
* @param scalar ArkScale<&[u64]>
* @return ArkScaleProjective<ark_bls12_381::G2Projective>
*/
virtual outcome::result<common::Buffer> 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<ark_bls12_381::G1Projective>
*/
virtual outcome::result<common::Buffer> 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<ark_bls12_381::G2Projective>
*/
virtual outcome::result<common::Buffer> 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<ark_ed_on_bls12_381_bandersnatch::SWProjective>
* @param scalar ArkScale<&[u64]>
* @return
* ArkScaleProjective<ark_ed_on_bls12_381_bandersnatch::SWProjective>
*/
virtual outcome::result<common::Buffer>
ed_on_bls12_381_bandersnatch_sw_mul_projective(
common::BufferView base, common::BufferView scalar) const = 0;
};

} // namespace kagome::crypto
83 changes: 83 additions & 0 deletions core/crypto/elliptic_curves/elliptic_curves_impl.cpp
Original file line number Diff line number Diff line change
@@ -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 <arkworks_crust.h>

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<uint8_t *>(view.data()), .size = view.size()};
}
outcome::result<common::Buffer> 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<common::Buffer>
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<common::Buffer>
EllipticCurvesImpl::bls12_381_final_exponentiation(
common::BufferView f) const {
return convert(::bls12_381_final_exponentiation(convert(f)));
}

outcome::result<common::Buffer>
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<common::Buffer>
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<common::Buffer> EllipticCurvesImpl::bls12_381_msm_g1(
common::BufferView bases, common::BufferView scalars) const {
return convert(::bls12_381_msm_g1(convert(bases), convert(scalars)));
}

outcome::result<common::Buffer> EllipticCurvesImpl::bls12_381_msm_g2(
common::BufferView bases, common::BufferView scalars) const {
return convert(::bls12_381_msm_g2(convert(bases), convert(scalars)));
}

outcome::result<common::Buffer>
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
42 changes: 42 additions & 0 deletions core/crypto/elliptic_curves/elliptic_curves_impl.hpp
Original file line number Diff line number Diff line change
@@ -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<common::Buffer> bls12_381_multi_miller_loop(
common::BufferView a, common::BufferView b) const override;

outcome::result<common::Buffer> bls12_381_final_exponentiation(
common::BufferView f) const override;

outcome::result<common::Buffer> bls12_381_mul_projective_g1(
common::BufferView base, common::BufferView scalar) const override;

outcome::result<common::Buffer> bls12_381_mul_projective_g2(
common::BufferView base, common::BufferView scalar) const override;

outcome::result<common::Buffer> bls12_381_msm_g1(
common::BufferView bases, common::BufferView scalars) const override;

outcome::result<common::Buffer> bls12_381_msm_g2(
common::BufferView bases, common::BufferView scalars) const override;

outcome::result<common::Buffer>
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);
1 change: 1 addition & 0 deletions core/host_api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ target_link_libraries(host_api
storage_extension
child_storage_extension
offchain_extension
elliptic_curves_extension
)
kagome_install(host_api)
Loading

0 comments on commit 5bca65d

Please sign in to comment.