Skip to content

Commit

Permalink
refactor: move assert_zero_ext to ExtensionBuilder (Plonky3#232)
Browse files Browse the repository at this point in the history
refactor ext logic to ExtensionBuilder trait and make PermutationBuilder inherit from it
  • Loading branch information
tamirhemo authored Feb 6, 2024
1 parent b6862ca commit 0bf1512
Showing 1 changed file with 48 additions and 48 deletions.
96 changes: 48 additions & 48 deletions air/src/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,51 +106,40 @@ pub trait AirBuilder: Sized {
let x = x.into();
self.assert_zero(x.clone() * (x - Self::Expr::one()));
}

fn assert_zero_ext<ExprExt, I>(&mut self, x: I)
where
ExprExt: AbstractExtensionField<Self::Expr>,
I: Into<ExprExt>,
{
for xb in x.into().as_base_slice().iter().cloned() {
self.assert_zero(xb);
}
}

fn assert_eq_ext<ExprExt, I1, I2>(&mut self, x: I1, y: I2)
where
ExprExt: AbstractExtensionField<Self::Expr>,
I1: Into<ExprExt>,
I2: Into<ExprExt>,
{
self.assert_zero_ext::<ExprExt, ExprExt>(x.into() - y.into());
}

fn assert_one_ext<ExprExt, I>(&mut self, x: I)
where
ExprExt: AbstractExtensionField<Self::Expr>,
I: Into<ExprExt>,
{
let xe: ExprExt = x.into();
let parts = xe.as_base_slice();
self.assert_one(parts[0].clone());
for part in &parts[1..] {
self.assert_zero(part.clone());
}
}
}

pub trait PairBuilder: AirBuilder {
fn preprocessed(&self) -> Self::M;
}

pub trait PermutationAirBuilder: AirBuilder {
pub trait ExtensionBuilder: AirBuilder {
type EF: ExtensionField<Self::F>;

type ExprEF: AbstractExtensionField<Self::Expr, F = Self::EF>;

type VarEF: Into<Self::ExprEF> + Copy;

fn assert_zero_ext<I>(&mut self, x: I)
where
I: Into<Self::ExprEF>;

fn assert_eq_ext<I1, I2>(&mut self, x: I1, y: I2)
where
I1: Into<Self::ExprEF>,
I2: Into<Self::ExprEF>,
{
self.assert_zero_ext(x.into() - y.into());
}

fn assert_one_ext<I>(&mut self, x: I)
where
I: Into<Self::ExprEF>,
{
self.assert_eq_ext(x, Self::ExprEF::one())
}
}

pub trait PermutationAirBuilder: ExtensionBuilder {
type MP: MatrixRowSlices<Self::VarEF>;

fn permutation(&self) -> Self::MP;
Expand All @@ -165,21 +154,6 @@ pub struct FilteredAirBuilder<'a, AB: AirBuilder> {
condition: AB::Expr,
}

impl<'a, AB: PermutationAirBuilder> PermutationAirBuilder for FilteredAirBuilder<'a, AB> {
type EF = AB::EF;
type VarEF = AB::VarEF;
type ExprEF = AB::ExprEF;
type MP = AB::MP;

fn permutation(&self) -> Self::MP {
self.inner.permutation()
}

fn permutation_randomness(&self) -> &[Self::EF] {
self.inner.permutation_randomness()
}
}

impl<'a, AB: AirBuilder> AirBuilder for FilteredAirBuilder<'a, AB> {
type F = AB::F;
type Expr = AB::Expr;
Expand Down Expand Up @@ -207,6 +181,32 @@ impl<'a, AB: AirBuilder> AirBuilder for FilteredAirBuilder<'a, AB> {
}
}

impl<'a, AB: ExtensionBuilder> ExtensionBuilder for FilteredAirBuilder<'a, AB> {
type EF = AB::EF;
type VarEF = AB::VarEF;
type ExprEF = AB::ExprEF;

fn assert_zero_ext<I>(&mut self, x: I)
where
I: Into<Self::ExprEF>,
{
self.inner
.assert_zero_ext(x.into() * self.condition.clone());
}
}

impl<'a, AB: PermutationAirBuilder> PermutationAirBuilder for FilteredAirBuilder<'a, AB> {
type MP = AB::MP;

fn permutation(&self) -> Self::MP {
self.inner.permutation()
}

fn permutation_randomness(&self) -> &[Self::EF] {
self.inner.permutation_randomness()
}
}

#[cfg(test)]
mod tests {
use p3_matrix::MatrixRowSlices;
Expand Down

0 comments on commit 0bf1512

Please sign in to comment.