diff --git a/src/core/air/evaluation.rs b/src/core/air/evaluation.rs index fc7bdac5e..9e7f8d36c 100644 --- a/src/core/air/evaluation.rs +++ b/src/core/air/evaluation.rs @@ -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; diff --git a/src/core/backend/avx512/bit_reverse.rs b/src/core/backend/avx512/bit_reverse.rs index 42a939564..83a68d01e 100644 --- a/src/core/backend/avx512/bit_reverse.rs +++ b/src/core/backend/avx512/bit_reverse.rs @@ -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] diff --git a/src/core/backend/avx512/circle.rs b/src/core/backend/avx512/circle.rs index c491571c1..c8e085ff9 100644 --- a/src/core/backend/avx512/circle.rs +++ b/src/core/backend/avx512/circle.rs @@ -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, }; @@ -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}; diff --git a/src/core/backend/avx512/fft/ifft.rs b/src/core/backend/avx512/fft/ifft.rs index ed1823c66..814812122 100644 --- a/src/core/backend/avx512/fft/ifft.rs +++ b/src/core/backend/avx512/fft/ifft.rs @@ -522,9 +522,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] diff --git a/src/core/backend/avx512/fft/rfft.rs b/src/core/backend/avx512/fft/rfft.rs index 33d57c6da..75a2730a4 100644 --- a/src/core/backend/avx512/fft/rfft.rs +++ b/src/core/backend/avx512/fft/rfft.rs @@ -494,9 +494,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] diff --git a/src/core/backend/avx512/mod.rs b/src/core/backend/avx512/mod.rs index 766ece4a5..e84a2c02d 100644 --- a/src/core/backend/avx512/mod.rs +++ b/src/core/backend/avx512/mod.rs @@ -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; @@ -46,7 +47,7 @@ impl BaseFieldVec { } } -impl FieldOps for AVX512Backend { +impl ColumnOps for AVX512Backend { type Column = BaseFieldVec; fn bit_reverse_column(column: &mut Self::Column) { @@ -57,7 +58,9 @@ impl FieldOps for AVX512Backend { } bit_reverse_m31(&mut column.data); } +} +impl FieldOps for AVX512Backend { fn batch_inverse(column: &Self::Column, dst: &mut Self::Column) { PackedBaseField::batch_inverse(&column.data, &mut dst.data); } @@ -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; diff --git a/src/core/backend/cpu/circle.rs b/src/core/backend/cpu/circle.rs index be2b9abf2..9d6f9360d 100644 --- a/src/core/backend/cpu/circle.rs +++ b/src/core/backend/cpu/circle.rs @@ -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, }; diff --git a/src/core/backend/cpu/mod.rs b/src/core/backend/cpu/mod.rs index 997d0ae3d..9d32bbfbc 100644 --- a/src/core/backend/cpu/mod.rs +++ b/src/core/backend/cpu/mod.rs @@ -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; @@ -14,13 +14,15 @@ pub struct CPUBackend; impl Backend for CPUBackend {} -impl FieldOps for CPUBackend { - type Column = Vec; +impl ColumnOps for CPUBackend { + type Column = Vec; fn bit_reverse_column(column: &mut Self::Column) { bit_reverse(column) } +} +impl FieldOps for CPUBackend { /// Batch inversion using the Montgomery's trick. // TODO(Ohad): Benchmark this function. fn batch_inverse(column: &Self::Column, dst: &mut Self::Column) { @@ -28,18 +30,18 @@ impl FieldOps for CPUBackend { } } -impl Column for Vec { +impl Column for Vec { fn zeros(len: usize) -> Self { - vec![F::zero(); len] + vec![T::default(); len] } - fn to_vec(&self) -> Vec { + fn to_vec(&self) -> Vec { 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() } } @@ -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() { diff --git a/src/core/backend/mod.rs b/src/core/backend/mod.rs index 6e74f9bf2..026fbacc4 100644 --- a/src/core/backend/mod.rs +++ b/src/core/backend/mod.rs @@ -14,3 +14,26 @@ pub trait Backend: Copy + Clone + Debug + FieldOps + FieldOps + PolyOps { } + +pub trait ColumnOps { + type Column: Column; + fn bit_reverse_column(column: &mut Self::Column); +} + +pub type Col = >::Column; + +// TODO(spapini): Consider removing the generic parameter and only support BaseField. +pub trait Column: Clone + Debug + FromIterator { + /// 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; + /// 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; +} diff --git a/src/core/fields/mod.rs b/src/core/fields/mod.rs index 44a7c1cca..72693b5fa 100644 --- a/src/core/fields/mod.rs +++ b/src/core/fields/mod.rs @@ -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 { - type Column: Column; - fn bit_reverse_column(column: &mut Self::Column); - +pub trait FieldOps: ColumnOps { // TODO(Ohad): change to use a mutable slice. fn batch_inverse(column: &Self::Column, dst: &mut Self::Column); } @@ -95,29 +94,12 @@ fn batch_inverse_classic(column: &[T], dst: &mut [T]) { dst[0] = curr_inverse; } -pub type Col = >::Column; - -// TODO(spapini): Consider removing the generic parameter and only support BaseField. -pub trait Column: Clone + Debug + FromIterator { - /// 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; - /// 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 + ComplexConjugate + Copy + + Default + Debug + Display + PartialOrd diff --git a/src/core/fri.rs b/src/core/fri.rs index da8dc588d..b7f8971f4 100644 --- a/src/core/fri.rs +++ b/src/core/fri.rs @@ -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; diff --git a/src/core/poly/circle/evaluation.rs b/src/core/poly/circle/evaluation.rs index f40b07aa5..5c52f953d 100644 --- a/src/core/poly/circle/evaluation.rs +++ b/src/core/poly/circle/evaluation.rs @@ -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; diff --git a/src/core/poly/circle/ops.rs b/src/core/poly/circle/ops.rs index ac49eb5cf..1e9479f10 100644 --- a/src/core/poly/circle/ops.rs +++ b/src/core/poly/circle/ops.rs @@ -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; diff --git a/src/core/poly/circle/poly.rs b/src/core/poly/circle/poly.rs index e48f3cbba..906f8a0a8 100644 --- a/src/core/poly/circle/poly.rs +++ b/src/core/poly/circle/poly.rs @@ -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; diff --git a/src/core/poly/line.rs b/src/core/poly/line.rs index b9029d03f..e369401a0 100644 --- a/src/core/poly/line.rs +++ b/src/core/poly/line.rs @@ -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;