From 75b43ee37337640f109c851c9f3e982d23dc3ff0 Mon Sep 17 00:00:00 2001 From: Alon Haramati Date: Wed, 26 Jun 2024 13:22:52 +0300 Subject: [PATCH] Implement from_qm31_array for QM31. --- crates/prover/src/core/fields/qm31.rs | 15 ++++++++++-- .../src/core/poly/circle/secure_poly.rs | 13 +---------- crates/prover/src/core/prover/mod.rs | 4 ++-- .../src/examples/wide_fibonacci/component.rs | 23 ++++++++----------- .../wide_fibonacci/constraint_eval.rs | 23 ++++++++----------- 5 files changed, 36 insertions(+), 42 deletions(-) diff --git a/crates/prover/src/core/fields/qm31.rs b/crates/prover/src/core/fields/qm31.rs index 54cee0d4f..26d28b2bb 100644 --- a/crates/prover/src/core/fields/qm31.rs +++ b/crates/prover/src/core/fields/qm31.rs @@ -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; @@ -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 { diff --git a/crates/prover/src/core/poly/circle/secure_poly.rs b/crates/prover/src/core/poly/circle/secure_poly.rs index 931e283e8..10fc400c9 100644 --- a/crates/prover/src/core/poly/circle/secure_poly.rs +++ b/crates/prover/src/core/poly/circle/secure_poly.rs @@ -14,7 +14,7 @@ pub struct SecureCirclePoly>(pub [CirclePoly; SECURE_E impl SecureCirclePoly { pub fn eval_at_point(&self, point: CirclePoint) -> 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( @@ -32,17 +32,6 @@ impl SecureCirclePoly { 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> Deref for SecureCirclePoly { diff --git a/crates/prover/src/core/prover/mod.rs b/crates/prover/src/core/prover/mod.rs index e28cb5f69..8d908c617 100644 --- a/crates/prover/src/core/prover/mod.rs +++ b/crates/prover/src/core/prover/mod.rs @@ -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}; @@ -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::::eval_from_partial_evals( + let composition_oods_value = SecureField::from_separate_evals( composition_partial_sampled_values .iter() .flatten() diff --git a/crates/prover/src/examples/wide_fibonacci/component.rs b/crates/prover/src/examples/wide_fibonacci/component.rs index a965418a2..3463ea55b 100644 --- a/crates/prover/src/examples/wide_fibonacci/component.rs +++ b/crates/prover/src/examples/wide_fibonacci/component.rs @@ -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}; @@ -99,10 +99,9 @@ impl WideFibComponent { lookup_values: &LookupValues, ) { let (alpha, z) = (interaction_elements[ALPHA_ID], interaction_elements[Z_ID]); - let value = - SecureCirclePoly::::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]], @@ -146,14 +145,12 @@ impl WideFibComponent { interaction_elements: &InteractionElements, ) { let (alpha, z) = (interaction_elements[ALPHA_ID], interaction_elements[Z_ID]); - let value = - SecureCirclePoly::::eval_from_partial_evals(std::array::from_fn(|i| { - mask[self.n_columns() + i][0] - })); - let prev_value = - SecureCirclePoly::::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]], diff --git a/crates/prover/src/examples/wide_fibonacci/constraint_eval.rs b/crates/prover/src/examples/wide_fibonacci/constraint_eval.rs index fc00383fc..91a5cbdd4 100644 --- a/crates/prover/src/examples/wide_fibonacci/constraint_eval.rs +++ b/crates/prover/src/examples/wide_fibonacci/constraint_eval.rs @@ -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::{ @@ -185,10 +185,9 @@ impl WideFibComponent { #[allow(clippy::needless_range_loop)] for i in 0..trace_eval_domain.size() { - let value = - SecureCirclePoly::::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( @@ -253,19 +252,17 @@ impl WideFibComponent { #[allow(clippy::needless_range_loop)] for i in 0..trace_eval_domain.size() { - let value = - SecureCirclePoly::::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::::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(