diff --git a/src/groth16/generator.rs b/src/groth16/generator.rs index c4c3f05..109bdfe 100644 --- a/src/groth16/generator.rs +++ b/src/groth16/generator.rs @@ -69,7 +69,13 @@ pub fn generate_parameters( } impl PublicConstraintSystem for KeypairAssembly { - fn alloc_input Result>(&mut self, f: F) -> Result { + fn alloc_input( + &mut self, + _: N, + f: F + ) -> Result + where NR: Into, N: FnOnce() -> NR, F: FnOnce() -> Result + { // In this context, we don't have an assignment. let _ = f(); @@ -85,7 +91,13 @@ pub fn generate_parameters( } impl ConstraintSystem for KeypairAssembly { - fn alloc Result>(&mut self, f: F) -> Result { + fn alloc( + &mut self, + _: N, + f: F + ) -> Result + where NR: Into, N: FnOnce() -> NR, F: FnOnce() -> Result + { // In this context, we don't have an assignment. let _ = f(); @@ -99,8 +111,9 @@ pub fn generate_parameters( Ok(Variable(Index::Aux(index))) } - fn enforce( + fn enforce, N: FnOnce() -> NR>( &mut self, + _: N, a: LinearCombination, b: LinearCombination, c: LinearCombination @@ -142,14 +155,15 @@ pub fn generate_parameters( }; // Allocate the "one" input variable - assembly.alloc_input(|| Ok(E::Fr::one()))?; + assembly.alloc_input(|| "", || Ok(E::Fr::one()))?; // Synthesize the circuit. circuit.synthesize(&mut assembly)?.synthesize(&mut assembly)?; // Input consistency constraints: x * 0 = 0 for i in 0..assembly.num_inputs { - assembly.enforce(LinearCombination::zero() + Variable(Index::Input(i)), + assembly.enforce(|| "", + LinearCombination::zero() + Variable(Index::Input(i)), LinearCombination::zero(), LinearCombination::zero()); } diff --git a/src/groth16/prover.rs b/src/groth16/prover.rs index ddc7bd7..a86d679 100644 --- a/src/groth16/prover.rs +++ b/src/groth16/prover.rs @@ -55,8 +55,14 @@ pub fn create_proof>( } impl PublicConstraintSystem for ProvingAssignment { - fn alloc_input Result>(&mut self, value: F) -> Result { - self.input_assignment.push(value()?); + fn alloc_input( + &mut self, + _: N, + f: F + ) -> Result + where NR: Into, N: FnOnce() -> NR, F: FnOnce() -> Result + { + self.input_assignment.push(f()?); self.b_input_density.add_element(); Ok(Variable(Index::Input(self.input_assignment.len() - 1))) @@ -64,16 +70,23 @@ pub fn create_proof>( } impl ConstraintSystem for ProvingAssignment { - fn alloc Result>(&mut self, value: F) -> Result { - self.aux_assignment.push(value()?); + fn alloc( + &mut self, + _: N, + f: F + ) -> Result + where NR: Into, N: FnOnce() -> NR, F: FnOnce() -> Result + { + self.aux_assignment.push(f()?); self.a_aux_density.add_element(); self.b_aux_density.add_element(); Ok(Variable(Index::Aux(self.aux_assignment.len() - 1))) } - fn enforce( + fn enforce, N: FnOnce() -> NR>( &mut self, + _: N, a: LinearCombination, b: LinearCombination, c: LinearCombination @@ -96,13 +109,14 @@ pub fn create_proof>( aux_assignment: vec![] }; - prover.alloc_input(|| Ok(E::Fr::one()))?; + prover.alloc_input(|| "", || Ok(E::Fr::one()))?; circuit.synthesize(&mut prover)?.synthesize(&mut prover)?; // Input consistency constraints: x * 0 = 0 for i in 0..prover.input_assignment.len() { - prover.enforce(LinearCombination::zero() + Variable(Index::Input(i)), + prover.enforce(|| "", + LinearCombination::zero() + Variable(Index::Input(i)), LinearCombination::zero(), LinearCombination::zero()); } diff --git a/src/groth16/verifier.rs b/src/groth16/verifier.rs index 6d23504..246e925 100644 --- a/src/groth16/verifier.rs +++ b/src/groth16/verifier.rs @@ -22,7 +22,13 @@ pub struct VerifierInput<'a, E: Engine> { } impl<'a, E: Engine> ConstraintSystem for VerifierInput<'a, E> { - fn alloc Result>(&mut self, f: F) -> Result { + fn alloc( + &mut self, + _: N, + f: F + ) -> Result + where NR: Into, N: FnOnce() -> NR, F: FnOnce() -> Result + { // Run the function for calculating the allocation but ignore the output, // since we don't care about the assignment of auxillary variables during // verification. @@ -34,8 +40,9 @@ impl<'a, E: Engine> ConstraintSystem for VerifierInput<'a, E> { Ok(Variable(Index::Aux(index))) } - fn enforce( + fn enforce, N: FnOnce() -> NR>( &mut self, + _: N, _: LinearCombination, _: LinearCombination, _: LinearCombination @@ -51,12 +58,19 @@ impl<'a, E: Engine> ConstraintSystem for VerifierInput<'a, E> { struct InputAllocator(T); impl<'a, 'b, E: Engine> ConstraintSystem for InputAllocator<&'a mut VerifierInput<'b, E>> { - fn alloc Result>(&mut self, value: F) -> Result { - self.0.alloc(value) + fn alloc( + &mut self, + name_fn: N, + f: F + ) -> Result + where NR: Into, N: FnOnce() -> NR, F: FnOnce() -> Result + { + self.0.alloc(name_fn, f) } - fn enforce( + fn enforce, N: FnOnce() -> NR>( &mut self, + _: N, _: LinearCombination, _: LinearCombination, _: LinearCombination @@ -68,11 +82,17 @@ impl<'a, 'b, E: Engine> ConstraintSystem for InputAllocator<&'a mut VerifierI } impl<'a, 'b, E: Engine> PublicConstraintSystem for InputAllocator<&'a mut VerifierInput<'b, E>> { - fn alloc_input Result>(&mut self, value: F) -> Result { + fn alloc_input( + &mut self, + _: N, + f: F + ) -> Result + where NR: Into, N: FnOnce() -> NR, F: FnOnce() -> Result + { if self.0.ic.len() == 0 { self.0.insufficient_inputs = true; } else { - self.0.acc.add_assign(&self.0.ic[0].mul(value()?)); + self.0.acc.add_assign(&self.0.ic[0].mul(f()?)); self.0.ic = &self.0.ic[1..]; } diff --git a/src/lib.rs b/src/lib.rs index 1aa7187..11ee6ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,8 @@ pub enum Error { AssignmentMissing, UnexpectedIdentity, UnconstrainedVariable(Variable), - IoError(io::Error) + IoError(io::Error), + NameConflict(&'static str, String) } impl From for Error { @@ -185,7 +186,12 @@ pub trait Input { pub trait PublicConstraintSystem: ConstraintSystem { /// Allocate a public input that the verifier knows. The provided function is used to /// determine the assignment of the variable. - fn alloc_input Result>(&mut self, f: F) -> Result; + fn alloc_input( + &mut self, + name_fn: N, + f: F + ) -> Result + where NR: Into, N: FnOnce() -> NR, F: FnOnce() -> Result; } pub trait ConstraintSystem { @@ -196,13 +202,30 @@ pub trait ConstraintSystem { /// Allocate a private variable in the constraint system. The provided function is used to /// determine the assignment of the variable. - fn alloc Result>(&mut self, f: F) -> Result; + fn alloc( + &mut self, + name_fn: N, + f: F + ) -> Result + where NR: Into, N: FnOnce() -> NR, F: FnOnce() -> Result; /// Enforce that `A` * `B` = `C`. - fn enforce( + fn enforce, N: FnOnce() -> NR>( &mut self, + name_fn: N, a: LinearCombination, b: LinearCombination, c: LinearCombination ); + + /// Begin a namespace for the constraint system + fn namespace( + &mut self, + _: N, + space_fn: F + ) -> Result + where NR: Into, N: FnOnce() -> NR, F: FnOnce(&mut Self) -> Result + { + space_fn(self) + } }