-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: elliptic curves functions Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]> * hotfix Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]> --------- Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
- Loading branch information
Showing
37 changed files
with
821 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* Copyright Quadrivium LLC | ||
* All Rights Reserved | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "common/blob.hpp" | ||
#include "common/buffer.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.