diff --git a/Cargo.toml b/Cargo.toml index 8c592df0..2cb70293 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,6 @@ keywords = ["cryptography", "crypto", "ristretto", "zero-knowledge", "bulletproo description = "A pure-Rust implementation of Bulletproofs using Ristretto" [dependencies] -cfg-if = "0.1" curve25519-dalek = { version = "1.2", default-features = false, features = ["u64_backend", "nightly", "serde"] } subtle = { version = "2", default-features = false } sha3 = { version = "0.8", default-features = false } diff --git a/src/errors.rs b/src/errors.rs index 732e81d7..ad1ec50b 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,11 +1,9 @@ //! Errors related to proving and verifying proofs. -cfg_if::cfg_if! { - if #[cfg(feature = "alloc")] { - extern crate alloc; - use alloc::vec::Vec; - } -} +#[cfg(feature = "alloc")] +extern crate alloc; +#[cfg(feature = "alloc")] +use alloc::vec::Vec; /// Represents an error in proof creation, verification, or parsing. #[derive(Fail, Clone, Debug, Eq, PartialEq)] diff --git a/src/generators.rs b/src/generators.rs index f41fb201..38d1630d 100644 --- a/src/generators.rs +++ b/src/generators.rs @@ -4,12 +4,11 @@ #![allow(non_snake_case)] #![deny(missing_docs)] -cfg_if::cfg_if! { - if #[cfg(feature = "alloc")] { - extern crate alloc; - use alloc::vec::Vec; - } -} +#[cfg(feature = "alloc")] +extern crate alloc; + +#[cfg(feature = "alloc")] +use alloc::vec::Vec; use curve25519_dalek::constants::RISTRETTO_BASEPOINT_COMPRESSED; use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT; diff --git a/src/inner_product_proof.rs b/src/inner_product_proof.rs index 41870af3..75443819 100644 --- a/src/inner_product_proof.rs +++ b/src/inner_product_proof.rs @@ -1,19 +1,15 @@ #![allow(non_snake_case)] #![doc(include = "../docs/inner-product-protocol.md")] -cfg_if::cfg_if! { - if #[cfg(feature = "std")] { - use std::borrow::Borrow; - } -} - -cfg_if::cfg_if! { - if #[cfg(feature = "alloc")] { - extern crate alloc; - use alloc::vec::Vec; - use alloc::borrow::Borrow; - } -} +#[cfg(feature = "alloc")] +extern crate alloc; + +#[cfg(feature = "alloc")] +use alloc::vec::Vec; +#[cfg(feature = "alloc")] +use alloc::borrow::Borrow; +#[cfg(feature = "std")] +use std::borrow::Borrow; use core::iter; use curve25519_dalek::ristretto::{CompressedRistretto, RistrettoPoint}; diff --git a/src/lib.rs b/src/lib.rs index afaf5829..bfabb072 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,12 +9,8 @@ extern crate byteorder; -cfg_if::cfg_if! { - if #[cfg(feature = "alloc")] { - extern crate alloc; - use alloc::vec::Vec; - } -} +#[cfg(feature = "alloc")] +extern crate alloc; #[cfg(feature = "std")] extern crate core; @@ -58,6 +54,9 @@ mod inner_product_proof; mod range_proof; mod transcript; +#[cfg(feature = "alloc")] +use alloc::vec::Vec; + pub use errors::ProofError; pub use generators::{BulletproofGens, BulletproofGensShare, PedersenGens}; pub use range_proof::RangeProof; @@ -71,8 +70,5 @@ pub mod range_proof_mpc { } #[cfg(feature = "yoloproofs")] -cfg_if::cfg_if! { - if #[cfg(feature = "std")] { - pub mod r1cs; - } -} +#[cfg(feature = "std")] +pub mod r1cs; diff --git a/src/range_proof/dealer.rs b/src/range_proof/dealer.rs index 021c17a4..896b9efa 100644 --- a/src/range_proof/dealer.rs +++ b/src/range_proof/dealer.rs @@ -6,12 +6,11 @@ use core::iter; -cfg_if::cfg_if! { - if #[cfg(feature = "alloc")] { - extern crate alloc; - use alloc::vec::Vec; - } -} +#[cfg(feature = "alloc")] +extern crate alloc; + +#[cfg(feature = "alloc")] +use alloc::vec::Vec; use curve25519_dalek::ristretto::RistrettoPoint; use curve25519_dalek::scalar::Scalar; diff --git a/src/range_proof/messages.rs b/src/range_proof/messages.rs index ac979fd9..ff6431c6 100644 --- a/src/range_proof/messages.rs +++ b/src/range_proof/messages.rs @@ -4,18 +4,14 @@ //! For more explanation of how the `dealer`, `party`, and `messages` modules orchestrate the protocol execution, see //! [the API for the aggregated multiparty computation protocol](../aggregation/index.html#api-for-the-aggregated-multiparty-computation-protocol). -cfg_if::cfg_if! { - if #[cfg(feature = "alloc")] { - extern crate alloc; - use alloc::vec::Vec; - } -} +#[cfg(feature = "alloc")] +extern crate alloc; +#[cfg(feature = "alloc")] +use alloc::vec::Vec; use core::iter; - use curve25519_dalek::ristretto::{CompressedRistretto, RistrettoPoint}; use curve25519_dalek::scalar::Scalar; - use generators::{BulletproofGens, PedersenGens}; /// A commitment to the bits of a party's value. diff --git a/src/range_proof/mod.rs b/src/range_proof/mod.rs index 498f4ddc..964f00c0 100644 --- a/src/range_proof/mod.rs +++ b/src/range_proof/mod.rs @@ -1,19 +1,15 @@ #![allow(non_snake_case)] #![doc(include = "../../docs/range-proof-protocol.md")] -cfg_if::cfg_if! { - if #[cfg(feature = "alloc")] { - extern crate alloc; - use alloc::vec::Vec; - } -} - -cfg_if::cfg_if! { - if #[cfg(feature = "std")] { - extern crate rand; - use self::rand::thread_rng; - } -} +#[cfg(feature = "alloc")] +extern crate alloc; +#[cfg(feature = "std")] +extern crate rand; + +#[cfg(feature = "alloc")] +use alloc::vec::Vec; +#[cfg(feature = "std")] +use self::rand::thread_rng; use core::iter; diff --git a/src/range_proof/party.rs b/src/range_proof/party.rs index e9bbd9fd..cc9a9d2c 100644 --- a/src/range_proof/party.rs +++ b/src/range_proof/party.rs @@ -10,18 +10,15 @@ //! modules orchestrate the protocol execution, see the documentation //! in the [`aggregation`](::range_proof_mpc) module. -cfg_if::cfg_if! { - if #[cfg(feature = "alloc")] { - extern crate alloc; - use alloc::vec::Vec; - } -} +#[cfg(feature = "alloc")] +extern crate alloc; +#[cfg(feature = "alloc")] +use alloc::vec::Vec; use core::iter; use curve25519_dalek::ristretto::{CompressedRistretto, RistrettoPoint}; use curve25519_dalek::scalar::Scalar; use curve25519_dalek::traits::MultiscalarMul; - use clear_on_drop::clear::Clear; use errors::MPCError; use generators::{BulletproofGens, PedersenGens}; diff --git a/src/util.rs b/src/util.rs index fb2cfa04..b7920e85 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,13 +1,13 @@ #![deny(missing_docs)] #![allow(non_snake_case)] -cfg_if::cfg_if! { - if #[cfg(feature = "alloc")] { - extern crate alloc; - use alloc::vec::Vec; - } -} +#[cfg(feature = "alloc")] +extern crate alloc; +#[cfg(feature = "alloc")] +use alloc::vec; +#[cfg(feature = "alloc")] +use alloc::vec::Vec; use clear_on_drop::clear::Clear; use curve25519_dalek::scalar::Scalar; use inner_product_proof::inner_product; @@ -73,7 +73,7 @@ pub fn add_vec(a: &[Scalar], b: &[Scalar]) -> Vec { // throw some error //println!("lengths of vectors don't match for vector addition"); } - let mut out: Vec = (0..b.len()).map(|_| Scalar::zero()).collect(); + let mut out = vec![Scalar::zero(); b.len()]; for i in 0..a.len() { out[i] = a[i] + b[i]; } @@ -82,8 +82,7 @@ pub fn add_vec(a: &[Scalar], b: &[Scalar]) -> Vec { impl VecPoly1 { pub fn zero(n: usize) -> Self { - let zn: Vec = (0..n).map(|_| Scalar::zero()).collect(); - VecPoly1(zn.clone(), zn) + VecPoly1(vec![Scalar::zero(); n], vec![Scalar::zero(); n]) } pub fn inner_product(&self, rhs: &VecPoly1) -> Poly2 { @@ -104,7 +103,7 @@ impl VecPoly1 { pub fn eval(&self, x: Scalar) -> Vec { let n = self.0.len(); - let mut out: Vec = (0..n).map(|_| Scalar::zero()).collect(); + let mut out = vec![Scalar::zero(); n]; for i in 0..n { out[i] = self.0[i] + self.1[i] * x; } @@ -115,8 +114,12 @@ impl VecPoly1 { #[cfg(feature = "yoloproofs")] impl VecPoly3 { pub fn zero(n: usize) -> Self { - let zn: Vec = (0..n).map(|_| Scalar::zero()).collect(); - VecPoly3(zn.clone(), zn.clone(), zn.clone(), zn.clone()) + VecPoly3( + vec![Scalar::zero(); n], + vec![Scalar::zero(); n], + vec![Scalar::zero(); n], + vec![Scalar::zero(); n], + ) } /// Compute an inner product of `lhs`, `rhs` which have the property that: @@ -145,7 +148,7 @@ impl VecPoly3 { pub fn eval(&self, x: Scalar) -> Vec { let n = self.0.len(); - let mut out: Vec = (0..n).map(|_| Scalar::zero()).collect(); + let mut out = vec![Scalar::zero(); n]; for i in 0..n { out[i] = self.0[i] + x * (self.1[i] + x * (self.2[i] + x * self.3[i])); } @@ -282,8 +285,18 @@ mod tests { #[test] fn test_inner_product() { - let a: Vec = (1..5).map(|i| Scalar::from(i as u64)).collect(); - let b: Vec = (2..6).map(|i| Scalar::from(i as u64)).collect(); + let a = vec![ + Scalar::from(1u64), + Scalar::from(2u64), + Scalar::from(3u64), + Scalar::from(4u64), + ]; + let b = vec![ + Scalar::from(2u64), + Scalar::from(3u64), + Scalar::from(4u64), + Scalar::from(5u64), + ]; assert_eq!(Scalar::from(40u64), inner_product(&a, &b)); } @@ -341,8 +354,7 @@ mod tests { #[test] fn vec_of_scalars_clear_on_drop() { - let mut v = Vec::new(); - v.extend_from_slice(&[Scalar::from(24u64), Scalar::from(42u64)]); + let mut v = vec![Scalar::from(24u64), Scalar::from(42u64)]; for e in v.iter_mut() { e.clear();