diff --git a/stwo_cairo_prover/crates/adapted_prover/src/main.rs b/stwo_cairo_prover/crates/adapted_prover/src/main.rs index 9a5695e9..65547d2e 100644 --- a/stwo_cairo_prover/crates/adapted_prover/src/main.rs +++ b/stwo_cairo_prover/crates/adapted_prover/src/main.rs @@ -28,6 +28,8 @@ struct Args { proof_path: PathBuf, #[structopt(long = "debug_lookup")] debug_lookup: bool, + #[structopt(long = "debug_lookup")] + display_components: bool, } #[derive(Debug, Error)] @@ -69,7 +71,7 @@ fn run(args: impl Iterator) -> Result) -> std::fmt::Result { + writeln!(f, "CairoComponents")?; + writeln!(f, "Opcodes: {}", self.opcodes)?; + writeln!( + f, + "VerifyInstruction: {}", + indent_component_display(&self.verify_instruction) + )?; + writeln!( + f, + "MemoryAddressToId: {}", + indent_component_display(&self.memory_address_to_id) + )?; + writeln!( + f, + "MemoryIdToValue: {}", + indent_component_display(&self.memory_id_to_value.0) + )?; + writeln!( + f, + "SmallMemoryIdToValue: {}", + indent_component_display(&self.memory_id_to_value.1) + )?; + writeln!( + f, + "RangeCheck19: {}", + indent_component_display(&self.range_check_19) + )?; + writeln!( + f, + "RangeCheck9_9: {}", + indent_component_display(&self.range_check9_9) + )?; + writeln!( + f, + "RangeCheck7_2_5: {}", + indent_component_display(&self.range_check7_2_5) + )?; + writeln!( + f, + "RangeCheck4_3: {}", + indent_component_display(&self.range_check4_3) + )?; + Ok(()) + } +} diff --git a/stwo_cairo_prover/crates/prover/src/cairo_air/debug.rs b/stwo_cairo_prover/crates/prover/src/cairo_air/debug.rs index 33e17171..315f5471 100644 --- a/stwo_cairo_prover/crates/prover/src/cairo_air/debug.rs +++ b/stwo_cairo_prover/crates/prover/src/cairo_air/debug.rs @@ -3,7 +3,9 @@ use num_traits::One; use stwo_prover::constraint_framework::relation_tracker::{ RelationTrackerComponent, RelationTrackerEntry, }; -use stwo_prover::constraint_framework::TraceLocationAllocator; +use stwo_prover::constraint_framework::{ + FrameworkComponent, FrameworkEval, TraceLocationAllocator, +}; use stwo_prover::core::backend::simd::SimdBackend; use stwo_prover::core::fields::m31::M31; use stwo_prover::core::pcs::CommitmentSchemeProver; @@ -583,3 +585,20 @@ pub fn track_cairo_relations( entries } + +pub(super) fn indent_component_display( + component: &FrameworkComponent, +) -> String { + let component_display = &format!("\n{}", component); + component_display + .lines() + .map(|line| format!("\t{}", line)) + .join("\n") +} + +pub(super) fn display_components(components: &[FrameworkComponent]) -> String { + components + .iter() + .map(|component| indent_component_display(component)) + .join("\n") +} diff --git a/stwo_cairo_prover/crates/prover/src/cairo_air/mod.rs b/stwo_cairo_prover/crates/prover/src/cairo_air/mod.rs index 4f6f7422..f244f9b7 100644 --- a/stwo_cairo_prover/crates/prover/src/cairo_air/mod.rs +++ b/stwo_cairo_prover/crates/prover/src/cairo_air/mod.rs @@ -27,6 +27,7 @@ const IS_FIRST_LOG_SIZES: [u32; 19] = [ pub fn prove_cairo( input: CairoInput, track_relations: bool, + print_resources: bool, ) -> Result, ProvingError> { let _span = span!(Level::INFO, "prove_cairo").entered(); let config = PcsConfig::default(); @@ -89,6 +90,9 @@ pub fn prove_cairo( // Component provers. let component_builder = CairoComponents::new(&claim, &interaction_elements, &interaction_claim); + if print_resources { + println!("{}", component_builder); + } let components = component_builder.provers(); // Prove stark. @@ -183,14 +187,14 @@ mod tests { #[test] fn test_basic_cairo_air() { - let cairo_proof = prove_cairo(test_input(), true).unwrap(); + let cairo_proof = prove_cairo(test_input(), true, true).unwrap(); verify_cairo(cairo_proof).unwrap(); } #[ignore] #[test] fn test_full_cairo_air() { - let cairo_proof = prove_cairo(small_cairo_input(), true).unwrap(); + let cairo_proof = prove_cairo(small_cairo_input(), true, true).unwrap(); verify_cairo(cairo_proof).unwrap(); } } diff --git a/stwo_cairo_prover/crates/prover/src/cairo_air/opcodes_air.rs b/stwo_cairo_prover/crates/prover/src/cairo_air/opcodes_air.rs index 0118909e..8f6fa841 100644 --- a/stwo_cairo_prover/crates/prover/src/cairo_air/opcodes_air.rs +++ b/stwo_cairo_prover/crates/prover/src/cairo_air/opcodes_air.rs @@ -10,6 +10,7 @@ use stwo_prover::core::pcs::{TreeBuilder, TreeVec}; use stwo_prover::core::vcs::blake2_merkle::Blake2sMerkleChannel; use super::air::CairoInteractionElements; +use super::debug::display_components; use crate::components::{ add_ap_opcode_is_imm_f_op1_base_fp_f, add_ap_opcode_is_imm_f_op1_base_fp_t, add_ap_opcode_is_imm_t_op1_base_fp_f, add_opcode_is_small_f_is_imm_f, @@ -2260,3 +2261,59 @@ impl OpcodeComponents { vec } } + +impl std::fmt::Display for OpcodeComponents { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!(f, "add_f_f:")?; + writeln!(f, "{}", display_components(&self.add_f_f))?; + writeln!(f, "add_f_t:")?; + writeln!(f, "{}", display_components(&self.add_f_t))?; + writeln!(f, "add_t_f:")?; + writeln!(f, "{}", display_components(&self.add_t_f))?; + writeln!(f, "add_t_t:")?; + writeln!(f, "{}", display_components(&self.add_t_t))?; + writeln!(f, "add_ap_f_f:")?; + writeln!(f, "{}", display_components(&self.add_ap_f_f))?; + writeln!(f, "add_ap_f_t:")?; + writeln!(f, "{}", display_components(&self.add_ap_f_t))?; + writeln!(f, "add_ap_t_f:")?; + writeln!(f, "{}", display_components(&self.add_ap_t_f))?; + writeln!(f, "assert_eq_f_f:")?; + writeln!(f, "{}", display_components(&self.assert_eq_f_f))?; + writeln!(f, "assert_eq_f_t:")?; + writeln!(f, "{}", display_components(&self.assert_eq_f_t))?; + writeln!(f, "assert_eq_t_f:")?; + writeln!(f, "{}", display_components(&self.assert_eq_t_f))?; + writeln!(f, "call_f_f:")?; + writeln!(f, "{}", display_components(&self.call_f_f))?; + writeln!(f, "call_f_t:")?; + writeln!(f, "{}", display_components(&self.call_f_t))?; + writeln!(f, "call_t_f:")?; + writeln!(f, "{}", display_components(&self.call_t_f))?; + writeln!(f, "generic:")?; + writeln!(f, "{}", display_components(&self.generic))?; + writeln!(f, "jnz_f_f:")?; + writeln!(f, "{}", display_components(&self.jnz_f_f))?; + writeln!(f, "jnz_f_t:")?; + writeln!(f, "{}", display_components(&self.jnz_f_t))?; + writeln!(f, "jnz_t_f:")?; + writeln!(f, "{}", display_components(&self.jnz_t_f))?; + writeln!(f, "jnz_t_t:")?; + writeln!(f, "{}", display_components(&self.jnz_t_t))?; + writeln!(f, "jump_f_f_f:")?; + writeln!(f, "{}", display_components(&self.jump_f_f_f))?; + writeln!(f, "jump_f_f_t:")?; + writeln!(f, "{}", display_components(&self.jump_f_f_t))?; + writeln!(f, "jump_t_f_f:")?; + writeln!(f, "{}", display_components(&self.jump_t_f_f))?; + writeln!(f, "jump_t_t_f:")?; + writeln!(f, "{}", display_components(&self.jump_t_t_f))?; + writeln!(f, "mul_f_f:")?; + writeln!(f, "{}", display_components(&self.mul_f_f))?; + writeln!(f, "mul_f_t:")?; + writeln!(f, "{}", display_components(&self.mul_f_t))?; + writeln!(f, "ret:")?; + writeln!(f, "{}", display_components(&self.ret))?; + Ok(()) + } +}