From 3c13348b215c8eceba34a801b2bc06dc8b50cb67 Mon Sep 17 00:00:00 2001 From: "dustin.ray" Date: Tue, 22 Oct 2024 10:12:50 -0700 Subject: [PATCH] feat: verifier setup file io --- .../src/proof_primitive/dory/setup.rs | 45 +++++++++++++++++++ .../src/proof_primitive/dory/setup_test.rs | 37 ++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/crates/proof-of-sql/src/proof_primitive/dory/setup.rs b/crates/proof-of-sql/src/proof_primitive/dory/setup.rs index 4283fa7d5..59150deb4 100644 --- a/crates/proof-of-sql/src/proof_primitive/dory/setup.rs +++ b/crates/proof-of-sql/src/proof_primitive/dory/setup.rs @@ -3,8 +3,15 @@ use crate::base::impl_serde_for_ark_serde_unchecked; use alloc::vec::Vec; use ark_ec::pairing::{Pairing, PairingOutput}; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; +use ark_serialize::{Compress, Validate}; use itertools::MultiUnzip; use num_traits::One; +#[cfg(feature = "std")] +use std::{ + fs::File, + io::{BufReader, BufWriter, Error, ErrorKind, Read, Write}, + path::Path, +}; /// The transparent setup information that the prover must know to create a proof. /// This is public knowledge and must match with the verifier's setup information. @@ -212,6 +219,44 @@ impl VerifierSetup { max_nu, } } + + #[must_use] + #[cfg(feature = "std")] + /// Function to save `VerifierSetup` to a file in binary form + pub fn save_to_file(&self, path: &Path) -> std::io::Result<()> { + // Create or open the file at the specified path + + let file = File::create(path)?; + let mut writer = BufWriter::new(file); + + // Serialize the PublicParameters struct into the file + let mut serialized_data = Vec::new(); + self.serialize_with_mode(&mut serialized_data, Compress::No) + .map_err(|e| Error::new(ErrorKind::Other, format!("{e}")))?; + + // Write serialized bytes to the file + writer.write_all(&serialized_data)?; + writer.flush()?; + Ok(()) + } + + #[must_use] + #[cfg(feature = "std")] + /// Function to load `VerifierSetup` from a file in binary form + pub fn load_from_file(path: &Path) -> std::io::Result { + // Open the file at the specified path + + let file = File::open(path)?; + let mut reader = BufReader::new(file); + + // Read the serialized data from the file + let mut serialized_data = Vec::new(); + reader.read_to_end(&mut serialized_data)?; + + // Deserialize the data into a PublicParameters instance + Self::deserialize_with_mode(&mut &serialized_data[..], Compress::No, Validate::Yes) + .map_err(|e| Error::new(ErrorKind::Other, format!("{e}"))) + } } impl From<&PublicParameters> for VerifierSetup { diff --git a/crates/proof-of-sql/src/proof_primitive/dory/setup_test.rs b/crates/proof-of-sql/src/proof_primitive/dory/setup_test.rs index 79ebc9d70..e03a77e1a 100644 --- a/crates/proof-of-sql/src/proof_primitive/dory/setup_test.rs +++ b/crates/proof-of-sql/src/proof_primitive/dory/setup_test.rs @@ -1,3 +1,5 @@ +use std::{fs, path::Path, time::Instant}; + use super::{test_rng, ProverSetup, PublicParameters, VerifierSetup}; use ark_ec::pairing::Pairing; @@ -21,10 +23,16 @@ fn we_can_create_and_manually_check_a_small_prover_setup() { } #[test] -fn we_can_create_and_manually_check_a_small_verifier_setup() { +fn we_can_create_save_load_and_manually_check_a_small_verifier_setup() { let mut rng = test_rng(); let pp = PublicParameters::test_rand(2, &mut rng); - let setup = VerifierSetup::from(&pp); + let v_setup = VerifierSetup::from(&pp); + + v_setup.save_to_file(Path::new("setup.bin")).unwrap(); + let setup = VerifierSetup::load_from_file(Path::new("setup.bin")); + assert!(setup.is_ok()); + + let setup = setup.unwrap(); assert_eq!(setup.max_nu, 2); assert_eq!(setup.Delta_1L.len(), 3); assert_eq!(setup.Delta_1R.len(), 3); @@ -81,6 +89,8 @@ fn we_can_create_and_manually_check_a_small_verifier_setup() { assert_eq!(setup.H_2, pp.H_2); assert_eq!(setup.H_T, Pairing::pairing(pp.H_1, pp.H_2)); assert_eq!(setup.Gamma_2_fin, pp.Gamma_2_fin); + + fs::remove_file(Path::new("setup.bin")).unwrap(); } #[test] @@ -145,3 +155,26 @@ fn we_can_serialize_and_deserialize_verifier_setups() { assert_eq!(setup, deserialized); } } + +#[test] +fn we_can_measure_size_of_various_verifier_setups() { + for i in 1..=4 { + let mut rng = test_rng(); + let pp = PublicParameters::test_rand(i, &mut rng); + + let setup_start = Instant::now(); + let v_setup = VerifierSetup::from(&pp); + let setup_elapsed = Instant::elapsed(&setup_start); + + println!( + "Created verifier setup with size {:?} in {:?}", + i, setup_elapsed + ); + + v_setup.save_to_file(Path::new("setup.bin")).unwrap(); + let setup = VerifierSetup::load_from_file(Path::new("setup.bin")); + assert!(setup.is_ok()); + + fs::remove_file(Path::new("setup.bin")).unwrap(); + } +}