Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: multilinear polynomial evaluation endianness (Arecibo backport) #281

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 63 additions & 5 deletions src/spartan/polys/multilinear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ mod tests {
use crate::provider::{self, bn256_grumpkin::bn256, secp_secq::secp256k1};

use super::*;
use pasta_curves::Fp;
use rand_chacha::ChaCha20Rng;
use rand_core::{CryptoRng, RngCore, SeedableRng};

fn make_mlp<F: PrimeField>(len: usize, value: F) -> MultilinearPolynomial<F> {
MultilinearPolynomial {
Expand Down Expand Up @@ -235,12 +236,12 @@ mod tests {

#[test]
fn test_multilinear_polynomial() {
test_multilinear_polynomial_with::<Fp>();
test_multilinear_polynomial_with::<pasta_curves::Fp>();
}

#[test]
fn test_sparse_polynomial() {
test_sparse_polynomial_with::<Fp>();
test_sparse_polynomial_with::<pasta_curves::Fp>();
}

fn test_mlp_add_with<F: PrimeField>() {
Expand All @@ -254,7 +255,7 @@ mod tests {

#[test]
fn test_mlp_add() {
test_mlp_add_with::<Fp>();
test_mlp_add_with::<pasta_curves::Fp>();
test_mlp_add_with::<bn256::Scalar>();
test_mlp_add_with::<secp256k1::Scalar>();
}
Expand Down Expand Up @@ -283,8 +284,65 @@ mod tests {

#[test]
fn test_evaluation() {
test_evaluation_with::<Fp>();
test_evaluation_with::<pasta_curves::Fp>();
test_evaluation_with::<provider::bn256_grumpkin::bn256::Scalar>();
test_evaluation_with::<provider::secp_secq::secp256k1::Scalar>();
}

/// Returns a random ML polynomial
fn random<R: RngCore + CryptoRng, Scalar: PrimeField>(
num_vars: usize,
mut rng: &mut R,
) -> MultilinearPolynomial<Scalar> {
MultilinearPolynomial::new(
std::iter::from_fn(|| Some(Scalar::random(&mut rng)))
.take(1 << num_vars)
.collect(),
)
}

/// This binds the variables of a multilinear polynomial to a provided sequence
/// of values.
///
/// Assuming `bind_poly_var_top` defines the "top" variable of the polynomial,
/// this aims to test whether variables should be provided to the
/// `evaluate` function in topmost-first (big endian) of topmost-last (lower endian)
/// order.
fn bind_sequence<F: PrimeField>(
poly: &MultilinearPolynomial<F>,
values: &[F],
) -> MultilinearPolynomial<F> {
// Assert that the size of the polynomial being evaluated is a power of 2 greater than (1 << values.len())
assert!(poly.Z.len().is_power_of_two());
assert!(poly.Z.len() >= 1 << values.len());

let mut tmp = poly.clone();
for v in values.iter() {
tmp.bind_poly_var_top(v);
}
tmp
}

fn bind_and_evaluate_with<F: PrimeField>() {
for i in 0..50 {
// Initialize a random polynomial
let n = 7;
let mut rng = ChaCha20Rng::from_seed([i as u8; 32]);
let poly = random(n, &mut rng);

// draw a random point
let pt: Vec<_> = std::iter::from_fn(|| Some(F::random(&mut rng)))
.take(n)
.collect();
// this shows the order in which coordinates are evaluated
assert_eq!(poly.evaluate(&pt), bind_sequence(&poly, &pt).Z[0])
}
}

#[test]
fn test_bind_and_evaluate() {
bind_and_evaluate_with::<pasta_curves::Fp>();
bind_and_evaluate_with::<bn256::Scalar>();
bind_and_evaluate_with::<secp256k1::Scalar>();
}
}
Loading