Skip to content

Commit

Permalink
feat: verifier setup file io
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin-Ray committed Oct 22, 2024
1 parent 5f6b66f commit 3c13348
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
45 changes: 45 additions & 0 deletions crates/proof-of-sql/src/proof_primitive/dory/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Self> {
// 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 {
Expand Down
37 changes: 35 additions & 2 deletions crates/proof-of-sql/src/proof_primitive/dory/setup_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{fs, path::Path, time::Instant};

use super::{test_rng, ProverSetup, PublicParameters, VerifierSetup};
use ark_ec::pairing::Pairing;

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

0 comments on commit 3c13348

Please sign in to comment.