use core::ops::{Add, Mul, Sub};
use p3_field::{AbstractExtensionField, AbstractField, ExtensionField, Field};
use p3_matrix::dense::RowMajorMatrix;
use p3_matrix::Matrix;
pub trait BaseAir<F>: Sync {
fn width(&self) -> usize;
fn preprocessed_trace(&self) -> Option<RowMajorMatrix<F>> {
None
}
}
pub trait BaseAirWithPublicValues<F>: BaseAir<F> {
fn num_public_values(&self) -> usize {
0
}
}
pub trait Air<AB: AirBuilder>: BaseAir<AB::F> {
fn eval(&self, builder: &mut AB);
}
pub trait AirBuilder: Sized {
type F: Field;
type Expr: AbstractField
+ From<Self::F>
+ Add<Self::Var, Output = Self::Expr>
+ Add<Self::F, Output = Self::Expr>
+ Sub<Self::Var, Output = Self::Expr>
+ Sub<Self::F, Output = Self::Expr>
+ Mul<Self::Var, Output = Self::Expr>
+ Mul<Self::F, Output = Self::Expr>;
type Var: Into<Self::Expr>
+ Copy
+ Send
+ Sync
+ Add<Self::F, Output = Self::Expr>
+ Add<Self::Var, Output = Self::Expr>
+ Add<Self::Expr, Output = Self::Expr>
+ Sub<Self::F, Output = Self::Expr>
+ Sub<Self::Var, Output = Self::Expr>
+ Sub<Self::Expr, Output = Self::Expr>
+ Mul<Self::F, Output = Self::Expr>
+ Mul<Self::Var, Output = Self::Expr>
+ Mul<Self::Expr, Output = Self::Expr>;
type M: Matrix<Self::Var>;
fn main(&self) -> Self::M;
fn is_first_row(&self) -> Self::Expr;
fn is_last_row(&self) -> Self::Expr;
fn is_transition(&self) -> Self::Expr {
self.is_transition_window(2)
}
fn is_transition_window(&self, size: usize) -> Self::Expr;
fn when<I: Into<Self::Expr>>(&mut self, condition: I) -> FilteredAirBuilder<'_, Self> {
FilteredAirBuilder {
inner: self,
condition: condition.into(),
}
}
fn when_ne<I1: Into<Self::Expr>, I2: Into<Self::Expr>>(
&mut self,
x: I1,
y: I2,
) -> FilteredAirBuilder<'_, Self> {
self.when(x.into() - y.into())
}
fn when_first_row(&mut self) -> FilteredAirBuilder<'_, Self> {
self.when(self.is_first_row())
}
fn when_last_row(&mut self) -> FilteredAirBuilder<'_, Self> {
self.when(self.is_last_row())
}
fn when_transition(&mut self) -> FilteredAirBuilder<'_, Self> {
self.when(self.is_transition())
}
fn when_transition_window(&mut self, size: usize) -> FilteredAirBuilder<'_, Self> {
self.when(self.is_transition_window(size))
}
fn assert_zero<I: Into<Self::Expr>>(&mut self, x: I);
fn assert_one<I: Into<Self::Expr>>(&mut self, x: I) {
self.assert_zero(x.into() - Self::Expr::ONE);
}
fn assert_eq<I1: Into<Self::Expr>, I2: Into<Self::Expr>>(&mut self, x: I1, y: I2) {
self.assert_zero(x.into() - y.into());
}
fn assert_bool<I: Into<Self::Expr>>(&mut self, x: I) {
let x = x.into();
self.assert_zero(x.clone() * (x - Self::Expr::ONE));
}
}
pub trait AirBuilderWithPublicValues: AirBuilder {
type PublicVar: Into<Self::Expr> + Copy;
fn public_values(&self) -> &[Self::PublicVar];
}
pub trait PairBuilder: AirBuilder {
fn preprocessed(&self) -> Self::M;
}
pub trait ExtensionBuilder: AirBuilder {
type EF: ExtensionField<Self::F>;
type ExprEF: AbstractExtensionField<Self::Expr, F = Self::EF>;
type VarEF: Into<Self::ExprEF> + Copy + Send + Sync;
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: Matrix<Self::VarEF>;
type RandomVar: Into<Self::ExprEF> + Copy;
fn permutation(&self) -> Self::MP;
fn permutation_randomness(&self) -> &[Self::RandomVar];
}
#[derive(Debug)]
pub struct FilteredAirBuilder<'a, AB: AirBuilder> {
pub inner: &'a mut AB,
condition: AB::Expr,
}
impl<'a, AB: AirBuilder> FilteredAirBuilder<'a, AB> {
pub fn condition(&self) -> AB::Expr {
self.condition.clone()
}
}
impl<'a, AB: AirBuilder> AirBuilder for FilteredAirBuilder<'a, AB> {
type F = AB::F;
type Expr = AB::Expr;
type Var = AB::Var;
type M = AB::M;
fn main(&self) -> Self::M {
self.inner.main()
}
fn is_first_row(&self) -> Self::Expr {
self.inner.is_first_row()
}
fn is_last_row(&self) -> Self::Expr {
self.inner.is_last_row()
}
fn is_transition_window(&self, size: usize) -> Self::Expr {
self.inner.is_transition_window(size)
}
fn assert_zero<I: Into<Self::Expr>>(&mut self, x: I) {
self.inner.assert_zero(self.condition() * x.into());
}
}
impl<'a, AB: ExtensionBuilder> ExtensionBuilder for FilteredAirBuilder<'a, AB> {
type EF = AB::EF;
type ExprEF = AB::ExprEF;
type VarEF = AB::VarEF;
fn assert_zero_ext<I>(&mut self, x: I)
where
I: Into<Self::ExprEF>,
{
self.inner.assert_zero_ext(x.into() * self.condition());
}
}
impl<'a, AB: PermutationAirBuilder> PermutationAirBuilder for FilteredAirBuilder<'a, AB> {
type MP = AB::MP;
type RandomVar = AB::RandomVar;
fn permutation(&self) -> Self::MP {
self.inner.permutation()
}
fn permutation_randomness(&self) -> &[Self::RandomVar] {
self.inner.permutation_randomness()
}
}