Skip to content

Commit

Permalink
add coments
Browse files Browse the repository at this point in the history
  • Loading branch information
tarassh committed Aug 21, 2024
1 parent d26e12e commit 535b68a
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/dory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ use crate::{
verify_generic::verify_proof, DoryProof, DoryPublicInput, VerificationKey, VerifyError,
};

/// Verifies a Dory proof against the provided public input and verification key.
///
/// # Arguments
///
/// * `proof` - The Dory proof to be verified.
/// * `pubs` - The public input for the proof.
/// * `vk` - The verification key used to verify the proof.
///
/// # Type Parameters
///
/// * `N` - A const generic parameter, likely related to the size of the verification key.
///
/// # Returns
///
/// * `Result<(), VerifyError>` - Ok(()) if the proof is valid, or an error if verification fails.
pub fn verify_dory_proof<const N: usize>(
proof: &DoryProof,
pubs: &DoryPublicInput,
Expand Down
12 changes: 12 additions & 0 deletions src/inner_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ pub use blitzar::proof::InnerProductProof;
pub use curve25519_dalek::RistrettoPoint;
pub use proof_of_sql::base::scalar::Curve25519Scalar;

/// Verifies an inner product proof against the provided expression, commitments, and query data.
///
/// # Arguments
///
/// * `proof` - The inner product proof to be verified, wrapped in a VerifiableQueryResult.
/// * `expr` - The proof plan expression.
/// * `commitments` - The query commitments.
/// * `query_data` - The query data.
///
/// # Returns
///
/// * `Result<(), VerifyError>` - Ok(()) if the proof is valid, or an error if verification fails.
pub fn verify_inner_product_proof(
proof: VerifiableQueryResult<InnerProductProof>,
expr: &ProofPlan<RistrettoPoint>,
Expand Down
32 changes: 32 additions & 0 deletions src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ use proof_of_sql::sql::proof::VerifiableQueryResult;

use crate::VerifyError;

/// Represents a Dory proof.
///
/// `DoryProof` is a wrapper around a `VerifiableQueryResult<DoryEvaluationProof>`.
/// It provides methods for creating, serializing, and deserializing Dory proofs.
///
/// # Fields
///
/// * `proof` - A `VerifiableQueryResult<DoryEvaluationProof>` containing the actual proof data.
#[derive(Clone)]
pub struct DoryProof {
proof: VerifiableQueryResult<DoryEvaluationProof>,
Expand All @@ -26,6 +34,15 @@ pub struct DoryProof {
impl TryFrom<&[u8]> for DoryProof {
type Error = VerifyError;

/// Attempts to create a DoryProof from a byte slice.
///
/// # Arguments
///
/// * `value` - The byte slice containing the serialized proof.
///
/// # Returns
///
/// * `Result<Self, Self::Error>` - A DoryProof if deserialization succeeds, or a VerifyError if it fails.
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let proof = bincode::deserialize(value).map_err(|_| VerifyError::InvalidProofData)?;

Expand All @@ -34,16 +51,31 @@ impl TryFrom<&[u8]> for DoryProof {
}

impl Into<VerifiableQueryResult<DoryEvaluationProof>> for DoryProof {
/// Converts the DoryProof into a VerifiableQueryResult<DoryEvaluationProof>.
fn into(self) -> VerifiableQueryResult<DoryEvaluationProof> {
self.proof
}
}

impl DoryProof {
/// Creates a new DoryProof.
///
/// # Arguments
///
/// * `proof` - A VerifiableQueryResult containing a DoryEvaluationProof.
///
/// # Returns
///
/// * `Self` - A new DoryProof instance.
pub fn new(proof: VerifiableQueryResult<DoryEvaluationProof>) -> Self {
Self { proof }
}

/// Converts the DoryProof into a byte vector.
///
/// # Returns
///
/// * `Vec<u8>` - The serialized proof as a byte vector.
pub fn into_bytes(self) -> Vec<u8> {
bincode::serialize(&self.proof).unwrap()
}
Expand Down
23 changes: 23 additions & 0 deletions src/pubs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,33 @@ use proof_of_sql::{
sql::{ast::ProofPlan, parse::QueryExpr, proof::QueryData},
};

/// Represents the public input for a Dory proof.
///
/// This structure encapsulates the necessary public information required
/// for verifying a Dory proof, including the proof expression, commitments,
/// and query data.
///
/// # Type Parameters
///
/// * `'a` - The lifetime of the referenced `ProofPlan`.
pub struct DoryPublicInput<'a> {
expr: &'a ProofPlan<DoryCommitment>,
commitments: QueryCommitments<DoryCommitment>,
query_data: QueryData<DoryScalar>,
}

impl<'a> DoryPublicInput<'a> {
/// Creates a new `DoryPublicInput` instance.
///
/// # Arguments
///
/// * `query_expr` - A reference to the query expression.
/// * `commitments` - The query commitments.
/// * `query_data` - The query data.
///
/// # Returns
///
/// A new `DoryPublicInput` instance.
pub fn new(
query_expr: &'a QueryExpr<DoryCommitment>,
commitments: QueryCommitments<DoryCommitment>,
Expand All @@ -38,14 +58,17 @@ impl<'a> DoryPublicInput<'a> {
}
}

/// Returns a reference to the proof expression.
pub fn expr(&self) -> &ProofPlan<DoryCommitment> {
&self.expr
}

/// Returns a reference to the query commitments.
pub fn commitments(&self) -> &QueryCommitments<DoryCommitment> {
&self.commitments
}

/// Returns a reference to the query data.
pub fn query_data(&self) -> &QueryData<DoryScalar> {
&self.query_data
}
Expand Down
40 changes: 40 additions & 0 deletions src/verification_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,28 @@ use proof_of_sql::proof_primitive::dory::{

use crate::VerifyError;

/// Represents a verification key for Dory proofs.
///
/// This structure wraps a `VerifierSetup` and provides methods for
/// creating, deserializing, and converting the verification key.
///
/// # Type Parameters
///
/// * `N` - A const generic parameter representing the size of the verification key.
pub struct VerificationKey<const N: usize>(VerifierSetup);

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

/// Attempts to create a VerificationKey from a byte slice.
///
/// # Arguments
///
/// * `value` - The byte slice containing the serialized verification key.
///
/// # Returns
///
/// * `Result<Self, Self::Error>` - A VerificationKey if deserialization succeeds, or a VerifyError if it fails.
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let setup = VerifierSetup::deserialize_compressed(value)
.map_err(|_| VerifyError::InvalidVerificationKey)?;
Expand All @@ -41,15 +58,38 @@ impl<const N: usize> TryFrom<&[u8]> for VerificationKey<N> {
}

impl<const N: usize> VerificationKey<N> {
/// Creates a new VerificationKey from PublicParameters.
///
/// # Arguments
///
/// * `params` - A reference to PublicParameters.
///
/// # Returns
///
/// A new VerificationKey instance.
pub fn new(params: &PublicParameters) -> Self {
Self(VerifierSetup::from(params))
}

/// Converts the VerificationKey into a DoryVerifierPublicSetup.
///
/// # Returns
///
/// A DoryVerifierPublicSetup instance.
pub fn into_dory(&self) -> DoryVerifierPublicSetup<'_> {
DoryVerifierPublicSetup::new(&self.0, N)
}
}

/// Converts a byte slice to a usize.
///
/// # Arguments
///
/// * `slice` - The byte slice to convert.
///
/// # Returns
///
/// The usize value represented by the byte slice.
fn slice_to_usize(slice: &[u8]) -> usize {
let mut array = [0u8; std::mem::size_of::<usize>()];
let len = slice.len().min(std::mem::size_of::<usize>());
Expand Down
17 changes: 17 additions & 0 deletions src/verify_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ use proof_of_sql::{

use crate::VerifyError;

/// Verifies a generic proof against the provided expression, commitments, and query data.
///
/// # Type Parameters
///
/// * `CP` - A type that implements `CommitmentEvaluationProof`.
///
/// # Arguments
///
/// * `proof` - The proof to be verified, wrapped in a `VerifiableQueryResult`.
/// * `expr` - The proof plan expression.
/// * `commitments` - The query commitments.
/// * `query_data` - The query data.
/// * `setup` - The verifier's public setup.
///
/// # Returns
///
/// * `Result<(), VerifyError>` - Ok(()) if the proof is valid, or an error if verification fails.
pub fn verify_proof<CP: CommitmentEvaluationProof>(
proof: VerifiableQueryResult<CP>,
expr: &ProofPlan<CP::Commitment>,
Expand Down
16 changes: 15 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub use proof_of_sql::{
sql::{parse::QueryExpr, proof::ProofExpr, proof::VerifiableQueryResult},
};

// Helper functions for setting up test data and queries

/// Computes query commitments for a given query expression and accessor.
fn compute_query_commitments<C: Commitment>(
query_expr: &QueryExpr<C>,
accessor: &(impl CommitmentAccessor<C> + SchemaAccessor),
Expand All @@ -30,6 +33,7 @@ fn compute_query_commitments<C: Commitment>(
QueryCommitments::from_accessor_with_max_bounds(columns, accessor)
}

/// Builds a test accessor with sample data.
fn build_accessor<T: CommitmentEvaluationProof>(
setup: <T as CommitmentEvaluationProof>::ProverPublicSetup<'_>,
) -> OwnedTableTestAccessor<T> {
Expand All @@ -45,6 +49,7 @@ fn build_accessor<T: CommitmentEvaluationProof>(
accessor
}

/// Builds a test accessor with altered sample data.
fn build_altered_accessor<T: CommitmentEvaluationProof>(
setup: <T as CommitmentEvaluationProof>::ProverPublicSetup<'_>,
) -> OwnedTableTestAccessor<T> {
Expand All @@ -60,6 +65,7 @@ fn build_altered_accessor<T: CommitmentEvaluationProof>(
accessor
}

/// Builds a test accessor with different table and column names.
fn build_alien_accessor<T: CommitmentEvaluationProof>(
setup: <T as CommitmentEvaluationProof>::ProverPublicSetup<'_>,
) -> OwnedTableTestAccessor<T> {
Expand All @@ -75,6 +81,7 @@ fn build_alien_accessor<T: CommitmentEvaluationProof>(
accessor
}

/// Builds a sample query for testing.
fn build_query<T: Commitment>(accessor: &impl SchemaAccessor) -> QueryExpr<T> {
QueryExpr::try_new(
"SELECT b FROM table WHERE a = 2".parse().unwrap(),
Expand All @@ -84,6 +91,7 @@ fn build_query<T: Commitment>(accessor: &impl SchemaAccessor) -> QueryExpr<T> {
.unwrap()
}

/// Builds a sample query for the "alien" accessor.
fn build_alien_query<T: Commitment>(accessor: &impl SchemaAccessor) -> QueryExpr<T> {
QueryExpr::try_new(
"SELECT d FROM table2 WHERE c = 2".parse().unwrap(),
Expand All @@ -93,6 +101,7 @@ fn build_alien_query<T: Commitment>(accessor: &impl SchemaAccessor) -> QueryExpr
.unwrap()
}

/// Builds a query for a non-existent record.
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(),
Expand All @@ -108,6 +117,7 @@ mod inner_product {

use blitzar::{self, proof::InnerProductProof};

/// Tests the generation and verification of an inner product proof.
#[test]
fn generate_and_verify_proof() {
blitzar::compute::init_backend();
Expand Down Expand Up @@ -141,7 +151,6 @@ mod inner_product {
}

mod dory {

use super::*;

use proof_of_sql::proof_primitive::dory::{
Expand All @@ -151,6 +160,7 @@ mod dory {
use proof_of_sql::base::commitment::QueryCommitments;
use proof_of_sql_verifier::{DoryProof, DoryPublicInput, VerificationKey};

/// Tests the generation and verification of a Dory proof.
#[test]
fn generate_and_verify_proof() {
// Initialize setup
Expand Down Expand Up @@ -184,6 +194,7 @@ mod dory {
assert!(result.is_ok());
}

/// Tests the generation and verification of a Dory proof for a non-existent record.
#[test]
fn generate_and_verify_proof_for_non_existant_record() {
// Initialize setup
Expand Down Expand Up @@ -215,6 +226,7 @@ mod dory {
assert!(result.is_ok());
}

/// Tests that verification fails when commitments are missing.
#[test]
fn generate_and_verify_proof_without_commitments() {
// Initialize setup
Expand Down Expand Up @@ -247,6 +259,7 @@ mod dory {
assert!(result.is_err());
}

/// Tests that verification fails when the underlying data has been altered.
#[test]
fn generate_and_verify_proof_for_altered_data() {
// Initialize setup
Expand Down Expand Up @@ -284,6 +297,7 @@ mod dory {
assert!(result.is_err());
}

/// Tests that verification fails when using commitments from a different accessor.
#[test]
fn generate_and_verify_proof_from_alien_accessor() {
// Initialize setup
Expand Down

0 comments on commit 535b68a

Please sign in to comment.