Skip to content

Commit

Permalink
Add RC6 and RC96 Builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
Gali-StarkWare committed Jan 21, 2025
1 parent c1d308c commit 20afccf
Show file tree
Hide file tree
Showing 7 changed files with 575 additions and 5 deletions.
127 changes: 125 additions & 2 deletions stwo_cairo_prover/crates/prover/src/cairo_air/air.rs

Large diffs are not rendered by default.

30 changes: 28 additions & 2 deletions stwo_cairo_prover/crates/prover/src/cairo_air/debug_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::components::{
jnz_opcode, jnz_opcode_dst_base_fp, jnz_opcode_taken, jnz_opcode_taken_dst_base_fp,
jump_opcode, jump_opcode_double_deref, jump_opcode_rel, jump_opcode_rel_imm,
memory_address_to_id, memory_id_to_big, mul_opcode, mul_opcode_imm, range_check_19,
range_check_4_3, range_check_7_2_5, range_check_9_9, range_check_builtin_bits_128, ret_opcode,
verify_instruction,
range_check_4_3, range_check_6, range_check_7_2_5, range_check_9_9,
range_check_builtin_bits_128, range_check_builtin_bits_96, ret_opcode, verify_instruction,
};
use crate::felt::split_f252;
use crate::relations;
Expand Down Expand Up @@ -485,6 +485,22 @@ where
);
}

if let Some(range_check_96_builtin) = claim.range_check_96_builtin {
entries.extend(
RelationTrackerComponent::new(
tree_span_provider,
range_check_builtin_bits_96::Eval {
claim: range_check_96_builtin,
memory_address_to_id_lookup_elements: relations::MemoryAddressToId::dummy(),
memory_id_to_big_lookup_elements: relations::MemoryIdToBig::dummy(),
range_check_6_lookup_elements: relations::RangeCheck_6::dummy(),
},
range_check_96_builtin.n_rows,
)
.entries(trace),
);
}

// Memory.
entries.extend(
RelationTrackerComponent::new(
Expand Down Expand Up @@ -563,6 +579,16 @@ where
)
.entries(trace),
);
entries.extend(
RelationTrackerComponent::new(
tree_span_provider,
range_check_6::Eval {
lookup_elements: relations::RangeCheck_6::dummy(),
},
1 << 6,
)
.entries(trace),
);

// Public data.
claim
Expand Down
5 changes: 4 additions & 1 deletion stwo_cairo_prover/crates/prover/src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod jump_opcode_rel;
pub mod jump_opcode_rel_imm;
pub mod memory;
pub mod range_check_builtin_bits_128;
pub mod range_check_builtin_bits_96;
pub mod range_check_vector;
pub mod ret_opcode;
pub mod utils;
Expand All @@ -32,4 +33,6 @@ pub mod mul_opcode;
pub mod mul_opcode_imm;

pub use memory::{memory_address_to_id, memory_id_to_big};
pub use range_check_vector::{range_check_19, range_check_4_3, range_check_7_2_5, range_check_9_9};
pub use range_check_vector::{
range_check_19, range_check_4_3, range_check_6, range_check_7_2_5, range_check_9_9,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#![allow(non_camel_case_types)]
#![allow(unused_imports)]
use num_traits::{One, Zero};
use serde::{Deserialize, Serialize};
use stwo_cairo_serialize::CairoSerialize;
use stwo_prover::constraint_framework::logup::{LogupAtRow, LogupSums, LookupElements};
use stwo_prover::constraint_framework::{
EvalAtRow, FrameworkComponent, FrameworkEval, RelationEntry,
};
use stwo_prover::core::backend::simd::m31::LOG_N_LANES;
use stwo_prover::core::channel::Channel;
use stwo_prover::core::fields::m31::M31;
use stwo_prover::core::fields::qm31::SecureField;
use stwo_prover::core::fields::secure_column::SECURE_EXTENSION_DEGREE;
use stwo_prover::core::pcs::TreeVec;

use crate::cairo_air::preprocessed::{PreProcessedColumn, Seq};
use crate::relations;

pub struct Eval {
pub claim: Claim,
pub memory_address_to_id_lookup_elements: relations::MemoryAddressToId,
pub memory_id_to_big_lookup_elements: relations::MemoryIdToBig,
pub range_check_6_lookup_elements: relations::RangeCheck_6,
}

#[derive(Copy, Clone, Serialize, Deserialize, CairoSerialize)]
pub struct Claim {
pub n_rows: usize,
pub range_check96_builtin_segment_start: u32,
}
impl Claim {
pub fn log_sizes(&self) -> TreeVec<Vec<u32>> {
let log_size = std::cmp::max(self.n_rows.next_power_of_two().ilog2(), LOG_N_LANES);
let trace_log_sizes = vec![log_size; 12];
let interaction_log_sizes = vec![log_size; SECURE_EXTENSION_DEGREE * 2];
let preprocessed_log_sizes = vec![log_size];
TreeVec::new(vec![
preprocessed_log_sizes,
trace_log_sizes,
interaction_log_sizes,
])
}

pub fn mix_into(&self, channel: &mut impl Channel) {
channel.mix_u64(self.n_rows as u64);
channel.mix_u64(self.range_check96_builtin_segment_start as u64);
}
}

#[derive(Copy, Clone, Serialize, Deserialize, CairoSerialize)]
pub struct InteractionClaim {
pub logup_sums: LogupSums,
}
impl InteractionClaim {
pub fn mix_into(&self, channel: &mut impl Channel) {
let (total_sum, claimed_sum) = self.logup_sums;
channel.mix_felts(&[total_sum]);
if let Some(claimed_sum) = claimed_sum {
channel.mix_felts(&[claimed_sum.0]);
channel.mix_u64(claimed_sum.1 as u64);
}
}
}

pub type Component = FrameworkComponent<Eval>;

impl FrameworkEval for Eval {
fn log_size(&self) -> u32 {
std::cmp::max(self.claim.n_rows.next_power_of_two().ilog2(), LOG_N_LANES)
}

fn max_constraint_log_degree_bound(&self) -> u32 {
self.log_size() + 1
}

#[allow(unused_parens)]
#[allow(clippy::double_parens)]
#[allow(non_snake_case)]
fn evaluate<E: EvalAtRow>(&self, mut eval: E) -> E {
let seq =
eval.get_preprocessed_column(PreProcessedColumn::Seq(Seq::new(self.log_size())).id());
let value_id_col0 = eval.next_trace_mask();
let value_limb_0_col1 = eval.next_trace_mask();
let value_limb_1_col2 = eval.next_trace_mask();
let value_limb_2_col3 = eval.next_trace_mask();
let value_limb_3_col4 = eval.next_trace_mask();
let value_limb_4_col5 = eval.next_trace_mask();
let value_limb_5_col6 = eval.next_trace_mask();
let value_limb_6_col7 = eval.next_trace_mask();
let value_limb_7_col8 = eval.next_trace_mask();
let value_limb_8_col9 = eval.next_trace_mask();
let value_limb_9_col10 = eval.next_trace_mask();
let value_limb_10_col11 = eval.next_trace_mask();

// Read Positive Num Bits 96.

eval.add_to_relation(RelationEntry::new(
&self.memory_address_to_id_lookup_elements,
E::EF::one(),
&[
(E::F::from(M31::from(self.claim.range_check96_builtin_segment_start)) + seq),
value_id_col0.clone(),
],
));

// Range Check Last Limb Bits In Ms Limb 6.

eval.add_to_relation(RelationEntry::new(
&self.range_check_6_lookup_elements,
E::EF::one(),
&[value_limb_10_col11.clone()],
));

eval.add_to_relation(RelationEntry::new(
&self.memory_id_to_big_lookup_elements,
E::EF::one(),
&[
value_id_col0.clone(),
value_limb_0_col1.clone(),
value_limb_1_col2.clone(),
value_limb_2_col3.clone(),
value_limb_3_col4.clone(),
value_limb_4_col5.clone(),
value_limb_5_col6.clone(),
value_limb_6_col7.clone(),
value_limb_7_col8.clone(),
value_limb_8_col9.clone(),
value_limb_9_col10.clone(),
value_limb_10_col11.clone(),
],
));

eval.finalize_logup_in_pairs();
eval
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod component;
pub mod prover;

pub use component::{Claim, Component, Eval, InteractionClaim};
pub use prover::{ClaimGenerator, InteractionClaimGenerator};
Loading

0 comments on commit 20afccf

Please sign in to comment.