miden-crypto/src/hash/rescue/mod.rs
src/hash/rescue/mod.rsRaw
1use core::ops::Range;
2
3use super::{CubeExtension, Digest, ElementHasher, Felt, FieldElement, Hasher, StarkField, ZERO};
4
5mod arch;
6pub use arch::optimized::{add_constants_and_apply_inv_sbox, add_constants_and_apply_sbox};
7
8mod mds;
9use mds::{apply_mds, MDS};
10
11mod rpo;
12pub use rpo::{Rpo256, RpoDigest, RpoDigestError};
13
14mod rpx;
15pub use rpx::{Rpx256, RpxDigest, RpxDigestError};
16
17#[cfg(test)]
18mod tests;
19
20// CONSTANTS
21// ================================================================================================
22
23/// The number of rounds is set to 7. For the RPO hash functions all rounds are uniform. For the
24/// RPX hash function, there are 3 different types of rounds.
25const NUM_ROUNDS: usize = 7;
26
27/// Sponge state is set to 12 field elements or 96 bytes; 8 elements are reserved for rate and
28/// the remaining 4 elements are reserved for capacity.
29const STATE_WIDTH: usize = 12;
30
31/// The rate portion of the state is located in elements 4 through 11.
32const RATE_RANGE: Range<usize> = 4..12;
33const RATE_WIDTH: usize = RATE_RANGE.end - RATE_RANGE.start;
34
35const INPUT1_RANGE: Range<usize> = 4..8;
36const INPUT2_RANGE: Range<usize> = 8..12;
37
38/// The capacity portion of the state is located in elements 0, 1, 2, and 3.
39const CAPACITY_RANGE: Range<usize> = 0..4;
40
41/// The output of the hash function is a digest which consists of 4 field elements or 32 bytes.
42///
43/// The digest is returned from state elements 4, 5, 6, and 7 (the first four elements of the
44/// rate portion).
45const DIGEST_RANGE: Range<usize> = 4..8;
46const DIGEST_SIZE: usize = DIGEST_RANGE.end - DIGEST_RANGE.start;
47
48/// The number of bytes needed to encoded a digest
49const DIGEST_BYTES: usize = 32;
50
51/// The number of byte chunks defining a field element when hashing a sequence of bytes
52const BINARY_CHUNK_SIZE: usize = 7;
53
54/// S-Box and Inverse S-Box powers;
55///
56/// The constants are defined for tests only because the exponentiations in the code are unrolled
57/// for efficiency reasons.
58#[cfg(test)]
59const ALPHA: u64 = 7;
60#[cfg(test)]
61const INV_ALPHA: u64 = 10540996611094048183;
62
63// SBOX FUNCTION
64// ================================================================================================
65
66#[inline(always)]
67fn apply_sbox(state: &mut [Felt; STATE_WIDTH]) {
68 state[0] = state[0].exp7();
69 state[1] = state[1].exp7();
70 state[2] = state[2].exp7();
71 state[3] = state[3].exp7();
72 state[4] = state[4].exp7();
73 state[5] = state[5].exp7();
74 state[6] = state[6].exp7();
75 state[7] = state[7].exp7();
76 state[8] = state[8].exp7();
77 state[9] = state[9].exp7();
78 state[10] = state[10].exp7();
79 state[11] = state[11].exp7();
80}
81
82// INVERSE SBOX FUNCTION
83// ================================================================================================
84
85#[inline(always)]
86fn apply_inv_sbox(state: &mut [Felt; STATE_WIDTH]) {
87 // compute base^10540996611094048183 using 72 multiplications per array element
88 // 10540996611094048183 = b1001001001001001001001001001000110110110110110110110110110110111
89
90 // compute base^10
91 let mut t1 = *state;
92 t1.iter_mut().for_each(|t| *t = t.square());
93
94 // compute base^100
95 let mut t2 = t1;
96 t2.iter_mut().for_each(|t| *t = t.square());
97
98 // compute base^100100
99 let t3 = exp_acc::<Felt, STATE_WIDTH, 3>(t2, t2);
100
101 // compute base^100100100100
102 let t4 = exp_acc::<Felt, STATE_WIDTH, 6>(t3, t3);
103
104 // compute base^100100100100100100100100
105 let t5 = exp_acc::<Felt, STATE_WIDTH, 12>(t4, t4);
106
107 // compute base^100100100100100100100100100100
108 let t6 = exp_acc::<Felt, STATE_WIDTH, 6>(t5, t3);
109
110 // compute base^1001001001001001001001001001000100100100100100100100100100100
111 let t7 = exp_acc::<Felt, STATE_WIDTH, 31>(t6, t6);
112
113 // compute base^1001001001001001001001001001000110110110110110110110110110110111
114 for (i, s) in state.iter_mut().enumerate() {
115 let a = (t7[i].square() * t6[i]).square().square();
116 let b = t1[i] * t2[i] * *s;
117 *s = a * b;
118 }
119
120 #[inline(always)]
121 fn exp_acc<B: StarkField, const N: usize, const M: usize>(
122 base: [B; N],
123 tail: [B; N],
124 ) -> [B; N] {
125 let mut result = base;
126 for _ in 0..M {
127 result.iter_mut().for_each(|r| *r = r.square());
128 }
129 result.iter_mut().zip(tail).for_each(|(r, t)| *r *= t);
130 result
131 }
132}
133
134#[inline(always)]
135fn add_constants(state: &mut [Felt; STATE_WIDTH], ark: &[Felt; STATE_WIDTH]) {
136 state.iter_mut().zip(ark).for_each(|(s, &k)| *s += k);
137}
138
139// ROUND CONSTANTS
140// ================================================================================================
141
142/// Rescue round constants;
143/// computed as in [specifications](https://github.com/ASDiscreteMathematics/rpo)
144///
145/// The constants are broken up into two arrays ARK1 and ARK2; ARK1 contains the constants for the
146/// first half of RPO round, and ARK2 contains constants for the second half of RPO round.
147const ARK1: [[Felt; STATE_WIDTH]; NUM_ROUNDS] = [
148 [
149 Felt::new(5789762306288267392),
150 Felt::new(6522564764413701783),
151 Felt::new(17809893479458208203),
152 Felt::new(107145243989736508),
153 Felt::new(6388978042437517382),
154 Felt::new(15844067734406016715),
155 Felt::new(9975000513555218239),
156 Felt::new(3344984123768313364),
157 Felt::new(9959189626657347191),
158 Felt::new(12960773468763563665),
159 Felt::new(9602914297752488475),
160 Felt::new(16657542370200465908),
161 ],
162 [
163 Felt::new(12987190162843096997),
164 Felt::new(653957632802705281),
165 Felt::new(4441654670647621225),
166 Felt::new(4038207883745915761),
167 Felt::new(5613464648874830118),
168 Felt::new(13222989726778338773),
169 Felt::new(3037761201230264149),
170 Felt::new(16683759727265180203),
171 Felt::new(8337364536491240715),
172 Felt::new(3227397518293416448),
173 Felt::new(8110510111539674682),
174 Felt::new(2872078294163232137),
175 ],
176 [
177 Felt::new(18072785500942327487),
178 Felt::new(6200974112677013481),
179 Felt::new(17682092219085884187),
180 Felt::new(10599526828986756440),
181 Felt::new(975003873302957338),
182 Felt::new(8264241093196931281),
183 Felt::new(10065763900435475170),
184 Felt::new(2181131744534710197),
185 Felt::new(6317303992309418647),
186 Felt::new(1401440938888741532),
187 Felt::new(8884468225181997494),
188 Felt::new(13066900325715521532),
189 ],
190 [
191 Felt::new(5674685213610121970),
192 Felt::new(5759084860419474071),
193 Felt::new(13943282657648897737),
194 Felt::new(1352748651966375394),
195 Felt::new(17110913224029905221),
196 Felt::new(1003883795902368422),
197 Felt::new(4141870621881018291),
198 Felt::new(8121410972417424656),
199 Felt::new(14300518605864919529),
200 Felt::new(13712227150607670181),
201 Felt::new(17021852944633065291),
202 Felt::new(6252096473787587650),
203 ],
204 [
205 Felt::new(4887609836208846458),
206 Felt::new(3027115137917284492),
207 Felt::new(9595098600469470675),
208 Felt::new(10528569829048484079),
209 Felt::new(7864689113198939815),
210 Felt::new(17533723827845969040),
211 Felt::new(5781638039037710951),
212 Felt::new(17024078752430719006),
213 Felt::new(109659393484013511),
214 Felt::new(7158933660534805869),
215 Felt::new(2955076958026921730),
216 Felt::new(7433723648458773977),
217 ],
218 [
219 Felt::new(16308865189192447297),
220 Felt::new(11977192855656444890),
221 Felt::new(12532242556065780287),
222 Felt::new(14594890931430968898),
223 Felt::new(7291784239689209784),
224 Felt::new(5514718540551361949),
225 Felt::new(10025733853830934803),
226 Felt::new(7293794580341021693),
227 Felt::new(6728552937464861756),
228 Felt::new(6332385040983343262),
229 Felt::new(13277683694236792804),
230 Felt::new(2600778905124452676),
231 ],
232 [
233 Felt::new(7123075680859040534),
234 Felt::new(1034205548717903090),
235 Felt::new(7717824418247931797),
236 Felt::new(3019070937878604058),
237 Felt::new(11403792746066867460),
238 Felt::new(10280580802233112374),
239 Felt::new(337153209462421218),
240 Felt::new(13333398568519923717),
241 Felt::new(3596153696935337464),
242 Felt::new(8104208463525993784),
243 Felt::new(14345062289456085693),
244 Felt::new(17036731477169661256),
245 ],
246];
247
248const ARK2: [[Felt; STATE_WIDTH]; NUM_ROUNDS] = [
249 [
250 Felt::new(6077062762357204287),
251 Felt::new(15277620170502011191),
252 Felt::new(5358738125714196705),
253 Felt::new(14233283787297595718),
254 Felt::new(13792579614346651365),
255 Felt::new(11614812331536767105),
256 Felt::new(14871063686742261166),
257 Felt::new(10148237148793043499),
258 Felt::new(4457428952329675767),
259 Felt::new(15590786458219172475),
260 Felt::new(10063319113072092615),
261 Felt::new(14200078843431360086),
262 ],
263 [
264 Felt::new(6202948458916099932),
265 Felt::new(17690140365333231091),
266 Felt::new(3595001575307484651),
267 Felt::new(373995945117666487),
268 Felt::new(1235734395091296013),
269 Felt::new(14172757457833931602),
270 Felt::new(707573103686350224),
271 Felt::new(15453217512188187135),
272 Felt::new(219777875004506018),
273 Felt::new(17876696346199469008),
274 Felt::new(17731621626449383378),
275 Felt::new(2897136237748376248),
276 ],
277 [
278 Felt::new(8023374565629191455),
279 Felt::new(15013690343205953430),
280 Felt::new(4485500052507912973),
281 Felt::new(12489737547229155153),
282 Felt::new(9500452585969030576),
283 Felt::new(2054001340201038870),
284 Felt::new(12420704059284934186),
285 Felt::new(355990932618543755),
286 Felt::new(9071225051243523860),
287 Felt::new(12766199826003448536),
288 Felt::new(9045979173463556963),
289 Felt::new(12934431667190679898),
290 ],
291 [
292 Felt::new(18389244934624494276),
293 Felt::new(16731736864863925227),
294 Felt::new(4440209734760478192),
295 Felt::new(17208448209698888938),
296 Felt::new(8739495587021565984),
297 Felt::new(17000774922218161967),
298 Felt::new(13533282547195532087),
299 Felt::new(525402848358706231),
300 Felt::new(16987541523062161972),
301 Felt::new(5466806524462797102),
302 Felt::new(14512769585918244983),
303 Felt::new(10973956031244051118),
304 ],
305 [
306 Felt::new(6982293561042362913),
307 Felt::new(14065426295947720331),
308 Felt::new(16451845770444974180),
309 Felt::new(7139138592091306727),
310 Felt::new(9012006439959783127),
311 Felt::new(14619614108529063361),
312 Felt::new(1394813199588124371),
313 Felt::new(4635111139507788575),
314 Felt::new(16217473952264203365),
315 Felt::new(10782018226466330683),
316 Felt::new(6844229992533662050),
317 Felt::new(7446486531695178711),
318 ],
319 [
320 Felt::new(3736792340494631448),
321 Felt::new(577852220195055341),
322 Felt::new(6689998335515779805),
323 Felt::new(13886063479078013492),
324 Felt::new(14358505101923202168),
325 Felt::new(7744142531772274164),
326 Felt::new(16135070735728404443),
327 Felt::new(12290902521256031137),
328 Felt::new(12059913662657709804),
329 Felt::new(16456018495793751911),
330 Felt::new(4571485474751953524),
331 Felt::new(17200392109565783176),
332 ],
333 [
334 Felt::new(17130398059294018733),
335 Felt::new(519782857322261988),
336 Felt::new(9625384390925085478),
337 Felt::new(1664893052631119222),
338 Felt::new(7629576092524553570),
339 Felt::new(3485239601103661425),
340 Felt::new(9755891797164033838),
341 Felt::new(15218148195153269027),
342 Felt::new(16460604813734957368),
343 Felt::new(9643968136937729763),
344 Felt::new(3611348709641382851),
345 Felt::new(18256379591337759196),
346 ],
347];