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

Add first point rc constraint. #33

Closed
Closed
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
30 changes: 23 additions & 7 deletions stwo_cairo_prover/src/components/memory/component.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use itertools::{zip_eq, Itertools};
use num_traits::Zero;
use num_traits::{One, Zero};
use stwo_prover::core::air::accumulation::PointEvaluationAccumulator;
use stwo_prover::core::air::mask::fixed_mask_points;
use stwo_prover::core::air::Component;
Expand Down Expand Up @@ -208,7 +208,7 @@ impl ComponentTraceGenerator<CpuBackend> for MemoryTraceGenerator {

impl Component for MemoryComponent {
fn n_constraints(&self) -> usize {
3
4
}

fn max_constraint_log_degree_bound(&self) -> u32 {
Expand All @@ -227,12 +227,19 @@ impl Component for MemoryComponent {
point: CirclePoint<SecureField>,
) -> TreeVec<ColumnVec<Vec<CirclePoint<SecureField>>>> {
let domain = CanonicCoset::new(self.log_n_rows);
let mut interaction_points =
vec![vec![point, point - domain.step().into_ef()]; SECURE_EXTENSION_DEGREE];
interaction_points.append(&mut vec![
vec![point];
SECURE_EXTENSION_DEGREE * (N_M31_IN_FELT252 - 1)
]);
interaction_points.append(&mut vec![
vec![point, point - domain.step().into_ef()];
SECURE_EXTENSION_DEGREE
]);
TreeVec::new(vec![
fixed_mask_points(&vec![vec![0_usize]; self.n_columns()], point),
vec![
vec![point, point - domain.step().into_ef()];
SECURE_EXTENSION_DEGREE * (1 + N_M31_IN_FELT252)
],
interaction_points,
])
}

Expand All @@ -247,9 +254,10 @@ impl Component for MemoryComponent {
// TODO(AlonH): Add constraints to the range check interaction columns.
// First lookup point boundary constraint.
let constraint_zero_domain = CanonicCoset::new(self.log_n_rows).coset;
let (alpha, z) = (
let (alpha, z, rc_z) = (
interaction_elements[MEMORY_ALPHA],
interaction_elements[MEMORY_Z],
interaction_elements[RC_Z],
);
let value =
SecureField::from_partial_evals(std::array::from_fn(|i| mask[INTERACTION_TRACE][i][0]));
Expand Down Expand Up @@ -283,6 +291,14 @@ impl Component for MemoryComponent {
let denom = coset_vanishing(constraint_zero_domain, point)
/ point_excluder(constraint_zero_domain.at(0), point);
evaluation_accumulator.accumulate(numerator / denom);

// First range check lookup column constraint.
let rc_value = SecureField::from_partial_evals(std::array::from_fn(|i| {
mask[INTERACTION_TRACE][i + SECURE_EXTENSION_DEGREE][0]
}));
let numerator = rc_value * (rc_z - address_and_value[1]) - BaseField::one();
let denom = coset_vanishing(constraint_zero_domain, point);
evaluation_accumulator.accumulate(numerator / denom);
}
}

Expand Down
52 changes: 36 additions & 16 deletions stwo_cairo_prover/src/components/memory/component_prover.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::collections::BTreeMap;

use itertools::izip;
use num_traits::Zero;
use num_traits::{One, Zero};
use stwo_prover::core::air::accumulation::DomainEvaluationAccumulator;
use stwo_prover::core::air::{Component, ComponentProver, ComponentTrace};
use stwo_prover::core::backend::CpuBackend;
use stwo_prover::core::constraints::{coset_vanishing, point_excluder};
use stwo_prover::core::fields::m31::BaseField;
use stwo_prover::core::fields::qm31::SecureField;
use stwo_prover::core::fields::secure_column::SECURE_EXTENSION_DEGREE;
use stwo_prover::core::fields::FieldExpOps;
use stwo_prover::core::poly::circle::CanonicCoset;
use stwo_prover::core::utils::{
Expand All @@ -23,6 +24,7 @@ use super::component::{
MEMORY_RC_LOOKUP_VALUE_1, MEMORY_RC_LOOKUP_VALUE_2, MEMORY_RC_LOOKUP_VALUE_3, MEMORY_Z,
MULTIPLICITY_COLUMN_OFFSET, N_M31_IN_FELT252,
};
use crate::components::range_check_unit::component::RC_Z;

impl ComponentProver<CpuBackend> for MemoryComponent {
fn evaluate_constraint_quotients_on_domain(
Expand All @@ -46,29 +48,41 @@ impl ComponentProver<CpuBackend> for MemoryComponent {
trace_eval_domain,
zero_domain.at(zero_domain.size() - 1),
);
let mut step_denoms = vec![];
let mut coset_vanishing_denoms = vec![];
let mut zero_point_excluders = vec![];
for point in trace_eval_domain.iter() {
step_denoms.push(
coset_vanishing(zero_domain, point) / point_excluder(zero_domain.at(0), point),
);
coset_vanishing_denoms.push(coset_vanishing(zero_domain, point));
zero_point_excluders.push(point_excluder(zero_domain.at(0), point));
}
bit_reverse(&mut step_denoms);
let mut step_denom_inverses = vec![BaseField::zero(); 1 << (max_constraint_degree)];
BaseField::batch_inverse(&step_denoms, &mut step_denom_inverses);
let (alpha, z) = (
bit_reverse(&mut coset_vanishing_denoms);
bit_reverse(&mut zero_point_excluders);
let mut coset_vanishing_denom_inverses =
vec![BaseField::zero(); 1 << (max_constraint_degree)];
BaseField::batch_inverse(&coset_vanishing_denoms, &mut coset_vanishing_denom_inverses);
let (alpha, z, rc_z) = (
interaction_elements[MEMORY_ALPHA],
interaction_elements[MEMORY_Z],
interaction_elements[RC_Z],
);
let lookup_value = SecureField::from_m31(
lookup_values[MEMORY_LOOKUP_VALUE_0],
lookup_values[MEMORY_LOOKUP_VALUE_1],
lookup_values[MEMORY_LOOKUP_VALUE_2],
lookup_values[MEMORY_LOOKUP_VALUE_3],
);
for (i, (first_point_denom_inverse, last_point_denom_inverse, step_denom_inverse)) in izip!(
for (
i,
(
first_point_denom_inverse,
last_point_denom_inverse,
coset_vanishing_denom_inverse,
zero_point_excluder,
),
) in izip!(
first_point_denom_inverses,
last_point_denom_inverses,
step_denom_inverses,
coset_vanishing_denom_inverses,
zero_point_excluders,
)
.enumerate()
{
Expand All @@ -86,19 +100,25 @@ impl ComponentProver<CpuBackend> for MemoryComponent {
let address_and_value: [BaseField; N_M31_IN_FELT252 + 1] =
std::array::from_fn(|j| trace_evals[BASE_TRACE][j][i]);

let first_point_numerator = accum.random_coeff_powers[2]
let first_point_numerator = accum.random_coeff_powers[3]
* (value * shifted_secure_combination(&address_and_value, alpha, z)
- trace_evals[BASE_TRACE][MULTIPLICITY_COLUMN_OFFSET][i]);

let last_point_numerator = accum.random_coeff_powers[1] * (value - lookup_value);
let step_numerator = accum.random_coeff_powers[0]
let last_point_numerator = accum.random_coeff_powers[2] * (value - lookup_value);
let step_numerator = accum.random_coeff_powers[1]
* ((value - prev_value) * shifted_secure_combination(&address_and_value, alpha, z)
- trace_evals[BASE_TRACE][MULTIPLICITY_COLUMN_OFFSET][i]);

let rc_value = SecureField::from_m31_array(std::array::from_fn(|j| {
trace_evals[INTERACTION_TRACE][j + SECURE_EXTENSION_DEGREE][i]
}));
let rc_first_point_numerator = accum.random_coeff_powers[0]
* (rc_value * (rc_z - address_and_value[1]) - BaseField::one());
accum.accumulate(
i,
first_point_numerator * first_point_denom_inverse
+ last_point_numerator * last_point_denom_inverse
+ step_numerator * step_denom_inverse,
+ step_numerator * coset_vanishing_denom_inverse * zero_point_excluder
+ rc_first_point_numerator * coset_vanishing_denom_inverse,
);
}
}
Expand Down
Loading