Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
gshep committed Feb 26, 2024
1 parent e4b3ebf commit 71bc7c6
Show file tree
Hide file tree
Showing 12 changed files with 815 additions and 785 deletions.
95 changes: 49 additions & 46 deletions primitives/crypto/ec-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,61 +1,64 @@
[package]
name = "sp-crypto-ec-utils"
version = "0.4.0"
version = "0.4.1"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
description = "Host functions for common Arkworks elliptic curve operations"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
ark-serialize = { version = "0.4.2", default-features = false }
ark-ff = { version = "0.4.2", default-features = false }
ark-ec = { version = "0.4.2", default-features = false }
ark-std = { version = "0.4.0", default-features = false }
ark-bls12-377 = { version = "0.4.0", features = ["curve"], default-features = false }
ark-bls12-381 = { version = "0.4.0", features = ["curve"], default-features = false }
ark-bw6-761 = { version = "0.4.0", default-features = false }
ark-ed-on-bls12-381-bandersnatch = { version = "0.4.0", default-features = false }
ark-ed-on-bls12-377 = { version = "0.4.0", default-features = false }
sp-std = { version = "8.0.0", path = "../../std", default-features = false }
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false }
ark-scale = { version = "0.0.3", features = ["hazmat"], default-features = false }
sp-runtime-interface = { version = "17.0.0", default-features = false, path = "../../runtime-interface" }

[dev-dependencies]
sp-io = { path = "../../io", default-features = false }
ark-algebra-test-templates = { version = "0.4.2", default-features = false }
sp-ark-models = { version = "0.4.0-beta", default-features = false }
sp-ark-bls12-377 = { version = "0.4.0-beta", default-features = false }
sp-ark-bls12-381 = { version = "0.4.0-beta", default-features = false }
sp-ark-bw6-761 = { version = "0.4.0-beta", default-features = false }
sp-ark-ed-on-bls12-377 = { version = "0.4.0-beta", default-features = false }
sp-ark-ed-on-bls12-381-bandersnatch = { version = "0.4.0-beta", default-features = false }
ark-ec = { version = "0.4.2", default-features = false, optional = true }
ark-bls12-377-ext = { version = "0.4.1", default-features = false, optional = true }
ark-bls12-377 = { version = "0.4.0", default-features = false, features = ["curve"], optional = true }
ark-bls12-381-ext = { version = "0.4.1", default-features = false, optional = true }
ark-bls12-381 = { version = "0.4.0", default-features = false, features = ["curve"], optional = true }
ark-bw6-761-ext = { version = "0.4.1", default-features = false, optional = true }
ark-bw6-761 = { version = "0.4.0", default-features = false, optional = true }
ark-ed-on-bls12-381-bandersnatch-ext = { version = "0.4.1", default-features = false, optional = true }
ark-ed-on-bls12-381-bandersnatch = { version = "0.4.0", default-features = false, optional = true }
ark-ed-on-bls12-377-ext = { version = "0.4.1", default-features = false, optional = true }
ark-ed-on-bls12-377 = { version = "0.4.0", default-features = false, optional = true }
ark-scale = { version = "0.0.12", default-features = false, features = ["hazmat"], optional = true }
sp-runtime-interface = { path = "../../runtime-interface", default-features = false, optional = true }
sp-std = { path = "../../std", default-features = false, optional = true }

[features]
default = [ "std" ]
default = ["std"]
std = [
"ark-serialize/std",
"ark-ff/std",
"ark-ec/std",
"ark-std/std",
"ark-bls12-377/std",
"ark-bls12-381/std",
"ark-bw6-761/std",
"ark-ed-on-bls12-381-bandersnatch/std",
"ark-ed-on-bls12-377/std",
"sp-std/std",
"codec/std",
"ark-scale/std",
"sp-runtime-interface/std",
"sp-io/std",
"ark-algebra-test-templates/std",
"sp-ark-bls12-377/std",
"sp-ark-bls12-381/std",
"sp-ark-bw6-761/std",
"sp-ark-ed-on-bls12-377/std",
"sp-ark-ed-on-bls12-381-bandersnatch/std",
"ark-bls12-377-ext?/std",
"ark-bls12-377?/std",
"ark-bls12-381-ext?/std",
"ark-bls12-381?/std",
"ark-bw6-761-ext?/std",
"ark-bw6-761?/std",
"ark-ec?/parallel",
"ark-ec?/std",
"ark-ed-on-bls12-377-ext?/std",
"ark-ed-on-bls12-377?/std",
"ark-ed-on-bls12-381-bandersnatch-ext?/std",
"ark-ed-on-bls12-381-bandersnatch?/std",
"ark-scale?/std",
"sp-runtime-interface?/std",
"sp-std?/std",
]
common = ["ark-ec", "ark-scale", "sp-runtime-interface", "sp-std"]
bls12-377 = ["ark-bls12-377", "ark-bls12-377-ext", "common"]
bls12-381 = ["ark-bls12-381", "ark-bls12-381-ext", "common"]
bw6-761 = ["ark-bw6-761", "ark-bw6-761-ext", "common"]
ed-on-bls12-377 = ["ark-ed-on-bls12-377", "ark-ed-on-bls12-377-ext", "common"]
ed-on-bls12-381-bandersnatch = [
"ark-ed-on-bls12-381-bandersnatch",
"ark-ed-on-bls12-381-bandersnatch-ext",
"common",
]
all-curves = [
"bls12-377",
"bls12-381",
"bw6-761",
"ed-on-bls12-377",
"ed-on-bls12-381-bandersnatch",
]
242 changes: 172 additions & 70 deletions primitives/crypto/ec-utils/src/bls12_377.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Substrate.

// Copyright (C) 2017-2022 Parity Technologies (UK) Ltd.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,89 +15,191 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Support functions for bls12_377 to improve the performance of
//! multi_miller_loop, final_exponentiation, msm's and projective
//! multiplications by host function calls
//! *BLS12-377* types and host functions.
use crate::utils::{
final_exponentiation_generic, msm_sw_generic, mul_projective_generic, multi_miller_loop_generic,
};
use ark_bls12_377::{g1, g2, Bls12_377};
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;

/// Compute a multi miller loop through arkworks
pub fn multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, ()> {
multi_miller_loop_generic::<Bls12_377>(a, b)
/// 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>;
}

/// Compute a final exponentiation through arkworks
pub fn final_exponentiation(target: Vec<u8>) -> Result<Vec<u8>, ()> {
final_exponentiation_generic::<Bls12_377>(target)
/// 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>;
}

/// Compute a multi scalar multiplication for short_weierstrass through
/// arkworks on G1.
pub fn msm_g1(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
msm_sw_generic::<g1::Config>(bases, scalars)
}
pub use self::{
g1::{Config as G1Config, G1Affine, G1Projective},
g2::{Config as G2Config, G2Affine, G2Projective},
};

/// Compute a multi scalar multiplication for short_weierstrass through
/// arkworks on G2.
pub fn msm_g2(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
msm_sw_generic::<g2::Config>(bases, scalars)
}
/// Curve hooks jumping into [`host_calls`] host functions.
#[derive(Copy, Clone)]
pub struct HostHooks;

/// Compute a projective scalar multiplication for short_weierstrass
/// through arkworks on G1.
pub fn mul_projective_g1(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_projective_generic::<g1::Config>(base, scalar)
}
/// 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)
}

/// Compute a projective scalar multiplication for short_weierstrass
/// through arkworks on G2.
pub fn mul_projective_g2(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_projective_generic::<g2::Config>(base, scalar)
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)
}
}

#[cfg(test)]
mod tests {
use super::*;
use ark_algebra_test_templates::*;
use sp_ark_bls12_377::{
Bls12_377 as Bls12_377Host, G1Projective as G1ProjectiveHost,
G2Projective as G2ProjectiveHost, HostFunctions,
};
/// 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)
}

#[derive(PartialEq, Eq)]
struct Host;

impl HostFunctions for Host {
fn bls12_377_multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, ()> {
crate::elliptic_curves::bls12_377_multi_miller_loop(a, b)
}
fn bls12_377_final_exponentiation(f12: Vec<u8>) -> Result<Vec<u8>, ()> {
crate::elliptic_curves::bls12_377_final_exponentiation(f12)
}
fn bls12_377_msm_g1(bases: Vec<u8>, bigints: Vec<u8>) -> Result<Vec<u8>, ()> {
crate::elliptic_curves::bls12_377_msm_g1(bases, bigints)
}
fn bls12_377_msm_g2(bases: Vec<u8>, bigints: Vec<u8>) -> Result<Vec<u8>, ()> {
crate::elliptic_curves::bls12_377_msm_g2(bases, bigints)
}
fn bls12_377_mul_projective_g1(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
crate::elliptic_curves::bls12_377_mul_projective_g1(base, scalar)
}
fn bls12_377_mul_projective_g2(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
crate::elliptic_curves::bls12_377_mul_projective_g2(base, scalar)
}
/// 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)
}

type Bls12_377 = Bls12_377Host<Host>;
type G1Projective = G1ProjectiveHost<Host>;
type G2Projective = G2ProjectiveHost<Host>;
/// 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)
}

test_group!(g1; G1Projective; sw);
test_group!(g2; G2Projective; sw);
test_group!(pairing_output; ark_ec::pairing::PairingOutput<Bls12_377>; msm);
test_pairing!(pairing; super::Bls12_377);
/// 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)
}
}
Loading

0 comments on commit 71bc7c6

Please sign in to comment.