use core::ops::{Add, Mul, Sub};
use p3_field::{Algebra, Dup, ExtensionField, Field, PrimeCharacteristicRing};
use crate::filtered::FilteredAirBuilder;
use crate::window::WindowAccess;
pub trait AirBuilder: Sized {
type F: PrimeCharacteristicRing + Sync;
type Expr: Algebra<Self::F> + Algebra<Self::Var>;
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 PreprocessedWindow: WindowAccess<Self::Var> + Clone;
type MainWindow: WindowAccess<Self::Var> + Clone;
type PublicVar: Into<Self::Expr> + Copy;
type PeriodicVar: Into<Self::Expr> + Copy;
const WINDOW: usize = 2;
fn main(&self) -> Self::MainWindow;
fn preprocessed(&self) -> &Self::PreprocessedWindow;
fn is_first_row(&self) -> Self::Expr;
fn is_last_row(&self) -> Self::Expr;
fn is_transition(&self) -> Self::Expr;
fn is_transition_window(&self, size: usize) -> Self::Expr {
assert!(
size <= Self::WINDOW,
"window size {size} exceeds supported arity {}",
Self::WINDOW
);
self.is_transition()
}
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_zeros<const N: usize, I: Into<Self::Expr>>(&mut self, array: [I; N]) {
for elem in array {
self.assert_zero(elem);
}
}
fn assert_bools<const N: usize, I: Into<Self::Expr>>(&mut self, array: [I; N]) {
let zero_array = array.map(|x| x.into().bool_check());
self.assert_zeros(zero_array);
}
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_eq_arrays<I1, I2, const N: usize>(&mut self, lhs: [I1; N], rhs: [I2; N])
where
I1: Dup + Into<Self::Expr>,
I2: Dup + Into<Self::Expr>,
{
debug_assert_eq!(
lhs.len(),
rhs.len(),
"assert_eq_arrays: length mismatch ({} vs {})",
lhs.len(),
rhs.len()
);
let diff: [Self::Expr; N] =
core::array::from_fn(|i| lhs[i].dup().into() - rhs[i].dup().into());
self.assert_zeros(diff);
}
fn public_values(&self) -> &[Self::PublicVar] {
&[]
}
fn periodic_values(&self) -> &[Self::PeriodicVar] {
&[]
}
fn assert_bool<I: Into<Self::Expr>>(&mut self, x: I) {
self.assert_zero(x.into().bool_check());
}
}
pub trait AirBuilderWithContext: AirBuilder {
type EvalContext;
fn eval_context(&self) -> &Self::EvalContext;
}
pub trait ExtensionBuilder: AirBuilder<F: Field> {
type EF: ExtensionField<Self::F>;
type ExprEF: Algebra<Self::Expr> + Algebra<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_zeros_ext<const N: usize, I>(&mut self, array: [I; N])
where
I: Into<Self::ExprEF>,
{
for x in array {
self.assert_zero_ext(x);
}
}
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: WindowAccess<Self::VarEF>;
type RandomVar: Into<Self::ExprEF> + Copy;
type PermutationVar: Into<Self::ExprEF> + Clone;
fn permutation(&self) -> Self::MP;
fn permutation_randomness(&self) -> &[Self::RandomVar];
fn permutation_values(&self) -> &[Self::PermutationVar];
}