Skip to content
Permalink
Browse files

Initial commit to extract crypto-primitives to new crate

  • Loading branch information...
Pratyush committed Sep 25, 2019
1 parent 502d105 commit 80981ecab5ec30058cbe2d839cbcb79fbfd48cc3
Showing with 728 additions and 270 deletions.
  1. +1 −1 Cargo.toml
  2. +0 −10 algebra/src/bytes.rs
  3. +29 −0 crypto-primitives/Cargo.toml
  4. 0 dpc/src/gadgets/commitment/blake2s.rs → crypto-primitives/src/commitment/blake2s/constraints.rs
  5. 0 dpc/src/crypto_primitives/commitment/blake2s.rs → crypto-primitives/src/commitment/blake2s/mod.rs
  6. +1 −5 dpc/src/gadgets/commitment/mod.rs → crypto-primitives/src/commitment/constraints.rs
  7. +6 −7 ...dgets/commitment/injective_map.rs → crypto-primitives/src/commitment/injective_map/constraints.rs
  8. +4 −1 ...to_primitives/commitment/injective_map.rs → crypto-primitives/src/commitment/injective_map/mod.rs
  9. +5 −0 {dpc/src/crypto_primitives → crypto-primitives/src}/commitment/mod.rs
  10. +7 −9 dpc/src/gadgets/commitment/pedersen.rs → crypto-primitives/src/commitment/pedersen/constraints.rs
  11. +5 −2 dpc/src/crypto_primitives/commitment/pedersen.rs → crypto-primitives/src/commitment/pedersen/mod.rs
  12. +1 −4 dpc/src/gadgets/crh/mod.rs → crypto-primitives/src/crh/constraints.rs
  13. +7 −7 dpc/src/gadgets/crh/injective_map.rs → crypto-primitives/src/crh/injective_map/constraints.rs
  14. +5 −1 dpc/src/crypto_primitives/crh/injective_map.rs → crypto-primitives/src/crh/injective_map/mod.rs
  15. +7 −0 {dpc/src/crypto_primitives → crypto-primitives/src}/crh/mod.rs
  16. +8 −9 dpc/src/gadgets/crh/pedersen.rs → crypto-primitives/src/crh/pedersen/constraints.rs
  17. +5 −1 dpc/src/crypto_primitives/crh/pedersen.rs → crypto-primitives/src/crh/pedersen/mod.rs
  18. +8 −0 dpc/src/crypto_primitives/mod.rs → crypto-primitives/src/lib.rs
  19. +2 −2 dpc/src/gadgets/mht/mod.rs → crypto-primitives/src/mht/constraints-old.rs
  20. +353 −0 crypto-primitives/src/mht/constraints.rs
  21. +187 −179 {dpc/src/crypto_primitives → crypto-primitives/src}/mht/mod.rs
  22. +1 −3 dpc/src/gadgets/verifier/mod.rs → crypto-primitives/src/nizk/constraints.rs
  23. +1 −1 dpc/src/gadgets/verifier/gm17.rs → crypto-primitives/src/nizk/gm17/constraints.rs
  24. +3 −0 dpc/src/crypto_primitives/nizk/gm17.rs → crypto-primitives/src/nizk/gm17/mod.rs
  25. +9 −2 {dpc/src/crypto_primitives → crypto-primitives/src}/nizk/mod.rs
  26. +4 −5 dpc/src/gadgets/prf/blake2s.rs → crypto-primitives/src/prf/blake2s/constraints.rs
  27. +4 −1 dpc/src/crypto_primitives/prf/blake2s.rs → crypto-primitives/src/prf/blake2s/mod.rs
  28. +1 −3 dpc/src/gadgets/prf/mod.rs → crypto-primitives/src/prf/constraints.rs
  29. +6 −1 {dpc/src/crypto_primitives → crypto-primitives/src}/prf/mod.rs
  30. +1 −3 dpc/src/gadgets/signature/mod.rs → crypto-primitives/src/signature/constraints.rs
  31. +7 −1 {dpc/src/crypto_primitives → crypto-primitives/src}/signature/mod.rs
  32. +2 −2 dpc/src/gadgets/signature/schnorr.rs → crypto-primitives/src/signature/schnorr/constraints.rs
  33. +11 −9 dpc/src/crypto_primitives/signature/schnorr.rs → crypto-primitives/src/signature/schnorr/mod.rs
  34. +0 −1 dpc/src/lib.rs
  35. +37 −0 r1cs-std/src/bits/mod.rs
@@ -1,6 +1,6 @@
[workspace]

members = [ "algebra", "ff-fft", "r1cs-core", "r1cs-std", "gm17", "dpc", "bench-utils" ]
members = [ "algebra", "ff-fft", "r1cs-core", "r1cs-std", "gm17", "crypto-primitives", "dpc", "bench-utils" ]

[profile.release]
opt-level = 3
@@ -236,16 +236,6 @@ impl FromBytes for bool {
}
}

impl<T: ToBytes> ToBytes for [T] {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
for item in self {
item.write(&mut writer)?;
}
Ok(())
}
}

impl<T: ToBytes> ToBytes for Vec<T> {
#[inline]
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
@@ -0,0 +1,29 @@
[package]
name = "crypto-primitives"
version = "0.1.0"
authors = ["Pratyush Mishra <pratyushmishra@berkeley.edu>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
algebra = { path = "../algebra" }
r1cs-core = { path = "../r1cs-core", optional = true }
r1cs-std = { path = "../r1cs-std", optional = true }
gm17 = { path = "../gm17", optional = true }
bench-utils = { path = "../bench-utils" }

digest = "0.7"
blake2 = "0.7"

rand = { version = "0.7" }
derivative = "1"
rayon = "1"

[features]
r1cs = [ "r1cs-core", "r1cs-std" ]

[dev-dependencies]
criterion = "0.2"
rand_xorshift = { version = "0.2" }

File renamed without changes.
@@ -1,13 +1,9 @@
use crate::crypto_primitives::CommitmentScheme;
use crate::CommitmentScheme;
use algebra::Field;
use r1cs_core::{ConstraintSystem, SynthesisError};
use r1cs_std::prelude::*;
use std::fmt::Debug;

pub mod blake2s;
pub mod injective_map;
pub mod pedersen;

pub trait CommitmentGadget<C: CommitmentScheme, ConstraintF: Field> {
type OutputGadget: EqGadget<ConstraintF>
+ ToBytesGadget<ConstraintF>
@@ -1,20 +1,19 @@
use algebra::{Field, PrimeField};

use crate::gadgets::commitment::{
pedersen::{
use crate::commitment::{
injective_map::{InjectiveMap, PedersenCommCompressor},
pedersen::PedersenWindow,
pedersen::constraints::{
PedersenCommitmentGadget, PedersenCommitmentGadgetParameters, PedersenRandomnessGadget,
},
CommitmentGadget,
};
pub use crate::gadgets::crh::injective_map::InjectiveMapGadget;

pub use crate::crh::injective_map::constraints::InjectiveMapGadget;
use algebra::groups::Group;
use r1cs_core::{ConstraintSystem, SynthesisError};
use r1cs_std::{groups::GroupGadget, uint8::UInt8};

use crate::crypto_primitives::commitment::{
injective_map::{InjectiveMap, PedersenCommCompressor},
pedersen::PedersenWindow,
};
use std::marker::PhantomData;

pub struct PedersenCommitmentCompressorGadget<
@@ -6,9 +6,12 @@ use super::{
pedersen::{PedersenCommitment, PedersenParameters, PedersenRandomness, PedersenWindow},
CommitmentScheme,
};
pub use crate::crypto_primitives::crh::injective_map::InjectiveMap;
pub use crate::crh::injective_map::InjectiveMap;
use algebra::groups::Group;

#[cfg(feature = "r1cs")]
pub mod constraints;

pub struct PedersenCommCompressor<G: Group, I: InjectiveMap<G>, W: PedersenWindow> {
_group: PhantomData<G>,
_compressor: PhantomData<I>,
@@ -8,6 +8,11 @@ pub mod blake2s;
pub mod injective_map;
pub mod pedersen;

#[cfg(feature = "r1cs")]
pub mod constraints;
#[cfg(feature = "r1cs")]
pub use constraints::*;

use crate::Error;

pub trait CommitmentScheme {
@@ -1,11 +1,11 @@
use crate::crypto_primitives::{
use crate::{
commitment::pedersen::{PedersenCommitment, PedersenParameters, PedersenRandomness},
crh::pedersen::PedersenWindow,
};
use algebra::{to_bytes, Group, ToBytes};
use r1cs_core::{ConstraintSystem, SynthesisError};

use crate::gadgets::CommitmentGadget;
use crate::commitment::CommitmentGadget;
use algebra::fields::{Field, PrimeField};
use r1cs_std::prelude::*;
use std::{borrow::Borrow, marker::PhantomData};
@@ -171,14 +171,12 @@ mod test {
use algebra::UniformRand;

use crate::{
crypto_primitives::{
commitment::{
pedersen::{PedersenCommitment, PedersenRandomness},
CommitmentScheme,
},
crh::pedersen::PedersenWindow,
commitment::{
pedersen::{PedersenCommitment, PedersenRandomness, constraints::PedersenCommitmentGadget},
CommitmentScheme,
CommitmentGadget,
},
gadgets::commitment::{pedersen::PedersenCommitmentGadget, CommitmentGadget},
crh::pedersen::PedersenWindow,
};
use algebra::curves::{jubjub::JubJubProjective as JubJub, ProjectiveCurve};
use r1cs_core::ConstraintSystem;
@@ -7,12 +7,15 @@ use super::CommitmentScheme;
use algebra::{bytes::ToBytes, groups::Group, BitIterator, FpParameters, PrimeField};
use std::io::{Result as IoResult, Write};

pub use crate::crypto_primitives::crh::pedersen::PedersenWindow;
use crate::crypto_primitives::crh::{
pub use crate::crh::pedersen::PedersenWindow;
use crate::crh::{
pedersen::{PedersenCRH, PedersenParameters as PedersenCRHParameters},
FixedLengthCRH,
};

#[cfg(feature = "r1cs")]
pub mod constraints;

#[derive(Clone)]
pub struct PedersenParameters<G: Group> {
pub randomness_generator: Vec<G>,
@@ -1,14 +1,11 @@
use algebra::Field;
use std::fmt::Debug;

use crate::crypto_primitives::crh::FixedLengthCRH;
use crate::crh::FixedLengthCRH;
use r1cs_core::{ConstraintSystem, SynthesisError};

use r1cs_std::prelude::*;

pub mod injective_map;
pub mod pedersen;

pub trait FixedLengthCRHGadget<H: FixedLengthCRH, ConstraintF: Field>: Sized {
type OutputGadget: ConditionalEqGadget<ConstraintF>
+ EqGadget<ConstraintF>
@@ -1,9 +1,14 @@
use std::{fmt::Debug, marker::PhantomData};

use crate::gadgets::crh::{
pedersen::{PedersenCRHGadget, PedersenCRHGadgetParameters},
use crate::crh::{
FixedLengthCRHGadget,
injective_map::{InjectiveMap, PedersenCRHCompressor, TECompressor},
pedersen::{
PedersenWindow,
constraints::{PedersenCRHGadget, PedersenCRHGadgetParameters},
}
};

use algebra::{
curves::{
models::{ModelParameters, TEModelParameters},
@@ -19,11 +24,6 @@ use r1cs_std::{
prelude::*,
};

use crate::crypto_primitives::crh::{
injective_map::{InjectiveMap, PedersenCRHCompressor, TECompressor},
pedersen::PedersenWindow,
};

pub trait InjectiveMapGadget<G: Group, I: InjectiveMap<G>, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>>
{
type OutputGadget: EqGadget<ConstraintF>
@@ -1,4 +1,4 @@
use crate::crypto_primitives::CryptoError;
use crate::CryptoError;
use algebra::bytes::ToBytes;
use crate::Error;
use rand::Rng;
@@ -17,6 +17,10 @@ use algebra::{
groups::Group,
};


#[cfg(feature = "r1cs")]
pub mod constraints;

pub trait InjectiveMap<G: Group> {
type Output: ToBytes + Clone + Eq + Hash + Default + Debug;
fn injective_map(ge: &G) -> Result<Self::Output, CryptoError>;
@@ -7,6 +7,13 @@ pub mod pedersen;

use crate::Error;


#[cfg(feature = "r1cs")]
pub mod constraints;
#[cfg(feature = "r1cs")]
pub use constraints::*;


pub trait FixedLengthCRH {
const INPUT_SIZE_BITS: usize;
type Output: ToBytes + Clone + Eq + Hash + Default;
@@ -1,8 +1,8 @@
use algebra::Field;

use crate::{
crypto_primitives::crh::pedersen::{PedersenCRH, PedersenParameters, PedersenWindow},
gadgets::crh::FixedLengthCRHGadget,
use crate::crh::{
FixedLengthCRHGadget,
pedersen::{PedersenCRH, PedersenParameters, PedersenWindow},
};
use algebra::groups::Group;
use r1cs_core::{ConstraintSystem, SynthesisError};
@@ -114,12 +114,11 @@ mod test {
use algebra::fields::bls12_381::fr::Fr;
use rand::{thread_rng, Rng};

use crate::{
crypto_primitives::crh::{
pedersen::{PedersenCRH, PedersenWindow},
FixedLengthCRH,
},
gadgets::crh::{pedersen::PedersenCRHGadget, FixedLengthCRHGadget},
use crate::crh::{
pedersen::{PedersenCRH, PedersenWindow},
pedersen::constraints::PedersenCRHGadget,
FixedLengthCRH,
FixedLengthCRHGadget
};
use algebra::curves::{jubjub::JubJubProjective as JubJub, ProjectiveCurve};
use r1cs_core::ConstraintSystem;
@@ -6,9 +6,13 @@ use std::{
marker::PhantomData,
};

use super::FixedLengthCRH;
use crate::crh::FixedLengthCRH;
use algebra::groups::Group;


#[cfg(feature = "r1cs")]
pub mod constraints;

pub trait PedersenWindow: Clone {
const WINDOW_SIZE: usize;
const NUM_WINDOWS: usize;
@@ -1,3 +1,9 @@
#[macro_use]
extern crate bench_utils;

#[macro_use]
extern crate derivative;

pub mod commitment;
pub mod crh;
pub mod mht;
@@ -14,6 +20,8 @@ pub use self::{
signature::SignatureScheme,
};

pub type Error = Box<dyn std::error::Error>;

#[derive(Debug)]
pub enum CryptoError {
IncorrectInputLength(usize),
@@ -3,9 +3,9 @@ use r1cs_core::{ConstraintSystem, SynthesisError};
use r1cs_std::prelude::*;
use r1cs_std::boolean::AllocatedBit;

use crate::crypto_primitives::{mht::HashMembershipProof, CommitmentScheme, FixedLengthCRH};
use crate::{mht::HashMembershipProof, CommitmentScheme, FixedLengthCRH};

use crate::gadgets::{commitment::CommitmentGadget, crh::FixedLengthCRHGadget};
use crate::{commitment::CommitmentGadget, crh::FixedLengthCRHGadget};

use crate::ledger::{CommPath, Digest, LedgerDigest, LedgerWitness};

0 comments on commit 80981ec

Please sign in to comment.
You can’t perform that action at this time.