From 1f8868b1dd677ae24c5240fde99d6e19b12c78f3 Mon Sep 17 00:00:00 2001 From: Shahar Papini Date: Fri, 15 Mar 2024 06:55:26 +0200 Subject: [PATCH] Move SecureColumn --- src/core/air/air_ext.rs | 3 +- src/core/air/evaluation.rs | 45 ++----------------- src/core/fields/mod.rs | 1 + .../secure_poly.rs => fields/secure.rs} | 43 ++++++++++++++++-- src/core/poly/circle/mod.rs | 2 - src/core/prover/mod.rs | 3 +- 6 files changed, 48 insertions(+), 49 deletions(-) rename src/core/{poly/circle/secure_poly.rs => fields/secure.rs} (52%) diff --git a/src/core/air/air_ext.rs b/src/core/air/air_ext.rs index 655075a90..a74c2118e 100644 --- a/src/core/air/air_ext.rs +++ b/src/core/air/air_ext.rs @@ -7,7 +7,8 @@ use super::{Air, ComponentTrace}; use crate::core::backend::CPUBackend; use crate::core::circle::CirclePoint; use crate::core::fields::qm31::SecureField; -use crate::core::poly::circle::{CanonicCoset, CirclePoly, SecureCirclePoly}; +use crate::core::fields::secure::SecureCirclePoly; +use crate::core::poly::circle::{CanonicCoset, CirclePoly}; use crate::core::prover::LOG_BLOWUP_FACTOR; use crate::core::ComponentVec; diff --git a/src/core/air/evaluation.rs b/src/core/air/evaluation.rs index cca111d2e..e89c6df11 100644 --- a/src/core/air/evaluation.rs +++ b/src/core/air/evaluation.rs @@ -3,50 +3,13 @@ //! Given a random alpha, the combined polynomial is defined as //! f(p) = sum_i alpha^{N-1-i} u_i (P). use crate::core::backend::cpu::CPUCircleEvaluation; -use crate::core::backend::{Backend, CPUBackend, Col, Column}; +use crate::core::backend::{Backend, CPUBackend}; use crate::core::fields::m31::BaseField; use crate::core::fields::qm31::SecureField; -use crate::core::fields::{ExtensionOf, FieldExpOps}; -use crate::core::poly::circle::{CanonicCoset, CirclePoly, SecureCirclePoly}; +use crate::core::fields::secure::{SecureCirclePoly, SecureColumn}; +use crate::core::fields::FieldExpOps; +use crate::core::poly::circle::{CanonicCoset, CirclePoly}; use crate::core::poly::BitReversedOrder; -use crate::core::utils::IteratorMutExt; - -pub const SECURE_EXTENSION_DEGREE: usize = - >::EXTENSION_DEGREE; - -// TODO(spapini): find a better place for this -pub struct SecureColumn { - pub cols: [Col; SECURE_EXTENSION_DEGREE], -} - -impl SecureColumn { - fn at(&self, index: usize) -> SecureField { - SecureField::from_m31_array(std::array::from_fn(|i| self.cols[i][index])) - } - - fn set(&mut self, index: usize, value: SecureField) { - self.cols - .iter_mut() - .map(|c| &mut c[index]) - .assign(value.to_m31_array()); - } -} - -impl SecureColumn { - pub fn zeros(len: usize) -> Self { - Self { - cols: std::array::from_fn(|_| Col::::zeros(len)), - } - } - - pub fn len(&self) -> usize { - self.cols[0].len() - } - - pub fn is_empty(&self) -> bool { - self.cols[0].is_empty() - } -} /// Accumulates evaluations of u_i(P0) at a single point. /// Computes f(P0), the combined polynomial at that point. diff --git a/src/core/fields/mod.rs b/src/core/fields/mod.rs index 6bb0894ad..a025c44b2 100644 --- a/src/core/fields/mod.rs +++ b/src/core/fields/mod.rs @@ -10,6 +10,7 @@ use super::backend::ColumnOps; pub mod cm31; pub mod m31; pub mod qm31; +pub mod secure; pub trait FieldOps: ColumnOps { // TODO(Ohad): change to use a mutable slice. diff --git a/src/core/poly/circle/secure_poly.rs b/src/core/fields/secure.rs similarity index 52% rename from src/core/poly/circle/secure_poly.rs rename to src/core/fields/secure.rs index 986c648bd..f557952e7 100644 --- a/src/core/poly/circle/secure_poly.rs +++ b/src/core/fields/secure.rs @@ -1,12 +1,48 @@ use std::ops::Deref; -use crate::core::air::evaluation::SECURE_EXTENSION_DEGREE; +use super::m31::BaseField; +use super::qm31::SecureField; +use super::ExtensionOf; use crate::core::backend::cpu::CPUCirclePoly; +use crate::core::backend::{Backend, CPUBackend, Col, Column}; use crate::core::circle::CirclePoint; -use crate::core::fields::qm31::SecureField; +use crate::core::utils::IteratorMutExt; -pub struct SecureCirclePoly(pub [CPUCirclePoly; SECURE_EXTENSION_DEGREE]); +pub const SECURE_EXTENSION_DEGREE: usize = + >::EXTENSION_DEGREE; + +pub struct SecureColumn { + pub cols: [Col; SECURE_EXTENSION_DEGREE], +} +impl SecureColumn { + pub fn at(&self, index: usize) -> SecureField { + SecureField::from_m31_array(std::array::from_fn(|i| self.cols[i][index])) + } + + pub fn set(&mut self, index: usize, value: SecureField) { + self.cols + .iter_mut() + .map(|c| &mut c[index]) + .assign(value.to_m31_array()); + } +} +impl SecureColumn { + pub fn zeros(len: usize) -> Self { + Self { + cols: std::array::from_fn(|_| Col::::zeros(len)), + } + } + pub fn len(&self) -> usize { + self.cols[0].len() + } + + pub fn is_empty(&self) -> bool { + self.cols[0].is_empty() + } +} + +pub struct SecureCirclePoly(pub [CPUCirclePoly; SECURE_EXTENSION_DEGREE]); impl SecureCirclePoly { pub fn eval_at_point(&self, point: CirclePoint) -> SecureField { combine_secure_value(self.eval_columns_at_point(point)) @@ -24,7 +60,6 @@ impl SecureCirclePoly { ] } } - impl Deref for SecureCirclePoly { type Target = [CPUCirclePoly; SECURE_EXTENSION_DEGREE]; diff --git a/src/core/poly/circle/mod.rs b/src/core/poly/circle/mod.rs index f2cdb308d..1532fe662 100644 --- a/src/core/poly/circle/mod.rs +++ b/src/core/poly/circle/mod.rs @@ -3,14 +3,12 @@ mod domain; mod evaluation; mod ops; mod poly; -mod secure_poly; pub use canonic::CanonicCoset; pub use domain::{CircleDomain, MAX_CIRCLE_DOMAIN_LOG_SIZE}; pub use evaluation::{CircleEvaluation, CosetSubEvaluation}; pub use ops::PolyOps; pub use poly::CirclePoly; -pub use secure_poly::{combine_secure_value, SecureCirclePoly}; #[cfg(test)] mod tests { diff --git a/src/core/prover/mod.rs b/src/core/prover/mod.rs index dae2e2715..db6461127 100644 --- a/src/core/prover/mod.rs +++ b/src/core/prover/mod.rs @@ -16,7 +16,8 @@ use crate::core::circle::CirclePoint; use crate::core::commitment_scheme::{CommitmentSchemeProver, CommitmentSchemeVerifier}; use crate::core::fields::m31::BaseField; use crate::core::fields::qm31::SecureField; -use crate::core::poly::circle::{combine_secure_value, CircleEvaluation}; +use crate::core::fields::secure::combine_secure_value; +use crate::core::poly::circle::CircleEvaluation; use crate::core::poly::BitReversedOrder; use crate::core::ComponentVec;