diff --git a/halo2_debug/Cargo.toml b/halo2_debug/Cargo.toml new file mode 100644 index 000000000..9e71ffdcc --- /dev/null +++ b/halo2_debug/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "halo2_debug" +version = "0.3.0" +authors = [ + "Privacy Scaling Explorations team", +] +edition = "2021" +rust-version = "1.66.0" +description = """ +Halo2 Debug. This package contains utilities for debugging and testing within +the halo2 ecosystem. +""" +license = "MIT OR Apache-2.0" +repository = "https://github.com/privacy-scaling-explorations/halo2" +documentation = "https://privacy-scaling-explorations.github.io/halo2/" +categories = ["cryptography"] +keywords = ["halo", "proofs", "zkp", "zkSNARKs"] + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs", "--html-in-header", "katex-header.html"] + +[dependencies] +tiny-keccak = { version = "2.0.2", features=["keccak"] } +hex = "0.4.3" +rand_core = "0.6.4" diff --git a/halo2_debug/src/lib.rs b/halo2_debug/src/lib.rs new file mode 100644 index 000000000..61f1c3bbe --- /dev/null +++ b/halo2_debug/src/lib.rs @@ -0,0 +1,44 @@ +use rand_core::block::BlockRng; +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. +pub struct OneNg {} + +impl BlockRngCore for OneNg { + type Item = u32; + type Results = [u32; 16]; + + fn generate(&mut self, results: &mut Self::Results) { + for elem in results.iter_mut() { + *elem = 1; + } + } +} + +pub fn one_rng() -> BlockRng { + BlockRng::::new(OneNg {}) +} + +// Random number generator for testing + +pub fn test_rng() -> OsRng { + OsRng +} + +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); + } +}