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

Commit

Permalink
use ark_scale (#13954)
Browse files Browse the repository at this point in the history
* use ark_scale

* fix tests

* fix tests

* cleanup & comments

* use correct PR branch
  • Loading branch information
achimcc authored Apr 20, 2023
1 parent a2b185f commit 70bcea0
Show file tree
Hide file tree
Showing 15 changed files with 896 additions and 628 deletions.
1,089 changes: 619 additions & 470 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions primitives/arkworks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ark-ed-on-bls12-381 = { version = "0.4.0", default-features = false }
ark-ed-on-bls12-377 = { version = "0.4.0", default-features = false }
sp-std = { version = "5.0.0", path = "../std", default-features = false }
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false }
ark-scale = { git = "https://github.com/w3f/ark-scale", default-features = false }

[features]
default = [ "std" ]
Expand All @@ -37,4 +38,5 @@ std = [
"ark-ed-on-bls12-377/std",
"sp-std/std",
"codec/std",
"ark-scale/std",
]
22 changes: 15 additions & 7 deletions primitives/arkworks/src/bls12_377.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,42 @@ use crate::utils::{
use ark_bls12_377::{g1, g2, Bls12_377};
use sp_std::vec::Vec;

pub fn multi_miller_loop(a: Vec<Vec<u8>>, b: Vec<Vec<u8>>) -> Result<Vec<u8>, ()> {
/// 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)
}

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

pub fn msm_g1(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
/// 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 fn msm_g2(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
/// 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)
}

pub fn mul_projective_g1(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// 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)
}

pub fn mul_projective_g2(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// 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)
}

pub fn mul_affine_g1(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// Compute a affine scalar multiplication for short_weierstrass through arkworks on G1
pub fn mul_affine_g1(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_affine_generic::<g1::Config>(base, scalar)
}

pub fn mul_affine_g2(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// Compute a affine scalar multiplication for short_weierstrass through arkworks on G2
pub fn mul_affine_g2(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_affine_generic::<g2::Config>(base, scalar)
}
22 changes: 15 additions & 7 deletions primitives/arkworks/src/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,42 @@ use crate::utils::{
use ark_bls12_381::{g1, g2, Bls12_381};
use sp_std::vec::Vec;

pub fn multi_miller_loop(a: Vec<Vec<u8>>, b: Vec<Vec<u8>>) -> Result<Vec<u8>, ()> {
/// 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_381>(a, b)
}

/// Compute a final exponentiation through arkworks
pub fn final_exponentiation(target: Vec<u8>) -> Result<Vec<u8>, ()> {
final_exponentiation_generic::<Bls12_381>(target)
}

pub fn msm_g1(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
/// 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 fn msm_g2(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
/// 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)
}

pub fn mul_projective_g1(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// 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)
}

pub fn mul_projective_g2(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// 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)
}

pub fn mul_affine_g1(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// Compute a affine scalar multiplication for short_weierstrass through arkworks on G1
pub fn mul_affine_g1(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_affine_generic::<g1::Config>(base, scalar)
}

pub fn mul_affine_g2(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// Compute a affine scalar multiplication for short_weierstrass through arkworks on G2
pub fn mul_affine_g2(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_affine_generic::<g2::Config>(base, scalar)
}
22 changes: 15 additions & 7 deletions primitives/arkworks/src/bw6_761.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,42 @@ use crate::utils::{
use ark_bw6_761::{g1, g2, BW6_761};
use sp_std::vec::Vec;

pub fn multi_miller_loop(a: Vec<Vec<u8>>, b: Vec<Vec<u8>>) -> Result<Vec<u8>, ()> {
/// 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::<BW6_761>(a, b)
}

/// Compute a final exponentiation through arkworks
pub fn final_exponentiation(target: Vec<u8>) -> Result<Vec<u8>, ()> {
final_exponentiation_generic::<BW6_761>(target)
}

pub fn msm_g1(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
/// 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 fn msm_g2(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
/// 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)
}

pub fn mul_projective_g1(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// 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)
}

pub fn mul_projective_g2(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// 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)
}

pub fn mul_affine_g1(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// Compute a affine scalar multiplication for short_weierstrass through arkworks on G1
pub fn mul_affine_g1(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_affine_generic::<g1::Config>(base, scalar)
}

pub fn mul_affine_g2(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// Compute a affine scalar multiplication for short_weierstrass through arkworks on G2
pub fn mul_affine_g2(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_affine_generic::<g2::Config>(base, scalar)
}
9 changes: 6 additions & 3 deletions primitives/arkworks/src/ed_on_bls12_377.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ use crate::utils::{msm_te_generic, mul_affine_te_generic, mul_projective_te_gene
use ark_ed_on_bls12_377::EdwardsConfig;
use sp_std::vec::Vec;

pub fn mul_projective(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// Compute a projective scalar multiplication for twisted_edwards through arkworks
pub fn mul_projective(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_projective_te_generic::<EdwardsConfig>(base, scalar)
}

pub fn mul_affine(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// Compute a scalar multiplication for twisted_edwards through arkworks
pub fn mul_affine(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_affine_te_generic::<EdwardsConfig>(base, scalar)
}

pub fn msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
/// Compute a multi scalar mulitplication for twisted_edwards through arkworks
pub fn msm(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
msm_te_generic::<EdwardsConfig>(bases, scalars)
}
18 changes: 12 additions & 6 deletions primitives/arkworks/src/ed_on_bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,32 @@ use crate::utils::{
use ark_ed_on_bls12_381::JubjubConfig;
use sp_std::vec::Vec;

pub fn sw_mul_projective(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// Compute a projective scalar multiplication for short_weierstrass through arkworks
pub fn sw_mul_projective(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_projective_generic::<JubjubConfig>(base, scalar)
}

pub fn sw_mul_affine(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// Compute a affine scalar multiplication for short_weierstrass through arkworks
pub fn sw_mul_affine(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_affine_generic::<JubjubConfig>(base, scalar)
}

pub fn te_mul_projective(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// Compute a projective scalar multiplication for twisted_edwards through arkworks
pub fn te_mul_projective(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_projective_te_generic::<JubjubConfig>(base, scalar)
}

pub fn te_mul_affine(base: Vec<u8>, scalar: Vec<u8>) -> Vec<u8> {
/// Compute a scalar multiplication for twisted_edwards through arkworks
pub fn te_mul_affine(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
mul_affine_te_generic::<JubjubConfig>(base, scalar)
}

pub fn te_msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
/// Compute a multi scalar mulitplication for twisted_edwards through arkworks
pub fn te_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
msm_te_generic::<JubjubConfig>(bases, scalars)
}

pub fn sw_msm(bases: Vec<Vec<u8>>, scalars: Vec<Vec<u8>>) -> Vec<u8> {
/// Compute a multi scalar multiplication for short_weierstrass through arkworks
pub fn sw_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
msm_sw_generic::<JubjubConfig>(bases, scalars)
}
Loading

0 comments on commit 70bcea0

Please sign in to comment.