use crate::arithmetic::parallelize;
use crate::helpers::{SerdeFormat, SerdePrimeField};
use group::ff::Field;
use halo2_middleware::poly::Rotation;
use std::fmt::Debug;
use std::io;
use std::marker::PhantomData;
use std::ops::{Add, Deref, DerefMut, Index, IndexMut, Mul, RangeFrom, RangeFull, Sub};
pub mod commitment;
mod domain;
mod query;
mod strategy;
pub mod kzg;
#[cfg(test)]
mod multiopen_test;
pub use domain::*;
pub(crate) use query::{ProverQuery, VerifierQuery};
pub use strategy::{Guard, VerificationStrategy};
#[derive(Debug)]
pub enum Error {
OpeningError,
SamplingError,
}
pub trait Basis: Copy + Debug + Send + Sync {}
pub trait LagrangeBasis: Copy + Debug + Send + Sync {}
#[derive(Clone, Copy, Debug)]
pub struct Coeff;
impl Basis for Coeff {}
#[derive(Clone, Copy, Debug)]
pub struct LagrangeCoeff;
impl Basis for LagrangeCoeff {}
impl LagrangeBasis for LagrangeCoeff {}
#[derive(Clone, Copy, Debug)]
pub struct ExtendedLagrangeCoeff;
impl Basis for ExtendedLagrangeCoeff {}
impl LagrangeBasis for ExtendedLagrangeCoeff {}
#[derive(Clone, Debug)]
pub struct Polynomial<F, B> {
pub values: Vec<F>,
pub _marker: PhantomData<B>,
}
impl<F: Clone, B> Polynomial<F, B> {
pub fn new_empty(size: usize, zero: F) -> Self {
Polynomial {
values: vec![zero; size],
_marker: PhantomData,
}
}
}
impl<F: Clone> Polynomial<F, LagrangeCoeff> {
pub fn new_lagrange_from_vec(values: Vec<F>) -> Polynomial<F, LagrangeCoeff> {
Polynomial {
values,
_marker: PhantomData,
}
}
}
impl<F, B> Index<usize> for Polynomial<F, B> {
type Output = F;
fn index(&self, index: usize) -> &F {
self.values.index(index)
}
}
impl<F, B> IndexMut<usize> for Polynomial<F, B> {
fn index_mut(&mut self, index: usize) -> &mut F {
self.values.index_mut(index)
}
}
impl<F, B> Index<RangeFrom<usize>> for Polynomial<F, B> {
type Output = [F];
fn index(&self, index: RangeFrom<usize>) -> &[F] {
self.values.index(index)
}
}
impl<F, B> IndexMut<RangeFrom<usize>> for Polynomial<F, B> {
fn index_mut(&mut self, index: RangeFrom<usize>) -> &mut [F] {
self.values.index_mut(index)
}
}
impl<F, B> Index<RangeFull> for Polynomial<F, B> {
type Output = [F];
fn index(&self, index: RangeFull) -> &[F] {
self.values.index(index)
}
}
impl<F, B> IndexMut<RangeFull> for Polynomial<F, B> {
fn index_mut(&mut self, index: RangeFull) -> &mut [F] {
self.values.index_mut(index)
}
}
impl<F, B> Deref for Polynomial<F, B> {
type Target = [F];
fn deref(&self) -> &[F] {
&self.values[..]
}
}
impl<F, B> DerefMut for Polynomial<F, B> {
fn deref_mut(&mut self) -> &mut [F] {
&mut self.values[..]
}
}
impl<F, B> Polynomial<F, B> {
pub fn iter(&self) -> impl Iterator<Item = &F> {
self.values.iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut F> {
self.values.iter_mut()
}
pub fn num_coeffs(&self) -> usize {
self.values.len()
}
}
impl<F: SerdePrimeField, B> Polynomial<F, B> {
pub(crate) fn read<R: io::Read>(reader: &mut R, format: SerdeFormat) -> io::Result<Self> {
let mut poly_len = [0u8; 4];
reader.read_exact(&mut poly_len)?;
let poly_len = u32::from_be_bytes(poly_len);
(0..poly_len)
.map(|_| F::read(reader, format))
.collect::<io::Result<Vec<_>>>()
.map(|values| Self {
values,
_marker: PhantomData,
})
}
pub(crate) fn write<W: io::Write>(
&self,
writer: &mut W,
format: SerdeFormat,
) -> io::Result<()> {
writer.write_all(&(self.values.len() as u32).to_be_bytes())?;
for value in self.values.iter() {
value.write(writer, format)?;
}
Ok(())
}
}
impl<'a, F: Field, B: Basis> Add<&'a Polynomial<F, B>> for Polynomial<F, B> {
type Output = Polynomial<F, B>;
fn add(mut self, rhs: &'a Polynomial<F, B>) -> Polynomial<F, B> {
parallelize(&mut self.values, |lhs, start| {
for (lhs, rhs) in lhs.iter_mut().zip(rhs.values[start..].iter()) {
*lhs += *rhs;
}
});
self
}
}
impl<'a, F: Field, B: Basis> Sub<&'a Polynomial<F, B>> for Polynomial<F, B> {
type Output = Polynomial<F, B>;
fn sub(mut self, rhs: &'a Polynomial<F, B>) -> Polynomial<F, B> {
parallelize(&mut self.values, |lhs, start| {
for (lhs, rhs) in lhs.iter_mut().zip(rhs.values[start..].iter()) {
*lhs -= *rhs;
}
});
self
}
}
impl<F: Field> Polynomial<F, LagrangeCoeff> {
pub fn rotate(&self, rotation: Rotation) -> Polynomial<F, LagrangeCoeff> {
let mut values = self.values.clone();
if rotation.0 < 0 {
values.rotate_right((-rotation.0) as usize);
} else {
values.rotate_left(rotation.0 as usize);
}
Polynomial {
values,
_marker: PhantomData,
}
}
}
impl<F: Field, B: Basis> Mul<F> for Polynomial<F, B> {
type Output = Polynomial<F, B>;
fn mul(mut self, rhs: F) -> Polynomial<F, B> {
if rhs == F::ZERO {
return Polynomial {
values: vec![F::ZERO; self.len()],
_marker: PhantomData,
};
}
if rhs == F::ONE {
return self;
}
parallelize(&mut self.values, |lhs, _| {
for lhs in lhs.iter_mut() {
*lhs *= rhs;
}
});
self
}
}
impl<'a, F: Field, B: Basis> Sub<F> for &'a Polynomial<F, B> {
type Output = Polynomial<F, B>;
fn sub(self, rhs: F) -> Polynomial<F, B> {
let mut res = self.clone();
res.values[0] -= rhs;
res
}
}