Skip to content

Increase testability of constraint failures #351

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions triton-vm/src/shared_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ impl TestableProgram {
} = self;

let claim = Claim::about_program(&program).with_input(public_input.clone());
// todo: the problem is that this `unwrap` does not work if the initial
// instruction that is under test is an illegal initial instruction.
// It's unclear what to do about this. I _want_ an execution trace, so
// returning an error is not an option. Generally, I don't want to
// produce an execution trace if the program is not valid – except,
// here, I need it!
let (aet, stdout) = VM::trace_execution(program, public_input, non_determinism).unwrap();
let claim = claim.with_output(stdout);

Expand Down
51 changes: 51 additions & 0 deletions triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2450,6 +2450,57 @@ pub(crate) mod tests {
Ok(())
}

#[test]
fn constraints_evaluate_to_zero_for_legal_initial_instructions() -> ConstraintResult {
fn is_legal_initial_instruction(instruction: Instruction) -> bool {
match instruction {
_ if instruction.op_stack_size_influence() < 0 => false, // op-stack too small
Instruction::Recurse | Instruction::Return => false, // empty jump-stack
Instruction::RecurseOrReturn => false, // empty jump-stack
Instruction::SpongeAbsorbMem => false, // uninitialized Sponge
Instruction::SpongeSqueeze => false, // uninitialized Sponge
Instruction::Invert => false, // inverse of 0
Instruction::Log2Floor => false, // log of 0
Instruction::DivMod => false, // division by 0
Instruction::XInvert => false, // inverse of 0
_ => true,
}
}

for instruction in Instruction::iter() {
dbg!(instruction);
let instruction = instruction.map_call_address(|_| bfe!(2)); // hack: `call foo`
let program = triton_program!( {instruction} halt foo: return );

let non_determinism =
NonDeterminism::new(bfe_vec![42; 5]).with_digests([Digest::new(bfe_array![42; 5])]);
let program_and_input = TestableProgram::new(program)
.with_input(bfe_vec![42; 5])
.with_non_determinism(non_determinism);
let constraint_check_result = triton_constraints_evaluate_to_zero(program_and_input);

if is_legal_initial_instruction(instruction) {
constraint_check_result?;
continue;
}

let Err(ConstraintErrorCollection { table, errors }) = constraint_check_result else {
panic!("initial {instruction} must fail at least one constraint");
};
if table != "ProcessorTable" {
panic!("expected failed processor constraint for {instruction}, found {table}");
}
let some_initial_constraint_failed = errors
.into_iter()
.any(|err| matches!(err, ConstraintError::Initial { .. }));
if !some_initial_constraint_failed {
panic!("initial {instruction} must fail some initial constraint");
}
}

Ok(())
}

#[test]
fn constraints_evaluate_to_zero_on_fibonacci() -> ConstraintResult {
let program = TestableProgram::new(crate::example_programs::FIBONACCI_SEQUENCE.clone())
Expand Down
Loading