Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test/canary: troubleshoot corrupt witness memory #597

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions ceno_zkvm/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,18 @@ impl<E: ExtensionField> ZKVMFixedTraces<E> {
input: &TC::FixedInput,
) {
let cs = cs.get_cs(&TC::name()).expect("cs not found");
let fixed = TC::generate_fixed_traces(config, cs.num_fixed, input);

assert!(
fixed.all_assigned(),
"not all assigned in table fixed {}",
TC::name()
);
tracing::trace!("All assigned in table fixed {}.", TC::name());

assert!(
self.circuit_fixed_traces
.insert(
TC::name(),
Some(TC::generate_fixed_traces(config, cs.num_fixed, input)),
)
.insert(TC::name(), Some(fixed))
.is_none()
);
}
Expand Down Expand Up @@ -235,6 +241,14 @@ impl<E: ExtensionField> ZKVMWitnesses<E> {
let cs = cs.get_cs(&OC::name()).unwrap();
let (witness, logup_multiplicity) =
OC::assign_instances(config, cs.num_witin as usize, records)?;

assert!(
witness.all_assigned(),
"not all assigned in opcode witness {}",
OC::name()
);
tracing::trace!("All assigned in opcode witness {}.", OC::name());

assert!(self.witnesses_opcodes.insert(OC::name(), witness).is_none());
assert!(!self.witnesses_tables.contains_key(&OC::name()));
assert!(
Expand Down Expand Up @@ -287,6 +301,14 @@ impl<E: ExtensionField> ZKVMWitnesses<E> {
self.combined_lk_mlt.as_ref().unwrap(),
input,
)?;

assert!(
witness.all_assigned(),
"not all assigned in table witness {}",
TC::name()
);
tracing::trace!("All assigned in table witness {}.", TC::name());

assert!(self.witnesses_tables.insert(TC::name(), witness).is_none());
assert!(!self.witnesses_opcodes.contains_key(&TC::name()));

Expand Down
28 changes: 25 additions & 3 deletions ceno_zkvm/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,39 @@ pub struct RowMajorMatrix<T: Sized + Sync + Clone + Send + Copy> {
num_col: usize,
}

impl<T: Sized + Sync + Clone + Send + Copy> RowMajorMatrix<T> {
impl<T: Sized + Sync + Clone + Send + Copy + From<u64> + Eq> RowMajorMatrix<T> {
pub fn new(num_rows: usize, num_col: usize) -> Self {
let num_total_rows = next_pow2_instance_padding(num_rows);
let num_padding_rows = num_total_rows - num_rows;
RowMajorMatrix {
let mut x = RowMajorMatrix {
values: create_uninit_vec(num_total_rows * num_col),
num_padding_rows,
num_col,
}
};

let c = Self::canary();
x.values.iter_mut().for_each(|v| *v = MaybeUninit::new(c));
x
}

pub fn all_assigned(&self) -> bool {
let c = Self::canary();
self.values
.iter()
.map(|v| {
let v = unsafe { v.assume_init() };
v != c
})
.all(|ok| ok)
}

/// Some random value that must be overwritten.
fn canary() -> T {
T::from(8979837498374919_u64)
}
}

impl<T: Sized + Sync + Clone + Send + Copy> RowMajorMatrix<T> {
pub fn num_instances(&self) -> usize {
self.values.len() / self.num_col - self.num_padding_rows
}
Expand Down