Skip to content

Commit

Permalink
Move SecureColumn
Browse files Browse the repository at this point in the history
  • Loading branch information
spapinistarkware committed Mar 21, 2024
1 parent 07a69ae commit 585aa23
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 60 deletions.
47 changes: 5 additions & 42 deletions src/core/air/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::fields::secure_column::SecureColumn;
use crate::core::fields::FieldExpOps;
use crate::core::poly::circle::{CanonicCoset, CirclePoly, SecureCirclePoly};
use crate::core::poly::BitReversedOrder;
use crate::core::utils::IteratorMutExt;

pub const SECURE_EXTENSION_DEGREE: usize =
<SecureField as ExtensionOf<BaseField>>::EXTENSION_DEGREE;

// TODO(spapini): find a better place for this
pub struct SecureColumn<B: Backend> {
pub cols: [Col<B, BaseField>; SECURE_EXTENSION_DEGREE],
}

impl SecureColumn<CPUBackend> {
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<B: Backend> SecureColumn<B> {
pub fn zeros(len: usize) -> Self {
Self {
cols: std::array::from_fn(|_| Col::<B, BaseField>::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.
Expand Down Expand Up @@ -167,7 +130,7 @@ impl DomainEvaluationAccumulator<CPUBackend> {
.skip(1)
{
let coeffs = SecureColumn {
cols: values.cols.map(|c| {
columns: values.columns.map(|c| {
CPUCircleEvaluation::<_, BitReversedOrder>::new(
CanonicCoset::new(log_size as u32).circle_domain(),
c,
Expand All @@ -185,7 +148,7 @@ impl DomainEvaluationAccumulator<CPUBackend> {
}
}

SecureCirclePoly(res_coeffs.cols.map(CirclePoly::new))
SecureCirclePoly(res_coeffs.columns.map(CirclePoly::new))
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/core/channel.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::iter;

use super::fields::m31::{BaseField, N_BYTES_FELT, P};
use super::fields::qm31::{SecureField, SECURE_FIELD_EXTENSION_DEGREE};
use super::fields::IntoSlice;
use super::fields::qm31::SecureField;
use super::fields::{ExtensionOf, IntoSlice};
use crate::commitment_scheme::blake2_hash::{Blake2sHash, Blake2sHasher};
use crate::commitment_scheme::hasher::Hasher;

Expand Down Expand Up @@ -122,7 +122,11 @@ impl Channel for Blake2sChannel {

fn draw_felt(&mut self) -> SecureField {
let felts: [BaseField; FELTS_PER_HASH] = self.draw_base_felts();
SecureField::from_m31_array(felts[..SECURE_FIELD_EXTENSION_DEGREE].try_into().unwrap())
SecureField::from_m31_array(
felts[..<SecureField as ExtensionOf<BaseField>>::EXTENSION_DEGREE]
.try_into()
.unwrap(),
)
}

fn draw_felts(&mut self, n_felts: usize) -> Vec<SecureField> {
Expand Down
1 change: 1 addition & 0 deletions src/core/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::backend::ColumnOps;
pub mod cm31;
pub mod m31;
pub mod qm31;
pub mod secure_column;

pub trait FieldOps<F: Field>: ColumnOps<F> {
// TODO(Ohad): change to use a mutable slice.
Expand Down
1 change: 0 additions & 1 deletion src/core/fields/qm31.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::core::fields::cm31::CM31;
use crate::core::fields::m31::M31;
use crate::{impl_extension_field, impl_field};

pub const SECURE_FIELD_EXTENSION_DEGREE: usize = 4;
pub const P4: u128 = 21267647892944572736998860269687930881; // (2 ** 31 - 1) ** 4
pub const R: CM31 = CM31::from_u32_unchecked(2, 1);

Expand Down
41 changes: 41 additions & 0 deletions src/core/fields/secure_column.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use super::m31::BaseField;
use super::qm31::SecureField;
use super::ExtensionOf;
use crate::core::backend::{Backend, CPUBackend, Col, Column};
use crate::core::utils::IteratorMutExt;

pub const SECURE_EXTENSION_DEGREE: usize =
<SecureField as ExtensionOf<BaseField>>::EXTENSION_DEGREE;

/// An array of `SECURE_EXTENSION_DEGREE` base field columns, that represents a column of secure
/// field elements.
pub struct SecureColumn<B: Backend> {
pub columns: [Col<B, BaseField>; SECURE_EXTENSION_DEGREE],
}
impl SecureColumn<CPUBackend> {
pub fn at(&self, index: usize) -> SecureField {
SecureField::from_m31_array(std::array::from_fn(|i| self.columns[i][index]))
}

pub fn set(&mut self, index: usize, value: SecureField) {
self.columns
.iter_mut()
.map(|c| &mut c[index])
.assign(value.to_m31_array());
}
}
impl<B: Backend> SecureColumn<B> {
pub fn zeros(len: usize) -> Self {
Self {
columns: std::array::from_fn(|_| Col::<B, BaseField>::zeros(len)),
}
}

pub fn len(&self) -> usize {
self.columns[0].len()
}

pub fn is_empty(&self) -> bool {
self.columns[0].is_empty()
}
}
2 changes: 1 addition & 1 deletion src/core/poly/circle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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};
pub use secure_poly::SecureCirclePoly;

#[cfg(test)]
mod tests {
Expand Down
22 changes: 12 additions & 10 deletions src/core/poly/circle/secure_poly.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::ops::Deref;

use crate::core::air::evaluation::SECURE_EXTENSION_DEGREE;
use crate::core::backend::cpu::CPUCirclePoly;
use crate::core::circle::CirclePoint;
use crate::core::fields::qm31::SecureField;
use crate::core::fields::secure_column::SECURE_EXTENSION_DEGREE;

pub struct SecureCirclePoly(pub [CPUCirclePoly; SECURE_EXTENSION_DEGREE]);

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

pub fn eval_columns_at_point(
Expand All @@ -23,6 +23,16 @@ impl SecureCirclePoly {
self[3].eval_at_point(point),
]
}

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

impl Deref for SecureCirclePoly {
Expand All @@ -32,11 +42,3 @@ impl Deref for SecureCirclePoly {
&self.0
}
}

pub fn combine_secure_value(value: [SecureField; SECURE_EXTENSION_DEGREE]) -> SecureField {
let mut res = value[0];
res += value[1] * SecureField::from_u32_unchecked(0, 1, 0, 0);
res += value[2] * SecureField::from_u32_unchecked(0, 0, 1, 0);
res += value[3] * SecureField::from_u32_unchecked(0, 0, 0, 1);
res
}
6 changes: 3 additions & 3 deletions src/core/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use thiserror::Error;

use super::commitment_scheme::{CommitmentSchemeProof, TreeVec};
use super::fri::FriVerificationError;
use super::poly::circle::MAX_CIRCLE_DOMAIN_LOG_SIZE;
use super::poly::circle::{SecureCirclePoly, MAX_CIRCLE_DOMAIN_LOG_SIZE};
use super::proof_of_work::ProofOfWorkVerificationError;
use super::ColumnVec;
use crate::commitment_scheme::blake2_hash::Blake2sHasher;
Expand All @@ -16,7 +16,7 @@ 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::poly::circle::CircleEvaluation;
use crate::core::poly::BitReversedOrder;
use crate::core::ComponentVec;

Expand Down Expand Up @@ -158,7 +158,7 @@ fn opened_values_to_mask(
air: &impl Air<CPUBackend>,
mut opened_values: TreeVec<ColumnVec<Vec<SecureField>>>,
) -> Result<(ComponentVec<Vec<SecureField>>, SecureField), ()> {
let composition_oods_values = combine_secure_value(
let composition_oods_values = SecureCirclePoly::eval_from_partial_evals(
opened_values
.pop()
.unwrap()
Expand Down

0 comments on commit 585aa23

Please sign in to comment.