Skip to content

Commit

Permalink
Initial time recording
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunming Jiang committed Dec 20, 2024
1 parent 5f17d48 commit 67b3ea4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
29 changes: 19 additions & 10 deletions circ_blocks/examples/zxc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@ fn get_run_time_knowledge<const VERBOSE: bool, S: SpartanExtensionField + Send +
)
};

let gen_start = Instant::now();
// Meta info
// The most time any block is executed
let mut block_max_num_proofs = 0;
Expand Down Expand Up @@ -936,7 +937,7 @@ fn get_run_time_knowledge<const VERBOSE: bool, S: SpartanExtensionField + Send +
let zero = Integer::from(0);
let one = Integer::from(1);
// Start from entry block, compute value of witnesses
// Note: block_vars_matrix are sorted by block_num_proofs, tie-breaked by block id
// Note: block_vars_matrix are sorted by block_num_proofs, tie-breaked by block id
// Thus, block_vars_matrix[0] might NOT store the executions of block 0!
let mut block_vars_matrix = vec![Vec::new(); num_blocks_live];
let mut exec_inputs = Vec::new();
Expand Down Expand Up @@ -1038,14 +1039,14 @@ fn get_run_time_knowledge<const VERBOSE: bool, S: SpartanExtensionField + Send +
.concat();
let inputs_assignment = Assignment::new(
&inputs
.iter()
.into_iter()
.map(|i| integer_to_bytes(i.clone()))
.collect::<Vec<[u8; 32]>>(),
)
.unwrap();
let vars_assignment = Assignment::new(
&vars
.iter()
.into_iter()
.map(|i| integer_to_bytes(i.clone()))
.collect::<Vec<[u8; 32]>>(),
)
Expand All @@ -1055,7 +1056,10 @@ fn get_run_time_knowledge<const VERBOSE: bool, S: SpartanExtensionField + Send +
exec_inputs.push(inputs_assignment.clone());
block_vars_matrix[slot].push(vars_assignment);
}
let gen_time = gen_start.elapsed();
println!("\n--\nGen time: {}ms", gen_time.as_millis());

let record_start = Instant::now();
// Initial Physical & Virtual Memory: valid, _, addr, data (ts and ls are both 0 and are not recorded)
let mut init_phy_mems_list = Vec::new();
for i in 0..init_phy_mem_list.len() {
Expand Down Expand Up @@ -1088,7 +1092,7 @@ fn get_run_time_knowledge<const VERBOSE: bool, S: SpartanExtensionField + Send +

init_vir_mems_list.push(
Assignment::new(
&mem.iter()
&mem.into_iter()
.map(|i| integer_to_bytes(i.clone()))
.collect::<Vec<[u8; 32]>>(),
)
Expand Down Expand Up @@ -1122,7 +1126,7 @@ fn get_run_time_knowledge<const VERBOSE: bool, S: SpartanExtensionField + Send +
if i == phy_mem_list.len() - 1 {
addr_phy_mems_list.push(
Assignment::new(
&mem.iter()
&mem.into_iter()
.map(|i| integer_to_bytes(i.clone()))
.collect::<Vec<[u8; 32]>>(),
)
Expand Down Expand Up @@ -1191,7 +1195,7 @@ fn get_run_time_knowledge<const VERBOSE: bool, S: SpartanExtensionField + Send +
if i == vir_mem_list.len() - 1 {
addr_vir_mems_list.push(
Assignment::new(
&mem.iter()
&mem.into_iter()
.map(|i| integer_to_bytes(i.clone()))
.collect::<Vec<[u8; 32]>>(),
)
Expand All @@ -1200,7 +1204,7 @@ fn get_run_time_knowledge<const VERBOSE: bool, S: SpartanExtensionField + Send +
addr_ts_bits_list.push(
Assignment::new(
&ts_bits
.iter()
.into_iter()
.map(|i| integer_to_bytes(i.clone()))
.collect::<Vec<[u8; 32]>>(),
)
Expand Down Expand Up @@ -1247,18 +1251,20 @@ fn get_run_time_knowledge<const VERBOSE: bool, S: SpartanExtensionField + Send +
println!("{:3} ", func_outputs);

let func_inputs = entry_regs
.iter()
.into_iter()
.map(|i| integer_to_bytes(i.clone()))
.collect();
let input_stack = entry_stacks
.iter()
.into_iter()
.map(|i| integer_to_bytes(i.clone()))
.collect();
let input_mem = entry_arrays
.iter()
.into_iter()
.map(|i| integer_to_bytes(i.clone()))
.collect();
let func_outputs = integer_to_bytes(func_outputs);
let record_time = record_start.elapsed();
println!("\n--\nRecord time: {}ms", record_time.as_millis());

RunTimeKnowledge {
block_max_num_proofs,
Expand Down Expand Up @@ -1513,6 +1519,7 @@ fn main() {
// Obtain Inputs
// --
let witness_start = Instant::now();
let read_input_start = Instant::now();
// Assume inputs are listed in the order of the parameters
let mut entry_regs: Vec<Integer> = Vec::new();
// Keep track of %SP and %AS and record initial memory state
Expand Down Expand Up @@ -1594,6 +1601,8 @@ fn main() {
reader.read_line(&mut buffer).unwrap();
}
}
let read_input_time = read_input_start.elapsed();
println!("\n--\nRead input time: {}ms", read_input_time.as_millis());

println!("INPUT: {:?}", entry_regs);

Expand Down
11 changes: 11 additions & 0 deletions circ_blocks/src/front/zsharp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::fmt::Display;
use std::hash::BuildHasherDefault;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Instant;
use zokrates_pest_ast as ast;

use term::*;
Expand Down Expand Up @@ -130,12 +131,16 @@ impl ZSharpFE {
g.file_stack_push(i.file);
g.generics_stack_push(HashMap::new());

let block_start = Instant::now();
let (blks, entry_bl, inputs) = g.bl_gen_entry_fn("main");
let (blks, entry_bl, input_liveness) =
g.optimize_block::<INTERPRET_VERBOSE>(blks, entry_bl, inputs.clone(), i.no_opt);
let (blks, entry_bl, io_size, _, _, _, _) =
g.process_block::<INTERPRET_VERBOSE, 1>(blks, entry_bl);
let block_time = block_start.elapsed();
println!("\n--\nBlock time: {}ms", block_time.as_millis());

let interpret_start = Instant::now();
println!("\n\n--\nInterpretation:");
let (
ret,
Expand All @@ -162,7 +167,10 @@ impl ZSharpFE {
// prover::print_state_list(&bl_exec_state);
// let _ = prover::sort_by_block(&bl_exec_state);
// let _ = prover::sort_by_mem(&bl_exec_state);
let interpret_time = interpret_start.elapsed();
println!("\n--\nInterpret time: {}ms", interpret_time.as_millis());

let convert_start = Instant::now();
// A vector of all the blocks executed
let mut block_id_list = Vec::new();
// Convert bl_exec_state to list of String -> Value hashmaps
Expand Down Expand Up @@ -389,6 +397,9 @@ impl ZSharpFE {
.concat()
})
.collect();
let convert_time = convert_start.elapsed();
println!("\n--\nConvert time: {}ms", convert_time.as_millis());

return (
ret,
block_id_list,
Expand Down
36 changes: 36 additions & 0 deletions spartan_parallel/src/custom_dense_mlpoly.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![allow(clippy::too_many_arguments)]
use std::cmp::min;

use rayon::iter::{IntoParallelIterator, ParallelIterator};

use crate::dense_mlpoly::DensePolynomial;
use crate::scalar::SpartanExtensionField;

Expand Down Expand Up @@ -244,6 +246,33 @@ impl<S: SpartanExtensionField> DensePolynomialPqx<S> {
}
}

// Bound the first variable of "q" section to r
pub fn bound_poly_q_parallel(&mut self, r: &S) {
self.max_num_proofs /= 2;

for p in 0..min(self.num_instances, self.Z.len()) {
if self.num_proofs[p] == 1 {
for w in 0..min(self.num_witness_secs, self.Z[p][0].len()) {
for x in 0..self.num_inputs[p] {
self.Z[p][0][w][x] = (S::field_one() - *r) * self.Z[p][0][w][x];
}
}
} else {
self.num_proofs[p] /= 2;
let num_proofs = self.num_proofs[p];
self.Z[p] = self.Z[p][..].split_at_mut(num_proofs).into_par_iter().map(|(left, right)| {
for w in 0..min(self.num_witness_secs, left.len()) {
for x in 0..self.num_inputs[p] {
left[w][x] = left[w][x] + *r * (right[w][x] - left[w][x]);
}
}
left.clone()
}
).collect();
}
}
}

// Bound the first variable of "w" section to r
pub fn bound_poly_w(&mut self, r: &S) {
self.num_witness_secs /= 2;
Expand Down Expand Up @@ -304,6 +333,13 @@ impl<S: SpartanExtensionField> DensePolynomialPqx<S> {
}
}

// Bound the entire "q_rev" section to r_q
pub fn bound_poly_vars_rq_parallel(&mut self, r_q: &Vec<S>) {
for r in r_q {
self.bound_poly_q_parallel(r);
}
}

// Bound the entire "w" section to r_w
pub fn bound_poly_vars_rw(&mut self, r_w: &Vec<S>) {
for r in r_w {
Expand Down
3 changes: 1 addition & 2 deletions spartan_parallel/src/r1csproof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSProof<S> {
);
timer_tmp.stop();
let timer_tmp = Timer::new("prove_z_bind");
Z_poly.bound_poly_vars_rq(&rq_rev.to_vec());
Z_poly.bound_poly_vars_rq_parallel(&rq_rev.to_vec());
timer_tmp.stop();

// An Eq function to match p with rp
Expand Down Expand Up @@ -595,7 +595,6 @@ impl<S: SpartanExtensionField + Send + Sync> R1CSProof<S> {
S::append_field_to_transcript(b"Bz_claim", transcript, Bz_claim);
S::append_field_to_transcript(b"Cz_claim", transcript, Cz_claim);

// debug_zk
assert_eq!(taus_bound_rx * (Az_claim * Bz_claim - Cz_claim), claim_post_phase_1);

// derive three public challenges and then derive a joint claim
Expand Down

0 comments on commit 67b3ea4

Please sign in to comment.