Skip to content

Commit

Permalink
refactor: get random g1 vec method on PublicParameters for pub use in…
Browse files Browse the repository at this point in the history
… sxt-db
  • Loading branch information
Dustin-Ray committed Oct 2, 2024
1 parent f3c9dca commit 962fd7b
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
dory_inner_product_prove, dory_inner_product_verify, rand_G_vecs, test_rng, DoryMessages,
G1Affine, ProverState, PublicParameters, GT,
dory_inner_product_prove, dory_inner_product_verify, rand_G_vecs_for_testing, test_rng,
DoryMessages, G1Affine, ProverState, PublicParameters, GT,
};
use ark_std::UniformRand;
use merlin::Transcript;
Expand All @@ -12,7 +12,7 @@ fn we_can_prove_and_verify_a_dory_inner_product() {
let pp = PublicParameters::rand(nu, &mut rng);
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ProverState::new(v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand All @@ -38,7 +38,7 @@ fn we_can_prove_and_verify_a_dory_inner_product_for_multiple_nu_values() {
let verifier_setup = (&pp).into();

for nu in 0..max_nu {
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ProverState::new(v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand All @@ -63,7 +63,7 @@ fn we_fail_to_verify_a_dory_inner_product_when_a_message_is_modified() {
let pp = PublicParameters::rand(nu, &mut rng);
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ProverState::new(v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand All @@ -89,7 +89,7 @@ fn we_fail_to_verify_a_dory_inner_product_when_there_are_too_few_GT_messages() {
let pp = PublicParameters::rand(nu, &mut rng);
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ProverState::new(v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand All @@ -115,7 +115,7 @@ fn we_fail_to_verify_a_dory_inner_product_when_there_are_too_many_GT_messages()
let pp = PublicParameters::rand(nu, &mut rng);
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ProverState::new(v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand All @@ -141,7 +141,7 @@ fn we_fail_to_verify_a_dory_inner_product_when_there_are_too_few_G1_messages() {
let pp = PublicParameters::rand(nu, &mut rng);
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ProverState::new(v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand All @@ -167,7 +167,7 @@ fn we_fail_to_verify_a_dory_inner_product_when_there_are_too_many_G1_messages()
let pp = PublicParameters::rand(nu, &mut rng);
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ProverState::new(v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand All @@ -193,7 +193,7 @@ fn we_fail_to_verify_a_dory_inner_product_when_the_transcripts_differ() {
let pp = PublicParameters::rand(nu, &mut rng);
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ProverState::new(v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand All @@ -218,7 +218,7 @@ fn we_fail_to_verify_a_dory_inner_product_when_the_setups_differ() {
let prover_setup = (&pp).into();
let pp_wrong = PublicParameters::rand(nu, &mut rng);
let verifier_setup = (&pp_wrong).into();
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ProverState::new(v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand All @@ -244,7 +244,7 @@ fn we_fail_to_verify_a_dory_inner_product_when_the_commitment_is_wrong() {
let pp = PublicParameters::rand(nu, &mut rng);
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ProverState::new(v1, v2, nu);
let mut verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{
extended_dory_inner_product_prove, extended_dory_inner_product_verify,
extended_dory_reduce_helper::extended_dory_reduce_verify_fold_s_vecs, rand_F_tensors,
rand_G_vecs, test_rng, DoryMessages, ExtendedProverState, G1Affine, PublicParameters, GT,
rand_G_vecs_for_testing, test_rng, DoryMessages, ExtendedProverState, G1Affine, PublicParameters, GT,
};
use ark_std::UniformRand;
use merlin::Transcript;
Expand All @@ -14,7 +14,7 @@ fn we_can_prove_and_verify_an_extended_dory_inner_product() {
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ExtendedProverState::new_from_tensors(s1_tensor, s2_tensor, v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down Expand Up @@ -42,7 +42,7 @@ fn we_can_prove_and_verify_an_extended_dory_inner_product_for_multiple_nu_values

for nu in 0..max_nu {
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ExtendedProverState::new_from_tensors(s1_tensor, s2_tensor, v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down Expand Up @@ -74,7 +74,7 @@ fn we_fail_to_verify_an_extended_dory_inner_product_when_a_message_is_modified()
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ExtendedProverState::new_from_tensors(s1_tensor, s2_tensor, v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down Expand Up @@ -102,7 +102,7 @@ fn we_fail_to_verify_an_extended_dory_inner_product_when_there_are_too_few_GT_me
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ExtendedProverState::new_from_tensors(s1_tensor, s2_tensor, v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down Expand Up @@ -130,7 +130,7 @@ fn we_fail_to_verify_an_extended_dory_inner_product_when_there_are_too_many_GT_m
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ExtendedProverState::new_from_tensors(s1_tensor, s2_tensor, v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down Expand Up @@ -158,7 +158,7 @@ fn we_fail_to_verify_an_extended_dory_inner_product_when_there_are_too_few_G1_me
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ExtendedProverState::new_from_tensors(s1_tensor, s2_tensor, v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down Expand Up @@ -186,7 +186,7 @@ fn we_fail_to_verify_an_extended_dory_inner_product_when_there_are_too_many_G1_m
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ExtendedProverState::new_from_tensors(s1_tensor, s2_tensor, v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down Expand Up @@ -214,7 +214,7 @@ fn we_fail_to_verify_an_extended_dory_inner_product_when_the_transcripts_differ(
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ExtendedProverState::new_from_tensors(s1_tensor, s2_tensor, v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand All @@ -241,7 +241,7 @@ fn we_fail_to_verify_an_extended_dory_inner_product_when_the_setups_differ() {
let pp_wrong = PublicParameters::rand(nu, &mut rng);
let verifier_setup = (&pp_wrong).into();
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ExtendedProverState::new_from_tensors(s1_tensor, s2_tensor, v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down Expand Up @@ -269,7 +269,7 @@ fn we_fail_to_verify_an_extended_dory_inner_product_when_the_base_commitment_is_
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ExtendedProverState::new_from_tensors(s1_tensor, s2_tensor, v1, v2, nu);
let mut verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down Expand Up @@ -297,7 +297,7 @@ fn we_fail_to_verify_an_extended_dory_inner_product_when_a_scalar_commitment_is_
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ExtendedProverState::new_from_tensors(s1_tensor, s2_tensor, v1, v2, nu);
let mut verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
rand_F_tensors, rand_G_vecs, test_rng, ExtendedProverState, G1Projective, G2Projective,
PublicParameters,
rand_F_tensors, rand_G_vecs_for_testing, test_rng, ExtendedProverState, G1Projective,
G2Projective, PublicParameters,
};
use crate::base::polynomial::compute_evaluation_vector;
use ark_ec::{pairing::Pairing, VariableBaseMSM};
Expand All @@ -12,7 +12,7 @@ pub fn we_can_create_an_extended_verifier_state_from_an_extended_prover_state()
let pp = PublicParameters::rand(max_nu, &mut rng);
let prover_setup = (&pp).into();
for nu in 0..max_nu {
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let mut s1 = vec![Default::default(); 1 << nu];
let mut s2 = vec![Default::default(); 1 << nu];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
extended_dory_reduce_helper::extended_dory_reduce_verify_fold_s_vecs, fold_scalars_0_prove,
fold_scalars_0_verify, rand_F_tensors, rand_G_vecs, test_rng, DoryMessages,
fold_scalars_0_verify, rand_F_tensors, rand_G_vecs_for_testing, test_rng, DoryMessages,
ExtendedProverState, PublicParameters,
};
use merlin::Transcript;
Expand All @@ -13,7 +13,7 @@ fn we_can_fold_scalars() {
let prover_setup = (&pp).into();
let verifier_setup = (&pp).into();
let (s1_tensor, s2_tensor) = rand_F_tensors(nu, &mut rng);
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ExtendedProverState::new_from_tensors(s1_tensor, s2_tensor, v1, v2, nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/proof_primitive/dory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod rand_util;
#[cfg(test)]
use rand_util::rand_F_tensors;
#[cfg(any(test, feature = "test"))]
use rand_util::rand_G_vecs;
use rand_util::rand_G_vecs_for_testing;
#[cfg(any(test, feature = "test"))]
pub use rand_util::test_rng;

Expand Down
35 changes: 33 additions & 2 deletions crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::{G1Affine, G2Affine};
use alloc::vec::Vec;
use ark_ff::UniformRand;

/// The public parameters for the Dory protocol. See section 5 of https://eprint.iacr.org/2020/1274.pdf for details.
///
/// Note: even though H_1 and H_2 are marked as blue, they are still needed.
Expand All @@ -21,14 +23,33 @@ pub struct PublicParameters {
}

impl PublicParameters {
#[cfg(any(test, feature = "test"))]
/// Create new public parameters from the requisite group element vectors
/// and setup size.
pub fn new(
Gamma_1: Vec<G1Affine>,
Gamma_2: Vec<G2Affine>,
H_1: G1Affine,
H_2: G2Affine,
Gamma_2_fin: G2Affine,
max_nu: usize,
) -> Self {
Self {
Gamma_1,
Gamma_2,
H_1,
H_2,
Gamma_2_fin,
max_nu,
}
}

/// Generate random public parameters for testing purposes.
pub fn rand<R>(max_nu: usize, rng: &mut R) -> Self
where
R: ark_std::rand::Rng + ?Sized,
{
use ark_std::UniformRand;
let (Gamma_1, Gamma_2) = super::rand_G_vecs(max_nu, rng);
let (Gamma_1, Gamma_2) = Self::rand_G_vecs(max_nu, rng);
let (H_1, H_2) = (G1Affine::rand(rng), G2Affine::rand(rng));
let Gamma_2_fin = G2Affine::rand(rng);

Expand All @@ -41,4 +62,14 @@ impl PublicParameters {
Gamma_2_fin,
}
}

/// Creates two vectors of random G1 and G2 elements with length 2^nu.
pub fn rand_G_vecs<R>(nu: usize, rng: &mut R) -> (Vec<G1Affine>, Vec<G2Affine>)
where
R: ark_std::rand::Rng + ?Sized,
{
core::iter::repeat_with(|| (G1Affine::rand(rng), G2Affine::rand(rng)))
.take(1 << nu)
.unzip()
}
}
16 changes: 8 additions & 8 deletions crates/proof-of-sql/src/proof_primitive/dory/rand_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn test_seed_rng(seed: [u8; 32]) -> impl Rng {
}

/// Creates two vectors of random G1 and G2 elements with length 2^nu.
pub fn rand_G_vecs<R>(nu: usize, rng: &mut R) -> (Vec<G1Affine>, Vec<G2Affine>)
pub fn rand_G_vecs_for_testing<R>(nu: usize, rng: &mut R) -> (Vec<G1Affine>, Vec<G2Affine>)
where
R: ark_std::rand::Rng + ?Sized,
{
Expand Down Expand Up @@ -52,7 +52,7 @@ where
fn we_can_create_rand_G_vecs() {
let mut rng = test_rng();
for nu in 0..5 {
let (Gamma_1, Gamma_2) = rand_G_vecs(nu, &mut rng);
let (Gamma_1, Gamma_2) = rand_G_vecs_for_testing(nu, &mut rng);
assert_eq!(Gamma_1.len(), 1 << nu);
assert_eq!(Gamma_2.len(), 1 << nu);
}
Expand All @@ -62,8 +62,8 @@ fn we_can_create_rand_G_vecs() {
fn we_can_create_different_rand_G_vecs_consecutively_from_the_same_rng() {
let mut rng = test_rng();
for nu in 0..5 {
let (Gamma_1, Gamma_2) = rand_G_vecs(nu, &mut rng);
let (Gamma_1_2, Gamma_2_2) = rand_G_vecs(nu, &mut rng);
let (Gamma_1, Gamma_2) = rand_G_vecs_for_testing(nu, &mut rng);
let (Gamma_1_2, Gamma_2_2) = rand_G_vecs_for_testing(nu, &mut rng);
assert_ne!(Gamma_1, Gamma_1_2);
assert_ne!(Gamma_2, Gamma_2_2);
}
Expand All @@ -74,8 +74,8 @@ fn we_can_create_the_same_rand_G_vecs_from_the_same_seed() {
let mut rng = test_seed_rng([1; 32]);
let mut rng_2 = test_seed_rng([1; 32]);
for nu in 0..5 {
let (Gamma_1, Gamma_2) = rand_G_vecs(nu, &mut rng);
let (Gamma_1_2, Gamma_2_2) = rand_G_vecs(nu, &mut rng_2);
let (Gamma_1, Gamma_2) = rand_G_vecs_for_testing(nu, &mut rng);
let (Gamma_1_2, Gamma_2_2) = rand_G_vecs_for_testing(nu, &mut rng_2);
assert_eq!(Gamma_1, Gamma_1_2);
assert_eq!(Gamma_2, Gamma_2_2);
}
Expand All @@ -86,8 +86,8 @@ fn we_can_create_different_rand_G_vecs_from_different_seeds() {
let mut rng = test_seed_rng([1; 32]);
let mut rng_2 = test_seed_rng([2; 32]);
for nu in 0..5 {
let (Gamma_1, Gamma_2) = rand_G_vecs(nu, &mut rng);
let (Gamma_1_2, Gamma_2_2) = rand_G_vecs(nu, &mut rng_2);
let (Gamma_1, Gamma_2) = rand_G_vecs_for_testing(nu, &mut rng);
let (Gamma_1_2, Gamma_2_2) = rand_G_vecs_for_testing(nu, &mut rng_2);
assert_ne!(Gamma_1, Gamma_1_2);
assert_ne!(Gamma_2, Gamma_2_2);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/proof-of-sql/src/proof_primitive/dory/state_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{rand_G_vecs, test_rng, ProverState, PublicParameters};
use super::{rand_G_vecs_for_testing, test_rng, ProverState, PublicParameters};
use ark_ec::pairing::Pairing;

#[test]
Expand All @@ -8,7 +8,7 @@ pub fn we_can_create_a_verifier_state_from_a_prover_state() {
let pp = PublicParameters::rand(max_nu, &mut rng);
let prover_setup = (&pp).into();
for nu in 0..max_nu {
let (v1, v2) = rand_G_vecs(nu, &mut rng);
let (v1, v2) = rand_G_vecs_for_testing(nu, &mut rng);
let prover_state = ProverState::new(v1.clone(), v2.clone(), nu);
let verifier_state = prover_state.calculate_verifier_state(&prover_setup);

Expand Down

0 comments on commit 962fd7b

Please sign in to comment.