Skip to content

Commit

Permalink
Merge branch 'main' into refactor/snafu
Browse files Browse the repository at this point in the history
  • Loading branch information
lgiussan authored Sep 26, 2024
2 parents 31910e5 + 057edde commit 35eb4f6
Show file tree
Hide file tree
Showing 76 changed files with 339 additions and 608 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version = "0.0.0" # DO NOT CHANGE THIS LINE! This will be automatically updated
license-file = "LICENSE"

[workspace.dependencies]
ahash = { version = "0.8.11", default-features = false, features = ["runtime-rng"] }
# alloy-primitives = { version = "0.8.1" }
# alloy-sol-types = { version = "0.8.1" }
ark-bls12-381 = { version = "0.4.0" }
Expand All @@ -36,7 +37,7 @@ curve25519-dalek = { version = "4", features = ["rand_core"] }
derive_more = { version = "0.99" }
flexbuffers = { version = "2.0.0" }
# forge-script = { git = "https://github.com/foundry-rs/foundry", tag = "nightly-bf1a39980532f76cd76fd87ee32661180f606435" }
indexmap = { version = "2.1" }
indexmap = { version = "2.1", default-features = false }
itertools = { version = "0.13.0" }
lalrpop = { version = "0.21.0" }
lalrpop-util = { version = "0.20.0", default-features = false }
Expand Down
5 changes: 4 additions & 1 deletion crates/proof-of-sql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ doctest = true
test = true

[dependencies]
ahash = { workspace = true }
ark-bls12-381 = { workspace = true }
ark-curve25519 = { workspace = true }
ark-ec = { workspace = true }
Expand All @@ -35,7 +36,7 @@ derive_more = { workspace = true }
indexmap = { workspace = true, features = ["serde"] }
itertools = { workspace = true }
lazy_static = { workspace = true }
merlin = { workspace = true }
merlin = { workspace = true, optional = true }
num-traits = { workspace = true }
num-bigint = { workspace = true, default-features = false }
postcard = { workspace = true, features = ["alloc"] }
Expand All @@ -57,6 +58,7 @@ blitzar = { workspace = true }
clap = { workspace = true, features = ["derive"] }
criterion = { workspace = true, features = ["html_reports"] }
# forge-script = { workspace = true }
merlin = { workspace = true }
opentelemetry = { workspace = true }
opentelemetry-jaeger = { workspace = true }
rand = { workspace = true, default-features = false }
Expand All @@ -74,6 +76,7 @@ development = ["arrow-csv"]
[features]
default = ["arrow", "blitzar", "rayon"]
arrow = ["dep:arrow"]
blitzar = ["dep:blitzar", "dep:merlin"]
test = ["dep:rand"]

[lints]
Expand Down
4 changes: 2 additions & 2 deletions crates/proof-of-sql/benches/scaffold/benchmark_accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ impl<'a, C: Commitment> BenchmarkAccessor<'a, C> {
.collect(),
);

let mut commitments = vec![C::default(); columns.len()];
let committable_columns = columns
.iter()
.map(|(_, col)| col.into())
.collect::<Vec<_>>();
C::compute_commitments(&mut commitments, &committable_columns, 0, setup);

let commitments = C::compute_commitments(&committable_columns, 0, setup);

let mut length = None;
for (column, commitment) in columns.iter().zip(commitments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use super::{
column_commitment_metadata::ColumnCommitmentMetadataMismatch, ColumnCommitmentMetadata,
CommittableColumn,
};
use crate::base::database::ColumnField;
use crate::base::{database::ColumnField, map::IndexMap};
use alloc::string::{String, ToString};
use indexmap::IndexMap;
use proof_of_sql_parser::Identifier;
use snafu::Snafu;

Expand Down
10 changes: 6 additions & 4 deletions crates/proof-of-sql/src/base/commitment/column_commitments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ use super::{
committable_column::CommittableColumn, ColumnCommitmentMetadata, ColumnCommitmentMetadataMap,
ColumnCommitmentMetadataMapExt, ColumnCommitmentsMismatch, Commitment, VecCommitmentExt,
};
use crate::base::database::{ColumnField, ColumnRef, CommitmentAccessor, TableRef};
use crate::base::{
database::{ColumnField, ColumnRef, CommitmentAccessor, TableRef},
map::IndexSet,
};
use alloc::{
borrow::ToOwned,
string::{String, ToString},
vec,
vec::Vec,
};
use core::{iter, slice};
use indexmap::IndexSet;
use proof_of_sql_parser::Identifier;
use serde::{Deserialize, Serialize};
use snafu::Snafu;
Expand Down Expand Up @@ -119,7 +121,7 @@ impl<C: Commitment> ColumnCommitments<C> {
COL: Into<CommittableColumn<'a>>,
{
// Check for duplicate identifiers
let mut unique_identifiers = IndexSet::new();
let mut unique_identifiers = IndexSet::default();
let unique_columns = columns
.into_iter()
.map(|(identifier, column)| {
Expand Down Expand Up @@ -171,7 +173,7 @@ impl<C: Commitment> ColumnCommitments<C> {
COL: Into<CommittableColumn<'a>>,
{
// Check for duplicate identifiers.
let mut unique_identifiers = IndexSet::new();
let mut unique_identifiers = IndexSet::default();
let unique_columns = columns
.into_iter()
.map(|(identifier, column)| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use super::Commitment;
use crate::base::{proof::Transcript as _, scalar::Scalar};
use crate::base::{proof::Transcript, scalar::Scalar};
#[cfg(feature = "blitzar")]
use crate::base::{scalar::MontScalar, slice_ops};
#[cfg(feature = "blitzar")]
use blitzar::proof::{InnerProductProof, ProofError};
#[cfg(feature = "blitzar")]
use curve25519_dalek::RistrettoPoint;
use merlin::Transcript;
use serde::{Deserialize, Serialize};

/// A trait for using commitment schemes generically. Specifically, this trait is for the evaluation proof of a commitment scheme.
Expand All @@ -31,7 +30,7 @@ pub trait CommitmentEvaluationProof {
/// `b_point` are the values for the variables that are being evaluated.
/// The resulting evaluation is the the inner product of `a` and `b`, where `b` is the expanded vector form of `b_point`.
fn new(
transcript: &mut Transcript,
transcript: &mut impl Transcript,
a: &[Self::Scalar],
b_point: &[Self::Scalar],
generators_offset: u64,
Expand All @@ -45,7 +44,7 @@ pub trait CommitmentEvaluationProof {
#[allow(clippy::too_many_arguments)]
fn verify_proof(
&self,
transcript: &mut Transcript,
transcript: &mut impl Transcript,
a_commit: &Self::Commitment,
product: &Self::Scalar,
b_point: &[Self::Scalar],
Expand All @@ -68,7 +67,7 @@ pub trait CommitmentEvaluationProof {
#[allow(clippy::too_many_arguments)]
fn verify_batched_proof(
&self,
transcript: &mut Transcript,
transcript: &mut impl Transcript,
commit_batch: &[Self::Commitment],
batching_factors: &[Self::Scalar],
product: &Self::Scalar,
Expand All @@ -87,7 +86,7 @@ impl CommitmentEvaluationProof for InnerProductProof {
type ProverPublicSetup<'a> = ();
type VerifierPublicSetup<'a> = ();
fn new(
transcript: &mut Transcript,
transcript: &mut impl Transcript,
a: &[Self::Scalar],
b_point: &[Self::Scalar],
generators_offset: u64,
Expand Down Expand Up @@ -115,7 +114,7 @@ impl CommitmentEvaluationProof for InnerProductProof {

fn verify_batched_proof(
&self,
transcript: &mut Transcript,
transcript: &mut impl Transcript,
commit_batch: &[Self::Commitment],
batching_factors: &[Self::Scalar],
product: &Self::Scalar,
Expand Down
23 changes: 10 additions & 13 deletions crates/proof-of-sql/src/base/commitment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,46 +84,43 @@ pub trait Commitment:
///
/// `offset` is the amount that `committable_columns` is "offset" by. Logically adding `offset` many 0s to the beginning of each of the `committable_columns`.
fn compute_commitments(
commitments: &mut [Self],
committable_columns: &[CommittableColumn],
offset: usize,
setup: &Self::PublicSetup<'_>,
);
) -> Vec<Self>;
}

impl Commitment for RistrettoPoint {
type Scalar = Curve25519Scalar;
type PublicSetup<'a> = ();
#[cfg(feature = "blitzar")]
fn compute_commitments(
commitments: &mut [Self],
committable_columns: &[CommittableColumn],
offset: usize,
_setup: &Self::PublicSetup<'_>,
) {
) -> Vec<Self> {
let sequences = Vec::from_iter(committable_columns.iter().map(Into::into));
let mut compressed_commitments = vec![Default::default(); committable_columns.len()];
blitzar::compute::compute_curve25519_commitments(
&mut compressed_commitments,
&sequences,
offset as u64,
);
commitments
.iter_mut()
.zip(compressed_commitments.iter())
.for_each(|(c, cc)| {
*c = cc.decompress().expect(
compressed_commitments
.into_iter()
.map(|cc| {
cc.decompress().expect(
"invalid ristretto point decompression in Commitment::compute_commitments",
);
});
)
})
.collect()
}
#[cfg(not(feature = "blitzar"))]
fn compute_commitments(
_commitments: &mut [Self],
_committable_columns: &[CommittableColumn],
_offset: usize,
_setup: &Self::PublicSetup<'_>,
) {
) -> Vec<Self> {
unimplemented!()
}
}
Expand Down
21 changes: 9 additions & 12 deletions crates/proof-of-sql/src/base/commitment/naive_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,13 @@ impl Commitment for NaiveCommitment {
type PublicSetup<'a> = ();

fn compute_commitments(
commitments: &mut [Self],
committable_columns: &[CommittableColumn],
offset: usize,
_setup: &Self::PublicSetup<'_>,
) {
commitments
.iter_mut()
.zip(committable_columns)
.for_each(|(nc, cc)| {
) -> Vec<Self> {
committable_columns
.iter()
.map(|cc| {
let mut vectors: Vec<TestScalar> = vec![TestScalar::ZERO; offset];
let mut existing_scalars: Vec<TestScalar> = match cc {
CommittableColumn::Boolean(bool_vec) => {
Expand Down Expand Up @@ -150,8 +148,9 @@ impl Commitment for NaiveCommitment {
}
};
vectors.append(&mut existing_scalars);
*nc = NaiveCommitment(vectors);
});
NaiveCommitment(vectors)
})
.collect()
}
}

Expand All @@ -170,8 +169,7 @@ fn we_can_compute_commitments_from_commitable_columns() {
let commitable_column_a = CommittableColumn::BigInt(&column_a);
let commitable_column_b = CommittableColumn::VarChar(column_b);
let committable_columns: &[CommittableColumn] = &[commitable_column_a, commitable_column_b];
let mut commitments: Vec<NaiveCommitment> = vec![NaiveCommitment(Vec::new()); 2];
NaiveCommitment::compute_commitments(&mut commitments, committable_columns, 0, &());
let commitments = NaiveCommitment::compute_commitments(committable_columns, 0, &());
assert_eq!(commitments[0].0, column_a_scalars);
assert_eq!(commitments[1].0, column_b_scalars);
}
Expand All @@ -182,7 +180,6 @@ fn we_can_compute_commitments_from_commitable_columns_with_offset() {
let column_a_scalars: Vec<TestScalar> = column_a.iter().map(|a| a.into()).collect();
let commitable_column_a = CommittableColumn::BigInt(&column_a[1..]);
let committable_columns: &[CommittableColumn] = &[commitable_column_a];
let mut commitments: Vec<NaiveCommitment> = vec![NaiveCommitment(Vec::new()); 2];
NaiveCommitment::compute_commitments(&mut commitments, committable_columns, 1, &());
let commitments = NaiveCommitment::compute_commitments(committable_columns, 1, &());
assert_eq!(commitments[0].0, column_a_scalars);
}
27 changes: 16 additions & 11 deletions crates/proof-of-sql/src/base/commitment/query_commitments.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use super::{Commitment, TableCommitment};
use crate::base::database::{
ColumnField, ColumnRef, ColumnType, CommitmentAccessor, MetadataAccessor, SchemaAccessor,
TableRef,
use crate::base::{
database::{
ColumnField, ColumnRef, ColumnType, CommitmentAccessor, MetadataAccessor, SchemaAccessor,
TableRef,
},
map::IndexMap,
};
use alloc::vec::Vec;
use indexmap::IndexMap;
use proof_of_sql_parser::Identifier;

/// The commitments for all of the tables in a query.
Expand Down Expand Up @@ -34,13 +36,16 @@ impl<C: Commitment> QueryCommitmentsExt<C> for QueryCommitments<C> {
) -> Self {
columns
.into_iter()
.fold(IndexMap::<_, Vec<_>>::new(), |mut table_columns, column| {
table_columns
.entry(column.table_ref())
.or_default()
.push(ColumnField::new(column.column_id(), *column.column_type()));
table_columns
})
.fold(
IndexMap::<_, Vec<_>>::default(),
|mut table_columns, column| {
table_columns
.entry(column.table_ref())
.or_default()
.push(ColumnField::new(column.column_id(), *column.column_type()));
table_columns
},
)
.into_iter()
.map(|(table_ref, columns)| {
(
Expand Down
4 changes: 2 additions & 2 deletions crates/proof-of-sql/src/base/commitment/table_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,12 @@ mod tests {
use crate::{
base::{
database::{owned_table_utility::*, OwnedColumn},
map::IndexMap,
scalar::Curve25519Scalar,
},
record_batch,
};
use curve25519_dalek::RistrettoPoint;
use indexmap::IndexMap;

#[test]
#[allow(clippy::reversed_empty_ranges)]
Expand All @@ -511,7 +511,7 @@ mod tests {
fn we_can_construct_table_commitment_from_columns_and_identifiers() {
// no-columns case
let mut empty_columns_iter: IndexMap<Identifier, OwnedColumn<Curve25519Scalar>> =
IndexMap::new();
IndexMap::default();
let empty_table_commitment =
TableCommitment::<RistrettoPoint>::try_from_columns_with_offset(
&empty_columns_iter,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{naive_commitment::NaiveCommitment, CommitmentEvaluationProof};
use crate::base::scalar::test_scalar::TestScalar;
use crate::base::{proof::Transcript, scalar::test_scalar::TestScalar};

/// This should only be used for the purpose of unit testing.
pub struct TestEvaluationProof {}
Expand All @@ -21,7 +21,7 @@ impl CommitmentEvaluationProof for TestEvaluationProof {
type VerifierPublicSetup<'a> = ();

fn new(
_transcript: &mut merlin::Transcript,
_transcript: &mut impl Transcript,
_a: &[Self::Scalar],
_b_point: &[Self::Scalar],
_generators_offset: u64,
Expand All @@ -32,7 +32,7 @@ impl CommitmentEvaluationProof for TestEvaluationProof {

fn verify_batched_proof(
&self,
_transcript: &mut merlin::Transcript,
_transcript: &mut impl Transcript,
_commit_batch: &[Self::Commitment],
_batching_factors: &[Self::Scalar],
_product: &Self::Scalar,
Expand Down
10 changes: 3 additions & 7 deletions crates/proof-of-sql/src/base/commitment/vec_commitment_ext.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Commitment;
use crate::base::commitment::committable_column::CommittableColumn;
use alloc::{vec, vec::Vec};
use alloc::vec::Vec;
use snafu::Snafu;

/// Cannot update commitment collections with different column counts
Expand Down Expand Up @@ -102,10 +102,7 @@ impl<C: Commitment> VecCommitmentExt for Vec<C> {
offset: usize,
setup: &Self::CommitmentPublicSetup<'_>,
) -> Self {
let mut commitments = vec![C::default(); committable_columns.len()];
C::compute_commitments(&mut commitments, committable_columns, offset, setup);

commitments
C::compute_commitments(committable_columns, offset, setup)
}

fn try_append_rows_with_offset<'a, COL>(
Expand All @@ -124,8 +121,7 @@ impl<C: Commitment> VecCommitmentExt for Vec<C> {
return Err(NumColumnsMismatch);
}

let partial_commitments =
Self::from_commitable_columns_with_offset(&committable_columns, offset, setup);
let partial_commitments = C::compute_commitments(&committable_columns, offset, setup);
unsafe_add_assign(self, &partial_commitments);

Ok(())
Expand Down
Loading

0 comments on commit 35eb4f6

Please sign in to comment.