Skip to content

Commit

Permalink
make dory default, and add check for the commitments.
Browse files Browse the repository at this point in the history
  • Loading branch information
tarassh committed Aug 20, 2024
1 parent 3366b1d commit e361586
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 32 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version = "0.1.0"
edition = "2021"

[dependencies]
ark-serialize = { version = "0.4.0", default-features = false }
bincode = "1"
serde = { version = "1", features = ["derive"] }
blitzar = { version = "3.1.0", default-features = false, optional = true }
curve25519-dalek = { version = "4", optional = true }
proof-of-sql = { version = "0.15.0", default-features = false }
Expand All @@ -13,6 +16,4 @@ proof-of-sql-parser = { version = "0.15.0" }
proof-of-sql = { version = "0.15.0", default-features = false, features = ["test"] }

[features]
default = ["dory"]
dory = ["proof-of-sql/test"]
inner-product = ["dep:blitzar", "dep:curve25519-dalek", "proof-of-sql/blitzar"]
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
mod client;
mod error;
mod proof;

Check failure on line 3 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build-test-check

file not found for module `proof`
mod verification_key;
mod verify_generic;

#[cfg(feature = "dory")]
pub mod dory;
#[cfg(feature = "inner-product")]
pub mod inner_product;
Expand All @@ -11,7 +12,6 @@ pub use client::*;
pub use error::*;
pub use verify_generic::*;

#[cfg(feature = "dory")]
pub use dory::*;
#[cfg(feature = "inner-product")]
pub use inner_product::*;
52 changes: 52 additions & 0 deletions src/verification_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use ark_serialize::CanonicalDeserialize;
use proof_of_sql::proof_primitive::dory::{DoryVerifierPublicSetup, VerifierSetup};

use crate::VerifyError;

pub struct VerificationKey<const N: usize>(VerifierSetup);

impl<const N: usize> TryFrom<&[u8]> for VerificationKey<N> {
type Error = VerifyError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if value.len() != 15408 {
return Err(VerifyError::InvalidVerificationKey);
}

let setup: VerifierSetup = CanonicalDeserialize::deserialize_compressed(value)
.map_err(|_| VerifyError::InvalidVerificationKey)?;

Ok(Self(setup))
}
}

impl<const N: usize> VerificationKey<N> {
pub fn new(setup: VerifierSetup) -> Self {
Self(setup)
}

pub fn into_dory_verifier_public_setup(&self) -> DoryVerifierPublicSetup<'_> {
DoryVerifierPublicSetup::new(&self.0, N)
}
}

#[cfg(test)]
mod test {
use ark_serialize::CanonicalSerialize;
use proof_of_sql::proof_primitive::dory::{test_rng, PublicParameters};

use super::*;

#[test]
fn test_verification_key() {
let public_parameters = PublicParameters::rand(4, &mut test_rng());
let vs: VerifierSetup = VerifierSetup::from(&public_parameters);
let mut writer = Vec::new();
vs.serialize_compressed(&mut writer).unwrap();

let verification_key = VerificationKey::<4>::try_from(writer.as_ref()).unwrap();
let dory_key = verification_key.into_dory_verifier_public_setup();

assert_eq!(dory_key.verifier_setup(), &vs);
}
}
23 changes: 15 additions & 8 deletions src/verify_generic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use proof_of_sql::base::commitment::CommitmentEvaluationProof;
use proof_of_sql::sql::proof::ProofExpr;

use crate::error::VerifyError;

Expand All @@ -17,14 +18,20 @@ pub fn verify_proof<CP: CommitmentEvaluationProof>(
query_data: &QueryData<CP::Scalar>,
setup: &CP::VerifierPublicSetup<'_>,
) -> Result<(), VerifyError> {
// TODO check that the provided `commitments` contain all the necessary data.
// This should be possible by replicating the same logic as
// `proof_of_sql::base::commitment::query_commitments::QueryCommitments::from_accessor_with_max_bounds`
// If this check is not done, then the `verify` method could panic if the accessor tries to access
// data which does not exist inside teh `QueryCommitments` struct.

if commitments.is_empty() {
return Err(VerifyError::VerifyError);
// Check that the columns in the proof match the columns in the commitments
for column in expr.get_column_references() {
if let Some(commitment) = commitments.get(&column.table_ref()) {
if let Some(metadata) = commitment
.column_commitments()
.get_metadata(&column.column_id())
{
if metadata.column_type() != column.column_type() {
return Err(VerifyError::VerifyError);
}
}
} else {
return Err(VerifyError::VerifyError);
}
}

let result = proof
Expand Down
124 changes: 104 additions & 20 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(any(feature = "inner-product", feature = "dory"))]
pub mod common {
pub use proof_of_sql::{
base::{
Expand All @@ -25,7 +24,7 @@ pub mod common {
accessor
}

pub fn build_alternative_accessor<T: CommitmentEvaluationProof>(
pub fn build_altered_accessor<T: CommitmentEvaluationProof>(
setup: <T as CommitmentEvaluationProof>::ProverPublicSetup<'_>,
) -> OwnedTableTestAccessor<T> {
let mut accessor = OwnedTableTestAccessor::<T>::new_empty_with_setup(setup);
Expand All @@ -40,6 +39,21 @@ pub mod common {
accessor
}

pub fn build_alien_accessor<T: CommitmentEvaluationProof>(
setup: <T as CommitmentEvaluationProof>::ProverPublicSetup<'_>,
) -> OwnedTableTestAccessor<T> {
let mut accessor = OwnedTableTestAccessor::<T>::new_empty_with_setup(setup);
accessor.add_table(
"sxt.table2".parse().unwrap(),
owned_table([
bigint("c", [1, 2, 3, 2]),
varchar("d", ["hi", "hello", "there", "world"]),
]),
0,
);
accessor
}

pub fn build_query<T: Commitment>(accessor: &impl SchemaAccessor) -> QueryExpr<T> {
QueryExpr::try_new(
"SELECT b FROM table WHERE a = 2".parse().unwrap(),
Expand All @@ -49,7 +63,18 @@ pub mod common {
.unwrap()
}

pub fn build_missing_query<T: Commitment>(accessor: &impl SchemaAccessor) -> QueryExpr<T> {
pub fn build_alien_query<T: Commitment>(accessor: &impl SchemaAccessor) -> QueryExpr<T> {
QueryExpr::try_new(
"SELECT d FROM table2 WHERE c = 2".parse().unwrap(),
"sxt".parse().unwrap(),
accessor,
)
.unwrap()
}

pub fn build_query_non_existant_record<T: Commitment>(
accessor: &impl SchemaAccessor,
) -> QueryExpr<T> {
QueryExpr::try_new(
"SELECT b FROM table WHERE a = 4".parse().unwrap(),
"sxt".parse().unwrap(),
Expand Down Expand Up @@ -97,7 +122,6 @@ mod inner_product {
}
}

#[cfg(feature = "dory")]
mod dory {

use super::common::*;
Expand All @@ -111,26 +135,31 @@ mod dory {

#[test]
fn generate_and_verify_proof() {
// Initialize setup
let public_parameters = PublicParameters::rand(4, &mut test_rng());
let ps = ProverSetup::from(&public_parameters);
let vs = VerifierSetup::from(&public_parameters);
let prover_setup = DoryProverPublicSetup::new(&ps, 4);
let verifier_setup = DoryVerifierPublicSetup::new(&vs, 4);

let accessor: OwnedTableTestAccessor<DoryEvaluationProof> = build_accessor(prover_setup);
// Build table accessor and query
let accessor = build_accessor::<DoryEvaluationProof>(prover_setup);
let query = build_query(&accessor);

// Generate proof
let proof = VerifiableQueryResult::<DoryEvaluationProof>::new(
query.proof_expr(),
&accessor,
&prover_setup,
);

// Get query data and commitments
let query_data = proof
.verify(query.proof_expr(), &accessor, &verifier_setup)
.unwrap();
let query_commitments = proof_of_sql_verifier::compute_query_commitments(&query, &accessor);

// Verify proof
let result = proof_of_sql_verifier::verify_dory_proof(
proof,
query.proof_expr(),
Expand All @@ -143,65 +172,114 @@ mod dory {
}

#[test]
fn generate_and_verify_proof_missing_data() {
fn generate_and_verify_proof_for_non_existant_record() {
// Initialize setup
let public_parameters = PublicParameters::rand(4, &mut test_rng());
let ps = ProverSetup::from(&public_parameters);
let vs = VerifierSetup::from(&public_parameters);
let prover_setup = DoryProverPublicSetup::new(&ps, 4);
let verifier_setup = DoryVerifierPublicSetup::new(&vs, 4);

let accessor: OwnedTableTestAccessor<DoryEvaluationProof> = build_accessor(prover_setup);
let query = build_missing_query(&accessor);
// Build table accessor and query
let accessor = build_accessor::<DoryEvaluationProof>(prover_setup);
let non_existant_query = build_query_non_existant_record(&accessor);

let proof = VerifiableQueryResult::<DoryEvaluationProof>::new(
non_existant_query.proof_expr(),
&accessor,
&prover_setup,
);

let query_data = proof
.verify(non_existant_query.proof_expr(), &accessor, &verifier_setup)
.unwrap();
let query_commitments =
proof_of_sql_verifier::compute_query_commitments(&non_existant_query, &accessor);

let result = proof_of_sql_verifier::verify_dory_proof(
proof,
non_existant_query.proof_expr(),
&query_commitments,
&query_data,
&verifier_setup,
);

assert!(result.is_ok());
}

#[test]
fn generate_and_verify_proof_without_commitments() {
// Initialize setup
let public_parameters = PublicParameters::rand(4, &mut test_rng());
let ps = ProverSetup::from(&public_parameters);
let vs = VerifierSetup::from(&public_parameters);
let prover_setup = DoryProverPublicSetup::new(&ps, 4);
let verifier_setup = DoryVerifierPublicSetup::new(&vs, 4);

// Build table accessor and query
let accessor = build_accessor::<DoryEvaluationProof>(prover_setup);
let query = build_query(&accessor);

// Generate proof
let proof = VerifiableQueryResult::<DoryEvaluationProof>::new(
query.proof_expr(),
&accessor,
&prover_setup,
);

// Get query data and commitments
let query_data = proof
.verify(query.proof_expr(), &accessor, &verifier_setup)
.unwrap();
let query_commitments = proof_of_sql_verifier::compute_query_commitments(&query, &accessor);
let no_commitments = QueryCommitments::new();

let result = proof_of_sql_verifier::verify_dory_proof(
proof,
query.proof_expr(),
&query_commitments,
&no_commitments,
&query_data,
&verifier_setup,
);

assert!(result.is_ok());
assert!(result.is_err());
}

#[test]
fn generate_and_verify_proof_missing_commitments() {
fn generate_and_verify_proof_for_altered_data() {
// Initialize setup
let public_parameters = PublicParameters::rand(4, &mut test_rng());
let ps = ProverSetup::from(&public_parameters);
let vs = VerifierSetup::from(&public_parameters);
let prover_setup = DoryProverPublicSetup::new(&ps, 4);
let verifier_setup = DoryVerifierPublicSetup::new(&vs, 4);

let accessor: OwnedTableTestAccessor<DoryEvaluationProof> = build_accessor(prover_setup);
// Build table accessor and query
let accessor = build_accessor::<DoryEvaluationProof>(prover_setup);
let query = build_query(&accessor);

// Generate proof
let proof = VerifiableQueryResult::<DoryEvaluationProof>::new(
query.proof_expr(),
&accessor,
&prover_setup,
);

// Get query data and commitments
let query_data = proof
.verify(query.proof_expr(), &accessor, &verifier_setup)
.unwrap();

let query_commitments = QueryCommitments::new();
// Alter the data
let altered_accessor: OwnedTableTestAccessor<DoryEvaluationProof> =
build_altered_accessor(prover_setup);
let altered_query_commitments =
proof_of_sql_verifier::compute_query_commitments(&query, &altered_accessor);

// Verify proof
let result = proof_of_sql_verifier::verify_dory_proof(
proof,
query.proof_expr(),
&query_commitments,
&altered_query_commitments,
&query_data,
&verifier_setup,
);
Expand All @@ -210,29 +288,35 @@ mod dory {
}

#[test]
fn generate_and_verify_proof_alternative_data() {
fn generate_and_verify_proof_from_alien_accessor() {
// Initialize setup
let public_parameters = PublicParameters::rand(4, &mut test_rng());
let ps = ProverSetup::from(&public_parameters);
let vs = VerifierSetup::from(&public_parameters);
let prover_setup = DoryProverPublicSetup::new(&ps, 4);
let verifier_setup = DoryVerifierPublicSetup::new(&vs, 4);

let accessor: OwnedTableTestAccessor<DoryEvaluationProof> = build_accessor(prover_setup);
// Build table accessors and queries
let accessor = build_accessor::<DoryEvaluationProof>(prover_setup);
let alien_accessor = build_alien_accessor::<DoryEvaluationProof>(prover_setup);
let query = build_query(&accessor);
let alient_query = build_alien_query(&alien_accessor);

// Generate proof for original accessor and query
let proof = VerifiableQueryResult::<DoryEvaluationProof>::new(
query.proof_expr(),
&accessor,
&prover_setup,
);

// Get the result
let query_data = proof
.verify(query.proof_expr(), &accessor, &verifier_setup)
.unwrap();

let accessor: OwnedTableTestAccessor<DoryEvaluationProof> =
build_alternative_accessor(prover_setup);
let query_commitments = proof_of_sql_verifier::compute_query_commitments(&query, &accessor);
// Compute query commitments for alien accessor
let query_commitments =
proof_of_sql_verifier::compute_query_commitments(&alient_query, &alien_accessor);

let result = proof_of_sql_verifier::verify_dory_proof(
proof,
Expand Down

0 comments on commit e361586

Please sign in to comment.