Skip to content

Commit

Permalink
Use CryptographicSponge as generic argument
Browse files Browse the repository at this point in the history
Resolves #87
  • Loading branch information
vlopes11 committed Mar 23, 2022
1 parent 903c741 commit f33232d
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 270 deletions.
35 changes: 18 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,35 @@ license = "MIT/Apache-2.0"
edition = "2018"

[dependencies]
ark-serialize = { version = "^0.2.0", default-features = false, features = [ "derive" ] }
ark-ff = { version = "^0.2.0", default-features = false }
ark-std = { version = "^0.2.0", default-features = false }
ark-poly = { version = "^0.2.0", default-features = false }
ark-relations = { version = "^0.2.0", default-features = false }
ark-serialize = { version = "0.3", default-features = false, features = [ "derive" ] }
ark-ff = { version = "0.3", default-features = false }
ark-std = { version = "0.3", default-features = false }
ark-poly = { version = "0.3", default-features = false }
ark-relations = { version = "0.3", default-features = false }
ark-poly-commit = { git = "https://github.com/arkworks-rs/poly-commit", branch = "constraints", default-features = false, features = [ "r1cs" ] }
ark-sponge = { version = "0.3", default-features = false }

rand_chacha = { version = "0.2.1", default-features = false }
rand_chacha = { version = "0.3", default-features = false }
rayon = { version = "1", optional = true }
digest = { version = "0.9" }
derivative = { version = "2", features = ["use_core"] }

ark-ec = { version = "^0.2.0", default-features = false }
ark-crypto-primitives = { version = "^0.2.0", default-features = false, features = [ "r1cs" ] }
ark-r1cs-std = { version = "^0.2.0", default-features = false }
ark-nonnative-field = { version = "^0.2.0", default-features = false }
ark-snark = { version = "^0.2.0", default-features = false }
ark-ec = { version = "0.3", default-features = false }
ark-crypto-primitives = { version = "0.3", default-features = false, features = [ "r1cs" ] }
ark-r1cs-std = { version = "0.3", default-features = false }
ark-nonnative-field = { version = "0.3", default-features = false }
ark-snark = { version = "0.3", default-features = false }
hashbrown = "0.9"
tracing = { version = "0.1", default-features = false, features = [ "attributes" ] }
tracing-subscriber = { version = "0.2", default-features = false, optional = true }

[dev-dependencies]
blake2 = { version = "0.9", default-features = false }
ark-bls12-381 = { version = "^0.2.0", default-features = false, features = [ "curve" ] }
ark-mnt4-298 = { version = "^0.2.0", default-features = false, features = ["r1cs", "curve"] }
ark-mnt6-298 = { version = "^0.2.0", default-features = false, features = ["r1cs"] }
ark-mnt4-753 = { version = "^0.2.0", default-features = false, features = ["r1cs", "curve"] }
ark-mnt6-753 = { version = "^0.2.0", default-features = false, features = ["r1cs"] }
ark-bls12-381 = { version = "0.3", default-features = false, features = [ "curve" ] }
ark-mnt4-298 = { version = "0.3", default-features = false, features = ["r1cs", "curve"] }
ark-mnt6-298 = { version = "0.3", default-features = false, features = ["r1cs"] }
ark-mnt4-753 = { version = "0.3", default-features = false, features = ["r1cs", "curve"] }
ark-mnt6-753 = { version = "0.3", default-features = false, features = ["r1cs"] }

[profile.release]
opt-level = 3
Expand Down Expand Up @@ -76,4 +77,4 @@ parallel = [ "std", "ark-ff/parallel", "ark-poly/parallel", "ark-std/parallel",
name = "marlin-benches"
path = "benches/bench.rs"
harness = false
required-features = ["std"]
required-features = ["std"]
18 changes: 11 additions & 7 deletions src/constraints/ahp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use ark_r1cs_std::{
ToBitsGadget, ToConstraintFieldGadget,
};
use ark_relations::r1cs::ConstraintSystemRef;
use ark_sponge::CryptographicSponge;
use hashbrown::{HashMap, HashSet};

#[derive(Clone)]
Expand Down Expand Up @@ -57,24 +58,27 @@ pub struct VerifierThirdMsgVar<TargetField: PrimeField, BaseField: PrimeField> {
pub struct AHPForR1CS<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
> where
PCG::VerifierKeyVar: ToConstraintFieldGadget<CF>,
PCG::CommitmentVar: ToConstraintFieldGadget<CF>,
{
field: PhantomData<F>,
constraint_field: PhantomData<CF>,
sponge: PhantomData<S>,
polynomial_commitment: PhantomData<PC>,
pc_check: PhantomData<PCG>,
}

impl<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
> AHPForR1CS<F, CF, PC, PCG>
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
> AHPForR1CS<F, CF, S, PC, PCG>
where
PCG::VerifierKeyVar: ToConstraintFieldGadget<CF>,
PCG::CommitmentVar: ToConstraintFieldGadget<CF>,
Expand Down Expand Up @@ -529,8 +533,8 @@ where
PR: FiatShamirRng<F, CF>,
R: FiatShamirRngVar<F, CF, PR>,
>(
index_pvk: &PreparedIndexVerifierKeyVar<F, CF, PC, PCG, PR, R>,
proof: &ProofVar<F, CF, PC, PCG>,
index_pvk: &PreparedIndexVerifierKeyVar<F, CF, S, PC, PCG, PR, R>,
proof: &ProofVar<F, CF, S, PC, PCG>,
state: &VerifierStateVar<F, CF>,
) -> Result<
(
Expand Down
116 changes: 67 additions & 49 deletions src/constraints/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ use ark_r1cs_std::{
R1CSVar, ToBytesGadget, ToConstraintFieldGadget,
};
use ark_relations::r1cs::{ConstraintSystemRef, Namespace};
use ark_sponge::CryptographicSponge;
use ark_std::borrow::Borrow;
use hashbrown::HashMap;

pub type UniversalSRS<F, PC> = <PC as PolynomialCommitment<F, DensePolynomial<F>>>::UniversalParams;
pub type UniversalSRS<F, PC, S> =
<PC as PolynomialCommitment<F, DensePolynomial<F>, S>>::UniversalParams;

pub struct IndexVerifierKeyVar<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
> {
pub cs: ConstraintSystemRef<CF>,
pub domain_h_size: u64,
Expand All @@ -40,9 +43,10 @@ pub struct IndexVerifierKeyVar<
impl<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
> IndexVerifierKeyVar<F, CF, PC, PCG>
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
> IndexVerifierKeyVar<F, CF, S, PC, PCG>
{
fn cs(&self) -> ConstraintSystemRef<CF> {
self.cs.clone()
Expand All @@ -52,9 +56,10 @@ impl<
impl<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
> AllocVar<IndexVerifierKey<F, PC>, CF> for IndexVerifierKeyVar<F, CF, PC, PCG>
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
> AllocVar<IndexVerifierKey<F, S, PC>, CF> for IndexVerifierKeyVar<F, CF, S, PC, PCG>
{
#[tracing::instrument(target = "r1cs", skip(cs, f))]
fn new_variable<T>(
Expand All @@ -63,7 +68,7 @@ impl<
mode: AllocationMode,
) -> Result<Self, SynthesisError>
where
T: Borrow<IndexVerifierKey<F, PC>>,
T: Borrow<IndexVerifierKey<F, S, PC>>,
{
let t = f()?;
let ivk = t.borrow();
Expand Down Expand Up @@ -117,9 +122,10 @@ impl<
impl<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
> ToBytesGadget<CF> for IndexVerifierKeyVar<F, CF, PC, PCG>
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
> ToBytesGadget<CF> for IndexVerifierKeyVar<F, CF, S, PC, PCG>
{
#[tracing::instrument(target = "r1cs", skip(self))]
fn to_bytes(&self) -> Result<Vec<UInt8<CF>>, SynthesisError> {
Expand All @@ -140,9 +146,10 @@ impl<
impl<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
> Clone for IndexVerifierKeyVar<F, CF, PC, PCG>
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
> Clone for IndexVerifierKeyVar<F, CF, S, PC, PCG>
{
fn clone(&self) -> Self {
Self {
Expand All @@ -160,9 +167,10 @@ impl<
impl<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
> IndexVerifierKeyVar<F, CF, PC, PCG>
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
> IndexVerifierKeyVar<F, CF, S, PC, PCG>
{
pub fn iter(&self) -> impl Iterator<Item = &PCG::CommitmentVar> {
self.index_comms.iter()
Expand All @@ -172,8 +180,9 @@ impl<
pub struct PreparedIndexVerifierKeyVar<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
PR: FiatShamirRng<F, CF>,
R: FiatShamirRngVar<F, CF, PR>,
> {
Expand All @@ -192,11 +201,12 @@ pub struct PreparedIndexVerifierKeyVar<
impl<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
PR: FiatShamirRng<F, CF>,
R: FiatShamirRngVar<F, CF, PR>,
> Clone for PreparedIndexVerifierKeyVar<F, CF, PC, PCG, PR, R>
> Clone for PreparedIndexVerifierKeyVar<F, CF, S, PC, PCG, PR, R>
{
fn clone(&self) -> Self {
PreparedIndexVerifierKeyVar {
Expand All @@ -213,24 +223,26 @@ impl<
}
}

impl<F, CF, PC, PCG, PR, R> PreparedIndexVerifierKeyVar<F, CF, PC, PCG, PR, R>
impl<F, CF, S, PC, PCG, PR, R> PreparedIndexVerifierKeyVar<F, CF, S, PC, PCG, PR, R>
where
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
PR: FiatShamirRng<F, CF>,
R: FiatShamirRngVar<F, CF, PR>,
PCG::VerifierKeyVar: ToConstraintFieldGadget<CF>,
PCG::CommitmentVar: ToConstraintFieldGadget<CF>,
{
#[tracing::instrument(target = "r1cs", skip(vk))]
pub fn prepare(vk: &IndexVerifierKeyVar<F, CF, PC, PCG>) -> Result<Self, SynthesisError> {
pub fn prepare(vk: &IndexVerifierKeyVar<F, CF, S, PC, PCG>) -> Result<Self, SynthesisError> {
let cs = vk.cs();

let mut fs_rng_raw = PR::new();
fs_rng_raw
.absorb_bytes(&to_bytes![&MarlinVerifierVar::<F, CF, PC, PCG>::PROTOCOL_NAME].unwrap());
fs_rng_raw.absorb_bytes(
&to_bytes![&MarlinVerifierVar::<F, CF, S, PC, PCG>::PROTOCOL_NAME].unwrap(),
);

let index_vk_hash = {
let mut vk_hash_rng = PR::new();
Expand Down Expand Up @@ -280,13 +292,14 @@ where
}
}

impl<F, CF, PC, PCG, PR, R> AllocVar<PreparedIndexVerifierKey<F, PC>, CF>
for PreparedIndexVerifierKeyVar<F, CF, PC, PCG, PR, R>
impl<F, CF, S, PC, PCG, PR, R> AllocVar<PreparedIndexVerifierKey<F, S, PC>, CF>
for PreparedIndexVerifierKeyVar<F, CF, S, PC, PCG, PR, R>
where
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
PR: FiatShamirRng<F, CF>,
R: FiatShamirRngVar<F, CF, PR>,
PC::VerifierKey: ToConstraintField<CF>,
Expand All @@ -301,7 +314,7 @@ where
mode: AllocationMode,
) -> Result<Self, SynthesisError>
where
T: Borrow<PreparedIndexVerifierKey<F, PC>>,
T: Borrow<PreparedIndexVerifierKey<F, S, PC>>,
{
let t = f()?;
let obj = t.borrow();
Expand Down Expand Up @@ -342,8 +355,9 @@ where
};

let mut fs_rng_raw = PR::new();
fs_rng_raw
.absorb_bytes(&to_bytes![&MarlinVerifierVar::<F, CF, PC, PCG>::PROTOCOL_NAME].unwrap());
fs_rng_raw.absorb_bytes(
&to_bytes![&MarlinVerifierVar::<F, CF, S, PC, PCG>::PROTOCOL_NAME].unwrap(),
);

let fs_rng = {
let mut fs_rng = R::constant(cs.clone(), &fs_rng_raw);
Expand Down Expand Up @@ -379,8 +393,9 @@ where
pub struct ProofVar<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
> {
pub cs: ConstraintSystemRef<CF>,
pub commitments: Vec<Vec<PCG::CommitmentVar>>,
Expand All @@ -392,9 +407,10 @@ pub struct ProofVar<
impl<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
> ProofVar<F, CF, PC, PCG>
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
> ProofVar<F, CF, S, PC, PCG>
{
pub fn new(
cs: ConstraintSystemRef<CF>,
Expand All @@ -413,12 +429,13 @@ impl<
}
}

impl<F, CF, PC, PCG> AllocVar<Proof<F, PC>, CF> for ProofVar<F, CF, PC, PCG>
impl<F, CF, S, PC, PCG> AllocVar<Proof<F, S, PC>, CF> for ProofVar<F, CF, S, PC, PCG>
where
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S, BatchProof = DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
PC::VerifierKey: ToConstraintField<CF>,
PC::Commitment: ToConstraintField<CF>,
PCG::VerifierKeyVar: ToConstraintFieldGadget<CF>,
Expand All @@ -431,7 +448,7 @@ where
mode: AllocationMode,
) -> Result<Self, SynthesisError>
where
T: Borrow<Proof<F, PC>>,
T: Borrow<Proof<F, S, PC>>,
{
let ns = cs.into();
let cs = ns.cs();
Expand Down Expand Up @@ -534,9 +551,10 @@ where
impl<
F: PrimeField,
CF: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF>,
> Clone for ProofVar<F, CF, PC, PCG>
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
PCG: PCCheckVar<F, DensePolynomial<F>, PC, CF, S>,
> Clone for ProofVar<F, CF, S, PC, PCG>
{
fn clone(&self) -> Self {
ProofVar {
Expand Down
Loading

0 comments on commit f33232d

Please sign in to comment.