Skip to content

Commit

Permalink
assert eq gen
Browse files Browse the repository at this point in the history
  • Loading branch information
ohad-starkware committed Dec 1, 2024
1 parent 98f9252 commit 05aef95
Show file tree
Hide file tree
Showing 12 changed files with 2,139 additions and 5 deletions.
265 changes: 261 additions & 4 deletions stwo_cairo_prover/crates/prover/src/cairo_air/opcodes_air.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#![allow(non_camel_case_types)]
#![allow(unused_imports)]
use num_traits::{One, Zero};
use serde::{Deserialize, Serialize};
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::relations;

pub struct Eval {
pub claim: Claim,
pub memoryaddresstoid_lookup_elements: relations::MemoryAddressToId,
pub opcodes_lookup_elements: relations::Opcodes,
pub verifyinstruction_lookup_elements: relations::VerifyInstruction,
}

#[derive(Copy, Clone, Serialize, Deserialize)]
pub struct Claim {
pub n_calls: usize,
}
impl Claim {
pub fn log_sizes(&self) -> TreeVec<Vec<u32>> {
let log_size = std::cmp::max(self.n_calls.next_power_of_two().ilog2(), LOG_N_LANES);
let trace_log_sizes = vec![log_size; 10];
let interaction_log_sizes = vec![log_size; SECURE_EXTENSION_DEGREE * 5];
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_calls as u64);
}
}

#[derive(Copy, Clone, Serialize, Deserialize)]
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_calls.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 M31_0 = E::F::from(M31::from(0));
let M31_1 = E::F::from(M31::from(1));
let M31_32767 = E::F::from(M31::from(32767));
let M31_32768 = E::F::from(M31::from(32768));
let input_pc_col0 = eval.next_trace_mask();
let input_ap_col1 = eval.next_trace_mask();
let input_fp_col2 = eval.next_trace_mask();
let offset0_col3 = eval.next_trace_mask();
let offset2_col4 = eval.next_trace_mask();
let dst_base_fp_col5 = eval.next_trace_mask();
let op1_base_fp_col6 = eval.next_trace_mask();
let op1_base_ap_col7 = eval.next_trace_mask();
let ap_update_add_1_col8 = eval.next_trace_mask();
let dst_id_col9 = eval.next_trace_mask();

// decode_instruction_dc55adb272664963.

eval.add_to_relation(&[RelationEntry::new(
&self.verifyinstruction_lookup_elements,
E::EF::one(),
&[
input_pc_col0.clone(),
offset0_col3.clone(),
M31_32767.clone(),
offset2_col4.clone(),
dst_base_fp_col5.clone(),
M31_1.clone(),
M31_0.clone(),
op1_base_fp_col6.clone(),
op1_base_ap_col7.clone(),
M31_0.clone(),
M31_0.clone(),
M31_0.clone(),
M31_0.clone(),
M31_0.clone(),
M31_0.clone(),
ap_update_add_1_col8.clone(),
M31_0.clone(),
M31_0.clone(),
M31_1.clone(),
],
)]);

// Either flag op1_base_fp is on or flag op1_base_ap is on.
eval.add_constraint(
((op1_base_fp_col6.clone() + op1_base_ap_col7.clone()) - M31_1.clone()),
);

// mem_verify_equal.

eval.add_to_relation(&[RelationEntry::new(
&self.memoryaddresstoid_lookup_elements,
E::EF::one(),
&[
(((dst_base_fp_col5.clone() * input_fp_col2.clone())
+ ((M31_1.clone() - dst_base_fp_col5.clone()) * input_ap_col1.clone()))
+ (offset0_col3.clone() - M31_32768.clone())),
dst_id_col9.clone(),
],
)]);

eval.add_to_relation(&[RelationEntry::new(
&self.memoryaddresstoid_lookup_elements,
E::EF::one(),
&[
(((op1_base_fp_col6.clone() * input_fp_col2.clone())
+ (op1_base_ap_col7.clone() * input_ap_col1.clone()))
+ (offset2_col4.clone() - M31_32768.clone())),
dst_id_col9.clone(),
],
)]);

eval.add_to_relation(&[RelationEntry::new(
&self.opcodes_lookup_elements,
E::EF::one(),
&[
input_pc_col0.clone(),
input_ap_col1.clone(),
input_fp_col2.clone(),
],
)]);

eval.add_to_relation(&[RelationEntry::new(
&self.opcodes_lookup_elements,
-E::EF::one(),
&[
(input_pc_col0.clone() + M31_1.clone()),
(input_ap_col1.clone() + ap_update_add_1_col8.clone()),
input_fp_col2.clone(),
],
)]);

eval.finalize_logup();
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, InputType, InteractionClaimGenerator};
Loading

0 comments on commit 05aef95

Please sign in to comment.