poseidon377/
hash.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
use crate::{Fq, Instance};

/// Hash a single [`Fq`] element with the provided `domain_separator`.
pub fn hash_1(domain_separator: &Fq, value: Fq) -> Fq {
    let params = &crate::RATE_1_PARAMS;
    let mut state = Instance::new(&params);
    state.n_to_1_fixed_hash(&[*domain_separator, value])
}

/// Hash two [`Fq`] elements with the provided `domain_separator`.
pub fn hash_2(domain_separator: &Fq, value: (Fq, Fq)) -> Fq {
    let params = &crate::RATE_2_PARAMS;
    let mut state = Instance::new(params);
    state.n_to_1_fixed_hash(&[*domain_separator, value.0, value.1])
}

/// Hash three [`Fq`] elements with the provided `domain_separator`.
pub fn hash_3(domain_separator: &Fq, value: (Fq, Fq, Fq)) -> Fq {
    let params = &crate::RATE_3_PARAMS;
    let mut state = Instance::new(params);
    state.n_to_1_fixed_hash(&[*domain_separator, value.0, value.1, value.2])
}

/// Hash four [`Fq`] elements with the provided `domain_separator`.
pub fn hash_4(domain_separator: &Fq, value: (Fq, Fq, Fq, Fq)) -> Fq {
    let params = &crate::RATE_4_PARAMS;
    let mut state = Instance::new(params);
    state.n_to_1_fixed_hash(&[*domain_separator, value.0, value.1, value.2, value.3])
}

/// Hash five [`Fq`] elements with the provided `domain_separator`.
pub fn hash_5(domain_separator: &Fq, value: (Fq, Fq, Fq, Fq, Fq)) -> Fq {
    let params = &crate::RATE_5_PARAMS;
    let mut state = Instance::new(params);
    state.n_to_1_fixed_hash(&[
        *domain_separator,
        value.0,
        value.1,
        value.2,
        value.3,
        value.4,
    ])
}

/// Hash six [`Fq`] elements with the provided `domain_separator`.
pub fn hash_6(domain_separator: &Fq, value: (Fq, Fq, Fq, Fq, Fq, Fq)) -> Fq {
    let params = &crate::RATE_6_PARAMS;
    let mut state = Instance::new(params);
    state.n_to_1_fixed_hash(&[
        *domain_separator,
        value.0,
        value.1,
        value.2,
        value.3,
        value.4,
        value.5,
    ])
}

/// Hash seven [`Fq`] elements with the provided `domain_separator`.
pub fn hash_7(domain_separator: &Fq, value: (Fq, Fq, Fq, Fq, Fq, Fq, Fq)) -> Fq {
    let params = &crate::RATE_7_PARAMS;
    let mut state = Instance::new(params);
    state.n_to_1_fixed_hash(&[
        *domain_separator,
        value.0,
        value.1,
        value.2,
        value.3,
        value.4,
        value.5,
        value.6,
    ])
}

#[cfg(test)]
mod test {
    use core::str::FromStr;

    use super::*;

    #[test]
    fn rate_1() {
        let domain_sep = Fq::from_le_bytes_mod_order(b"Penumbra_TestVec");

        let input = Fq::from_str(
            "7553885614632219548127688026174585776320152166623257619763178041781456016062",
        )
        .unwrap();
        let output = hash_1(&domain_sep, input);

        let expected_output = Fq::from_str(
            "2337838243217876174544784248400816541933405738836087430664765452605435675740",
        )
        .unwrap();

        assert_eq!(output, expected_output);
    }

    #[test]
    fn rate_2() {
        let domain_sep = Fq::from_le_bytes_mod_order(b"Penumbra_TestVec");

        let input = (
            Fq::from_str(
                "7553885614632219548127688026174585776320152166623257619763178041781456016062",
            )
            .unwrap(),
            Fq::from_str(
                "2337838243217876174544784248400816541933405738836087430664765452605435675740",
            )
            .unwrap(),
        );

        let output = hash_2(&domain_sep, input);

        let expected_output = Fq::from_str(
            "4318449279293553393006719276941638490334729643330833590842693275258805886300",
        )
        .unwrap();

        assert_eq!(output, expected_output);
    }

    #[test]
    fn rate_3() {
        let domain_sep = Fq::from_le_bytes_mod_order(b"Penumbra_TestVec");

        let input = (
            Fq::from_str(
                "7553885614632219548127688026174585776320152166623257619763178041781456016062",
            )
            .unwrap(),
            Fq::from_str(
                "2337838243217876174544784248400816541933405738836087430664765452605435675740",
            )
            .unwrap(),
            Fq::from_str(
                "4318449279293553393006719276941638490334729643330833590842693275258805886300",
            )
            .unwrap(),
        );

        let output = hash_3(&domain_sep, input);

        let expected_output = Fq::from_str(
            "2884734248868891876687246055367204388444877057000108043377667455104051576315",
        )
        .unwrap();

        assert_eq!(output, expected_output);
    }

    #[test]
    fn rate_4() {
        let domain_sep = Fq::from_le_bytes_mod_order(b"Penumbra_TestVec");

        let input = (
            Fq::from_str(
                "7553885614632219548127688026174585776320152166623257619763178041781456016062",
            )
            .unwrap(),
            Fq::from_str(
                "2337838243217876174544784248400816541933405738836087430664765452605435675740",
            )
            .unwrap(),
            Fq::from_str(
                "4318449279293553393006719276941638490334729643330833590842693275258805886300",
            )
            .unwrap(),
            Fq::from_str(
                "2884734248868891876687246055367204388444877057000108043377667455104051576315",
            )
            .unwrap(),
        );

        let output = hash_4(&domain_sep, input);

        let expected_output = Fq::from_str(
            "5235431038142849831913898188189800916077016298531443239266169457588889298166",
        )
        .unwrap();

        assert_eq!(output, expected_output);
    }

    #[test]
    fn rate_5() {
        let domain_sep = Fq::from_le_bytes_mod_order(b"Penumbra_TestVec");

        let input = (
            Fq::from_str(
                "7553885614632219548127688026174585776320152166623257619763178041781456016062",
            )
            .unwrap(),
            Fq::from_str(
                "2337838243217876174544784248400816541933405738836087430664765452605435675740",
            )
            .unwrap(),
            Fq::from_str(
                "4318449279293553393006719276941638490334729643330833590842693275258805886300",
            )
            .unwrap(),
            Fq::from_str(
                "2884734248868891876687246055367204388444877057000108043377667455104051576315",
            )
            .unwrap(),
            Fq::from_str(
                "5235431038142849831913898188189800916077016298531443239266169457588889298166",
            )
            .unwrap(),
        );

        let output = hash_5(&domain_sep, input);

        let expected_output = Fq::from_str(
            "66948599770858083122195578203282720327054804952637730715402418442993895152",
        )
        .unwrap();

        assert_eq!(output, expected_output);
    }

    #[test]
    fn rate_6() {
        let domain_sep = Fq::from_le_bytes_mod_order(b"Penumbra_TestVec");

        let input = (
            Fq::from_str(
                "7553885614632219548127688026174585776320152166623257619763178041781456016062",
            )
            .unwrap(),
            Fq::from_str(
                "2337838243217876174544784248400816541933405738836087430664765452605435675740",
            )
            .unwrap(),
            Fq::from_str(
                "4318449279293553393006719276941638490334729643330833590842693275258805886300",
            )
            .unwrap(),
            Fq::from_str(
                "2884734248868891876687246055367204388444877057000108043377667455104051576315",
            )
            .unwrap(),
            Fq::from_str(
                "5235431038142849831913898188189800916077016298531443239266169457588889298166",
            )
            .unwrap(),
            Fq::from_str(
                "66948599770858083122195578203282720327054804952637730715402418442993895152",
            )
            .unwrap(),
        );

        let output = hash_6(&domain_sep, input);

        let expected_output = Fq::from_str(
            "6797655301930638258044003960605211404784492298673033525596396177265014216269",
        )
        .unwrap();

        assert_eq!(output, expected_output);
    }
}