Skip to content

Commit

Permalink
Make closure of challenge phase FnOnce. (#276)
Browse files Browse the repository at this point in the history
It allows for the challenge phase to accept closures that take some more
context, e.g. moving non-Clone values in the challenge closure.

This works in stable since Rust 1.35; cfr.
rust-lang/rust#28796.

This closes issue #244.
  • Loading branch information
rubdos authored May 26, 2020
1 parent fdb68ff commit 10a9d81
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/r1cs/constraint_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub trait RandomizableConstraintSystem: ConstraintSystem {
/// ```
fn specify_randomized_constraints<F>(&mut self, callback: F) -> Result<(), R1CSError>
where
F: 'static + Fn(&mut Self::RandomizedCS) -> Result<(), R1CSError>;
F: 'static + FnOnce(&mut Self::RandomizedCS) -> Result<(), R1CSError>;
}

/// Represents a constraint system in the second phase:
Expand Down
5 changes: 3 additions & 2 deletions src/r1cs/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ pub struct Prover<'g, T: BorrowMut<Transcript>> {

/// This list holds closures that will be called in the second phase of the protocol,
/// when non-randomized variables are committed.
deferred_constraints: Vec<Box<dyn Fn(&mut RandomizingProver<'g, T>) -> Result<(), R1CSError>>>,
deferred_constraints:
Vec<Box<dyn FnOnce(&mut RandomizingProver<'g, T>) -> Result<(), R1CSError>>>,

/// Index of a pending multiplier that's not fully assigned yet.
pending_multiplier: Option<usize>,
Expand Down Expand Up @@ -188,7 +189,7 @@ impl<'g, T: BorrowMut<Transcript>> RandomizableConstraintSystem for Prover<'g, T

fn specify_randomized_constraints<F>(&mut self, callback: F) -> Result<(), R1CSError>
where
F: 'static + Fn(&mut Self::RandomizedCS) -> Result<(), R1CSError>,
F: 'static + FnOnce(&mut Self::RandomizedCS) -> Result<(), R1CSError>,
{
self.deferred_constraints.push(Box::new(callback));
Ok(())
Expand Down
5 changes: 3 additions & 2 deletions src/r1cs/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ pub struct Verifier<T: BorrowMut<Transcript>> {
/// when non-randomized variables are committed.
/// After that, the option will flip to None and additional calls to `randomize_constraints`
/// will invoke closures immediately.
deferred_constraints: Vec<Box<dyn Fn(&mut RandomizingVerifier<T>) -> Result<(), R1CSError>>>,
deferred_constraints:
Vec<Box<dyn FnOnce(&mut RandomizingVerifier<T>) -> Result<(), R1CSError>>>,

/// Index of a pending multiplier that's not fully assigned yet.
pending_multiplier: Option<usize>,
Expand Down Expand Up @@ -140,7 +141,7 @@ impl<T: BorrowMut<Transcript>> RandomizableConstraintSystem for Verifier<T> {

fn specify_randomized_constraints<F>(&mut self, callback: F) -> Result<(), R1CSError>
where
F: 'static + Fn(&mut Self::RandomizedCS) -> Result<(), R1CSError>,
F: 'static + FnOnce(&mut Self::RandomizedCS) -> Result<(), R1CSError>,
{
self.deferred_constraints.push(Box::new(callback));
Ok(())
Expand Down

0 comments on commit 10a9d81

Please sign in to comment.