Skip to content

Commit

Permalink
Formal polynomial evaluator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alon-Ti committed Sep 22, 2024
1 parent 83fe6fb commit 3cbbf8b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/prover/src/constraint_framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod info;
pub mod logup;
mod point;
pub mod poly;
pub mod poly_eval;
mod simd_domain;

use std::array;
Expand Down
34 changes: 32 additions & 2 deletions crates/prover/src/constraint_framework/poly.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::collections::BTreeMap;
use std::fmt::{Display, Formatter};
use std::ops::{Add, AddAssign, Mul, Neg};
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg};

use itertools::Itertools;
use num_traits::{One, Zero};

use crate::core::fields::m31::BaseField;
use crate::core::fields::qm31::SecureField;
use crate::core::fields::FieldExpOps;

/// A monic monomial consists of a list of variables and their exponents.
#[derive(Debug, Hash, PartialEq, PartialOrd, Eq, Ord, Clone)]
Expand All @@ -29,7 +30,7 @@ impl Monomial {

/// A polynomial consists of a list of monomials with coefficients.
#[derive(Debug, Hash, PartialEq, PartialOrd, Eq, Ord, Clone)]
struct Polynomial<F: From<BaseField>> {
pub struct Polynomial<F: From<BaseField>> {
monomials: BTreeMap<Monomial, F>,
}

Expand Down Expand Up @@ -126,6 +127,16 @@ where
}
}

impl<F, G> MulAssign<G> for Polynomial<F>
where
F: Clone + Mul<Output = F> + Add<Output = F> + AddAssign + From<BaseField>,
G: Into<Polynomial<F>>,
{
fn mul_assign(&mut self, rhs: G) {
*self = self.clone() * rhs.into();
}
}

impl<F> Mul<SecureField> for Polynomial<F>
where
F: Clone
Expand Down Expand Up @@ -157,6 +168,14 @@ impl<F: Zero + Clone + From<BaseField>> Zero for Polynomial<F> {
}
}

impl<F: One + From<BaseField> + AddAssign + Clone + Add<Output = F>> One for Polynomial<F> {
fn one() -> Self {
Self {
monomials: [(Monomial::default(), F::one())].into(),
}
}
}

impl<F: Neg<Output = F> + From<BaseField>> Neg for Polynomial<F> {
type Output = Self;
fn neg(self) -> Self {
Expand All @@ -180,6 +199,17 @@ where
}
}

impl<F> FieldExpOps for Polynomial<F>
where
F: Zero + One + Clone + Add + Mul + AddAssign + From<BaseField>,
{
fn inverse(&self) -> Self {
assert!(!self.is_zero(), "0 has no inverse");
let mut res = Self::from(BaseField::one()) / self.clone();
res
}
}

fn to_superscript(n: usize) -> String {
if n == 0 {
return std::char::from_u32(0x2070_u32).unwrap().to_string();
Expand Down
28 changes: 28 additions & 0 deletions crates/prover/src/constraint_framework/poly_eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use super::poly::Polynomial;
use super::EvalAtRow;
use crate::core::fields::m31::BaseField;
use crate::core::fields::qm31::SecureField;
use crate::core::fields::secure_column::SECURE_EXTENSION_DEGREE;

pub struct PolyEvaluator<'a> {}

impl<'a> EvalAtRow for PolyEvaluator<'a> {
type F = Polynomial<BaseField>;
type EF = Polynomial<SecureField>;
fn next_interaction_mask<const N: usize>(
&mut self,
interaction: usize,
offsets: [isize; N],
) -> [Self::F; N] {
unimplemented!()
}
fn add_constraint<G>(&mut self, constraint: G)
where
Self::EF: std::ops::Mul<G, Output = Self::EF>,
{
unimplemented!()
}
fn combine_ef(values: [Self::F; SECURE_EXTENSION_DEGREE]) -> Self::EF {
unimplemented!()
}
}

0 comments on commit 3cbbf8b

Please sign in to comment.