From 5757b896fe6c6cfbcd000008d9676239776fb3c7 Mon Sep 17 00:00:00 2001 From: 0xKanekiKen <100861945+0xKanekiKen@users.noreply.github.com> Date: Mon, 23 Jan 2023 20:17:34 +0000 Subject: [PATCH] test: unit tests for merge in domain method Signed-off-by: 0xKanekiKen <100861945+0xKanekiKen@users.noreply.github.com> --- src/hash/rpo/mod.rs | 2 +- src/hash/rpo/tests.rs | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/hash/rpo/mod.rs b/src/hash/rpo/mod.rs index c85c13c..0d2fde3 100644 --- a/src/hash/rpo/mod.rs +++ b/src/hash/rpo/mod.rs @@ -297,7 +297,7 @@ impl Rpo256 { // DOMAIN IDENTIFIER // -------------------------------------------------------------------------------------------- - /// Returns a hash of two digests and a domain separator. + /// Returns a hash of two digests and a domain identifier. pub fn merge_in_domain(values: &[RpoDigest; 2], domain: Felt) -> RpoDigest { // initialize the state by copying the digest elements into the rate portion of the state // (8 total elements), and set the capacity elements to 0. diff --git a/src/hash/rpo/tests.rs b/src/hash/rpo/tests.rs index 03f69f9..80227ab 100644 --- a/src/hash/rpo/tests.rs +++ b/src/hash/rpo/tests.rs @@ -1,5 +1,6 @@ use super::{ - Felt, FieldElement, Hasher, Rpo256, RpoDigest, StarkField, ALPHA, INV_ALPHA, STATE_WIDTH, ZERO, + Felt, FieldElement, Hasher, Rpo256, RpoDigest, StarkField, ALPHA, CAPACITY_RANGE, DIGEST_RANGE, + INV_ALPHA, RATE_RANGE, STATE_WIDTH, ZERO, }; use core::convert::TryInto; use rand_utils::rand_value; @@ -51,6 +52,40 @@ fn hash_elements_vs_merge() { assert_eq!(m_result, h_result); } +#[test] +fn hash_elements_vs_merge_in_domain() { + let elements = [Felt::new(rand_value()); 8]; + + let digests: [RpoDigest; 2] = [ + RpoDigest::new(elements[..4].try_into().unwrap()), + RpoDigest::new(elements[4..].try_into().unwrap()), + ]; + + // pick a random domain value. + let domain = Felt::new(rand_value()); + + // convert the elements into a list of base field elements + let elements = Felt::as_base_elements(&elements); + + // initialize state to all zeros. + let mut state = [ZERO; STATE_WIDTH]; + + // set the second capacity element to the domain. + state[CAPACITY_RANGE.start + 1] = domain; + + // absorb elements into the state. + state[RATE_RANGE.start..RATE_RANGE.end].copy_from_slice(elements); + + // apply permutation to the state. + Rpo256::apply_permutation(&mut state); + + // return the first 4 elements of the state as hash result + let h_result = RpoDigest::new(state[DIGEST_RANGE].try_into().unwrap()); + + let m_result = Rpo256::merge_in_domain(&digests, domain); + assert_eq!(m_result, h_result); +} + #[test] fn hash_elements_vs_merge_with_int() { let tmp = [Felt::new(rand_value()); 4];