Skip to content

Commit 49189e5

Browse files
committed
Debug in progress
1 parent 67b3ea4 commit 49189e5

File tree

9 files changed

+964
-10289
lines changed

9 files changed

+964
-10289
lines changed

circ_blocks/aaa

Lines changed: 152 additions & 5068 deletions
Large diffs are not rendered by default.

circ_blocks/bbb

Lines changed: 92 additions & 5179 deletions
Large diffs are not rendered by default.

circ_blocks/ccc

Lines changed: 637 additions & 0 deletions
Large diffs are not rendered by default.

circ_blocks/examples/zxc.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// TODO: Might want to simplify Liveness Analysis & PMR now that scope changes are handled in optimization
22

33
const PRINT_PROOF: bool = false;
4-
const INLINE_SPARTAN_PROOF: bool = true;
4+
const INLINE_SPARTAN_PROOF: bool = false;
55
const TOTAL_NUM_VARS_BOUND: usize = 10000000000;
66
const MAX_FILE_SIZE: usize = 1073741824;
77

@@ -951,9 +951,15 @@ fn get_run_time_knowledge<const VERBOSE: bool, S: SpartanExtensionField + Send +
951951
}
952952
let mut evaluator = StagedWitCompEvaluator::new(&prover_data_list[id].precompute);
953953
let mut eval = Vec::new();
954+
if i == 0 {
955+
println!("INPUT: {:?}", input);
956+
}
954957
eval.extend(evaluator.eval_stage(input).into_iter().cloned());
955958
// Drop the last entry of io, which is the dummy return 0
956959
eval.pop();
960+
if i == 0 {
961+
println!("EVAL: {:?}", eval);
962+
}
957963
eval.extend(
958964
evaluator
959965
.eval_stage(Default::default())

circ_blocks/src/front/zsharp/blocks.rs

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3183,11 +3183,11 @@ impl<'ast> ZGen<'ast> {
31833183
}
31843184

31853185
// Convert a list of blocks to circ_ir
3186-
pub fn bls_to_circ(&'ast self, blks: &Vec<Block>) {
3186+
pub fn bls_to_circ<const PROVER: bool>(&'ast self, blks: &Vec<Block>) {
31873187
for b in blks {
31883188
let f = &format!("Block_{}", b.name);
31893189
self.circ_init_block(f);
3190-
self.bl_to_circ::<false>(&b, f);
3190+
self.bl_to_circ::<false, PROVER>(&b, f);
31913191
}
31923192
}
31933193

@@ -3721,7 +3721,8 @@ impl<'ast> ZGen<'ast> {
37213721
// Convert a block to circ_ir
37223722
// This can be done to either produce the constraints, or to estimate the size of constraints
37233723
// In estimation mode, we rename all output variable from X -> oX, add assertion, and process the terminator
3724-
pub fn bl_to_circ<const ESTIMATE: bool>(&self, b: &Block, f: &str) {
3724+
// If in prover mode, declare all block outputs at the end of the block
3725+
pub fn bl_to_circ<const ESTIMATE: bool, const PROVER: bool>(&self, b: &Block, f: &str) {
37253726
debug!("Bl to Circ: {}", b.name);
37263727

37273728
// setup stack frame for entry function
@@ -3958,20 +3959,36 @@ impl<'ast> ZGen<'ast> {
39583959

39593960
if let Some(r) = self.circ_exit_fn() {
39603961
match self.mode {
3961-
Mode::Mpc(_) => {
3962-
let ret_term = r.unwrap_term();
3963-
let ret_terms = ret_term.terms();
3964-
self.circ
3965-
.borrow()
3966-
.cir_ctx()
3967-
.cs
3968-
.borrow_mut()
3969-
.get_mut(f)
3970-
.unwrap()
3971-
.outputs
3972-
.extend(ret_terms);
3973-
}
39743962
Mode::Proof => {
3963+
if PROVER {
3964+
for (o_name, o_ty) in &b.outputs {
3965+
if let Some(o_ty) = o_ty {
3966+
println!("CVARS_STACK: {:?}", self.cvars_stack);
3967+
println!("O_NAME: {}", o_name);
3968+
let o_val = self.cvar_lookup(&o_name).unwrap();
3969+
let o_var_val = self
3970+
.circ_declare_input(
3971+
&f,
3972+
o_name.to_string(),
3973+
o_ty,
3974+
ZVis::Public,
3975+
Some(o_val.clone()),
3976+
false,
3977+
&None,
3978+
)
3979+
.expect(&format!("circ_declare {o_name}"));
3980+
let o_eq = eq(o_val, o_var_val).unwrap().term;
3981+
let mut assertions = std::mem::take(&mut *self.assertions.borrow_mut());
3982+
let o_assert = if assertions.is_empty() {
3983+
o_eq
3984+
} else {
3985+
assertions.push(o_eq);
3986+
term(AND, assertions)
3987+
};
3988+
self.circ.borrow_mut().assert(&f, o_assert);
3989+
}
3990+
}
3991+
}
39753992
let ty = ret_ty.as_ref().unwrap();
39763993
let name = "return".to_owned();
39773994
let ret_val = r.unwrap_term();
@@ -3996,28 +4013,6 @@ impl<'ast> ZGen<'ast> {
39964013
};
39974014
self.circ.borrow_mut().assert(&f, to_assert);
39984015
}
3999-
Mode::Opt => {
4000-
let ret_term = r.unwrap_term();
4001-
let ret_terms = ret_term.terms();
4002-
assert!(
4003-
ret_terms.len() == 1,
4004-
"When compiling to optimize, there can only be one output"
4005-
);
4006-
let t = ret_terms.into_iter().next().unwrap();
4007-
let t_sort = check(&t);
4008-
if !matches!(t_sort, Sort::BitVector(_)) {
4009-
panic!("Cannot maximize output of type {}", t_sort);
4010-
}
4011-
self.circ
4012-
.borrow()
4013-
.cir_ctx()
4014-
.cs
4015-
.borrow_mut()
4016-
.get_mut(f)
4017-
.unwrap()
4018-
.outputs
4019-
.push(t);
4020-
}
40214016
_ => {
40224017
panic!("Supported Mode!")
40234018
}

circ_blocks/src/front/zsharp/blocks_optimization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2763,7 +2763,7 @@ impl<'ast> ZGen<'ast> {
27632763
fn bl_count_num_cons(&self, bl: &Block<'ast>) -> usize {
27642764
let block_name = &format!("Pseudo_Block_{}", bl.name);
27652765
self.circ_init_block(block_name);
2766-
self.bl_to_circ::<true>(bl, block_name);
2766+
self.bl_to_circ::<true, false>(bl, block_name);
27672767

27682768
let mut cs = Computations::new();
27692769
cs.comps = self.circ.borrow().cir_ctx().cs.borrow_mut().clone();

circ_blocks/src/front/zsharp/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl FrontEnd for ZSharpFE {
8787
// NOTE: The input of block 0 includes %BN, which should be removed when reasoning about function input
8888
let func_input_width = blks[0].get_num_inputs() - 1;
8989
println!("\n\n--\nCirc IR:");
90-
g.bls_to_circ(&blks);
90+
g.bls_to_circ::<false>(&blks);
9191

9292
g.generics_stack_pop();
9393
g.file_stack_pop();
@@ -400,6 +400,15 @@ impl ZSharpFE {
400400
let convert_time = convert_start.elapsed();
401401
println!("\n--\nConvert time: {}ms", convert_time.as_millis());
402402

403+
g.bls_to_circ::<true>(&blks);
404+
g.generics_stack_pop();
405+
g.file_stack_pop();
406+
let mut cs = Computations::new();
407+
cs.comps = g.into_circify().cir_ctx().cs.borrow_mut().clone();
408+
for (name, c) in cs.comps {
409+
println!("{}: {:?}", name, c);
410+
}
411+
403412
return (
404413
ret,
405414
block_id_list,
@@ -1415,7 +1424,7 @@ impl<'ast> ZGen<'ast> {
14151424
.map_err(|e| format!("{e}"))
14161425
}
14171426
ast::Statement::Assertion(e) => {
1418-
match self.expr_impl_::<true>(&e.expression).and_then(|v| {
1427+
match self.expr_impl_::<IS_CNST>(&e.expression).and_then(|v| {
14191428
const_bool(v)
14201429
.ok_or_else(|| "interpreting expr as const bool failed".to_string())
14211430
}) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
low 0
2+
b 0
3+
c 0
4+
high 800
5+
END
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def main(u32 low, u32 b, u32 c, u32 high) -> u32:
2+
array_decl u32[ro high] arr
3+
u32 sum = 0
4+
for u32 i in 0..high do
5+
sum = sum + i
6+
arr[i] = sum
7+
endfor
8+
9+
10+
// random access to ensure array is written in memory
11+
u32 x = arr[b]
12+
13+
u32 min = arr[low]
14+
u32 min_idx = low
15+
16+
u32 i = low + 1
17+
while i < high do
18+
if arr[i] < min then
19+
min = arr[i]
20+
min_idx = i
21+
endif
22+
i = i + 1
23+
endwhile
24+
25+
26+
return min + (min_idx + x) * c

0 commit comments

Comments
 (0)