Skip to content

Commit

Permalink
feat: add a watcher (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirhemo authored Nov 21, 2023
1 parent 11f52dd commit bd14c11
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
26 changes: 22 additions & 4 deletions curta/src/chip/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use super::table::lookup::table::LookupTable;
use super::table::lookup::values::LookupValues;
use super::trace::data::AirTraceData;
use super::{AirParameters, Chip};
use crate::chip::register::RegisterSerializable;

#[derive(Debug, Clone)]
#[allow(clippy::type_complexity)]
Expand Down Expand Up @@ -106,6 +107,19 @@ impl<L: AirParameters> AirBuilder<L> {
array
}

/// Prints out a log message (using the log::debug! macro) with the value of the register.
///
/// The message will be presented with `RUST_LOG=debug` or `RUST_LOG=trace`.
pub fn watch(&mut self, data: &impl Register, name: &str) {
let register = ArrayRegister::from_register_unsafe(*data.register());
let instruction = AirInstruction::Watch(name.to_string(), register);
if data.is_trace() {
self.register_air_instruction_internal(instruction);
} else {
self.register_global_air_instruction_internal(instruction);
}
}

/// Registers an custom instruction with the builder.
pub fn register_instruction<I>(&mut self, instruction: I)
where
Expand Down Expand Up @@ -317,6 +331,7 @@ pub(crate) mod tests {
type Instruction = EmptyInstruction<GoldilocksField>;
const NUM_ARITHMETIC_COLUMNS: usize = 0;
const NUM_FREE_COLUMNS: usize = 2;
const EXTENDED_COLUMNS: usize = 0;
}

#[test]
Expand Down Expand Up @@ -371,14 +386,18 @@ pub(crate) mod tests {
type L = FibonacciParameters;
type SC = PoseidonGoldilocksStarkConfig;

let _ = env_logger::builder().is_test(true).try_init();

let mut builder = AirBuilder::<L>::new();
let x_0 = builder.alloc::<ElementRegister>();
let x_1 = builder.alloc::<ElementRegister>();

// x0' <- x1
let constr_1 = builder.set_to_expression_transition(&x_0.next(), x_1.expr());
builder.set_to_expression_transition(&x_0.next(), x_1.expr());
// x1' <- x0 + x1
let constr_2 = builder.set_to_expression_transition(&x_1.next(), x_0.expr() + x_1.expr());
builder.set_to_expression_transition(&x_1.next(), x_0.expr() + x_1.expr());

builder.watch(&x_1, "x_1 fib");

let num_rows = 1 << 10;
let public_inputs = [
Expand All @@ -399,8 +418,7 @@ pub(crate) mod tests {
writer.write(&x_1, &F::ONE, 0);

for i in 0..num_rows {
writer.write_instruction(&constr_1, i);
writer.write_instruction(&constr_2, i);
writer.write_row_instructions(&generator.air_data, i);
}
let stark = Starky::new(air);
let config = SC::standard_fast_config(num_rows);
Expand Down
18 changes: 18 additions & 0 deletions curta/src/chip/instruction/set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloc::sync::Arc;

use log::debug;
use serde::{Deserialize, Serialize};

use super::assign::AssignInstruction;
Expand All @@ -12,6 +13,8 @@ use crate::air::AirConstraint;
use crate::chip::arithmetic::expression::ArithmeticExpression;
use crate::chip::bool::SelectInstruction;
use crate::chip::memory::instruction::MemoryInstruction;
use crate::chip::register::array::ArrayRegister;
use crate::chip::register::element::ElementRegister;
use crate::chip::register::memory::MemorySlice;
use crate::chip::trace::writer::TraceWriter;
use crate::math::prelude::*;
Expand All @@ -27,6 +30,7 @@ pub enum AirInstruction<F, I> {
ProcessId(ProcessIdInstruction),
Filtered(ArithmeticExpression<F>, Arc<Self>),
Mem(MemoryInstruction),
Watch(String, ArrayRegister<ElementRegister>),
}

impl<F: Field, AP: AirParser<Field = F>, I> AirConstraint<AP> for AirInstruction<F, I>
Expand Down Expand Up @@ -58,6 +62,7 @@ where
}
}
AirInstruction::Mem(i) => AirConstraint::<AP>::eval(i, parser),
AirInstruction::Watch(_, _) => {}
}
}
}
Expand All @@ -79,6 +84,10 @@ impl<F: Field, I: Instruction<F>> Instruction<F> for AirInstruction<F, I> {
}
}
AirInstruction::Mem(i) => Instruction::<F>::write(i, writer, row_index),
AirInstruction::Watch(name, register) => {
let value = writer.read_vec(register, row_index);
debug!("row {}: , {}: {:?}", row_index, name, value);
}
}
}

Expand All @@ -98,6 +107,15 @@ impl<F: Field, I: Instruction<F>> Instruction<F> for AirInstruction<F, I> {
}
}
AirInstruction::Mem(i) => i.write_to_air(writer),
AirInstruction::Watch(name, register) => {
let value = writer.read_vec(register);
let row_index = writer.row_index();
if let Some(index) = row_index {
debug!("row {}: , {}: {:?}", index, name, value);
} else {
debug!("{}: {:?}", name, value);
}
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions curta/src/machine/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ pub trait Builder: Sized {
register
}

/// Prints out a log message (using the log::debug! macro) with the value of the register.
///
/// The message will be presented with `RUST_LOG=debug` or `RUST_LOG=trace`.
fn watch(&mut self, data: &impl Register, name: &str) {
self.api().watch(data, name);
}

/// Computes the expression `expression` and returns the result as a public register of type `T`.
fn public_expression<T: Register>(
&mut self,
Expand Down
3 changes: 2 additions & 1 deletion curta/src/machine/stark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,8 @@ mod tests {

let a = builder.alloc::<FieldRegister<Fp25519>>();
let b = builder.alloc::<FieldRegister<Fp25519>>();
let _ = builder.add(a, b);
let c = builder.add(a, b);
builder.watch(&c, "c");

let num_rows = 1 << 16;
let stark = builder.build::<C, 2>(num_rows);
Expand Down

0 comments on commit bd14c11

Please sign in to comment.