From d499c6d64915954c033d14dbd2ec937434b8b564 Mon Sep 17 00:00:00 2001 From: lgiussan <36673031+lgiussan@users.noreply.github.com> Date: Mon, 21 Oct 2024 11:32:51 +0200 Subject: [PATCH] Switch to unchecked vk deserialization (#5) * Add unchecked deserialization for vk * Make max_nu configurable in generate-sample-proof binary --- .gitignore | 4 +-- Cargo.toml | 4 ++- Makefile.toml | 2 +- src/bin/generate-sample-proof.rs | 33 ++++++++++++++++----- src/verification_key.rs | 49 +++++++++++++++++++------------- 5 files changed, 61 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 6f8c7b7..2ba01ca 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,4 @@ Cargo.lock lcov.info # Outputs of generate-sample-proof binary -proof.bin -pubs.bin -vk.bin \ No newline at end of file +*.bin diff --git a/Cargo.toml b/Cargo.toml index 3bd8283..5756dad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ ark-bls12-381 = { version = "0.4.0", default-features = false } ark-ec = { version = "0.4.0", default-features = false } ark-serialize = { version = "0.4.0", default-features = false } ciborium = { version = "0.2.2", default-features = false } +clap = { version = "4.5.20", optional = true, features = ["derive"] } indexmap = { version = "2.1", default-features = false, features = ["serde"] } proof-of-sql = { version = "0.28.6", default-features = false } proof-of-sql-parser = { version = "0.28.6", default-features = false } @@ -40,7 +41,8 @@ test = [ "proof-of-sql/test" ] rand = ["dep:rand"] +clap = ["dep:clap"] [[bin]] name = "generate-sample-proof" -required-features = ["rand", "test"] +required-features = ["rand", "test", "clap"] diff --git a/Makefile.toml b/Makefile.toml index 2868a0d..5af2b71 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -37,7 +37,7 @@ dependencies = ["run-generate-sample-proof"] [tasks.run-generate-sample-proof] command = "cargo" -args = ["run", "--bin", "generate-sample-proof", "--features", "rand test"] +args = ["run", "--bin", "generate-sample-proof", "--features", "rand test clap", "--", "--max-nu=1"] [tasks.format_inst] [tasks.format-inst] diff --git a/src/bin/generate-sample-proof.rs b/src/bin/generate-sample-proof.rs index f90a783..32ffe1b 100644 --- a/src/bin/generate-sample-proof.rs +++ b/src/bin/generate-sample-proof.rs @@ -16,6 +16,7 @@ use std::fs::File; use std::io::prelude::*; +use clap::Parser; use proof_of_sql::base::commitment::QueryCommitments; use proof_of_sql::proof_primitive::dory::{ DoryEvaluationProof, DoryProverPublicSetup, DoryVerifierPublicSetup, ProverSetup, @@ -34,9 +35,19 @@ pub use proof_of_sql::{ use proof_of_sql_verifier::{Proof, PublicInput, VerificationKey}; use rand::thread_rng; +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + /// Value of `max_nu` + #[arg(long)] + max_nu: usize, +} + fn main() { + let args = Args::parse(); + // Initialize setup - let max_nu = 4; + let max_nu = args.max_nu; let sigma = max_nu; let public_parameters = PublicParameters::rand(max_nu, &mut thread_rng()); let ps = ProverSetup::from(&public_parameters); @@ -50,14 +61,22 @@ fn main() { accessor.add_table( "sxt.table".parse().unwrap(), owned_table([ - bigint("a", [1, 2, 3, 2]), - varchar("b", ["hi", "hello", "there", "world"]), + bigint("a", [1, 2]), + varchar("b", ["hello", "bye"]), + varchar("c", ["foo", "bar"]), + varchar("d", ["dc", "marvel"]), + varchar("e", ["hide", "seek"]), + varchar("f", ["yin", "yang"]), + varchar("g", ["chip", "dale"]), + varchar("h", ["vim ", "emacs"]), ]), 0, ); let query = QueryExpr::try_new( - "SELECT b FROM table WHERE a = 2".parse().unwrap(), + "SELECT a, b, c, d, e, f, g, h FROM table WHERE a = 2" + .parse() + .unwrap(), "sxt".parse().unwrap(), &accessor, ) @@ -85,10 +104,10 @@ fn main() { let _result = proof_of_sql_verifier::verify_proof(&proof, &pubs, &vk); // Write proof, pubs, and vk to binary files - let mut proof_bin = File::create("proof.bin").unwrap(); + let mut proof_bin = File::create(format!("VALID_PROOF_MAX_NU_{}.bin", max_nu)).unwrap(); proof_bin.write_all(&proof.to_bytes()).unwrap(); - let mut pubs_bin = File::create("pubs.bin").unwrap(); + let mut pubs_bin = File::create(format!("VALID_PUBS_MAX_NU_{}.bin", max_nu)).unwrap(); pubs_bin.write_all(&pubs.try_to_bytes().unwrap()).unwrap(); - let mut vk_bin = File::create("vk.bin").unwrap(); + let mut vk_bin = File::create(format!("VALID_VK_MAX_NU_{}.bin", max_nu)).unwrap(); vk_bin.write_all(&vk.to_bytes()).unwrap(); } diff --git a/src/verification_key.rs b/src/verification_key.rs index c9db236..3418f49 100644 --- a/src/verification_key.rs +++ b/src/verification_key.rs @@ -35,39 +35,49 @@ pub struct VerificationKey { sigma: usize, } -impl TryFrom<&[u8]> for VerificationKey { - type Error = VerifyError; +impl VerificationKey { + /// Creates a new VerificationKey from PublicParameters. + /// + /// # Arguments + /// + /// * `params` - A reference to PublicParameters. + /// + /// # Returns + /// + /// A new VerificationKey instance. + pub fn new(params: &PublicParameters, sigma: usize) -> Self { + Self { + setup: VerifierSetup::from(params), + sigma, + } + } /// Attempts to create a VerificationKey from a byte slice. /// /// # Arguments /// - /// * `value` - The byte slice containing the serialized verification key. + /// * `bytes` - The byte slice containing the serialized verification key. /// /// # Returns /// /// * `Result` - A VerificationKey if deserialization succeeds, or a VerifyError if it fails. - fn try_from(value: &[u8]) -> Result { - VerificationKey::deserialize_compressed(value) + pub fn try_from_bytes(bytes: &[u8]) -> Result { + VerificationKey::deserialize_compressed(bytes) .map_err(|_| VerifyError::InvalidVerificationKey) } -} -impl VerificationKey { - /// Creates a new VerificationKey from PublicParameters. + /// Attempts to create a VerificationKey from a byte slice. Skips checking if points lie on the curve. /// /// # Arguments /// - /// * `params` - A reference to PublicParameters. + /// * `bytes` - The byte slice containing the serialized verification key. /// /// # Returns /// - /// A new VerificationKey instance. - pub fn new(params: &PublicParameters, sigma: usize) -> Self { - Self { - setup: VerifierSetup::from(params), - sigma, - } + /// * `Result` - A VerificationKey if deserialization succeeds, or a VerifyError if it fails. + pub fn try_from_bytes_unchecked(bytes: &[u8]) -> Result { + VerificationKey::deserialize_compressed_unchecked(bytes) + .map_err(|_| VerifyError::InvalidVerificationKey) } /// Converts the verification key into a byte array. @@ -96,11 +106,11 @@ impl VerificationKey { /// /// The size in bytes of the serialized VerificationKey. pub fn serialized_size(max_nu: usize) -> usize { - 5 * (size_of::() + (max_nu + 1) * GT_SERIALIZED_SIZE) // Delta_1L, Delta_1R, Delta_2L, Delta_2R, chi + 5 * (size_of::() + (max_nu + 1) * GT_SERIALIZED_SIZE) // Delta_1L, Delta_1R, Delta_2L, Delta_2R, chi + 2 * G1_AFFINE_SERIALIZED_SIZE// Gamma_1_0, H_1 + 3 * G2_AFFINE_SERIALIZED_SIZE // Gamma_2_0, H_2, Gamma_2_fin + GT_SERIALIZED_SIZE // H_T - + 2 * size_of::() // max_nu, sigma + + 2 * size_of::() // max_nu, sigma } } @@ -118,7 +128,7 @@ mod test { let public_parameters = PublicParameters::test_rand(4, &mut test_rng()); let vk = VerificationKey::new(&public_parameters, 1); let serialized_vk = vk.to_bytes(); - let deserialized_vk = VerificationKey::try_from(serialized_vk.as_slice()).unwrap(); + let deserialized_vk = VerificationKey::try_from_bytes(serialized_vk.as_slice()).unwrap(); let dory_key = deserialized_vk.to_dory(); assert_eq!(dory_key.verifier_setup(), &vk.setup); @@ -129,7 +139,8 @@ mod test { let public_parameters = PublicParameters::test_rand(4, &mut test_rng()); let vk = VerificationKey::new(&public_parameters, 1); let serialized_vk = vk.to_bytes(); - let deserialized_vk = VerificationKey::try_from(&serialized_vk[..serialized_vk.len() - 1]); + let deserialized_vk = + VerificationKey::try_from_bytes(&serialized_vk[..serialized_vk.len() - 1]); assert!(deserialized_vk.is_err()); }