Skip to content

Commit 5196830

Browse files
committed
Add parallelism
1 parent d72bd9a commit 5196830

File tree

7 files changed

+440
-205
lines changed

7 files changed

+440
-205
lines changed

circ_blocks/examples/zxc.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,11 @@ struct CompileTimeKnowledge {
239239
}
240240

241241
impl CompileTimeKnowledge {
242-
fn serialize_to_file(&self, benchmark_name: String, max_file_size: usize) -> std::io::Result<()> {
242+
fn serialize_to_file(
243+
&self,
244+
benchmark_name: String,
245+
max_file_size: usize,
246+
) -> std::io::Result<()> {
243247
let content = bincode::serialize(&self).unwrap();
244248
println!("CTK SIZE: {}", content.len());
245249
for i in 0..content.len().div_ceil(max_file_size) {
@@ -383,7 +387,11 @@ struct RunTimeKnowledge<S: SpartanExtensionField + Send + Sync> {
383387
}
384388

385389
impl<S: SpartanExtensionField + Send + Sync> RunTimeKnowledge<S> {
386-
fn serialize_to_file(&self, benchmark_name: String, max_file_size: usize) -> std::io::Result<()> {
390+
fn serialize_to_file(
391+
&self,
392+
benchmark_name: String,
393+
max_file_size: usize,
394+
) -> std::io::Result<()> {
387395
let content = bincode::serialize(&self).unwrap();
388396
println!("RTK SIZE: {}", content.len());
389397
for i in 0..content.len().div_ceil(max_file_size) {
@@ -1311,18 +1319,17 @@ fn run_spartan_proof<S: SpartanExtensionField + Send + Sync>(
13111319
// block_inst is used by sumcheck. Every block has the same number of variables
13121320
let (block_num_vars, block_num_cons, block_num_non_zero_entries, mut block_inst) =
13131321
Instance::gen_block_inst::<true, false>(
1314-
block_num_instances_bound,
1315-
num_vars,
1316-
&ctk.args,
1317-
num_inputs_unpadded,
1318-
&block_num_phy_ops,
1319-
&block_num_vir_ops,
1320-
&ctk.num_vars_per_block,
1321-
&rtk.block_num_proofs,
1322+
block_num_instances_bound,
1323+
num_vars,
1324+
&ctk.args,
1325+
num_inputs_unpadded,
1326+
&block_num_phy_ops,
1327+
&block_num_vir_ops,
1328+
&ctk.num_vars_per_block,
1329+
&rtk.block_num_proofs,
13221330
);
13231331
// block_inst is used by commitment. Every block has different number of variables
1324-
let (_, _, _, block_inst_for_commit) =
1325-
Instance::<S>::gen_block_inst::<true, true>(
1332+
let (_, _, _, block_inst_for_commit) = Instance::<S>::gen_block_inst::<true, true>(
13261333
block_num_instances_bound,
13271334
num_vars,
13281335
&ctk.args,
@@ -1331,7 +1338,7 @@ fn run_spartan_proof<S: SpartanExtensionField + Send + Sync>(
13311338
&block_num_vir_ops,
13321339
&ctk.num_vars_per_block,
13331340
&rtk.block_num_proofs,
1334-
);
1341+
);
13351342
println!("Finished Block");
13361343

13371344
// Pairwise INSTANCES
@@ -1368,7 +1375,8 @@ fn run_spartan_proof<S: SpartanExtensionField + Send + Sync>(
13681375
println!("Comitting Circuits...");
13691376
// block_comm_map records the sparse_polys committed in each commitment
13701377
// Note that A, B, C are committed separately, so sparse_poly[3*i+2] corresponds to poly C of instance i
1371-
let (block_comm_map, block_comm_list, block_decomm_list) = SNARK::multi_encode(&block_inst_for_commit);
1378+
let (block_comm_map, block_comm_list, block_decomm_list) =
1379+
SNARK::multi_encode(&block_inst_for_commit);
13721380
println!("Finished Block");
13731381
let (pairwise_check_comm, pairwise_check_decomm) = SNARK::encode(&pairwise_check_inst);
13741382
println!("Finished Pairwise");
@@ -1612,12 +1620,14 @@ fn main() {
16121620
ctk.write_to_file(benchmark_name.to_string()).unwrap();
16131621
rtk.write_to_file(benchmark_name.to_string()).unwrap();
16141622
}
1615-
1623+
16161624
// --
16171625
// Write CTK, RTK to file
16181626
// --
1619-
ctk.serialize_to_file(benchmark_name.to_string(), MAX_FILE_SIZE).unwrap();
1620-
rtk.serialize_to_file(benchmark_name.to_string(), MAX_FILE_SIZE).unwrap();
1627+
ctk.serialize_to_file(benchmark_name.to_string(), MAX_FILE_SIZE)
1628+
.unwrap();
1629+
rtk.serialize_to_file(benchmark_name.to_string(), MAX_FILE_SIZE)
1630+
.unwrap();
16211631
if INLINE_SPARTAN_PROOF {
16221632
run_spartan_proof(ctk, rtk);
16231633
}

spartan_parallel/examples/interface.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,16 @@ fn main() {
148148
&rtk.block_num_proofs,
149149
);
150150
// block_inst is used by commitment. Every block has different number of variables
151-
let (_, _, _, block_inst_for_commit) =
152-
Instance::<ScalarExt2>::gen_block_inst::<true, true>(
153-
block_num_instances_bound,
154-
num_vars,
155-
&ctk.args,
156-
num_inputs_unpadded,
157-
&block_num_phy_ops,
158-
&block_num_vir_ops,
159-
&ctk.num_vars_per_block,
160-
&rtk.block_num_proofs,
161-
);
151+
let (_, _, _, block_inst_for_commit) = Instance::<ScalarExt2>::gen_block_inst::<true, true>(
152+
block_num_instances_bound,
153+
num_vars,
154+
&ctk.args,
155+
num_inputs_unpadded,
156+
&block_num_phy_ops,
157+
&block_num_vir_ops,
158+
&ctk.num_vars_per_block,
159+
&rtk.block_num_proofs,
160+
);
162161
println!("Finished Block");
163162

164163
// Pairwise INSTANCES
@@ -198,7 +197,8 @@ fn main() {
198197
println!("Comitting Circuits...");
199198
// block_comm_map records the sparse_polys committed in each commitment
200199
// Note that A, B, C are committed separately, so sparse_poly[3*i+2] corresponds to poly C of instance i
201-
let (block_comm_map, block_comm_list, block_decomm_list) = SNARK::multi_encode(&block_inst_for_commit);
200+
let (block_comm_map, block_comm_list, block_decomm_list) =
201+
SNARK::multi_encode(&block_inst_for_commit);
202202
println!("Finished Block");
203203
let (pairwise_check_comm, pairwise_check_decomm) = SNARK::encode(&pairwise_check_inst);
204204
println!("Finished Pairwise");

spartan_parallel/src/instance.rs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ impl<S: SpartanExtensionField + Send + Sync> Instance<S> {
8282
}
8383
}
8484

85-
(max_num_vars_padded, num_vars_padded, max_num_cons_padded, num_cons_padded)
85+
(
86+
max_num_vars_padded,
87+
num_vars_padded,
88+
max_num_cons_padded,
89+
num_cons_padded,
90+
)
8691
};
8792

8893
let bytes_to_scalar =
@@ -266,7 +271,7 @@ impl<S: SpartanExtensionField + Send + Sync> Instance<S> {
266271
/// - VMR3 = r^3 * VT
267272
/// - VMC = (1 or VMC[i-1]) * (tau - VA - VMR1 - VMR2 - VMR3)
268273
/// The final product is stored in X = MC[NV - 1]
269-
///
274+
///
270275
/// If in COMMIT_MODE, commit instance by num_vars_per_block, rounded to the nearest power of four
271276
pub fn gen_block_inst<const PRINT_SIZE: bool, const COMMIT_MODE: bool>(
272277
num_instances: usize,
@@ -301,7 +306,15 @@ impl<S: SpartanExtensionField + Send + Sync> Instance<S> {
301306
max_size_per_group.insert(next_group_size(*num_vars), num_vars.next_power_of_two());
302307
}
303308
}
304-
num_vars_per_block.iter().map(|i| max_size_per_group.get(&next_group_size(*i)).unwrap().clone()).collect()
309+
num_vars_per_block
310+
.iter()
311+
.map(|i| {
312+
max_size_per_group
313+
.get(&next_group_size(*i))
314+
.unwrap()
315+
.clone()
316+
})
317+
.collect()
305318
} else {
306319
vec![num_vars; num_instances]
307320
};
@@ -346,19 +359,26 @@ impl<S: SpartanExtensionField + Send + Sync> Instance<S> {
346359
2 * num_vars_padded_per_block[b] + 2 + i
347360
}
348361
};
349-
let V_output_dot_prod = |b: usize, i: usize| 2 * num_vars_padded_per_block[b] + 2 + (num_inputs_unpadded - 1) + i;
362+
let V_output_dot_prod =
363+
|b: usize, i: usize| 2 * num_vars_padded_per_block[b] + 2 + (num_inputs_unpadded - 1) + i;
350364
// in BLOCK_W2 / PHY_W2
351-
let V_PMR = |b: usize, i: usize| 2 * num_vars_padded_per_block[b] + 2 * num_inputs_unpadded + 2 * i;
352-
let V_PMC = |b: usize, i: usize| 2 * num_vars_padded_per_block[b] + 2 * num_inputs_unpadded + 2 * i + 1;
365+
let V_PMR =
366+
|b: usize, i: usize| 2 * num_vars_padded_per_block[b] + 2 * num_inputs_unpadded + 2 * i;
367+
let V_PMC =
368+
|b: usize, i: usize| 2 * num_vars_padded_per_block[b] + 2 * num_inputs_unpadded + 2 * i + 1;
353369
// in BLOCK_W2 / VIR_W2
354-
let V_VMR1 =
355-
|b: usize, i: usize| 2 * num_vars_padded_per_block[b] + 2 * num_inputs_unpadded + 2 * num_phy_ops[b] + 4 * i;
356-
let V_VMR2 =
357-
|b: usize, i: usize| 2 * num_vars_padded_per_block[b] + 2 * num_inputs_unpadded + 2 * num_phy_ops[b] + 4 * i + 1;
358-
let V_VMR3 =
359-
|b: usize, i: usize| 2 * num_vars_padded_per_block[b] + 2 * num_inputs_unpadded + 2 * num_phy_ops[b] + 4 * i + 2;
360-
let V_VMC =
361-
|b: usize, i: usize| 2 * num_vars_padded_per_block[b] + 2 * num_inputs_unpadded + 2 * num_phy_ops[b] + 4 * i + 3;
370+
let V_VMR1 = |b: usize, i: usize| {
371+
2 * num_vars_padded_per_block[b] + 2 * num_inputs_unpadded + 2 * num_phy_ops[b] + 4 * i
372+
};
373+
let V_VMR2 = |b: usize, i: usize| {
374+
2 * num_vars_padded_per_block[b] + 2 * num_inputs_unpadded + 2 * num_phy_ops[b] + 4 * i + 1
375+
};
376+
let V_VMR3 = |b: usize, i: usize| {
377+
2 * num_vars_padded_per_block[b] + 2 * num_inputs_unpadded + 2 * num_phy_ops[b] + 4 * i + 2
378+
};
379+
let V_VMC = |b: usize, i: usize| {
380+
2 * num_vars_padded_per_block[b] + 2 * num_inputs_unpadded + 2 * num_phy_ops[b] + 4 * i + 3
381+
};
362382
// in BLOCK_W3
363383
let V_v = |b: usize| 3 * num_vars_padded_per_block[b];
364384
let V_x = |b: usize| 3 * num_vars_padded_per_block[b] + 1;
@@ -724,7 +744,10 @@ impl<S: SpartanExtensionField + Send + Sync> Instance<S> {
724744
max_cons_per_group.insert(num_vars_padded_per_block[i], block_num_cons[i]);
725745
}
726746
}
727-
num_vars_padded_per_block.iter().map(|i| max_cons_per_group.get(i).unwrap().clone()).collect()
747+
num_vars_padded_per_block
748+
.iter()
749+
.map(|i| max_cons_per_group.get(i).unwrap().clone())
750+
.collect()
728751
} else {
729752
block_num_cons
730753
}
@@ -736,7 +759,10 @@ impl<S: SpartanExtensionField + Send + Sync> Instance<S> {
736759
block_max_num_cons,
737760
num_cons_padded_per_block,
738761
block_num_vars,
739-
num_vars_padded_per_block.into_iter().map(|i| 8 * i).collect(),
762+
num_vars_padded_per_block
763+
.into_iter()
764+
.map(|i| 8 * i)
765+
.collect(),
740766
&A_list,
741767
&B_list,
742768
&C_list,

0 commit comments

Comments
 (0)