Skip to content

Commit

Permalink
tune coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
adria0 committed Jun 3, 2024
1 parent 24b5deb commit bfbbcf1
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 40 deletions.
10 changes: 5 additions & 5 deletions halo2_proofs/tests/compress_selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(())
Expand Down
22 changes: 9 additions & 13 deletions halo2_proofs/tests/frontend_backend_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
);
}
10 changes: 5 additions & 5 deletions halo2_proofs/tests/plonk_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -602,9 +602,9 @@ fn plonk_api() {
AccumulatorStrategy<_>,
>(&verifier_params, pk.get_vk(), &proof[..]);

assert_eq!(
assert_test_proof(
"07382b50df5d591f5f54f99b09577f971986e4c343e8d050fb064432fda4be95",
keccak_hex(proof)
proof,
);
}

Expand Down Expand Up @@ -636,9 +636,9 @@ fn plonk_api() {
AccumulatorStrategy<_>,
>(&verifier_params, pk.get_vk(), &proof[..]);

assert_eq!(
assert_test_proof(
"32bb491e0f52a10f3361fc0aea6ea5aee3128f431e0fb846338e501c810dba49",
keccak_hex(proof)
proof,
);
}

Expand Down
6 changes: 3 additions & 3 deletions halo2_proofs/tests/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -190,8 +190,8 @@ fn test_serialization() {
)
.is_ok());

assert_eq!(
assert_test_proof(
"09e497a9a52d56f23d3f2cf832b5849a1ebbaab2086dec90144b3eb1a38b5331",
keccak_hex(proof)
proof,
)
}
10 changes: 5 additions & 5 deletions halo2_proofs/tests/shuffle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -333,9 +333,9 @@ fn test_shuffle() {
{
test_mock_prover(K, circuit.clone(), Ok(()));
let proof = test_prover::<EqAffine, W, H>(K, circuit.clone(), true);
assert_eq!(
assert_test_proof(
"dba3dbe7a83a719ec028317511e260b8c8e6207dc62b2d1ecd8ba0fa6ddc39ed",
keccak_hex(proof)
proof,
);
}

Expand All @@ -361,9 +361,9 @@ fn test_shuffle() {
)]),
);
let proof = test_prover::<EqAffine, W, H>(K, circuit, false);
assert_eq!(
assert_test_proof(
"0b4e97f2d561fae56fe893333eba2df5228c78e80f8bd7c509d4d40d127dff92",
keccak_hex(proof)
proof,
);
}
}
6 changes: 3 additions & 3 deletions halo2_proofs/tests/shuffle_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: Field> {
Expand Down Expand Up @@ -219,8 +219,8 @@ fn test_shuffle_api() {
prover.assert_satisfied();
let proof = test_prover::<EqAffine>(K, circuit, true);

assert_eq!(
assert_test_proof(
"10866a2a15d9cf36b36045277cae71057702f61a41ef56b04f813c30a5f8daa0",
keccak_hex(proof)
proof,
);
}
10 changes: 5 additions & 5 deletions halo2_proofs/tests/vector-ops-unblinded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: Field>: Chip<F> {
Expand Down Expand Up @@ -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::<halo2curves::pasta::EqAffine>(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::<halo2curves::pasta::EqAffine>(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
Expand Down
12 changes: 11 additions & 1 deletion halo2_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,24 @@ pub fn one_rng() -> BlockRng<OneNg> {
BlockRng::<OneNg>::new(OneNg {})
}

// Random number generator for testing

pub fn test_rng() -> OsRng {
OsRng
}

pub fn keccak_hex<D: AsRef<[u8]>>(data: D) -> String {
fn keccak_hex<D: AsRef<[u8]>>(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<D: AsRef<[u8]>>(hex: &str, data: D) {
if cfg!(all(feature = "thread-safe-region", not(coverage))) {
assert_eq!(keccak_hex(data), hex);
}
}

0 comments on commit bfbbcf1

Please sign in to comment.