Skip to content

Commit

Permalink
removes string from log_info vars in backends and double print code
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnullifier committed Jan 7, 2025
1 parent 20dcc63 commit b26eebe
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 44 deletions.
35 changes: 2 additions & 33 deletions src/backends/kimchi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub struct KimchiVesta {
pub(crate) private_input_cell_vars: Vec<KimchiCellVar>,

/// Log information
pub(crate) log_info: Vec<(String, Span, VarInfo<VestaField, KimchiCellVar>)>,
pub(crate) log_info: Vec<(Span, VarInfo<VestaField, KimchiCellVar>)>,
}

impl Witness {
Expand Down Expand Up @@ -482,36 +482,6 @@ impl Backend for KimchiVesta {
}
self.print_log(witness_env, &self.log_info, sources, typed)?;

// output log values to the console
for (_, span, var_info) in &self.log_info {
let (filename, source) = sources.get(&span.filename_id).unwrap();
let (line, _, line_str) = crate::utils::find_exact_line(source, *span);
let line_str = line_str.trim_start();
let dbg_msg = format!("[{filename}:{line}] `{line_str}` -> ");
match &var_info.typ {
Some(TyKind::Field { .. }) => match &var_info.var[0] {
ConstOrCell::Const(cst) => {
println!("{dbg_msg}{}", cst.pretty());
}
ConstOrCell::Cell(cell) => {
let val = self.compute_var(witness_env, cell)?;
println!("{dbg_msg}{}", val.pretty());
}
},
Some(TyKind::Bool) => match &var_info.var[0] {
ConstOrCell::Const(cst) => {
let val = *cst == VestaField::one();
println!("{dbg_msg}{}", val);
}
ConstOrCell::Cell(cell) => {
let val = self.compute_var(witness_env, cell)? == VestaField::one();
println!("{dbg_msg}{}", val);
}
},
_ => todo!("Type not implemented yet"),
}
}

// sanity check the witness
for (row, (gate, witness_row, debug_info)) in
izip!(self.gates.iter(), &witness, &self.debug_info).enumerate()
Expand Down Expand Up @@ -839,9 +809,8 @@ impl Backend for KimchiVesta {
fn log_var(
&mut self,
var: &crate::circuit_writer::VarInfo<Self::Field, Self::Var>,
msg: String,
span: Span,
) {
self.log_info.push((msg, span, var.clone()));
self.log_info.push((span, var.clone()));
}
}
6 changes: 3 additions & 3 deletions src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,18 @@ pub trait Backend: Clone {
/// Generate the asm for a backend.
fn generate_asm(&self, sources: &Sources, debug: bool) -> String;

fn log_var(&mut self, var: &VarInfo<Self::Field, Self::Var>, msg: String, span: Span);
fn log_var(&mut self, var: &VarInfo<Self::Field, Self::Var>, span: Span);

/// print the log given the log_info
fn print_log(
&self,
witness_env: &mut WitnessEnv<Self::Field>,
logs: &[(String, Span, VarInfo<Self::Field, Self::Var>)],
logs: &[(Span, VarInfo<Self::Field, Self::Var>)],
sources: &Sources,
typed: &Mast<Self>,
) -> Result<()> {
let mut logs_iter = logs.into_iter();
while let Some((_, span, var_info)) = logs_iter.next() {
while let Some((span, var_info)) = logs_iter.next() {
let (filename, source) = sources.get(&span.filename_id).unwrap();
let (line, _, _) = crate::utils::find_exact_line(source, *span);
let dbg_msg = format!("[{filename}:{line}] -> ");
Expand Down
6 changes: 3 additions & 3 deletions src/backends/r1cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ where
/// Debug information for each constraint.
debug_info: Vec<DebugInfo>,
/// Debug information for var info.
log_info: Vec<(String, Span, VarInfo<F, LinearCombination<F>>)>,
log_info: Vec<(Span, VarInfo<F, LinearCombination<F>>)>,
/// Record the public inputs for reordering the witness vector
public_inputs: Vec<CellVar>,
/// Record the private inputs for checking
Expand Down Expand Up @@ -685,8 +685,8 @@ where
var
}

fn log_var(&mut self, var: &VarInfo<Self::Field, Self::Var>, msg: String, span: Span) {
self.log_info.push((msg, span, var.clone()));
fn log_var(&mut self, var: &VarInfo<Self::Field, Self::Var>, span: Span) {
self.log_info.push((span, var.clone()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/stdlib/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn log_fn<B: Backend>(
) -> Result<Option<Var<B::Field, B::Var>>> {
for var in vars {
// todo: will need to support string argument in order to customize msg
compiler.backend.log_var(var, "log".to_owned(), span);
compiler.backend.log_var(var, span);
}

Ok(None)
Expand Down
7 changes: 3 additions & 4 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@ fn replace_all(

pub fn log_string_type<B: Backend>(
backend: &B,
logs_iter: &mut Iter<'_, (String, Span, VarInfo<B::Field, B::Var>)>,
logs_iter: &mut Iter<'_, (Span, VarInfo<B::Field, B::Var>)>,
str: &str,
witness: &mut WitnessEnv<B::Field>,
typed: &Mast<B>,
span: &Span,
) -> Result<String> {
let re = Regex::new(r"\{\s*\}").unwrap();
let replacer = |_: &Captures| {
let (_, span, var) = match logs_iter.next() {
Some((str, span, var)) => (str, span, var),
let (span, var) = match logs_iter.next() {
Some((span, var)) => (span, var),
None => return Err(Error::new("log", ErrorKind::InsufficientVariables, *span)),
};
let replacement = match &var.typ {
Expand Down Expand Up @@ -190,7 +190,6 @@ pub fn log_string_type<B: Backend>(
unreachable!("GenericSizedArray should be monomorphized")
}
Some(TyKind::String(_)) => todo!("String cannot be in circuit yet"),
// I cannot return an error here
None => {
return Err(Error::new(
"log",
Expand Down

0 comments on commit b26eebe

Please sign in to comment.