diff --git a/halo2_gadgets/src/ecc/chip/mul.rs b/halo2_gadgets/src/ecc/chip/mul.rs index e4e2004882..9cf8391dc5 100644 --- a/halo2_gadgets/src/ecc/chip/mul.rs +++ b/halo2_gadgets/src/ecc/chip/mul.rs @@ -460,22 +460,20 @@ pub mod tests { ff::{Field, PrimeField}, Curve, }; - use halo2_proofs::circuit::Chip; use halo2_proofs::{ - circuit::{Layouter, Value}, + circuit::{Chip, Layouter, Value}, plonk::Error, }; use pasta_curves::pallas; use rand::rngs::OsRng; - use crate::utilities::lookup_range_check::PallasLookupRC; use crate::{ ecc::{ chip::{EccChip, EccPoint}, tests::TestFixedBases, EccInstructions, NonIdentityPoint, Point, ScalarVar, }, - utilities::UtilitiesInstructions, + utilities::{lookup_range_check::PallasLookupRC, UtilitiesInstructions}, }; pub(crate) fn test_mul( diff --git a/halo2_gadgets/src/sinsemilla/chip/hash_to_point.rs b/halo2_gadgets/src/sinsemilla/chip/hash_to_point.rs index 709da2455d..1470b33a5f 100644 --- a/halo2_gadgets/src/sinsemilla/chip/hash_to_point.rs +++ b/halo2_gadgets/src/sinsemilla/chip/hash_to_point.rs @@ -17,7 +17,7 @@ use pasta_curves::{arithmetic::CurveAffine, pallas}; use std::ops::Deref; -/// Define an enum that can hold either type +/// Define an enum that can hold either a public or a private ECC Point #[derive(Debug, Clone)] #[allow(dead_code)] pub enum EccPointQ<'a> { @@ -51,7 +51,7 @@ where ), Error, > { - let (offset, x_a, y_a) = self.public_initialization(region, Q)?; + let (offset, x_a, y_a) = self.public_q_initialization(region, Q)?; let (x_a, y_a, zs_sum) = self.hash_all_pieces(region, offset, message, x_a, y_a)?; @@ -59,13 +59,14 @@ where } #[allow(non_snake_case)] - /// Assign the coordinates of the initial public point `Q`, - /// y_Q to a fixed column + /// Assign the coordinates of the initial public point `Q` + /// - `x_Q` in a advice column, and + /// - `y_Q` in a fixed column. /// /// | offset | x_A | q_sinsemilla4 | fixed_y_q | /// -------------------------------------- /// | 0 | x_Q | 1 | y_Q | - fn public_initialization( + fn public_q_initialization( &self, region: &mut Region<'_, pallas::Base>, Q: pallas::Affine, diff --git a/halo2_gadgets/src/sinsemilla/merkle.rs b/halo2_gadgets/src/sinsemilla/merkle.rs index 95e9e401b9..c87514a51c 100644 --- a/halo2_gadgets/src/sinsemilla/merkle.rs +++ b/halo2_gadgets/src/sinsemilla/merkle.rs @@ -202,6 +202,7 @@ pub mod tests { use rand::{rngs::OsRng, RngCore}; use std::{convert::TryInto, iter}; + const MERKLE_DEPTH: usize = 32; #[derive(Default)] @@ -360,6 +361,7 @@ pub mod tests { Ok(()) } } + fn generate_circuit() -> MyCircuit { let mut rng = OsRng; diff --git a/halo2_gadgets/src/tests/test_utils.rs b/halo2_gadgets/src/tests/test_utils.rs index ed73cc3c17..085f695f08 100644 --- a/halo2_gadgets/src/tests/test_utils.rs +++ b/halo2_gadgets/src/tests/test_utils.rs @@ -1,9 +1,6 @@ -//! functions used for circuit test +//! Functions used for circuit test. -use std::{ - env, fs, - path::{Path, PathBuf}, -}; +use std::{env, fs, path::Path}; use rand::rngs::OsRng; @@ -33,14 +30,14 @@ impl AsRef<[u8]> for Proof { } impl Proof { - /// Creates a proof for the given circuits and instances. + /// Creates a proof for the given circuit and instances. pub fn create( vk: &VerifyingKey, params: &Params, circuit: C, ) -> Result - where - C: Circuit, + where + C: Circuit, { let pk = plonk::keygen_pk(params, vk.clone(), &circuit).unwrap(); @@ -69,8 +66,10 @@ impl Proof { } /// Test the generated vk against the stored vk. +/// +/// If the env variable GEN_ENV_VAR is set, save `vk` into a file. pub(crate) fn test_against_stored_vk>(circuit: &C, circuit_name: &str) { - let full_file_name = Path::new(TEST_DATA_DIR) + let file_path = Path::new(TEST_DATA_DIR) .join(format!("vk_{circuit_name}")) .with_extension("rdata"); @@ -81,39 +80,26 @@ pub(crate) fn test_against_stored_vk>(circuit: &C, circ let vk_text = format!("{:#?}\n", vk.pinned()); if env::var_os(GEN_ENV_VAR).is_some() { - fs::write(&full_file_name, &vk_text).expect("Unable to write vk test file") - } - - assert_eq!( - vk_text, - fs::read_to_string(full_file_name) - .expect("Unable to read vk test file") - .replace("\r\n", "\n") - ); -} - -/// If the env variable GEN_ENV_VAR is set, write the `Proof` to a file -fn conditionally_save_proof_to_disk>( - vk: &VerifyingKey, - params: &Params, - circuit: C, - full_file_name: &PathBuf, -) { - if env::var_os(GEN_ENV_VAR).is_some() { - let proof = Proof::create(vk, params, circuit).unwrap(); - assert!(proof.verify(vk, params).is_ok()); - - fs::write(full_file_name, proof.as_ref()).expect("Unable to write proof test file"); + fs::write(&file_path, &vk_text).expect("Unable to write vk test file"); + } else { + assert_eq!( + vk_text, + fs::read_to_string(file_path) + .expect("Unable to read vk test file") + .replace("\r\n", "\n") + ); } } /// Test the generated circuit against the stored proof. +/// +/// If the env variable GEN_ENV_VAR is set, save `vk` into a file. pub(crate) fn test_against_stored_proof>( circuit: C, circuit_name: &str, index: usize, ) { - let full_file_name = Path::new(TEST_DATA_DIR) + let file_path = Path::new(TEST_DATA_DIR) .join(format!("proof_{circuit_name}_{index}")) .with_extension("bin"); @@ -121,10 +107,15 @@ pub(crate) fn test_against_stored_proof>( let params: Params = Params::new(11); let vk = plonk::keygen_vk(¶ms, &circuit).unwrap(); - conditionally_save_proof_to_disk(&vk, ¶ms, circuit, &full_file_name); - - // Read the proof from storage - let proof = Proof::new(fs::read(full_file_name).expect("Unable to read proof test file")); + let proof = if env::var_os(GEN_ENV_VAR).is_some() { + // Create the proof and save it into a file + let proof = Proof::create(&vk, ¶ms, circuit).unwrap(); + fs::write(&file_path, proof.as_ref()).expect("Unable to write proof test file"); + proof + } else { + // Read the proof from storage + Proof::new(fs::read(file_path).expect("Unable to read proof test file")) + }; // Verify the stored proof with the generated vk assert!(proof.verify(&vk, ¶ms).is_ok());