Skip to content

Commit

Permalink
add: basic polynomial crate (#1)
Browse files Browse the repository at this point in the history
* add: basic poly

* fix: doctest

* fix: roots_for_poly_repr
  • Loading branch information
supragya authored Mar 6, 2024
1 parent 18c91d5 commit fccde7e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"polynomial",
"univariate-polynomial-iop-zerotest"
]
resolver = "2"
Expand Down
7 changes: 7 additions & 0 deletions polynomial/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
edition = "2021"
name = "polynomial"
version = "0.1.0"

[dependencies]
num-traits = "0.2.18"
94 changes: 94 additions & 0 deletions polynomial/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::{fmt::Debug, ops::{Mul, Sub}};

use num_traits::{One, Zero};

pub enum PolynomialRepr<T> {
Points(Vec<(T, T)>),
Roots(Vec<T>),
/// Assumed index `i` to hold `C_i` when polynomial looks as follows:
/// `C_0 + C_1 * x + C_2 * x^2 + ... + C_n * x^n`
Coeff(Vec<T>),
}

/// A polynomial
pub struct Polynomial<T> {
repr: PolynomialRepr<T>,
}

impl<T> Polynomial<T>
where
T: Clone,
{
/// Generate a polynomial from its coefficients `C_0`, `C_1`, ... `C_n`
/// when polynomial looks as follows:
/// `C_0 + C_1 * x + C_2 * x^2 + ... + C_n * x^n`
pub fn new_from_coeffs(coeffs: &[T]) -> Self {
Self {
repr: PolynomialRepr::Coeff(coeffs.to_vec()),
}
}

/// Generate a polynomial from its roots `r_0`, `r_1`, ... `r_n`
/// when polynomial looks as follows:
/// `(x - r_0)(x - r_1)(x - r_2)...(x - r_n)`
pub fn new_from_roots(roots: &[T]) -> Self {
Self {
repr: PolynomialRepr::Roots(roots.to_vec()),
}
}
}

impl<T: Zero + One + Mul<Output = T> + Sub<Output = T> + Clone + Debug> Polynomial<T> {
/// Evaluates the polynomial at a point.
///
/// # Examples
///
/// ```
/// use polynomial::Polynomial;
/// let polynomial = Polynomial::<u32>::new_from_coeffs(&[3, 2, 1]);
/// assert_eq!(polynomial.eval(0), 3);
/// assert_eq!(polynomial.eval(1), 6);
/// assert_eq!(polynomial.eval(10), 123);
/// ```
#[inline]
pub fn eval(&self, x: T) -> T {
match &self.repr {
PolynomialRepr::Points(_points) => unimplemented!(),
PolynomialRepr::Roots(roots) => {
let mut result: T = One::one();
for n in roots.iter() {
result = (x.clone() - n.clone()) * result;
}
result
},
PolynomialRepr::Coeff(coeffs) => {
let mut result: T = Zero::zero();
for n in coeffs.iter().rev() {
result = n.clone() + result * x.clone();
}
result
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn eval_for_coeff_polynomial_repr() {
let polynomial = Polynomial::<u32>::new_from_coeffs(&[3, 2, 1]);
assert_eq!(polynomial.eval(0), 3);
assert_eq!(polynomial.eval(1), 6);
assert_eq!(polynomial.eval(10), 123);
}

#[test]
fn eval_for_roots_polynomial_repr() {
let polynomial = Polynomial::<i32>::new_from_roots(&[1, 2, 3]);
assert_eq!(polynomial.eval(1), 0);
assert_eq!(polynomial.eval(2), 0);
assert_eq!(polynomial.eval(3), 0);
}
}
15 changes: 12 additions & 3 deletions univariate-polynomial-iop-zerotest/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
use ark_ff::fields::{Fp64, MontBackend, MontConfig};

/// 41 = (2^3 x 5) + 1
/// Hence, a 8th root of unity would exist in this
/// Hence, a 8th root of unity would exist in this, since 2^3 is a divisor
/// 3 is such root of unity. 3^8 = 6561. And 6561 % 41 = 1
/// Building a field is given at: https://docs.rs/ark-ff/latest/ark_ff/fields/trait.Field.html
#[derive(MontConfig)]
#[modulus = "41"]
#[generator = "2"]
#[small_subgroup_base = "3"]
#[small_subgroup_power = "8"]
pub struct FqConfig;
pub type Fq = Fp64<MontBackend<FqConfig, 1>>;

pub enum PolynomialRepr<T> {
Points(Vec<(T, T)>),
Roots(Vec<T>),
Coeff(Vec<T>),
}

/// A polynomial
pub struct Polynomial<T> {
repr: PolynomialRepr<T>,
}

#[cfg(test)]
mod tests {
use ark_ff::{BigInt, Field, PrimeField};
Expand Down

0 comments on commit fccde7e

Please sign in to comment.