arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
miden-crypto/crypto/src/hash/rpo/mod.rs
crypto/src/hash/rpo/mod.rsRaw
  1use super::{ElementHasher, HashFn};
  2use crate::{Felt, FieldElement, StarkField, ONE, ZERO};
  3use core::{convert::TryInto, ops::Range};
  4
  5mod digest;
  6pub use digest::RpoDigest256;
  7
  8#[cfg(test)]
  9mod tests;
 10
 11mod mds_freq;
 12use mds_freq::mds_multiply_freq;
 13
 14// CONSTANTS
 15// ================================================================================================
 16
 17/// Sponge state is set to 12 field elements or 96 bytes; 8 elements are reserved for rate and
 18/// the remaining 4 elements are reserved for capacity.
 19const STATE_WIDTH: usize = 12;
 20
 21/// The rate portion of the state is located in elements 4 through 11.
 22const RATE_RANGE: Range<usize> = 4..12;
 23const RATE_WIDTH: usize = RATE_RANGE.end - RATE_RANGE.start;
 24
 25const INPUT1_RANGE: Range<usize> = 4..8;
 26const INPUT2_RANGE: Range<usize> = 8..12;
 27
 28/// The capacity portion of the state is located in elements 0, 1, 2, and 3.
 29const CAPACITY_RANGE: Range<usize> = 0..4;
 30
 31/// The output of the hash function is a digest which consists of 4 field elements or 32 bytes.
 32///
 33/// The digest is returned from state elements 4, 5, 6, and 7 (the first four elements of the
 34/// rate portion).
 35const DIGEST_RANGE: Range<usize> = 4..8;
 36const DIGEST_SIZE: usize = DIGEST_RANGE.end - DIGEST_RANGE.start;
 37
 38/// The number of rounds is set to 7 to target 128-bit security level
 39const NUM_ROUNDS: usize = 7;
 40
 41/// S-Box and Inverse S-Box powers;
 42///
 43/// The constants are defined for tests only because the exponentiations in the code are unrolled
 44/// for efficiency reasons.
 45#[cfg(test)]
 46const ALPHA: u64 = 7;
 47#[cfg(test)]
 48const INV_ALPHA: u64 = 10540996611094048183;
 49
 50// HASHER IMPLEMENTATION
 51// ================================================================================================
 52
 53/// Implementation of [Hasher] trait for Rescue Prime Optimized (Rpo256) hash function with 256-bit output.
 54///
 55/// The hash function is implemented according to the Rescue Prime Optimized
 56/// [specifications](https://github.com/ASDiscreteMathematics/rpo)
 57///
 58/// The parameters used to instantiate the function are:
 59/// * Field: 64-bit prime field with modulus 2^64 - 2^32 + 1.
 60/// * State width: 12 field elements.
 61/// * Capacity size: 4 field elements.
 62/// * Number of founds: 7.
 63/// * S-Box degree: 7.
 64///
 65/// The above parameters target 128-bit security level. The digest consists of four field elements
 66/// and it can be serialized into 32 bytes (256 bits).
 67///
 68/// ## Hash output consistency
 69/// Functions [hash_elements()](Rpo256::hash_elements), [merge()](Rpo256::merge), and
 70/// [merge_with_int()](Rpo256::merge_with_int) are internally consistent. That is, computing
 71/// a hash for the same set of elements using these functions will always produce the same
 72/// result. For example, merging two digests using [merge()](Rpo256::merge) will produce the
 73/// same result as hashing 8 elements which make up these digests using
 74/// [hash_elements()](Rpo256::hash_elements) function.
 75///
 76/// However, [hash()](Rpo256::hash) function is not consistent with functions mentioned above.
 77/// For example, if we take two field elements, serialize them to bytes and hash them using
 78/// [hash()](Rpo256::hash), the result will differ from the result obtained by hashing these
 79/// elements directly using [hash_elements()](Rpo256::hash_elements) function. The reason for
 80/// this difference is that [hash()](Rpo256::hash) function needs to be able to handle
 81/// arbitrary binary strings, which may or may not encode valid field elements - and thus,
 82/// deserialization procedure used by this function is different from the procedure used to
 83/// deserialize valid field elements.
 84///
 85/// Thus, if the underlying data consists of valid field elements, it might make more sense
 86/// to deserialize them into field elements and then hash them using
 87/// [hash_elements()](Rpo256::hash_elements) function rather then hashing the serialized bytes
 88/// using [hash()](Rpo256::hash) function.
 89pub struct Rpo256();
 90
 91impl HashFn for Rpo256 {
 92    type Digest = RpoDigest256;
 93
 94    fn hash(bytes: &[u8]) -> Self::Digest {
 95        // compute the number of elements required to represent the string; we will be processing
 96        // the string in 7-byte chunks, thus the number of elements will be equal to the number
 97        // of such chunks (including a potential partial chunk at the end).
 98        let num_elements = if bytes.len() % 7 == 0 {
 99            bytes.len() / 7
100        } else {
101            bytes.len() / 7 + 1
102        };
103
104        // initialize state to all zeros, except for the first element of the capacity part, which
105        // is set to the number of elements to be hashed. this is done so that adding zero elements
106        // at the end of the list always results in a different hash.
107        let mut state = [ZERO; STATE_WIDTH];
108        state[CAPACITY_RANGE.start] = Felt::new(num_elements as u64);
109
110        // break the string into 7-byte chunks, convert each chunk into a field element, and
111        // absorb the element into the rate portion of the state. we use 7-byte chunks because
112        // every 7-byte chunk is guaranteed to map to some field element.
113        let mut i = 0;
114        let mut buf = [0_u8; 8];
115        for chunk in bytes.chunks(7) {
116            if i < num_elements - 1 {
117                buf[..7].copy_from_slice(chunk);
118            } else {
119                // if we are dealing with the last chunk, it may be smaller than 7 bytes long, so
120                // we need to handle it slightly differently. we also append a byte with value 1
121                // to the end of the string; this pads the string in such a way that adding
122                // trailing zeros results in different hash
123                let chunk_len = chunk.len();
124                buf = [0_u8; 8];
125                buf[..chunk_len].copy_from_slice(chunk);
126                buf[chunk_len] = 1;
127            }
128
129            // convert the bytes into a field element and absorb it into the rate portion of the
130            // state; if the rate is filled up, apply the Rescue permutation and start absorbing
131            // again from zero index.
132            state[RATE_RANGE.start + i] += Felt::new(u64::from_le_bytes(buf));
133            i += 1;
134            if i % RATE_WIDTH == 0 {
135                Self::apply_permutation(&mut state);
136                i = 0;
137            }
138        }
139
140        // if we absorbed some elements but didn't apply a permutation to them (would happen when
141        // the number of elements is not a multiple of RATE_WIDTH), apply the Rescue permutation.
142        // we don't need to apply any extra padding because we injected total number of elements
143        // in the input list into the capacity portion of the state during initialization.
144        if i > 0 {
145            Self::apply_permutation(&mut state);
146        }
147
148        // return the first 4 elements of the state as hash result
149        RpoDigest256::new(state[DIGEST_RANGE].try_into().unwrap())
150    }
151
152    fn merge(values: &[Self::Digest; 2]) -> Self::Digest {
153        // initialize the state by copying the digest elements into the rate portion of the state
154        // (8 total elements), and set the capacity elements to 0.
155        let mut state = [ZERO; STATE_WIDTH];
156        let it = Self::Digest::digests_as_elements(values.into_iter());
157        for (i, v) in it.enumerate() {
158            state[RATE_RANGE.start + i] = *v;
159        }
160
161        // apply the RPO permutation and return the first four elements of the state
162        Self::apply_permutation(&mut state);
163        RpoDigest256::new(state[DIGEST_RANGE].try_into().unwrap())
164    }
165
166    fn merge_with_int(seed: Self::Digest, value: u64) -> Self::Digest {
167        // initialize the state as follows:
168        // - seed is copied into the first 4 elements of the rate portion of the state.
169        // - if the value fits into a single field element, copy it into the fifth rate element
170        //   and set the sixth rate element to 1.
171        // - if the value doesn't fit into a single field element, split it into two field
172        //   elements, copy them into rate elements 5 and 6, and set the seventh rate element
173        //   to 1.
174        // - set the first capacity element to 1
175        let mut state = [ZERO; STATE_WIDTH];
176        state[INPUT1_RANGE].copy_from_slice(seed.as_elements());
177        state[INPUT2_RANGE.start] = Felt::new(value);
178        if value < Felt::MODULUS {
179            state[INPUT2_RANGE.start + 1] = ONE;
180        } else {
181            state[INPUT2_RANGE.start + 1] = Felt::new(value / Felt::MODULUS);
182            state[INPUT2_RANGE.start + 2] = ONE;
183        }
184
185        // common padding for both cases
186        state[CAPACITY_RANGE.start] = ONE;
187
188        // apply the RPO permutation and return the first four elements of the state
189        Self::apply_permutation(&mut state);
190        RpoDigest256::new(state[DIGEST_RANGE].try_into().unwrap())
191    }
192}
193
194impl ElementHasher for Rpo256 {
195    type BaseField = Felt;
196
197    fn hash_elements<E: FieldElement<BaseField = Self::BaseField>>(elements: &[E]) -> Self::Digest {
198        // convert the elements into a list of base field elements
199        let elements = E::as_base_elements(elements);
200
201        // initialize state to all zeros, except for the first element of the capacity part, which
202        // is set to 1 if the number of elements is not a multiple of RATE_WIDTH.
203        let mut state = [ZERO; STATE_WIDTH];
204        if elements.len() % RATE_WIDTH != 0 {
205            state[CAPACITY_RANGE.start] = ONE;
206        }
207
208        // absorb elements into the state one by one until the rate portion of the state is filled
209        // up; then apply the Rescue permutation and start absorbing again; repeat until all
210        // elements have been absorbed
211        let mut i = 0;
212        for &element in elements.iter() {
213            state[RATE_RANGE.start + i] = element;
214            i += 1;
215            if i % RATE_WIDTH == 0 {
216                Self::apply_permutation(&mut state);
217                i = 0;
218            }
219        }
220
221        // if we absorbed some elements but didn't apply a permutation to them (would happen when
222        // the number of elements is not a multiple of RATE_WIDTH), apply the RPO permutation after
223        // padding by appending a 1 followed by as many 0 as necessary to make the input length a
224        // multiple of the RATE_WIDTH.
225        if i > 0 {
226            state[RATE_RANGE.start + i] = ONE;
227            i += 1;
228            while i != RATE_WIDTH {
229                state[RATE_RANGE.start + i] = ZERO;
230                i += 1;
231            }
232            Self::apply_permutation(&mut state);
233        }
234
235        // return the first 4 elements of the state as hash result
236        RpoDigest256::new(state[DIGEST_RANGE].try_into().unwrap())
237    }
238}
239
240// HASH FUNCTION IMPLEMENTATION
241// ================================================================================================
242
243impl Rpo256 {
244    // RESCUE PERMUTATION
245    // --------------------------------------------------------------------------------------------
246
247    /// Applies RPO permutation to the provided state.
248    pub fn apply_permutation(state: &mut [Felt; STATE_WIDTH]) {
249        for i in 0..NUM_ROUNDS {
250            Self::apply_round(state, i);
251        }
252    }
253
254    /// RPO round function.
255    #[inline(always)]
256    pub fn apply_round(state: &mut [Felt; STATE_WIDTH], round: usize) {
257        // apply first half of RPO round
258        Self::apply_mds(state);
259        Self::add_constants(state, &ARK1[round]);
260        Self::apply_sbox(state);
261
262        // apply second half of RPO round
263        Self::apply_mds(state);
264        Self::add_constants(state, &ARK2[round]);
265        Self::apply_inv_sbox(state);
266    }
267
268    // HELPER FUNCTIONS
269    // --------------------------------------------------------------------------------------------
270
271    #[inline(always)]
272    fn apply_mds(state: &mut [Felt; STATE_WIDTH]) {
273        let mut result = [ZERO; STATE_WIDTH];
274
275        // Using the linearity of the operations we can split the state into a low||high decomposition
276        // and operate on each with no overflow and then combine/reduce the result to a field element.
277        // The no overflow is guaranteed by the fact that the MDS matrix is a small powers of two in
278        // frequency domain.
279        let mut state_l = [0u64; STATE_WIDTH];
280        let mut state_h = [0u64; STATE_WIDTH];
281
282        for r in 0..STATE_WIDTH {
283            let s = state[r].inner();
284            state_h[r] = s >> 32;
285            state_l[r] = (s as u32) as u64;
286        }
287
288        let state_h = mds_multiply_freq(state_h);
289        let state_l = mds_multiply_freq(state_l);
290
291        for r in 0..STATE_WIDTH {
292            let s = state_l[r] as u128 + ((state_h[r] as u128) << 32);
293            let s_hi = (s >> 64) as u64;
294            let s_lo = s as u64;
295            let z = (s_hi << 32) - s_hi;
296            let (res, over) = s_lo.overflowing_add(z);
297
298            result[r] = Felt::from_mont(res.wrapping_add(0u32.wrapping_sub(over as u32) as u64));
299        }
300        *state = result;
301    }
302
303    #[inline(always)]
304    fn add_constants(state: &mut [Felt; STATE_WIDTH], ark: &[Felt; STATE_WIDTH]) {
305        state.iter_mut().zip(ark).for_each(|(s, &k)| *s += k);
306    }
307
308    #[inline(always)]
309    fn apply_sbox(state: &mut [Felt; STATE_WIDTH]) {
310        state[0] = state[0].exp7();
311        state[1] = state[1].exp7();
312        state[2] = state[2].exp7();
313        state[3] = state[3].exp7();
314        state[4] = state[4].exp7();
315        state[5] = state[5].exp7();
316        state[6] = state[6].exp7();
317        state[7] = state[7].exp7();
318        state[8] = state[8].exp7();
319        state[9] = state[9].exp7();
320        state[10] = state[10].exp7();
321        state[11] = state[11].exp7();
322    }
323
324    #[inline(always)]
325    fn apply_inv_sbox(state: &mut [Felt; STATE_WIDTH]) {
326        // compute base^10540996611094048183 using 72 multiplications per array element
327        // 10540996611094048183 = b1001001001001001001001001001000110110110110110110110110110110111
328
329        // compute base^10
330        let mut t1 = *state;
331        t1.iter_mut().for_each(|t| *t = t.square());
332
333        // compute base^100
334        let mut t2 = t1;
335        t2.iter_mut().for_each(|t| *t = t.square());
336
337        // compute base^100100
338        let t3 = Self::exp_acc::<Felt, STATE_WIDTH, 3>(t2, t2);
339
340        // compute base^100100100100
341        let t4 = Self::exp_acc::<Felt, STATE_WIDTH, 6>(t3, t3);
342
343        // compute base^100100100100100100100100
344        let t5 = Self::exp_acc::<Felt, STATE_WIDTH, 12>(t4, t4);
345
346        // compute base^100100100100100100100100100100
347        let t6 = Self::exp_acc::<Felt, STATE_WIDTH, 6>(t5, t3);
348
349        // compute base^1001001001001001001001001001000100100100100100100100100100100
350        let t7 = Self::exp_acc::<Felt, STATE_WIDTH, 31>(t6, t6);
351
352        // compute base^1001001001001001001001001001000110110110110110110110110110110111
353        for (i, s) in state.iter_mut().enumerate() {
354            let a = (t7[i].square() * t6[i]).square().square();
355            let b = t1[i] * t2[i] * *s;
356            *s = a * b;
357        }
358    }
359
360    #[inline(always)]
361    fn exp_acc<B: StarkField, const N: usize, const M: usize>(
362        base: [B; N],
363        tail: [B; N],
364    ) -> [B; N] {
365        let mut result = base;
366        for _ in 0..M {
367            result.iter_mut().for_each(|r| *r = r.square());
368        }
369        result.iter_mut().zip(tail).for_each(|(r, t)| *r *= t);
370        result
371    }
372}
373
374// MDS
375// ================================================================================================
376/// RPO MDS matrix
377const MDS: [[Felt; STATE_WIDTH]; STATE_WIDTH] = [
378    [
379        Felt::new(7),
380        Felt::new(23),
381        Felt::new(8),
382        Felt::new(26),
383        Felt::new(13),
384        Felt::new(10),
385        Felt::new(9),
386        Felt::new(7),
387        Felt::new(6),
388        Felt::new(22),
389        Felt::new(21),
390        Felt::new(8),
391    ],
392    [
393        Felt::new(8),
394        Felt::new(7),
395        Felt::new(23),
396        Felt::new(8),
397        Felt::new(26),
398        Felt::new(13),
399        Felt::new(10),
400        Felt::new(9),
401        Felt::new(7),
402        Felt::new(6),
403        Felt::new(22),
404        Felt::new(21),
405    ],
406    [
407        Felt::new(21),
408        Felt::new(8),
409        Felt::new(7),
410        Felt::new(23),
411        Felt::new(8),
412        Felt::new(26),
413        Felt::new(13),
414        Felt::new(10),
415        Felt::new(9),
416        Felt::new(7),
417        Felt::new(6),
418        Felt::new(22),
419    ],
420    [
421        Felt::new(22),
422        Felt::new(21),
423        Felt::new(8),
424        Felt::new(7),
425        Felt::new(23),
426        Felt::new(8),
427        Felt::new(26),
428        Felt::new(13),
429        Felt::new(10),
430        Felt::new(9),
431        Felt::new(7),
432        Felt::new(6),
433    ],
434    [
435        Felt::new(6),
436        Felt::new(22),
437        Felt::new(21),
438        Felt::new(8),
439        Felt::new(7),
440        Felt::new(23),
441        Felt::new(8),
442        Felt::new(26),
443        Felt::new(13),
444        Felt::new(10),
445        Felt::new(9),
446        Felt::new(7),
447    ],
448    [
449        Felt::new(7),
450        Felt::new(6),
451        Felt::new(22),
452        Felt::new(21),
453        Felt::new(8),
454        Felt::new(7),
455        Felt::new(23),
456        Felt::new(8),
457        Felt::new(26),
458        Felt::new(13),
459        Felt::new(10),
460        Felt::new(9),
461    ],
462    [
463        Felt::new(9),
464        Felt::new(7),
465        Felt::new(6),
466        Felt::new(22),
467        Felt::new(21),
468        Felt::new(8),
469        Felt::new(7),
470        Felt::new(23),
471        Felt::new(8),
472        Felt::new(26),
473        Felt::new(13),
474        Felt::new(10),
475    ],
476    [
477        Felt::new(10),
478        Felt::new(9),
479        Felt::new(7),
480        Felt::new(6),
481        Felt::new(22),
482        Felt::new(21),
483        Felt::new(8),
484        Felt::new(7),
485        Felt::new(23),
486        Felt::new(8),
487        Felt::new(26),
488        Felt::new(13),
489    ],
490    [
491        Felt::new(13),
492        Felt::new(10),
493        Felt::new(9),
494        Felt::new(7),
495        Felt::new(6),
496        Felt::new(22),
497        Felt::new(21),
498        Felt::new(8),
499        Felt::new(7),
500        Felt::new(23),
501        Felt::new(8),
502        Felt::new(26),
503    ],
504    [
505        Felt::new(26),
506        Felt::new(13),
507        Felt::new(10),
508        Felt::new(9),
509        Felt::new(7),
510        Felt::new(6),
511        Felt::new(22),
512        Felt::new(21),
513        Felt::new(8),
514        Felt::new(7),
515        Felt::new(23),
516        Felt::new(8),
517    ],
518    [
519        Felt::new(8),
520        Felt::new(26),
521        Felt::new(13),
522        Felt::new(10),
523        Felt::new(9),
524        Felt::new(7),
525        Felt::new(6),
526        Felt::new(22),
527        Felt::new(21),
528        Felt::new(8),
529        Felt::new(7),
530        Felt::new(23),
531    ],
532    [
533        Felt::new(23),
534        Felt::new(8),
535        Felt::new(26),
536        Felt::new(13),
537        Felt::new(10),
538        Felt::new(9),
539        Felt::new(7),
540        Felt::new(6),
541        Felt::new(22),
542        Felt::new(21),
543        Felt::new(8),
544        Felt::new(7),
545    ],
546];
547
548/// RPO Inverse MDS matrix
549const INV_MDS: [[Felt; STATE_WIDTH]; STATE_WIDTH] = [
550    [
551        Felt::new(14868391535953158196),
552        Felt::new(13278298489594233127),
553        Felt::new(389999932707070822),
554        Felt::new(9782021734907796003),
555        Felt::new(4829905704463175582),
556        Felt::new(7567822018949214430),
557        Felt::new(14205019324568680367),
558        Felt::new(15489674211196160593),
559        Felt::new(17636013826542227504),
560        Felt::new(16254215311946436093),
561        Felt::new(3641486184877122796),
562        Felt::new(11069068059762973582),
563    ],
564    [
565        Felt::new(11069068059762973582),
566        Felt::new(14868391535953158196),
567        Felt::new(13278298489594233127),
568        Felt::new(389999932707070822),
569        Felt::new(9782021734907796003),
570        Felt::new(4829905704463175582),
571        Felt::new(7567822018949214430),
572        Felt::new(14205019324568680367),
573        Felt::new(15489674211196160593),
574        Felt::new(17636013826542227504),
575        Felt::new(16254215311946436093),
576        Felt::new(3641486184877122796),
577    ],
578    [
579        Felt::new(3641486184877122796),
580        Felt::new(11069068059762973582),
581        Felt::new(14868391535953158196),
582        Felt::new(13278298489594233127),
583        Felt::new(389999932707070822),
584        Felt::new(9782021734907796003),
585        Felt::new(4829905704463175582),
586        Felt::new(7567822018949214430),
587        Felt::new(14205019324568680367),
588        Felt::new(15489674211196160593),
589        Felt::new(17636013826542227504),
590        Felt::new(16254215311946436093),
591    ],
592    [
593        Felt::new(16254215311946436093),
594        Felt::new(3641486184877122796),
595        Felt::new(11069068059762973582),
596        Felt::new(14868391535953158196),
597        Felt::new(13278298489594233127),
598        Felt::new(389999932707070822),
599        Felt::new(9782021734907796003),
600        Felt::new(4829905704463175582),
601        Felt::new(7567822018949214430),
602        Felt::new(14205019324568680367),
603        Felt::new(15489674211196160593),
604        Felt::new(17636013826542227504),
605    ],
606    [
607        Felt::new(17636013826542227504),
608        Felt::new(16254215311946436093),
609        Felt::new(3641486184877122796),
610        Felt::new(11069068059762973582),
611        Felt::new(14868391535953158196),
612        Felt::new(13278298489594233127),
613        Felt::new(389999932707070822),
614        Felt::new(9782021734907796003),
615        Felt::new(4829905704463175582),
616        Felt::new(7567822018949214430),
617        Felt::new(14205019324568680367),
618        Felt::new(15489674211196160593),
619    ],
620    [
621        Felt::new(15489674211196160593),
622        Felt::new(17636013826542227504),
623        Felt::new(16254215311946436093),
624        Felt::new(3641486184877122796),
625        Felt::new(11069068059762973582),
626        Felt::new(14868391535953158196),
627        Felt::new(13278298489594233127),
628        Felt::new(389999932707070822),
629        Felt::new(9782021734907796003),
630        Felt::new(4829905704463175582),
631        Felt::new(7567822018949214430),
632        Felt::new(14205019324568680367),
633    ],
634    [
635        Felt::new(14205019324568680367),
636        Felt::new(15489674211196160593),
637        Felt::new(17636013826542227504),
638        Felt::new(16254215311946436093),
639        Felt::new(3641486184877122796),
640        Felt::new(11069068059762973582),
641        Felt::new(14868391535953158196),
642        Felt::new(13278298489594233127),
643        Felt::new(389999932707070822),
644        Felt::new(9782021734907796003),
645        Felt::new(4829905704463175582),
646        Felt::new(7567822018949214430),
647    ],
648    [
649        Felt::new(7567822018949214430),
650        Felt::new(14205019324568680367),
651        Felt::new(15489674211196160593),
652        Felt::new(17636013826542227504),
653        Felt::new(16254215311946436093),
654        Felt::new(3641486184877122796),
655        Felt::new(11069068059762973582),
656        Felt::new(14868391535953158196),
657        Felt::new(13278298489594233127),
658        Felt::new(389999932707070822),
659        Felt::new(9782021734907796003),
660        Felt::new(4829905704463175582),
661    ],
662    [
663        Felt::new(4829905704463175582),
664        Felt::new(7567822018949214430),
665        Felt::new(14205019324568680367),
666        Felt::new(15489674211196160593),
667        Felt::new(17636013826542227504),
668        Felt::new(16254215311946436093),
669        Felt::new(3641486184877122796),
670        Felt::new(11069068059762973582),
671        Felt::new(14868391535953158196),
672        Felt::new(13278298489594233127),
673        Felt::new(389999932707070822),
674        Felt::new(9782021734907796003),
675    ],
676    [
677        Felt::new(9782021734907796003),
678        Felt::new(4829905704463175582),
679        Felt::new(7567822018949214430),
680        Felt::new(14205019324568680367),
681        Felt::new(15489674211196160593),
682        Felt::new(17636013826542227504),
683        Felt::new(16254215311946436093),
684        Felt::new(3641486184877122796),
685        Felt::new(11069068059762973582),
686        Felt::new(14868391535953158196),
687        Felt::new(13278298489594233127),
688        Felt::new(389999932707070822),
689    ],
690    [
691        Felt::new(389999932707070822),
692        Felt::new(9782021734907796003),
693        Felt::new(4829905704463175582),
694        Felt::new(7567822018949214430),
695        Felt::new(14205019324568680367),
696        Felt::new(15489674211196160593),
697        Felt::new(17636013826542227504),
698        Felt::new(16254215311946436093),
699        Felt::new(3641486184877122796),
700        Felt::new(11069068059762973582),
701        Felt::new(14868391535953158196),
702        Felt::new(13278298489594233127),
703    ],
704    [
705        Felt::new(13278298489594233127),
706        Felt::new(389999932707070822),
707        Felt::new(9782021734907796003),
708        Felt::new(4829905704463175582),
709        Felt::new(7567822018949214430),
710        Felt::new(14205019324568680367),
711        Felt::new(15489674211196160593),
712        Felt::new(17636013826542227504),
713        Felt::new(16254215311946436093),
714        Felt::new(3641486184877122796),
715        Felt::new(11069068059762973582),
716        Felt::new(14868391535953158196),
717    ],
718];
719
720// ROUND CONSTANTS
721// ================================================================================================
722
723/// Rescue round constants;
724/// computed as in [specifications](https://github.com/ASDiscreteMathematics/rpo)
725///
726/// The constants are broken up into two arrays ARK1 and ARK2; ARK1 contains the constants for the
727/// first half of RPO round, and ARK2 contains constants for the second half of RPO round.
728const ARK1: [[Felt; STATE_WIDTH]; NUM_ROUNDS] = [
729    [
730        Felt::new(5789762306288267392),
731        Felt::new(6522564764413701783),
732        Felt::new(17809893479458208203),
733        Felt::new(107145243989736508),
734        Felt::new(6388978042437517382),
735        Felt::new(15844067734406016715),
736        Felt::new(9975000513555218239),
737        Felt::new(3344984123768313364),
738        Felt::new(9959189626657347191),
739        Felt::new(12960773468763563665),
740        Felt::new(9602914297752488475),
741        Felt::new(16657542370200465908),
742    ],
743    [
744        Felt::new(12987190162843096997),
745        Felt::new(653957632802705281),
746        Felt::new(4441654670647621225),
747        Felt::new(4038207883745915761),
748        Felt::new(5613464648874830118),
749        Felt::new(13222989726778338773),
750        Felt::new(3037761201230264149),
751        Felt::new(16683759727265180203),
752        Felt::new(8337364536491240715),
753        Felt::new(3227397518293416448),
754        Felt::new(8110510111539674682),
755        Felt::new(2872078294163232137),
756    ],
757    [
758        Felt::new(18072785500942327487),
759        Felt::new(6200974112677013481),
760        Felt::new(17682092219085884187),
761        Felt::new(10599526828986756440),
762        Felt::new(975003873302957338),
763        Felt::new(8264241093196931281),
764        Felt::new(10065763900435475170),
765        Felt::new(2181131744534710197),
766        Felt::new(6317303992309418647),
767        Felt::new(1401440938888741532),
768        Felt::new(8884468225181997494),
769        Felt::new(13066900325715521532),
770    ],
771    [
772        Felt::new(5674685213610121970),
773        Felt::new(5759084860419474071),
774        Felt::new(13943282657648897737),
775        Felt::new(1352748651966375394),
776        Felt::new(17110913224029905221),
777        Felt::new(1003883795902368422),
778        Felt::new(4141870621881018291),
779        Felt::new(8121410972417424656),
780        Felt::new(14300518605864919529),
781        Felt::new(13712227150607670181),
782        Felt::new(17021852944633065291),
783        Felt::new(6252096473787587650),
784    ],
785    [
786        Felt::new(4887609836208846458),
787        Felt::new(3027115137917284492),
788        Felt::new(9595098600469470675),
789        Felt::new(10528569829048484079),
790        Felt::new(7864689113198939815),
791        Felt::new(17533723827845969040),
792        Felt::new(5781638039037710951),
793        Felt::new(17024078752430719006),
794        Felt::new(109659393484013511),
795        Felt::new(7158933660534805869),
796        Felt::new(2955076958026921730),
797        Felt::new(7433723648458773977),
798    ],
799    [
800        Felt::new(16308865189192447297),
801        Felt::new(11977192855656444890),
802        Felt::new(12532242556065780287),
803        Felt::new(14594890931430968898),
804        Felt::new(7291784239689209784),
805        Felt::new(5514718540551361949),
806        Felt::new(10025733853830934803),
807        Felt::new(7293794580341021693),
808        Felt::new(6728552937464861756),
809        Felt::new(6332385040983343262),
810        Felt::new(13277683694236792804),
811        Felt::new(2600778905124452676),
812    ],
813    [
814        Felt::new(7123075680859040534),
815        Felt::new(1034205548717903090),
816        Felt::new(7717824418247931797),
817        Felt::new(3019070937878604058),
818        Felt::new(11403792746066867460),
819        Felt::new(10280580802233112374),
820        Felt::new(337153209462421218),
821        Felt::new(13333398568519923717),
822        Felt::new(3596153696935337464),
823        Felt::new(8104208463525993784),
824        Felt::new(14345062289456085693),
825        Felt::new(17036731477169661256),
826    ],
827];
828
829const ARK2: [[Felt; STATE_WIDTH]; NUM_ROUNDS] = [
830    [
831        Felt::new(6077062762357204287),
832        Felt::new(15277620170502011191),
833        Felt::new(5358738125714196705),
834        Felt::new(14233283787297595718),
835        Felt::new(13792579614346651365),
836        Felt::new(11614812331536767105),
837        Felt::new(14871063686742261166),
838        Felt::new(10148237148793043499),
839        Felt::new(4457428952329675767),
840        Felt::new(15590786458219172475),
841        Felt::new(10063319113072092615),
842        Felt::new(14200078843431360086),
843    ],
844    [
845        Felt::new(6202948458916099932),
846        Felt::new(17690140365333231091),
847        Felt::new(3595001575307484651),
848        Felt::new(373995945117666487),
849        Felt::new(1235734395091296013),
850        Felt::new(14172757457833931602),
851        Felt::new(707573103686350224),
852        Felt::new(15453217512188187135),
853        Felt::new(219777875004506018),
854        Felt::new(17876696346199469008),
855        Felt::new(17731621626449383378),
856        Felt::new(2897136237748376248),
857    ],
858    [
859        Felt::new(8023374565629191455),
860        Felt::new(15013690343205953430),
861        Felt::new(4485500052507912973),
862        Felt::new(12489737547229155153),
863        Felt::new(9500452585969030576),
864        Felt::new(2054001340201038870),
865        Felt::new(12420704059284934186),
866        Felt::new(355990932618543755),
867        Felt::new(9071225051243523860),
868        Felt::new(12766199826003448536),
869        Felt::new(9045979173463556963),
870        Felt::new(12934431667190679898),
871    ],
872    [
873        Felt::new(18389244934624494276),
874        Felt::new(16731736864863925227),
875        Felt::new(4440209734760478192),
876        Felt::new(17208448209698888938),
877        Felt::new(8739495587021565984),
878        Felt::new(17000774922218161967),
879        Felt::new(13533282547195532087),
880        Felt::new(525402848358706231),
881        Felt::new(16987541523062161972),
882        Felt::new(5466806524462797102),
883        Felt::new(14512769585918244983),
884        Felt::new(10973956031244051118),
885    ],
886    [
887        Felt::new(6982293561042362913),
888        Felt::new(14065426295947720331),
889        Felt::new(16451845770444974180),
890        Felt::new(7139138592091306727),
891        Felt::new(9012006439959783127),
892        Felt::new(14619614108529063361),
893        Felt::new(1394813199588124371),
894        Felt::new(4635111139507788575),
895        Felt::new(16217473952264203365),
896        Felt::new(10782018226466330683),
897        Felt::new(6844229992533662050),
898        Felt::new(7446486531695178711),
899    ],
900    [
901        Felt::new(3736792340494631448),
902        Felt::new(577852220195055341),
903        Felt::new(6689998335515779805),
904        Felt::new(13886063479078013492),
905        Felt::new(14358505101923202168),
906        Felt::new(7744142531772274164),
907        Felt::new(16135070735728404443),
908        Felt::new(12290902521256031137),
909        Felt::new(12059913662657709804),
910        Felt::new(16456018495793751911),
911        Felt::new(4571485474751953524),
912        Felt::new(17200392109565783176),
913    ],
914    [
915        Felt::new(17130398059294018733),
916        Felt::new(519782857322261988),
917        Felt::new(9625384390925085478),
918        Felt::new(1664893052631119222),
919        Felt::new(7629576092524553570),
920        Felt::new(3485239601103661425),
921        Felt::new(9755891797164033838),
922        Felt::new(15218148195153269027),
923        Felt::new(16460604813734957368),
924        Felt::new(9643968136937729763),
925        Felt::new(3611348709641382851),
926        Felt::new(18256379591337759196),
927    ],
928];