Skip to content

Commit

Permalink
Put together test_against_stored_vk/proof into test_against_stored_ci…
Browse files Browse the repository at this point in the history
…rcuit

Previously, the functions test_against_stored_vk and test_against_stored_proof
each generated a verification key (vk). This operation can be quite slow.
We are combining these two tests to generate the vk only once.
  • Loading branch information
ConstanceBeguier committed Jul 2, 2024
1 parent 65b3ae4 commit 80dd7a1
Show file tree
Hide file tree
Showing 19 changed files with 254 additions and 274 deletions.
12 changes: 3 additions & 9 deletions halo2_gadgets/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ pub(crate) mod tests {
FixedPoints,
};
use crate::{
tests::test_utils::{test_against_stored_proof, test_against_stored_vk},
tests::test_utils::test_against_stored_circuit,
utilities::lookup_range_check::{LookupRangeCheck, PallasLookupRCConfig},
};

Expand Down Expand Up @@ -910,15 +910,9 @@ pub(crate) mod tests {
}

#[test]
fn fixed_verification_key_test() {
fn test_ecc_chip_against_stored_circuit() {
let circuit = MyCircuit { test_errors: false };
test_against_stored_vk(&circuit, "ecc_chip");
}

#[test]
fn serialized_proof_test_case() {
let circuit = MyCircuit { test_errors: false };
test_against_stored_proof(circuit, "ecc_chip", 0);
test_against_stored_circuit(circuit, "ecc_chip", 3872);
}

#[cfg(feature = "test-dev-graph")]
Expand Down
12 changes: 3 additions & 9 deletions halo2_gadgets/src/sinsemilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ pub(crate) mod tests {
NonIdentityPoint, ScalarFixed,
},
sinsemilla::primitives::{self as sinsemilla, K},
tests::test_utils::{test_against_stored_proof, test_against_stored_vk},
tests::test_utils::test_against_stored_circuit,
utilities::lookup_range_check::{LookupRangeCheck, PallasLookupRCConfig},
};

Expand Down Expand Up @@ -753,15 +753,9 @@ pub(crate) mod tests {
}

#[test]
fn fixed_verification_key_test() {
fn test_sinsemilla_chip_against_stored_circuit() {
let circuit = MyCircuit {};
test_against_stored_vk(&circuit, "sinsemilla_chip");
}

#[test]
fn serialized_proof_test_case() {
let circuit = MyCircuit {};
test_against_stored_proof(circuit, "sinsemilla_chip", 0);
test_against_stored_circuit(circuit, "sinsemilla_chip", 4576);
}

#[cfg(feature = "test-dev-graph")]
Expand Down
12 changes: 3 additions & 9 deletions halo2_gadgets/src/sinsemilla/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pub mod tests {
tests::{TestCommitDomain, TestHashDomain},
HashDomains,
},
tests::test_utils::{test_against_stored_proof, test_against_stored_vk},
tests::test_utils::test_against_stored_circuit,
utilities::{
i2lebsp,
lookup_range_check::{LookupRangeCheck, PallasLookupRCConfig},
Expand Down Expand Up @@ -391,15 +391,9 @@ pub mod tests {
}

#[test]
fn fixed_verification_key_test() {
fn test_merkle_chip_against_stored_circuit() {
let circuit = generate_circuit();
test_against_stored_vk(&circuit, "merkle_chip");
}

#[test]
fn serialized_proof_test_case() {
let circuit = generate_circuit();
test_against_stored_proof(circuit, "merkle_chip", 0);
test_against_stored_circuit(circuit, "merkle_chip", 4160);
}

#[cfg(feature = "test-dev-graph")]
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
42 changes: 17 additions & 25 deletions halo2_gadgets/src/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,58 +65,50 @@ impl Proof {
}
}

/// Test the generated vk against the stored vk.
/// Test the generated vk and the generated proof against the stored vk and the stored proof.
///
/// 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 file_path = Path::new(TEST_DATA_DIR)
/// If the env variable GEN_ENV_VAR is set, save `vk` and `proof` into a file.
pub(crate) fn test_against_stored_circuit<C: Circuit<pallas::Base>>(
circuit: C,
circuit_name: &str,
expected_proof_size: usize,
) {
let vk_file_path = Path::new(TEST_DATA_DIR)
.join(format!("vk_{circuit_name}"))
.with_extension("rdata");

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

let vk_text = format!("{:#?}\n", vk.pinned());

if env::var_os(GEN_ENV_VAR).is_some() {
fs::write(&file_path, &vk_text).expect("Unable to write vk test file");
fs::write(&vk_file_path, &vk_text).expect("Unable to write vk test file");
} else {
assert_eq!(
vk_text,
fs::read_to_string(file_path)
fs::read_to_string(vk_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 file_path = Path::new(TEST_DATA_DIR)
.join(format!("proof_{circuit_name}_{index}"))
let proof_file_path = Path::new(TEST_DATA_DIR)
.join(format!("proof_{circuit_name}"))
.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();

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");
fs::write(&proof_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"))
Proof::new(fs::read(proof_file_path).expect("Unable to read proof test file"))
};

// Verify the stored proof with the generated vk
// Verify the stored proof with the generated or stored vk.
assert!(proof.verify(&vk, &params).is_ok());
assert_eq!(proof.0.len(), expected_proof_size);
}
Loading

0 comments on commit 80dd7a1

Please sign in to comment.