Skip to content

Commit

Permalink
ColumnOps
Browse files Browse the repository at this point in the history
  • Loading branch information
spapinistarkware committed Mar 14, 2024
1 parent 3664014 commit 53ef116
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/core/air/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
//! 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};
use crate::core::backend::{Backend, CPUBackend, Col, Column};
use crate::core::fields::m31::BaseField;
use crate::core::fields::qm31::SecureField;
use crate::core::fields::{Col, Column, ExtensionOf, FieldExpOps};
use crate::core::fields::{ExtensionOf, FieldExpOps};
use crate::core::poly::circle::{CircleDomain, CirclePoly, SecureCirclePoly};
use crate::core::poly::BitReversedOrder;
use crate::core::utils::IteratorMutExt;
Expand Down
2 changes: 1 addition & 1 deletion src/core/backend/avx512/bit_reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ mod tests {
use super::bit_reverse16;
use crate::core::backend::avx512::bit_reverse::bit_reverse_m31;
use crate::core::backend::avx512::BaseFieldVec;
use crate::core::backend::Column;
use crate::core::fields::m31::BaseField;
use crate::core::fields::Column;
use crate::core::utils::bit_reverse;

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/core/backend/avx512/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use super::m31::PackedBaseField;
use super::{as_cpu_vec, AVX512Backend, VECS_LOG_SIZE};
use crate::core::backend::avx512::fft::rfft;
use crate::core::backend::avx512::BaseFieldVec;
use crate::core::backend::CPUBackend;
use crate::core::backend::{CPUBackend, Col};
use crate::core::circle::{CirclePoint, Coset};
use crate::core::fields::m31::BaseField;
use crate::core::fields::{Col, ExtensionOf, FieldExpOps};
use crate::core::fields::{ExtensionOf, FieldExpOps};
use crate::core::poly::circle::{
CanonicCoset, CircleDomain, CircleEvaluation, CirclePoly, PolyOps,
};
Expand Down Expand Up @@ -178,8 +178,8 @@ impl PolyOps for AVX512Backend {
mod tests {
use crate::core::backend::avx512::fft::{CACHED_FFT_LOG_SIZE, MIN_FFT_LOG_SIZE};
use crate::core::backend::avx512::AVX512Backend;
use crate::core::backend::Column;
use crate::core::fields::m31::BaseField;
use crate::core::fields::Column;
use crate::core::poly::circle::{CanonicCoset, CircleDomain, CircleEvaluation, CirclePoly};
use crate::core::poly::{BitReversedOrder, NaturalOrder};

Expand Down
2 changes: 1 addition & 1 deletion src/core/backend/avx512/fft/ifft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,9 @@ mod tests {
use crate::core::backend::avx512::m31::PackedBaseField;
use crate::core::backend::avx512::BaseFieldVec;
use crate::core::backend::cpu::CPUCircleEvaluation;
use crate::core::backend::Column;
use crate::core::fft::ibutterfly;
use crate::core::fields::m31::BaseField;
use crate::core::fields::Column;
use crate::core::poly::circle::{CanonicCoset, CircleDomain};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/core/backend/avx512/fft/rfft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,9 @@ mod tests {
use super::*;
use crate::core::backend::avx512::{BaseFieldVec, PackedBaseField};
use crate::core::backend::cpu::CPUCirclePoly;
use crate::core::backend::Column;
use crate::core::fft::butterfly;
use crate::core::fields::m31::BaseField;
use crate::core::fields::Column;
use crate::core::poly::circle::{CanonicCoset, CircleDomain};

#[test]
Expand Down
9 changes: 6 additions & 3 deletions src/core/backend/avx512/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use num_traits::Zero;

use self::bit_reverse::bit_reverse_m31;
pub use self::m31::{PackedBaseField, K_BLOCK_SIZE};
use super::{Column, ColumnOps};
use crate::core::fields::m31::BaseField;
use crate::core::fields::{Column, FieldExpOps, FieldOps};
use crate::core::fields::{FieldExpOps, FieldOps};
use crate::core::utils;

const VECS_LOG_SIZE: usize = 4;
Expand Down Expand Up @@ -46,7 +47,7 @@ impl BaseFieldVec {
}
}

impl FieldOps<BaseField> for AVX512Backend {
impl ColumnOps<BaseField> for AVX512Backend {
type Column = BaseFieldVec;

fn bit_reverse_column(column: &mut Self::Column) {
Expand All @@ -57,7 +58,9 @@ impl FieldOps<BaseField> for AVX512Backend {
}
bit_reverse_m31(&mut column.data);
}
}

impl FieldOps<BaseField> for AVX512Backend {
fn batch_inverse(column: &Self::Column, dst: &mut Self::Column) {
PackedBaseField::batch_inverse(&column.data, &mut dst.data);
}
Expand Down Expand Up @@ -130,8 +133,8 @@ mod tests {
use rand::{Rng, SeedableRng};

use super::*;
use crate::core::backend::{Col, Column};
use crate::core::fields::m31::P;
use crate::core::fields::{Col, Column};

type B = AVX512Backend;

Expand Down
3 changes: 2 additions & 1 deletion src/core/backend/cpu/circle.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use num_traits::Zero;

use super::CPUBackend;
use crate::core::backend::{Col, ColumnOps};
use crate::core::circle::{CirclePoint, Coset};
use crate::core::fft::{butterfly, ibutterfly};
use crate::core::fields::m31::BaseField;
use crate::core::fields::{Col, ExtensionOf, FieldExpOps, FieldOps};
use crate::core::fields::{ExtensionOf, FieldExpOps};
use crate::core::poly::circle::{
CanonicCoset, CircleDomain, CircleEvaluation, CirclePoly, PolyOps,
};
Expand Down
24 changes: 13 additions & 11 deletions src/core/backend/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ mod fri;

use std::fmt::Debug;

use super::{Backend, FieldOps};
use crate::core::fields::{Column, Field};
use super::{Backend, Column, ColumnOps, FieldOps};
use crate::core::fields::Field;
use crate::core::poly::circle::{CircleEvaluation, CirclePoly};
use crate::core::poly::line::LineEvaluation;
use crate::core::utils::bit_reverse;
Expand All @@ -14,32 +14,34 @@ pub struct CPUBackend;

impl Backend for CPUBackend {}

impl<F: Field> FieldOps<F> for CPUBackend {
type Column = Vec<F>;
impl<T: Debug + Clone + Default> ColumnOps<T> for CPUBackend {
type Column = Vec<T>;

fn bit_reverse_column(column: &mut Self::Column) {
bit_reverse(column)
}
}

impl<F: Field> FieldOps<F> for CPUBackend {
/// Batch inversion using the Montgomery's trick.
// TODO(Ohad): Benchmark this function.
fn batch_inverse(column: &Self::Column, dst: &mut Self::Column) {
F::batch_inverse(column, &mut dst[..]);
}
}

impl<F: Field> Column<F> for Vec<F> {
impl<T: Debug + Clone + Default> Column<T> for Vec<T> {
fn zeros(len: usize) -> Self {
vec![F::zero(); len]
vec![T::default(); len]
}
fn to_vec(&self) -> Vec<F> {
fn to_vec(&self) -> Vec<T> {
self.clone()
}
fn len(&self) -> usize {
self.len()
}
fn at(&self, index: usize) -> F {
self[index]
fn at(&self, index: usize) -> T {
self[index].clone()
}
}

Expand All @@ -54,9 +56,9 @@ mod tests {
use rand::prelude::*;
use rand::rngs::SmallRng;

use crate::core::backend::{CPUBackend, FieldOps};
use crate::core::backend::{CPUBackend, Column, FieldOps};
use crate::core::fields::qm31::QM31;
use crate::core::fields::{Column, FieldExpOps};
use crate::core::fields::FieldExpOps;

#[test]
fn batch_inverse_test() {
Expand Down
23 changes: 23 additions & 0 deletions src/core/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,26 @@ pub trait Backend:
Copy + Clone + Debug + FieldOps<BaseField> + FieldOps<SecureField> + PolyOps
{
}

pub trait ColumnOps<T> {
type Column: Column<T>;
fn bit_reverse_column(column: &mut Self::Column);
}

pub type Col<B, T> = <B as ColumnOps<T>>::Column;

// TODO(spapini): Consider removing the generic parameter and only support BaseField.
pub trait Column<T>: Clone + Debug + FromIterator<T> {
/// Creates a new column of zeros with the given length.
fn zeros(len: usize) -> Self;
/// Returns a cpu vector of the column.
fn to_vec(&self) -> Vec<T>;
/// Returns the length of the column.
fn len(&self) -> usize;
/// Returns true if the column is empty.
fn is_empty(&self) -> bool {
self.len() == 0
}
/// Retrieves the element at the given index.
fn at(&self, index: usize) -> T;
}
26 changes: 4 additions & 22 deletions src/core/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use std::ops::{Mul, MulAssign, Neg};

use num_traits::{NumAssign, NumAssignOps, NumOps, One};

use super::backend::ColumnOps;

#[cfg(target_arch = "x86_64")]
pub mod cm31;
pub mod m31;
pub mod qm31;

pub trait FieldOps<F: Field> {
type Column: Column<F>;
fn bit_reverse_column(column: &mut Self::Column);

pub trait FieldOps<F: Field>: ColumnOps<F> {
// TODO(Ohad): change to use a mutable slice.
fn batch_inverse(column: &Self::Column, dst: &mut Self::Column);
}
Expand Down Expand Up @@ -95,29 +94,12 @@ fn batch_inverse_classic<T: FieldExpOps>(column: &[T], dst: &mut [T]) {
dst[0] = curr_inverse;
}

pub type Col<B, F> = <B as FieldOps<F>>::Column;

// TODO(spapini): Consider removing the generic parameter and only support BaseField.
pub trait Column<F: Field>: Clone + Debug + FromIterator<F> {
/// Creates a new column of zeros with the given length.
fn zeros(len: usize) -> Self;
/// Returns a cpu vector of the column.
fn to_vec(&self) -> Vec<F>;
/// Returns the length of the column.
fn len(&self) -> usize;
/// Returns true if the column is empty.
fn is_empty(&self) -> bool {
self.len() == 0
}
/// Retrieves the element at the given index.
fn at(&self, index: usize) -> F;
}

pub trait Field:
NumAssign
+ Neg<Output = Self>
+ ComplexConjugate
+ Copy
+ Default
+ Debug
+ Display
+ PartialOrd
Expand Down
2 changes: 1 addition & 1 deletion src/core/fri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use super::queries::{Queries, SparseSubCircleDomain};
use crate::commitment_scheme::hasher::Hasher;
use crate::commitment_scheme::merkle_decommitment::MerkleDecommitment;
use crate::commitment_scheme::merkle_tree::MerkleTree;
use crate::core::backend::Column;
use crate::core::circle::Coset;
use crate::core::fields::Column;
use crate::core::poly::line::LineDomain;
use crate::core::utils::bit_reverse_index;

Expand Down
3 changes: 2 additions & 1 deletion src/core/poly/circle/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::ops::{Deref, Index};

use super::{CanonicCoset, CircleDomain, CirclePoly, PolyOps};
use crate::core::backend::cpu::CPUCircleEvaluation;
use crate::core::backend::{Col, Column};
use crate::core::circle::{CirclePointIndex, Coset};
use crate::core::fields::m31::BaseField;
use crate::core::fields::{Col, Column, ExtensionOf, FieldOps};
use crate::core::fields::{ExtensionOf, FieldOps};
use crate::core::poly::twiddles::TwiddleTree;
use crate::core::poly::{BitReversedOrder, NaturalOrder};
use crate::core::utils::bit_reverse_index;
Expand Down
3 changes: 2 additions & 1 deletion src/core/poly/circle/ops.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::{CanonicCoset, CircleDomain, CircleEvaluation, CirclePoly};
use crate::core::backend::Col;
use crate::core::circle::{CirclePoint, Coset};
use crate::core::fields::m31::BaseField;
use crate::core::fields::{Col, ExtensionOf, FieldOps};
use crate::core::fields::{ExtensionOf, FieldOps};
use crate::core::poly::twiddles::TwiddleTree;
use crate::core::poly::BitReversedOrder;

Expand Down
3 changes: 2 additions & 1 deletion src/core/poly/circle/poly.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::{CircleDomain, CircleEvaluation, PolyOps};
use crate::core::backend::{Col, Column};
use crate::core::circle::CirclePoint;
use crate::core::fields::m31::BaseField;
use crate::core::fields::{Col, Column, ExtensionOf, FieldOps};
use crate::core::fields::{ExtensionOf, FieldOps};
use crate::core::poly::twiddles::TwiddleTree;
use crate::core::poly::BitReversedOrder;

Expand Down
3 changes: 2 additions & 1 deletion src/core/poly/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use num_traits::Zero;
use super::utils::fold;
use super::{BitReversedOrder, NaturalOrder};
use crate::core::backend::cpu::CPULineEvaluation;
use crate::core::backend::{Col, Column};
use crate::core::circle::{CirclePoint, Coset, CosetIterator};
use crate::core::fft::{butterfly, ibutterfly};
use crate::core::fields::m31::BaseField;
use crate::core::fields::{Col, Column, ExtensionOf, Field, FieldExpOps, FieldOps};
use crate::core::fields::{ExtensionOf, Field, FieldExpOps, FieldOps};
use crate::core::poly::utils::repeat_value;
use crate::core::utils::bit_reverse;

Expand Down

0 comments on commit 53ef116

Please sign in to comment.