p3_symmetric/
serializing_hasher.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
use core::iter;

use p3_field::{PackedValue, PrimeField32, PrimeField64};

use crate::CryptographicHasher;

/// Serializes 32-bit field elements to bytes (i.e. the little-endian encoding of their canonical
/// values), then hashes those bytes using some inner hasher, and outputs a `[u8; 32]`.
#[derive(Copy, Clone, Debug)]
pub struct SerializingHasher32<Inner> {
    inner: Inner,
}

/// Serializes 32-bit field elements to u64s (packing two canonical values together), then hashes
/// those u64s using some inner hasher, and outputs a `[u64; 4]`.
#[derive(Copy, Clone, Debug)]
pub struct SerializingHasher32To64<Inner> {
    inner: Inner,
}

/// Serializes 64-bit field elements to bytes (i.e. the little-endian encoding of their canonical
/// values), then hashes those bytes using some inner hasher, and outputs a `[u8; 32]`.
#[derive(Copy, Clone, Debug)]
pub struct SerializingHasher64<Inner> {
    inner: Inner,
}

impl<Inner> SerializingHasher32<Inner> {
    pub const fn new(inner: Inner) -> Self {
        Self { inner }
    }
}

impl<Inner> SerializingHasher32To64<Inner> {
    pub const fn new(inner: Inner) -> Self {
        Self { inner }
    }
}

impl<Inner> SerializingHasher64<Inner> {
    pub const fn new(inner: Inner) -> Self {
        Self { inner }
    }
}

impl<F, Inner> CryptographicHasher<F, [u8; 32]> for SerializingHasher32<Inner>
where
    F: PrimeField32,
    Inner: CryptographicHasher<u8, [u8; 32]>,
{
    fn hash_iter<I>(&self, input: I) -> [u8; 32]
    where
        I: IntoIterator<Item = F>,
    {
        self.inner.hash_iter(
            input
                .into_iter()
                .flat_map(|x| x.as_canonical_u32().to_le_bytes()),
        )
    }
}

impl<P, PW, Inner> CryptographicHasher<P, [PW; 8]> for SerializingHasher32<Inner>
where
    P: PackedValue,
    P::Value: PrimeField32,
    PW: PackedValue<Value = u32>,
    Inner: CryptographicHasher<PW, [PW; 8]>,
{
    fn hash_iter<I>(&self, input: I) -> [PW; 8]
    where
        I: IntoIterator<Item = P>,
    {
        self.inner.hash_iter(
            input
                .into_iter()
                .map(|x| PW::from_fn(|i| x.as_slice()[i].as_canonical_u32())),
        )
    }
}

impl<P, PW, Inner> CryptographicHasher<P, [PW; 4]> for SerializingHasher32To64<Inner>
where
    P: PackedValue,
    P::Value: PrimeField32,
    PW: PackedValue<Value = u64>,
    Inner: CryptographicHasher<PW, [PW; 4]>,
{
    fn hash_iter<I>(&self, input: I) -> [PW; 4]
    where
        I: IntoIterator<Item = P>,
    {
        assert_eq!(P::WIDTH, PW::WIDTH);
        let mut input = input.into_iter();
        self.inner.hash_iter(iter::from_fn(
            #[inline]
            || {
                let a = input.next();
                let b = input.next();
                if let (Some(a), Some(b)) = (a, b) {
                    let ab = PW::from_fn(|i| {
                        let a_i = a.as_slice()[i].as_canonical_u64();
                        let b_i = b.as_slice()[i].as_canonical_u64();
                        a_i | (b_i << 32)
                    });
                    Some(ab)
                } else {
                    a.map(|a| PW::from_fn(|i| a.as_slice()[i].as_canonical_u64()))
                }
            },
        ))
    }
}

impl<F, Inner> CryptographicHasher<F, [u8; 32]> for SerializingHasher64<Inner>
where
    F: PrimeField64,
    Inner: CryptographicHasher<u8, [u8; 32]>,
{
    fn hash_iter<I>(&self, input: I) -> [u8; 32]
    where
        I: IntoIterator<Item = F>,
    {
        self.inner.hash_iter(
            input
                .into_iter()
                .flat_map(|x| x.as_canonical_u64().to_le_bytes()),
        )
    }
}

impl<P, PW, Inner> CryptographicHasher<P, [PW; 4]> for SerializingHasher64<Inner>
where
    P: PackedValue,
    P::Value: PrimeField64,
    PW: PackedValue<Value = u64>,
    Inner: CryptographicHasher<PW, [PW; 4]>,
{
    fn hash_iter<I>(&self, input: I) -> [PW; 4]
    where
        I: IntoIterator<Item = P>,
    {
        self.inner.hash_iter(
            input
                .into_iter()
                .map(|x| PW::from_fn(|i| x.as_slice()[i].as_canonical_u64())),
        )
    }
}