diff --git a/halo2_proofs/tests/compress_selectors.rs b/halo2_proofs/tests/compress_selectors.rs index 19083d76b..1d264f403 100644 --- a/halo2_proofs/tests/compress_selectors.rs +++ b/halo2_proofs/tests/compress_selectors.rs @@ -19,7 +19,7 @@ use halo2_proofs::plonk::{ use halo2_proofs::poly::kzg::commitment::{KZGCommitmentScheme, ParamsKZG}; use halo2_proofs::poly::kzg::multiopen::{ProverSHPLONK, VerifierSHPLONK}; use halo2_proofs::poly::kzg::strategy::SingleStrategy; -use halo2_test_utils::{keccak_hex, one_rng}; +use halo2_test_utils::{assert_test_proof, one_rng}; use halo2curves::bn256::{Bn256, Fr, G1Affine}; #[derive(Debug, Clone)] @@ -425,15 +425,15 @@ How the `compress_selectors` works in `MyCircuit` under the hood: #[test] fn test_success() -> Result<(), halo2_proofs::plonk::Error> { // vk & pk keygen both WITH compress - assert_eq!( + assert_test_proof( "8083f3ecb002d25d66682a08581d9dfdf9c621e7d290db62238f8bc7b671eb1b", - test_mycircuit(true, true).map(keccak_hex)? + test_mycircuit(true, true)?, ); // vk & pk keygen both WITHOUT compress - assert_eq!( + assert_test_proof( "dbb85c029aa10ad0d5aa3f9711472f39dfe67cd82dc27a66ea403ad0ec499dc9", - test_mycircuit(false, false).map(keccak_hex)? + test_mycircuit(false, false)?, ); Ok(()) diff --git a/halo2_proofs/tests/frontend_backend_split.rs b/halo2_proofs/tests/frontend_backend_split.rs index 9a5f7b477..fd757d5f4 100644 --- a/halo2_proofs/tests/frontend_backend_split.rs +++ b/halo2_proofs/tests/frontend_backend_split.rs @@ -28,7 +28,7 @@ use halo2_frontend::{ }, }; use halo2_middleware::{ff::Field, poly::Rotation}; -use halo2_test_utils::{keccak_hex, one_rng}; +use halo2_test_utils::{assert_test_proof, one_rng}; use std::collections::HashMap; #[derive(Clone)] @@ -545,12 +545,10 @@ fn test_mycircuit_full_legacy() { .expect("verify succeeds"); println!("Verify: {:?}", start.elapsed()); - if cfg!(feature = "thread-safe-region") { - assert_eq!( - "c5c11281474b586795a5d97bdefeee80456d2921584b3a8b00523eebd49f2fac", - keccak_hex(proof) - ); - } + assert_test_proof( + "c5c11281474b586795a5d97bdefeee80456d2921584b3a8b00523eebd49f2fac", + proof, + ); } #[test] @@ -632,10 +630,8 @@ fn test_mycircuit_full_split() { .expect("verify succeeds"); println!("Verify: {:?}", start.elapsed()); - if cfg!(feature = "thread-safe-region") { - assert_eq!( - "e612e9d5ebcdcff5ed9823c527218cdbaaf9fe76cc5e42d05a60f652dcc92660", - keccak_hex(proof) - ); - } + assert_test_proof( + "e612e9d5ebcdcff5ed9823c527218cdbaaf9fe76cc5e42d05a60f652dcc92660", + proof, + ); } diff --git a/halo2_proofs/tests/plonk_api.rs b/halo2_proofs/tests/plonk_api.rs index 3686de5d5..cf0a96b89 100644 --- a/halo2_proofs/tests/plonk_api.rs +++ b/halo2_proofs/tests/plonk_api.rs @@ -23,7 +23,7 @@ use halo2_proofs::transcript::{ TranscriptWriterBuffer, }; use halo2_test_utils::test_rng; -use halo2_test_utils::{keccak_hex, one_rng}; +use halo2_test_utils::{assert_test_proof, one_rng}; use rand_core::RngCore; use std::marker::PhantomData; @@ -602,9 +602,9 @@ fn plonk_api() { AccumulatorStrategy<_>, >(&verifier_params, pk.get_vk(), &proof[..]); - assert_eq!( + assert_test_proof( "07382b50df5d591f5f54f99b09577f971986e4c343e8d050fb064432fda4be95", - keccak_hex(proof) + proof, ); } @@ -636,9 +636,9 @@ fn plonk_api() { AccumulatorStrategy<_>, >(&verifier_params, pk.get_vk(), &proof[..]); - assert_eq!( + assert_test_proof( "32bb491e0f52a10f3361fc0aea6ea5aee3128f431e0fb846338e501c810dba49", - keccak_hex(proof) + proof, ); } diff --git a/halo2_proofs/tests/serialization.rs b/halo2_proofs/tests/serialization.rs index e2856c28a..1a2d1e1fe 100644 --- a/halo2_proofs/tests/serialization.rs +++ b/halo2_proofs/tests/serialization.rs @@ -23,7 +23,7 @@ use halo2_proofs::{ }, SerdeFormat, }; -use halo2_test_utils::{keccak_hex, one_rng}; +use halo2_test_utils::{assert_test_proof, one_rng}; use halo2curves::bn256::{Bn256, Fr, G1Affine}; #[derive(Clone, Copy)] @@ -190,8 +190,8 @@ fn test_serialization() { ) .is_ok()); - assert_eq!( + assert_test_proof( "09e497a9a52d56f23d3f2cf832b5849a1ebbaab2086dec90144b3eb1a38b5331", - keccak_hex(proof) + proof, ) } diff --git a/halo2_proofs/tests/shuffle.rs b/halo2_proofs/tests/shuffle.rs index 69bb5c476..97d8ce604 100644 --- a/halo2_proofs/tests/shuffle.rs +++ b/halo2_proofs/tests/shuffle.rs @@ -18,7 +18,7 @@ use halo2_proofs::{ Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer, }, }; -use halo2_test_utils::{keccak_hex, one_rng}; +use halo2_test_utils::{assert_test_proof, one_rng}; use rand_chacha::ChaCha20Rng; use rand_core::{RngCore, SeedableRng}; use std::iter; @@ -333,9 +333,9 @@ fn test_shuffle() { { test_mock_prover(K, circuit.clone(), Ok(())); let proof = test_prover::(K, circuit.clone(), true); - assert_eq!( + assert_test_proof( "dba3dbe7a83a719ec028317511e260b8c8e6207dc62b2d1ecd8ba0fa6ddc39ed", - keccak_hex(proof) + proof, ); } @@ -361,9 +361,9 @@ fn test_shuffle() { )]), ); let proof = test_prover::(K, circuit, false); - assert_eq!( + assert_test_proof( "0b4e97f2d561fae56fe893333eba2df5228c78e80f8bd7c509d4d40d127dff92", - keccak_hex(proof) + proof, ); } } diff --git a/halo2_proofs/tests/shuffle_api.rs b/halo2_proofs/tests/shuffle_api.rs index 988bbd67e..c405d23e3 100644 --- a/halo2_proofs/tests/shuffle_api.rs +++ b/halo2_proofs/tests/shuffle_api.rs @@ -22,7 +22,7 @@ use halo2_proofs::{ Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer, }, }; -use halo2_test_utils::{keccak_hex, one_rng}; +use halo2_test_utils::{assert_test_proof, one_rng}; use halo2curves::{pasta::EqAffine, CurveAffine}; struct ShuffleChip { @@ -219,8 +219,8 @@ fn test_shuffle_api() { prover.assert_satisfied(); let proof = test_prover::(K, circuit, true); - assert_eq!( + assert_test_proof( "10866a2a15d9cf36b36045277cae71057702f61a41ef56b04f813c30a5f8daa0", - keccak_hex(proof) + proof, ); } diff --git a/halo2_proofs/tests/vector-ops-unblinded.rs b/halo2_proofs/tests/vector-ops-unblinded.rs index 83babafa4..86fffb7c9 100644 --- a/halo2_proofs/tests/vector-ops-unblinded.rs +++ b/halo2_proofs/tests/vector-ops-unblinded.rs @@ -21,7 +21,7 @@ use halo2_proofs::{ Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer, }, }; -use halo2_test_utils::{keccak_hex, one_rng}; +use halo2_test_utils::{assert_test_proof, one_rng}; // ANCHOR: instructions trait NumericInstructions: Chip { @@ -547,16 +547,16 @@ fn test_vector_ops_unbinded() { // the commitments will be the first columns of the proof transcript so we can compare them easily let proof_1 = test_prover::(k, mul_circuit, true, c_mul); - assert_eq!( + assert_test_proof( "845349549e3776ba45e5bc03d44fd44f8e65f6338e8b7b8975dcc4f310094bf3", - keccak_hex(&proof_1) + &proof_1, ); // the commitments will be the first columns of the proof transcript so we can compare them easily let proof_2 = test_prover::(k, add_circuit, true, c_add); - assert_eq!( + assert_test_proof( "55f4b12e359be5541f539f74ae2b4afd2206160609faa1b902d90e91bfd4a641", - keccak_hex(&proof_2) + &proof_2, ); // the commitments will be the first columns of the proof transcript so we can compare them easily diff --git a/halo2_test_utils/src/lib.rs b/halo2_test_utils/src/lib.rs index 14e52b4bd..61f1c3bbe 100644 --- a/halo2_test_utils/src/lib.rs +++ b/halo2_test_utils/src/lib.rs @@ -21,14 +21,24 @@ pub fn one_rng() -> BlockRng { BlockRng::::new(OneNg {}) } +// Random number generator for testing + pub fn test_rng() -> OsRng { OsRng } -pub fn keccak_hex>(data: D) -> String { +fn keccak_hex>(data: D) -> String { let mut hash = [0u8; 32]; let mut hasher = tiny_keccak::Keccak::v256(); hasher.update(data.as_ref()); hasher.finalize(&mut hash); hex::encode(hash) } + +// Check the a test proof against a known hash +// Note that this function is only called in CI in "cargo test --all-fetaures" +pub fn assert_test_proof>(hex: &str, data: D) { + if cfg!(all(feature = "thread-safe-region", not(coverage))) { + assert_eq!(keccak_hex(data), hex); + } +}