From 499c92be492a0c051c4c50f3a43a3b2f3f407dc0 Mon Sep 17 00:00:00 2001 From: "dustin.ray" Date: Fri, 4 Oct 2024 17:10:49 -0700 Subject: [PATCH 1/6] feat: read and write prover public setup parameters from file --- .../proof_primitive/dory/public_parameters.rs | 276 ++++++++++++++++++ 1 file changed, 276 insertions(+) diff --git a/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs b/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs index b1f8925c4..75aea3282 100644 --- a/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs +++ b/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs @@ -1,8 +1,16 @@ use super::{G1Affine, G2Affine}; use alloc::vec::Vec; use ark_ff::UniformRand; +use ark_serialize::{ + CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Valid, Validate, +}; use ark_std::rand::{CryptoRng, Rng}; use core::iter; +use std::{ + fs::File, + io::{BufReader, BufWriter, Read, Write}, + path::Path, +}; /// The public parameters for the Dory protocol. See section 5 of for details. /// @@ -50,4 +58,272 @@ impl PublicParameters { Gamma_2_fin, } } + + /// Function to save PublicParameters 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) + .expect("Failed to serialize PublicParameters"); + + // Write serialized bytes to the file + writer.write_all(&serialized_data)?; + writer.flush()?; + Ok(()) + } + + /// Function to load PublicParameters 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 + let params = PublicParameters::deserialize_with_mode( + &mut &serialized_data[..], + Compress::No, + Validate::Yes, + ) + .expect("Failed to deserialize PublicParameters"); + + Ok(params) + } +} + +impl CanonicalSerialize for PublicParameters { + fn serialize_with_mode( + &self, + mut writer: W, + compress: ark_serialize::Compress, + ) -> Result<(), SerializationError> { + // Serialize max_nu (usize as u64) first + let max_nu_u64 = self.max_nu as u64; + max_nu_u64.serialize_with_mode(&mut writer, compress)?; + + // Serialize Gamma_1 (Vec) + for g1 in &self.Gamma_1 { + g1.serialize_with_mode(&mut writer, compress)?; + } + + // Serialize Gamma_2 (Vec) + for g2 in &self.Gamma_2 { + g2.serialize_with_mode(&mut writer, compress)?; + } + + // Serialize H_1 (G1Affine) + self.H_1.serialize_with_mode(&mut writer, compress)?; + + // Serialize H_2 (G2Affine) + self.H_2.serialize_with_mode(&mut writer, compress)?; + + // Serialize Gamma_2_fin (G2Affine) + self.Gamma_2_fin + .serialize_with_mode(&mut writer, compress)?; + + Ok(()) + } + + // Update serialized_size accordingly + fn serialized_size(&self, compress: ark_serialize::Compress) -> usize { + let mut size = 0; + + // Size of max_nu + size += 8; // u64 is 8 bytes + + // Calculate size of Gamma_1 (Vec) + for g1 in &self.Gamma_1 { + size += g1.serialized_size(compress); + } + + // Calculate size of Gamma_2 (Vec) + for g2 in &self.Gamma_2 { + size += g2.serialized_size(compress); + } + + // Calculate size of H_1 (G1Affine) + size += self.H_1.serialized_size(compress); + + // Calculate size of H_2 (G2Affine) + size += self.H_2.serialized_size(compress); + + // Calculate size of Gamma_2_fin (G2Affine) + size += self.Gamma_2_fin.serialized_size(compress); + + size + } +} + +impl CanonicalDeserialize for PublicParameters { + fn deserialize_with_mode( + mut reader: R, + compress: ark_serialize::Compress, + validate: ark_serialize::Validate, + ) -> Result { + // Deserialize max_nu (u64) + let max_nu_u64 = u64::deserialize_with_mode(&mut reader, compress, validate)?; + let max_nu = max_nu_u64 as usize; + + // Deserialize Gamma_1 (Vec) + let mut Gamma_1 = Vec::with_capacity(1 << max_nu); + for _ in 0..(1 << max_nu) { + Gamma_1.push(G1Affine::deserialize_with_mode( + &mut reader, + compress, + validate, + )?); + } + + // Deserialize Gamma_2 (Vec) + let mut Gamma_2 = Vec::with_capacity(1 << max_nu); + for _ in 0..(1 << max_nu) { + Gamma_2.push(G2Affine::deserialize_with_mode( + &mut reader, + compress, + validate, + )?); + } + + // Deserialize H_1 (G1Affine) + let H_1 = G1Affine::deserialize_with_mode(&mut reader, compress, validate)?; + + // Deserialize H_2 (G2Affine) + let H_2 = G2Affine::deserialize_with_mode(&mut reader, compress, validate)?; + + // Deserialize Gamma_2_fin (G2Affine) + let Gamma_2_fin = G2Affine::deserialize_with_mode(&mut reader, compress, validate)?; + + Ok(Self { + Gamma_1, + Gamma_2, + H_1, + H_2, + Gamma_2_fin, + max_nu, + }) + } + + // Remove unnecessary methods if they're not overridden +} + +// Implement the Valid trait to perform validation on deserialized data +impl Valid for PublicParameters { + fn check(&self) -> Result<(), SerializationError> { + // Check that all G1Affine and G2Affine elements are valid + for g1 in &self.Gamma_1 { + g1.check()?; + } + for g2 in &self.Gamma_2 { + g2.check()?; + } + self.H_1.check()?; + self.H_2.check()?; + self.Gamma_2_fin.check()?; + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; + use ark_std::rand::thread_rng; + use std::io::Cursor; + + #[test] + fn we_can_serialize_and_deserialize_round_trip() { + // Create a random PublicParameters instance + let mut rng = thread_rng(); + let original_params = PublicParameters::rand(2, &mut rng); + + // Serialize the original parameters to a byte buffer + let mut serialized_data = Vec::new(); + original_params + .serialize_with_mode(&mut serialized_data, ark_serialize::Compress::No) + .expect("Failed to serialize PublicParameters"); + + // Deserialize the byte buffer back into a PublicParameters instance + let mut reader = Cursor::new(serialized_data); + let deserialized_params = PublicParameters::deserialize_with_mode( + &mut reader, + ark_serialize::Compress::No, + ark_serialize::Validate::Yes, + ) + .expect("Failed to deserialize PublicParameters"); + + // Check that the original and deserialized parameters are the same + assert_eq!(original_params.Gamma_1, deserialized_params.Gamma_1); + assert_eq!(original_params.Gamma_2, deserialized_params.Gamma_2); + assert_eq!(original_params.H_1, deserialized_params.H_1); + assert_eq!(original_params.H_2, deserialized_params.H_2); + assert_eq!(original_params.Gamma_2_fin, deserialized_params.Gamma_2_fin); + assert_eq!(original_params.max_nu, deserialized_params.max_nu); + + // Validate the deserialized parameters to ensure correctness + deserialized_params + .check() + .expect("Deserialized parameters are not valid"); + } + + // Observed proof size vs nu: + // nu = 4 | 0.005 MB | ▏ + // nu = 10 | 0.282 MB | ███▏ + // nu = 12 | 1.125 MB | █████████ + // nu = 14 | 4.500 MB | ████████████████████████████ + // nu = 15 | 9.000 MB | ██████████████████████████████████████████████ + #[test] + fn we_can_read_and_write_a_file_round_trip() { + let nu_values = vec![18]; + + // Loop through each nu value + for &nu in &nu_values { + dbg!("\nTesting with nu = {}", nu); + + let start_time = std::time::Instant::now(); + + // Create a random PublicParameters instance with the current nu value + let mut rng = thread_rng(); + let original_params = PublicParameters::rand(nu, &mut rng); + + // File path in the current working directory + let file_name = format!("public_params_{}.bin", nu); + let file_path = Path::new(&file_name); + + original_params + .save_to_file(file_path) + .expect("Failed to save PublicParameters to file"); + + // Load the PublicParameters from the file + let loaded_params = PublicParameters::load_from_file(file_path) + .expect("Failed to load PublicParameters from file"); + + // Check that the original and loaded parameters are identical + assert_eq!(original_params.Gamma_1, loaded_params.Gamma_1); + assert_eq!(original_params.Gamma_2, loaded_params.Gamma_2); + assert_eq!(original_params.H_1, loaded_params.H_1); + assert_eq!(original_params.H_2, loaded_params.H_2); + assert_eq!(original_params.Gamma_2_fin, loaded_params.Gamma_2_fin); + assert_eq!(original_params.max_nu, loaded_params.max_nu); + + // Record the file size in bytes + let metadata = std::fs::metadata(file_path).expect("Failed to get file metadata"); + let file_size = metadata.len(); // Get the file size in bytes + dbg!("File size for nu = {}: {} bytes", nu, file_size); + + // Record the time taken and print it + let elapsed_time = start_time.elapsed(); + dbg!("Time taken for nu = {}: {:?}", nu, elapsed_time); + + // Clean up the test file after the test runs + std::fs::remove_file(file_path).expect("Failed to remove test file"); + } + } } From 856b238a589121e6132b409f2207709980bcca9a Mon Sep 17 00:00:00 2001 From: "dustin.ray" Date: Fri, 4 Oct 2024 17:13:43 -0700 Subject: [PATCH 2/6] docs docs docs docs gate file generation behind test feature tests tests fix: file io and checks script --- .../proof_primitive/dory/public_parameters.rs | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs b/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs index 75aea3282..108d61b60 100644 --- a/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs +++ b/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs @@ -1,11 +1,13 @@ use super::{G1Affine, G2Affine}; use alloc::vec::Vec; use ark_ff::UniformRand; -use ark_serialize::{ - CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Valid, Validate, -}; +#[cfg(feature = "std")] +use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError, Valid}; +#[cfg(feature = "std")] +use ark_serialize::{Compress, Validate}; use ark_std::rand::{CryptoRng, Rng}; use core::iter; +#[cfg(feature = "std")] use std::{ fs::File, io::{BufReader, BufWriter, Read, Write}, @@ -58,27 +60,32 @@ impl PublicParameters { Gamma_2_fin, } } - - /// Function to save PublicParameters to a file in binary form + #[cfg(feature = "std")] + /// Function to save `PublicParameters` 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 + + use std::io; 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) - .expect("Failed to serialize PublicParameters"); + .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{e}")))?; // Write serialized bytes to the file writer.write_all(&serialized_data)?; writer.flush()?; Ok(()) } - - /// Function to load PublicParameters from a file in binary form + #[cfg(feature = "std")] + /// Function to load `PublicParameters` from a file in binary form pub fn load_from_file(path: &Path) -> std::io::Result { // Open the file at the specified path + + use std::io; + let file = File::open(path)?; let mut reader = BufReader::new(file); @@ -87,19 +94,18 @@ impl PublicParameters { reader.read_to_end(&mut serialized_data)?; // Deserialize the data into a PublicParameters instance - let params = PublicParameters::deserialize_with_mode( + PublicParameters::deserialize_with_mode( &mut &serialized_data[..], Compress::No, Validate::Yes, ) - .expect("Failed to deserialize PublicParameters"); - - Ok(params) + .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{e}"))) } } +#[cfg(feature = "std")] impl CanonicalSerialize for PublicParameters { - fn serialize_with_mode( + fn serialize_with_mode( &self, mut writer: W, compress: ark_serialize::Compress, @@ -161,8 +167,9 @@ impl CanonicalSerialize for PublicParameters { } } +#[cfg(feature = "std")] impl CanonicalDeserialize for PublicParameters { - fn deserialize_with_mode( + fn deserialize_with_mode( mut reader: R, compress: ark_serialize::Compress, validate: ark_serialize::Validate, @@ -213,6 +220,7 @@ impl CanonicalDeserialize for PublicParameters { // Remove unnecessary methods if they're not overridden } +#[cfg(feature = "std")] // Implement the Valid trait to perform validation on deserialized data impl Valid for PublicParameters { fn check(&self) -> Result<(), SerializationError> { @@ -232,6 +240,7 @@ impl Valid for PublicParameters { } #[cfg(test)] +#[cfg(feature = "std")] mod tests { use super::*; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; @@ -273,19 +282,20 @@ mod tests { .expect("Deserialized parameters are not valid"); } - // Observed proof size vs nu: - // nu = 4 | 0.005 MB | ▏ - // nu = 10 | 0.282 MB | ███▏ - // nu = 12 | 1.125 MB | █████████ - // nu = 14 | 4.500 MB | ████████████████████████████ - // nu = 15 | 9.000 MB | ██████████████████████████████████████████████ + // 13th Gen Intel® Core™ i9-13900H × 20 + // nu vs proof size & time: + // nu = 4 | 0.005 MB | 287.972567ms + // nu = 10 | 0.282 MB | 16.130250627s + // nu = 12 | 1.125 MB | 64.036526973s + // nu = 14 | 4.500 MB | 254.316791697s + // nu = 15 | 9.000 MB | 504.351756724s #[test] fn we_can_read_and_write_a_file_round_trip() { - let nu_values = vec![18]; + let nu_values = vec![1, 2, 4]; // Loop through each nu value for &nu in &nu_values { - dbg!("\nTesting with nu = {}", nu); + println!("\nTesting with nu = {nu}"); let start_time = std::time::Instant::now(); @@ -294,7 +304,7 @@ mod tests { let original_params = PublicParameters::rand(nu, &mut rng); // File path in the current working directory - let file_name = format!("public_params_{}.bin", nu); + let file_name = format!("public_params_{nu}.bin"); let file_path = Path::new(&file_name); original_params @@ -316,11 +326,11 @@ mod tests { // Record the file size in bytes let metadata = std::fs::metadata(file_path).expect("Failed to get file metadata"); let file_size = metadata.len(); // Get the file size in bytes - dbg!("File size for nu = {}: {} bytes", nu, file_size); + println!("File size for nu = {nu}: {file_size} bytes"); // Record the time taken and print it let elapsed_time = start_time.elapsed(); - dbg!("Time taken for nu = {}: {:?}", nu, elapsed_time); + println!("Time taken for nu = {nu}: {elapsed_time:?}"); // Clean up the test file after the test runs std::fs::remove_file(file_path).expect("Failed to remove test file"); From ce6c3a783c2d5a47d7d0d8db9b1ec725562e39f4 Mon Sep 17 00:00:00 2001 From: Dustin Ray <40841027+Dustin-Ray@users.noreply.github.com> Date: Sat, 5 Oct 2024 17:13:15 -0700 Subject: [PATCH 3/6] Update public_parameters.rs docs --- .../src/proof_primitive/dory/public_parameters.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs b/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs index 108d61b60..9258f7bf5 100644 --- a/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs +++ b/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs @@ -63,9 +63,8 @@ impl PublicParameters { #[cfg(feature = "std")] /// Function to save `PublicParameters` 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 - use std::io; + // Create or open the file at the specified path let file = File::create(path)?; let mut writer = BufWriter::new(file); @@ -82,10 +81,8 @@ impl PublicParameters { #[cfg(feature = "std")] /// Function to load `PublicParameters` from a file in binary form pub fn load_from_file(path: &Path) -> std::io::Result { - // Open the file at the specified path - use std::io; - + // Open the file at the specified path let file = File::open(path)?; let mut reader = BufReader::new(file); From a44db93efcfb277b6591d56a4d3ac2259d5c99ff Mon Sep 17 00:00:00 2001 From: "dustin.ray" Date: Mon, 7 Oct 2024 15:31:42 -0700 Subject: [PATCH 4/6] fix: move imports to head of file instead of local in function --- .../src/proof_primitive/dory/public_parameters.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs b/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs index 34a6acb74..e24321433 100644 --- a/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs +++ b/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs @@ -10,7 +10,7 @@ use core::iter; #[cfg(feature = "std")] use std::{ fs::File, - io::{BufReader, BufWriter, Read, Write}, + io::{BufReader, BufWriter, Error, ErrorKind, Read, Write}, path::Path, }; @@ -63,7 +63,6 @@ impl PublicParameters { #[cfg(feature = "std")] /// Function to save `PublicParameters` to a file in binary form pub fn save_to_file(&self, path: &Path) -> std::io::Result<()> { - use std::io; // Create or open the file at the specified path let file = File::create(path)?; let mut writer = BufWriter::new(file); @@ -71,7 +70,7 @@ impl PublicParameters { // 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| io::Error::new(io::ErrorKind::Other, format!("{e}")))?; + .map_err(|e| Error::new(ErrorKind::Other, format!("{e}")))?; // Write serialized bytes to the file writer.write_all(&serialized_data)?; @@ -81,7 +80,6 @@ impl PublicParameters { #[cfg(feature = "std")] /// Function to load `PublicParameters` from a file in binary form pub fn load_from_file(path: &Path) -> std::io::Result { - use std::io; // Open the file at the specified path let file = File::open(path)?; let mut reader = BufReader::new(file); @@ -96,7 +94,7 @@ impl PublicParameters { Compress::No, Validate::Yes, ) - .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{e}"))) + .map_err(|e| Error::new(ErrorKind::Other, format!("{e}"))) } } From 2987c651e95c711b85a267c47afa6faf0d05ed1d Mon Sep 17 00:00:00 2001 From: "dustin.ray" Date: Mon, 7 Oct 2024 15:32:45 -0700 Subject: [PATCH 5/6] refactor: imports --- .../src/proof_primitive/dory/public_parameters.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs b/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs index e24321433..e1d3a24c6 100644 --- a/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs +++ b/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs @@ -2,9 +2,9 @@ use super::{G1Affine, G2Affine}; use alloc::vec::Vec; use ark_ff::UniformRand; #[cfg(feature = "std")] -use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError, Valid}; -#[cfg(feature = "std")] -use ark_serialize::{Compress, Validate}; +use ark_serialize::{ + CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Valid, Validate, +}; use ark_std::rand::{CryptoRng, Rng}; use core::iter; #[cfg(feature = "std")] From 31a2eff509230d6d8e14f4f35821561269b72f84 Mon Sep 17 00:00:00 2001 From: "dustin.ray" Date: Mon, 7 Oct 2024 16:15:21 -0700 Subject: [PATCH 6/6] refactor: functional methods --- .../proof_primitive/dory/public_parameters.rs | 105 ++++++++---------- 1 file changed, 49 insertions(+), 56 deletions(-) diff --git a/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs b/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs index e1d3a24c6..604eedd75 100644 --- a/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs +++ b/crates/proof-of-sql/src/proof_primitive/dory/public_parameters.rs @@ -105,19 +105,18 @@ impl CanonicalSerialize for PublicParameters { mut writer: W, compress: ark_serialize::Compress, ) -> Result<(), SerializationError> { - // Serialize max_nu (usize as u64) first - let max_nu_u64 = self.max_nu as u64; - max_nu_u64.serialize_with_mode(&mut writer, compress)?; + // Serialize max_nu (usize as u64) + (self.max_nu as u64).serialize_with_mode(&mut writer, compress)?; // Serialize Gamma_1 (Vec) - for g1 in &self.Gamma_1 { - g1.serialize_with_mode(&mut writer, compress)?; - } + self.Gamma_1 + .iter() + .try_for_each(|g1| g1.serialize_with_mode(&mut writer, compress))?; // Serialize Gamma_2 (Vec) - for g2 in &self.Gamma_2 { - g2.serialize_with_mode(&mut writer, compress)?; - } + self.Gamma_2 + .iter() + .try_for_each(|g2| g2.serialize_with_mode(&mut writer, compress))?; // Serialize H_1 (G1Affine) self.H_1.serialize_with_mode(&mut writer, compress)?; @@ -134,31 +133,34 @@ impl CanonicalSerialize for PublicParameters { // Update serialized_size accordingly fn serialized_size(&self, compress: ark_serialize::Compress) -> usize { - let mut size = 0; - - // Size of max_nu - size += 8; // u64 is 8 bytes - - // Calculate size of Gamma_1 (Vec) - for g1 in &self.Gamma_1 { - size += g1.serialized_size(compress); - } - - // Calculate size of Gamma_2 (Vec) - for g2 in &self.Gamma_2 { - size += g2.serialized_size(compress); - } - - // Calculate size of H_1 (G1Affine) - size += self.H_1.serialized_size(compress); - - // Calculate size of H_2 (G2Affine) - size += self.H_2.serialized_size(compress); - - // Calculate size of Gamma_2_fin (G2Affine) - size += self.Gamma_2_fin.serialized_size(compress); - - size + // Size of max_nu (u64 is 8 bytes) + let max_nu_size = 8; + + // Size of Gamma_1 (Vec) + let gamma_1_size: usize = self + .Gamma_1 + .iter() + .map(|g1| g1.serialized_size(compress)) + .sum(); + + // Size of Gamma_2 (Vec) + let gamma_2_size: usize = self + .Gamma_2 + .iter() + .map(|g2| g2.serialized_size(compress)) + .sum(); + + // Size of H_1 (G1Affine) + let h1_size = self.H_1.serialized_size(compress); + + // Size of H_2 (G2Affine) + let h2_size = self.H_2.serialized_size(compress); + + // Size of Gamma_2_fin (G2Affine) + let gamma_2_fin_size = self.Gamma_2_fin.serialized_size(compress); + + // Sum everything to get the total size + max_nu_size + gamma_1_size + gamma_2_size + h1_size + h2_size + gamma_2_fin_size } } @@ -174,24 +176,14 @@ impl CanonicalDeserialize for PublicParameters { let max_nu = max_nu_u64 as usize; // Deserialize Gamma_1 (Vec) - let mut Gamma_1 = Vec::with_capacity(1 << max_nu); - for _ in 0..(1 << max_nu) { - Gamma_1.push(G1Affine::deserialize_with_mode( - &mut reader, - compress, - validate, - )?); - } + let Gamma_1: Vec = (0..(1 << max_nu)) + .map(|_| G1Affine::deserialize_with_mode(&mut reader, compress, validate)) + .collect::>()?; // Deserialize Gamma_2 (Vec) - let mut Gamma_2 = Vec::with_capacity(1 << max_nu); - for _ in 0..(1 << max_nu) { - Gamma_2.push(G2Affine::deserialize_with_mode( - &mut reader, - compress, - validate, - )?); - } + let Gamma_2: Vec = (0..(1 << max_nu)) + .map(|_| G2Affine::deserialize_with_mode(&mut reader, compress, validate)) + .collect::>()?; // Deserialize H_1 (G1Affine) let H_1 = G1Affine::deserialize_with_mode(&mut reader, compress, validate)?; @@ -220,12 +212,13 @@ impl CanonicalDeserialize for PublicParameters { impl Valid for PublicParameters { fn check(&self) -> Result<(), SerializationError> { // Check that all G1Affine and G2Affine elements are valid - for g1 in &self.Gamma_1 { - g1.check()?; - } - for g2 in &self.Gamma_2 { - g2.check()?; - } + self.Gamma_1 + .iter() + .try_for_each(ark_serialize::Valid::check)?; + self.Gamma_2 + .iter() + .try_for_each(ark_serialize::Valid::check)?; + self.H_1.check()?; self.H_2.check()?; self.Gamma_2_fin.check()?;