Skip to content

Commit 7bbc366

Browse files
Arbitrary number of variables and contraints (#34)
* This commit makes adding an arbitrary number of variables and inputs possible and removes the implementation leaking to the interface for num_inps + 1 <= num_vars, num_vars: a power of 2, num_cons: a power of 2, but not 1. 1. When creating a new R1CS Instance throught the public interface, it is required # constraints and # of vars be a power of 2. I remove that requirement by padding with dummy constraints and vars until the nearest power of 2. 2. The sumcheck protocol in src/sumcheck.rs does not work for 1 constraint, even though 1 is a power of 2. I have to pad to a minimum of two constraints. 3. Added a test in src/r1csproof.rs called test_padded_constraints. * Move test to src/lib.rs * Remove padding metadata * remove unused use * Simplify padding to power of 2 * run cargo fmt * Fix indexing bug * Rayon is optional, depending on 'multicore' feature * Update rust toolchain * cargo fmt * cleaner to track num_vars_padded and num_cons_padded * cleanup * further cleanup * Cleanup & comments * small fixes * adjust code for padding constraints * fix a bug with pad call * add comment about num_nz_entries * extend padding to NIZK methods extend padding to NIZK methods Co-authored-by: Lef Ioannidis <[email protected]> Co-authored-by: Srinath Setty <[email protected]>
1 parent 096c079 commit 7bbc366

File tree

7 files changed

+235
-42
lines changed

7 files changed

+235
-42
lines changed

.github/workflows/rust.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
steps:
1515
- uses: actions/checkout@v2
1616
- name: Install
17-
run: rustup default nightly-2021-01-03
17+
run: rustup default nightly-2021-01-31
1818
- name: Build
1919
run: cargo build --verbose
2020
- name: Run tests

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ rand = "0.7.3"
1717
digest = "0.8.1"
1818
sha3 = "0.8.2"
1919
byteorder = "1.3.4"
20-
rayon = "1.3.0"
20+
rayon = { version = "1.3.0", optional = true }
2121
serde = { version = "1.0.106", features = ["derive"] }
2222
bincode = "1.2.1"
2323
subtle = { version = "^2.2.3", default-features = false }
@@ -52,5 +52,5 @@ name = "nizk"
5252
harness = false
5353

5454
[features]
55-
multicore = []
55+
multicore = ["rayon"]
5656
profile = []

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Here is another example to use the NIZK variant of the Spartan proof system:
8282
let num_inputs = 10;
8383

8484
// produce public parameters
85-
let gens = NIZKGens::new(num_cons, num_vars);
85+
let gens = NIZKGens::new(num_cons, num_vars, num_inputs);
8686

8787
// ask the library to produce a synthentic R1CS instance
8888
let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
@@ -102,6 +102,7 @@ Here is another example to use the NIZK variant of the Spartan proof system:
102102

103103
Finally, we provide an example that specifies a custom R1CS instance instead of using a synthetic instance
104104
```rust
105+
#![allow(non_snake_case)]
105106
# extern crate curve25519_dalek;
106107
# extern crate libspartan;
107108
# extern crate merlin;
@@ -163,9 +164,9 @@ Finally, we provide an example that specifies a custom R1CS instance instead of
163164

164165
// parameters of the R1CS instance rounded to the nearest power of two
165166
let num_cons = 4;
166-
let num_vars = 8;
167+
let num_vars = 5;
167168
let num_inputs = 2;
168-
let num_non_zero_entries = 8;
169+
let num_non_zero_entries = 5;
169170

170171
// We will encode the above constraints into three matrices, where
171172
// the coefficients in the matrix are in the little-endian byte order

benches/nizk.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn nizk_prove_benchmark(c: &mut Criterion) {
2424

2525
let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
2626

27-
let gens = NIZKGens::new(num_cons, num_vars);
27+
let gens = NIZKGens::new(num_cons, num_vars, num_inputs);
2828

2929
let name = format!("NIZK_prove_{}", num_vars);
3030
group.bench_function(&name, move |b| {
@@ -54,7 +54,7 @@ fn nizk_verify_benchmark(c: &mut Criterion) {
5454
let num_inputs = 10;
5555
let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
5656

57-
let gens = NIZKGens::new(num_cons, num_vars);
57+
let gens = NIZKGens::new(num_cons, num_vars, num_inputs);
5858

5959
// produce a proof of satisfiability
6060
let mut prover_transcript = Transcript::new(b"example");

profiler/nizk.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn main() {
2727
let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
2828

2929
// produce public generators
30-
let gens = NIZKGens::new(num_cons, num_vars);
30+
let gens = NIZKGens::new(num_cons, num_vars, num_inputs);
3131

3232
// produce a proof of satisfiability
3333
let mut prover_transcript = Transcript::new(b"nizk_example");

src/dense_mlpoly.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl DensePolynomial {
149149
assert_eq!(L_size * R_size, self.Z.len());
150150
let C = (0..L_size)
151151
.into_par_iter()
152-
.map(|&i| {
152+
.map(|i| {
153153
self.Z[R_size * i..R_size * (i + 1)]
154154
.commit(&blinds[i], gens)
155155
.compress()

0 commit comments

Comments
 (0)