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

Revert "Product implementation" #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
23 changes: 3 additions & 20 deletions src/polynomial.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::collections::HashMap;
use ark_ff::Field;
use std::ops::Mul;
use ark_poly::{multivariate::{SparsePolynomial, SparseTerm, Term}, DenseMVPolynomial, Polynomial};

use ark_std::{UniformRand};
Expand All @@ -10,24 +8,7 @@ use crate::field::Field64 as F;

pub type MLPolynomial = SparsePolynomial<F, SparseTerm>;

pub type PolynomialDescription = Vec<F>;

pub type MVMLPolynomial = Vec<MLPolynomial>;


pub fn evaluate_mvml_polynomial(mvml_polynomial: MVMLPolynomial, point: &Vec<F>) -> F {
mvml_polynomial.iter().map(|ml_polynomial|ml_polynomial.evaluate(&point)).fold(F::ONE, F::mul)
}

pub fn get_num_vars(multilinears: &MVMLPolynomial) -> Option<usize> {
match multilinears.as_slice() {
[head, tail @ ..] => tail
.iter()
.all(|x| x.num_vars == head.num_vars)
.then(|| head.num_vars),
[] => None,
}
}
pub type LinearDescription = (F,F);

pub fn evaluate_polynomial_on_hypercube(p: &MLPolynomial) -> HashMap<String, F> {
let num_vars = p.num_vars();
Expand Down Expand Up @@ -87,6 +68,7 @@ mod tests {
poly1.evaluate(&random_point),
poly2.evaluate(&random_point)
);

}

#[test]
Expand Down Expand Up @@ -155,4 +137,5 @@ mod tests {
assert_eq!(some_point, bit_string_to_vector(binary_point));
assert_eq!(*value_from_map, value_from_poly)
}

}
69 changes: 18 additions & 51 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ark_poly::{multivariate::{SparsePolynomial, SparseTerm, Term}, DenseMVPolynomial};
use crate::field::Field64 as F;
use crate::polynomial::{get_num_vars, PolynomialDescription, MVMLPolynomial};
use crate::polynomial::{LinearDescription, MLPolynomial};
use crate::protocol::prover::{Prover, ProverState};
use crate::protocol::verifier::{Verifier, VerifierState};

Expand All @@ -14,8 +14,8 @@ struct ProtocolTranscript {
accept: bool,
}

fn setup_protocol(poly: &MVMLPolynomial) -> (usize, F, ProverState, VerifierState) {
let num_vars = get_num_vars(&poly).unwrap();
fn setup_protocol(poly: &MLPolynomial) -> (usize, F, ProverState, VerifierState) {
let num_vars = poly.num_vars;
let (claimed_sum, prover_state) = Prover::claim_sum(&poly);
let verifier_state = Verifier::initialize(&poly, claimed_sum);
(num_vars, claimed_sum, prover_state, verifier_state)
Expand All @@ -26,7 +26,7 @@ fn orchestrate_protocol(num_vars: usize,
mut prover_state: ProverState,
mut verifier_state: VerifierState)
-> ProtocolTranscript {
let mut poly_descr: PolynomialDescription;
let mut poly_descr: LinearDescription;
for _ in 0..num_vars
{
(poly_descr, prover_state) = Prover::round_phase_1(prover_state);
Expand All @@ -51,15 +51,15 @@ mod tests {
#[test]
fn test_protocol_3_variables() {

let poly = Vec::from(&[SparsePolynomial::from_coefficients_vec(
let poly = SparsePolynomial::from_coefficients_vec(
3,
vec![
(F::from(2), SparseTerm::new(vec![(0, 1)])),
(F::from(7), SparseTerm::new(vec![(0, 1), (2, 1)])),
(F::from(1), SparseTerm::new(vec![(1, 1), (2, 1)])),
(F::from(5), SparseTerm::new(vec![])),
],
)]);
);
let (num_vars, claimed_sum, prover_state, verifier_state) = setup_protocol(&poly);
let transcript = orchestrate_protocol(num_vars, claimed_sum, prover_state, verifier_state);
assert!(transcript.accept);
Expand All @@ -69,47 +69,47 @@ mod tests {
fn test_fail_3_variables() {
// We create a polynomial of degree 2. the verifier will accept all the intermediate rounds,
// except the last check.
let poly = Vec::from(&[SparsePolynomial::from_coefficients_vec(
let poly = SparsePolynomial::from_coefficients_vec(
3,
vec![
(F::from(2), SparseTerm::new(vec![(0, 1)])),
(F::from(7), SparseTerm::new(vec![(0, 2), (2, 1)])),
(F::from(1), SparseTerm::new(vec![(1, 1), (2, 1)])),
(F::from(5), SparseTerm::new(vec![])),
],
)]);
);
let (num_vars, claimed_sum, prover_state, verifier_state) = setup_protocol(&poly);
let transcript = orchestrate_protocol(num_vars, claimed_sum, prover_state, verifier_state);
assert!(!transcript.accept);
}

#[test]
fn test_protocol_6_variables() {
let poly = Vec::from(&[SparsePolynomial::from_coefficients_vec(
let poly = SparsePolynomial::from_coefficients_vec(
6,
vec![
(F::from(1), SparseTerm::new(vec![(0, 1), (4,1), (3, 1)])),
(F::from(83), SparseTerm::new(vec![(0, 1), (3,1), (2, 1)])),
(F::from(62), SparseTerm::new(vec![(0, 1), (5,1), (3, 1)])),
(F::from(84), SparseTerm::new(vec![(2, 1), (4,1), (3, 1)])),
],
)]);
);
let (num_vars, claimed_sum, prover_state, verifier_state) = setup_protocol(&poly);
let transcript = orchestrate_protocol(num_vars, claimed_sum, prover_state, verifier_state);
assert!(transcript.accept);
}

#[test]
fn test_fail_6_variables() {
let poly = Vec::from(&[SparsePolynomial::from_coefficients_vec(
let poly = SparsePolynomial::from_coefficients_vec(
6,
vec![
(F::from(1), SparseTerm::new(vec![(0, 1), (4,1), (3, 1)])),
(F::from(83), SparseTerm::new(vec![(0, 1), (3,1), (2, 1)])),
(F::from(62), SparseTerm::new(vec![(0, 1), (5,1), (3, 4)])),
(F::from(84), SparseTerm::new(vec![(2, 1), (4,1), (3, 1)])),
],
)]);
);
let (num_vars, claimed_sum, prover_state, verifier_state) = setup_protocol(&poly);
let transcript = orchestrate_protocol(num_vars, claimed_sum, prover_state, verifier_state);
assert!(!transcript.accept);
Expand All @@ -119,29 +119,29 @@ mod tests {

#[test]
fn test_protocol_12_variables() {
let poly = Vec::from(&[SparsePolynomial::from_coefficients_vec(
let poly = SparsePolynomial::from_coefficients_vec(
12,
vec![
(F::from(1), SparseTerm::new(vec![(0, 1), (4,1), (3, 1)])),
(F::from(83), SparseTerm::new(vec![(0, 1), (3,1), (2, 1)])),
(F::from(62), SparseTerm::new(vec![(0, 1), (5,1), (3, 1)])),
(F::from(84), SparseTerm::new(vec![(2, 1), (4,1), (3, 1)])),
],
)]);
);
let (num_vars, claimed_sum, prover_state, verifier_state) = setup_protocol(&poly);
let transcript = orchestrate_protocol(num_vars, claimed_sum, prover_state, verifier_state);
assert!(transcript.accept);
}

#[test]
fn test_protocol_univariate() {
let poly = Vec::from(&[SparsePolynomial::from_coefficients_vec(
let poly = SparsePolynomial::from_coefficients_vec(
1,
vec![
(F::from(2), SparseTerm::new(vec![(0, 1)])),
(F::from(5), SparseTerm::new(vec![])),
],
)]);
);
let (num_vars, claimed_sum, prover_state, verifier_state) = setup_protocol(&poly);
let transcript = orchestrate_protocol(num_vars, claimed_sum, prover_state, verifier_state);
assert!(transcript.accept);
Expand All @@ -151,13 +151,13 @@ mod tests {

#[test]
fn test_fail_intermediate_check() {
let poly = Vec::from(&[SparsePolynomial::from_coefficients_vec(
let poly = SparsePolynomial::from_coefficients_vec(
1,
vec![
(F::from(2), SparseTerm::new(vec![(0, 1)])),
(F::from(5), SparseTerm::new(vec![])),
],
)]);
);
let (num_vars, claimed_sum, prover_state, verifier_state) = setup_protocol(&poly);
let alt_verifier_state = VerifierState{
running_eval: F::from(0),
Expand All @@ -167,39 +167,6 @@ mod tests {
assert!(!transcript.accept);
assert_eq!(transcript.randomness.len(), 0)
}
#[test]
fn test_product_check() {
let p1 = SparsePolynomial::from_coefficients_vec(
3,
Vec::from([
(F::from(1), SparseTerm::new(vec![(0, 1)])),
(F::from(1), SparseTerm::new(vec![(1, 1)])),
(F::from(1), SparseTerm::new(vec![(2, 1)])),
])
);
let p2 = SparsePolynomial::from_coefficients_vec(
3,
Vec::from([
(F::from(1), SparseTerm::new(vec![(0, 1)])),
(F::from(1), SparseTerm::new(vec![(1, 1)])),
(F::from(1), SparseTerm::new(vec![(2, 1)])),
])
);
let p3 = SparsePolynomial::from_coefficients_vec(
3,
Vec::from([
(F::from(1), SparseTerm::new(vec![(0, 1)])),
(F::from(1), SparseTerm::new(vec![(1, 1)])),
(F::from(1), SparseTerm::new(vec![(2, 1)])),
])
);
let multilinear_list = vec![
p1, p2, p3
];
let (num_vars, claimed_sum, prover_state, verifier_state) = setup_protocol(&multilinear_list);
let transcript = orchestrate_protocol(num_vars, claimed_sum, prover_state, verifier_state);
assert!(transcript.accept);
}


}
Loading