Skip to content

Commit

Permalink
refactor!: expose random public parameter generation (#207)
Browse files Browse the repository at this point in the history
# Rationale for this change

If PublicParameters are generated within sxtDB and not an argument
passed in via protobuf, then we need a way to randomly generate the
setup within external crates such as sxtdb. This PR exposes a trait
bounded method requiring a CSRNG which generates the public parameters
securely.

# What changes are included in this PR?

- [x] exposes a crpytographically secure method for public parameter
generation
- [x] rename existing rand to ```test_rand```
- [x] refactor feature gates  

# Are these changes tested?

existing tests pass
  • Loading branch information
Dustin-Ray authored Oct 3, 2024
1 parent 063f105 commit 88e09d0
Show file tree
Hide file tree
Showing 21 changed files with 129 additions and 114 deletions.
5 changes: 3 additions & 2 deletions crates/proof-of-sql/benches/bench_append_rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! cargo bench --features "test" --bench bench_append_rows
//! ```
#![allow(missing_docs, clippy::missing_docs_in_private_items)]
use ark_std::test_rng;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use proof_of_sql::{
base::{
Expand All @@ -20,7 +21,7 @@ use proof_of_sql::{
scalar::Scalar,
},
proof_primitive::dory::{
test_rng, DoryCommitment, DoryProverPublicSetup, DoryScalar, ProverSetup, PublicParameters,
DoryCommitment, DoryProverPublicSetup, DoryScalar, ProverSetup, PublicParameters,
},
};
use proof_of_sql_parser::posql_time::{PoSQLTimeUnit, PoSQLTimeZone};
Expand All @@ -38,7 +39,7 @@ use std::ops::Deref;
/// append 1000 rows to 10 cols in 1 table = 652ms
/// ```
fn bench_append_rows(c: &mut Criterion, cols: usize, rows: usize) {
let public_parameters = PublicParameters::rand(10, &mut test_rng());
let public_parameters = PublicParameters::test_rand(10, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let dory_prover_setup = DoryProverPublicSetup::new(&prover_setup, 3);
c.bench_function("append_rows_to_table_commitment", |b| {
Expand Down
5 changes: 3 additions & 2 deletions crates/proof-of-sql/benches/jaeger_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//! ```
//! Then, navigate to http://localhost:16686 to view the traces.

#[cfg(feature = "test")]
use ark_std::test_rng;
use blitzar::{compute::init_backend, proof::InnerProductProof};
#[cfg(feature = "test")]
use proof_of_sql::proof_primitive::dory::{
Expand Down Expand Up @@ -53,8 +55,7 @@ fn main() {
#[cfg(feature = "test")]
"Dory" => {
// Run 3 times to ensure that warm-up of the GPU has occurred.
let pp =
PublicParameters::rand(10, &mut proof_of_sql::proof_primitive::dory::test_rng());
let pp = PublicParameters::test_rand(10, &mut test_rng());
let ps = ProverSetup::from(&pp);
let prover_setup = DoryProverPublicSetup::new(&ps, 10);
let vs = VerifierSetup::from(&pp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ mod tests {

#[test]
fn we_can_get_query_commitments_from_accessor() {
let public_parameters = PublicParameters::rand(4, &mut test_rng());
let public_parameters = PublicParameters::test_rand(4, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 3);

Expand Down
16 changes: 8 additions & 8 deletions crates/proof-of-sql/src/proof_primitive/dory/dory_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ mod tests {

#[test]
fn we_can_convert_from_columns() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let Gamma_1 = &public_parameters.Gamma_1;
Expand Down Expand Up @@ -156,7 +156,7 @@ mod tests {

#[test]
fn we_can_append_rows() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let Gamma_1 = &public_parameters.Gamma_1;
Expand Down Expand Up @@ -206,7 +206,7 @@ mod tests {

#[test]
fn we_cannot_append_rows_with_different_column_count() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);

Expand Down Expand Up @@ -245,7 +245,7 @@ mod tests {

#[test]
fn we_can_extend_columns() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let Gamma_1 = &public_parameters.Gamma_1;
Expand Down Expand Up @@ -302,7 +302,7 @@ mod tests {

#[test]
fn we_can_add_commitment_collections() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let Gamma_1 = &public_parameters.Gamma_1;
Expand Down Expand Up @@ -353,7 +353,7 @@ mod tests {

#[test]
fn we_cannot_add_commitment_collections_of_mixed_column_counts() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);

Expand Down Expand Up @@ -398,7 +398,7 @@ mod tests {

#[test]
fn we_can_sub_commitment_collections() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let Gamma_1 = &public_parameters.Gamma_1;
Expand Down Expand Up @@ -441,7 +441,7 @@ mod tests {

#[test]
fn we_cannot_sub_commitment_collections_of_mixed_column_counts() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use merlin::Transcript;

#[test]
fn test_simple_ipa() {
let public_parameters = PublicParameters::rand(4, &mut test_rng());
let public_parameters = PublicParameters::test_rand(4, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let verifier_setup = VerifierSetup::from(&public_parameters);
test_simple_commitment_evaluation_proof::<DoryEvaluationProof>(
Expand All @@ -19,7 +19,7 @@ fn test_simple_ipa() {
&DoryProverPublicSetup::new(&prover_setup, 3),
&DoryVerifierPublicSetup::new(&verifier_setup, 3),
);
let public_parameters = PublicParameters::rand(6, &mut test_rng());
let public_parameters = PublicParameters::test_rand(6, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let verifier_setup = VerifierSetup::from(&public_parameters);
test_simple_commitment_evaluation_proof::<DoryEvaluationProof>(
Expand All @@ -30,7 +30,7 @@ fn test_simple_ipa() {

#[test]
fn test_random_ipa_with_length_1() {
let public_parameters = PublicParameters::rand(4, &mut test_rng());
let public_parameters = PublicParameters::test_rand(4, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let verifier_setup = VerifierSetup::from(&public_parameters);
test_commitment_evaluation_proof_with_length_1::<DoryEvaluationProof>(
Expand All @@ -41,7 +41,7 @@ fn test_random_ipa_with_length_1() {
&DoryProverPublicSetup::new(&prover_setup, 3),
&DoryVerifierPublicSetup::new(&verifier_setup, 3),
);
let public_parameters = PublicParameters::rand(6, &mut test_rng());
let public_parameters = PublicParameters::test_rand(6, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let verifier_setup = VerifierSetup::from(&public_parameters);
test_commitment_evaluation_proof_with_length_1::<DoryEvaluationProof>(
Expand All @@ -55,7 +55,7 @@ fn test_random_ipa_with_various_lengths() {
let lengths = [128, 100, 64, 50, 32, 20, 16, 10, 8, 5, 4, 3, 2];
let setup_setup = [(4, 4), (4, 3), (6, 2)];
for setup_p in setup_setup {
let public_parameters = PublicParameters::rand(setup_p.0, &mut test_rng());
let public_parameters = PublicParameters::test_rand(setup_p.0, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let verifier_setup = VerifierSetup::from(&public_parameters);
for length in lengths {
Expand All @@ -72,7 +72,7 @@ fn test_random_ipa_with_various_lengths() {
#[test]
fn we_can_serialize_and_deserialize_dory_evaluation_proofs() {
let mut rng = test_rng();
let public_parameters = PublicParameters::rand(4, &mut rng);
let public_parameters = PublicParameters::test_rand(4, &mut rng);
let prover_setup = ProverSetup::from(&public_parameters);
let a = core::iter::repeat_with(|| DoryScalar::rand(&mut rng))
.take(30)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use proof_of_sql_parser::posql_time::{PoSQLTimeUnit, PoSQLTimeZone};

#[test]
fn we_can_compute_a_dory_commitment_with_int128_values() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(&[CommittableColumn::Int128(&[0, -1, 2])], 0, &setup);
Expand All @@ -25,7 +25,7 @@ fn we_can_compute_a_dory_commitment_with_int128_values() {

#[test]
fn we_can_compute_a_dory_commitment_with_boolean_values() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(
Expand All @@ -43,7 +43,7 @@ fn we_can_compute_a_dory_commitment_with_boolean_values() {

#[test]
fn we_can_compute_a_dory_commitment_with_only_one_row() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(&[CommittableColumn::BigInt(&[0, 1, 2])], 0, &setup);
Expand All @@ -57,7 +57,7 @@ fn we_can_compute_a_dory_commitment_with_only_one_row() {

#[test]
fn we_can_compute_a_dory_commitment_with_exactly_one_full_row() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(&[CommittableColumn::BigInt(&[0, 1, 2, 3])], 0, &setup);
Expand All @@ -72,7 +72,7 @@ fn we_can_compute_a_dory_commitment_with_exactly_one_full_row() {

#[test]
fn we_can_compute_a_dory_commitment_with_exactly_one_full_row_and_an_offset() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(&[CommittableColumn::BigInt(&[2, 3])], 2, &setup);
Expand All @@ -85,7 +85,7 @@ fn we_can_compute_a_dory_commitment_with_exactly_one_full_row_and_an_offset() {

#[test]
fn we_can_compute_a_dory_commitment_with_exactly_one_full_row_and_an_offset_with_signed_data() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(&[CommittableColumn::BigInt(&[-2, -3])], 2, &setup);
Expand All @@ -98,7 +98,7 @@ fn we_can_compute_a_dory_commitment_with_exactly_one_full_row_and_an_offset_with

#[test]
fn we_can_compute_a_dory_commitment_with_fewer_rows_than_columns() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(
Expand All @@ -123,7 +123,7 @@ fn we_can_compute_a_dory_commitment_with_fewer_rows_than_columns() {

#[test]
fn we_can_compute_a_dory_commitment_with_more_rows_than_columns() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(
Expand Down Expand Up @@ -159,7 +159,7 @@ fn we_can_compute_a_dory_commitment_with_more_rows_than_columns() {

#[test]
fn we_can_compute_a_dory_commitment_with_an_offset_and_only_one_row() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(&[CommittableColumn::BigInt(&[0, 1])], 5, &setup);
Expand All @@ -172,7 +172,7 @@ fn we_can_compute_a_dory_commitment_with_an_offset_and_only_one_row() {

#[test]
fn we_can_compute_a_dory_commitment_with_an_offset_and_fewer_rows_than_columns() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(
Expand All @@ -197,7 +197,7 @@ fn we_can_compute_a_dory_commitment_with_an_offset_and_fewer_rows_than_columns()

#[test]
fn we_can_compute_a_dory_commitment_with_an_offset_and_more_rows_than_columns() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(
Expand Down Expand Up @@ -233,7 +233,7 @@ fn we_can_compute_a_dory_commitment_with_an_offset_and_more_rows_than_columns()

#[test]
fn we_can_compute_three_dory_commitments_with_an_offset_and_more_rows_than_columns() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(
Expand Down Expand Up @@ -319,7 +319,7 @@ fn we_can_compute_three_dory_commitments_with_an_offset_and_more_rows_than_colum

#[test]
fn we_can_compute_an_empty_dory_commitment() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(&[CommittableColumn::BigInt(&[0, 0])], 0, &setup);
Expand All @@ -332,7 +332,7 @@ fn we_can_compute_an_empty_dory_commitment() {

#[test]
fn test_compute_dory_commitment_when_sigma_is_zero() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 0);
let res = compute_dory_commitments(&[CommittableColumn::BigInt(&[0, 1, 2, 3, 4])], 0, &setup);
Expand All @@ -348,7 +348,7 @@ fn test_compute_dory_commitment_when_sigma_is_zero() {

#[test]
fn test_compute_dory_commitment_with_zero_sigma_and_with_an_offset() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 0);
let res = compute_dory_commitments(&[CommittableColumn::BigInt(&[0, 1, 2, 3, 4])], 5, &setup);
Expand All @@ -364,7 +364,7 @@ fn test_compute_dory_commitment_with_zero_sigma_and_with_an_offset() {

#[test]
fn we_can_compute_a_dory_commitment_with_mixed_committable_columns_with_fewer_rows_than_columns() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(
Expand Down Expand Up @@ -441,7 +441,7 @@ fn we_can_compute_a_dory_commitment_with_mixed_committable_columns_with_fewer_ro
#[test]
fn we_can_compute_a_dory_commitment_with_mixed_committable_columns_with_an_offset_and_fewer_rows_than_columns(
) {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(
Expand Down Expand Up @@ -517,7 +517,7 @@ fn we_can_compute_a_dory_commitment_with_mixed_committable_columns_with_an_offse

#[test]
fn we_can_compute_a_dory_commitment_with_mixed_committable_columns_with_signed_values() {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(
Expand Down Expand Up @@ -606,7 +606,7 @@ fn we_can_compute_a_dory_commitment_with_mixed_committable_columns_with_signed_v
#[test]
fn we_can_compute_a_dory_commitment_with_mixed_committable_columns_with_an_offset_and_signed_values(
) {
let public_parameters = PublicParameters::rand(5, &mut test_rng());
let public_parameters = PublicParameters::test_rand(5, &mut test_rng());
let prover_setup = ProverSetup::from(&public_parameters);
let setup = DoryProverPublicSetup::new(&prover_setup, 2);
let res = compute_dory_commitments(
Expand Down
Loading

0 comments on commit 88e09d0

Please sign in to comment.