mirror of
https://github.com/arnaucube/hyperplonk.git
synced 2026-01-11 08:31:29 +01:00
fix bug in interpolation (#29)
This commit is contained in:
@@ -281,18 +281,18 @@ fn interpolate_uni_poly<F: PrimeField>(p_i: &[F], eval_at: F) -> Result<F, PolyI
|
|||||||
/// compute the factorial(a) = 1 * 2 * ... * a
|
/// compute the factorial(a) = 1 * 2 * ... * a
|
||||||
#[inline]
|
#[inline]
|
||||||
fn field_factorial<F: PrimeField>(a: usize) -> F {
|
fn field_factorial<F: PrimeField>(a: usize) -> F {
|
||||||
let mut res = 1u64;
|
let mut res = F::one();
|
||||||
for i in 1..=a {
|
for i in 2..=a {
|
||||||
res *= i as u64;
|
res *= F::from(i as u64);
|
||||||
}
|
}
|
||||||
F::from(res)
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
/// compute the factorial(a) = 1 * 2 * ... * a
|
/// compute the factorial(a) = 1 * 2 * ... * a
|
||||||
#[inline]
|
#[inline]
|
||||||
fn u128_factorial(a: usize) -> u128 {
|
fn u128_factorial(a: usize) -> u128 {
|
||||||
let mut res = 1u128;
|
let mut res = 1u128;
|
||||||
for i in 1..=a {
|
for i in 2..=a {
|
||||||
res *= i as u128;
|
res *= i as u128;
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
@@ -302,8 +302,51 @@ fn u128_factorial(a: usize) -> u128 {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn u64_factorial(a: usize) -> u64 {
|
fn u64_factorial(a: usize) -> u64 {
|
||||||
let mut res = 1u64;
|
let mut res = 1u64;
|
||||||
for i in 1..=a {
|
for i in 2..=a {
|
||||||
res *= i as u64;
|
res *= i as u64;
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::interpolate_uni_poly;
|
||||||
|
use crate::errors::PolyIOPErrors;
|
||||||
|
use ark_bls12_381::Fr;
|
||||||
|
use ark_poly::{univariate::DensePolynomial, Polynomial, UVPolynomial};
|
||||||
|
use ark_std::{vec::Vec, UniformRand};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_interpolation() -> Result<(), PolyIOPErrors> {
|
||||||
|
let mut prng = ark_std::test_rng();
|
||||||
|
|
||||||
|
// test a polynomial with 20 known points, i.e., with degree 19
|
||||||
|
let poly = DensePolynomial::<Fr>::rand(20 - 1, &mut prng);
|
||||||
|
let evals = (0..20)
|
||||||
|
.map(|i| poly.evaluate(&Fr::from(i)))
|
||||||
|
.collect::<Vec<Fr>>();
|
||||||
|
let query = Fr::rand(&mut prng);
|
||||||
|
|
||||||
|
assert_eq!(poly.evaluate(&query), interpolate_uni_poly(&evals, query)?);
|
||||||
|
|
||||||
|
// test a polynomial with 33 known points, i.e., with degree 32
|
||||||
|
let poly = DensePolynomial::<Fr>::rand(33 - 1, &mut prng);
|
||||||
|
let evals = (0..33)
|
||||||
|
.map(|i| poly.evaluate(&Fr::from(i)))
|
||||||
|
.collect::<Vec<Fr>>();
|
||||||
|
let query = Fr::rand(&mut prng);
|
||||||
|
|
||||||
|
assert_eq!(poly.evaluate(&query), interpolate_uni_poly(&evals, query)?);
|
||||||
|
|
||||||
|
// test a polynomial with 64 known points, i.e., with degree 63
|
||||||
|
let poly = DensePolynomial::<Fr>::rand(64 - 1, &mut prng);
|
||||||
|
let evals = (0..64)
|
||||||
|
.map(|i| poly.evaluate(&Fr::from(i)))
|
||||||
|
.collect::<Vec<Fr>>();
|
||||||
|
let query = Fr::rand(&mut prng);
|
||||||
|
|
||||||
|
assert_eq!(poly.evaluate(&query), interpolate_uni_poly(&evals, query)?);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user