Skip to content

Commit c275ef4

Browse files
authored
Merge pull request #259 from 0xnullifier/log-string
Adds generic `self.print_log` and logging for string literal
2 parents 6f8db33 + 2038c48 commit c275ef4

File tree

17 files changed

+547
-302
lines changed

17 files changed

+547
-302
lines changed

examples/log.no

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
struct Thing {
3+
xx: Field,
4+
yy: Field
5+
}
6+
7+
fn main(pub public_input: Field) -> Field {
8+
9+
log(1234);
10+
log(true);
11+
12+
let arr = [1,2,3];
13+
log(arr);
14+
15+
let thing = Thing { xx : public_input , yy: public_input + 1};
16+
log("formatted string with a number {} boolean {} arr {} struct {}" , 1234,true, arr , thing);
17+
18+
return public_input + 1;
19+
}

src/backends/kimchi/mod.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
backends::kimchi::asm::parse_coeffs,
2323
circuit_writer::{
2424
writer::{AnnotatedCell, Cell, PendingGate},
25-
DebugInfo, Gate, GateKind, Wiring,
25+
DebugInfo, Gate, GateKind, VarInfo, Wiring,
2626
},
2727
compiler::Sources,
2828
constants::Span,
@@ -128,6 +128,9 @@ pub struct KimchiVesta {
128128
/// Indexes used by the private inputs
129129
/// (this is useful to check that they appear in the circuit)
130130
pub(crate) private_input_cell_vars: Vec<KimchiCellVar>,
131+
132+
/// Log information
133+
pub(crate) log_info: Vec<(Span, VarInfo<VestaField, KimchiCellVar>)>,
131134
}
132135

133136
impl Witness {
@@ -174,6 +177,7 @@ impl KimchiVesta {
174177
finalized: false,
175178
public_input_size: 0,
176179
private_input_cell_vars: vec![],
180+
log_info: vec![],
177181
}
178182
}
179183

@@ -418,11 +422,11 @@ impl Backend for KimchiVesta {
418422
self.compute_val(env, val, var.index)
419423
}
420424

421-
fn generate_witness<B: Backend>(
425+
fn generate_witness(
422426
&self,
423427
witness_env: &mut WitnessEnv<VestaField>,
424428
sources: &Sources,
425-
_typed: &Mast<B>,
429+
typed: &Mast<Self>,
426430
) -> Result<GeneratedWitness> {
427431
if !self.finalized {
428432
unreachable!("the circuit must be finalized before generating a witness");
@@ -471,6 +475,7 @@ impl Backend for KimchiVesta {
471475
}
472476
public_outputs.push(val);
473477
}
478+
self.print_log(witness_env, &self.log_info, sources, typed)?;
474479

475480
// sanity check the witness
476481
for (row, (gate, witness_row, debug_info)) in
@@ -799,9 +804,8 @@ impl Backend for KimchiVesta {
799804
fn log_var(
800805
&mut self,
801806
var: &crate::circuit_writer::VarInfo<Self::Field, Self::Var>,
802-
msg: String,
803807
span: Span,
804808
) {
805-
println!("todo: implement log_var for kimchi backend");
809+
self.log_info.push((span, var.clone()));
806810
}
807811
}

src/backends/mod.rs

+95-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use crate::{
1313
error::{Error, ErrorKind, Result},
1414
helpers::PrettyField,
1515
imports::FnHandle,
16+
parser::types::TyKind,
17+
utils::{log_array_type, log_custom_type, log_string_type},
1618
var::{ConstOrCell, Value, Var},
1719
witness::WitnessEnv,
1820
};
@@ -413,15 +415,105 @@ pub trait Backend: Clone {
413415
) -> Result<()>;
414416

415417
/// Generate the witness for a backend.
416-
fn generate_witness<B: Backend>(
418+
fn generate_witness(
417419
&self,
418420
witness_env: &mut WitnessEnv<Self::Field>,
419421
sources: &Sources,
420-
typed: &Mast<B>,
422+
typed: &Mast<Self>,
421423
) -> Result<Self::GeneratedWitness>;
422424

423425
/// Generate the asm for a backend.
424426
fn generate_asm(&self, sources: &Sources, debug: bool) -> String;
425427

426-
fn log_var(&mut self, var: &VarInfo<Self::Field, Self::Var>, msg: String, span: Span);
428+
fn log_var(&mut self, var: &VarInfo<Self::Field, Self::Var>, span: Span);
429+
430+
/// print the log given the log_info
431+
fn print_log(
432+
&self,
433+
witness_env: &mut WitnessEnv<Self::Field>,
434+
logs: &[(Span, VarInfo<Self::Field, Self::Var>)],
435+
sources: &Sources,
436+
typed: &Mast<Self>,
437+
) -> Result<()> {
438+
let mut logs_iter = logs.into_iter();
439+
while let Some((span, var_info)) = logs_iter.next() {
440+
let (filename, source) = sources.get(&span.filename_id).unwrap();
441+
let (line, _, _) = crate::utils::find_exact_line(source, *span);
442+
let dbg_msg = format!("[{filename}:{line}] -> ");
443+
444+
match &var_info.typ {
445+
// Field
446+
Some(TyKind::Field { .. }) => match &var_info.var[0] {
447+
ConstOrCell::Const(cst) => {
448+
println!("{dbg_msg}{}", cst.pretty());
449+
}
450+
ConstOrCell::Cell(cell) => {
451+
let val = self.compute_var(witness_env, cell)?;
452+
println!("{dbg_msg}{}", val.pretty());
453+
}
454+
},
455+
456+
// Bool
457+
Some(TyKind::Bool) => match &var_info.var[0] {
458+
ConstOrCell::Const(cst) => {
459+
let val = *cst == Self::Field::one();
460+
println!("{dbg_msg}{}", val);
461+
}
462+
ConstOrCell::Cell(cell) => {
463+
let val = self.compute_var(witness_env, cell)? == Self::Field::one();
464+
println!("{dbg_msg}{}", val);
465+
}
466+
},
467+
468+
// Array
469+
Some(TyKind::Array(b, s)) => {
470+
let (output, remaining) =
471+
log_array_type(self, &var_info.var.cvars, b, *s, witness_env, typed, span)?;
472+
assert!(remaining.is_empty());
473+
println!("{dbg_msg}{}", output);
474+
}
475+
476+
// Custom types
477+
Some(TyKind::Custom {
478+
module,
479+
name: struct_name,
480+
}) => {
481+
let mut string_vec = Vec::new();
482+
let (output, remaining) = log_custom_type(
483+
self,
484+
module,
485+
struct_name,
486+
typed,
487+
&var_info.var.cvars,
488+
witness_env,
489+
span,
490+
&mut string_vec,
491+
)?;
492+
assert!(remaining.is_empty());
493+
println!("{dbg_msg}{}{}", struct_name, output);
494+
}
495+
496+
// GenericSizedArray
497+
Some(TyKind::GenericSizedArray(_, _)) => {
498+
unreachable!("GenericSizedArray should be monomorphized")
499+
}
500+
501+
Some(TyKind::String(s)) => {
502+
let output =
503+
log_string_type(self, &mut logs_iter, s, witness_env, typed, span)?;
504+
println!("{dbg_msg}{}", output);
505+
}
506+
507+
None => {
508+
return Err(Error::new(
509+
"log",
510+
ErrorKind::UnexpectedError("No type info for logging"),
511+
*span,
512+
))
513+
}
514+
}
515+
}
516+
517+
Ok(())
518+
}
427519
}

0 commit comments

Comments
 (0)