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