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

refactor: make dynamic dory helper code generic #424

Merged
merged 6 commits into from
Dec 12, 2024
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::{
dynamic_dory_structure::{
full_width_of_row, index_from_row_and_column, matrix_size, row_and_column_from_index,
},
G1Affine, F,
};
use super::{G1Affine, F};
use crate::{
base::{commitment::CommittableColumn, database::ColumnType, if_rayon},
proof_primitive::dory::offset_to_bytes::OffsetToBytes,
proof_primitive::{
dory::offset_to_bytes::OffsetToBytes,
dynamic_matrix_utils::matrix_structure::{
full_width_of_row, index_from_row_and_column, matrix_size, row_and_column_from_index,
},
},
};
use alloc::{vec, vec::Vec};
use ark_ec::CurveGroup;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::{
dynamic_dory_helper::{compute_dynamic_v_vec, compute_dynamic_vecs},
DeferredGT, G1Affine, VMVProverState, VMVVerifierState, F,
dynamic_dory_helper::compute_dynamic_v_vec, DeferredGT, DoryScalar, G1Affine, VMVProverState,
VMVVerifierState, F,
};
use crate::proof_primitive::dynamic_matrix_utils::standard_basis_helper::compute_dynamic_vecs;
use alloc::vec::Vec;

/// Builds a [`VMVProverState`] from the given parameters.
Expand All @@ -11,13 +12,18 @@ pub(super) fn build_dynamic_vmv_prover_state(
T_vec_prime: Vec<G1Affine>,
nu: usize,
) -> VMVProverState {
let (lo_vec, hi_vec) = compute_dynamic_vecs(b_point);
let v_vec = compute_dynamic_v_vec(a, &hi_vec, nu);
let (lo_vec, hi_vec) =
compute_dynamic_vecs(bytemuck::TransparentWrapper::wrap_slice(b_point) as &[DoryScalar]);
let (lo_vec, hi_vec) = (
bytemuck::TransparentWrapper::peel_slice(&lo_vec) as &[F],
bytemuck::TransparentWrapper::peel_slice(&hi_vec) as &[F],
);
let v_vec = compute_dynamic_v_vec(a, hi_vec, nu);
VMVProverState {
v_vec,
T_vec_prime,
L_vec: hi_vec,
R_vec: lo_vec,
L_vec: hi_vec.to_vec(),
R_vec: lo_vec.to_vec(),
#[cfg(test)]
l_tensor: Vec::with_capacity(0),
#[cfg(test)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use super::{
dynamic_dory_structure::{full_width_of_row, row_and_column_from_index, row_start_index},
pairings, DoryScalar, DynamicDoryCommitment, G1Projective, ProverSetup, GT,
use super::{pairings, DoryScalar, DynamicDoryCommitment, G1Projective, ProverSetup, GT};
use crate::{
base::{commitment::CommittableColumn, if_rayon, slice_ops::slice_cast},
proof_primitive::dynamic_matrix_utils::matrix_structure::{
full_width_of_row, row_and_column_from_index, row_start_index,
},
};
use crate::base::{commitment::CommittableColumn, if_rayon, slice_ops::slice_cast};
use alloc::vec::Vec;
use ark_ec::VariableBaseMSM;
use bytemuck::TransparentWrapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use super::{
blitzar_metadata_table::{create_blitzar_metadata_tables, signed_commits},
dynamic_dory_structure::row_and_column_from_index,
pairings, DynamicDoryCommitment, G1Affine, ProverSetup,
};
use crate::base::{commitment::CommittableColumn, if_rayon, slice_ops::slice_cast};
use crate::{
base::{commitment::CommittableColumn, if_rayon, slice_ops::slice_cast},
proof_primitive::dynamic_matrix_utils::matrix_structure::row_and_column_from_index,
};
use blitzar::compute::ElementP2;
#[cfg(feature = "rayon")]
use rayon::iter::{IntoParallelIterator, ParallelIterator};
Expand Down
149 changes: 28 additions & 121 deletions crates/proof-of-sql/src/proof_primitive/dory/dynamic_dory_helper.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use super::{
blitzar_metadata_table::create_blitzar_metadata_tables,
dynamic_dory_standard_basis_helper::fold_dynamic_standard_basis_tensors,
dynamic_dory_structure::row_and_column_from_index, ExtendedVerifierState, G1Affine,
blitzar_metadata_table::create_blitzar_metadata_tables, ExtendedVerifierState, G1Affine,
ProverSetup, F,
};
use crate::{
base::{commitment::CommittableColumn, slice_ops::slice_cast},
proof_primitive::dory::{
dynamic_dory_standard_basis_helper::compute_dynamic_standard_basis_vecs, DoryScalar,
proof_primitive::{
dory::DoryScalar,
dynamic_matrix_utils::{
matrix_structure::row_and_column_from_index,
standard_basis_helper::fold_dynamic_standard_basis_tensors,
},
},
};
use alloc::{vec, vec::Vec};
Expand Down Expand Up @@ -93,29 +95,6 @@ pub(super) fn compute_dynamic_nu(num_vars: usize) -> usize {
num_vars / 2 + 1
}

/// Compute the hi and lo vectors (or L and R) that are derived from `point`.
/// L and R are the vectors such that LMR is exactly the evaluation of `a` at the point `point`.
/// # Panics
/// This function requires that `point` has length at least as big as the number of rows in `M` that is created by `a`.
pub(super) fn compute_dynamic_vecs(point: &[F]) -> (Vec<F>, Vec<F>) {
let nu = point.len() / 2 + 1;
let mut lo_vec = vec![F::ZERO; 1 << nu];
let mut hi_vec = vec![F::ZERO; 1 << nu];
lo_vec[0] = point.iter().take(nu).map(|b| F::ONE - b).product();
hi_vec[0] = point.iter().skip(nu).map(|b| F::ONE - b).product();
let standard_basis_point = point
.iter()
.map(|b| {
(F::ONE - b)
.inverse()
.expect("Values in point cannot be 1.")
- F::ONE
})
.collect_vec();
compute_dynamic_standard_basis_vecs(&standard_basis_point, &mut lo_vec, &mut hi_vec);
(lo_vec, hi_vec)
}

/// Folds the `s1` and `s2` tensors:
///
/// This is the analogous function of the non-dynamic folding function [`extended_dory_reduce_verify_fold_s_vecs`](super::extended_dory_reduce_helper::extended_dory_reduce_verify_fold_s_vecs).
Expand All @@ -137,101 +116,21 @@ pub(super) fn fold_dynamic_tensors(state: &ExtendedVerifierState) -> (F, F) {
})
.collect_vec();
let (lo_fold, hi_fold) = fold_dynamic_standard_basis_tensors(
&standard_basis_point,
&state.alphas,
&state.alpha_invs,
bytemuck::TransparentWrapper::wrap_slice(&standard_basis_point) as &[DoryScalar],
bytemuck::TransparentWrapper::wrap_slice(&state.alphas) as &[DoryScalar],
bytemuck::TransparentWrapper::wrap_slice(&state.alpha_invs) as &[DoryScalar],
);
(lo_fold * lo_inv_prod, hi_fold * hi_inv_prod)
(lo_fold.0 * lo_inv_prod, hi_fold.0 * hi_inv_prod)
}

#[cfg(test)]
mod tests {
use super::{super::dynamic_dory_standard_basis_helper::tests::naive_fold, *};
use crate::{
base::polynomial::compute_evaluation_vector,
proof_primitive::dory::{
deferred_msm::DeferredMSM, test_rng, PublicParameters, VerifierState,
},
use super::*;
use crate::proof_primitive::{
dory::{deferred_msm::DeferredMSM, test_rng, PublicParameters, VerifierState},
dynamic_matrix_utils::standard_basis_helper::{compute_dynamic_vecs, tests::naive_fold},
};

#[test]
fn we_can_compute_dynamic_vecs_for_length_0_point() {
let point = vec![];
let expected_lo_vec = vec![F::from(1), F::from(0)];
let expected_hi_vec = vec![F::from(1), F::from(1)];
let (lo_vec, hi_vec) = compute_dynamic_vecs(&point);
assert_eq!(expected_lo_vec, lo_vec);
assert_eq!(expected_hi_vec, hi_vec);
}

#[test]
fn we_can_compute_dynamic_vecs_for_length_1_point() {
let point = vec![F::from(2)];
let expected_lo_vec = vec![F::from(1 - 2), F::from(2)];
let expected_hi_vec = vec![F::from(1), F::from(1)];
let (lo_vec, hi_vec) = compute_dynamic_vecs(&point);
assert_eq!(expected_lo_vec, lo_vec);
assert_eq!(expected_hi_vec, hi_vec);
}

#[test]
fn we_can_compute_dynamic_vecs_for_length_2_point() {
let point = vec![F::from(2), F::from(3)];
let expected_lo_vec = vec![
F::from((1 - 2) * (1 - 3)),
F::from(2 * (1 - 3)),
F::from((1 - 2) * 3),
F::from(2 * 3),
];
let expected_hi_vec = vec![
F::from(1),
F::from(1),
F::from(3) / F::from(1 - 3),
F::from(0),
];
let (lo_vec, hi_vec) = compute_dynamic_vecs(&point);
assert_eq!(expected_lo_vec, lo_vec);
assert_eq!(expected_hi_vec, hi_vec);
}

#[test]
fn we_can_compute_dynamic_vecs_for_length_3_point() {
let point = vec![F::from(2), F::from(3), F::from(5)];
let expected_lo_vec = vec![
F::from((1 - 2) * (1 - 3)),
F::from(2 * (1 - 3)),
F::from((1 - 2) * 3),
F::from(2 * 3),
];
let expected_hi_vec = vec![
F::from(1 - 5),
F::from(1 - 5),
F::from((1 - 5) * 3) / F::from(1 - 3),
F::from(5),
];
let (lo_vec, hi_vec) = compute_dynamic_vecs(&point);
assert_eq!(expected_lo_vec, lo_vec);
assert_eq!(expected_hi_vec, hi_vec);
}

#[test]
fn we_can_compute_dynamic_vecs_that_matches_evaluation_vec() {
use ark_std::UniformRand;
let mut rng = ark_std::test_rng();
for num_vars in 0..20 {
let point: Vec<_> = core::iter::repeat_with(|| F::rand(&mut rng))
.take(num_vars)
.collect();
let (lo_vec, hi_vec) = compute_dynamic_vecs(&point);
let mut eval_vec = vec![F::ZERO; 1 << num_vars];
compute_evaluation_vector(&mut eval_vec, &point);
for (i, val) in eval_vec.into_iter().enumerate() {
let (row, column) = row_and_column_from_index(i);
assert_eq!(hi_vec[row] * lo_vec[column], val);
}
}
}

#[test]
fn we_can_fold_dynamic_tensors() {
use ark_std::{test_rng, UniformRand};
Expand All @@ -249,9 +148,17 @@ mod tests {
.take(nu)
.collect_vec();

let (mut lo_vec, mut hi_vec) = compute_dynamic_vecs(&point);
naive_fold(&mut lo_vec, &alphas);
naive_fold(&mut hi_vec, &alpha_invs);
let (mut lo_vec, mut hi_vec) = compute_dynamic_vecs(
bytemuck::TransparentWrapper::wrap_slice(&point) as &[DoryScalar],
);
naive_fold(
&mut lo_vec,
bytemuck::TransparentWrapper::wrap_slice(&alphas) as &[DoryScalar],
);
naive_fold(
&mut hi_vec,
bytemuck::TransparentWrapper::wrap_slice(&alpha_invs) as &[DoryScalar],
);

let state = ExtendedVerifierState {
s1_tensor: point,
Expand All @@ -270,8 +177,8 @@ mod tests {
};
let (lo_fold, hi_fold) = fold_dynamic_tensors(&state);

assert_eq!(lo_fold, lo_vec[0]);
assert_eq!(hi_fold, hi_vec[0]);
assert_eq!(lo_fold, lo_vec[0].0);
assert_eq!(hi_fold, hi_vec[0].0);
}
}

Expand Down
Loading
Loading