Skip to content

Commit

Permalink
Move arkworks curves to manta-crypto (#247)
Browse files Browse the repository at this point in the history
* feat: move arkworks curves to manta-crypto
* chore: update CHANGELOG and dependencies
* chore: update dependencies
* chore: update rand-chacha dependency
* chore: fix test dependency

Signed-off-by: Brandon H. Gomes <bhgomes@pm.me>
  • Loading branch information
bhgomes authored Sep 9, 2022
1 parent 00e3c80 commit f511813
Show file tree
Hide file tree
Showing 26 changed files with 124 additions and 86 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [\#197](https://github.com/Manta-Network/manta-rs/pull/197) Add ECLAIR utilities for next circuit upgrade

### Changed
- [\#247](https://github.com/Manta-Network/manta-rs/pull/247) Moved BLS12-381 and BN254 curves (and Edwards counterparts) to `manta-crypto`
- [\#236](https://github.com/Manta-Network/manta-rs/pull/236) Moved `RatioProof` from `manta-trusted-setup` to `manta-crypto`
- [\#180](https://github.com/Manta-Network/manta-rs/pull/180) Start moving to new `arkworks` backend for `manta-crypto`
- [\#191](https://github.com/Manta-Network/manta-rs/pull/191) Move HTTP Utilities to `manta-util`
Expand Down
3 changes: 1 addition & 2 deletions manta-benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ name = "reclaim"
harness = false

[dependencies]
ark-bls12-381 = { version = "0.3.0", default-features = false }
ark-ec = { version = "0.3.0", default-features = false }
ark-ff = { version = "0.3.0", default-features = false }
getrandom = { version = "0.2.6", default-features = false, features = ["js"] }
instant = { version = "0.1.12", default-features = false, features = [ "wasm-bindgen" ] }
manta-accounting = { path = "../manta-accounting", default-features = false, features = ["test"] }
manta-crypto = { path = "../manta-crypto", default-features = false, features = ["getrandom", "test"] }
manta-crypto = { path = "../manta-crypto", default-features = false, features = ["ark-bls12-381", "getrandom", "test"] }
manta-pay = { path = "../manta-pay", default-features = false, features = ["groth16", "test"] }
wasm-bindgen = { version = "0.2.82", default-features = false }
wasm-bindgen-test = { version = "0.3.30", default-features = false }
Expand Down
14 changes: 12 additions & 2 deletions manta-benchmark/benches/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

//! Elliptic Curve Cryptography Benchmarks
use ark_bls12_381::{G1Affine, G1Projective};
use core::iter::repeat_with;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use manta_benchmark::ecc;
use manta_crypto::rand::OsRng;
use manta_crypto::{
arkworks::bls12_381::{G1Affine, G1Projective},
rand::OsRng,
};

#[inline]
fn affine_affine_addition(c: &mut Criterion) {
let mut group = c.benchmark_group("bench");
let mut rng = OsRng;
Expand All @@ -34,6 +37,7 @@ fn affine_affine_addition(c: &mut Criterion) {
});
}

#[inline]
fn projective_affine_addition(c: &mut Criterion) {
let mut group = c.benchmark_group("bench");
let mut rng = OsRng;
Expand All @@ -46,6 +50,7 @@ fn projective_affine_addition(c: &mut Criterion) {
});
}

#[inline]
fn projective_projective_addition(c: &mut Criterion) {
let mut group = c.benchmark_group("bench");
let mut rng = OsRng;
Expand All @@ -58,6 +63,7 @@ fn projective_projective_addition(c: &mut Criterion) {
});
}

#[inline]
fn affine_scalar_multiplication(c: &mut Criterion) {
let mut group = c.benchmark_group("bench");
let mut rng = OsRng;
Expand All @@ -70,6 +76,7 @@ fn affine_scalar_multiplication(c: &mut Criterion) {
});
}

#[inline]
fn projective_scalar_multiplication(c: &mut Criterion) {
let mut group = c.benchmark_group("bench");
let mut rng = OsRng;
Expand All @@ -82,6 +89,7 @@ fn projective_scalar_multiplication(c: &mut Criterion) {
});
}

#[inline]
fn projective_to_affine_normalization(c: &mut Criterion) {
let mut group = c.benchmark_group("bench");
let mut rng = OsRng;
Expand All @@ -93,6 +101,7 @@ fn projective_to_affine_normalization(c: &mut Criterion) {
});
}

#[inline]
fn batch_vector_projective_to_affine_normalization(c: &mut Criterion) {
let mut group = c.benchmark_group("bench");
let mut rng = OsRng;
Expand All @@ -109,6 +118,7 @@ fn batch_vector_projective_to_affine_normalization(c: &mut Criterion) {
});
}

#[inline]
fn naive_vector_projective_to_affine_normalization(c: &mut Criterion) {
let mut group = c.benchmark_group("bench");
let mut rng = OsRng;
Expand Down
4 changes: 2 additions & 2 deletions manta-benchmark/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ where
point_vec.iter().map(P::into_affine).collect()
}

/// Testing Suite
#[cfg(test)]
mod test {
use super::*;
use ark_bls12_381::G1Affine;
use manta_crypto::rand::OsRng;
use manta_crypto::{arkworks::bls12_381::G1Affine, rand::OsRng};

/// Tests if affine-affine addition, affine-projective addition, and projective-projective
/// addition give same results.
Expand Down
25 changes: 19 additions & 6 deletions manta-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ arkworks = [
]

# Dalek Cryptography Backend
dalek = [
"ed25519-dalek",
]
dalek = ["ed25519-dalek"]

# Enable `getrandom` Entropy Source
getrandom = ["rand_core/getrandom"]
Expand All @@ -50,13 +48,29 @@ serde = [
]

# Standard Library
std = ["manta-util/std", "rand_chacha?/std"]
std = [
"ark-bls12-381?/std",
"ark-bn254?/std",
"ark-ec?/std",
"ark-ed-on-bls12-381?/std",
"ark-ed-on-bn254?/std",
"ark-ff?/std",
"ark-r1cs-std?/std",
"ark-relations?/std",
"ark-serialize?/std",
"manta-util/std",
"rand_chacha?/std",
]

# Testing Frameworks
test = []

[dependencies]
ark-bls12-381 = { version = "0.3.0", optional = true, default-features = false, features = ["curve"] }
ark-bn254 = { version = "0.3.0", optional = true, default-features = false, features = ["curve"] }
ark-ec = { version = "0.3.0", optional = true, default-features = false }
ark-ed-on-bls12-381 = { version = "0.3.0", optional = true, default-features = false, features = ["r1cs"] }
ark-ed-on-bn254 = { version = "0.3.0", optional = true, default-features = false, features = ["r1cs"] }
ark-ff = { version = "0.3.0", optional = true, default-features = false }
ark-r1cs-std = { version = "0.3.1", optional = true, default-features = false }
ark-relations = { version = "0.3.0", optional = true, default-features = false }
Expand All @@ -69,5 +83,4 @@ rand_chacha = { version = "0.3.1", optional = true, default-features = false }
rand_core = { version = "0.6.3", default-features = false }

[dev-dependencies]
ark-bn254 = { version = "0.3.0", default-features = false, features = ["scalar_field"] }
manta-crypto = { path = ".", default-features = false, features = ["getrandom"] }
manta-crypto = { path = ".", default-features = false, features = ["ark-bn254", "getrandom"] }
7 changes: 5 additions & 2 deletions manta-crypto/src/arkworks/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ field_try_into! {
#[cfg(test)]
mod test {
use super::*;
use crate::rand::{OsRng, Rand, RngCore, Sample};
use crate::{
arkworks::bn254::Fr,
rand::{OsRng, Rand, RngCore, Sample},
};
use alloc::vec::Vec;
use core::fmt::Debug;

Expand Down Expand Up @@ -101,7 +104,7 @@ mod test {
($name:ident, $convert:ident, $type:tt) => {
#[test]
fn $name() {
assert_valid_integer_conversions::<ark_bn254::Fr, _, _, _, 0xFFFF>(
assert_valid_integer_conversions::<Fr, _, _, _, 0xFFFF>(
$convert,
vec![0, 1, 2, $type::MAX - 2, $type::MAX - 1, $type::MAX],
&mut OsRng,
Expand Down
12 changes: 12 additions & 0 deletions manta-crypto/src/arkworks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ pub use ark_r1cs_std as r1cs_std;
pub use ark_relations as relations;
pub use ark_serialize as serialize;

#[cfg(feature = "ark-bls12-381")]
pub use ark_bls12_381 as bls12_381;

#[cfg(feature = "ark-bn254")]
pub use ark_bn254 as bn254;

#[cfg(feature = "ark-ed-on-bls12-381")]
pub use ark_ed_on_bls12_381 as ed_on_bls12_381;

#[cfg(feature = "ark-ed-on-bn254")]
pub use ark_ed_on_bn254 as ed_on_bn254;

pub mod algebra;
pub mod constraint;
pub mod ff;
Expand Down
31 changes: 29 additions & 2 deletions manta-crypto/src/arkworks/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,15 @@ pub trait PairingEngineExt: PairingEngine {
impl<E> PairingEngineExt for E where E: PairingEngine {}

/// Testing Framework
#[cfg(feature = "test")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "test")))]
#[cfg(any(feature = "test", test))]
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "test", test))))]
pub mod test {
use super::*;
use crate::arkworks::ec::ProjectiveCurve;

#[cfg(test)]
use crate::rand::{OsRng, Rand};

/// Asserts that `g1` and `g1*scalar` are in the same ratio as `g2` and `g2*scalar`.
#[inline]
pub fn assert_valid_pairing_ratio<E>(g1: E::G1Affine, g2: E::G2Affine, scalar: E::Fr)
Expand All @@ -125,4 +128,28 @@ pub mod test {
)
.is_some());
}

/// Checks that BLS12-381 has a valid pairing ratio.
#[cfg(feature = "ark-bls12-381")]
#[test]
fn bls12_381_has_valid_pairing_ratio() {
let mut rng = OsRng;
assert_valid_pairing_ratio::<crate::arkworks::bls12_381::Bls12_381>(
rng.gen(),
rng.gen(),
rng.gen(),
);
}

/// Checks that BN254 has a valid pairing ratio.
#[cfg(feature = "ark-bn254")]
#[test]
fn bn254_has_valid_pairing_ratio() {
let mut rng = OsRng;
assert_valid_pairing_ratio::<crate::arkworks::bn254::Bn254>(
rng.gen(),
rng.gen(),
rng.gen(),
);
}
}
6 changes: 3 additions & 3 deletions manta-parameters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ download = ["anyhow", "attohttpc", "std"]
std = ["anyhow?/std"]

[dependencies]
anyhow = { version = "1.0.62", optional = true, default-features = false }
attohttpc = { version = "0.19.1", optional = true }
anyhow = { version = "1.0.64", optional = true, default-features = false }
attohttpc = { version = "0.22.0", optional = true }
blake3 = { version = "1.3.1", default-features = false }

[dev-dependencies]
Expand All @@ -44,7 +44,7 @@ tempfile = { version = "3.3.0", default-features = false }
walkdir = { version = "2.3.2", default-features = false }

[build-dependencies]
anyhow = { version = "1.0.62", default-features = false, features = ["std"] }
anyhow = { version = "1.0.64", default-features = false, features = ["std"] }
blake3 = { version = "1.3.1", default-features = false, features = ["std"] }
gitignore = { version = "1.0.7", default-features = false }
hex = { version = "0.4.3", default-features = false, features = ["std"] }
Expand Down
23 changes: 9 additions & 14 deletions manta-pay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ required-features = ["clap", "groth16", "simulation"]
[features]
# Enable Arkworks Backend
arkworks = [
"ark-bls12-381",
"ark-ed-on-bls12-381",
"ark-std",
"manta-crypto/ark-bls12-381",
"manta-crypto/ark-ed-on-bls12-381",
"manta-crypto/arkworks",
]

Expand All @@ -67,9 +67,9 @@ serde = ["manta-accounting/serde", "manta-crypto/serde"]
simulation = [
"indexmap",
"parking_lot",
"rayon",
"manta-util/rayon",
"test",
"tide",
"manta-util/tide",
"tokio/io-std",
"tokio/io-util",
"tokio/macros",
Expand Down Expand Up @@ -100,31 +100,26 @@ websocket = [

[dependencies]
aes-gcm = { version = "0.9.4", default-features = false, features = ["aes", "alloc"] }
ark-bls12-381 = { version = "0.3.0", optional = true, default-features = false, features = ["curve"] }
ark-ed-on-bls12-381 = { version = "0.3.0", optional = true, default-features = false, features = ["r1cs"] }
ark-groth16 = { version = "0.3.0", optional = true, default-features = false }
ark-snark = { version = "0.3.0", optional = true, default-features = false }
ark-std = { version = "0.3.0", optional = true, default-features = false }
bip32 = { version = "0.3.0", optional = true, default-features = false, features = ["bip39", "secp256k1"] }
blake2 = { version = "0.10.4", default-features = false }
bs58 = { version = "0.4.0", optional = true, default-features = false, features = ["alloc"] }
clap = { version = "3.2.17", optional = true, default-features = false, features = ["color", "derive", "std", "suggestions", "unicode", "wrap_help"] }
clap = { version = "3.2.20", optional = true, default-features = false, features = ["color", "derive", "std", "suggestions", "unicode", "wrap_help"] }
derivative = { version = "2.2.0", default-features = false, features = ["use_core"] }
futures = { version = "0.3.21", optional = true, default-features = false }
indexmap = { version = "1.8.2", optional = true, default-features = false }
futures = { version = "0.3.24", optional = true, default-features = false }
indexmap = { version = "1.9.1", optional = true, default-features = false }
manta-accounting = { path = "../manta-accounting", default-features = false }
manta-crypto = { path = "../manta-crypto", default-features = false }
manta-crypto = { path = "../manta-crypto", default-features = false, features = ["rand_chacha"] }
manta-parameters = { path = "../manta-parameters", optional = true, default-features = false }
manta-util = { path = "../manta-util", default-features = false }
parking_lot = { version = "0.12.1", optional = true, default-features = false }
rand_chacha = { version = "0.3.1", default-features = false }
rayon = { version = "1.5.1", optional = true, default-features = false }
scale-codec = { package = "parity-scale-codec", version = "3.1.2", optional = true, default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "2.1.2", optional = true, default-features = false, features = ["derive"] }
serde_json = { version = "1.0.85", optional = true, default-features = false, features = ["alloc"] }
tempfile = { version = "3.3.0", optional = true, default-features = false }
tide = { version = "0.16.0", optional = true, default-features = false, features = ["h1-server"] }
tokio = { version = "1.20.1", optional = true, default-features = false }
tokio = { version = "1.21.0", optional = true, default-features = false }
tokio-tungstenite = { version = "0.17.2", optional = true, default-features = false, features = ["native-tls"] }
ws_stream_wasm = { version = "0.7.3", optional = true, default-features = false }

Expand Down
3 changes: 1 addition & 2 deletions manta-pay/src/bin/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ use manta_crypto::{
eclair::alloc::{mode::Secret, Allocate, Allocator},
hash::ArrayHashFunction,
key::agreement::{Agree, Derive},
rand::{Sample, SeedableRng},
rand::{ChaCha20Rng, Sample, SeedableRng},
};
use manta_pay::config::{
Compiler, KeyAgreementScheme, KeyAgreementSchemeVar, Poseidon2, Poseidon2Var, Poseidon4,
Poseidon4Var,
};
use rand_chacha::ChaCha20Rng;

/// Runs some basic measurements of the circuit component sizes.
#[inline]
Expand Down
14 changes: 4 additions & 10 deletions manta-pay/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ use blake2::{
digest::{Update, VariableOutput},
Blake2sVar,
};
use bls12_381::Bls12_381;
use bls12_381_ed::constraints::EdwardsVar as Bls12_381_EdwardsVar;
use manta_accounting::{
asset::{Asset, AssetId, AssetValue},
transfer,
Expand All @@ -38,6 +36,8 @@ use manta_crypto::{
accumulator,
algebra::DiffieHellman,
arkworks::{
bls12_381::{self, Bls12_381},
ed_on_bls12_381::{self, constraints::EdwardsVar as Bls12_381_EdwardsVar},
ff::ToConstraintField,
serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError},
},
Expand Down Expand Up @@ -66,19 +66,13 @@ use alloc::string::String;
#[cfg(any(feature = "test", test))]
use manta_crypto::rand::{Rand, RngCore, Sample};

#[doc(inline)]
pub use ark_bls12_381 as bls12_381;

#[doc(inline)]
pub use ark_ed_on_bls12_381 as bls12_381_ed;

pub(crate) use bls12_381_ed::EdwardsProjective as Bls12_381_Edwards;
pub(crate) use ed_on_bls12_381::EdwardsProjective as Bls12_381_Edwards;

/// Pairing Curve Type
pub type PairingCurve = Bls12_381;

/// Embedded Scalar Field Type
pub type EmbeddedScalarField = bls12_381_ed::Fr;
pub type EmbeddedScalarField = ed_on_bls12_381::Fr;

/// Embedded Scalar Type
pub type EmbeddedScalar = ecc::arkworks::Scalar<Bls12_381_Edwards>;
Expand Down
3 changes: 1 addition & 2 deletions manta-pay/src/crypto/constraint/arkworks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,9 @@ where
#[cfg(test)]
mod tests {
use super::*;
use ark_bls12_381::Fr;
use core::iter::repeat_with;
use manta_crypto::{
arkworks::ff::BigInteger,
arkworks::{bls12_381::Fr, ff::BigInteger},
eclair::alloc::Allocate,
rand::{OsRng, Rand},
};
Expand Down
Loading

0 comments on commit f511813

Please sign in to comment.