Skip to content

Commit

Permalink
Implement from_qm31_array for QM31.
Browse files Browse the repository at this point in the history
  • Loading branch information
alonh5 committed Jul 2, 2024
1 parent ee8f106 commit 75b43ee
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 42 deletions.
15 changes: 13 additions & 2 deletions crates/prover/src/core/fields/qm31.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign,
};

use super::secure_column::SECURE_EXTENSION_DEGREE;
use super::{ComplexConjugate, FieldExpOps};
use crate::core::fields::cm31::CM31;
use crate::core::fields::m31::M31;
Expand Down Expand Up @@ -33,13 +34,23 @@ impl QM31 {
Self(CM31::from_m31(a, b), CM31::from_m31(c, d))
}

pub fn from_m31_array(array: [M31; 4]) -> Self {
pub fn from_m31_array(array: [M31; SECURE_EXTENSION_DEGREE]) -> Self {
Self::from_m31(array[0], array[1], array[2], array[3])
}

pub fn to_m31_array(self) -> [M31; 4] {
pub fn to_m31_array(self) -> [M31; SECURE_EXTENSION_DEGREE] {
[self.0 .0, self.0 .1, self.1 .0, self.1 .1]
}

/// Returns the combined value, given the values of its composing base field polynomials at that
/// point.
pub fn from_separate_evals(evals: [Self; SECURE_EXTENSION_DEGREE]) -> Self {
let mut res = evals[0];
res += evals[1] * Self::from_u32_unchecked(0, 1, 0, 0);
res += evals[2] * Self::from_u32_unchecked(0, 0, 1, 0);
res += evals[3] * Self::from_u32_unchecked(0, 0, 0, 1);
res
}
}

impl Display for QM31 {
Expand Down
13 changes: 1 addition & 12 deletions crates/prover/src/core/poly/circle/secure_poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct SecureCirclePoly<B: FieldOps<BaseField>>(pub [CirclePoly<B>; SECURE_E

impl<B: PolyOps> SecureCirclePoly<B> {
pub fn eval_at_point(&self, point: CirclePoint<SecureField>) -> SecureField {
Self::eval_from_partial_evals(self.eval_columns_at_point(point))
SecureField::from_separate_evals(self.eval_columns_at_point(point))
}

pub fn eval_columns_at_point(
Expand All @@ -32,17 +32,6 @@ impl<B: PolyOps> SecureCirclePoly<B> {
pub fn log_size(&self) -> u32 {
self[0].log_size()
}

/// Evaluates the polynomial at a point, given evaluations of its composing base field
/// polynomials at that point.
// TODO(AlonH): Move to SecureField and rename.
pub fn eval_from_partial_evals(evals: [SecureField; SECURE_EXTENSION_DEGREE]) -> SecureField {
let mut res = evals[0];
res += evals[1] * SecureField::from_u32_unchecked(0, 1, 0, 0);
res += evals[2] * SecureField::from_u32_unchecked(0, 0, 1, 0);
res += evals[3] * SecureField::from_u32_unchecked(0, 0, 0, 1);
res
}
}

impl<B: FieldOps<BaseField>> Deref for SecureCirclePoly<B> {
Expand Down
4 changes: 2 additions & 2 deletions crates/prover/src/core/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::backend::Backend;
use super::fields::secure_column::SECURE_EXTENSION_DEGREE;
use super::fri::FriVerificationError;
use super::pcs::{CommitmentSchemeProof, TreeVec};
use super::poly::circle::{CanonicCoset, SecureCirclePoly, MAX_CIRCLE_DOMAIN_LOG_SIZE};
use super::poly::circle::{CanonicCoset, MAX_CIRCLE_DOMAIN_LOG_SIZE};
use super::poly::twiddles::TwiddleTree;
use super::proof_of_work::ProofOfWorkVerificationError;
use super::{ColumnVec, InteractionElements, LookupValues};
Expand Down Expand Up @@ -315,7 +315,7 @@ fn sampled_values_to_mask(

let composition_partial_sampled_values =
sampled_values.last().ok_or(InvalidOodsSampleStructure)?;
let composition_oods_value = SecureCirclePoly::<CpuBackend>::eval_from_partial_evals(
let composition_oods_value = SecureField::from_separate_evals(
composition_partial_sampled_values
.iter()
.flatten()
Expand Down
23 changes: 10 additions & 13 deletions crates/prover/src/examples/wide_fibonacci/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::core::fields::qm31::SecureField;
use crate::core::fields::secure_column::{SecureColumn, SECURE_EXTENSION_DEGREE};
use crate::core::fields::FieldExpOps;
use crate::core::pcs::TreeVec;
use crate::core::poly::circle::{CanonicCoset, CircleEvaluation, SecureCirclePoly};
use crate::core::poly::circle::{CanonicCoset, CircleEvaluation};
use crate::core::poly::BitReversedOrder;
use crate::core::utils::shifted_secure_combination;
use crate::core::{ColumnVec, InteractionElements, LookupValues};
Expand Down Expand Up @@ -99,10 +99,9 @@ impl WideFibComponent {
lookup_values: &LookupValues,
) {
let (alpha, z) = (interaction_elements[ALPHA_ID], interaction_elements[Z_ID]);
let value =
SecureCirclePoly::<CpuBackend>::eval_from_partial_evals(std::array::from_fn(|i| {
mask[self.n_columns() + i][0]
}));
let value = SecureField::from_separate_evals(std::array::from_fn(|i| {
mask[self.n_columns() + i][0]
}));
let numerator = (value
* shifted_secure_combination(
&[mask[self.n_columns() - 2][0], mask[self.n_columns() - 1][0]],
Expand Down Expand Up @@ -146,14 +145,12 @@ impl WideFibComponent {
interaction_elements: &InteractionElements,
) {
let (alpha, z) = (interaction_elements[ALPHA_ID], interaction_elements[Z_ID]);
let value =
SecureCirclePoly::<CpuBackend>::eval_from_partial_evals(std::array::from_fn(|i| {
mask[self.n_columns() + i][0]
}));
let prev_value =
SecureCirclePoly::<CpuBackend>::eval_from_partial_evals(std::array::from_fn(|i| {
mask[self.n_columns() + i][1]
}));
let value = SecureField::from_separate_evals(std::array::from_fn(|i| {
mask[self.n_columns() + i][0]
}));
let prev_value = SecureField::from_separate_evals(std::array::from_fn(|i| {
mask[self.n_columns() + i][1]
}));
let numerator = (value
* shifted_secure_combination(
&[mask[self.n_columns() - 2][0], mask[self.n_columns() - 1][0]],
Expand Down
23 changes: 10 additions & 13 deletions crates/prover/src/examples/wide_fibonacci/constraint_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::core::fields::m31::BaseField;
use crate::core::fields::qm31::SecureField;
use crate::core::fields::FieldExpOps;
use crate::core::pcs::TreeVec;
use crate::core::poly::circle::{CanonicCoset, CircleDomain, CircleEvaluation, SecureCirclePoly};
use crate::core::poly::circle::{CanonicCoset, CircleDomain, CircleEvaluation};
use crate::core::poly::BitReversedOrder;
use crate::core::prover::{BASE_TRACE, INTERACTION_TRACE};
use crate::core::utils::{
Expand Down Expand Up @@ -185,10 +185,9 @@ impl WideFibComponent {

#[allow(clippy::needless_range_loop)]
for i in 0..trace_eval_domain.size() {
let value =
SecureCirclePoly::<CpuBackend>::eval_from_partial_evals(std::array::from_fn(|j| {
trace_evals[INTERACTION_TRACE][j][i].into()
}));
let value = SecureField::from_m31_array(std::array::from_fn(|j| {
trace_evals[INTERACTION_TRACE][j][i]
}));
first_point_numerators[i] = accum.random_coeff_powers[self.n_columns() - 1]
* ((value
* shifted_secure_combination(
Expand Down Expand Up @@ -253,19 +252,17 @@ impl WideFibComponent {

#[allow(clippy::needless_range_loop)]
for i in 0..trace_eval_domain.size() {
let value =
SecureCirclePoly::<CpuBackend>::eval_from_partial_evals(std::array::from_fn(|j| {
trace_evals[INTERACTION_TRACE][j][i].into()
}));
let value = SecureField::from_m31_array(std::array::from_fn(|j| {
trace_evals[INTERACTION_TRACE][j][i]
}));
let prev_index = previous_bit_reversed_circle_domain_index(
i,
zero_domain.log_size,
trace_eval_domain.log_size(),
);
let prev_value =
SecureCirclePoly::<CpuBackend>::eval_from_partial_evals(std::array::from_fn(|j| {
trace_evals[INTERACTION_TRACE][j][prev_index].into()
}));
let prev_value = SecureField::from_m31_array(std::array::from_fn(|j| {
trace_evals[INTERACTION_TRACE][j][prev_index]
}));
numerators[i] = accum.random_coeff_powers[self.n_columns()]
* ((value
* shifted_secure_combination(
Expand Down

0 comments on commit 75b43ee

Please sign in to comment.