Skip to content

Commit

Permalink
Switch to unchecked vk deserialization (#5)
Browse files Browse the repository at this point in the history
* Add unchecked deserialization for vk

* Make max_nu configurable in generate-sample-proof binary
  • Loading branch information
lgiussan authored Oct 21, 2024
1 parent 1d0bada commit d499c6d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 31 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ Cargo.lock
lcov.info

# Outputs of generate-sample-proof binary
proof.bin
pubs.bin
vk.bin
*.bin
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
33 changes: 26 additions & 7 deletions src/bin/generate-sample-proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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,
)
Expand Down Expand Up @@ -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();
}
49 changes: 30 additions & 19 deletions src/verification_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Error>` - A VerificationKey if deserialization succeeds, or a VerifyError if it fails.
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
VerificationKey::deserialize_compressed(value)
pub fn try_from_bytes(bytes: &[u8]) -> Result<Self, VerifyError> {
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<Self, Self::Error>` - A VerificationKey if deserialization succeeds, or a VerifyError if it fails.
pub fn try_from_bytes_unchecked(bytes: &[u8]) -> Result<Self, VerifyError> {
VerificationKey::deserialize_compressed_unchecked(bytes)
.map_err(|_| VerifyError::InvalidVerificationKey)
}

/// Converts the verification key into a byte array.
Expand Down Expand Up @@ -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::<usize>() + (max_nu + 1) * GT_SERIALIZED_SIZE) // Delta_1L, Delta_1R, Delta_2L, Delta_2R, chi
5 * (size_of::<u64>() + (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::<usize>() // max_nu, sigma
+ 2 * size_of::<u64>() // max_nu, sigma
}
}

Expand All @@ -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);
Expand All @@ -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());
}

Expand Down

0 comments on commit d499c6d

Please sign in to comment.