-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* FiniteFiniteField corr * add basic multi_var_poly * make stuff vec * test passes * added testing and display * add comments to mult_var_poly * added comments to sum-check * added example * added readme --------- Co-authored-by: goforashutosh <ashtheknight@gmail.com>
- Loading branch information
1 parent
ed9de5d
commit 5e61a8e
Showing
9 changed files
with
816 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// File: examples/sum_check_demo.rs | ||
|
||
use ronkathon::{ | ||
algebra::field::prime::PlutoBaseField, multi_var_poly::MultiVarPolynomial, sumcheck::SumCheck, | ||
}; | ||
|
||
type F = PlutoBaseField; | ||
|
||
fn create_demo_polynomial() -> MultiVarPolynomial<F> { | ||
// Create the polynomial: | ||
// 3 x^2 y^2 z^2 + 2x^2 y + 5x^2 z^2 + 4yz + 6x + 1 | ||
let coordinates = vec![ | ||
vec![0, 0, 0], // Constant term | ||
vec![1, 0, 0], // x term | ||
vec![0, 1, 1], // yz term | ||
vec![2, 0, 2], // x^2 z^2 term | ||
vec![2, 1, 0], // x^2 y term | ||
vec![2, 2, 2], // x^2 y^2 z^2 term | ||
]; | ||
let coefficients = vec![ | ||
F::from(1), // Constant term | ||
F::from(6), // x term | ||
F::from(4), // yz term | ||
F::from(5), // x^2 z^2 term | ||
F::from(2), // x^2 y term | ||
F::from(3), // x^2 y^2 z^2 term | ||
]; | ||
MultiVarPolynomial::from_coordinates(coordinates, coefficients).unwrap() | ||
} | ||
|
||
fn main() { | ||
println!("Sum-Check Protocol Demonstration"); | ||
println!("================================"); | ||
|
||
let poly = create_demo_polynomial(); | ||
println!("Created multivariate polynomial:"); | ||
println!("3 x^2 y^2 z^2 + 2x^2 y + 5x^2 z^2 + 4yz + 6x + 1"); | ||
|
||
let expected_sum = F::from(57); | ||
println!("\nExpected sum over boolean hypercube: {:?}", expected_sum); | ||
|
||
let mut sumcheck = SumCheck::new(poly, true); | ||
println!("\nRunning interactive sum-check protocol:"); | ||
sumcheck.run_interactive_protocol(); | ||
|
||
println!("\nVerification result:"); | ||
if sumcheck.verifier.result == expected_sum { | ||
println!("Sum-check protocol succeeded! The sum is verified to be {:?}", expected_sum); | ||
} else { | ||
println!( | ||
"Sum-check protocol failed. Expected {:?}, but got {:?}", | ||
expected_sum, sumcheck.verifier.result | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//! Arithmetic operations for multivariate polynomials. | ||
//! The operations are implemented for [`MultiVarPolynomial`] in the monomial basis. | ||
//! | ||
//! Note: Operations are restricted to polynomials with the same degree structure. | ||
//! | ||
//! ## Implementations | ||
//! - [`Add`] for adding two multivariate polynomials. | ||
//! - [`AddAssign`] for adding two multivariate polynomials in place. | ||
//! - [`Sum`] for summing a collection of multivariate polynomials. | ||
//! - [`Sub`] for subtracting two multivariate polynomials. | ||
//! - [`SubAssign`] for subtracting two multivariate polynomials in place. | ||
//! - [`Neg`] for negating a multivariate polynomial. | ||
//! - [`Mul`] for scalar multiplication of a multivariate polynomial. | ||
//! - [`MulAssign`] for scalar multiplication of a multivariate polynomial in place. | ||
|
||
use std::{ | ||
iter::Sum, | ||
ops::{Add, AddAssign, Neg, Sub, SubAssign}, | ||
}; | ||
|
||
use super::*; | ||
|
||
impl<F: FiniteField> Add for MultiVarPolynomial<F> { | ||
type Output = Self; | ||
|
||
/// Implements addition of two multivariate polynomials by adding their coefficients. | ||
fn add(self, rhs: Self) -> Self::Output { | ||
assert_eq!(self.degree, rhs.degree, "Polynomials must have the same degree structure"); | ||
|
||
let coefficients = | ||
self.coefficients.iter().zip(rhs.coefficients.iter()).map(|(&a, &b)| a + b).collect(); | ||
|
||
Self { degree: self.degree, coefficients } | ||
} | ||
} | ||
|
||
impl<F: FiniteField> AddAssign for MultiVarPolynomial<F> { | ||
/// Implements in-place addition of two multivariate polynomials by adding their coefficients. | ||
fn add_assign(&mut self, rhs: Self) { | ||
assert_eq!(self.degree, rhs.degree, "Polynomials must have the same degree structure"); | ||
|
||
for (a, b) in self.coefficients.iter_mut().zip(rhs.coefficients.iter()) { | ||
*a += *b; | ||
} | ||
} | ||
} | ||
|
||
impl<F: FiniteField> Sum for MultiVarPolynomial<F> { | ||
/// Implements summing a collection of multivariate polynomials. | ||
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self { | ||
iter.reduce(|x, y| x + y).expect("Cannot sum an empty iterator of MultiVarPolynomials") | ||
} | ||
} | ||
|
||
impl<F: FiniteField> Sub for MultiVarPolynomial<F> { | ||
type Output = Self; | ||
|
||
/// Implements subtraction of two multivariate polynomials by subtracting their coefficients. | ||
fn sub(self, rhs: Self) -> Self::Output { | ||
assert_eq!(self.degree, rhs.degree, "Polynomials must have the same degree structure"); | ||
|
||
let coefficients = | ||
self.coefficients.iter().zip(rhs.coefficients.iter()).map(|(&a, &b)| a - b).collect(); | ||
|
||
Self { degree: self.degree, coefficients } | ||
} | ||
} | ||
|
||
impl<F: FiniteField> SubAssign for MultiVarPolynomial<F> { | ||
/// Implements in-place subtraction of two multivariate polynomials by subtracting their | ||
/// coefficients. | ||
fn sub_assign(&mut self, rhs: Self) { | ||
assert_eq!(self.degree, rhs.degree, "Polynomials must have the same degree structure"); | ||
|
||
for (a, b) in self.coefficients.iter_mut().zip(rhs.coefficients.iter()) { | ||
*a -= *b; | ||
} | ||
} | ||
} | ||
|
||
impl<F: FiniteField> Neg for MultiVarPolynomial<F> { | ||
type Output = Self; | ||
|
||
/// Implements negation of a multivariate polynomial by negating its coefficients. | ||
fn neg(self) -> Self::Output { | ||
Self { | ||
degree: self.degree, | ||
coefficients: self.coefficients.into_iter().map(|c| -c).collect(), | ||
} | ||
} | ||
} | ||
|
||
impl<F: FiniteField> Mul<F> for MultiVarPolynomial<F> { | ||
type Output = Self; | ||
|
||
/// Implements scalar multiplication of a multivariate polynomial. | ||
fn mul(self, rhs: F) -> Self::Output { self.scalar_mul(rhs) } | ||
} | ||
|
||
impl<F: FiniteField> MulAssign<F> for MultiVarPolynomial<F> { | ||
/// Implements in-place scalar multiplication of a multivariate polynomial. | ||
fn mul_assign(&mut self, rhs: F) { | ||
for coeff in &mut self.coefficients { | ||
*coeff *= rhs; | ||
} | ||
} | ||
} |
Oops, something went wrong.