Elliptic curves utilities refactory (#2068)

- Usage the new published
[arkworks-extensions](https://github.com/paritytech/arkworks-extensions)
crates.
  Hooks are internally defined to jump into the proper host functions.
- Conditional compilation of each curve (gated by feature with curve
name)
- Separation in smaller host functions sets, divided by curve (fits
nicely with prev point)
This commit is contained in:
Davide Galassi
2023-10-31 14:59:15 +01:00
committed by GitHub
parent 3ae86ae075
commit c38aae628b
9 changed files with 1065 additions and 368 deletions
@@ -0,0 +1,205 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! *BLS12-377* types and host functions.
use crate::utils;
use ark_bls12_377_ext::CurveHooks;
use ark_ec::{pairing::Pairing, CurveConfig};
use sp_runtime_interface::runtime_interface;
use sp_std::vec::Vec;
/// First pairing group definitions.
pub mod g1 {
pub use ark_bls12_377_ext::g1::{
G1_GENERATOR_X, G1_GENERATOR_Y, TE_GENERATOR_X, TE_GENERATOR_Y,
};
/// Group configuration.
pub type Config = ark_bls12_377_ext::g1::Config<super::HostHooks>;
/// Short Weierstrass form point affine representation.
pub type G1Affine = ark_bls12_377_ext::g1::G1Affine<super::HostHooks>;
/// Short Weierstrass form point projective representation.
pub type G1Projective = ark_bls12_377_ext::g1::G1Projective<super::HostHooks>;
/// Short Weierstrass form point affine representation.
pub type G1SWAffine = ark_bls12_377_ext::g1::G1SWAffine<super::HostHooks>;
/// Short Weierstrass form point projective representation.
pub type G1SWProjective = ark_bls12_377_ext::g1::G1SWProjective<super::HostHooks>;
/// Twisted Edwards form point affine representation.
pub type G1TEAffine = ark_bls12_377_ext::g1::G1TEAffine<super::HostHooks>;
/// Twisted Edwards form point projective representation.
pub type G1TEProjective = ark_bls12_377_ext::g1::G1TEProjective<super::HostHooks>;
}
/// Second pairing group definitions.
pub mod g2 {
pub use ark_bls12_377_ext::g2::{
G2_GENERATOR_X, G2_GENERATOR_X_C0, G2_GENERATOR_X_C1, G2_GENERATOR_Y, G2_GENERATOR_Y_C0,
G2_GENERATOR_Y_C1,
};
/// Group configuration.
pub type Config = ark_bls12_377_ext::g2::Config<super::HostHooks>;
/// Short Weierstrass form point affine representation.
pub type G2Affine = ark_bls12_377_ext::g2::G2Affine<super::HostHooks>;
/// Short Weierstrass form point projective representation.
pub type G2Projective = ark_bls12_377_ext::g2::G2Projective<super::HostHooks>;
}
pub use self::{
g1::{Config as G1Config, G1Affine, G1Projective},
g2::{Config as G2Config, G2Affine, G2Projective},
};
/// Curve hooks jumping into [`host_calls`] host functions.
#[derive(Copy, Clone)]
pub struct HostHooks;
/// Configuration for *BLS12-377* curve.
pub type Config = ark_bls12_377_ext::Config<HostHooks>;
/// *BLS12-377* definition.
///
/// A generic *BLS12* model specialized with *BLS12-377* configuration.
pub type Bls12_377 = ark_bls12_377_ext::Bls12_377<HostHooks>;
impl CurveHooks for HostHooks {
fn bls12_377_multi_miller_loop(
g1: impl Iterator<Item = <Bls12_377 as Pairing>::G1Prepared>,
g2: impl Iterator<Item = <Bls12_377 as Pairing>::G2Prepared>,
) -> Result<<Bls12_377 as Pairing>::TargetField, ()> {
let g1 = utils::encode(g1.collect::<Vec<_>>());
let g2 = utils::encode(g2.collect::<Vec<_>>());
let res = host_calls::bls12_377_multi_miller_loop(g1, g2).unwrap_or_default();
utils::decode(res)
}
fn bls12_377_final_exponentiation(
target: <Bls12_377 as Pairing>::TargetField,
) -> Result<<Bls12_377 as Pairing>::TargetField, ()> {
let target = utils::encode(target);
let res = host_calls::bls12_377_final_exponentiation(target).unwrap_or_default();
utils::decode(res)
}
fn bls12_377_msm_g1(
bases: &[G1Affine],
scalars: &[<G1Config as CurveConfig>::ScalarField],
) -> Result<G1Projective, ()> {
let bases = utils::encode(bases);
let scalars = utils::encode(scalars);
let res = host_calls::bls12_377_msm_g1(bases, scalars).unwrap_or_default();
utils::decode_proj_sw(res)
}
fn bls12_377_msm_g2(
bases: &[G2Affine],
scalars: &[<G2Config as CurveConfig>::ScalarField],
) -> Result<G2Projective, ()> {
let bases = utils::encode(bases);
let scalars = utils::encode(scalars);
let res = host_calls::bls12_377_msm_g2(bases, scalars).unwrap_or_default();
utils::decode_proj_sw(res)
}
fn bls12_377_mul_projective_g1(
base: &G1Projective,
scalar: &[u64],
) -> Result<G1Projective, ()> {
let base = utils::encode_proj_sw(base);
let scalar = utils::encode(scalar);
let res = host_calls::bls12_377_mul_projective_g1(base, scalar).unwrap_or_default();
utils::decode_proj_sw(res)
}
fn bls12_377_mul_projective_g2(
base: &G2Projective,
scalar: &[u64],
) -> Result<G2Projective, ()> {
let base = utils::encode_proj_sw(base);
let scalar = utils::encode(scalar);
let res = host_calls::bls12_377_mul_projective_g2(base, scalar).unwrap_or_default();
utils::decode_proj_sw(res)
}
}
/// Interfaces for working with *Arkworks* *BLS12-377* elliptic curve related types
/// from within the runtime.
///
/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
///
/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
/// and "not-compressed".
#[runtime_interface]
pub trait HostCalls {
/// Pairing multi Miller loop for *BLS12-377*.
///
/// - Receives encoded:
/// - `a`: `ArkScale<Vec<G1Affine>>`.
/// - `b`: `ArkScale<Vec<G2Affine>>`.
/// - Returns encoded: `ArkScale<Bls12_377::TargetField>`.
fn bls12_377_multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::multi_miller_loop::<ark_bls12_377::Bls12_377>(a, b)
}
/// Pairing final exponentiation for *BLS12-377.*
///
/// - Receives encoded: `ArkScale<Bls12_377::TargetField>`.
/// - Returns encoded: `ArkScale<Bls12_377::TargetField>`.
fn bls12_377_final_exponentiation(f: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::final_exponentiation::<ark_bls12_377::Bls12_377>(f)
}
/// Multi scalar multiplication on *G1* for *BLS12-377*.
///
/// - Receives encoded:
/// - `bases`: `ArkScale<Vec<G1Affine>>`.
/// - `scalars`: `ArkScale<Vec<G1Config::ScalarField>>`.
/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
fn bls12_377_msm_g1(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::msm_sw::<ark_bls12_377::g1::Config>(bases, scalars)
}
/// Multi scalar multiplication on *G2* for *BLS12-377*.
///
/// - Receives encoded:
/// - `bases`: `ArkScale<Vec<G2Affine>>`.
/// - `scalars`: `ArkScale<Vec<G2Config::ScalarField>>`.
/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
fn bls12_377_msm_g2(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::msm_sw::<ark_bls12_377::g2::Config>(bases, scalars)
}
/// Projective multiplication on *G1* for *BLS12-377*.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<G1Projective>`.
/// - `scalar`: `ArkScale<Vec<u64>>`.
/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
fn bls12_377_mul_projective_g1(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::mul_projective_sw::<ark_bls12_377::g1::Config>(base, scalar)
}
/// Projective multiplication on *G2* for *BLS12-377*.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<G2Projective>`.
/// - `scalar`: `ArkScale<Vec<u64>>`.
/// - Returns encoded: `ArkScaleProjective<ark_bls12_377::G2Projective>`.
fn bls12_377_mul_projective_g2(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::mul_projective_sw::<ark_bls12_377::g2::Config>(base, scalar)
}
}
@@ -0,0 +1,195 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! *BLS12-381* types and host functions.
use crate::utils;
use ark_bls12_381_ext::CurveHooks;
use ark_ec::{pairing::Pairing, CurveConfig};
use sp_runtime_interface::runtime_interface;
use sp_std::vec::Vec;
/// First pairing group definitions.
pub mod g1 {
pub use ark_bls12_381_ext::g1::{BETA, G1_GENERATOR_X, G1_GENERATOR_Y};
/// Group configuration.
pub type Config = ark_bls12_381_ext::g1::Config<super::HostHooks>;
/// Short Weierstrass form point affine representation.
pub type G1Affine = ark_bls12_381_ext::g1::G1Affine<super::HostHooks>;
/// Short Weierstrass form point projective representation.
pub type G1Projective = ark_bls12_381_ext::g1::G1Projective<super::HostHooks>;
}
/// Second pairing group definitions.
pub mod g2 {
pub use ark_bls12_381_ext::g2::{
G2_GENERATOR_X, G2_GENERATOR_X_C0, G2_GENERATOR_X_C1, G2_GENERATOR_Y, G2_GENERATOR_Y_C0,
G2_GENERATOR_Y_C1,
};
/// Group configuration.
pub type Config = ark_bls12_381_ext::g2::Config<super::HostHooks>;
/// Short Weierstrass form point affine representation.
pub type G2Affine = ark_bls12_381_ext::g2::G2Affine<super::HostHooks>;
/// Short Weierstrass form point projective representation.
pub type G2Projective = ark_bls12_381_ext::g2::G2Projective<super::HostHooks>;
}
pub use self::{
g1::{Config as G1Config, G1Affine, G1Projective},
g2::{Config as G2Config, G2Affine, G2Projective},
};
/// Curve hooks jumping into [`host_calls`] host functions.
#[derive(Copy, Clone)]
pub struct HostHooks;
/// Configuration for *BLS12-381* curve.
pub type Config = ark_bls12_381_ext::Config<HostHooks>;
/// *BLS12-381* definition.
///
/// A generic *BLS12* model specialized with *BLS12-381* configuration.
pub type Bls12_381 = ark_bls12_381_ext::Bls12_381<HostHooks>;
impl CurveHooks for HostHooks {
fn bls12_381_multi_miller_loop(
g1: impl Iterator<Item = <Bls12_381 as Pairing>::G1Prepared>,
g2: impl Iterator<Item = <Bls12_381 as Pairing>::G2Prepared>,
) -> Result<<Bls12_381 as Pairing>::TargetField, ()> {
let g1 = utils::encode(g1.collect::<Vec<_>>());
let g2 = utils::encode(g2.collect::<Vec<_>>());
let res = host_calls::bls12_381_multi_miller_loop(g1, g2).unwrap_or_default();
utils::decode(res)
}
fn bls12_381_final_exponentiation(
target: <Bls12_381 as Pairing>::TargetField,
) -> Result<<Bls12_381 as Pairing>::TargetField, ()> {
let target = utils::encode(target);
let res = host_calls::bls12_381_final_exponentiation(target).unwrap_or_default();
utils::decode(res)
}
fn bls12_381_msm_g1(
bases: &[G1Affine],
scalars: &[<G1Config as CurveConfig>::ScalarField],
) -> Result<G1Projective, ()> {
let bases = utils::encode(bases);
let scalars = utils::encode(scalars);
let res = host_calls::bls12_381_msm_g1(bases, scalars).unwrap_or_default();
utils::decode_proj_sw(res)
}
fn bls12_381_msm_g2(
bases: &[G2Affine],
scalars: &[<G2Config as CurveConfig>::ScalarField],
) -> Result<G2Projective, ()> {
let bases = utils::encode(bases);
let scalars = utils::encode(scalars);
let res = host_calls::bls12_381_msm_g2(bases, scalars).unwrap_or_default();
utils::decode_proj_sw(res)
}
fn bls12_381_mul_projective_g1(
base: &G1Projective,
scalar: &[u64],
) -> Result<G1Projective, ()> {
let base = utils::encode_proj_sw(base);
let scalar = utils::encode(scalar);
let res = host_calls::bls12_381_mul_projective_g1(base, scalar).unwrap_or_default();
utils::decode_proj_sw(res)
}
fn bls12_381_mul_projective_g2(
base: &G2Projective,
scalar: &[u64],
) -> Result<G2Projective, ()> {
let base = utils::encode_proj_sw(base);
let scalar = utils::encode(scalar);
let res = host_calls::bls12_381_mul_projective_g2(base, scalar).unwrap_or_default();
utils::decode_proj_sw(res)
}
}
/// Interfaces for working with *Arkworks* *BLS12-381* elliptic curve related types
/// from within the runtime.
///
/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
///
/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
/// and "not-compressed".
#[runtime_interface]
pub trait HostCalls {
/// Pairing multi Miller loop for *BLS12-381*.
///
/// - Receives encoded:
/// - `a`: `ArkScale<Vec<G1Affine>>`.
/// - `b`: `ArkScale<Vec<G2Affine>>`.
/// - Returns encoded: `ArkScale<Bls12_381::TargetField>`.
fn bls12_381_multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::multi_miller_loop::<ark_bls12_381::Bls12_381>(a, b)
}
/// Pairing final exponentiation for *BLS12-381*.
///
/// - Receives encoded: `ArkScale<<Bls12_377::TargetField>`.
/// - Returns encoded: `ArkScale<<Bls12_377::TargetField>`
fn bls12_381_final_exponentiation(f: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::final_exponentiation::<ark_bls12_381::Bls12_381>(f)
}
/// Multi scalar multiplication on *G1* for *BLS12-381*
///
/// - Receives encoded:
/// - `bases`: `ArkScale<Vec<G1Affine>>`.
/// - `scalars`: `ArkScale<Vec<G1Config::ScalarField>>`.
/// - Returns encoded: `ArkScaleProjective<ark_bls12_381::G1Projective>`.
fn bls12_381_msm_g1(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::msm_sw::<ark_bls12_381::g1::Config>(bases, scalars)
}
/// Multi scalar multiplication on *G2* for *BLS12-381*
///
/// - Receives encoded:
/// - `bases`: `ArkScale<Vec<G2Affine>>`.
/// - `scalars`: `ArkScale<Vec<G2Config::ScalarField>>`.
/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
fn bls12_381_msm_g2(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::msm_sw::<ark_bls12_381::g2::Config>(bases, scalars)
}
/// Projective multiplication on *G1* for *BLS12-381*.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<G1Projective>`.
/// - `scalar`: `ArkScale<Vec<u64>>`.
/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
fn bls12_381_mul_projective_g1(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::mul_projective_sw::<ark_bls12_381::g1::Config>(base, scalar)
}
/// Projective multiplication on *G2* for *BLS12-381*
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<G2Projective>`.
/// - `scalar`: `ArkScale<Vec<u64>>`.
/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
fn bls12_381_mul_projective_g2(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::mul_projective_sw::<ark_bls12_381::g2::Config>(base, scalar)
}
}
@@ -0,0 +1,186 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! *BW6-761* types and host functions.
use crate::utils;
use ark_bw6_761_ext::CurveHooks;
use ark_ec::{pairing::Pairing, CurveConfig};
use sp_runtime_interface::runtime_interface;
use sp_std::vec::Vec;
/// First pairing group definitions.
pub mod g1 {
pub use ark_bw6_761_ext::g1::{G1_GENERATOR_X, G1_GENERATOR_Y};
/// Group configuration.
pub type Config = ark_bw6_761_ext::g1::Config<super::HostHooks>;
/// Short Weierstrass form point affine representation.
pub type G1Affine = ark_bw6_761_ext::g1::G1Affine<super::HostHooks>;
/// Short Weierstrass form point projective representation.
pub type G1Projective = ark_bw6_761_ext::g1::G1Projective<super::HostHooks>;
}
/// Second pairing group definitions.
pub mod g2 {
pub use ark_bw6_761_ext::g2::{G2_GENERATOR_X, G2_GENERATOR_Y};
/// Group configuration.
pub type Config = ark_bw6_761_ext::g2::Config<super::HostHooks>;
/// Short Weierstrass form point affine representation.
pub type G2Affine = ark_bw6_761_ext::g2::G2Affine<super::HostHooks>;
/// Short Weierstrass form point projective representation.
pub type G2Projective = ark_bw6_761_ext::g2::G2Projective<super::HostHooks>;
}
pub use self::{
g1::{Config as G1Config, G1Affine, G1Projective},
g2::{Config as G2Config, G2Affine, G2Projective},
};
/// Curve hooks jumping into [`host_calls`] host functions.
#[derive(Copy, Clone)]
pub struct HostHooks;
/// Configuration for *BW6-361* curve.
pub type Config = ark_bw6_761_ext::Config<HostHooks>;
/// *BW6-361* definition.
///
/// A generic *BW6* model specialized with *BW6-761* configuration.
pub type BW6_761 = ark_bw6_761_ext::BW6_761<HostHooks>;
impl CurveHooks for HostHooks {
fn bw6_761_multi_miller_loop(
g1: impl Iterator<Item = <BW6_761 as Pairing>::G1Prepared>,
g2: impl Iterator<Item = <BW6_761 as Pairing>::G2Prepared>,
) -> Result<<BW6_761 as Pairing>::TargetField, ()> {
let g1 = utils::encode(g1.collect::<Vec<_>>());
let g2 = utils::encode(g2.collect::<Vec<_>>());
let res = host_calls::bw6_761_multi_miller_loop(g1, g2).unwrap_or_default();
utils::decode(res)
}
fn bw6_761_final_exponentiation(
target: <BW6_761 as Pairing>::TargetField,
) -> Result<<BW6_761 as Pairing>::TargetField, ()> {
let target = utils::encode(target);
let res = host_calls::bw6_761_final_exponentiation(target).unwrap_or_default();
utils::decode(res)
}
fn bw6_761_msm_g1(
bases: &[G1Affine],
scalars: &[<G1Config as CurveConfig>::ScalarField],
) -> Result<G1Projective, ()> {
let bases = utils::encode(bases);
let scalars = utils::encode(scalars);
let res = host_calls::bw6_761_msm_g1(bases, scalars).unwrap_or_default();
utils::decode_proj_sw(res)
}
fn bw6_761_msm_g2(
bases: &[G2Affine],
scalars: &[<G2Config as CurveConfig>::ScalarField],
) -> Result<G2Projective, ()> {
let bases = utils::encode(bases);
let scalars = utils::encode(scalars);
let res = host_calls::bw6_761_msm_g2(bases, scalars).unwrap_or_default();
utils::decode_proj_sw(res)
}
fn bw6_761_mul_projective_g1(base: &G1Projective, scalar: &[u64]) -> Result<G1Projective, ()> {
let base = utils::encode_proj_sw(base);
let scalar = utils::encode(scalar);
let res = host_calls::bw6_761_mul_projective_g1(base, scalar).unwrap_or_default();
utils::decode_proj_sw(res)
}
fn bw6_761_mul_projective_g2(base: &G2Projective, scalar: &[u64]) -> Result<G2Projective, ()> {
let base = utils::encode_proj_sw(base);
let scalar = utils::encode(scalar);
let res = host_calls::bw6_761_mul_projective_g2(base, scalar).unwrap_or_default();
utils::decode_proj_sw(res)
}
}
/// Interfaces for working with *Arkworks* *BW6-761* elliptic curve related types
/// from within the runtime.
///
/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
///
/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
/// and "not-compressed".
#[runtime_interface]
pub trait HostCalls {
/// Pairing multi Miller loop for *BW6-761*.
///
/// - Receives encoded:
/// - `a: ArkScale<Vec<G1Affine>>`.
/// - `b: ArkScale<Vec<G2Affine>>`.
/// - Returns encoded: `ArkScale<BW6_761;:TargetField>`.
fn bw6_761_multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::multi_miller_loop::<ark_bw6_761::BW6_761>(a, b)
}
/// Pairing final exponentiation for *BW6-761*.
///
/// - Receives encoded: `ArkScale<BW6_761::TargetField>`.
/// - Returns encoded: `ArkScale<BW6_761::TargetField>`.
fn bw6_761_final_exponentiation(f: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::final_exponentiation::<ark_bw6_761::BW6_761>(f)
}
/// Multi scalar multiplication on *G1* for *BW6-761*.
///
/// - Receives encoded:
/// - `bases`: `ArkScale<Vec<G1Affine>>`.
/// - `scalars`: `ArkScale<G1Config::ScalarField>`.
/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
fn bw6_761_msm_g1(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::msm_sw::<ark_bw6_761::g1::Config>(bases, scalars)
}
/// Multi scalar multiplication on *G2* for *BW6-761*.
///
/// - Receives encoded:
/// - `bases`: `ArkScale<Vec<G2Affine>>`.
/// - `scalars`: `ArkScale<Vec<G2Config::ScalarField>>`.
/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
fn bw6_761_msm_g2(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::msm_sw::<ark_bw6_761::g2::Config>(bases, scalars)
}
/// Projective multiplication on *G1* for *BW6-761*.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<G1Projective>`.
/// - `scalar`: `ArkScale<Vec<u64>>`.
/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
fn bw6_761_mul_projective_g1(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::mul_projective_sw::<ark_bw6_761::g1::Config>(base, scalar)
}
/// Projective multiplication on *G2* for *BW6-761*.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<G2Projective>`.
/// - `scalar`: `ArkScale<Vec<u64>>`.
/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
fn bw6_761_mul_projective_g2(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::mul_projective_sw::<ark_bw6_761::g2::Config>(base, scalar)
}
}
@@ -0,0 +1,88 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! *Ed-on-BLS12-377* types and host functions.
use crate::utils;
use ark_ec::CurveConfig;
use ark_ed_on_bls12_377_ext::CurveHooks;
use sp_runtime_interface::runtime_interface;
use sp_std::vec::Vec;
/// Curve hooks jumping into [`host_calls`] host functions.
#[derive(Copy, Clone)]
pub struct HostHooks;
/// Group configuration.
pub type EdwardsConfig = ark_ed_on_bls12_377_ext::EdwardsConfig<HostHooks>;
/// Twisted Edwards form point affine representation.
pub type EdwardsAffine = ark_ed_on_bls12_377_ext::EdwardsAffine<HostHooks>;
/// Twisted Edwards form point projective representation.
pub type EdwardsProjective = ark_ed_on_bls12_377_ext::EdwardsProjective<HostHooks>;
impl CurveHooks for HostHooks {
fn ed_on_bls12_377_msm(
bases: &[EdwardsAffine],
scalars: &[<EdwardsConfig as CurveConfig>::ScalarField],
) -> Result<EdwardsProjective, ()> {
let bases = utils::encode(bases);
let scalars = utils::encode(scalars);
let res = host_calls::ed_on_bls12_377_te_msm(bases, scalars).unwrap_or_default();
utils::decode_proj_te(res)
}
fn ed_on_bls12_377_mul_projective(
base: &EdwardsProjective,
scalar: &[u64],
) -> Result<EdwardsProjective, ()> {
let base = utils::encode_proj_te(base);
let scalar = utils::encode(scalar);
let res = host_calls::ed_on_bls12_377_te_mul_projective(base, scalar).unwrap_or_default();
utils::decode_proj_te(res)
}
}
/// Interfaces for working with *Arkworks* *Ed-on-BLS12-377* elliptic curve
/// related types from within the runtime.
///
/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
///
/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
/// and "not-compressed".
#[runtime_interface]
pub trait HostCalls {
/// Twisted Edwards multi scalar multiplication for *Ed-on-BLS12-377*.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<EdwardsProjective>`.
/// - `scalars`: `ArkScale<Vec<EdwardsConfig::ScalarField>>`.
/// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
fn ed_on_bls12_377_te_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::msm_te::<ark_ed_on_bls12_377::EdwardsConfig>(bases, scalars)
}
/// Twisted Edwards projective multiplication for *Ed-on-BLS12-377*.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<EdwardsProjective>`.
/// - `scalar`: `ArkScale<Vec<u64>>`.
/// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
fn ed_on_bls12_377_te_mul_projective(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
utils::mul_projective_te::<ark_ed_on_bls12_377::EdwardsConfig>(base, scalar)
}
}
@@ -0,0 +1,153 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Elliptic Curves host functions to handle some of the *Arkworks* *Ed-on-BLS12-381-Bandersnatch*
//! computationally expensive operations.
use crate::utils;
use ark_ec::CurveConfig;
use ark_ed_on_bls12_381_bandersnatch_ext::CurveHooks;
use sp_runtime_interface::runtime_interface;
use sp_std::vec::Vec;
/// Curve hooks jumping into [`host_calls`] host functions.
#[derive(Copy, Clone)]
pub struct HostHooks;
/// Group configuration.
pub type BandersnatchConfig = ark_ed_on_bls12_381_bandersnatch_ext::BandersnatchConfig<HostHooks>;
/// Group configuration for Twisted Edwards form (equal to [`BandersnatchConfig`]).
pub type EdwardsConfig = ark_ed_on_bls12_381_bandersnatch_ext::EdwardsConfig<HostHooks>;
/// Twisted Edwards form point affine representation.
pub type EdwardsAffine = ark_ed_on_bls12_381_bandersnatch_ext::EdwardsAffine<HostHooks>;
/// Twisted Edwards form point projective representation.
pub type EdwardsProjective = ark_ed_on_bls12_381_bandersnatch_ext::EdwardsProjective<HostHooks>;
/// Group configuration for Short Weierstrass form (equal to [`BandersnatchConfig`]).
pub type SWConfig = ark_ed_on_bls12_381_bandersnatch_ext::SWConfig<HostHooks>;
/// Short Weierstrass form point affine representation.
pub type SWAffine = ark_ed_on_bls12_381_bandersnatch_ext::SWAffine<HostHooks>;
/// Short Weierstrass form point projective representation.
pub type SWProjective = ark_ed_on_bls12_381_bandersnatch_ext::SWProjective<HostHooks>;
impl CurveHooks for HostHooks {
fn ed_on_bls12_381_bandersnatch_te_msm(
bases: &[EdwardsAffine],
scalars: &[<EdwardsConfig as CurveConfig>::ScalarField],
) -> Result<EdwardsProjective, ()> {
let bases = utils::encode(bases);
let scalars = utils::encode(scalars);
let res =
host_calls::ed_on_bls12_381_bandersnatch_te_msm(bases, scalars).unwrap_or_default();
utils::decode_proj_te(res)
}
fn ed_on_bls12_381_bandersnatch_te_mul_projective(
base: &EdwardsProjective,
scalar: &[u64],
) -> Result<EdwardsProjective, ()> {
let base = utils::encode_proj_te(base);
let scalar = utils::encode(scalar);
let res = host_calls::ed_on_bls12_381_bandersnatch_te_mul_projective(base, scalar)
.unwrap_or_default();
utils::decode_proj_te(res)
}
fn ed_on_bls12_381_bandersnatch_sw_msm(
bases: &[SWAffine],
scalars: &[<SWConfig as CurveConfig>::ScalarField],
) -> Result<SWProjective, ()> {
let bases = utils::encode(bases);
let scalars = utils::encode(scalars);
let res =
host_calls::ed_on_bls12_381_bandersnatch_sw_msm(bases, scalars).unwrap_or_default();
utils::decode_proj_sw(res)
}
fn ed_on_bls12_381_bandersnatch_sw_mul_projective(
base: &SWProjective,
scalar: &[u64],
) -> Result<SWProjective, ()> {
let base = utils::encode_proj_sw(base);
let scalar = utils::encode(scalar);
let res = host_calls::ed_on_bls12_381_bandersnatch_sw_mul_projective(base, scalar)
.unwrap_or_default();
utils::decode_proj_sw(res)
}
}
/// Interfaces for working with *Arkworks* *Ed-on-BLS12-381-Bandersnatch* elliptic curve
/// related types from within the runtime.
///
/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
///
/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
/// and "not-compressed".
#[runtime_interface]
pub trait HostCalls {
/// Twisted Edwards multi scalar multiplication for *Ed-on-BLS12-381-Bandersnatch*.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<EdwardsProjective>`.
/// - `scalars`: `ArkScale<Vec<EdwardsConfig::ScalarField>>`.
/// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
fn ed_on_bls12_381_bandersnatch_te_msm(
bases: Vec<u8>,
scalars: Vec<u8>,
) -> Result<Vec<u8>, ()> {
utils::msm_te::<ark_ed_on_bls12_381_bandersnatch::EdwardsConfig>(bases, scalars)
}
/// Twisted Edwards projective multiplication for *Ed-on-BLS12-381-Bandersnatch*.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<EdwardsProjective>`.
/// - `scalar`: `ArkScale<Vec<u64>>`.
/// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
fn ed_on_bls12_381_bandersnatch_te_mul_projective(
base: Vec<u8>,
scalar: Vec<u8>,
) -> Result<Vec<u8>, ()> {
utils::mul_projective_te::<ark_ed_on_bls12_381_bandersnatch::EdwardsConfig>(base, scalar)
}
/// Short Weierstrass multi scalar multiplication for *Ed-on-BLS12-381-Bandersnatch*.
///
/// - Receives encoded:
/// - `bases`: `ArkScale<Vec<SWAffine>>`.
/// - `scalars`: `ArkScale<Vec<SWConfig::ScalarField>>`.
/// - Returns encoded: `ArkScaleProjective<SWProjective>`.
fn ed_on_bls12_381_bandersnatch_sw_msm(
bases: Vec<u8>,
scalars: Vec<u8>,
) -> Result<Vec<u8>, ()> {
utils::msm_sw::<ark_ed_on_bls12_381_bandersnatch::SWConfig>(bases, scalars)
}
/// Short Weierstrass projective multiplication for *Ed-on-BLS12-381-Bandersnatch*.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<SWProjective>`.
/// - `scalar`: `ArkScale<Vec<u64>>`.
/// - Returns encoded: `ArkScaleProjective<SWProjective>`.
fn ed_on_bls12_381_bandersnatch_sw_mul_projective(
base: Vec<u8>,
scalar: Vec<u8>,
) -> Result<Vec<u8>, ()> {
utils::mul_projective_sw::<ark_ed_on_bls12_381_bandersnatch::SWConfig>(base, scalar)
}
}
+31 -264
View File
@@ -15,272 +15,39 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//! Elliptic Curves host functions which may be used to handle some of the *Arkworks*
//! computationally expensive operations.
//! Elliptic curves which are mostly compatible with *Arkworks* library
//! mostly useful in non-native contexts.
//!
//! The definitions make use of host functions to offload the non-native
//! computational environment from the some of the most computationally
//! expensive operations by internally leveraging the
//! [arkworks-extensions](https://github.com/paritytech/arkworks-extensions)
//! library.
//!
//! The exported types are organized and named in a way that mirrors the structure
//! of the types in the original Arkworks library. This design choice aims to make
//! it easier for users already familiar with the library to understand and utilize
//! the exported types effectively.
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "bls12-377")]
pub mod bls12_377;
#[cfg(feature = "bls12-381")]
pub mod bls12_381;
#[cfg(feature = "bw6-761")]
pub mod bw6_761;
#[cfg(feature = "ed-on-bls12-377")]
pub mod ed_on_bls12_377;
#[cfg(feature = "ed-on-bls12-381-bandersnatch")]
pub mod ed_on_bls12_381_bandersnatch;
#[cfg(any(
feature = "bls12-377",
feature = "bls12-381",
feature = "bw6-761",
feature = "ed-on-bls12-377",
feature = "ed-on-bls12-381-bandersnatch",
))]
mod utils;
use sp_runtime_interface::runtime_interface;
use sp_std::vec::Vec;
use utils::*;
/// Interfaces for working with *Arkworks* elliptic curves related types from within the runtime.
///
/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
///
/// `ArkScale`'s `Usage` generic parameter is expected to be set to `HOST_CALL`, which is
/// a shortcut for "not-validated" and "not-compressed".
#[runtime_interface]
pub trait EllipticCurves {
/// Pairing multi Miller loop for BLS12-377.
///
/// - Receives encoded:
/// - `a: ArkScale<Vec<ark_ec::bls12::G1Prepared::<ark_bls12_377::Config>>>`.
/// - `b: ArkScale<Vec<ark_ec::bls12::G2Prepared::<ark_bls12_377::Config>>>`.
/// - Returns encoded: ArkScale<MillerLoopOutput<Bls12<ark_bls12_377::Config>>>.
fn bls12_377_multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, ()> {
multi_miller_loop::<ark_bls12_377::Bls12_377>(a, b)
}
/// Pairing final exponentiation for BLS12-377.
///
/// - Receives encoded: `ArkScale<MillerLoopOutput<Bls12<ark_bls12_377::Config>>>`.
/// - Returns encoded: `ArkScale<PairingOutput<Bls12<ark_bls12_377::Config>>>`.
fn bls12_377_final_exponentiation(f: Vec<u8>) -> Result<Vec<u8>, ()> {
final_exponentiation::<ark_bls12_377::Bls12_377>(f)
}
/// Projective multiplication on G1 for BLS12-377.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<ark_bls12_377::G1Projective>`.
/// - `scalar`: `ArkScale<&[u64]>`.
/// - Returns encoded: `ArkScaleProjective<ark_bls12_377::G1Projective>`.
fn bls12_377_mul_projective_g1(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_projective_sw::<ark_bls12_377::g1::Config>(base, scalar)
}
/// Projective multiplication on G2 for BLS12-377.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<ark_bls12_377::G2Projective>`.
/// - `scalar`: `ArkScale<&[u64]>`.
/// - Returns encoded: `ArkScaleProjective<ark_bls12_377::G2Projective>`.
fn bls12_377_mul_projective_g2(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_projective_sw::<ark_bls12_377::g2::Config>(base, scalar)
}
/// Multi scalar multiplication on G1 for BLS12-377.
///
/// - Receives encoded:
/// - `bases`: `ArkScale<&[ark_bls12_377::G1Affine]>`.
/// - `scalars`: `ArkScale<&[ark_bls12_377::Fr]>`.
/// - Returns encoded: `ArkScaleProjective<ark_bls12_377::G1Projective>`.
fn bls12_377_msm_g1(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
msm_sw::<ark_bls12_377::g1::Config>(bases, scalars)
}
/// Multi scalar multiplication on G2 for BLS12-377.
///
/// - Receives encoded:
/// - `bases`: `ArkScale<&[ark_bls12_377::G2Affine]>`.
/// - `scalars`: `ArkScale<&[ark_bls12_377::Fr]>`.
/// - Returns encoded: `ArkScaleProjective<ark_bls12_377::G2Projective>`.
fn bls12_377_msm_g2(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
msm_sw::<ark_bls12_377::g2::Config>(bases, scalars)
}
/// Pairing multi Miller loop for BLS12-381.
///
/// - Receives encoded:
/// - `a`: `ArkScale<Vec<ark_ec::bls12::G1Prepared::<ark_bls12_381::Config>>>`.
/// - `b`: `ArkScale<Vec<ark_ec::bls12::G2Prepared::<ark_bls12_381::Config>>>`.
/// - Returns encoded: ArkScale<MillerLoopOutput<Bls12<ark_bls12_381::Config>>>
fn bls12_381_multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, ()> {
multi_miller_loop::<ark_bls12_381::Bls12_381>(a, b)
}
/// Pairing final exponentiation for BLS12-381.
///
/// - Receives encoded: `ArkScale<MillerLoopOutput<Bls12<ark_bls12_381::Config>>>`.
/// - Returns encoded: `ArkScale<PairingOutput<Bls12<ark_bls12_381::Config>>>`.
fn bls12_381_final_exponentiation(f: Vec<u8>) -> Result<Vec<u8>, ()> {
final_exponentiation::<ark_bls12_381::Bls12_381>(f)
}
/// Projective multiplication on G1 for BLS12-381.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<ark_bls12_381::G1Projective>`.
/// - `scalar`: `ArkScale<&[u64]>`.
/// - Returns encoded: `ArkScaleProjective<ark_bls12_381::G1Projective>`.
fn bls12_381_mul_projective_g1(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_projective_sw::<ark_bls12_381::g1::Config>(base, scalar)
}
/// Projective multiplication on G2 for BLS12-381.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<ark_bls12_381::G2Projective>`.
/// - `scalar`: `ArkScale<&[u64]>`.
/// - Returns encoded: `ArkScaleProjective<ark_bls12_381::G2Projective>`.
fn bls12_381_mul_projective_g2(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_projective_sw::<ark_bls12_381::g2::Config>(base, scalar)
}
/// Multi scalar multiplication on G1 for BLS12-381.
///
/// - Receives encoded:
/// - bases: `ArkScale<&[ark_bls12_381::G1Affine]>`.
/// - scalars: `ArkScale<&[ark_bls12_381::Fr]>`.
/// - Returns encoded: `ArkScaleProjective<ark_bls12_381::G1Projective>`.
fn bls12_381_msm_g1(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
msm_sw::<ark_bls12_381::g1::Config>(bases, scalars)
}
/// Multi scalar multiplication on G2 for BLS12-381.
///
/// - Receives encoded:
/// - `bases`: `ArkScale<&[ark_bls12_381::G2Affine]>`.
/// - `scalars`: `ArkScale<&[ark_bls12_381::Fr]>`.
/// - Returns encoded: `ArkScaleProjective<ark_bls12_381::G2Projective>`.
fn bls12_381_msm_g2(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
msm_sw::<ark_bls12_381::g2::Config>(bases, scalars)
}
/// Pairing multi Miller loop for BW6-761.
///
/// - Receives encoded:
/// - `a`: `ArkScale<Vec<ark_ec::bw6::G1Prepared::<ark_bw6_761::Config>>>`.
/// - `b`: `ArkScale<Vec<ark_ec::bw6::G2Prepared::<ark_bw6_761::Config>>>`.
/// - Returns encoded: `ArkScale<MillerLoopOutput<Bls12<ark_bw6_761::Config>>>`.
fn bw6_761_multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, ()> {
multi_miller_loop::<ark_bw6_761::BW6_761>(a, b)
}
/// Pairing final exponentiation for BW6-761.
///
/// - Receives encoded: `ArkScale<MillerLoopOutput<BW6<ark_bw6_761::Config>>>`.
/// - Returns encoded: `ArkScale<PairingOutput<BW6<ark_bw6_761::Config>>>`.
fn bw6_761_final_exponentiation(f: Vec<u8>) -> Result<Vec<u8>, ()> {
final_exponentiation::<ark_bw6_761::BW6_761>(f)
}
/// Projective multiplication on G1 for BW6-761.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<ark_bw6_761::G1Projective>`.
/// - `scalar`: `ArkScale<&[u64]>`.
/// - Returns encoded: `ArkScaleProjective<ark_bw6_761::G1Projective>`.
fn bw6_761_mul_projective_g1(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_projective_sw::<ark_bw6_761::g1::Config>(base, scalar)
}
/// Projective multiplication on G2 for BW6-761.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<ark_bw6_761::G2Projective>`.
/// - `scalar`: `ArkScale<&[u64]>`.
/// - Returns encoded: `ArkScaleProjective<ark_bw6_761::G2Projective>`.
fn bw6_761_mul_projective_g2(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_projective_sw::<ark_bw6_761::g2::Config>(base, scalar)
}
/// Multi scalar multiplication on G1 for BW6-761.
///
/// - Receives encoded:
/// - `bases`: `ArkScale<&[ark_bw6_761::G1Affine]>`.
/// - `scalars`: `ArkScale<&[ark_bw6_761::Fr]>`.
/// - Returns encoded: `ArkScaleProjective<ark_bw6_761::G1Projective>`.
fn bw6_761_msm_g1(bases: Vec<u8>, bigints: Vec<u8>) -> Result<Vec<u8>, ()> {
msm_sw::<ark_bw6_761::g1::Config>(bases, bigints)
}
/// Multi scalar multiplication on G2 for BW6-761.
///
/// - Receives encoded:
/// - `bases`: `ArkScale<&[ark_bw6_761::G2Affine]>`.
/// - `scalars`: `ArkScale<&[ark_bw6_761::Fr]>`.
/// - Returns encoded: `ArkScaleProjective<ark_bw6_761::G2Projective>`.
fn bw6_761_msm_g2(bases: Vec<u8>, bigints: Vec<u8>) -> Result<Vec<u8>, ()> {
msm_sw::<ark_bw6_761::g2::Config>(bases, bigints)
}
/// Twisted Edwards projective multiplication for Ed-on-BLS12-377.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<ark_ed_on_bls12_377::EdwardsProjective>`.
/// - `scalar`: `ArkScale<&[u64]>`.
/// - Returns encoded: `ArkScaleProjective<ark_ed_on_bls12_377::EdwardsProjective>`.
fn ed_on_bls12_377_mul_projective(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_projective_te::<ark_ed_on_bls12_377::EdwardsConfig>(base, scalar)
}
/// Twisted Edwards multi scalar multiplication for Ed-on-BLS12-377.
///
/// - Receives encoded:
/// - `bases`: `ArkScale<&[ark_ed_on_bls12_377::EdwardsAffine]>`.
/// - `scalars`: `ArkScale<&[ark_ed_on_bls12_377::Fr]>`.
/// - Returns encoded: `ArkScaleProjective<ark_ed_on_bls12_377::EdwardsProjective>`.
fn ed_on_bls12_377_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
msm_te::<ark_ed_on_bls12_377::EdwardsConfig>(bases, scalars)
}
/// Short Weierstrass projective multiplication for Ed-on-BLS12-381-Bandersnatch.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<ark_ed_on_bls12_381_bandersnatch::SWProjective>`.
/// - `scalar`: `ArkScale<&[u64]>`.
/// - Returns encoded: `ArkScaleProjective<ark_ed_on_bls12_381_bandersnatch::SWProjective>`.
fn ed_on_bls12_381_bandersnatch_sw_mul_projective(
base: Vec<u8>,
scalar: Vec<u8>,
) -> Result<Vec<u8>, ()> {
mul_projective_sw::<ark_ed_on_bls12_381_bandersnatch::SWConfig>(base, scalar)
}
/// Twisted Edwards projective multiplication for Ed-on-BLS12-381-Bandersnatch.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<ark_ed_on_bls12_381_bandersnatch::EdwardsProjective>`.
/// - `scalar`: `ArkScale<&[u64]>`.
/// - Returns encoded:
/// `ArkScaleProjective<ark_ed_on_bls12_381_bandersnatch::EdwardsProjective>`.
fn ed_on_bls12_381_bandersnatch_te_mul_projective(
base: Vec<u8>,
scalar: Vec<u8>,
) -> Result<Vec<u8>, ()> {
mul_projective_te::<ark_ed_on_bls12_381_bandersnatch::EdwardsConfig>(base, scalar)
}
/// Short Weierstrass multi scalar multiplication for Ed-on-BLS12-381-Bandersnatch.
///
/// - Receives encoded:
/// - `bases`: `ArkScale<&[ark_ed_on_bls12_381_bandersnatch::SWAffine]>`.
/// - `scalars`: `ArkScale<&[ark_ed_on_bls12_381_bandersnatch::Fr]>`.
/// - Returns encoded: `ArkScaleProjective<ark_ed_on_bls12_381_bandersnatch::SWProjective>`.
fn ed_on_bls12_381_bandersnatch_sw_msm(
bases: Vec<u8>,
scalars: Vec<u8>,
) -> Result<Vec<u8>, ()> {
msm_sw::<ark_ed_on_bls12_381_bandersnatch::SWConfig>(bases, scalars)
}
/// Twisted Edwards multi scalar multiplication for Ed-on-BLS12-381-Bandersnatch.
///
/// - Receives encoded:
/// - `base`: `ArkScaleProjective<ark_ed_on_bls12_381_bandersnatch::EdwardsProjective>`.
/// - `scalars`: `ArkScale<&[ark_ed_on_bls12_381_bandersnatch::Fr]>`.
/// - Returns encoded:
/// `ArkScaleProjective<ark_ed_on_bls12_381_bandersnatch::EdwardsProjective>`.
fn ed_on_bls12_381_bandersnatch_te_msm(
bases: Vec<u8>,
scalars: Vec<u8>,
) -> Result<Vec<u8>, ()> {
msm_te::<ark_ed_on_bls12_381_bandersnatch::EdwardsConfig>(bases, scalars)
}
}
@@ -17,109 +17,100 @@
//! Generic executions of the operations for *Arkworks* elliptic curves.
// As not all functions are used by each elliptic curve and some elliptic
// curve may be excluded by the build we resort to `#[allow(unused)]` to
// suppress the expected warning.
use ark_ec::{
pairing::{MillerLoopOutput, Pairing, PairingOutput},
short_weierstrass,
short_weierstrass::SWCurveConfig,
twisted_edwards,
twisted_edwards::TECurveConfig,
pairing::{MillerLoopOutput, Pairing},
short_weierstrass::{Affine as SWAffine, Projective as SWProjective, SWCurveConfig},
twisted_edwards::{Affine as TEAffine, Projective as TEProjective, TECurveConfig},
CurveConfig, VariableBaseMSM,
};
use ark_scale::{
hazmat::ArkScaleProjective,
ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Compress, Validate},
scale::{Decode, Encode},
};
use sp_std::vec::Vec;
// Scale codec type which is expected to be used by the host functions.
//
// Encoding is set to `HOST_CALL` which is a shortcut for "not-validated" and "not-compressed".
type ArkScale<T> = ark_scale::ArkScale<T, { ark_scale::HOST_CALL }>;
// SCALE encoding parameters shared by all the enabled modules
const SCALE_USAGE: u8 = ark_scale::make_usage(Compress::No, Validate::No);
type ArkScale<T> = ark_scale::ArkScale<T, SCALE_USAGE>;
type ArkScaleProjective<T> = ark_scale::hazmat::ArkScaleProjective<T>;
pub fn multi_miller_loop<Curve: Pairing>(g1: Vec<u8>, g2: Vec<u8>) -> Result<Vec<u8>, ()> {
let g1 = <ArkScale<Vec<<Curve as Pairing>::G1Affine>> as Decode>::decode(&mut g1.as_slice())
.map_err(|_| ())?;
let g2 = <ArkScale<Vec<<Curve as Pairing>::G2Affine>> as Decode>::decode(&mut g2.as_slice())
.map_err(|_| ())?;
let result = Curve::multi_miller_loop(g1.0, g2.0).0;
let result: ArkScale<<Curve as Pairing>::TargetField> = result.into();
Ok(result.encode())
#[inline(always)]
pub fn encode<T: CanonicalSerialize>(val: T) -> Vec<u8> {
ArkScale::from(val).encode()
}
pub fn final_exponentiation<Curve: Pairing>(target: Vec<u8>) -> Result<Vec<u8>, ()> {
let target =
<ArkScale<<Curve as Pairing>::TargetField> as Decode>::decode(&mut target.as_slice())
.map_err(|_| ())?;
let result = Curve::final_exponentiation(MillerLoopOutput(target.0)).ok_or(())?;
let result: ArkScale<PairingOutput<Curve>> = result.into();
Ok(result.encode())
#[inline(always)]
pub fn decode<T: CanonicalDeserialize>(buf: Vec<u8>) -> Result<T, ()> {
ArkScale::<T>::decode(&mut &buf[..]).map_err(|_| ()).map(|v| v.0)
}
pub fn msm_sw<Curve: SWCurveConfig>(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
let bases =
<ArkScale<Vec<short_weierstrass::Affine<Curve>>> as Decode>::decode(&mut bases.as_slice())
.map_err(|_| ())?;
let scalars = <ArkScale<Vec<<Curve as CurveConfig>::ScalarField>> as Decode>::decode(
&mut scalars.as_slice(),
)
.map_err(|_| ())?;
let result =
<short_weierstrass::Projective<Curve> as VariableBaseMSM>::msm(&bases.0, &scalars.0)
.map_err(|_| ())?;
let result: ArkScaleProjective<short_weierstrass::Projective<Curve>> = result.into();
Ok(result.encode())
#[inline(always)]
pub fn encode_proj_sw<T: SWCurveConfig>(val: &SWProjective<T>) -> Vec<u8> {
ArkScaleProjective::from(val).encode()
}
pub fn msm_te<Curve: TECurveConfig>(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
let bases =
<ArkScale<Vec<twisted_edwards::Affine<Curve>>> as Decode>::decode(&mut bases.as_slice())
.map_err(|_| ())?;
let scalars = <ArkScale<Vec<<Curve as CurveConfig>::ScalarField>> as Decode>::decode(
&mut scalars.as_slice(),
)
.map_err(|_| ())?;
let result = <twisted_edwards::Projective<Curve> as VariableBaseMSM>::msm(&bases.0, &scalars.0)
.map_err(|_| ())?;
let result: ArkScaleProjective<twisted_edwards::Projective<Curve>> = result.into();
Ok(result.encode())
#[inline(always)]
pub fn decode_proj_sw<T: SWCurveConfig>(buf: Vec<u8>) -> Result<SWProjective<T>, ()> {
ArkScaleProjective::decode(&mut &buf[..]).map_err(|_| ()).map(|v| v.0)
}
pub fn mul_projective_sw<Group: SWCurveConfig>(
base: Vec<u8>,
scalar: Vec<u8>,
) -> Result<Vec<u8>, ()> {
let base = <ArkScaleProjective<short_weierstrass::Projective<Group>> as Decode>::decode(
&mut base.as_slice(),
)
.map_err(|_| ())?;
let scalar = <ArkScale<Vec<u64>> as Decode>::decode(&mut scalar.as_slice()).map_err(|_| ())?;
let result = <Group as SWCurveConfig>::mul_projective(&base.0, &scalar.0);
let result: ArkScaleProjective<short_weierstrass::Projective<Group>> = result.into();
Ok(result.encode())
#[inline(always)]
pub fn encode_proj_te<T: TECurveConfig>(val: &TEProjective<T>) -> Vec<u8> {
ArkScaleProjective::from(val).encode()
}
pub fn mul_projective_te<Group: TECurveConfig>(
base: Vec<u8>,
scalar: Vec<u8>,
) -> Result<Vec<u8>, ()> {
let base = <ArkScaleProjective<twisted_edwards::Projective<Group>> as Decode>::decode(
&mut base.as_slice(),
)
.map_err(|_| ())?;
let scalar = <ArkScale<Vec<u64>> as Decode>::decode(&mut scalar.as_slice()).map_err(|_| ())?;
let result = <Group as TECurveConfig>::mul_projective(&base.0, &scalar.0);
let result: ArkScaleProjective<twisted_edwards::Projective<Group>> = result.into();
Ok(result.encode())
#[inline(always)]
pub fn decode_proj_te<T: TECurveConfig>(buf: Vec<u8>) -> Result<TEProjective<T>, ()> {
ArkScaleProjective::decode(&mut &buf[..]).map_err(|_| ()).map(|v| v.0)
}
#[allow(unused)]
pub fn multi_miller_loop<T: Pairing>(g1: Vec<u8>, g2: Vec<u8>) -> Result<Vec<u8>, ()> {
let g1 = decode::<Vec<<T as Pairing>::G1Affine>>(g1)?;
let g2 = decode::<Vec<<T as Pairing>::G2Affine>>(g2)?;
let res = T::multi_miller_loop(g1, g2);
Ok(encode(res.0))
}
#[allow(unused)]
pub fn final_exponentiation<T: Pairing>(target: Vec<u8>) -> Result<Vec<u8>, ()> {
let target = decode::<<T as Pairing>::TargetField>(target)?;
let res = T::final_exponentiation(MillerLoopOutput(target)).ok_or(())?;
Ok(encode(res.0))
}
#[allow(unused)]
pub fn msm_sw<T: SWCurveConfig>(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
let bases = decode::<Vec<SWAffine<T>>>(bases)?;
let scalars = decode::<Vec<<T as CurveConfig>::ScalarField>>(scalars)?;
let res = <SWProjective<T> as VariableBaseMSM>::msm(&bases, &scalars).map_err(|_| ())?;
Ok(encode_proj_sw(&res))
}
#[allow(unused)]
pub fn msm_te<T: TECurveConfig>(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
let bases = decode::<Vec<TEAffine<T>>>(bases)?;
let scalars = decode::<Vec<<T as CurveConfig>::ScalarField>>(scalars)?;
let res = <TEProjective<T> as VariableBaseMSM>::msm(&bases, &scalars).map_err(|_| ())?;
Ok(encode_proj_te(&res))
}
#[allow(unused)]
pub fn mul_projective_sw<T: SWCurveConfig>(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
let base = decode_proj_sw::<T>(base)?;
let scalar = decode::<Vec<u64>>(scalar)?;
let res = <T as SWCurveConfig>::mul_projective(&base, &scalar);
Ok(encode_proj_sw(&res))
}
#[allow(unused)]
pub fn mul_projective_te<T: TECurveConfig>(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
let base = decode_proj_te::<T>(base)?;
let scalar = decode::<Vec<u64>>(scalar)?;
let res = <T as TECurveConfig>::mul_projective(&base, &scalar);
Ok(encode_proj_te(&res))
}