Skip to content

Commit

Permalink
Merge pull request #280 from xoloki/nostd
Browse files Browse the repository at this point in the history
Implement no_std support
  • Loading branch information
hdevalence authored Sep 28, 2019
2 parents f7c6df9 + 76c0e75 commit 426c87a
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 51 deletions.
25 changes: 14 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ keywords = ["cryptography", "crypto", "ristretto", "zero-knowledge", "bulletproo
description = "A pure-Rust implementation of Bulletproofs using Ristretto"

[dependencies]
curve25519-dalek = { version = "^1.2.3", features = ["serde"] }
subtle = "2"
sha3 = "0.8"
digest = "0.8"
rand = "0.6"
byteorder = "1"
serde = "1"
serde_derive = "1"
failure = "0.1"
merlin = "1.1"
clear_on_drop = "0.2"
curve25519-dalek = { version = "^1.2.3", default-features = false, features = ["u64_backend", "nightly", "serde", "alloc"] }
subtle = { version = "2", default-features = false }
sha3 = { version = "0.8", default-features = false }
digest = { version = "0.8", default-features = false }
rand_core = { version = "0.4", default-features = false, features = ["alloc"] }
rand = { version = "0.6", default-features = false, optional = true }
byteorder = { version = "1", default-features = false }
serde = { version = "1", default-features = false, features = ["alloc"] }
serde_derive = { version = "1", default-features = false }
failure = { version = "0.1", default-features = false, features = ["derive"] }
merlin = { version = "1.2", default-features = false }
clear_on_drop = { version = "0.2", default-features = false, features = ["nightly"] }

[dev-dependencies]
hex = "0.3"
Expand All @@ -31,8 +32,10 @@ bincode = "1"
rand_chacha = "0.1"

[features]
default = ["std", "avx2_backend"]
avx2_backend = ["curve25519-dalek/avx2_backend"]
yoloproofs = []
std = ["rand", "rand/std"]

[[test]]
name = "range_proof"
Expand Down
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Errors related to proving and verifying proofs.
extern crate alloc;
use alloc::vec::Vec;

/// Represents an error in proof creation, verification, or parsing.
#[derive(Fail, Clone, Debug, Eq, PartialEq)]
pub enum ProofError {
Expand Down
4 changes: 3 additions & 1 deletion src/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
#![allow(non_snake_case)]
#![deny(missing_docs)]

extern crate alloc;

use alloc::vec::Vec;
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_COMPRESSED;
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
use curve25519_dalek::ristretto::RistrettoPoint;
use curve25519_dalek::scalar::Scalar;
use curve25519_dalek::traits::MultiscalarMul;

use digest::{ExtendableOutput, Input, XofReader};
use sha3::{Sha3XofReader, Sha3_512, Shake256};

Expand Down
7 changes: 5 additions & 2 deletions src/inner_product_proof.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#![allow(non_snake_case)]
#![doc(include = "../docs/inner-product-protocol.md")]

use std::borrow::Borrow;
use std::iter;
extern crate alloc;

use alloc::borrow::Borrow;
use alloc::vec::Vec;

use core::iter;
use curve25519_dalek::ristretto::{CompressedRistretto, RistrettoPoint};
use curve25519_dalek::scalar::Scalar;
use curve25519_dalek::traits::VartimeMultiscalarMul;
Expand Down
12 changes: 11 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(nll)]
#![feature(external_doc)]
#![feature(try_trait)]
Expand All @@ -6,9 +7,17 @@
#![doc(html_logo_url = "https://doc.dalek.rs/assets/dalek-logo-clear.png")]

extern crate byteorder;

extern crate alloc;

#[cfg(feature = "std")]
extern crate core;
extern crate digest;

#[cfg(feature = "std")]
extern crate rand;

extern crate digest;
extern crate rand_core;
extern crate sha3;

extern crate clear_on_drop;
Expand Down Expand Up @@ -56,4 +65,5 @@ pub mod range_proof_mpc {
}

#[cfg(feature = "yoloproofs")]
#[cfg(feature = "std")]
pub mod r1cs;
29 changes: 27 additions & 2 deletions src/range_proof/dealer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
//! [the API for the aggregated multiparty computation protocol](../aggregation/index.html#api-for-the-aggregated-multiparty-computation-protocol).
use core::iter;

extern crate alloc;

use alloc::vec::Vec;

use curve25519_dalek::ristretto::RistrettoPoint;
use curve25519_dalek::scalar::Scalar;
use merlin::Transcript;
Expand All @@ -15,8 +20,13 @@ use inner_product_proof;
use range_proof::RangeProof;
use transcript::TranscriptProtocol;

use rand_core::{CryptoRng, RngCore};

use util;

#[cfg(feature = "std")]
use rand::thread_rng;

use super::messages::*;

/// Used to construct a dealer for the aggregated rangeproof MPC protocol.
Expand Down Expand Up @@ -282,6 +292,17 @@ impl<'a, 'b> DealerAwaitingProofShares<'a, 'b> {
})
}

/// Assemble the final aggregated [`RangeProof`] from the given
/// `proof_shares`, then validate the proof to ensure that all
/// `ProofShare`s were well-formed.
///
/// This is a convenience wrapper around receive_shares_with_rng
///
#[cfg(feature = "std")]
pub fn receive_shares(self, proof_shares: &[ProofShare]) -> Result<RangeProof, MPCError> {
self.receive_shares_with_rng(proof_shares, &mut thread_rng())
}

/// Assemble the final aggregated [`RangeProof`] from the given
/// `proof_shares`, then validate the proof to ensure that all
/// `ProofShare`s were well-formed.
Expand All @@ -295,15 +316,19 @@ impl<'a, 'b> DealerAwaitingProofShares<'a, 'b> {
/// performing local aggregation,
/// [`receive_trusted_shares`](DealerAwaitingProofShares::receive_trusted_shares)
/// saves time by skipping verification of the aggregated proof.
pub fn receive_shares(mut self, proof_shares: &[ProofShare]) -> Result<RangeProof, MPCError> {
pub fn receive_shares_with_rng<T: RngCore + CryptoRng>(
mut self,
proof_shares: &[ProofShare],
rng: &mut T,
) -> Result<RangeProof, MPCError> {
let proof = self.assemble_shares(proof_shares)?;

let Vs: Vec<_> = self.bit_commitments.iter().map(|vc| vc.V_j).collect();

// See comment in `Dealer::new` for why we use `initial_transcript`
let transcript = &mut self.initial_transcript;
if proof
.verify_multiple(self.bp_gens, self.pc_gens, transcript, &Vs, self.n)
.verify_multiple_with_rng(self.bp_gens, self.pc_gens, transcript, &Vs, self.n, rng)
.is_ok()
{
Ok(proof)
Expand Down
7 changes: 4 additions & 3 deletions src/range_proof/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
//! 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).
extern crate 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 Expand Up @@ -87,8 +90,6 @@ impl ProofShare {
poly_commitment: &PolyCommitment,
poly_challenge: &PolyChallenge,
) -> Result<(), ()> {
use std::iter;

use curve25519_dalek::traits::{IsIdentity, VartimeMultiscalarMul};

use inner_product_proof::inner_product;
Expand Down
Loading

0 comments on commit 426c87a

Please sign in to comment.