Skip to content

Commit

Permalink
refactor!: communicate possible STARK proving failures with Result
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Jan 24, 2024
1 parent 3fe35ad commit 5613f19
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion triton-vm/benches/prove_fib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn prover_timing_report(claim: &Claim, aet: &AlgebraicExecutionTrace) -> Report
let profile_name = format!("Prove Fibonacci {FIBONACCI_INDEX}");
let parameters = StarkParameters::default();
let mut profiler = Some(TritonProfiler::new(&profile_name));
let proof = Stark::prove(parameters, claim, aet, &mut profiler);
let proof = Stark::prove(parameters, claim, aet, &mut profiler).unwrap();
let mut profiler = profiler.unwrap();
profiler.finish();

Expand Down
2 changes: 1 addition & 1 deletion triton-vm/benches/prove_halt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn prove_halt(criterion: &mut Criterion) {
output,
};
let mut profiler = Some(TritonProfiler::new("Prove Halt"));
let proof = Stark::prove(parameters, &claim, &aet, &mut profiler);
let proof = Stark::prove(parameters, &claim, &aet, &mut profiler).unwrap();
let mut profiler = profiler.unwrap();
profiler.finish();

Expand Down
2 changes: 1 addition & 1 deletion triton-vm/benches/verify_halt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn verify_halt(criterion: &mut Criterion) {
};

let (aet, _) = program.trace_execution([].into(), [].into()).unwrap();
let proof = Stark::prove(parameters, &claim, &aet, &mut None);
let proof = Stark::prove(parameters, &claim, &aet, &mut None).unwrap();

let mut profiler = Some(TritonProfiler::new("Verify Halt"));
Stark::verify(parameters, &claim, &proof, &mut profiler).unwrap();
Expand Down
14 changes: 13 additions & 1 deletion triton-vm/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub enum FriSetupError {
}

#[non_exhaustive]
#[derive(Debug, Error)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
pub enum FriProvingError {
#[error(transparent)]
MerkleTreeError(#[from] MerkleTreeError),
Expand Down Expand Up @@ -212,6 +212,18 @@ pub enum ProvingError {
#[error("claimed public output does not match actual public output")]
PublicOutputMismatch,

#[error(transparent)]
CanonicalRepresentationError(#[from] CanonicalRepresentationError),

#[error(transparent)]
MerkleTreeError(#[from] MerkleTreeError),

#[error(transparent)]
FriSetupError(#[from] FriSetupError),

#[error(transparent)]
FriProvingError(#[from] FriProvingError),

#[error(transparent)]
VMError(#[from] VMError),
}
Expand Down
10 changes: 4 additions & 6 deletions triton-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@

#![recursion_limit = "4096"]

use std::error::Error;

pub use twenty_first;
pub use twenty_first::shared_math::b_field_element::BFieldElement;
pub use twenty_first::shared_math::tip5::Digest;
Expand Down Expand Up @@ -503,7 +501,7 @@ pub fn prove_program(
program: &Program,
public_input: &[u64],
non_determinism: &NonDeterminism<u64>,
) -> Result<(StarkParameters, Claim, Proof), Box<dyn Error>> {
) -> Result<(StarkParameters, Claim, Proof), ProvingError> {
input_elements_have_unique_representation(public_input, non_determinism)?;

// Convert public and secret inputs to BFieldElements.
Expand Down Expand Up @@ -538,7 +536,7 @@ pub fn prove_program(
};

// Generate the proof.
let proof = Stark::prove(parameters, &claim, &aet, &mut None);
let proof = Stark::prove(parameters, &claim, &aet, &mut None)?;

Ok((parameters, claim, proof))
}
Expand Down Expand Up @@ -579,8 +577,8 @@ pub fn prove(
if public_output != claim.output {
return Err(ProvingError::PublicOutputMismatch);
}
let proof = Stark::prove(parameters, claim, &aet, &mut None);
Ok(proof)

Stark::prove(parameters, claim, &aet, &mut None)
}

/// Verify a proof generated by [`prove`] or [`prove_program`].
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/shared_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub(crate) fn prove_with_low_security_level(
let claim = construct_claim(&aet, public_input.individual_tokens, public_output);

prof_start!(maybe_profiler, "prove");
let proof = Stark::prove(parameters, &claim, &aet, maybe_profiler);
let proof = Stark::prove(parameters, &claim, &aet, maybe_profiler).unwrap();
prof_stop!(maybe_profiler, "prove");

(parameters, claim, proof)
Expand Down
29 changes: 13 additions & 16 deletions triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use twenty_first::util_types::merkle_tree_maker::MerkleTreeMaker;

use crate::aet::AlgebraicExecutionTrace;
use crate::arithmetic_domain::ArithmeticDomain;
use crate::error::ProvingError;
use crate::error::VerificationError;
use crate::error::VerificationError::*;
use crate::fri;
Expand Down Expand Up @@ -143,7 +144,7 @@ impl Stark {
claim: &Claim,
aet: &AlgebraicExecutionTrace,
maybe_profiler: &mut Option<TritonProfiler>,
) -> Proof {
) -> Result<Proof, ProvingError> {
prof_start!(maybe_profiler, "Fiat-Shamir: claim", "hash");
let mut proof_stream = StarkProofStream::new();
proof_stream.alter_fiat_shamir_state_with(claim);
Expand All @@ -152,7 +153,7 @@ impl Stark {
prof_start!(maybe_profiler, "derive additional parameters");
let padded_height = aet.padded_height();
let max_degree = Self::derive_max_degree(padded_height, parameters.num_trace_randomizers);
let fri = Self::derive_fri(parameters, padded_height).unwrap();
let fri = Self::derive_fri(parameters, padded_height)?;
let quotient_domain = Self::quotient_domain(fri.domain, max_degree);
proof_stream.enqueue(ProofItem::Log2PaddedHeight(padded_height.ilog2()));
prof_stop!(maybe_profiler, "derive additional parameters");
Expand Down Expand Up @@ -266,7 +267,7 @@ impl Stark {
prof_stop!(maybe_profiler, "hash rows of quotient segments");
prof_start!(maybe_profiler, "Merkle tree", "hash");
let quot_merkle_tree: MerkleTree<StarkHasher> =
MTMaker::from_digests(&fri_domain_quotient_segment_codewords_digests).unwrap();
MTMaker::from_digests(&fri_domain_quotient_segment_codewords_digests)?;
let quot_merkle_tree_root = quot_merkle_tree.root();
proof_stream.enqueue(ProofItem::MerkleRoot(quot_merkle_tree_root));
prof_stop!(maybe_profiler, "Merkle tree");
Expand Down Expand Up @@ -438,9 +439,8 @@ impl Stark {
prof_stop!(maybe_profiler, "combined DEEP polynomial");

prof_start!(maybe_profiler, "FRI");
let revealed_current_row_indices = fri
.prove(&fri_combination_codeword, &mut proof_stream)
.unwrap();
let revealed_current_row_indices =
fri.prove(&fri_combination_codeword, &mut proof_stream)?;
assert_eq!(
parameters.num_combination_codeword_checks,
revealed_current_row_indices.len()
Expand All @@ -453,9 +453,8 @@ impl Stark {
master_base_table.fri_domain_table(),
&revealed_current_row_indices,
);
let base_authentication_structure = base_merkle_tree
.authentication_structure(&revealed_current_row_indices)
.unwrap();
let base_authentication_structure =
base_merkle_tree.authentication_structure(&revealed_current_row_indices)?;
proof_stream.enqueue(ProofItem::MasterBaseTableRows(revealed_base_elems));
proof_stream.enqueue(ProofItem::AuthenticationStructure(
base_authentication_structure,
Expand All @@ -465,9 +464,8 @@ impl Stark {
master_ext_table.fri_domain_table(),
&revealed_current_row_indices,
);
let ext_authentication_structure = ext_merkle_tree
.authentication_structure(&revealed_current_row_indices)
.unwrap();
let ext_authentication_structure =
ext_merkle_tree.authentication_structure(&revealed_current_row_indices)?;
proof_stream.enqueue(ProofItem::MasterExtTableRows(revealed_ext_elems));
proof_stream.enqueue(ProofItem::AuthenticationStructure(
ext_authentication_structure,
Expand All @@ -481,9 +479,8 @@ impl Stark {
.map(|&i| fri_domain_quotient_segment_codewords.row(i))
.map(into_fixed_width_row)
.collect_vec();
let revealed_quotient_authentication_structure = quot_merkle_tree
.authentication_structure(&revealed_current_row_indices)
.unwrap();
let revealed_quotient_authentication_structure =
quot_merkle_tree.authentication_structure(&revealed_current_row_indices)?;
proof_stream.enqueue(ProofItem::QuotientSegmentsElements(
revealed_quotient_segments_rows,
));
Expand All @@ -492,7 +489,7 @@ impl Stark {
));
prof_stop!(maybe_profiler, "open trace leafs");

proof_stream.into()
Ok(proof_stream.into())
}

fn random_linear_sum_base_field(
Expand Down

0 comments on commit 5613f19

Please sign in to comment.