zkhash/gmimc/
gmimc.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
use crate::merkle_tree::merkle_tree_fp::MerkleTreeHash;

use std::sync::Arc;

use ark_ff::PrimeField;

use super::gmimc_params::GmimcParams;

#[derive(Clone, Debug)]
pub struct Gmimc<S: PrimeField> {
    pub(crate) params: Arc<GmimcParams<S>>,
}

impl<S: PrimeField> Gmimc<S> {
    pub fn new(params: &Arc<GmimcParams<S>>) -> Self {
        Gmimc {
            params: Arc::clone(params),
        }
    }

    pub fn get_t(&self) -> usize {
        self.params.t
    }

    fn sbox(&self, state_0: &S, round: usize) -> S {
        let mut input = *state_0;
        input.add_assign(&self.params.round_constants[round]);

        let mut input2 = input.to_owned();
        input2.square_in_place();
        match self.params.d {
            3 => {
                let mut out = input2;
                out.mul_assign(&input);
                out
            }
            5 => {
                let mut out = input2;
                out.square_in_place();
                out.mul_assign(&input);
                out
            }
            7 => {
                let mut out = input2;
                out.square_in_place();
                out.mul_assign(&input2);
                out.mul_assign(&input);
                out
            }
            _ => {
                panic!();
            }
        }
    }

    fn round(&self, state: &mut [S], round: usize) {
        let power = self.sbox(&state[0], round);
        state.iter_mut().skip(1).for_each(|f| f.add_assign(&power));
    }

    pub fn permutation(&self, input: &[S]) -> Vec<S> {
        let t = self.params.t;
        // not opt is faster for small t
        if t < 8 {
            return self.permutation_not_opt(input);
        }

        assert_eq!(t, input.len());
        let mut current_state = input.to_owned();
        let mut acc = S::zero();
        let mut acc_queue = vec![S::zero(); t - 1];
        for r in 0..self.params.rounds - 1 {
            let power = self.sbox(&current_state[0], r);
            acc_queue.rotate_right(1);
            acc.sub_assign(&acc_queue[0]);
            acc_queue[0] = power;
            acc.add_assign(&power);

            current_state.rotate_right(1);
            current_state[0].add_assign(&acc);
        }

        // finally without rotation
        let power = self.sbox(&current_state[0], self.params.rounds - 1);
        acc_queue.rotate_right(1);
        acc.sub_assign(&acc_queue[0]);
        acc_queue[0] = power;
        acc.add_assign(&power);
        current_state[t - 1].add_assign(&acc);

        // final adds
        for el in current_state.iter_mut().skip(1).take(t - 2).rev() {
            acc_queue.rotate_right(1);
            acc.sub_assign(&acc_queue[0]);
            el.add_assign(&acc);
        }

        current_state
    }

    pub fn permutation_not_opt(&self, input: &[S]) -> Vec<S> {
        assert_eq!(self.params.t, input.len());
        let mut current_state = input.to_owned();
        for r in 0..self.params.rounds - 1 {
            self.round(&mut current_state, r);
            current_state.rotate_right(1);
        }

        // finally without rotation
        self.round(&mut current_state, self.params.rounds - 1);

        current_state
    }
}

impl<F: PrimeField> MerkleTreeHash<F> for Gmimc<F> {
    fn compress(&self, input: &[&F]) -> F {
        self.permutation(&[input[0].to_owned(), input[1].to_owned(), F::zero()])[0]
    }
}

#[cfg(test)]
mod gmimc_tests_bls12 {
    use super::*;
    use crate::gmimc::gmimc_instance_bls12::GMIMC_BLS_3_PARAMS;
    use crate::fields::{bls12::FpBLS12, utils::random_scalar};

    type Scalar = FpBLS12;

    static TESTRUNS: usize = 5;

    #[test]
    fn consistent_perm() {
        let gmimc = Gmimc::new(&GMIMC_BLS_3_PARAMS);
        let t = gmimc.params.t;
        for _ in 0..TESTRUNS {
            let input1: Vec<Scalar> = (0..t).map(|_| random_scalar()).collect();

            let mut input2: Vec<Scalar>;
            loop {
                input2 = (0..t).map(|_| random_scalar()).collect();
                if input1 != input2 {
                    break;
                }
            }

            let perm1 = gmimc.permutation(&input1);
            let perm2 = gmimc.permutation(&input1);
            let perm3 = gmimc.permutation(&input2);
            assert_eq!(perm1, perm2);
            assert_ne!(perm1, perm3);
        }
    }

    #[test]
    fn opt_equals_not_opt() {
        let gmimc = Gmimc::new(&GMIMC_BLS_3_PARAMS);
        let t = gmimc.params.t;
        for _ in 0..TESTRUNS {
            let input: Vec<Scalar> = (0..t).map(|_| random_scalar()).collect();

            let perm1 = gmimc.permutation(&input);
            let perm2 = gmimc.permutation_not_opt(&input);
            assert_eq!(perm1, perm2);
        }
    }
}

#[cfg(test)]
mod gmimc_tests_bn256 {
    use super::*;
    use crate::gmimc::gmimc_instance_bn256::GMIMC_BN_3_PARAMS;
    use crate::fields::{bn256::FpBN256, utils::random_scalar};

    type Scalar = FpBN256;

    static TESTRUNS: usize = 5;

    #[test]
    fn consistent_perm() {
        let gmimc = Gmimc::new(&GMIMC_BN_3_PARAMS);
        let t = gmimc.params.t;
        for _ in 0..TESTRUNS {
            let input1: Vec<Scalar> = (0..t).map(|_| random_scalar()).collect();

            let mut input2: Vec<Scalar>;
            loop {
                input2 = (0..t).map(|_| random_scalar()).collect();
                if input1 != input2 {
                    break;
                }
            }

            let perm1 = gmimc.permutation(&input1);
            let perm2 = gmimc.permutation(&input1);
            let perm3 = gmimc.permutation(&input2);
            assert_eq!(perm1, perm2);
            assert_ne!(perm1, perm3);
        }
    }

    #[test]
    fn opt_equals_not_opt() {
        let gmimc = Gmimc::new(&GMIMC_BN_3_PARAMS);
        let t = gmimc.params.t;
        for _ in 0..TESTRUNS {
            let input: Vec<Scalar> = (0..t).map(|_| random_scalar()).collect();

            let perm1 = gmimc.permutation(&input);
            let perm2 = gmimc.permutation_not_opt(&input);
            assert_eq!(perm1, perm2);
        }
    }
}

#[cfg(test)]
mod gmimc_tests_goldilocks {
    use super::*;
    use crate::fields::{goldilocks::FpGoldiLocks, utils::random_scalar};
    use crate::gmimc::gmimc_instance_goldilocks::{
        GMIMC_GOLDILOCKS_8_PARAMS,
        GMIMC_GOLDILOCKS_12_PARAMS,
        GMIMC_GOLDILOCKS_16_PARAMS,
        GMIMC_GOLDILOCKS_20_PARAMS,
    };

    type Scalar = FpGoldiLocks;

    static TESTRUNS: usize = 5;

    #[test]
    fn consistent_perm() {
        let instances = vec![
            Gmimc::new(&GMIMC_GOLDILOCKS_8_PARAMS),
            Gmimc::new(&GMIMC_GOLDILOCKS_12_PARAMS),
            Gmimc::new(&GMIMC_GOLDILOCKS_16_PARAMS),
            Gmimc::new(&GMIMC_GOLDILOCKS_20_PARAMS),
        ];
        for instance in instances {
            let t = instance.params.t;
            for _ in 0..TESTRUNS {
                let input1: Vec<Scalar> = (0..t).map(|_| random_scalar()).collect();

                let mut input2: Vec<Scalar>;
                loop {
                    input2 = (0..t).map(|_| random_scalar()).collect();
                    if input1 != input2 {
                        break;
                    }
                }

                let perm1 = instance.permutation(&input1);
                let perm2 = instance.permutation(&input1);
                let perm3 = instance.permutation(&input2);
                assert_eq!(perm1, perm2);
                assert_ne!(perm1, perm3);
            }
        }
    }

    #[test]
    fn opt_equals_not_opt() {
        let instances = vec![
            Gmimc::new(&GMIMC_GOLDILOCKS_8_PARAMS),
            Gmimc::new(&GMIMC_GOLDILOCKS_12_PARAMS),
            Gmimc::new(&GMIMC_GOLDILOCKS_16_PARAMS),
            Gmimc::new(&GMIMC_GOLDILOCKS_20_PARAMS),
        ];
        for instance in instances {
            let t = instance.params.t;
            for _ in 0..TESTRUNS {
                let input: Vec<Scalar> = (0..t).map(|_| random_scalar()).collect();

                let perm1 = instance.permutation(&input);
                let perm2 = instance.permutation_not_opt(&input);
                assert_eq!(perm1, perm2);
            }
        }
    }
}

#[cfg(test)]
mod gmimc_tests_babybear {
    use super::*;
    use crate::gmimc::gmimc_instance_babybear::GMIMC_BABYBEAR_16_PARAMS;
    use crate::gmimc::gmimc_instance_babybear::GMIMC_BABYBEAR_24_PARAMS;
    use crate::fields::{babybear::FpBabyBear, utils::random_scalar};

    type Scalar = FpBabyBear;

    static TESTRUNS: usize = 5;

    #[test]
    fn consistent_perm() {
        let instances = vec![
            Gmimc::new(&GMIMC_BABYBEAR_16_PARAMS),
            Gmimc::new(&GMIMC_BABYBEAR_24_PARAMS),
        ];
        for instance in instances {
            let t = instance.params.t;
            for _ in 0..TESTRUNS {
                let input1: Vec<Scalar> = (0..t).map(|_| random_scalar()).collect();

                let mut input2: Vec<Scalar>;
                loop {
                    input2 = (0..t).map(|_| random_scalar()).collect();
                    if input1 != input2 {
                        break;
                    }
                }

                let perm1 = instance.permutation(&input1);
                let perm2 = instance.permutation(&input1);
                let perm3 = instance.permutation(&input2);
                assert_eq!(perm1, perm2);
                assert_ne!(perm1, perm3);
            }
        }
    }

    #[test]
    fn opt_equals_not_opt() {
        let instances = vec![
            Gmimc::new(&GMIMC_BABYBEAR_16_PARAMS),
            Gmimc::new(&GMIMC_BABYBEAR_24_PARAMS),
        ];
        for instance in instances {
            let t = instance.params.t;
            for _ in 0..TESTRUNS {
                let input: Vec<Scalar> = (0..t).map(|_| random_scalar()).collect();

                let perm1 = instance.permutation(&input);
                let perm2 = instance.permutation_not_opt(&input);
                assert_eq!(perm1, perm2);
            }
        }
    }
}