Browse Source

reorganize traits into a module; cut boilerplate code (#91)

use a default implementation for step circuit
main
Srinath Setty 2 years ago
committed by GitHub
parent
commit
35cb03f977
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 100 additions and 258 deletions
  1. +7
    -42
      benches/compressed-snark.rs
  2. +9
    -48
      benches/recursive-snark.rs
  3. +5
    -28
      examples/minroot.rs
  4. +10
    -42
      src/circuit.rs
  5. +18
    -79
      src/lib.rs
  6. +1
    -1
      src/nifs.rs
  7. +4
    -2
      src/spartan_with_ipa_pc/mod.rs
  8. +41
    -0
      src/traits/circuit.rs
  9. +3
    -14
      src/traits/mod.rs
  10. +2
    -2
      src/traits/snark.rs

+ 7
- 42
benches/compressed-snark.rs

@ -1,46 +1,19 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
use criterion::*;
use nova_snark::{ use nova_snark::{
traits::{Group, StepCircuit},
traits::{circuit::TrivialTestCircuit, Group},
CompressedSNARK, PublicParams, RecursiveSNARK, CompressedSNARK, PublicParams, RecursiveSNARK,
}; };
use std::time::Duration;
type G1 = pasta_curves::pallas::Point; type G1 = pasta_curves::pallas::Point;
type G2 = pasta_curves::vesta::Point; type G2 = pasta_curves::vesta::Point;
type S1 = nova_snark::spartan_with_ipa_pc::RelaxedR1CSSNARK<G1>; type S1 = nova_snark::spartan_with_ipa_pc::RelaxedR1CSSNARK<G1>;
type S2 = nova_snark::spartan_with_ipa_pc::RelaxedR1CSSNARK<G2>; type S2 = nova_snark::spartan_with_ipa_pc::RelaxedR1CSSNARK<G2>;
#[derive(Clone, Debug)]
struct TrivialTestCircuit<F: PrimeField> {
_p: PhantomData<F>,
}
impl<F> StepCircuit<F> for TrivialTestCircuit<F>
where
F: PrimeField,
{
fn synthesize<CS: ConstraintSystem<F>>(
&self,
_cs: &mut CS,
z: AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError> {
Ok(z)
}
fn compute(&self, z: &F) -> F {
*z
}
}
type C1 = TrivialTestCircuit<<G1 as Group>::Scalar>; type C1 = TrivialTestCircuit<<G1 as Group>::Scalar>;
type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>; type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>;
use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
use core::marker::PhantomData;
use criterion::*;
use ff::PrimeField;
use std::time::Duration;
fn compressed_snark_benchmark(c: &mut Criterion) { fn compressed_snark_benchmark(c: &mut Criterion) {
let num_samples = 10; let num_samples = 10;
bench_compressed_snark(c, num_samples); bench_compressed_snark(c, num_samples);
@ -64,12 +37,8 @@ fn bench_compressed_snark(c: &mut Criterion, num_samples: usize) {
// Produce public parameters // Produce public parameters
let pp = PublicParams::<G1, G2, C1, C2>::setup( let pp = PublicParams::<G1, G2, C1, C2>::setup(
TrivialTestCircuit {
_p: Default::default(),
},
TrivialTestCircuit {
_p: Default::default(),
},
TrivialTestCircuit::default(),
TrivialTestCircuit::default(),
); );
// produce a recursive SNARK // produce a recursive SNARK
@ -80,12 +49,8 @@ fn bench_compressed_snark(c: &mut Criterion, num_samples: usize) {
let res = RecursiveSNARK::prove_step( let res = RecursiveSNARK::prove_step(
&pp, &pp,
recursive_snark, recursive_snark,
TrivialTestCircuit {
_p: Default::default(),
},
TrivialTestCircuit {
_p: Default::default(),
},
TrivialTestCircuit::default(),
TrivialTestCircuit::default(),
<G1 as Group>::Scalar::one(), <G1 as Group>::Scalar::one(),
<G2 as Group>::Scalar::zero(), <G2 as Group>::Scalar::zero(),
); );

+ 9
- 48
benches/recursive-snark.rs

@ -1,44 +1,17 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
use criterion::*;
use nova_snark::{ use nova_snark::{
traits::{Group, StepCircuit},
traits::{circuit::TrivialTestCircuit, Group},
PublicParams, RecursiveSNARK, PublicParams, RecursiveSNARK,
}; };
use std::time::Duration;
type G1 = pasta_curves::pallas::Point; type G1 = pasta_curves::pallas::Point;
type G2 = pasta_curves::vesta::Point; type G2 = pasta_curves::vesta::Point;
#[derive(Clone, Debug)]
struct TrivialTestCircuit<F: PrimeField> {
_p: PhantomData<F>,
}
impl<F> StepCircuit<F> for TrivialTestCircuit<F>
where
F: PrimeField,
{
fn synthesize<CS: ConstraintSystem<F>>(
&self,
_cs: &mut CS,
z: AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError> {
Ok(z)
}
fn compute(&self, z: &F) -> F {
*z
}
}
type C1 = TrivialTestCircuit<<G1 as Group>::Scalar>; type C1 = TrivialTestCircuit<<G1 as Group>::Scalar>;
type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>; type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>;
use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
use core::marker::PhantomData;
use criterion::*;
use ff::PrimeField;
use std::time::Duration;
fn recursive_snark_benchmark(c: &mut Criterion) { fn recursive_snark_benchmark(c: &mut Criterion) {
let num_samples = 10; let num_samples = 10;
bench_recursive_snark(c, num_samples); bench_recursive_snark(c, num_samples);
@ -62,12 +35,8 @@ fn bench_recursive_snark(c: &mut Criterion, num_samples: usize) {
// Produce public parameters // Produce public parameters
let pp = PublicParams::<G1, G2, C1, C2>::setup( let pp = PublicParams::<G1, G2, C1, C2>::setup(
TrivialTestCircuit {
_p: Default::default(),
},
TrivialTestCircuit {
_p: Default::default(),
},
TrivialTestCircuit::default(),
TrivialTestCircuit::default(),
); );
// Bench time to produce a recursive SNARK; // Bench time to produce a recursive SNARK;
@ -81,12 +50,8 @@ fn bench_recursive_snark(c: &mut Criterion, num_samples: usize) {
let res = RecursiveSNARK::prove_step( let res = RecursiveSNARK::prove_step(
&pp, &pp,
recursive_snark, recursive_snark,
TrivialTestCircuit {
_p: Default::default(),
},
TrivialTestCircuit {
_p: Default::default(),
},
TrivialTestCircuit::default(),
TrivialTestCircuit::default(),
<G1 as Group>::Scalar::one(), <G1 as Group>::Scalar::one(),
<G2 as Group>::Scalar::zero(), <G2 as Group>::Scalar::zero(),
); );
@ -112,12 +77,8 @@ fn bench_recursive_snark(c: &mut Criterion, num_samples: usize) {
assert!(RecursiveSNARK::prove_step( assert!(RecursiveSNARK::prove_step(
black_box(&pp), black_box(&pp),
black_box(recursive_snark.clone()), black_box(recursive_snark.clone()),
black_box(TrivialTestCircuit {
_p: Default::default(),
}),
black_box(TrivialTestCircuit {
_p: Default::default(),
}),
black_box(TrivialTestCircuit::default()),
black_box(TrivialTestCircuit::default()),
black_box(<G1 as Group>::Scalar::zero()), black_box(<G1 as Group>::Scalar::zero()),
black_box(<G2 as Group>::Scalar::zero()), black_box(<G2 as Group>::Scalar::zero()),
) )

+ 5
- 28
examples/minroot.rs

@ -12,11 +12,13 @@ use neptune::{
Strength, Strength,
}; };
use nova_snark::{ use nova_snark::{
traits::{Group, StepCircuit},
traits::{
circuit::{StepCircuit, TrivialTestCircuit},
Group,
},
CompressedSNARK, PublicParams, RecursiveSNARK, CompressedSNARK, PublicParams, RecursiveSNARK,
}; };
use num_bigint::BigUint; use num_bigint::BigUint;
use std::marker::PhantomData;
use std::time::Instant; use std::time::Instant;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -183,9 +185,7 @@ fn main() {
pc: pc.clone(), pc: pc.clone(),
}; };
let circuit_secondary = TrivialTestCircuit {
_p: Default::default(),
};
let circuit_secondary = TrivialTestCircuit::default();
println!("Nova-based VDF with MinRoot delay function"); println!("Nova-based VDF with MinRoot delay function");
println!("=========================================="); println!("==========================================");
@ -299,26 +299,3 @@ fn main() {
); );
assert!(res.is_ok()); assert!(res.is_ok());
} }
// A trivial test circuit that we use on the secondary curve
#[derive(Clone, Debug)]
struct TrivialTestCircuit<F: PrimeField> {
_p: PhantomData<F>,
}
impl<F> StepCircuit<F> for TrivialTestCircuit<F>
where
F: PrimeField,
{
fn synthesize<CS: ConstraintSystem<F>>(
&self,
_cs: &mut CS,
z: AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError> {
Ok(z)
}
fn compute(&self, z: &F) -> F {
*z
}
}

+ 10
- 42
src/circuit.rs

@ -16,7 +16,7 @@ use super::{
}, },
}, },
r1cs::{R1CSInstance, RelaxedR1CSInstance}, r1cs::{R1CSInstance, RelaxedR1CSInstance},
traits::{Group, HashFuncCircuitTrait, HashFuncConstantsCircuit, StepCircuit},
traits::{circuit::StepCircuit, Group, HashFuncCircuitTrait, HashFuncConstantsCircuit},
}; };
use bellperson::{ use bellperson::{
gadgets::{ gadgets::{
@ -355,32 +355,8 @@ mod tests {
use crate::{ use crate::{
bellperson::r1cs::{NovaShape, NovaWitness}, bellperson::r1cs::{NovaShape, NovaWitness},
poseidon::PoseidonConstantsCircuit, poseidon::PoseidonConstantsCircuit,
traits::HashFuncConstantsTrait,
traits::{circuit::TrivialTestCircuit, HashFuncConstantsTrait},
}; };
use ff::PrimeField;
use std::marker::PhantomData;
#[derive(Clone)]
struct TestCircuit<F: PrimeField> {
_p: PhantomData<F>,
}
impl<F> StepCircuit<F> for TestCircuit<F>
where
F: PrimeField,
{
fn synthesize<CS: ConstraintSystem<F>>(
&self,
_cs: &mut CS,
z: AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError> {
Ok(z)
}
fn compute(&self, z: &F) -> F {
*z
}
}
#[test] #[test]
fn test_verification_circuit() { fn test_verification_circuit() {
@ -391,13 +367,11 @@ mod tests {
let ro_consts2: HashFuncConstantsCircuit<G1> = PoseidonConstantsCircuit::new(); let ro_consts2: HashFuncConstantsCircuit<G1> = PoseidonConstantsCircuit::new();
// Initialize the shape and gens for the primary // Initialize the shape and gens for the primary
let circuit1: NIFSVerifierCircuit<G2, TestCircuit<<G2 as Group>::Base>> =
let circuit1: NIFSVerifierCircuit<G2, TrivialTestCircuit<<G2 as Group>::Base>> =
NIFSVerifierCircuit::new( NIFSVerifierCircuit::new(
params1.clone(), params1.clone(),
None, None,
TestCircuit {
_p: Default::default(),
},
TrivialTestCircuit::default(),
ro_consts1.clone(), ro_consts1.clone(),
); );
let mut cs: ShapeCS<G1> = ShapeCS::new(); let mut cs: ShapeCS<G1> = ShapeCS::new();
@ -406,13 +380,11 @@ mod tests {
assert_eq!(cs.num_constraints(), 20584); assert_eq!(cs.num_constraints(), 20584);
// Initialize the shape and gens for the secondary // Initialize the shape and gens for the secondary
let circuit2: NIFSVerifierCircuit<G1, TestCircuit<<G1 as Group>::Base>> =
let circuit2: NIFSVerifierCircuit<G1, TrivialTestCircuit<<G1 as Group>::Base>> =
NIFSVerifierCircuit::new( NIFSVerifierCircuit::new(
params2.clone(), params2.clone(),
None, None,
TestCircuit {
_p: Default::default(),
},
TrivialTestCircuit::default(),
ro_consts2.clone(), ro_consts2.clone(),
); );
let mut cs: ShapeCS<G2> = ShapeCS::new(); let mut cs: ShapeCS<G2> = ShapeCS::new();
@ -425,13 +397,11 @@ mod tests {
let mut cs1: SatisfyingAssignment<G1> = SatisfyingAssignment::new(); let mut cs1: SatisfyingAssignment<G1> = SatisfyingAssignment::new();
let inputs1: NIFSVerifierCircuitInputs<G2> = let inputs1: NIFSVerifierCircuitInputs<G2> =
NIFSVerifierCircuitInputs::new(shape2.get_digest(), zero1, zero1, None, None, None, None); NIFSVerifierCircuitInputs::new(shape2.get_digest(), zero1, zero1, None, None, None, None);
let circuit1: NIFSVerifierCircuit<G2, TestCircuit<<G2 as Group>::Base>> =
let circuit1: NIFSVerifierCircuit<G2, TrivialTestCircuit<<G2 as Group>::Base>> =
NIFSVerifierCircuit::new( NIFSVerifierCircuit::new(
params1, params1,
Some(inputs1), Some(inputs1),
TestCircuit {
_p: Default::default(),
},
TrivialTestCircuit::default(),
ro_consts1, ro_consts1,
); );
let _ = circuit1.synthesize(&mut cs1); let _ = circuit1.synthesize(&mut cs1);
@ -451,13 +421,11 @@ mod tests {
Some(inst1), Some(inst1),
None, None,
); );
let circuit: NIFSVerifierCircuit<G1, TestCircuit<<G1 as Group>::Base>> =
let circuit: NIFSVerifierCircuit<G1, TrivialTestCircuit<<G1 as Group>::Base>> =
NIFSVerifierCircuit::new( NIFSVerifierCircuit::new(
params2, params2,
Some(inputs2), Some(inputs2),
TestCircuit {
_p: Default::default(),
},
TrivialTestCircuit::default(),
ro_consts2, ro_consts2,
); );
let _ = circuit.synthesize(&mut cs2); let _ = circuit.synthesize(&mut cs2);

+ 18
- 79
src/lib.rs

@ -16,7 +16,6 @@ mod r1cs;
pub mod errors; pub mod errors;
pub mod gadgets; pub mod gadgets;
pub mod pasta; pub mod pasta;
pub mod snark;
pub mod spartan_with_ipa_pc; pub mod spartan_with_ipa_pc;
pub mod traits; pub mod traits;
@ -36,10 +35,9 @@ use nifs::NIFS;
use r1cs::{ use r1cs::{
R1CSGens, R1CSInstance, R1CSShape, R1CSWitness, RelaxedR1CSInstance, RelaxedR1CSWitness, R1CSGens, R1CSInstance, R1CSShape, R1CSWitness, RelaxedR1CSInstance, RelaxedR1CSWitness,
}; };
use snark::RelaxedR1CSSNARKTrait;
use traits::{ use traits::{
AbsorbInROTrait, Group, HashFuncConstants, HashFuncConstantsCircuit, HashFuncConstantsTrait,
HashFuncTrait, StepCircuit,
circuit::StepCircuit, snark::RelaxedR1CSSNARKTrait, AbsorbInROTrait, Group, HashFuncConstants,
HashFuncConstantsCircuit, HashFuncConstantsTrait, HashFuncTrait,
}; };
/// A type that holds public parameters of Nova /// A type that holds public parameters of Nova
@ -665,32 +663,11 @@ mod tests {
type S1 = spartan_with_ipa_pc::RelaxedR1CSSNARK<G1>; type S1 = spartan_with_ipa_pc::RelaxedR1CSSNARK<G1>;
type S2 = spartan_with_ipa_pc::RelaxedR1CSSNARK<G2>; type S2 = spartan_with_ipa_pc::RelaxedR1CSSNARK<G2>;
use ::bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError}; use ::bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
use core::marker::PhantomData;
use ff::PrimeField; use ff::PrimeField;
use std::marker::PhantomData;
use traits::circuit::TrivialTestCircuit;
#[derive(Clone, Debug)]
struct TrivialTestCircuit<F: PrimeField> {
_p: PhantomData<F>,
}
impl<F> StepCircuit<F> for TrivialTestCircuit<F>
where
F: PrimeField,
{
fn synthesize<CS: ConstraintSystem<F>>(
&self,
_cs: &mut CS,
z: AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError> {
Ok(z)
}
fn compute(&self, z: &F) -> F {
*z
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
struct CubicCircuit<F: PrimeField> { struct CubicCircuit<F: PrimeField> {
_p: PhantomData<F>, _p: PhantomData<F>,
} }
@ -743,14 +720,7 @@ mod tests {
G2, G2,
TrivialTestCircuit<<G1 as Group>::Scalar>, TrivialTestCircuit<<G1 as Group>::Scalar>,
TrivialTestCircuit<<G2 as Group>::Scalar>, TrivialTestCircuit<<G2 as Group>::Scalar>,
>::setup(
TrivialTestCircuit {
_p: Default::default(),
},
TrivialTestCircuit {
_p: Default::default(),
},
);
>::setup(TrivialTestCircuit::default(), TrivialTestCircuit::default());
let num_steps = 1; let num_steps = 1;
@ -758,12 +728,8 @@ mod tests {
let res = RecursiveSNARK::prove_step( let res = RecursiveSNARK::prove_step(
&pp, &pp,
None, None,
TrivialTestCircuit {
_p: Default::default(),
},
TrivialTestCircuit {
_p: Default::default(),
},
TrivialTestCircuit::default(),
TrivialTestCircuit::default(),
<G1 as Group>::Scalar::zero(), <G1 as Group>::Scalar::zero(),
<G2 as Group>::Scalar::zero(), <G2 as Group>::Scalar::zero(),
); );
@ -782,12 +748,8 @@ mod tests {
#[test] #[test]
fn test_ivc_nontrivial() { fn test_ivc_nontrivial() {
let circuit_primary = TrivialTestCircuit {
_p: Default::default(),
};
let circuit_secondary = CubicCircuit {
_p: Default::default(),
};
let circuit_primary = TrivialTestCircuit::default();
let circuit_secondary = CubicCircuit::default();
// produce public parameters // produce public parameters
let pp = PublicParams::< let pp = PublicParams::<
@ -852,10 +814,7 @@ mod tests {
assert_eq!(zn_primary, <G1 as Group>::Scalar::one()); assert_eq!(zn_primary, <G1 as Group>::Scalar::one());
let mut zn_secondary_direct = <G2 as Group>::Scalar::zero(); let mut zn_secondary_direct = <G2 as Group>::Scalar::zero();
for _i in 0..num_steps { for _i in 0..num_steps {
zn_secondary_direct = CubicCircuit {
_p: Default::default(),
}
.compute(&zn_secondary_direct);
zn_secondary_direct = CubicCircuit::default().compute(&zn_secondary_direct);
} }
assert_eq!(zn_secondary, zn_secondary_direct); assert_eq!(zn_secondary, zn_secondary_direct);
assert_eq!(zn_secondary, <G2 as Group>::Scalar::from(2460515u64)); assert_eq!(zn_secondary, <G2 as Group>::Scalar::from(2460515u64));
@ -863,12 +822,8 @@ mod tests {
#[test] #[test]
fn test_ivc_nontrivial_with_compression() { fn test_ivc_nontrivial_with_compression() {
let circuit_primary = TrivialTestCircuit {
_p: Default::default(),
};
let circuit_secondary = CubicCircuit {
_p: Default::default(),
};
let circuit_primary = TrivialTestCircuit::default();
let circuit_secondary = CubicCircuit::default();
// produce public parameters // produce public parameters
let pp = PublicParams::< let pp = PublicParams::<
@ -921,10 +876,7 @@ mod tests {
assert_eq!(zn_primary, <G1 as Group>::Scalar::one()); assert_eq!(zn_primary, <G1 as Group>::Scalar::one());
let mut zn_secondary_direct = <G2 as Group>::Scalar::zero(); let mut zn_secondary_direct = <G2 as Group>::Scalar::zero();
for _i in 0..num_steps { for _i in 0..num_steps {
zn_secondary_direct = CubicCircuit {
_p: Default::default(),
}
.compute(&zn_secondary_direct);
zn_secondary_direct = CubicCircuit::default().compute(&zn_secondary_direct);
} }
assert_eq!(zn_secondary, zn_secondary_direct); assert_eq!(zn_secondary, zn_secondary_direct);
assert_eq!(zn_secondary, <G2 as Group>::Scalar::from(2460515u64)); assert_eq!(zn_secondary, <G2 as Group>::Scalar::from(2460515u64));
@ -1027,9 +979,7 @@ mod tests {
y: <G1 as Group>::Scalar::zero(), y: <G1 as Group>::Scalar::zero(),
}; };
let circuit_secondary = TrivialTestCircuit {
_p: Default::default(),
};
let circuit_secondary = TrivialTestCircuit::default();
// produce public parameters // produce public parameters
let pp = PublicParams::< let pp = PublicParams::<
@ -1093,14 +1043,7 @@ mod tests {
G2, G2,
TrivialTestCircuit<<G1 as Group>::Scalar>, TrivialTestCircuit<<G1 as Group>::Scalar>,
CubicCircuit<<G2 as Group>::Scalar>, CubicCircuit<<G2 as Group>::Scalar>,
>::setup(
TrivialTestCircuit {
_p: Default::default(),
},
CubicCircuit {
_p: Default::default(),
},
);
>::setup(TrivialTestCircuit::default(), CubicCircuit::default());
let num_steps = 1; let num_steps = 1;
@ -1108,12 +1051,8 @@ mod tests {
let res = RecursiveSNARK::prove_step( let res = RecursiveSNARK::prove_step(
&pp, &pp,
None, None,
TrivialTestCircuit {
_p: Default::default(),
},
CubicCircuit {
_p: Default::default(),
},
TrivialTestCircuit::default(),
CubicCircuit::default(),
<G1 as Group>::Scalar::one(), <G1 as Group>::Scalar::one(),
<G2 as Group>::Scalar::zero(), <G2 as Group>::Scalar::zero(),
); );

+ 1
- 1
src/nifs.rs

@ -8,7 +8,7 @@ use super::r1cs::{
R1CSGens, R1CSInstance, R1CSShape, R1CSWitness, RelaxedR1CSInstance, RelaxedR1CSWitness, R1CSGens, R1CSInstance, R1CSShape, R1CSWitness, RelaxedR1CSInstance, RelaxedR1CSWitness,
}; };
use super::traits::{AbsorbInROTrait, Group, HashFuncTrait}; use super::traits::{AbsorbInROTrait, Group, HashFuncTrait};
use std::marker::PhantomData;
use core::marker::PhantomData;
/// A SNARK that holds the proof of a step of an incremental computation /// A SNARK that holds the proof of a step of an incremental computation
#[allow(clippy::upper_case_acronyms)] #[allow(clippy::upper_case_acronyms)]

+ 4
- 2
src/spartan_with_ipa_pc/mod.rs

@ -8,8 +8,10 @@ use super::{
commitments::CommitGens, commitments::CommitGens,
errors::NovaError, errors::NovaError,
r1cs::{R1CSGens, R1CSShape, RelaxedR1CSInstance, RelaxedR1CSWitness}, r1cs::{R1CSGens, R1CSShape, RelaxedR1CSInstance, RelaxedR1CSWitness},
snark::{ProverKeyTrait, RelaxedR1CSSNARKTrait, VerifierKeyTrait},
traits::{AppendToTranscriptTrait, ChallengeTrait, Group},
traits::{
snark::{ProverKeyTrait, RelaxedR1CSSNARKTrait, VerifierKeyTrait},
AppendToTranscriptTrait, ChallengeTrait, Group,
},
}; };
use core::cmp::max; use core::cmp::max;
use ff::Field; use ff::Field;

+ 41
- 0
src/traits/circuit.rs

@ -0,0 +1,41 @@
//! This module defines traits that a step function must implement
use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
use core::marker::PhantomData;
use ff::PrimeField;
/// A helper trait for a step of the incremental computation (i.e., circuit for F)
pub trait StepCircuit<F: PrimeField>: Send + Sync + Clone {
/// Sythesize the circuit for a computation step and return variable
/// that corresponds to the output of the step z_{i+1}
fn synthesize<CS: ConstraintSystem<F>>(
&self,
cs: &mut CS,
z: AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError>;
/// Execute the circuit for a computation step and return output
fn compute(&self, z: &F) -> F;
}
/// A trivial step circuit that simply returns the input
#[derive(Clone, Debug, Default)]
pub struct TrivialTestCircuit<F: PrimeField> {
_p: PhantomData<F>,
}
impl<F> StepCircuit<F> for TrivialTestCircuit<F>
where
F: PrimeField,
{
fn synthesize<CS: ConstraintSystem<F>>(
&self,
_cs: &mut CS,
z: AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError> {
Ok(z)
}
fn compute(&self, z: &F) -> F {
*z
}
}

src/traits.rs → src/traits/mod.rs

@ -175,20 +175,6 @@ impl ScalarMul for T where T: Mul
pub trait ScalarMulOwned<Rhs, Output = Self>: for<'r> ScalarMul<&'r Rhs, Output> {} pub trait ScalarMulOwned<Rhs, Output = Self>: for<'r> ScalarMul<&'r Rhs, Output> {}
impl<T, Rhs, Output> ScalarMulOwned<Rhs, Output> for T where T: for<'r> ScalarMul<&'r Rhs, Output> {} impl<T, Rhs, Output> ScalarMulOwned<Rhs, Output> for T where T: for<'r> ScalarMul<&'r Rhs, Output> {}
/// A helper trait for a step of the incremental computation (i.e., circuit for F)
pub trait StepCircuit<F: PrimeField>: Send + Sync + Clone {
/// Sythesize the circuit for a computation step and return variable
/// that corresponds to the output of the step z_{i+1}
fn synthesize<CS: ConstraintSystem<F>>(
&self,
cs: &mut CS,
z: AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError>;
/// Execute the circuit for a computation step and return output
fn compute(&self, z: &F) -> F;
}
impl<F: PrimeField> AppendToTranscriptTrait for F { impl<F: PrimeField> AppendToTranscriptTrait for F {
fn append_to_transcript(&self, label: &'static [u8], transcript: &mut Transcript) { fn append_to_transcript(&self, label: &'static [u8], transcript: &mut Transcript) {
transcript.append_message(label, self.to_repr().as_ref()); transcript.append_message(label, self.to_repr().as_ref());
@ -202,3 +188,6 @@ impl AppendToTranscriptTrait for [F] {
} }
} }
} }
pub mod circuit;
pub mod snark;

src/snark.rs → src/traits/snark.rs

@ -1,5 +1,5 @@
//! A collection of traits that define the behavior of a zkSNARK for RelaxedR1CS
use super::{
//! This module defines a collection of traits that define the behavior of a zkSNARK for RelaxedR1CS
use crate::{
errors::NovaError, errors::NovaError,
r1cs::{R1CSGens, R1CSShape, RelaxedR1CSInstance, RelaxedR1CSWitness}, r1cs::{R1CSGens, R1CSShape, RelaxedR1CSInstance, RelaxedR1CSWitness},
traits::Group, traits::Group,

Loading…
Cancel
Save