Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce generics for common traits #68

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ digest = "0.9"
ark-r1cs-std = { version = "^0.3.0", optional = true, default-features = false }
ark-snark = { version = "^0.3.0", default-features = false }

ark-nonnative-field = { version = "^0.3.0", optional = true, default-features = false }

rayon = { version = "1.0", optional = true }
derivative = { version = "2.0", features = ["use_core"] }
tracing = { version = "0.1", default-features = false, features = [ "attributes" ], optional = true }
Expand All @@ -39,10 +37,18 @@ default = ["std"]
std = [ "ark-ff/std", "ark-ec/std", "ark-std/std", "ark-relations/std" ]
print-trace = [ "ark-std/print-trace" ]
parallel = [ "std", "rayon", "ark-ec/parallel", "ark-std/parallel", "ark-ff/parallel" ]
r1cs = [ "ark-r1cs-std", "tracing", "ark-nonnative-field", "ark-sponge/r1cs" ]
r1cs = [ "ark-r1cs-std", "tracing", "ark-sponge/r1cs" ]

[dev-dependencies]
ark-ed-on-bls12-381 = { version = "^0.3.0", default-features = false, features = [ "r1cs" ] }
ark-bls12-377 = { version = "^0.3.0", default-features = false, features = [ "curve", "r1cs" ] }
ark-mnt4-298 = { version = "^0.3.0", default-features = false, features = [ "curve", "r1cs" ] }
ark-mnt6-298 = { version = "^0.3.0", default-features = false, features = [ "r1cs" ] }

[patch.crates-io]
ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std", branch = "reduce-generics", optional = true, default-features = false }
ark-ed-on-bls12-381 = { git = "https://github.com/arkworks-rs/curves", branch = "reduce-generics", default-features = false, features = [ "r1cs" ] }
ark-bls12-377 = { git = "https://github.com/arkworks-rs/curves", branch = "reduce-generics", default-features = false, features = [ "curve", "r1cs" ] }
ark-mnt4-298 = { git = "https://github.com/arkworks-rs/curves", branch = "reduce-generics", default-features = false, features = [ "curve", "r1cs" ] }
ark-mnt6-298 = { git = "https://github.com/arkworks-rs/curves", branch = "reduce-generics", default-features = false, features = [ "curve", "r1cs" ] }
ark-sponge = { git = "https://github.com/arkworks-rs/sponge", branch = "reduce-generics", default-features = false }
2 changes: 1 addition & 1 deletion cp-benches/benches/crypto_primitives/crh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate criterion;

use ark_crypto_primitives::crh::{
pedersen::{Window, CRH as PedersenCRH},
CRHScheme,
CRH,
};
use ark_ed_on_bls12_377::EdwardsProjective as Edwards;
use criterion::Criterion;
Expand Down
25 changes: 8 additions & 17 deletions src/commitment/blake2s/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ark_relations::r1cs::{Namespace, SynthesisError};

use crate::{
commitment::{blake2s, CommitmentGadget},
commitment::{blake2s, CommitmentWithGadget},
prf::blake2s::constraints::{evaluate_blake2s, OutputVar},
Vec,
};
Expand All @@ -16,15 +16,13 @@ pub struct ParametersVar;
#[derive(Clone)]
pub struct RandomnessVar<F: Field>(pub Vec<UInt8<F>>);

pub struct CommGadget;

impl<F: PrimeField> CommitmentGadget<blake2s::Commitment, F> for CommGadget {
impl<F: PrimeField> CommitmentWithGadget<F> for blake2s::Commitment {
type OutputVar = OutputVar<F>;
type ParametersVar = ParametersVar;
type RandomnessVar = RandomnessVar<F>;

#[tracing::instrument(target = "r1cs", skip(input, r))]
fn commit(
fn commit_gadget(
_: &Self::ParametersVar,
input: &[UInt8<F>],
r: &Self::RandomnessVar,
Expand Down Expand Up @@ -72,12 +70,10 @@ impl<ConstraintF: PrimeField> AllocVar<[u8; 32], ConstraintF> for RandomnessVar<
#[cfg(test)]
mod test {
use crate::commitment::{
blake2s::{
constraints::{CommGadget, RandomnessVar},
Commitment,
},
blake2s::{constraints::RandomnessVar, Commitment},
CommitmentGadget, CommitmentScheme,
};
use crate::Gadget;
use ark_ed_on_bls12_381::Fq as Fr;
use ark_r1cs_std::prelude::*;
use ark_relations::r1cs::ConstraintSystem;
Expand All @@ -92,7 +88,6 @@ mod test {
let rng = &mut ark_std::test_rng();

type TestCOMM = Commitment;
type TestCOMMGadget = CommGadget;

let mut randomness = [0u8; 32];
rng.fill(&mut randomness);
Expand All @@ -112,17 +107,13 @@ mod test {
let randomness_var = RandomnessVar(randomness_var);

let parameters_var =
<TestCOMMGadget as CommitmentGadget<TestCOMM, Fr>>::ParametersVar::new_witness(
<Gadget<TestCOMM> as CommitmentGadget<Fr>>::ParametersVar::new_witness(
ark_relations::ns!(cs, "gadget_parameters"),
|| Ok(&parameters),
)
.unwrap();
let result_var = <TestCOMMGadget as CommitmentGadget<TestCOMM, Fr>>::commit(
&parameters_var,
&input_var,
&randomness_var,
)
.unwrap();
let result_var =
Gadget::<TestCOMM>::commit(&parameters_var, &input_var, &randomness_var).unwrap();

for i in 0..32 {
assert_eq!(primitive_result[i], result_var.0[i].value().unwrap());
Expand Down
49 changes: 44 additions & 5 deletions src/commitment/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,59 @@ use ark_r1cs_std::prelude::*;
use ark_relations::r1cs::SynthesisError;
use core::fmt::Debug;

pub trait CommitmentGadget<C: CommitmentScheme, ConstraintF: Field> {
pub trait CommitmentWithGadget<ConstraintF: Field>: CommitmentScheme {
type OutputVar: EqGadget<ConstraintF>
+ ToBytesGadget<ConstraintF>
+ AllocVar<C::Output, ConstraintF>
+ AllocVar<Self::Output, ConstraintF>
+ R1CSVar<ConstraintF>
+ Clone
+ Sized
+ Debug;
type ParametersVar: AllocVar<C::Parameters, ConstraintF> + Clone;
type RandomnessVar: AllocVar<C::Randomness, ConstraintF> + Clone;
type ParametersVar: AllocVar<Self::Parameters, ConstraintF> + Clone;
type RandomnessVar: AllocVar<Self::Randomness, ConstraintF> + Clone;

fn commit(
fn commit_gadget(
parameters: &Self::ParametersVar,
input: &[UInt8<ConstraintF>],
r: &Self::RandomnessVar,
) -> Result<Self::OutputVar, SynthesisError>;
}

pub trait CommitmentGadget<ConstraintF: Field> {
type Native: CommitmentWithGadget<
ConstraintF,
OutputVar = Self::OutputVar,
ParametersVar = Self::ParametersVar,
RandomnessVar = Self::RandomnessVar,
>;
type OutputVar: EqGadget<ConstraintF>
+ ToBytesGadget<ConstraintF>
+ AllocVar<<Self::Native as CommitmentScheme>::Output, ConstraintF>
+ R1CSVar<ConstraintF>
+ Clone
+ Sized
+ Debug;
type ParametersVar: AllocVar<<Self::Native as CommitmentScheme>::Parameters, ConstraintF>
+ Clone;
type RandomnessVar: AllocVar<<Self::Native as CommitmentScheme>::Randomness, ConstraintF>
+ Clone;

fn commit(
parameters: &Self::ParametersVar,
input: &[UInt8<ConstraintF>],
r: &Self::RandomnessVar,
) -> Result<Self::OutputVar, SynthesisError> {
Self::Native::commit_gadget(parameters, input, r)
}
}

impl<C, ConstraintF> CommitmentGadget<ConstraintF> for crate::Gadget<C>
where
C: CommitmentWithGadget<ConstraintF>,
ConstraintF: Field,
{
type Native = C;
type OutputVar = C::OutputVar;
type ParametersVar = C::ParametersVar;
type RandomnessVar = C::RandomnessVar;
}
55 changes: 20 additions & 35 deletions src/commitment/injective_map/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,45 @@
use crate::commitment::{
injective_map::{InjectiveMap, PedersenCommCompressor},
pedersen::{
constraints::{CommGadget, ParametersVar, RandomnessVar},
Window,
use crate::{
commitment::{
injective_map::PedersenCommCompressor,
pedersen::{
constraints::{ParametersVar, RandomnessVar},
Commitment, Window,
},
CommitmentGadget,
},
Gadget,
};

pub use crate::crh::injective_map::constraints::InjectiveMapGadget;
use ark_ec::ProjectiveCurve;
use ark_ff::{Field, PrimeField};
use ark_r1cs_std::{
groups::{CurveVar, GroupOpsBounds},
groups::{CurveWithVar, GroupOpsBounds},
uint8::UInt8,
};
use ark_relations::r1cs::SynthesisError;

use ark_std::marker::PhantomData;

type ConstraintF<C> = <<C as ProjectiveCurve>::BaseField as Field>::BasePrimeField;

pub struct CommitmentCompressorGadget<C, I, W, GG, IG>
where
C: ProjectiveCurve,
I: InjectiveMap<C>,
W: Window,
GG: CurveVar<C, ConstraintF<C>>,
IG: InjectiveMapGadget<C, I, GG>,
for<'a> &'a GG: GroupOpsBounds<'a, C, GG>,
{
_compressor: PhantomData<I>,
_compressor_gadget: PhantomData<IG>,
_comm: PhantomData<CommGadget<C, GG, W>>,
}

impl<C, I, GG, IG, W>
crate::commitment::CommitmentGadget<PedersenCommCompressor<C, I, W>, ConstraintF<C>>
for CommitmentCompressorGadget<C, I, W, GG, IG>
impl<C, I, W> crate::commitment::CommitmentWithGadget<ConstraintF<C>>
for PedersenCommCompressor<C, I, W>
where
C: ProjectiveCurve,
I: InjectiveMap<C>,
GG: CurveVar<C, ConstraintF<C>>,
C: CurveWithVar<ConstraintF<C>>,
I: InjectiveMapGadget<C>,
ConstraintF<C>: PrimeField,
IG: InjectiveMapGadget<C, I, GG>,
W: Window,
for<'a> &'a GG: GroupOpsBounds<'a, C, GG>,
for<'a> &'a C::Var: GroupOpsBounds<'a, C, C::Var>,
{
type OutputVar = IG::OutputVar;
type ParametersVar = ParametersVar<C, GG>;
type OutputVar = I::OutputVar;
type ParametersVar = ParametersVar<C>;
type RandomnessVar = RandomnessVar<ConstraintF<C>>;

fn commit(
fn commit_gadget(
parameters: &Self::ParametersVar,
input: &[UInt8<ConstraintF<C>>],
r: &Self::RandomnessVar,
) -> Result<Self::OutputVar, SynthesisError> {
let result = CommGadget::<C, GG, W>::commit(parameters, input, r)?;
IG::evaluate(&result)
let result = Gadget::<Commitment<C, W>>::commit(parameters, input, r)?;
I::evaluate(&result)
}
}
Loading