Fix structure of polynomial

This commit is contained in:
jeong0982
2024-03-27 12:28:20 +09:00
parent 16c5ab665f
commit 8b664c5c8e
6 changed files with 60 additions and 40 deletions

View File

@@ -1 +1,22 @@
use ff::Field;
use crate::utils::{ProtocolError, TWO, log};
#[derive(Clone, Debug)]
pub struct Table<F: Field> {
table: Vec<F>,
/// Table size should be 2^k
/// pow_vars = k
exp_vars: u32
}
impl<F: Field> TryFrom<Vec<F>> for Table<F> {
type Error = ProtocolError;
fn try_from(table: Vec<F>) -> Result<Self, Self::Error> {
let exp_vars = log(table.len());
if TWO.pow(exp_vars) != table.len() {
Err(ProtocolError::SizeError)
} else {
Ok(Self { table, exp_vars })
}
}
}

View File

@@ -1,11 +1,39 @@
use std::fmt::Debug;
use ff::Field;
// These are different form of multivariate polynomial
pub mod coefficient;
pub mod factored;
pub mod composition;
/// [coeff, deg(x_1), ..., deg(x_n)]
#[derive(Debug, Clone)]
struct Term<F: Field>(Vec<F>);
pub trait MultivariatePolynomial<F: Field>: Clone + Debug {
fn interpolate(points: Vec<F>) -> Self;
/// num_vars: number of variables
/// coefficients: [[coeff, deg(x_1), ..., deg(x_n)], ...] respectively
#[derive(Clone, Debug)]
struct CoefficientForm<F: Field> {
num_vars: u32,
coefficients: Vec<Term<F>>,
}
/// format: f(g(h(..)))
/// outer: f
/// inner: g(..)
#[derive(Debug, Clone)]
struct CompositionForm {
}
#[derive(Debug, Clone)]
struct FactoredForm<F: Field> {
terms: Vec<Vec<Term<F>>>,
}
#[derive(Debug, Clone)]
pub enum MultivariatePolynomial<F: Field> {
Coeff(CoefficientForm<F>),
Comp(CompositionForm),
Fac(FactoredForm<F>),
}
impl<F: Field> MultivariatePolynomial<F> {
fn interpolate(points: Vec<F>, k: u32) -> Self {
todo!()
}
}

View File

@@ -1,34 +0,0 @@
use ff::Field;
use crate::poly::Polynomial;
use crate::utils::*;
use super::MultivariatePolynomial;
const TWO: usize = 2;
/// num_vars: number of variables
/// coefficients: [[coeff, deg(x_1), ..., deg(x_n)], ...] respectively
#[derive(Clone, Debug)]
pub struct CoefficientForm<F: Field> {
num_vars: u32,
coefficients: Vec<F>,
}
impl<F: Field> Polynomial<F> for CoefficientForm<F> {
type Point = Vec<F>;
fn evaluate(&self, point: &Self::Point) -> F {
todo!()
}
}
impl<F: Field> MultivariatePolynomial<F> for CoefficientForm<F> {
fn interpolate(points: Vec<F>) -> Self {
let num_vars = log(points.len());
// TODO: interpolation can be done with 2^k values
assert_eq!(TWO.pow(num_vars), points.len());
Self { num_vars, coefficients: () }
}
}

View File

@@ -1,3 +1,8 @@
pub const TWO: usize = 2;
pub enum ProtocolError {
SizeError,
}
pub fn log(n: usize) -> u32 {
let mut k = 0;
let mut m = n;