From 22f7f7e80a112d759e270396652c9b68e428436f Mon Sep 17 00:00:00 2001 From: adria0 Date: Wed, 5 Jun 2024 08:37:21 +0200 Subject: [PATCH] Simplify --- halo2_debug/src/lib.rs | 16 ++++------------ halo2_proofs/tests/compress_selectors.rs | 15 ++++++++++----- halo2_proofs/tests/frontend_backend_split.rs | 14 +++++++++----- halo2_proofs/tests/plonk_api.rs | 10 +++++----- halo2_proofs/tests/serialization.rs | 6 +++--- halo2_proofs/tests/shuffle.rs | 10 +++++----- halo2_proofs/tests/shuffle_api.rs | 6 +++--- halo2_proofs/tests/vector-ops-unblinded.rs | 10 +++++----- 8 files changed, 44 insertions(+), 43 deletions(-) diff --git a/halo2_debug/src/lib.rs b/halo2_debug/src/lib.rs index 61f1c3bbe..f8a6fc01a 100644 --- a/halo2_debug/src/lib.rs +++ b/halo2_debug/src/lib.rs @@ -3,7 +3,7 @@ use rand_core::block::BlockRngCore; use rand_core::OsRng; use tiny_keccak::Hasher; -// One number generator, that can be used as a deterministic Rng, outputing fixed values. +/// One number generator, that can be used as a deterministic Rng, outputing fixed values. pub struct OneNg {} impl BlockRngCore for OneNg { @@ -21,24 +21,16 @@ pub fn one_rng() -> BlockRng { BlockRng::::new(OneNg {}) } -// Random number generator for testing - +/// Random number generator for testing pub fn test_rng() -> OsRng { OsRng } -fn keccak_hex>(data: D) -> String { +/// Gets the hex representation of the keccak hash of the input data +pub 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); - } -} diff --git a/halo2_proofs/tests/compress_selectors.rs b/halo2_proofs/tests/compress_selectors.rs index ae6b011c1..6e6212505 100644 --- a/halo2_proofs/tests/compress_selectors.rs +++ b/halo2_proofs/tests/compress_selectors.rs @@ -10,7 +10,7 @@ use halo2_proofs::poly::Rotation; use halo2_backend::transcript::{ Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer, }; -use halo2_debug::{assert_test_proof, one_rng}; +use halo2_debug::{keccak_hex, one_rng}; use halo2_middleware::zal::impls::{H2cEngine, PlonkEngineConfig}; use halo2_proofs::arithmetic::Field; use halo2_proofs::plonk::{ @@ -423,17 +423,22 @@ How the `compress_selectors` works in `MyCircuit` under the hood: */ #[test] +#[allow(unused_variables)] fn test_success() -> Result<(), halo2_proofs::plonk::Error> { // vk & pk keygen both WITH compress - assert_test_proof( + let proof = test_mycircuit(true, true)?; + #[cfg(not(coverage))] + assert_eq!( "8083f3ecb002d25d66682a08581d9dfdf9c621e7d290db62238f8bc7b671eb1b", - test_mycircuit(true, true)?, + keccak_hex(proof), ); // vk & pk keygen both WITHOUT compress - assert_test_proof( + let proof = test_mycircuit(false, false)?; + #[cfg(not(coverage))] + assert_eq!( "dbb85c029aa10ad0d5aa3f9711472f39dfe67cd82dc27a66ea403ad0ec499dc9", - test_mycircuit(false, false)?, + keccak_hex(proof), ); Ok(()) diff --git a/halo2_proofs/tests/frontend_backend_split.rs b/halo2_proofs/tests/frontend_backend_split.rs index 9a6dcb793..88bf43925 100644 --- a/halo2_proofs/tests/frontend_backend_split.rs +++ b/halo2_proofs/tests/frontend_backend_split.rs @@ -11,7 +11,7 @@ use halo2_backend::{ Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer, }, }; -use halo2_debug::{assert_test_proof, one_rng}; +use halo2_debug::one_rng; use halo2_frontend::{ circuit::{ compile_circuit, AssignedCell, Layouter, Region, SimpleFloorPlanner, Value, @@ -538,9 +538,11 @@ fn test_mycircuit_full_legacy() { .expect("verify succeeds"); println!("Verify: {:?}", start.elapsed()); - assert_test_proof( + // TODO: Check why the proof is different + #[cfg(all(feature = "thread-safe-region", not(coverage)))] + assert_eq!( "c5c11281474b586795a5d97bdefeee80456d2921584b3a8b00523eebd49f2fac", - proof, + halo2_debug::keccak_hex(proof), ); } @@ -620,8 +622,10 @@ fn test_mycircuit_full_split() { .expect("verify succeeds"); println!("Verify: {:?}", start.elapsed()); - assert_test_proof( + // TODO: Check why the proof is different + #[cfg(all(feature = "thread-safe-region", not(coverage)))] + assert_eq!( "c5c11281474b586795a5d97bdefeee80456d2921584b3a8b00523eebd49f2fac", - proof, + halo2_debug::keccak_hex(proof), ); } diff --git a/halo2_proofs/tests/plonk_api.rs b/halo2_proofs/tests/plonk_api.rs index bcdfeabd3..692617518 100644 --- a/halo2_proofs/tests/plonk_api.rs +++ b/halo2_proofs/tests/plonk_api.rs @@ -4,7 +4,7 @@ use assert_matches::assert_matches; use ff::{FromUniformBytes, WithSmallOrderMulGroup}; use halo2_debug::test_rng; -use halo2_debug::{assert_test_proof, one_rng}; +use halo2_debug::{keccak_hex, one_rng}; use halo2_middleware::zal::{ impls::{PlonkEngine, PlonkEngineConfig}, traits::MsmAccel, @@ -602,9 +602,9 @@ fn plonk_api() { AccumulatorStrategy<_>, >(&verifier_params, pk.get_vk(), &proof[..]); - assert_test_proof( + assert_eq!( "07382b50df5d591f5f54f99b09577f971986e4c343e8d050fb064432fda4be95", - proof, + keccak_hex(proof), ); } @@ -636,9 +636,9 @@ fn plonk_api() { AccumulatorStrategy<_>, >(&verifier_params, pk.get_vk(), &proof[..]); - assert_test_proof( + assert_eq!( "32bb491e0f52a10f3361fc0aea6ea5aee3128f431e0fb846338e501c810dba49", - proof, + keccak_hex(proof), ); } diff --git a/halo2_proofs/tests/serialization.rs b/halo2_proofs/tests/serialization.rs index 65421405b..f8f123134 100644 --- a/halo2_proofs/tests/serialization.rs +++ b/halo2_proofs/tests/serialization.rs @@ -4,7 +4,7 @@ use std::{ }; use ff::Field; -use halo2_debug::{assert_test_proof, one_rng}; +use halo2_debug::{keccak_hex, one_rng}; use halo2_proofs::{ circuit::{Layouter, SimpleFloorPlanner, Value}, plonk::{ @@ -190,8 +190,8 @@ fn test_serialization() { ) .is_ok()); - assert_test_proof( + assert_eq!( "09e497a9a52d56f23d3f2cf832b5849a1ebbaab2086dec90144b3eb1a38b5331", - proof, + keccak_hex(proof), ) } diff --git a/halo2_proofs/tests/shuffle.rs b/halo2_proofs/tests/shuffle.rs index ee18a3e63..78bd83273 100644 --- a/halo2_proofs/tests/shuffle.rs +++ b/halo2_proofs/tests/shuffle.rs @@ -1,5 +1,5 @@ use ff::{BatchInvert, FromUniformBytes}; -use halo2_debug::{assert_test_proof, one_rng}; +use halo2_debug::{keccak_hex, one_rng}; use halo2_proofs::{ arithmetic::{CurveAffine, Field}, circuit::{floor_planner::V1, Layouter, Value}, @@ -333,9 +333,9 @@ fn test_shuffle() { { test_mock_prover(K, circuit.clone(), Ok(())); let proof = test_prover::(K, circuit.clone(), true); - assert_test_proof( + assert_eq!( "dba3dbe7a83a719ec028317511e260b8c8e6207dc62b2d1ecd8ba0fa6ddc39ed", - proof, + keccak_hex(proof), ); } @@ -361,9 +361,9 @@ fn test_shuffle() { )]), ); let proof = test_prover::(K, circuit, false); - assert_test_proof( + assert_eq!( "0b4e97f2d561fae56fe893333eba2df5228c78e80f8bd7c509d4d40d127dff92", - proof, + keccak_hex(proof), ); } } diff --git a/halo2_proofs/tests/shuffle_api.rs b/halo2_proofs/tests/shuffle_api.rs index 951144fb8..2ff64e69b 100644 --- a/halo2_proofs/tests/shuffle_api.rs +++ b/halo2_proofs/tests/shuffle_api.rs @@ -1,7 +1,7 @@ use std::{marker::PhantomData, vec}; use ff::FromUniformBytes; -use halo2_debug::{assert_test_proof, one_rng}; +use halo2_debug::{keccak_hex, one_rng}; use halo2_proofs::{ arithmetic::Field, circuit::{Layouter, SimpleFloorPlanner, Value}, @@ -219,8 +219,8 @@ fn test_shuffle_api() { prover.assert_satisfied(); let proof = test_prover::(K, circuit, true); - assert_test_proof( + assert_eq!( "10866a2a15d9cf36b36045277cae71057702f61a41ef56b04f813c30a5f8daa0", - proof, + keccak_hex(proof), ); } diff --git a/halo2_proofs/tests/vector-ops-unblinded.rs b/halo2_proofs/tests/vector-ops-unblinded.rs index 461a19293..b90fa9836 100644 --- a/halo2_proofs/tests/vector-ops-unblinded.rs +++ b/halo2_proofs/tests/vector-ops-unblinded.rs @@ -4,7 +4,7 @@ use std::marker::PhantomData; use ff::FromUniformBytes; -use halo2_debug::{assert_test_proof, one_rng}; +use halo2_debug::{keccak_hex, one_rng}; use halo2_proofs::{ arithmetic::{CurveAffine, Field}, circuit::{AssignedCell, Chip, Layouter, Region, SimpleFloorPlanner, Value}, @@ -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_test_proof( + assert_eq!( "845349549e3776ba45e5bc03d44fd44f8e65f6338e8b7b8975dcc4f310094bf3", - &proof_1, + keccak_hex(&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_test_proof( + assert_eq!( "55f4b12e359be5541f539f74ae2b4afd2206160609faa1b902d90e91bfd4a641", - &proof_2, + keccak_hex(&proof_2), ); // the commitments will be the first columns of the proof transcript so we can compare them easily