Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
replace Vec<Vec<u8>>
Browse files Browse the repository at this point in the history
  • Loading branch information
Achim Schneider committed Mar 18, 2023
1 parent ab35a7a commit 4389714
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 53 deletions.
6 changes: 3 additions & 3 deletions primitives/arkworks/src/bls12_377.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use sp_std::vec::Vec;

/// Compute multi miller loop through arkworks

pub fn multi_miller_loop(a: Vec<Vec<u8>>, b: Vec<Vec<u8>>) -> Result<Vec<u8>, PairingError> {
pub fn multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, PairingError> {
multi_miller_loop_generic::<Bls12_377>(a, b)
}

Expand All @@ -40,11 +40,11 @@ pub fn final_exponentiation(target: Vec<u8>) -> Result<Vec<u8>, PairingError> {
}

/// Compute a multi scalar multiplication on G! through arkworks
pub fn msm_g1(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
pub fn msm_g1(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
msm_g1_generic::<Bls12_377>(bases, scalars)
}

/// Compute a multi scalar multiplication on G! through arkworks
pub fn msm_g2(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
pub fn msm_g2(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
msm_g2_generic::<Bls12_377>(bases, scalars)
}
6 changes: 3 additions & 3 deletions primitives/arkworks/src/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use ark_bls12_381::Bls12_381;
use sp_std::vec::Vec;

/// Compute multi miller loop through arkworks
pub fn multi_miller_loop(a: Vec<Vec<u8>>, b: Vec<Vec<u8>>) -> Result<Vec<u8>, PairingError> {
pub fn multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, PairingError> {
multi_miller_loop_generic::<Bls12_381>(a, b)
}

Expand All @@ -39,11 +39,11 @@ pub fn final_exponentiation(target: Vec<u8>) -> Result<Vec<u8>, PairingError> {
}

/// Compute a multi scalar multiplication on G! through arkworks
pub fn msm_g1(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
pub fn msm_g1(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
msm_g1_generic::<Bls12_381>(bases, scalars)
}

/// Compute a multi scalar multiplication on G! through arkworks
pub fn msm_g2(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
pub fn msm_g2(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
msm_g2_generic::<Bls12_381>(bases, scalars)
}
6 changes: 3 additions & 3 deletions primitives/arkworks/src/bw6_761.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use ark_bw6_761::BW6_761;
use sp_std::vec::Vec;

/// Compute multi miller loop through arkworks
pub fn multi_miller_loop(a: Vec<Vec<u8>>, b: Vec<Vec<u8>>) -> Result<Vec<u8>, PairingError> {
pub fn multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, PairingError> {
multi_miller_loop_generic::<BW6_761>(a, b)
}

Expand All @@ -39,11 +39,11 @@ pub fn final_exponentiation(target: Vec<u8>) -> Result<Vec<u8>, PairingError> {
}

/// Compute a multi scalar multiplication on G! through arkworks
pub fn msm_g1(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
pub fn msm_g1(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
msm_g1_generic::<BW6_761>(bases, scalars)
}

/// Compute a multi scalar multiplication on G! through arkworks
pub fn msm_g2(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
pub fn msm_g2(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
msm_g2_generic::<BW6_761>(bases, scalars)
}
2 changes: 1 addition & 1 deletion primitives/arkworks/src/ed_on_bls12_377.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use ark_ed_on_bls12_377::{EdwardsConfig, EdwardsProjective};
use sp_std::vec::Vec;

/// Compute a multi scalar multiplication on G! through arkworks
pub fn msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
pub fn msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
let bases: Vec<_> = bases
.iter()
.map(|a| deserialize_argument::<twisted_edwards::Affine<EdwardsConfig>>(a))
Expand Down
4 changes: 2 additions & 2 deletions primitives/arkworks/src/ed_on_bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use ark_ed_on_bls12_381::{EdwardsProjective, JubjubConfig, SWProjective};
use sp_std::vec::Vec;

/// Compute a multi scalar multiplication on G! through arkworks
pub fn te_msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
pub fn te_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
let bases: Vec<_> = bases
.iter()
.map(|a| deserialize_argument::<TEAffine<JubjubConfig>>(a))
Expand All @@ -44,7 +44,7 @@ pub fn te_msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
}

/// Compute a multi scalar multiplication on G! through arkworks
pub fn sw_msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
pub fn sw_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
let bases: Vec<_> = bases
.iter()
.map(|a| deserialize_argument::<SWAffine<JubjubConfig>>(a))
Expand Down
12 changes: 8 additions & 4 deletions primitives/arkworks/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ pub fn deserialize_argument<Field: CanonicalDeserialize>(argument: &Vec<u8>) ->
}

pub fn multi_miller_loop_generic<Curve: Pairing>(
a_vec: Vec<Vec<u8>>,
b_vec: Vec<Vec<u8>>,
a_vec: Vec<u8>,
b_vec: Vec<u8>,
) -> Result<Vec<u8>, PairingError> {
let g1: Vec<_> = a_vec
.chunks(<Curve as Pairing>::G1Affine.serialized_size(Compress::No))
.iter()
.map(|elem| deserialize_argument::<<Curve as Pairing>::G1Affine>(elem))
.collect();
let g2: Vec<_> = b_vec
.chunks(<Curve as Pairing>::G2Affine.serialized_size(Compress::No))
.iter()
.map(|elem| deserialize_argument::<<Curve as Pairing>::G2Affine>(elem))
.collect();
Expand All @@ -46,8 +48,9 @@ pub fn final_exponentiation_generic<Curve: Pairing>(
}
}

pub fn msm_g1_generic<Curve: Pairing>(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
pub fn msm_g1_generic<Curve: Pairing>(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
let bases: Vec<_> = bases
.chunks(<Curve as Pairing>::G1Affine.serialized_size(Compress::No))
.iter()
.map(|a| deserialize_argument::<<Curve as Pairing>::G1Affine>(a))
.collect();
Expand All @@ -60,8 +63,9 @@ pub fn msm_g1_generic<Curve: Pairing>(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>
serialize_result(result)
}

pub fn msm_g2_generic<Curve: Pairing>(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
pub fn msm_g2_generic<Curve: Pairing>(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
let bases: Vec<_> = bases
.chunks(<Curve as Pairing>::G2Affine.serialized_size(Compress::No))
.iter()
.map(|a| deserialize_argument::<<Curve as Pairing>::G2Affine>(a))
.collect();
Expand Down
9 changes: 3 additions & 6 deletions primitives/arkworks/test/tests/bls12_377.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ use sp_arkworks::PairingError;
pub struct Host {}

impl HostFunctions for Host {
fn bls12_377_multi_miller_loop(
a: Vec<Vec<u8>>,
b: Vec<Vec<u8>>,
) -> Result<Vec<u8>, PairingError> {
fn bls12_377_multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, PairingError> {
sp_io::elliptic_curves::bls12_377_multi_miller_loop(a, b)
}
fn bls12_377_final_exponentiation(f12: Vec<u8>) -> Result<Vec<u8>, PairingError> {
sp_io::elliptic_curves::bls12_377_final_exponentiation(f12)
}
fn bls12_377_msm_g1(bases: Vec<Vec<u8>>, bigints: Vec<Vec<u8>>) -> Vec<u8> {
fn bls12_377_msm_g1(bases: Vec<u8>, bigints: Vec<u8>) -> Vec<u8> {
sp_io::elliptic_curves::bls12_377_msm_g1(bases, bigints)
}
fn bls12_377_msm_g2(bases: Vec<Vec<u8>>, bigints: Vec<Vec<u8>>) -> Vec<u8> {
fn bls12_377_msm_g2(bases: Vec<u8>, bigints: Vec<u8>) -> Vec<u8> {
sp_io::elliptic_curves::bls12_377_msm_g2(bases, bigints)
}
}
Expand Down
6 changes: 3 additions & 3 deletions primitives/arkworks/test/tests/bls12_381/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ use sp_ark_bls12_381::{
pub struct Host {}

impl HostFunctions for Host {
fn bls12_381_multi_miller_loop(a: Vec<Vec<u8>>, b: Vec<Vec<u8>> ) -> Result<Vec<u8>, PairingError> {
fn bls12_381_multi_miller_loop(a: Vec<u8>, b: Vec<u8> ) -> Result<Vec<u8>, PairingError> {
sp_io::elliptic_curves::bls12_381_multi_miller_loop(a, b)
}
fn bls12_381_final_exponentiation(f12: Vec<u8>) -> Result<Vec<u8>, PairingError> {
sp_io::elliptic_curves::bls12_381_final_exponentiation(f12)
}
fn bls12_381_msm_g1(bases: Vec<Vec<u8>>, bigints: Vec<Vec<u8>>) -> Vec<u8> {
fn bls12_381_msm_g1(bases: Vec<u8>, bigints: Vec<u8>) -> Vec<u8> {
sp_io::elliptic_curves::bls12_381_msm_g1(bases, bigints)
}
fn bls12_381_msm_g2(bases: Vec<Vec<u8>>, bigints: Vec<Vec<u8>>) -> Vec<u8> {
fn bls12_381_msm_g2(bases: Vec<u8>, bigints: Vec<u8>) -> Vec<u8> {
sp_io::elliptic_curves::bls12_381_msm_g2(bases, bigints)
}
}
Expand Down
9 changes: 3 additions & 6 deletions primitives/arkworks/test/tests/bw6_761.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ use sp_arkworks::PairingError;
pub struct Host;

impl HostFunctions for Host {
fn bw6_761_multi_miller_loop(
a: Vec<Vec<u8>>,
b: Vec<Vec<u8>>,
) -> Result<Vec<u8>, PairingError> {
fn bw6_761_multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, PairingError> {
sp_io::elliptic_curves::bw6_761_multi_miller_loop(a, b)
}
fn bw6_761_final_exponentiation(f12: Vec<u8>) -> Result<Vec<u8>, PairingError> {
sp_io::elliptic_curves::bw6_761_final_exponentiation(f12)
}
fn bw6_761_msm_g1(bases: Vec<Vec<u8>>, bigints: Vec<Vec<u8>>) -> Vec<u8> {
fn bw6_761_msm_g1(bases: Vec<u8>, bigints: Vec<u8>) -> Vec<u8> {
sp_io::elliptic_curves::bw6_761_msm_g1(bases, bigints)
}
fn bw6_761_msm_g2(bases: Vec<Vec<u8>>, bigints: Vec<Vec<u8>>) -> Vec<u8> {
fn bw6_761_msm_g2(bases: Vec<u8>, bigints: Vec<u8>) -> Vec<u8> {
sp_io::elliptic_curves::bw6_761_msm_g2(bases, bigints)
}
}
Expand Down
2 changes: 1 addition & 1 deletion primitives/arkworks/test/tests/ed_on_bls12_377.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sp_ark_ed_on_bls12_377::HostFunctions;
pub struct Host {}

impl HostFunctions for Host {
fn ed_on_bls12_377_msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
fn ed_on_bls12_377_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
sp_io::elliptic_curves::ed_on_bls12_377_msm(bases, scalars)
}
}
Expand Down
4 changes: 2 additions & 2 deletions primitives/arkworks/test/tests/ed_on_bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use sp_ark_ed_on_bls12_381::HostFunctions;
pub struct Host {}

impl HostFunctions for Host {
fn ed_on_bls12_381_te_msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
fn ed_on_bls12_381_te_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
sp_io::elliptic_curves::ed_on_bls12_381_te_msm(bases, scalars)
}
fn ed_on_bls12_381_sw_msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
fn ed_on_bls12_381_sw_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
sp_io::elliptic_curves::ed_on_bls12_381_sw_msm(bases, scalars)
}
}
Expand Down
29 changes: 10 additions & 19 deletions primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,10 +1128,7 @@ pub trait Crypto {
#[runtime_interface]
pub trait EllipticCurves {
/// Compute a multi Miller loop on bls12_381
fn bls12_381_multi_miller_loop(
a: Vec<Vec<u8>>,
b: Vec<Vec<u8>>,
) -> Result<Vec<u8>, PairingError> {
fn bls12_381_multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, PairingError> {
sp_arkworks::bls12_381::multi_miller_loop(a, b)
}

Expand All @@ -1141,20 +1138,17 @@ pub trait EllipticCurves {
}

/// Compute a msm on G1 for bls12_381
fn bls12_381_msm_g1(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
fn bls12_381_msm_g1(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
sp_arkworks::bls12_381::msm_g1(bases, scalars)
}

/// Compute a msm on G2 for bls12_381
fn bls12_381_msm_g2(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
fn bls12_381_msm_g2(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
sp_arkworks::bls12_381::msm_g2(bases, scalars)
}

/// Compute a multi Miller loop for bls12_377
fn bls12_377_multi_miller_loop(
a: Vec<Vec<u8>>,
b: Vec<Vec<u8>>,
) -> Result<Vec<u8>, PairingError> {
fn bls12_377_multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, PairingError> {
sp_arkworks::bls12_377::multi_miller_loop(a, b)
}

Expand All @@ -1174,10 +1168,7 @@ pub trait EllipticCurves {
}

/// Compute a multi Miller loop on bw6_761
fn bw6_761_multi_miller_loop(
a: Vec<Vec<u8>>,
b: Vec<Vec<u8>>,
) -> Result<Vec<u8>, PairingError> {
fn bw6_761_multi_miller_loop(a: Vec<u8>, b: Vec<u8>) -> Result<Vec<u8>, PairingError> {
sp_arkworks::bw6_761::multi_miller_loop(a, b)
}

Expand All @@ -1187,27 +1178,27 @@ pub trait EllipticCurves {
}

/// Compute a msm on G1 for bw6_761
fn bw6_761_msm_g1(bases: Vec<Vec<u8>>, bigints: Vec<Vec<u8>>) -> Vec<u8> {
fn bw6_761_msm_g1(bases: Vec<u8>, bigints: Vec<u8>) -> Vec<u8> {
sp_arkworks::bw6_761::msm_g1(bases, bigints)
}

/// Compute a msm on G2 for bw6_761
fn bw6_761_msm_g2(bases: Vec<Vec<u8>>, bigints: Vec<Vec<u8>>) -> Vec<u8> {
fn bw6_761_msm_g2(bases: Vec<u8>, bigints: Vec<u8>) -> Vec<u8> {
sp_arkworks::bw6_761::msm_g2(bases, bigints)
}

/// Compute twisted edwards msm on ed_on_bls12_381
fn ed_on_bls12_381_te_msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
fn ed_on_bls12_381_te_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
sp_arkworks::ed_on_bls12_381::te_msm(bases, scalars)
}

/// Compute short weierstrass msm on ed_on_bls12_381
fn ed_on_bls12_381_sw_msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
fn ed_on_bls12_381_sw_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
sp_arkworks::ed_on_bls12_381::sw_msm(bases, scalars)
}

/// Compute msm on ed_on_bls12_377
fn ed_on_bls12_377_msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
fn ed_on_bls12_377_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
sp_arkworks::ed_on_bls12_377::msm(bases, scalars)
}
}
Expand Down

0 comments on commit 4389714

Please sign in to comment.