miden-crypto/crypto/src/hash/rpo/tests.rs
crypto/src/hash/rpo/tests.rsRaw
1use super::{
2 ElementHasher, Felt, FieldElement, HashFn, Rpo, RpoDigest256, StarkField, ALPHA, INV_ALPHA,
3 INV_MDS, MDS, STATE_WIDTH, ZERO,
4};
5use core::convert::TryInto;
6use rand_utils::rand_value;
7
8#[test]
9fn mds_inv_test() {
10 let mut mul_result = [[Felt::new(0); STATE_WIDTH]; STATE_WIDTH];
11 for i in 0..STATE_WIDTH {
12 for j in 0..STATE_WIDTH {
13 let result = {
14 let mut result = Felt::new(0);
15 for k in 0..STATE_WIDTH {
16 result += MDS[i][k] * INV_MDS[k][j]
17 }
18 result
19 };
20 mul_result[i][j] = result;
21 if i == j {
22 assert_eq!(result, Felt::new(1));
23 } else {
24 assert_eq!(result, Felt::new(0));
25 }
26 }
27 }
28}
29#[test]
30fn test_alphas() {
31 let e: Felt = Felt::new(rand_value());
32 let e_exp = e.exp(ALPHA.into());
33 assert_eq!(e, e_exp.exp(INV_ALPHA));
34}
35
36#[test]
37fn test_sbox() {
38 let state = [Felt::new(rand_value()); STATE_WIDTH];
39
40 let mut expected = state;
41 expected.iter_mut().for_each(|v| *v = v.exp(ALPHA));
42
43 let mut actual = state;
44 Rpo::apply_sbox(&mut actual);
45
46 assert_eq!(expected, actual);
47}
48
49#[test]
50fn test_inv_sbox() {
51 let state = [Felt::new(rand_value()); STATE_WIDTH];
52
53 let mut expected = state;
54 expected.iter_mut().for_each(|v| *v = v.exp(INV_ALPHA));
55
56 let mut actual = state;
57 Rpo::apply_inv_sbox(&mut actual);
58
59 assert_eq!(expected, actual);
60}
61
62#[test]
63fn hash_elements_vs_merge() {
64 let elements = [Felt::new(rand_value()); 8];
65
66 let digests: [RpoDigest256; 2] = [
67 RpoDigest256::new(elements[..4].try_into().unwrap()),
68 RpoDigest256::new(elements[4..].try_into().unwrap()),
69 ];
70
71 let m_result = Rpo::merge(&digests);
72 let h_result = Rpo::hash_elements(&elements);
73 assert_eq!(m_result, h_result);
74}
75
76#[test]
77fn hash_elements_vs_merge_with_int() {
78 let tmp = [Felt::new(rand_value()); 4];
79 let seed = RpoDigest256::new(tmp);
80
81 // ----- value fits into a field element ------------------------------------------------------
82 let val: Felt = Felt::new(rand_value());
83 let m_result = Rpo::merge_with_int(seed, val.as_int());
84
85 let mut elements = seed.as_elements().to_vec();
86 elements.push(val);
87 let h_result = Rpo::hash_elements(&elements);
88
89 assert_eq!(m_result, h_result);
90
91 // ----- value does not fit into a field element ----------------------------------------------
92 let val = Felt::MODULUS + 2;
93 let m_result = Rpo::merge_with_int(seed, val);
94
95 let mut elements = seed.as_elements().to_vec();
96 elements.push(Felt::new(val));
97 elements.push(Felt::new(1));
98 let h_result = Rpo::hash_elements(&elements);
99
100 assert_eq!(m_result, h_result);
101}
102
103#[test]
104fn hash_padding() {
105 // adding a zero bytes at the end of a byte string should result in a different hash
106 let r1 = Rpo::hash(&[1_u8, 2, 3]);
107 let r2 = Rpo::hash(&[1_u8, 2, 3, 0]);
108 assert_ne!(r1, r2);
109
110 // same as above but with bigger inputs
111 let r1 = Rpo::hash(&[1_u8, 2, 3, 4, 5, 6]);
112 let r2 = Rpo::hash(&[1_u8, 2, 3, 4, 5, 6, 0]);
113 assert_ne!(r1, r2);
114
115 // same as above but with input splitting over two elements
116 let r1 = Rpo::hash(&[1_u8, 2, 3, 4, 5, 6, 7]);
117 let r2 = Rpo::hash(&[1_u8, 2, 3, 4, 5, 6, 7, 0]);
118 assert_ne!(r1, r2);
119
120 // same as above but with multiple zeros
121 let r1 = Rpo::hash(&[1_u8, 2, 3, 4, 5, 6, 7, 0, 0]);
122 let r2 = Rpo::hash(&[1_u8, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0]);
123 assert_ne!(r1, r2);
124}
125
126#[test]
127fn hash_elements_padding() {
128 let e1 = [Felt::new(rand_value()); 2];
129 let e2 = [e1[0], e1[1], ZERO];
130
131 let r1 = Rpo::hash_elements(&e1);
132 let r2 = Rpo::hash_elements(&e2);
133 assert_ne!(r1, r2);
134}
135
136#[test]
137fn hash_elements() {
138 let elements = [
139 Felt::new(0),
140 Felt::new(1),
141 Felt::new(2),
142 Felt::new(3),
143 Felt::new(4),
144 Felt::new(5),
145 Felt::new(6),
146 Felt::new(7),
147 ];
148
149 let digests: [RpoDigest256; 2] = [
150 RpoDigest256::new(elements[..4].try_into().unwrap()),
151 RpoDigest256::new(elements[4..8].try_into().unwrap()),
152 ];
153
154 let m_result = Rpo::merge(&digests);
155 let h_result = Rpo::hash_elements(&elements);
156 assert_eq!(m_result, h_result);
157}