Skip to content

Commit

Permalink
refactor(test): Simplify constraint checking, etc
Browse files Browse the repository at this point in the history
- integrate cross-table constraint check with check of all constraints
- remove ad-hoc proving & verification code through use of one function
- remove duplicate tests

changelog: ignore
  • Loading branch information
jan-ferdinand committed Jul 22, 2024
1 parent 0baff70 commit 6fd207f
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 460 deletions.
26 changes: 8 additions & 18 deletions triton-vm/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ pub(crate) fn cache_lde_trace() -> Option<CacheDecision> {

#[cfg(test)]
mod tests {
use assert2::assert;
use twenty_first::prelude::*;

use crate::example_programs::FIBONACCI_SEQUENCE;
use crate::prelude::*;
use crate::shared_tests::prove_with_low_security_level;
use crate::shared_tests::prove_and_verify;
use crate::shared_tests::ProgramAndInput;
use crate::shared_tests::DEFAULT_LOG2_FRI_EXPANSION_FACTOR_FOR_TESTS;

use super::*;

Expand All @@ -109,20 +108,11 @@ mod tests {
}

fn prove_and_verify_a_triton_vm_program() {
let stdin = PublicInput::from(bfe_array![100]);
let secret_in = NonDeterminism::default();

let log2_fri_expansion_factor = 2;
crate::profiler::start("Prove Fib 100");
let (stark, claim, proof) = prove_with_low_security_level(
&FIBONACCI_SEQUENCE,
stdin,
secret_in,
log2_fri_expansion_factor,
let program_and_input = ProgramAndInput::new(FIBONACCI_SEQUENCE.clone())
.with_input(PublicInput::from(bfe_array![100]));
prove_and_verify(
program_and_input,
DEFAULT_LOG2_FRI_EXPANSION_FACTOR_FOR_TESTS,
);
assert!(let Ok(()) = stark.verify(&claim, &proof));

let profile = crate::profiler::finish();
println!("{profile}");
}
}
6 changes: 3 additions & 3 deletions triton-vm/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ mod tests {

use super::*;

// For testing purposes only.
impl Default for Claim {
/// For testing purposes only.
fn default() -> Self {
Self::new(Digest::default())
}
Expand All @@ -115,7 +115,7 @@ mod tests {
}

#[proptest(cases = 10)]
fn proof_with_no_log_2_padded_height_gives_err(#[strategy(arb())] root: Digest) {
fn proof_with_no_padded_height_gives_err(#[strategy(arb())] root: Digest) {
let mut proof_stream = ProofStream::new();
proof_stream.enqueue(ProofItem::MerkleRoot(root));
let proof: Proof = proof_stream.into();
Expand All @@ -124,7 +124,7 @@ mod tests {
}

#[proptest(cases = 10)]
fn proof_with_multiple_log_2_padded_height_gives_err(#[strategy(arb())] root: Digest) {
fn proof_with_multiple_padded_height_gives_err(#[strategy(arb())] root: Digest) {
let mut proof_stream = ProofStream::new();
proof_stream.enqueue(ProofItem::Log2PaddedHeight(8));
proof_stream.enqueue(ProofItem::MerkleRoot(root));
Expand Down
55 changes: 41 additions & 14 deletions triton-vm/src/shared_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use assert2::assert;
use assert2::let_assert;
use num_traits::Zero;
use proptest::collection::vec;
use proptest::prelude::*;
Expand All @@ -8,15 +10,17 @@ use twenty_first::prelude::*;
use crate::aet::AlgebraicExecutionTrace;
use crate::error::VMError;
use crate::fri::AuthenticationStructure;
use crate::profiler::profiler;
use crate::program::Program;
use crate::proof::Claim;
use crate::proof::Proof;
use crate::proof_item::FriResponse;
use crate::stark::Stark;
use crate::table::master_table::MasterBaseTable;
use crate::NonDeterminism;
use crate::PublicInput;

pub(crate) const DEFAULT_LOG2_FRI_EXPANSION_FACTOR_FOR_TESTS: usize = 2;

#[derive(Debug, Copy, Clone, Eq, PartialEq, Arbitrary)]
#[filter(!#self.0.is_zero())]
struct NonZeroXFieldElement(#[strategy(arb())] XFieldElement);
Expand All @@ -42,7 +46,7 @@ prop_compose! {
false => vec![],
};
let polynomial = Polynomial::new(coefficients);
assert_eq!(degree, polynomial.degree() as i64);
assert!(degree== polynomial.degree() as i64);
polynomial
}
}
Expand Down Expand Up @@ -92,25 +96,47 @@ impl LeavedMerkleTreeTestData {
}
}

/// Convenience function to prove correct execution of the given program.
pub(crate) fn prove_with_low_security_level(
program: &Program,
public_input: PublicInput,
non_determinism: NonDeterminism,
log2_fri_expansion_factor: usize,
) -> (Stark, Claim, Proof) {
/// Prove correct execution of the supplied program, then verify said proof.
/// Also print the [`VMPerformanceProfile`][profile] for both proving and
/// verification to standard out.
///
/// [profile]: crate::profiler::VMPerformanceProfile
pub(crate) fn prove_and_verify(
program_and_input: ProgramAndInput,
log_2_fri_expansion_factor: usize,
) {
let ProgramAndInput {
program,
public_input,
non_determinism,
} = program_and_input;

profiler!(start "Pre-flight");
let (aet, public_output) = program
.trace_execution(public_input.clone(), non_determinism)
.trace_execution(public_input.clone(), non_determinism.clone())
.unwrap();

let claim = Claim::about_program(&aet.program)
.with_input(public_input.individual_tokens)
let claim = Claim::about_program(&program)
.with_input(public_input.individual_tokens.clone())
.with_output(public_output);
let stark = low_security_stark(log_2_fri_expansion_factor);
profiler!(stop "Pre-flight");

let stark = low_security_stark(log2_fri_expansion_factor);
profiler!(start "Prove");
let proof = stark.prove(&claim, &aet).unwrap();
profiler!(stop "Prove");

profiler!(start "Verify");
assert!(let Ok(()) = stark.verify(&claim, &proof));
profiler!(stop "Verify");
let profile = crate::profiler::finish();

(stark, claim, proof)
let_assert!(Ok(padded_height) = proof.padded_height());
let fri = stark.derive_fri(padded_height).unwrap();
let profile = profile
.with_padded_height(padded_height)
.with_fri_domain_len(fri.domain.length);
println!("{profile}");
}

pub(crate) fn low_security_stark(log_expansion_factor: usize) -> Stark {
Expand All @@ -135,6 +161,7 @@ pub(crate) fn construct_master_base_table(
}

/// Program and associated inputs.
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct ProgramAndInput {
pub program: Program,
pub public_input: PublicInput,
Expand Down
Loading

0 comments on commit 6fd207f

Please sign in to comment.