Skip to content

Commit ea08911

Browse files
authored
r1cs: decouple randomization from CS (#270)
Closes #268
1 parent 41c5588 commit ea08911

File tree

7 files changed

+35
-34
lines changed

7 files changed

+35
-34
lines changed

benches/r1cs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ For K = 1:
7373
struct KShuffleGadget {}
7474

7575
impl KShuffleGadget {
76-
fn fill_cs<CS: ConstraintSystem>(
76+
fn fill_cs<CS: RandomizableConstraintSystem>(
7777
cs: &mut CS,
7878
x: Vec<Variable>,
7979
y: Vec<Variable>,

docs/r1cs-docs-example.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ use rand::thread_rng;
7575
struct ShuffleProof(R1CSProof);
7676

7777
impl ShuffleProof {
78-
fn gadget<CS: ConstraintSystem>(cs: &mut CS, x: Vec<Variable>, y: Vec<Variable>) -> Result<(),R1CSError> {
78+
fn gadget<CS: RandomizableConstraintSystem>(cs: &mut CS, x: Vec<Variable>, y: Vec<Variable>) -> Result<(),R1CSError> {
7979

8080
assert_eq!(x.len(), y.len());
8181
let k = x.len();
@@ -156,7 +156,7 @@ For simplicity, in this example the `prove` function does not take a list of bli
156156
# struct ShuffleProof(R1CSProof);
157157
#
158158
# impl ShuffleProof {
159-
# fn gadget<CS: ConstraintSystem>(cs: &mut CS, x: Vec<Variable>, y: Vec<Variable>) -> Result<(),R1CSError> {
159+
# fn gadget<CS: RandomizableConstraintSystem>(cs: &mut CS, x: Vec<Variable>, y: Vec<Variable>) -> Result<(),R1CSError> {
160160
#
161161
# assert_eq!(x.len(), y.len());
162162
# let k = x.len();
@@ -263,7 +263,7 @@ The verifier receives a proof, and a list of committed inputs and outputs, from
263263
# struct ShuffleProof(R1CSProof);
264264
#
265265
# impl ShuffleProof {
266-
# fn gadget<CS: ConstraintSystem>(cs: &mut CS, x: Vec<Variable>, y: Vec<Variable>) -> Result<(),R1CSError> {
266+
# fn gadget<CS: RandomizableConstraintSystem>(cs: &mut CS, x: Vec<Variable>, y: Vec<Variable>) -> Result<(),R1CSError> {
267267
#
268268
# assert_eq!(x.len(), y.len());
269269
# let k = x.len();
@@ -403,7 +403,7 @@ Because only the prover knows the scalar values of the inputs and outputs, and t
403403
# struct ShuffleProof(R1CSProof);
404404
#
405405
# impl ShuffleProof {
406-
# fn gadget<CS: ConstraintSystem>(cs: &mut CS, x: Vec<Variable>, y: Vec<Variable>) -> Result<(),R1CSError> {
406+
# fn gadget<CS: RandomizableConstraintSystem>(cs: &mut CS, x: Vec<Variable>, y: Vec<Variable>) -> Result<(),R1CSError> {
407407
#
408408
# assert_eq!(x.len(), y.len());
409409
# let k = x.len();

src/r1cs/constraint_system.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ use merlin::Transcript;
1717
/// using the `ConstraintSystem` trait, so that the prover and
1818
/// verifier share the logic for specifying constraints.
1919
pub trait ConstraintSystem {
20-
/// Represents a concrete type for the CS in a randomization phase.
21-
type RandomizedCS: RandomizedConstraintSystem;
22-
2320
/// Leases the proof transcript to the user, so they can
2421
/// add extra data to which the proof must be bound, but which
2522
/// is not available before creation of the constraint system.
@@ -74,6 +71,16 @@ pub trait ConstraintSystem {
7471
/// lc = 0
7572
/// ```
7673
fn constrain(&mut self, lc: LinearCombination);
74+
}
75+
76+
/// An extension to the constraint system trait that permits randomized constraints.
77+
/// Gadgets that do not use randomization should use trait bound `CS: ConstraintSystem`,
78+
/// while gadgets that need randomization should use trait bound `CS: RandomizedConstraintSystem`.
79+
/// Gadgets generally _should not_ use this trait as a bound on the CS argument: it should be used
80+
/// by the higher-order protocol that composes gadgets together.
81+
pub trait RandomizableConstraintSystem: ConstraintSystem {
82+
/// Represents a concrete type for the CS in a randomization phase.
83+
type RandomizedCS: RandomizedConstraintSystem;
7784

7885
/// Specify additional variables and constraints randomized using a challenge scalar
7986
/// bound to the assignments of the non-randomized variables.

src/r1cs/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ mod proof;
99
mod prover;
1010
mod verifier;
1111

12-
pub use self::constraint_system::{ConstraintSystem, RandomizedConstraintSystem};
12+
pub use self::constraint_system::{
13+
ConstraintSystem, RandomizableConstraintSystem, RandomizedConstraintSystem,
14+
};
1315
pub use self::linear_combination::{LinearCombination, Variable};
1416
pub use self::proof::R1CSProof;
1517
pub use self::prover::Prover;

src/r1cs/prover.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use curve25519_dalek::scalar::Scalar;
77
use curve25519_dalek::traits::{Identity, MultiscalarMul};
88
use merlin::Transcript;
99

10-
use super::{ConstraintSystem, LinearCombination, R1CSProof, RandomizedConstraintSystem, Variable};
10+
use super::{
11+
ConstraintSystem, LinearCombination, R1CSProof, RandomizableConstraintSystem,
12+
RandomizedConstraintSystem, Variable,
13+
};
1114

1215
use errors::R1CSError;
1316
use generators::{BulletproofGens, PedersenGens};
@@ -83,8 +86,6 @@ impl<'t, 'g> Drop for Prover<'t, 'g> {
8386
}
8487

8588
impl<'t, 'g> ConstraintSystem for Prover<'t, 'g> {
86-
type RandomizedCS = RandomizingProver<'t, 'g>;
87-
8889
fn transcript(&mut self) -> &mut Transcript {
8990
self.transcript
9091
}
@@ -162,6 +163,10 @@ impl<'t, 'g> ConstraintSystem for Prover<'t, 'g> {
162163
// (e.g. that variables are valid, that the linear combination evals to 0 for prover, etc).
163164
self.constraints.push(lc);
164165
}
166+
}
167+
168+
impl<'t, 'g> RandomizableConstraintSystem for Prover<'t, 'g> {
169+
type RandomizedCS = RandomizingProver<'t, 'g>;
165170

166171
fn specify_randomized_constraints<F>(&mut self, callback: F) -> Result<(), R1CSError>
167172
where
@@ -173,8 +178,6 @@ impl<'t, 'g> ConstraintSystem for Prover<'t, 'g> {
173178
}
174179

175180
impl<'t, 'g> ConstraintSystem for RandomizingProver<'t, 'g> {
176-
type RandomizedCS = Self;
177-
178181
fn transcript(&mut self) -> &mut Transcript {
179182
self.prover.transcript
180183
}
@@ -201,13 +204,6 @@ impl<'t, 'g> ConstraintSystem for RandomizingProver<'t, 'g> {
201204
fn constrain(&mut self, lc: LinearCombination) {
202205
self.prover.constrain(lc)
203206
}
204-
205-
fn specify_randomized_constraints<F>(&mut self, callback: F) -> Result<(), R1CSError>
206-
where
207-
F: 'static + Fn(&mut Self::RandomizedCS) -> Result<(), R1CSError>,
208-
{
209-
callback(self)
210-
}
211207
}
212208

213209
impl<'t, 'g> RandomizedConstraintSystem for RandomizingProver<'t, 'g> {

src/r1cs/verifier.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use curve25519_dalek::scalar::Scalar;
66
use curve25519_dalek::traits::VartimeMultiscalarMul;
77
use merlin::Transcript;
88

9-
use super::{ConstraintSystem, LinearCombination, R1CSProof, RandomizedConstraintSystem, Variable};
9+
use super::{
10+
ConstraintSystem, LinearCombination, R1CSProof, RandomizableConstraintSystem,
11+
RandomizedConstraintSystem, Variable,
12+
};
1013

1114
use errors::R1CSError;
1215
use generators::{BulletproofGens, PedersenGens};
@@ -57,8 +60,6 @@ pub struct RandomizingVerifier<'t> {
5760
}
5861

5962
impl<'t> ConstraintSystem for Verifier<'t> {
60-
type RandomizedCS = RandomizingVerifier<'t>;
61-
6263
fn transcript(&mut self) -> &mut Transcript {
6364
self.transcript
6465
}
@@ -121,6 +122,10 @@ impl<'t> ConstraintSystem for Verifier<'t> {
121122
// evals to 0 for prover, etc).
122123
self.constraints.push(lc);
123124
}
125+
}
126+
127+
impl<'t> RandomizableConstraintSystem for Verifier<'t> {
128+
type RandomizedCS = RandomizingVerifier<'t>;
124129

125130
fn specify_randomized_constraints<F>(&mut self, callback: F) -> Result<(), R1CSError>
126131
where
@@ -132,8 +137,6 @@ impl<'t> ConstraintSystem for Verifier<'t> {
132137
}
133138

134139
impl<'t> ConstraintSystem for RandomizingVerifier<'t> {
135-
type RandomizedCS = Self;
136-
137140
fn transcript(&mut self) -> &mut Transcript {
138141
self.verifier.transcript
139142
}
@@ -160,13 +163,6 @@ impl<'t> ConstraintSystem for RandomizingVerifier<'t> {
160163
fn constrain(&mut self, lc: LinearCombination) {
161164
self.verifier.constrain(lc)
162165
}
163-
164-
fn specify_randomized_constraints<F>(&mut self, callback: F) -> Result<(), R1CSError>
165-
where
166-
F: 'static + Fn(&mut Self::RandomizedCS) -> Result<(), R1CSError>,
167-
{
168-
callback(self)
169-
}
170166
}
171167

172168
impl<'t> RandomizedConstraintSystem for RandomizingVerifier<'t> {

tests/r1cs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rand::thread_rng;
1717
struct ShuffleProof(R1CSProof);
1818

1919
impl ShuffleProof {
20-
fn gadget<CS: ConstraintSystem>(
20+
fn gadget<CS: RandomizableConstraintSystem>(
2121
cs: &mut CS,
2222
x: Vec<Variable>,
2323
y: Vec<Variable>,

0 commit comments

Comments
 (0)