Skip to content

Commit

Permalink
fmt for cairo
Browse files Browse the repository at this point in the history
  • Loading branch information
ohad-starkware committed Dec 9, 2024
1 parent 39fff93 commit 6e1f256
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 4 deletions.
4 changes: 3 additions & 1 deletion stwo_cairo_prover/crates/adapted_prover/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -69,7 +71,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<CairoProof<Blake2sMerkleHas
let casm_states_by_opcode_count = &vm_output.state_transitions.casm_states_by_opcode.counts();
log::info!("Casm states by opcode count: {casm_states_by_opcode_count:?}");

let proof = prove_cairo(vm_output, args.debug_lookup)?;
let proof = prove_cairo(vm_output, args.debug_lookup, args.display_components)?;

// TODO(yuval): This is just some serialization for the sake of serialization. Find the right
// way to serialize the proof.
Expand Down
49 changes: 49 additions & 0 deletions stwo_cairo_prover/crates/prover/src/cairo_air/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use stwo_prover::core::vcs::blake2_merkle::Blake2sMerkleChannel;
use stwo_prover::core::vcs::ops::MerkleHasher;
use tracing::{span, Level};

use super::debug::indent_component_display;
use super::opcodes_air::{
OpcodeClaim, OpcodeComponents, OpcodeInteractionClaim, OpcodesClaimGenerator,
OpcodesInteractionClaimGenerator,
Expand Down Expand Up @@ -547,3 +548,51 @@ impl CairoComponents {
.collect()
}
}

impl std::fmt::Display for CairoComponents {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> 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(())
}
}
21 changes: 20 additions & 1 deletion stwo_cairo_prover/crates/prover/src/cairo_air/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -583,3 +585,20 @@ pub fn track_cairo_relations(

entries
}

pub(super) fn indent_component_display<E: FrameworkEval>(
component: &FrameworkComponent<E>,
) -> String {
let component_display = &format!("\n{}", component);
component_display
.lines()
.map(|line| format!("\t{}", line))
.join("\n")
}

pub(super) fn display_components<E: FrameworkEval>(components: &[FrameworkComponent<E>]) -> String {
components
.iter()
.map(|component| indent_component_display(component))
.join("\n")
}
8 changes: 6 additions & 2 deletions stwo_cairo_prover/crates/prover/src/cairo_air/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const IS_FIRST_LOG_SIZES: [u32; 19] = [
pub fn prove_cairo(
input: CairoInput,
track_relations: bool,
print_resources: bool,
) -> Result<CairoProof<Blake2sMerkleHasher>, ProvingError> {
let _span = span!(Level::INFO, "prove_cairo").entered();
let config = PcsConfig::default();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
}
57 changes: 57 additions & 0 deletions stwo_cairo_prover/crates/prover/src/cairo_air/opcodes_air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit 6e1f256

Please sign in to comment.