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 f0d0ad9
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 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,41 @@ 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, except for the first element of the capacity part, which
// is set to 1 if the number of elements is not a multiple of RATE_WIDTH.
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 f0d0ad9

Please sign in to comment.