Skip to content
Merged
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
20 changes: 9 additions & 11 deletions src/hypercube/core.rs → src/hypercube/hypercube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<O: OrderStrategy> Iterator for Hypercube<O> {
mod tests {
use crate::{
hypercube::{Hypercube, HypercubeMember},
order_strategy::{GraycodeOrder, LexicographicOrder},
order_strategy::{AscendingOrder, GraycodeOrder},
};

fn is_eq(given: HypercubeMember, expected: Vec<bool>) {
Expand All @@ -56,22 +56,22 @@ mod tests {
#[test]
fn lexicographic_hypercube_members() {
// for n=0, should return empty vec first call, none second call
let mut hypercube_size_0 = Hypercube::<LexicographicOrder>::new(0);
let mut hypercube_size_0 = Hypercube::<AscendingOrder>::new(0);
is_eq(hypercube_size_0.next().unwrap().1, vec![]);
// for n=1, should return vec[false] first call, vec[true] second call and None third call
let mut hypercube_size_1: Hypercube<LexicographicOrder> = Hypercube::new(1);
let mut hypercube_size_1: Hypercube<AscendingOrder> = Hypercube::new(1);
is_eq(hypercube_size_1.next().unwrap().1, vec![false]);
is_eq(hypercube_size_1.next().unwrap().1, vec![true]);
assert_eq!(hypercube_size_1.next(), None);
// so on for n=2
let mut hypercube_size_2: Hypercube<LexicographicOrder> = Hypercube::new(2);
let mut hypercube_size_2: Hypercube<AscendingOrder> = Hypercube::new(2);
is_eq(hypercube_size_2.next().unwrap().1, vec![false, false]);
is_eq(hypercube_size_2.next().unwrap().1, vec![false, true]);
is_eq(hypercube_size_2.next().unwrap().1, vec![true, false]);
is_eq(hypercube_size_2.next().unwrap().1, vec![true, true]);
assert_eq!(hypercube_size_2.next(), None);
// so on for n=3
let mut hypercube_size_3: Hypercube<LexicographicOrder> = Hypercube::new(3);
let mut hypercube_size_3: Hypercube<AscendingOrder> = Hypercube::new(3);
is_eq(
hypercube_size_3.next().unwrap().1,
vec![false, false, false],
Expand All @@ -89,22 +89,22 @@ mod tests {
#[test]
fn lexicographic_indices() {
// for n=0, should return empty vec first call, none second call
let mut hypercube_size_0 = Hypercube::<LexicographicOrder>::new(0);
let mut hypercube_size_0 = Hypercube::<AscendingOrder>::new(0);
assert_eq!(hypercube_size_0.next().unwrap().0, 0);
// for n=1, should return vec[false] first call, vec[true] second call and None third call
let mut hypercube_size_1: Hypercube<LexicographicOrder> = Hypercube::new(1);
let mut hypercube_size_1: Hypercube<AscendingOrder> = Hypercube::new(1);
assert_eq!(hypercube_size_1.next().unwrap().0, 0);
assert_eq!(hypercube_size_1.next().unwrap().0, 1);
assert_eq!(hypercube_size_1.next(), None);
// so on for n=2
let mut hypercube_size_2: Hypercube<LexicographicOrder> = Hypercube::new(2);
let mut hypercube_size_2: Hypercube<AscendingOrder> = Hypercube::new(2);
assert_eq!(hypercube_size_2.next().unwrap().0, 0);
assert_eq!(hypercube_size_2.next().unwrap().0, 1);
assert_eq!(hypercube_size_2.next().unwrap().0, 2);
assert_eq!(hypercube_size_2.next().unwrap().0, 3);
assert_eq!(hypercube_size_2.next(), None);
// so on for n=3
let mut hypercube_size_3: Hypercube<LexicographicOrder> = Hypercube::new(3);
let mut hypercube_size_3: Hypercube<AscendingOrder> = Hypercube::new(3);
assert_eq!(hypercube_size_3.next().unwrap().0, 0);
assert_eq!(hypercube_size_3.next().unwrap().0, 1);
assert_eq!(hypercube_size_3.next().unwrap().0, 2);
Expand All @@ -118,7 +118,6 @@ mod tests {

#[test]
fn graycode_hypercube_members() {
// https://docs.rs/gray-codes/latest/gray_codes/struct.GrayCode8.html#examples
// for n=0, should return empty vec first call, none second call
let mut hypercube_size_0 = Hypercube::<GraycodeOrder>::new(0);
is_eq(hypercube_size_0.next().unwrap().1, vec![]);
Expand Down Expand Up @@ -152,7 +151,6 @@ mod tests {

#[test]
fn graycode_indices() {
// https://docs.rs/gray-codes/latest/gray_codes/struct.GrayCode8.html#examples
// for n=0, should return empty vec first call, none second call
let mut hypercube_size_0 = Hypercube::<GraycodeOrder>::new(0);
assert_eq!(hypercube_size_0.next().unwrap().0, 0);
Expand Down
5 changes: 3 additions & 2 deletions src/hypercube/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod core;
#[allow(clippy::module_inception)]
mod hypercube;
mod hypercube_member;

pub use core::Hypercube;
pub use hypercube::Hypercube;
pub use hypercube_member::HypercubeMember;
22 changes: 8 additions & 14 deletions src/interpolation/lagrange_polynomial.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
hypercube::{Hypercube, HypercubeMember},
messages::VerifierMessages,
order_strategy::{GraycodeOrder, OrderStrategy, SignificantBitOrder},
order_strategy::{GraycodeOrder, MSBOrder, OrderStrategy},
};
use ark_ff::Field;

Expand Down Expand Up @@ -116,7 +116,7 @@ impl<'a, F: Field> Iterator for LagrangePolynomial<'a, F, GraycodeOrder> {
}
}

impl<'a, F: Field> Iterator for LagrangePolynomial<'a, F, SignificantBitOrder> {
impl<'a, F: Field> Iterator for LagrangePolynomial<'a, F, MSBOrder> {
type Item = F;
fn next(&mut self) -> Option<Self::Item> {
// Step 1: check if finished iterating
Expand All @@ -130,19 +130,15 @@ impl<'a, F: Field> Iterator for LagrangePolynomial<'a, F, SignificantBitOrder> {
!= self.verifier_messages.zero_ones_mask
{
// NOTICE! we do not update last_position in this case
self.position = SignificantBitOrder::next_value_in_msb_order(
self.position,
self.order.num_vars() as u32,
);
self.position =
MSBOrder::next_value_in_msb_order(self.position, self.order.num_vars() as u32);
return Some(F::ZERO);
}
// Step 3: check if position is 0, which is a special case
// Notice! step 2 could apply when position == 0
if self.position == 0 {
self.position = SignificantBitOrder::next_value_in_msb_order(
self.position,
self.order.num_vars() as u32,
);
self.position =
MSBOrder::next_value_in_msb_order(self.position, self.order.num_vars() as u32);
return Some(self.value);
}
// Step 3: update the value
Expand All @@ -158,10 +154,8 @@ impl<'a, F: Field> Iterator for LagrangePolynomial<'a, F, SignificantBitOrder> {

// Step 5: increment positions
self.last_position = self.position;
self.position = SignificantBitOrder::next_value_in_msb_order(
self.position,
self.order.num_vars() as u32,
);
self.position =
MSBOrder::next_value_in_msb_order(self.position, self.order.num_vars() as u32);

// Step 6: return
Some(self.value)
Expand Down
42 changes: 21 additions & 21 deletions src/multilinear_product/provers/blendy/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
interpolation::LagrangePolynomial,
messages::VerifierMessages,
multilinear_product::TimeProductProver,
order_strategy::{GraycodeOrder, SignificantBitOrder},
order_strategy::{GraycodeOrder, MSBOrder},
streams::{Stream, StreamIterator},
};
use ark_ff::Field;
Expand All @@ -13,7 +13,7 @@ use std::collections::BTreeSet;
pub struct BlendyProductProver<F: Field, S: Stream<F>> {
pub current_round: usize,
pub streams: Vec<S>,
pub stream_iterators: Vec<StreamIterator<F, S, SignificantBitOrder>>,
pub stream_iterators: Vec<StreamIterator<F, S, MSBOrder>>,
pub num_stages: usize,
pub num_variables: usize,
pub last_round_phase1: usize,
Expand Down Expand Up @@ -69,9 +69,9 @@ impl<F: Field, S: Stream<F>> BlendyProductProver<F, S> {
// if first few rounds, then no table is computed, need to compute sums from the streams
else if self.current_round < self.last_round_phase1 {
// Lag Poly
let mut sequential_lag_poly: LagrangePolynomial<F, SignificantBitOrder> =
let mut sequential_lag_poly: LagrangePolynomial<F, MSBOrder> =
LagrangePolynomial::new(&self.verifier_messages_round_comp);
let lag_polys_len = Hypercube::<SignificantBitOrder>::stop_value(self.current_round);
let lag_polys_len = Hypercube::<MSBOrder>::stop_value(self.current_round);
let mut lag_polys: Vec<F> = vec![F::ONE; lag_polys_len];

// reset the streams
Expand All @@ -80,7 +80,7 @@ impl<F: Field, S: Stream<F>> BlendyProductProver<F, S> {
.for_each(|stream_it| stream_it.reset());

for (x_index, _) in
Hypercube::<SignificantBitOrder>::new(self.num_variables - self.current_round - 1)
Hypercube::<MSBOrder>::new(self.num_variables - self.current_round - 1)
{
// can avoid unnecessary additions for first round since there is no lag poly: gives a small speedup
if self.is_initial_round() {
Expand All @@ -96,15 +96,15 @@ impl<F: Field, S: Stream<F>> BlendyProductProver<F, S> {
let mut partial_sum_p_1 = F::ZERO;
let mut partial_sum_q_0 = F::ZERO;
let mut partial_sum_q_1 = F::ZERO;
for (b_index, _) in Hypercube::<SignificantBitOrder>::new(self.current_round) {
for (b_index, _) in Hypercube::<MSBOrder>::new(self.current_round) {
if x_index == 0 {
lag_polys[b_index] = sequential_lag_poly.next().unwrap();
}
let lag_poly = lag_polys[b_index];
partial_sum_p_0 += self.stream_iterators[0].next().unwrap() * lag_poly;
partial_sum_q_0 += self.stream_iterators[1].next().unwrap() * lag_poly;
}
for (b_index, _) in Hypercube::<SignificantBitOrder>::new(self.current_round) {
for (b_index, _) in Hypercube::<MSBOrder>::new(self.current_round) {
let lag_poly = lag_polys[b_index];
partial_sum_p_1 += self.stream_iterators[0].next().unwrap() * lag_poly;
partial_sum_q_1 += self.stream_iterators[1].next().unwrap() * lag_poly;
Expand Down Expand Up @@ -179,7 +179,7 @@ impl<F: Field, S: Stream<F>> BlendyProductProver<F, S> {
let t = self.prev_table_size;

// zero out the table
let table_len = Hypercube::<SignificantBitOrder>::stop_value(t);
let table_len = Hypercube::<MSBOrder>::stop_value(t);
self.j_prime_table = vec![vec![F::ZERO; table_len]; table_len];

// basically, this needs to get "zeroed" out at the beginning of state computation
Expand All @@ -194,14 +194,14 @@ impl<F: Field, S: Stream<F>> BlendyProductProver<F, S> {
let x_num_vars = j_prime - 1;

// Lag Poly
let mut sequential_lag_poly: LagrangePolynomial<F, SignificantBitOrder> =
let mut sequential_lag_poly: LagrangePolynomial<F, MSBOrder> =
LagrangePolynomial::new(&self.verifier_messages);

assert!(x_num_vars == self.verifier_messages.messages.len());
let lag_polys_len = Hypercube::<SignificantBitOrder>::stop_value(x_num_vars);
let lag_polys_len = Hypercube::<MSBOrder>::stop_value(x_num_vars);
let mut lag_polys: Vec<F> = vec![F::ONE; lag_polys_len];

for (x_index, _) in Hypercube::<SignificantBitOrder>::new(x_num_vars) {
for (x_index, _) in Hypercube::<MSBOrder>::new(x_num_vars) {
lag_polys[x_index] = sequential_lag_poly.next().unwrap();
}

Expand All @@ -211,23 +211,23 @@ impl<F: Field, S: Stream<F>> BlendyProductProver<F, S> {
.for_each(|stream_it| stream_it.reset());

// Ensure x_table and y_table are initialized with the correct size
self.x_table = vec![F::ZERO; Hypercube::<SignificantBitOrder>::stop_value(t)];
self.y_table = vec![F::ZERO; Hypercube::<SignificantBitOrder>::stop_value(t)];
self.x_table = vec![F::ZERO; Hypercube::<MSBOrder>::stop_value(t)];
self.y_table = vec![F::ZERO; Hypercube::<MSBOrder>::stop_value(t)];

for (_, _) in Hypercube::<SignificantBitOrder>::new(b_num_vars) {
for (b_prime_index, _) in Hypercube::<SignificantBitOrder>::new(t) {
for (_, _) in Hypercube::<MSBOrder>::new(b_num_vars) {
for (b_prime_index, _) in Hypercube::<MSBOrder>::new(t) {
self.x_table[b_prime_index] = F::ZERO;
self.y_table[b_prime_index] = F::ZERO;

for (x_index, _) in Hypercube::<SignificantBitOrder>::new(x_num_vars) {
for (x_index, _) in Hypercube::<MSBOrder>::new(x_num_vars) {
self.x_table[b_prime_index] +=
lag_polys[x_index] * self.stream_iterators[0].next().unwrap();
self.y_table[b_prime_index] +=
lag_polys[x_index] * self.stream_iterators[1].next().unwrap();
}
}
for (b_prime_index, _) in Hypercube::<SignificantBitOrder>::new(t) {
for (b_prime_prime_index, _) in Hypercube::<SignificantBitOrder>::new(t) {
for (b_prime_index, _) in Hypercube::<MSBOrder>::new(t) {
for (b_prime_prime_index, _) in Hypercube::<MSBOrder>::new(t) {
self.j_prime_table[b_prime_index][b_prime_prime_index] +=
self.x_table[b_prime_index] * self.y_table[b_prime_prime_index];
}
Expand All @@ -247,10 +247,10 @@ impl<F: Field, S: Stream<F>> BlendyProductProver<F, S> {
let mut evaluations_p = vec![F::ZERO; 1 << num_variables_new];
let mut evaluations_q = vec![F::ZERO; 1 << num_variables_new];

for (b_prime_index, _) in Hypercube::<SignificantBitOrder>::new(num_variables_new) {
let mut sequential_lag_poly: LagrangePolynomial<F, SignificantBitOrder> =
for (b_prime_index, _) in Hypercube::<MSBOrder>::new(num_variables_new) {
let mut sequential_lag_poly: LagrangePolynomial<F, MSBOrder> =
LagrangePolynomial::new(&self.verifier_messages);
for (_, _) in Hypercube::<SignificantBitOrder>::new(j - 1) {
for (_, _) in Hypercube::<MSBOrder>::new(j - 1) {
let lag_poly = sequential_lag_poly.next().unwrap();
evaluations_p[b_prime_index] +=
lag_poly * self.stream_iterators[0].next().unwrap();
Expand Down
8 changes: 4 additions & 4 deletions src/multilinear_product/provers/blendy/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::BTreeSet;
use crate::{
messages::VerifierMessages,
multilinear_product::{BlendyProductProver, BlendyProductProverConfig, TimeProductProver},
order_strategy::SignificantBitOrder,
order_strategy::MSBOrder,
prover::Prover,
streams::{Stream, StreamIterator},
};
Expand Down Expand Up @@ -49,7 +49,7 @@ impl<F: Field, S: Stream<F>> Prover<F> for BlendyProductProver<F, S> {
.streams
.iter()
.cloned()
.map(|s| StreamIterator::<F, S, SignificantBitOrder>::new(s))
.map(|s| StreamIterator::<F, S, MSBOrder>::new(s))
.collect();

// return the BlendyProver instance
Expand Down Expand Up @@ -112,7 +112,7 @@ mod tests {

use crate::{
multilinear_product::{BlendyProductProver, BlendyProductProverConfig},
order_strategy::SignificantBitOrder,
order_strategy::MSBOrder,
prover::{ProductProverConfig, Prover},
streams::{multivariate_product_claim, MemoryStream, Stream},
tests::{
Expand Down Expand Up @@ -140,7 +140,7 @@ mod tests {
}

// create the stream in SigBit order
let s: MemoryStream<F64> = MemoryStream::new_from_lex::<SignificantBitOrder>(evals.clone());
let s: MemoryStream<F64> = MemoryStream::new_from_lex::<MSBOrder>(evals.clone());
let claim: F64 = multivariate_product_claim(vec![s.clone(), s.clone()]);

// get transcript from Blendy prover
Expand Down
16 changes: 7 additions & 9 deletions src/multilinear_product/provers/space/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use crate::{
hypercube::Hypercube,
interpolation::LagrangePolynomial,
messages::VerifierMessages,
order_strategy::SignificantBitOrder,
order_strategy::MSBOrder,
streams::{Stream, StreamIterator},
};

pub struct SpaceProductProver<F: Field, S: Stream<F>> {
pub current_round: usize,
pub stream_iterators: Vec<StreamIterator<F, S, SignificantBitOrder>>,
pub stream_iterators: Vec<StreamIterator<F, S, MSBOrder>>,
pub num_variables: usize,
pub verifier_messages: VerifierMessages<F>,
pub inverse_four: F,
Expand All @@ -27,9 +27,7 @@ impl<F: Field, S: Stream<F>> SpaceProductProver<F, S> {
.iter_mut()
.for_each(|stream_it| stream_it.reset());

for (_, _) in
Hypercube::<SignificantBitOrder>::new(self.num_variables - self.current_round - 1)
{
for (_, _) in Hypercube::<MSBOrder>::new(self.num_variables - self.current_round - 1) {
// can avoid unnecessary additions for first round since there is no lag poly: gives a small speedup
if self.current_round == 0 {
let p0 = self.stream_iterators[0].next().unwrap();
Expand All @@ -45,17 +43,17 @@ impl<F: Field, S: Stream<F>> SpaceProductProver<F, S> {
let mut partial_sum_q_0 = F::ZERO;
let mut partial_sum_q_1 = F::ZERO;

let mut sequential_lag_poly: LagrangePolynomial<F, SignificantBitOrder> =
let mut sequential_lag_poly: LagrangePolynomial<F, MSBOrder> =
LagrangePolynomial::new(&self.verifier_messages);
for (_, _) in Hypercube::<SignificantBitOrder>::new(self.current_round) {
for (_, _) in Hypercube::<MSBOrder>::new(self.current_round) {
let lag_poly = sequential_lag_poly.next().unwrap();
partial_sum_p_0 += self.stream_iterators[0].next().unwrap() * lag_poly;
partial_sum_q_0 += self.stream_iterators[1].next().unwrap() * lag_poly;
}

let mut sequential_lag_poly: LagrangePolynomial<F, SignificantBitOrder> =
let mut sequential_lag_poly: LagrangePolynomial<F, MSBOrder> =
LagrangePolynomial::new(&self.verifier_messages);
for (_, _) in Hypercube::<SignificantBitOrder>::new(self.current_round) {
for (_, _) in Hypercube::<MSBOrder>::new(self.current_round) {
let lag_poly = sequential_lag_poly.next().unwrap();
partial_sum_p_1 += self.stream_iterators[0].next().unwrap() * lag_poly;
partial_sum_q_1 += self.stream_iterators[1].next().unwrap() * lag_poly;
Expand Down
4 changes: 2 additions & 2 deletions src/multilinear_product/provers/space/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ark_ff::Field;
use crate::{
messages::VerifierMessages,
multilinear_product::{SpaceProductProver, SpaceProductProverConfig},
order_strategy::SignificantBitOrder,
order_strategy::MSBOrder,
prover::Prover,
streams::{Stream, StreamIterator},
};
Expand All @@ -18,7 +18,7 @@ impl<F: Field, S: Stream<F>> Prover<F> for SpaceProductProver<F, S> {
.streams
.iter()
.cloned()
.map(|s| StreamIterator::<F, S, SignificantBitOrder>::new(s))
.map(|s| StreamIterator::<F, S, MSBOrder>::new(s))
.collect();

Self {
Expand Down
Loading
Loading