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