Skip to content

Commit

Permalink
remove cfg_if and just repeat cfg directives as necessary; use alloc:…
Browse files Browse the repository at this point in the history
…:vec to get vec! macro, and revert all macro removals
  • Loading branch information
xoloki committed Aug 5, 2019
1 parent 931288d commit b935b5b
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 88 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
10 changes: 4 additions & 6 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
11 changes: 5 additions & 6 deletions src/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 9 additions & 13 deletions src/inner_product_proof.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
18 changes: 7 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
11 changes: 5 additions & 6 deletions src/range_proof/dealer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 4 additions & 8 deletions src/range_proof/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 9 additions & 13 deletions src/range_proof/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
11 changes: 4 additions & 7 deletions src/range_proof/party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
46 changes: 29 additions & 17 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn add_vec(a: &[Scalar], b: &[Scalar]) -> Vec<Scalar> {
// throw some error
//println!("lengths of vectors don't match for vector addition");
}
let mut out: Vec<Scalar> = (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];
}
Expand All @@ -82,8 +82,7 @@ pub fn add_vec(a: &[Scalar], b: &[Scalar]) -> Vec<Scalar> {

impl VecPoly1 {
pub fn zero(n: usize) -> Self {
let zn: Vec<Scalar> = (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 {
Expand All @@ -104,7 +103,7 @@ impl VecPoly1 {

pub fn eval(&self, x: Scalar) -> Vec<Scalar> {
let n = self.0.len();
let mut out: Vec<Scalar> = (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;
}
Expand All @@ -115,8 +114,12 @@ impl VecPoly1 {
#[cfg(feature = "yoloproofs")]
impl VecPoly3 {
pub fn zero(n: usize) -> Self {
let zn: Vec<Scalar> = (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:
Expand Down Expand Up @@ -145,7 +148,7 @@ impl VecPoly3 {

pub fn eval(&self, x: Scalar) -> Vec<Scalar> {
let n = self.0.len();
let mut out: Vec<Scalar> = (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]));
}
Expand Down Expand Up @@ -282,8 +285,18 @@ mod tests {

#[test]
fn test_inner_product() {
let a: Vec<Scalar> = (1..5).map(|i| Scalar::from(i as u64)).collect();
let b: Vec<Scalar> = (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));
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit b935b5b

Please sign in to comment.