Skip to content
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

Renamed instructions to components in Adapter #111

Open
wants to merge 1 commit into
base: yuval/adapted_stwo_log_cairo_input
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
4 changes: 2 additions & 2 deletions stwo_cairo_prover/crates/adapted_prover/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ fn run(args: impl Iterator<Item = String>) -> Result<CairoProof<Blake2sMerkleHas
let vm_output: CairoInput =
import_from_vm_output(args.pub_json.as_path(), args.priv_json.as_path())?;

let instruction_counts = vm_output.instructions.counts();
log::info!("Instruction counts: {instruction_counts:?}");
let component_counts = vm_output.components_usage.counts();
log::info!("Component counts: {component_counts:?}");

let proof = prove_cairo(vm_output)?;

Expand Down
8 changes: 4 additions & 4 deletions stwo_cairo_prover/crates/prover/src/cairo_air/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::components::ret_opcode::component::{
};
use crate::components::ret_opcode::prover::RetOpcodeClaimProver;
use crate::felt::split_f252;
use crate::input::instructions::VmState;
use crate::input::components_usage::VmState;
use crate::input::CairoInput;

const RC9_LOG_MAX: u32 = 9;
Expand Down Expand Up @@ -261,7 +261,7 @@ pub fn prove_cairo(input: CairoInput) -> Result<CairoProof<Blake2sMerkleHasher>,

// Base trace.
// TODO(Ohad): change to OpcodeClaimProvers, and integrate padding.
let ret_trace_generator = RetOpcodeClaimProver::new(input.instructions.ret);
let ret_trace_generator = RetOpcodeClaimProver::new(input.components_usage.ret);
let range_check_builtin_trace_generator =
RangeCheckBuiltinClaimProver::new(input.range_check_builtin);
let mut memory_trace_generator = MemoryClaimProver::new(input.mem);
Expand Down Expand Up @@ -291,8 +291,8 @@ pub fn prove_cairo(input: CairoInput) -> Result<CairoProof<Blake2sMerkleHasher>,
// Commit to the claim and the trace.
let claim = CairoClaim {
public_memory,
initial_state: input.instructions.initial_state,
final_state: input.instructions.final_state,
initial_state: input.components_usage.initial_state,
final_state: input.components_usage.final_state,
ret: vec![ret_claim],
range_check_builtin: range_check_builtin_claim.clone(),
memory: memory_claim.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::component::{RetOpcodeClaim, RetOpcodeInteractionClaim, RET_INSTRUCTIO
use crate::components::memory::component::N_M31_IN_FELT252;
use crate::components::memory::prover::MemoryClaimProver;
use crate::components::memory::MemoryLookupElements;
use crate::input::instructions::VmState;
use crate::input::components_usage::VmState;

const N_MEMORY_CALLS: usize = 3;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ impl From<TraceEntry> for VmState {
}
}

/// The instructions usage in the input, split to Stwo opcodes.
/// The Stwo components usage in the input.
///
/// For each opcode with flags, the array describes the different flag combinations. The index
/// refers to the flag combination in little endian. For example, jnz_imm at index 1 (100 in little
/// endian) is for: fp (1=true), not taken (0=false), no ap++ (0=false).
/// Note: for the flag "fp/ap", true means fp-based and false means ap-based.
/// refers to the flag combination in little endian, and each one matches a specific Stwo component.
/// For example, jnz_imm at index 1 (100 in little endian) is for: fp (1=true), not taken (0=false),
/// no ap++ (0=false). Note: for the flag "fp/ap", true means fp-based and false means ap-based.
#[derive(Debug, Default)]
pub struct Instructions {
pub struct ComponentsUsage {
pub initial_state: VmState,
pub final_state: VmState,

Expand Down Expand Up @@ -68,7 +68,7 @@ pub struct Instructions {

pub generic: Vec<VmState>,
}
impl Instructions {
impl ComponentsUsage {
pub fn from_iter(mut iter: impl Iterator<Item = TraceEntry>, mem: &Memory) -> Self {
let mut res = Self::default();

Expand Down Expand Up @@ -330,8 +330,8 @@ impl Instructions {
}
}

pub fn counts(&self) -> InstructionCounts {
InstructionCounts {
pub fn counts(&self) -> ComponentCounts {
ComponentCounts {
ret: self.ret.len(),
add_ap: self.add_ap.len(),
jmp_rel_imm: self.jmp_rel_imm.each_ref().map(Vec::len),
Expand All @@ -347,30 +347,30 @@ impl Instructions {
}
}

/// The counts of the instructions usage in the input, split to Stwo opcodes.
/// The counts of the Stwo component usage in the input.
///
/// For each opcode with flags, the array describes the different flag combinations. The index
/// refers to the flag combination in little endian. For example, jnz_imm at index 1 (100 in little
/// endian) is for: fp (1=true), not taken (0=false), no ap++ (0=false).
/// Note: for the flag "fp/ap", true means fp-based and false means ap-based.
/// refers to the flag combination in little endian, and each one matches a specific Stwo component.
/// For example, jnz_imm at index 1 (100 in little endian) is for: fp (1=true), not taken (0=false),
/// no ap++ (0=false). Note: for the flag "fp/ap", true means fp-based and false means ap-based.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct InstructionCounts {
pub struct ComponentCounts {
pub ret: usize,
pub add_ap: usize,

/// jump rel imm.
/// Flags: ap++?.
pub jmp_rel_imm: [usize; 2],

// jump abs [fp/ap + offset].
// Flags: fp/ap, ap++?.
/// jump abs [fp/ap + offset].
/// Flags: fp/ap, ap++?.
pub jmp_abs: [usize; 4],

/// call rel imm.
pub call_rel_imm: usize,

// call abs [fp/ap + offset].
// Flags: fp/ap.
/// call abs [fp/ap + offset].
/// Flags: fp/ap.
pub call_abs: [usize; 2],

/// jump rel imm if [fp/ap + offset] != 0.
Expand Down
6 changes: 3 additions & 3 deletions stwo_cairo_prover/crates/prover/src/input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use instructions::Instructions;
use components_usage::ComponentsUsage;
use mem::Memory;
use serde::{Deserialize, Serialize};

use self::range_check_unit::RangeCheckUnitInput;

pub mod components_usage;
mod decode;
pub mod instructions;
pub mod mem;
pub mod plain;
pub mod range_check_unit;
Expand All @@ -16,7 +16,7 @@ pub const N_REGISTERS: usize = 3;
// Externally provided inputs.
#[derive(Debug)]
pub struct CairoInput {
pub instructions: Instructions,
pub components_usage: ComponentsUsage,
pub mem: Memory,
pub public_mem_addresses: Vec<u32>,
pub range_check9: RangeCheckUnitInput,
Expand Down
6 changes: 3 additions & 3 deletions stwo_cairo_prover/crates/prover/src/input/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::runners::cairo_runner::CairoRunner;
use itertools::Itertools;

use super::instructions::Instructions;
use super::components_usage::ComponentsUsage;
use super::mem::{MemConfig, MemoryBuilder};
use super::range_check_unit::RangeCheckUnitInput;
use super::vm_import::MemEntry;
Expand Down Expand Up @@ -66,12 +66,12 @@ pub fn input_from_finished_runner(runner: CairoRunner) -> CairoInput {
let mut range_check9 = RangeCheckUnitInput::new();
let mem_config = MemConfig::default();
let mem = MemoryBuilder::from_iter(mem_config, &mut range_check9, mem);
let instructions = Instructions::from_iter(trace, &mem);
let components_usage = ComponentsUsage::from_iter(trace, &mem);

// TODO(spapini): Add output builtin to public memory.
let public_mem_addresses = (0..(program_len as u32)).collect_vec();
CairoInput {
instructions,
components_usage,
mem,
public_mem_addresses,
range_check9,
Expand Down
20 changes: 10 additions & 10 deletions stwo_cairo_prover/crates/prover/src/input/vm_import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use json::{PrivateInput, PublicInput};
use thiserror::Error;
use tracing::{span, Level};

use super::instructions::Instructions;
use super::components_usage::ComponentsUsage;
use super::mem::MemConfig;
use super::CairoInput;
use crate::input::mem::MemoryBuilder;
Expand Down Expand Up @@ -50,7 +50,7 @@ pub fn import_from_vm_output(
let mut trace_file = std::io::BufReader::new(std::fs::File::open(trace_path)?);
let mut mem_file = std::io::BufReader::new(std::fs::File::open(mem_path)?);
let mem = MemoryBuilder::from_iter(mem_config, &mut range_check9, MemEntryIter(&mut mem_file));
let instructions = Instructions::from_iter(TraceIter(&mut trace_file), &mem);
let components_usage = ComponentsUsage::from_iter(TraceIter(&mut trace_file), &mem);

let public_mem_addresses = pub_data
.public_memory
Expand All @@ -59,7 +59,7 @@ pub fn import_from_vm_output(
.collect();

Ok(CairoInput {
instructions,
components_usage,
mem,
public_mem_addresses,
range_check9,
Expand Down Expand Up @@ -131,7 +131,7 @@ pub mod tests {
use std::path::PathBuf;

use super::*;
use crate::input::instructions::InstructionCounts;
use crate::input::components_usage::ComponentCounts;

pub fn large_cairo_input() -> CairoInput {
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand All @@ -157,8 +157,8 @@ pub mod tests {
fn test_read_from_large_files() {
let input = large_cairo_input();
assert_eq!(
input.instructions.counts(),
InstructionCounts {
input.components_usage.counts(),
ComponentCounts {
ret: 49472,
add_ap: 36895,
jmp_rel_imm: [31873866, 0],
Expand All @@ -172,15 +172,15 @@ pub mod tests {
generic: 362623
}
);
println!("Instruction counts: {:#?}", input.instructions.counts());
println!("Component counts: {:#?}", input.components_usage.counts());
}

#[test]
fn test_read_from_small_files() {
let input = small_cairo_input();
assert_eq!(
input.instructions.counts(),
InstructionCounts {
input.components_usage.counts(),
ComponentCounts {
ret: 462,
add_ap: 2,
jmp_rel_imm: [124627, 0],
Expand All @@ -194,6 +194,6 @@ pub mod tests {
generic: 951
}
);
println!("Instruction counts: {:#?}", input.instructions.counts());
println!("Component counts: {:#?}", input.components_usage.counts());
}
}
6 changes: 3 additions & 3 deletions stwo_cairo_prover/crates/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ mod tests {
.instructions;

let inp = input_from_plain_casm(instructions);
let instruction_counts = inp.instructions.counts();
assert_eq!(instruction_counts.jmp_abs[0], 1);
println!("Instruction counts: {instruction_counts:#?}");
let component_counts = inp.components_usage.counts();
assert_eq!(component_counts.jmp_abs[0], 1);
println!("Component counts: {component_counts:#?}");
}
}
Loading