-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. univariate lagrange polynomial 1. evaluate mpoly 2. Fix univariable lagrange interpolate poly. * TODO 1. The lagrange of mpoly havn't done.
- Loading branch information
1 parent
9da0757
commit e783285
Showing
17 changed files
with
540 additions
and
58 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
42 changes: 42 additions & 0 deletions
42
2_univariate_lagrange_interpolation/benches/lagrange_interpolate.rs
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,42 @@ | ||
#[macro_use] | ||
extern crate criterion; | ||
|
||
use bls12_381::Scalar; | ||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
use ff::{Field, PrimeField}; | ||
use rand_core::OsRng; | ||
use univariate_lagrange_interpolation::polynomial::Polynomial; | ||
|
||
fn bench_lagrange_interpolate(c: &mut Criterion) { | ||
let MIN_K: u32 = std::env::var("DEGREE") | ||
.unwrap_or_else(|_| "16".to_string()) | ||
.parse() | ||
.expect("Cannot parse DEGREE env var as u32"); | ||
|
||
const MAX_K: u32 = 19; | ||
|
||
// values | ||
let max_n = 1 << MAX_K; | ||
let domain: Vec<Scalar> = (0..max_n).map(|i| Scalar::from_u128(i)).collect::<Vec<_>>(); | ||
let values: Vec<Scalar> = (0..max_n) | ||
.map(|_| Scalar::random(OsRng)) | ||
.collect::<Vec<_>>(); | ||
|
||
let mut group = c.benchmark_group("lagrange_interpolate"); | ||
|
||
for k in MIN_K..=MAX_K { | ||
let n: u128 = 1 << k; | ||
|
||
let x = &domain[..n]; | ||
let y = &values[..n]; | ||
|
||
group.bench_function(BenchmarkId::new("k", k), |b| { | ||
b.iter(|| Polynomial::lagrange_interpolate(x.clone(), y.clone())); | ||
}); | ||
} | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, bench_lagrange_interpolate); | ||
criterion_main!(benches); |
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 |
---|---|---|
@@ -1,67 +1,76 @@ | ||
use crate::polynomial::Polynomial; | ||
use bls12_381::Scalar; | ||
use ff::PrimeField; | ||
#![allow(non_snake_case)] | ||
|
||
mod polynomial; | ||
mod utils; | ||
pub mod polynomial; | ||
|
||
/// Encode vector into polynomial. | ||
// TODO: can do a bench for diff impl | ||
// eg: | ||
// 1: https://github.com/Neptune-Crypto/twenty-first/twenty-first/src/shared_math/polynomial.rs#lagrange_interpolate | ||
// 2. halo2's | ||
// 3. arkwork's | ||
// 4. lambda-work's | ||
|
||
#[test] | ||
fn encode() { | ||
let two = Scalar::one().add(&Scalar::one()); | ||
#[cfg(test)] | ||
mod test { | ||
use crate::polynomial::Polynomial; | ||
use bls12_381::Scalar; | ||
use ff::PrimeField; | ||
use std::ops::Sub; | ||
use std::time::Instant; | ||
|
||
// p = 1 + 2x + x^2 | ||
let a = vec![Scalar::one(), two, Scalar::one()]; | ||
// Encode vector into polynomial. | ||
#[test] | ||
fn encode() { | ||
let two = Scalar::one().add(&Scalar::one()); | ||
|
||
let poly = Polynomial::encode(a); | ||
// p = 1 + 2x + x^2 | ||
let a = vec![Scalar::one(), two, Scalar::one()]; | ||
|
||
let z = poly.evaluate(Scalar::one()); | ||
let poly = Polynomial::encode(a); | ||
|
||
assert_eq!(Scalar::from_u128(4), z); | ||
let z = poly.evaluate(Scalar::one()); | ||
|
||
let z = poly.evaluate(two.double()); | ||
assert_eq!(Scalar::from_u128(25), z); | ||
assert_eq!(Scalar::from_u128(4), z); | ||
|
||
for i in 1..10 { | ||
println!("{:?}", poly.evaluate(Scalar::from_u128(i))); | ||
} | ||
} | ||
let z = poly.evaluate(two.double()); | ||
assert_eq!(Scalar::from_u128(25), z); | ||
|
||
#[test] | ||
fn lagrange_interpolate() { | ||
// aim: p = 1 + 2x + x^2 | ||
for i in 1..10 { | ||
println!("{:?}", poly.evaluate(Scalar::from_u128(i))); | ||
} | ||
} | ||
|
||
let domain = vec![ | ||
Scalar::from_u128(1), | ||
Scalar::from_u128(2), | ||
Scalar::from_u128(3), | ||
Scalar::from_u128(4), | ||
Scalar::from_u128(5), | ||
Scalar::from_u128(6), | ||
Scalar::from_u128(7), | ||
Scalar::from_u128(8), | ||
Scalar::from_u128(9), | ||
]; | ||
let evals = vec![ | ||
Scalar::from_u128(4), | ||
Scalar::from_u128(9), | ||
Scalar::from_u128(10), | ||
Scalar::from_u128(19), | ||
Scalar::from_u128(24), | ||
Scalar::from_u128(31), | ||
Scalar::from_u128(40), | ||
Scalar::from_u128(51), | ||
Scalar::from_u128(64), | ||
]; | ||
#[test] | ||
fn lagrange_interpolate() { | ||
// aim: p = 1 + 2x + x^2 | ||
|
||
let poly = Polynomial::lagrange_interpolate(domain.clone(), evals.clone()); | ||
let domain = vec![ | ||
Scalar::from_u128(1), | ||
Scalar::from_u128(2), | ||
Scalar::from_u128(3), | ||
Scalar::from_u128(4), | ||
Scalar::from_u128(5), | ||
Scalar::from_u128(6), | ||
Scalar::from_u128(7), | ||
Scalar::from_u128(8), | ||
Scalar::from_u128(9), | ||
]; | ||
let evals = vec![ | ||
Scalar::from_u128(4), | ||
Scalar::from_u128(9), | ||
Scalar::from_u128(10), | ||
Scalar::from_u128(19), | ||
Scalar::from_u128(24), | ||
Scalar::from_u128(31), | ||
Scalar::from_u128(40), | ||
Scalar::from_u128(51), | ||
Scalar::from_u128(64), | ||
]; | ||
|
||
let z = poly.evaluate(Scalar::from_u128(3)); | ||
println!("{:?}", z); | ||
let poly = Polynomial::lagrange_interpolate(domain.clone(), evals.clone()); | ||
|
||
// todo meet errors | ||
for (x, y) in domain.iter().zip(evals) { | ||
assert_eq!(poly.evaluate(*x), y); | ||
for (x, y) in domain.iter().zip(evals) { | ||
assert_eq!(poly.evaluate(*x), y); | ||
} | ||
println!("pass"); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,16 @@ | ||
[package] | ||
name = "multilinear_lagrange_interpolation" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
ff = "0.13.0" | ||
bls12_381 = "0.8.0" | ||
rand = "0.8.5" | ||
rand_core = { version = "0.6.4", default-features = false, features = ["std"] } | ||
rayon = "1.7.0" | ||
|
||
[dev-dependencies] | ||
criterion = "0.3" |
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,4 @@ | ||
Multivariable Polynomial Reference | ||
1. https://github.com/int-e/twenty-first/blob/cbda032f2c7b3ba44aa5582a1057c6b8b32e4ab6/twenty-first/src/shared_math/mpolynomial.rs | ||
2. https://github.com/benruijl/symbolica/blob/cfb96196dd6f8dcae813157f9f42ad9eaa64a4ab/src/poly/polynomial.rs | ||
3. https://github.com/arkworks-rs/algebra/blob/master/poly/src/polynomial/multivariate/sparse.rs |
Oops, something went wrong.