Skip to content

Commit

Permalink
refactor!: tidy dory setups (#39)
Browse files Browse the repository at this point in the history
# Rationale for this change

The setups in Dory are somewhat unwieldy, in particular, there isn't a
separation between the public parameters, the precomputed setups, and
the parameters for a particular proofs.

# What changes are included in this PR?

The major change involves `PublicParameters`, `ProverSetup`,
`VerifierSetup`, `DoryProverPublicSetup`, and `DoryVerifierPublicSetup`.
The code for constructing these was made more explicit and broken apart
better.
### Old
```
    let dory_prover_setup = DoryProverPublicSetup::rand(4, 3, &mut test_rng());
    let dory_verifier_setup = (&dory_prover_setup).into();
```
### New
```
    let public_parameters = PublicParameters::rand(4, &mut test_rng()); // This can be stored on disc and is the actual data from which everything else is generated.
    let prover_setup = ProverSetup::from(&public_parameters); // This largely is just a reference to `PublicParameters`, but also contains a blitzar handle, which takes significant computation to obtain.
    let verifier_setup = VerifierSetup::from(&public_parameters); // This can be stored on disc, and contains only the digest of `PublicParameters` that is needed to do verification.
    let dory_prover_setup = DoryProverPublicSetup::new(&prover_setup, 3); // This is essentially just a tuple `(&ProverSetup, sigma)`
    let dory_verifier_setup = DoryVerifierPublicSetup::new(&verifier_setup, 3); // This is essentially just a tuple `(&VerifierSetup, sigma)`
```

In order to achieve this, several changes were needed:
* The `CommitmentEvaluationProof::ProverPublicSetup` and
`CommitmentEvaluationProof::VerifierPublicSetup` have lifetime generics
added because `DoryProverPublicSetup` and `DoryVerifierPublicSetup` now
have lifetime generics.
* `rand` and `from` methods are removed in some places to enforce the
above flow.
* Tests are refactored. This is the majority of the LOC.

# Are these changes tested?

They are tested by existing tests.
  • Loading branch information
JayWhite2357 authored Jul 2, 2024
1 parent 2dbd966 commit 153e647
Show file tree
Hide file tree
Showing 24 changed files with 383 additions and 465 deletions.
2 changes: 1 addition & 1 deletion crates/proof-of-sql/benches/scaffold/benchmark_accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'a, C: Commitment> BenchmarkAccessor<'a, C> {
&mut self,
table_ref: TableRef,
columns: &[(Identifier, Column<'a, C::Scalar>)],
setup: &C::PublicSetup,
setup: &C::PublicSetup<'_>,
) {
self.table_schemas.insert(
table_ref,
Expand Down
10 changes: 5 additions & 5 deletions crates/proof-of-sql/benches/scaffold/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn scaffold<'a, CP: CommitmentEvaluationProof>(
query: &str,
columns: &[(&str, ColumnType, OptionalRandBound)],
size: usize,
prover_setup: &CP::ProverPublicSetup,
prover_setup: &CP::ProverPublicSetup<'_>,
alloc: &'a Bump,
accessor: &mut BenchmarkAccessor<'a, CP::Commitment>,
rng: &mut impl Rng,
Expand All @@ -41,8 +41,8 @@ pub fn jaeger_scaffold<CP: CommitmentEvaluationProof>(
query: &str,
columns: &[(&str, ColumnType, OptionalRandBound)],
size: usize,
prover_setup: &CP::ProverPublicSetup,
verifier_setup: &CP::VerifierPublicSetup,
prover_setup: &CP::ProverPublicSetup<'_>,
verifier_setup: &CP::VerifierPublicSetup<'_>,
) {
let mut accessor = BenchmarkAccessor::default();
let mut rng = rand::thread_rng();
Expand All @@ -68,8 +68,8 @@ pub fn criterion_scaffold<CP: CommitmentEvaluationProof>(
query: &str,
columns: &[(&str, ColumnType, OptionalRandBound)],
sizes: &[usize],
prover_setup: &CP::ProverPublicSetup,
verifier_setup: &CP::VerifierPublicSetup,
prover_setup: &CP::ProverPublicSetup<'_>,
verifier_setup: &CP::VerifierPublicSetup<'_>,
) {
let mut group = c.benchmark_group(format!("{} - {}", title, query));
group.sample_size(10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where

/// Private convenience aliases.
type Decompressed<C> = <Vec<C> as VecCommitmentExt>::DecompressedCommitment;
type Setup<C> = <Vec<C> as VecCommitmentExt>::CommitmentPublicSetup;
type Setup<'a, C> = <Vec<C> as VecCommitmentExt>::CommitmentPublicSetup<'a>;

impl<C> ColumnCommitments<C>
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ pub trait CommitmentEvaluationProof {
/// The associated scalar that the commitment is for.
type Scalar: Scalar + Serialize + for<'a> Deserialize<'a>;
/// The associated commitment type.
type Commitment: Commitment<Scalar = Self::Scalar, PublicSetup = Self::ProverPublicSetup>
type Commitment: for<'a> Commitment<Scalar = Self::Scalar, PublicSetup<'a> = Self::ProverPublicSetup<'a>>
+ Serialize
+ for<'a> Deserialize<'a>;
/// The error type for the proof.
type Error;
/// The public setup parameters required by the prover.
/// This is simply precomputed data that is required by the prover to create a proof.
type ProverPublicSetup;
type ProverPublicSetup<'a>: Copy;
/// The public setup parameters required by the verifier.
/// This is simply precomputed data that is required by the verifier to verify a proof.
type VerifierPublicSetup;
type VerifierPublicSetup<'a>: Copy;
/// Create a new proof.
///
/// Note: b_point must have length `nu`, where `2^nu` is at least the length of `a`.
Expand All @@ -35,7 +35,7 @@ pub trait CommitmentEvaluationProof {
a: &[Self::Scalar],
b_point: &[Self::Scalar],
generators_offset: u64,
setup: &Self::ProverPublicSetup,
setup: &Self::ProverPublicSetup<'_>,
) -> Self;
/// Verify a proof.
///
Expand All @@ -51,7 +51,7 @@ pub trait CommitmentEvaluationProof {
b_point: &[Self::Scalar],
generators_offset: u64,
table_length: usize,
setup: &Self::VerifierPublicSetup,
setup: &Self::VerifierPublicSetup<'_>,
) -> Result<(), Self::Error>;
/// Verify a batch proof. This can be more efficient than verifying individual proofs for some schemes.
#[allow(clippy::too_many_arguments)]
Expand All @@ -64,7 +64,7 @@ pub trait CommitmentEvaluationProof {
b_point: &[Self::Scalar],
generators_offset: u64,
table_length: usize,
setup: &Self::VerifierPublicSetup,
setup: &Self::VerifierPublicSetup<'_>,
) -> Result<(), Self::Error> {
self.verify_proof(
transcript,
Expand All @@ -83,14 +83,14 @@ impl CommitmentEvaluationProof for InnerProductProof {
type Scalar = MontScalar<ark_curve25519::FrConfig>;
type Commitment = RistrettoPoint;
type Error = ProofError;
type ProverPublicSetup = ();
type VerifierPublicSetup = ();
type ProverPublicSetup<'a> = ();
type VerifierPublicSetup<'a> = ();
fn new(
transcript: &mut Transcript,
a: &[Self::Scalar],
b_point: &[Self::Scalar],
generators_offset: u64,
_setup: &Self::ProverPublicSetup,
_setup: &Self::ProverPublicSetup<'_>,
) -> Self {
assert!(!a.is_empty());
let b = &mut vec![Default::default(); a.len()];
Expand All @@ -115,7 +115,7 @@ impl CommitmentEvaluationProof for InnerProductProof {
b_point: &[Self::Scalar],
generators_offset: u64,
table_length: usize,
_setup: &Self::VerifierPublicSetup,
_setup: &Self::VerifierPublicSetup<'_>,
) -> Result<(), Self::Error> {
assert!(table_length > 0);
let b = &mut vec![Default::default(); table_length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use merlin::Transcript;
use num_traits::{One, Zero};

pub fn test_simple_commitment_evaluation_proof<CP: CommitmentEvaluationProof>(
prover_setup: &CP::ProverPublicSetup,
verifier_setup: &CP::VerifierPublicSetup,
prover_setup: &CP::ProverPublicSetup<'_>,
verifier_setup: &CP::VerifierPublicSetup<'_>,
) {
let mut transcript = Transcript::new(b"evaluation_proof");
let proof = CP::new(
Expand Down Expand Up @@ -44,8 +44,8 @@ pub fn test_simple_commitment_evaluation_proof<CP: CommitmentEvaluationProof>(
}

pub fn test_commitment_evaluation_proof_with_length_1<CP: CommitmentEvaluationProof>(
prover_setup: &CP::ProverPublicSetup,
verifier_setup: &CP::VerifierPublicSetup,
prover_setup: &CP::ProverPublicSetup<'_>,
verifier_setup: &CP::VerifierPublicSetup<'_>,
) {
let mut rng = ark_std::test_rng();
let r = CP::Scalar::rand(&mut rng);
Expand All @@ -64,8 +64,8 @@ pub fn test_commitment_evaluation_proof_with_length_1<CP: CommitmentEvaluationPr
pub fn test_random_commitment_evaluation_proof<CP: CommitmentEvaluationProof>(
table_length: usize,
offset: usize,
prover_setup: &CP::ProverPublicSetup,
verifier_setup: &CP::VerifierPublicSetup,
prover_setup: &CP::ProverPublicSetup<'_>,
verifier_setup: &CP::VerifierPublicSetup<'_>,
) {
let nu = table_length.next_power_of_two().trailing_zeros() as usize;
assert!(table_length <= 1 << nu);
Expand Down
10 changes: 5 additions & 5 deletions crates/proof-of-sql/src/base/commitment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ pub trait Commitment:
+ for<'a> serde::Deserialize<'a>;

/// The public setup for the commitment scheme.
type PublicSetup;
type PublicSetup<'a>;

/// Compute the commitments for the given columns.
fn compute_commitments(
commitments: &mut [Self],
committable_columns: &[CommittableColumn],
offset: usize,
setup: &Self::PublicSetup,
setup: &Self::PublicSetup<'_>,
);

/// Compute a linear combination of the given commitments: `sum commitment[i] * multiplier[i]`.
Expand All @@ -79,13 +79,13 @@ pub trait Commitment:

impl Commitment for RistrettoPoint {
type Scalar = Curve25519Scalar;
type PublicSetup = ();
type PublicSetup<'a> = ();
#[cfg(feature = "blitzar")]
fn compute_commitments(
commitments: &mut [Self],
committable_columns: &[CommittableColumn],
offset: usize,
_setup: &Self::PublicSetup,
_setup: &Self::PublicSetup<'_>,
) {
let sequences = Vec::from_iter(committable_columns.iter().map(Into::into));
let mut compressed_commitments = vec![Default::default(); committable_columns.len()];
Expand All @@ -108,7 +108,7 @@ impl Commitment for RistrettoPoint {
_commitments: &mut [Self],
_committable_columns: &[CommittableColumn],
_offset: usize,
_setup: &Self::PublicSetup,
_setup: &Self::PublicSetup<'_>,
) {
unimplemented!()
}
Expand Down
10 changes: 7 additions & 3 deletions crates/proof-of-sql/src/base/commitment/query_commitments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,11 @@ mod tests {
},
scalar::Curve25519Scalar,
},
proof_primitive::dory::{DoryCommitment, DoryEvaluationProof, DoryProverPublicSetup},
proof_primitive::dory::{
test_rng, DoryCommitment, DoryEvaluationProof, DoryProverPublicSetup, ProverSetup,
PublicParameters,
},
};
use ark_std::test_rng;
use curve25519_dalek::RistrettoPoint;

#[test]
Expand Down Expand Up @@ -328,7 +330,9 @@ mod tests {

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

let column_a_id: Identifier = "column_a".parse().unwrap();
let column_b_id: Identifier = "column_b".parse().unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ where
}

/// Private convenience alias.
type Setup<C> = <Vec<C> as VecCommitmentExt>::CommitmentPublicSetup;
type Setup<'a, C> = <Vec<C> as VecCommitmentExt>::CommitmentPublicSetup<'a>;
type Decompressed<C> = <Vec<C> as VecCommitmentExt>::DecompressedCommitment;

impl<C> TableCommitment<C>
Expand Down
20 changes: 10 additions & 10 deletions crates/proof-of-sql/src/base/commitment/vec_commitment_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ pub struct NumColumnsMismatch;
pub trait VecCommitmentExt {
/// The public setup parameters required to compute the commitments.
/// This is simply precomputed data that is required to compute the commitments.
type CommitmentPublicSetup;
type CommitmentPublicSetup<'a>;

/// Returns a collection of commitments to the provided columns using the given generator offset.
fn from_columns_with_offset<'a, C>(
columns: impl IntoIterator<Item = C>,
offset: usize,
setup: &Self::CommitmentPublicSetup,
setup: &Self::CommitmentPublicSetup<'_>,
) -> Self
where
C: Into<CommittableColumn<'a>>;
Expand All @@ -28,7 +28,7 @@ pub trait VecCommitmentExt {
fn from_commitable_columns_with_offset(
committable_columns: &[CommittableColumn],
offset: usize,
setup: &Self::CommitmentPublicSetup,
setup: &Self::CommitmentPublicSetup<'_>,
) -> Self;

/// Append rows of data from the provided columns to the existing commitments.
Expand All @@ -41,7 +41,7 @@ pub trait VecCommitmentExt {
&mut self,
columns: impl IntoIterator<Item = C>,
offset: usize,
setup: &Self::CommitmentPublicSetup,
setup: &Self::CommitmentPublicSetup<'_>,
) -> Result<(), NumColumnsMismatch>
where
C: Into<CommittableColumn<'a>>;
Expand All @@ -51,7 +51,7 @@ pub trait VecCommitmentExt {
&mut self,
columns: impl IntoIterator<Item = C>,
offset: usize,
setup: &Self::CommitmentPublicSetup,
setup: &Self::CommitmentPublicSetup<'_>,
) where
C: Into<CommittableColumn<'a>>;

Expand Down Expand Up @@ -92,11 +92,11 @@ fn unsafe_sub_assign<C: Commitment>(a: &mut [C], b: &[C]) {
}

impl<C: Commitment> VecCommitmentExt for Vec<C> {
type CommitmentPublicSetup = C::PublicSetup;
type CommitmentPublicSetup<'a> = C::PublicSetup<'a>;
fn from_columns_with_offset<'a, COL>(
columns: impl IntoIterator<Item = COL>,
offset: usize,
setup: &Self::CommitmentPublicSetup,
setup: &Self::CommitmentPublicSetup<'_>,
) -> Self
where
COL: Into<CommittableColumn<'a>>,
Expand All @@ -110,7 +110,7 @@ impl<C: Commitment> VecCommitmentExt for Vec<C> {
fn from_commitable_columns_with_offset(
committable_columns: &[CommittableColumn],
offset: usize,
setup: &Self::CommitmentPublicSetup,
setup: &Self::CommitmentPublicSetup<'_>,
) -> Self {
let mut commitments = vec![C::default(); committable_columns.len()];
C::compute_commitments(&mut commitments, committable_columns, offset, setup);
Expand All @@ -122,7 +122,7 @@ impl<C: Commitment> VecCommitmentExt for Vec<C> {
&mut self,
columns: impl IntoIterator<Item = COL>,
offset: usize,
setup: &Self::CommitmentPublicSetup,
setup: &Self::CommitmentPublicSetup<'_>,
) -> Result<(), NumColumnsMismatch>
where
COL: Into<CommittableColumn<'a>>,
Expand All @@ -145,7 +145,7 @@ impl<C: Commitment> VecCommitmentExt for Vec<C> {
&mut self,
columns: impl IntoIterator<Item = COL>,
offset: usize,
setup: &Self::CommitmentPublicSetup,
setup: &Self::CommitmentPublicSetup<'_>,
) where
COL: Into<CommittableColumn<'a>>,
{
Expand Down
Loading

0 comments on commit 153e647

Please sign in to comment.