Skip to content

Commit

Permalink
test: unit tests for merge in domain method
Browse files Browse the repository at this point in the history
Signed-off-by: 0xKanekiKen <100861945+0xKanekiKen@users.noreply.github.com>
  • Loading branch information
0xkanekiken committed Jan 23, 2023
1 parent bc6191b commit 5757b89
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/hash/rpo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 36 additions & 1 deletion src/hash/rpo/tests.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 5757b89

Please sign in to comment.