miden_crypto/hash/rescue/rpx/
digest.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
use alloc::string::String;
use core::{cmp::Ordering, fmt::Display, ops::Deref};

use super::{Digest, Felt, StarkField, DIGEST_BYTES, DIGEST_SIZE, ZERO};
use crate::{
    rand::Randomizable,
    utils::{
        bytes_to_hex_string, hex_to_bytes, ByteReader, ByteWriter, Deserializable,
        DeserializationError, HexParseError, Serializable,
    },
};

// DIGEST TRAIT IMPLEMENTATIONS
// ================================================================================================

#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(into = "String", try_from = "&str"))]
pub struct RpxDigest([Felt; DIGEST_SIZE]);

impl RpxDigest {
    /// The serialized size of the digest in bytes.
    pub const SERIALIZED_SIZE: usize = DIGEST_BYTES;

    pub const fn new(value: [Felt; DIGEST_SIZE]) -> Self {
        Self(value)
    }

    pub fn as_elements(&self) -> &[Felt] {
        self.as_ref()
    }

    pub fn as_bytes(&self) -> [u8; DIGEST_BYTES] {
        <Self as Digest>::as_bytes(self)
    }

    pub fn digests_as_elements<'a, I>(digests: I) -> impl Iterator<Item = &'a Felt>
    where
        I: Iterator<Item = &'a Self>,
    {
        digests.flat_map(|d| d.0.iter())
    }

    /// Returns hexadecimal representation of this digest prefixed with `0x`.
    pub fn to_hex(&self) -> String {
        bytes_to_hex_string(self.as_bytes())
    }
}

impl Digest for RpxDigest {
    fn as_bytes(&self) -> [u8; DIGEST_BYTES] {
        let mut result = [0; DIGEST_BYTES];

        result[..8].copy_from_slice(&self.0[0].as_int().to_le_bytes());
        result[8..16].copy_from_slice(&self.0[1].as_int().to_le_bytes());
        result[16..24].copy_from_slice(&self.0[2].as_int().to_le_bytes());
        result[24..].copy_from_slice(&self.0[3].as_int().to_le_bytes());

        result
    }
}

impl Deref for RpxDigest {
    type Target = [Felt; DIGEST_SIZE];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Ord for RpxDigest {
    fn cmp(&self, other: &Self) -> Ordering {
        // compare the inner u64 of both elements.
        //
        // it will iterate the elements and will return the first computation different than
        // `Equal`. Otherwise, the ordering is equal.
        //
        // the endianness is irrelevant here because since, this being a cryptographically secure
        // hash computation, the digest shouldn't have any ordered property of its input.
        //
        // finally, we use `Felt::inner` instead of `Felt::as_int` so we avoid performing a
        // montgomery reduction for every limb. that is safe because every inner element of the
        // digest is guaranteed to be in its canonical form (that is, `x in [0,p)`).
        self.0.iter().map(Felt::inner).zip(other.0.iter().map(Felt::inner)).fold(
            Ordering::Equal,
            |ord, (a, b)| match ord {
                Ordering::Equal => a.cmp(&b),
                _ => ord,
            },
        )
    }
}

impl PartialOrd for RpxDigest {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Display for RpxDigest {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let encoded: String = self.into();
        write!(f, "{}", encoded)?;
        Ok(())
    }
}

impl Randomizable for RpxDigest {
    const VALUE_SIZE: usize = DIGEST_BYTES;

    fn from_random_bytes(bytes: &[u8]) -> Option<Self> {
        let bytes_array: Option<[u8; 32]> = bytes.try_into().ok();
        if let Some(bytes_array) = bytes_array {
            Self::try_from(bytes_array).ok()
        } else {
            None
        }
    }
}

// CONVERSIONS: FROM RPX DIGEST
// ================================================================================================

#[derive(Copy, Clone, Debug)]
pub enum RpxDigestError {
    InvalidInteger,
}

impl TryFrom<&RpxDigest> for [bool; DIGEST_SIZE] {
    type Error = RpxDigestError;

    fn try_from(value: &RpxDigest) -> Result<Self, Self::Error> {
        (*value).try_into()
    }
}

impl TryFrom<RpxDigest> for [bool; DIGEST_SIZE] {
    type Error = RpxDigestError;

    fn try_from(value: RpxDigest) -> Result<Self, Self::Error> {
        fn to_bool(v: u64) -> Option<bool> {
            if v <= 1 {
                Some(v == 1)
            } else {
                None
            }
        }

        Ok([
            to_bool(value.0[0].as_int()).ok_or(RpxDigestError::InvalidInteger)?,
            to_bool(value.0[1].as_int()).ok_or(RpxDigestError::InvalidInteger)?,
            to_bool(value.0[2].as_int()).ok_or(RpxDigestError::InvalidInteger)?,
            to_bool(value.0[3].as_int()).ok_or(RpxDigestError::InvalidInteger)?,
        ])
    }
}

impl TryFrom<&RpxDigest> for [u8; DIGEST_SIZE] {
    type Error = RpxDigestError;

    fn try_from(value: &RpxDigest) -> Result<Self, Self::Error> {
        (*value).try_into()
    }
}

impl TryFrom<RpxDigest> for [u8; DIGEST_SIZE] {
    type Error = RpxDigestError;

    fn try_from(value: RpxDigest) -> Result<Self, Self::Error> {
        Ok([
            value.0[0].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
            value.0[1].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
            value.0[2].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
            value.0[3].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
        ])
    }
}

impl TryFrom<&RpxDigest> for [u16; DIGEST_SIZE] {
    type Error = RpxDigestError;

    fn try_from(value: &RpxDigest) -> Result<Self, Self::Error> {
        (*value).try_into()
    }
}

impl TryFrom<RpxDigest> for [u16; DIGEST_SIZE] {
    type Error = RpxDigestError;

    fn try_from(value: RpxDigest) -> Result<Self, Self::Error> {
        Ok([
            value.0[0].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
            value.0[1].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
            value.0[2].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
            value.0[3].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
        ])
    }
}

impl TryFrom<&RpxDigest> for [u32; DIGEST_SIZE] {
    type Error = RpxDigestError;

    fn try_from(value: &RpxDigest) -> Result<Self, Self::Error> {
        (*value).try_into()
    }
}

impl TryFrom<RpxDigest> for [u32; DIGEST_SIZE] {
    type Error = RpxDigestError;

    fn try_from(value: RpxDigest) -> Result<Self, Self::Error> {
        Ok([
            value.0[0].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
            value.0[1].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
            value.0[2].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
            value.0[3].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
        ])
    }
}

impl From<&RpxDigest> for [u64; DIGEST_SIZE] {
    fn from(value: &RpxDigest) -> Self {
        (*value).into()
    }
}

impl From<RpxDigest> for [u64; DIGEST_SIZE] {
    fn from(value: RpxDigest) -> Self {
        [
            value.0[0].as_int(),
            value.0[1].as_int(),
            value.0[2].as_int(),
            value.0[3].as_int(),
        ]
    }
}

impl From<&RpxDigest> for [Felt; DIGEST_SIZE] {
    fn from(value: &RpxDigest) -> Self {
        value.0
    }
}

impl From<RpxDigest> for [Felt; DIGEST_SIZE] {
    fn from(value: RpxDigest) -> Self {
        value.0
    }
}

impl From<&RpxDigest> for [u8; DIGEST_BYTES] {
    fn from(value: &RpxDigest) -> Self {
        value.as_bytes()
    }
}

impl From<RpxDigest> for [u8; DIGEST_BYTES] {
    fn from(value: RpxDigest) -> Self {
        value.as_bytes()
    }
}

impl From<&RpxDigest> for String {
    /// The returned string starts with `0x`.
    fn from(value: &RpxDigest) -> Self {
        (*value).into()
    }
}

impl From<RpxDigest> for String {
    /// The returned string starts with `0x`.
    fn from(value: RpxDigest) -> Self {
        value.to_hex()
    }
}

// CONVERSIONS: TO RPX DIGEST
// ================================================================================================

impl From<&[bool; DIGEST_SIZE]> for RpxDigest {
    fn from(value: &[bool; DIGEST_SIZE]) -> Self {
        (*value).into()
    }
}

impl From<[bool; DIGEST_SIZE]> for RpxDigest {
    fn from(value: [bool; DIGEST_SIZE]) -> Self {
        [value[0] as u32, value[1] as u32, value[2] as u32, value[3] as u32].into()
    }
}

impl From<&[u8; DIGEST_SIZE]> for RpxDigest {
    fn from(value: &[u8; DIGEST_SIZE]) -> Self {
        (*value).into()
    }
}

impl From<[u8; DIGEST_SIZE]> for RpxDigest {
    fn from(value: [u8; DIGEST_SIZE]) -> Self {
        Self([value[0].into(), value[1].into(), value[2].into(), value[3].into()])
    }
}

impl From<&[u16; DIGEST_SIZE]> for RpxDigest {
    fn from(value: &[u16; DIGEST_SIZE]) -> Self {
        (*value).into()
    }
}

impl From<[u16; DIGEST_SIZE]> for RpxDigest {
    fn from(value: [u16; DIGEST_SIZE]) -> Self {
        Self([value[0].into(), value[1].into(), value[2].into(), value[3].into()])
    }
}

impl From<&[u32; DIGEST_SIZE]> for RpxDigest {
    fn from(value: &[u32; DIGEST_SIZE]) -> Self {
        (*value).into()
    }
}

impl From<[u32; DIGEST_SIZE]> for RpxDigest {
    fn from(value: [u32; DIGEST_SIZE]) -> Self {
        Self([value[0].into(), value[1].into(), value[2].into(), value[3].into()])
    }
}

impl TryFrom<&[u64; DIGEST_SIZE]> for RpxDigest {
    type Error = RpxDigestError;

    fn try_from(value: &[u64; DIGEST_SIZE]) -> Result<Self, RpxDigestError> {
        (*value).try_into()
    }
}

impl TryFrom<[u64; DIGEST_SIZE]> for RpxDigest {
    type Error = RpxDigestError;

    fn try_from(value: [u64; DIGEST_SIZE]) -> Result<Self, RpxDigestError> {
        Ok(Self([
            value[0].try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
            value[1].try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
            value[2].try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
            value[3].try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
        ]))
    }
}

impl From<&[Felt; DIGEST_SIZE]> for RpxDigest {
    fn from(value: &[Felt; DIGEST_SIZE]) -> Self {
        Self(*value)
    }
}

impl From<[Felt; DIGEST_SIZE]> for RpxDigest {
    fn from(value: [Felt; DIGEST_SIZE]) -> Self {
        Self(value)
    }
}

impl TryFrom<&[u8; DIGEST_BYTES]> for RpxDigest {
    type Error = HexParseError;

    fn try_from(value: &[u8; DIGEST_BYTES]) -> Result<Self, Self::Error> {
        (*value).try_into()
    }
}

impl TryFrom<[u8; DIGEST_BYTES]> for RpxDigest {
    type Error = HexParseError;

    fn try_from(value: [u8; DIGEST_BYTES]) -> Result<Self, Self::Error> {
        // Note: the input length is known, the conversion from slice to array must succeed so the
        // `unwrap`s below are safe
        let a = u64::from_le_bytes(value[0..8].try_into().unwrap());
        let b = u64::from_le_bytes(value[8..16].try_into().unwrap());
        let c = u64::from_le_bytes(value[16..24].try_into().unwrap());
        let d = u64::from_le_bytes(value[24..32].try_into().unwrap());

        if [a, b, c, d].iter().any(|v| *v >= Felt::MODULUS) {
            return Err(HexParseError::OutOfRange);
        }

        Ok(RpxDigest([Felt::new(a), Felt::new(b), Felt::new(c), Felt::new(d)]))
    }
}

impl TryFrom<&[u8]> for RpxDigest {
    type Error = HexParseError;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        (*value).try_into()
    }
}

impl TryFrom<&str> for RpxDigest {
    type Error = HexParseError;

    /// Expects the string to start with `0x`.
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        hex_to_bytes::<DIGEST_BYTES>(value).and_then(RpxDigest::try_from)
    }
}

impl TryFrom<&String> for RpxDigest {
    type Error = HexParseError;

    /// Expects the string to start with `0x`.
    fn try_from(value: &String) -> Result<Self, Self::Error> {
        value.as_str().try_into()
    }
}

impl TryFrom<String> for RpxDigest {
    type Error = HexParseError;

    /// Expects the string to start with `0x`.
    fn try_from(value: String) -> Result<Self, Self::Error> {
        value.as_str().try_into()
    }
}

// SERIALIZATION / DESERIALIZATION
// ================================================================================================

impl Serializable for RpxDigest {
    fn write_into<W: ByteWriter>(&self, target: &mut W) {
        target.write_bytes(&self.as_bytes());
    }

    fn get_size_hint(&self) -> usize {
        Self::SERIALIZED_SIZE
    }
}

impl Deserializable for RpxDigest {
    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
        let mut inner: [Felt; DIGEST_SIZE] = [ZERO; DIGEST_SIZE];
        for inner in inner.iter_mut() {
            let e = source.read_u64()?;
            if e >= Felt::MODULUS {
                return Err(DeserializationError::InvalidValue(String::from(
                    "Value not in the appropriate range",
                )));
            }
            *inner = Felt::new(e);
        }

        Ok(Self(inner))
    }
}

// ITERATORS
// ================================================================================================
impl IntoIterator for RpxDigest {
    type Item = Felt;
    type IntoIter = <[Felt; 4] as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

// TESTS
// ================================================================================================

#[cfg(test)]
mod tests {
    use alloc::string::String;
    use rand_utils::rand_value;

    use super::{Deserializable, Felt, RpxDigest, Serializable, DIGEST_BYTES, DIGEST_SIZE};
    use crate::utils::SliceReader;

    #[test]
    fn digest_serialization() {
        let e1 = Felt::new(rand_value());
        let e2 = Felt::new(rand_value());
        let e3 = Felt::new(rand_value());
        let e4 = Felt::new(rand_value());

        let d1 = RpxDigest([e1, e2, e3, e4]);

        let mut bytes = vec![];
        d1.write_into(&mut bytes);
        assert_eq!(DIGEST_BYTES, bytes.len());
        assert_eq!(bytes.len(), d1.get_size_hint());

        let mut reader = SliceReader::new(&bytes);
        let d2 = RpxDigest::read_from(&mut reader).unwrap();

        assert_eq!(d1, d2);
    }

    #[test]
    fn digest_encoding() {
        let digest = RpxDigest([
            Felt::new(rand_value()),
            Felt::new(rand_value()),
            Felt::new(rand_value()),
            Felt::new(rand_value()),
        ]);

        let string: String = digest.into();
        let round_trip: RpxDigest = string.try_into().expect("decoding failed");

        assert_eq!(digest, round_trip);
    }

    #[test]
    fn test_conversions() {
        let digest = RpxDigest([
            Felt::new(rand_value()),
            Felt::new(rand_value()),
            Felt::new(rand_value()),
            Felt::new(rand_value()),
        ]);

        // BY VALUE
        // ----------------------------------------------------------------------------------------
        let v: [bool; DIGEST_SIZE] = [true, false, true, true];
        let v2: RpxDigest = v.into();
        assert_eq!(v, <[bool; DIGEST_SIZE]>::try_from(v2).unwrap());

        let v: [u8; DIGEST_SIZE] = [0_u8, 1_u8, 2_u8, 3_u8];
        let v2: RpxDigest = v.into();
        assert_eq!(v, <[u8; DIGEST_SIZE]>::try_from(v2).unwrap());

        let v: [u16; DIGEST_SIZE] = [0_u16, 1_u16, 2_u16, 3_u16];
        let v2: RpxDigest = v.into();
        assert_eq!(v, <[u16; DIGEST_SIZE]>::try_from(v2).unwrap());

        let v: [u32; DIGEST_SIZE] = [0_u32, 1_u32, 2_u32, 3_u32];
        let v2: RpxDigest = v.into();
        assert_eq!(v, <[u32; DIGEST_SIZE]>::try_from(v2).unwrap());

        let v: [u64; DIGEST_SIZE] = digest.into();
        let v2: RpxDigest = v.try_into().unwrap();
        assert_eq!(digest, v2);

        let v: [Felt; DIGEST_SIZE] = digest.into();
        let v2: RpxDigest = v.into();
        assert_eq!(digest, v2);

        let v: [u8; DIGEST_BYTES] = digest.into();
        let v2: RpxDigest = v.try_into().unwrap();
        assert_eq!(digest, v2);

        let v: String = digest.into();
        let v2: RpxDigest = v.try_into().unwrap();
        assert_eq!(digest, v2);

        // BY REF
        // ----------------------------------------------------------------------------------------
        let v: [bool; DIGEST_SIZE] = [true, false, true, true];
        let v2: RpxDigest = (&v).into();
        assert_eq!(v, <[bool; DIGEST_SIZE]>::try_from(&v2).unwrap());

        let v: [u8; DIGEST_SIZE] = [0_u8, 1_u8, 2_u8, 3_u8];
        let v2: RpxDigest = (&v).into();
        assert_eq!(v, <[u8; DIGEST_SIZE]>::try_from(&v2).unwrap());

        let v: [u16; DIGEST_SIZE] = [0_u16, 1_u16, 2_u16, 3_u16];
        let v2: RpxDigest = (&v).into();
        assert_eq!(v, <[u16; DIGEST_SIZE]>::try_from(&v2).unwrap());

        let v: [u32; DIGEST_SIZE] = [0_u32, 1_u32, 2_u32, 3_u32];
        let v2: RpxDigest = (&v).into();
        assert_eq!(v, <[u32; DIGEST_SIZE]>::try_from(&v2).unwrap());

        let v: [u64; DIGEST_SIZE] = (&digest).into();
        let v2: RpxDigest = (&v).try_into().unwrap();
        assert_eq!(digest, v2);

        let v: [Felt; DIGEST_SIZE] = (&digest).into();
        let v2: RpxDigest = (&v).into();
        assert_eq!(digest, v2);

        let v: [u8; DIGEST_BYTES] = (&digest).into();
        let v2: RpxDigest = (&v).try_into().unwrap();
        assert_eq!(digest, v2);

        let v: String = (&digest).into();
        let v2: RpxDigest = (&v).try_into().unwrap();
        assert_eq!(digest, v2);
    }
}