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
8 changes: 5 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,14 @@ 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-sponge = { git = "https://github.com/arkworks-rs/sponge", branch = "reduce-generics", default-features = false }
23 changes: 8 additions & 15 deletions src/commitment/blake2s/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ 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> CommitmentGadget<F> for blake2s::Commitment {
type OutputVar = OutputVar<F>;
type ParametersVar = ParametersVar;
type RandomnessVar = RandomnessVar<F>;
Expand Down Expand Up @@ -72,10 +70,7 @@ 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 ark_ed_on_bls12_381::Fq as Fr;
Expand All @@ -92,7 +87,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 @@ -111,13 +105,12 @@ mod test {
}
let randomness_var = RandomnessVar(randomness_var);

let parameters_var =
<TestCOMMGadget as CommitmentGadget<TestCOMM, Fr>>::ParametersVar::new_witness(
ark_relations::ns!(cs, "gadget_parameters"),
|| Ok(&parameters),
)
.unwrap();
let result_var = <TestCOMMGadget as CommitmentGadget<TestCOMM, Fr>>::commit(
let parameters_var = <TestCOMM as CommitmentGadget<Fr>>::ParametersVar::new_witness(
ark_relations::ns!(cs, "gadget_parameters"),
|| Ok(&parameters),
)
.unwrap();
let result_var = <TestCOMM as CommitmentGadget<Fr>>::commit(
&parameters_var,
&input_var,
&randomness_var,
Expand Down
8 changes: 4 additions & 4 deletions src/commitment/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use ark_r1cs_std::prelude::*;
use ark_relations::r1cs::SynthesisError;
use core::fmt::Debug;

pub trait CommitmentGadget<C: CommitmentScheme, ConstraintF: Field> {
pub trait CommitmentGadget<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(
parameters: &Self::ParametersVar,
Expand Down
45 changes: 13 additions & 32 deletions src/commitment/injective_map/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,41 @@
use crate::commitment::{
injective_map::{InjectiveMap, PedersenCommCompressor},
injective_map::PedersenCommCompressor,
pedersen::{
constraints::{CommGadget, ParametersVar, RandomnessVar},
Window,
constraints::{ParametersVar, RandomnessVar},
Commitment, Window,
},
};

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::CommitmentGadget<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(
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 = Commitment::<C, W>::commit(parameters, input, r)?;
I::evaluate(&result)
}
}
79 changes: 27 additions & 52 deletions src/commitment/pedersen/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,28 @@ use ark_ff::{
use ark_relations::r1cs::{Namespace, SynthesisError};

use ark_r1cs_std::prelude::*;
use core::{borrow::Borrow, marker::PhantomData};
use core::borrow::Borrow;

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

#[derive(Derivative)]
#[derivative(Clone(bound = "C: ProjectiveCurve, GG: CurveVar<C, ConstraintF<C>>"))]
pub struct ParametersVar<C: ProjectiveCurve, GG: CurveVar<C, ConstraintF<C>>>
where
for<'a> &'a GG: GroupOpsBounds<'a, C, GG>,
{
#[derivative(Clone(bound = "C: CurveWithVar<ConstraintF<C>>"))]
pub struct ParametersVar<C: CurveWithVar<ConstraintF<C>>> {
params: Parameters<C>,
#[doc(hidden)]
_group_var: PhantomData<GG>,
}

#[derive(Clone, Debug)]
pub struct RandomnessVar<F: Field>(Vec<UInt8<F>>);

pub struct CommGadget<C: ProjectiveCurve, GG: CurveVar<C, ConstraintF<C>>, W: Window>
impl<C, W> crate::commitment::CommitmentGadget<ConstraintF<C>> for Commitment<C, W>
where
for<'a> &'a GG: GroupOpsBounds<'a, C, GG>,
{
#[doc(hidden)]
_curve: PhantomData<*const C>,
#[doc(hidden)]
_group_var: PhantomData<*const GG>,
#[doc(hidden)]
_window: PhantomData<*const W>,
}

impl<C, GG, W> crate::commitment::CommitmentGadget<Commitment<C, W>, ConstraintF<C>>
for CommGadget<C, GG, W>
where
C: ProjectiveCurve,
GG: CurveVar<C, ConstraintF<C>>,
C: CurveWithVar<ConstraintF<C>>,
W: Window,
for<'a> &'a GG: GroupOpsBounds<'a, C, GG>,
for<'a> &'a C::Var: GroupOpsBounds<'a, C, C::Var>,
ConstraintF<C>: PrimeField,
{
type OutputVar = GG;
type ParametersVar = ParametersVar<C, GG>;
type OutputVar = C::Var;
type ParametersVar = ParametersVar<C>;
type RandomnessVar = RandomnessVar<ConstraintF<C>>;

#[tracing::instrument(target = "r1cs", skip(parameters, r))]
Expand Down Expand Up @@ -80,8 +61,10 @@ where
.flat_map(|byte| byte.to_bits_le().unwrap())
.collect();
let input_in_bits = input_in_bits.chunks(W::WINDOW_SIZE);
let mut result =
GG::precomputed_base_multiscalar_mul_le(&parameters.params.generators, input_in_bits)?;
let mut result = C::Var::precomputed_base_multiscalar_mul_le(
&parameters.params.generators,
input_in_bits,
)?;

// Compute h^r
let rand_bits: Vec<_> =
Expand All @@ -98,22 +81,18 @@ where
}
}

impl<C, GG> AllocVar<Parameters<C>, ConstraintF<C>> for ParametersVar<C, GG>
impl<C> AllocVar<Parameters<C>, ConstraintF<C>> for ParametersVar<C>
where
C: ProjectiveCurve,
GG: CurveVar<C, ConstraintF<C>>,
for<'a> &'a GG: GroupOpsBounds<'a, C, GG>,
C: CurveWithVar<ConstraintF<C>>,
for<'a> &'a C::Var: GroupOpsBounds<'a, C, C::Var>,
{
fn new_variable<T: Borrow<Parameters<C>>>(
_cs: impl Into<Namespace<ConstraintF<C>>>,
f: impl FnOnce() -> Result<T, SynthesisError>,
_mode: AllocationMode,
) -> Result<Self, SynthesisError> {
let params = f()?.borrow().clone();
Ok(ParametersVar {
params,
_group_var: PhantomData,
})
Ok(ParametersVar { params })
}
}

Expand Down Expand Up @@ -168,7 +147,6 @@ mod test {
let rng = &mut test_rng();

type TestCOMM = Commitment<JubJub, Window>;
type TestCOMMGadget = CommGadget<JubJub, EdwardsVar, Window>;

let randomness = Randomness(Fr::rand(rng));

Expand All @@ -181,20 +159,17 @@ mod test {
input_var.push(UInt8::new_witness(cs.clone(), || Ok(*input_byte)).unwrap());
}

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

let primitive_result = primitive_result;
assert_eq!(primitive_result, result_var.value().unwrap());
Expand Down
Loading