Skip to content

Commit

Permalink
Cleanup code and some renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
ConstanceBeguier committed Jun 11, 2024
1 parent 236dfc5 commit 3ed08fb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 46 deletions.
6 changes: 2 additions & 4 deletions halo2_gadgets/src/ecc/chip/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Lookup: PallasLookupRC>(
Expand Down
11 changes: 6 additions & 5 deletions halo2_gadgets/src/sinsemilla/chip/hash_to_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -51,21 +51,22 @@ 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)?;

self.check_hash_result(EccPointQ::PublicPoint(Q), message, x_a, y_a, zs_sum)
}

#[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,
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/src/sinsemilla/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ pub mod tests {

use rand::{rngs::OsRng, RngCore};
use std::{convert::TryInto, iter};

const MERKLE_DEPTH: usize = 32;

#[derive(Default)]
Expand Down Expand Up @@ -360,6 +361,7 @@ pub mod tests {
Ok(())
}
}

fn generate_circuit() -> MyCircuit {
let mut rng = OsRng;

Expand Down
65 changes: 28 additions & 37 deletions halo2_gadgets/src/tests/test_utils.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<C>(
vk: &VerifyingKey<Affine>,
params: &Params<Affine>,
circuit: C,
) -> Result<Self, plonk::Error>
where
C: Circuit<pallas::Base>,
where
C: Circuit<pallas::Base>,
{
let pk = plonk::keygen_pk(params, vk.clone(), &circuit).unwrap();

Expand Down Expand Up @@ -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<C: Circuit<pallas::Base>>(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");

Expand All @@ -81,50 +80,42 @@ pub(crate) fn test_against_stored_vk<C: Circuit<pallas::Base>>(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<C: Circuit<pallas::Base>>(
vk: &VerifyingKey<Affine>,
params: &Params<Affine>,
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<C: Circuit<pallas::Base>>(
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");

// Setup phase: generate parameters, vk for the circuit.
let params: Params<Affine> = Params::new(11);
let vk = plonk::keygen_vk(&params, &circuit).unwrap();

conditionally_save_proof_to_disk(&vk, &params, 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, &params, 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, &params).is_ok());
Expand Down

0 comments on commit 3ed08fb

Please sign in to comment.