franklin_crypto/rescue/bn256/
mod.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
use super::{generate_mds_matrix, PowerSBox, QuinticSBox, RescueEngine, RescueHashParams, RescueParamsInternal};
use bellman::pairing::bn256;
use bellman::pairing::ff::{Field, PrimeField, PrimeFieldRepr};
use group_hash::{BlakeHasher, GroupHasher};

extern crate num_bigint;
extern crate num_integer;
extern crate num_traits;
use self::num_bigint::{BigInt, BigUint};
use self::num_integer::{ExtendedGcd, Integer};
use self::num_traits::{One, ToPrimitive, Zero};

impl RescueEngine for bn256::Bn256 {
    type Params = Bn256RescueParams;
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Bn256RescueParams {
    c: u32,
    r: u32,
    rounds: u32,
    round_constants: Vec<bn256::Fr>,
    mds_matrix: Vec<bn256::Fr>,
    security_level: u32,
    sbox_0: PowerSBox<bn256::Bn256>,
    sbox_1: QuinticSBox<bn256::Bn256>,
    custom_gates_allowed: bool,
}

impl Bn256RescueParams {
    pub fn new_checked_2_into_1() -> Self {
        let c = 1u32;
        let r = 2u32;
        let rounds = 22u32;
        let security_level = 126u32;

        Self::new_for_params::<BlakeHasher>(c, r, rounds, security_level)
    }

    pub fn new_2_into_1<H: GroupHasher>() -> Self {
        let c = 1u32;
        let r = 2u32;
        let rounds = 22u32;
        let security_level = 126u32;

        Self::new_for_params::<H>(c, r, rounds, security_level)
    }

    pub fn new_3_into_1<H: GroupHasher>() -> Self {
        let c = 1u32;
        let r = 3u32;
        let rounds = 22u32;
        let security_level = 126u32;

        Self::new_for_params::<H>(c, r, rounds, security_level)
    }

    pub fn new_for_params<H: GroupHasher>(c: u32, r: u32, rounds: u32, _security_level: u32) -> Self {
        use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
        use constants;

        let state_width = c + r;
        let num_round_constants = (1 + rounds * 2) * state_width;
        let num_round_constants = num_round_constants as usize;

        // generate round constants based on some seed and hashing
        let round_constants = {
            let tag = b"Rescue_f";
            let mut round_constants = Vec::with_capacity(num_round_constants);
            let mut nonce = 0u32;
            let mut nonce_bytes = [0u8; 4];

            loop {
                (&mut nonce_bytes[0..4]).write_u32::<BigEndian>(nonce).unwrap();
                let mut h = H::new(&tag[..]);
                h.update(constants::GH_FIRST_BLOCK);
                h.update(&nonce_bytes[..]);
                let h = h.finalize();
                assert!(h.len() == 32);

                let mut constant_repr = <bn256::Fr as PrimeField>::Repr::default();
                constant_repr.read_le(&h[..]).unwrap();

                if let Ok(constant) = bn256::Fr::from_repr(constant_repr) {
                    if !constant.is_zero() {
                        round_constants.push(constant);
                    }
                }

                if round_constants.len() == num_round_constants {
                    break;
                }

                nonce += 1;
            }

            round_constants
        };

        let mds_matrix = {
            use rand::chacha::ChaChaRng;
            use rand::SeedableRng;
            // Create an RNG based on the outcome of the random beacon
            let mut rng = {
                // This tag is a first one in a sequence of b"ResMxxxx"
                // that produces MDS matrix without eigenvalues for rate = 2,
                // capacity = 1 variant over Bn254 curve
                let tag = b"ResM0003";
                let mut h = H::new(&tag[..]);
                h.update(constants::GH_FIRST_BLOCK);
                let h = h.finalize();
                assert!(h.len() == 32);
                let mut seed = [0u32; 8];
                for i in 0..8 {
                    seed[i] = (&h[..]).read_u32::<BigEndian>().expect("digest is large enough for this to work");
                }

                ChaChaRng::from_seed(&seed)
            };

            generate_mds_matrix::<bn256::Bn256, _>(state_width, &mut rng)
        };

        let alpha = BigUint::from(5u64);

        let mut p_minus_one_biguint = BigUint::from(0u64);
        for limb in bn256::Fr::char().as_ref().iter().rev() {
            p_minus_one_biguint <<= 64;
            p_minus_one_biguint += BigUint::from(*limb);
        }

        p_minus_one_biguint -= BigUint::one();

        fn biguint_to_u64_array(mut v: BigUint) -> [u64; 4] {
            let m: BigUint = BigUint::from(1u64) << 64;
            let mut ret = [0; 4];

            for idx in 0..4 {
                ret[idx] = (&v % &m).to_u64().expect("is guaranteed to fit");
                v >>= 64;
            }
            assert!(v.is_zero());
            ret
        }

        let alpha_signed = BigInt::from(alpha);
        let p_minus_one_signed = BigInt::from(p_minus_one_biguint);

        let ExtendedGcd { gcd, x: _, y, .. } = p_minus_one_signed.extended_gcd(&alpha_signed);
        assert!(gcd.is_one());
        let y = if y < BigInt::zero() {
            let mut y = y;
            y += p_minus_one_signed;

            y.to_biguint().expect("must be > 0")
        } else {
            y.to_biguint().expect("must be > 0")
        };

        let inv_alpha = biguint_to_u64_array(y);

        let mut alpha_inv_repr = <bn256::Fr as PrimeField>::Repr::default();
        for (r, limb) in alpha_inv_repr.as_mut().iter_mut().zip(inv_alpha.iter()) {
            *r = *limb;
        }

        Self {
            c: c,
            r: r,
            rounds: rounds,
            round_constants: round_constants,
            mds_matrix: mds_matrix,
            security_level: 126,
            sbox_0: PowerSBox { power: alpha_inv_repr, inv: 5u64 },
            sbox_1: QuinticSBox { _marker: std::marker::PhantomData },
            custom_gates_allowed: false,
        }
    }

    pub fn set_allow_custom_gate(&mut self, allowed: bool) {
        self.custom_gates_allowed = allowed;
    }
}

impl RescueParamsInternal<bn256::Bn256> for Bn256RescueParams {
    fn set_round_constants(&mut self, to: Vec<bn256::Fr>) {
        assert_eq!(self.round_constants.len(), to.len());
        self.round_constants = to;
    }
}

impl RescueHashParams<bn256::Bn256> for Bn256RescueParams {
    type SBox0 = PowerSBox<bn256::Bn256>;
    type SBox1 = QuinticSBox<bn256::Bn256>;

    fn capacity(&self) -> u32 {
        self.c
    }
    fn rate(&self) -> u32 {
        self.r
    }
    fn num_rounds(&self) -> u32 {
        self.rounds
    }
    fn round_constants(&self, round: u32) -> &[bn256::Fr] {
        let t = self.c + self.r;
        let start = (t * round) as usize;
        let end = (t * (round + 1)) as usize;

        &self.round_constants[start..end]
    }
    fn mds_matrix_row(&self, row: u32) -> &[bn256::Fr] {
        let t = self.c + self.r;
        let start = (t * row) as usize;
        let end = (t * (row + 1)) as usize;

        &self.mds_matrix[start..end]
    }
    fn security_level(&self) -> u32 {
        self.security_level
    }
    fn output_len(&self) -> u32 {
        self.capacity()
    }
    fn absorbtion_cycle_len(&self) -> u32 {
        self.rate()
    }
    fn compression_rate(&self) -> u32 {
        self.absorbtion_cycle_len() / self.output_len()
    }

    fn sbox_0(&self) -> &Self::SBox0 {
        &self.sbox_0
    }
    fn sbox_1(&self) -> &Self::SBox1 {
        &self.sbox_1
    }
    fn can_use_custom_gates(&self) -> bool {
        self.custom_gates_allowed
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::group_hash::BlakeHasher;
    use crate::rescue::*;
    use bellman::pairing::bn256::{Bn256, Fr};
    use bellman::pairing::ff::Field;
    use bellman::pairing::ff::PrimeField;
    use rand::{thread_rng, Rand, Rng, SeedableRng, XorShiftRng};

    #[test]
    fn test_generate_bn256_rescue_params() {
        let _params = Bn256RescueParams::new_2_into_1::<BlakeHasher>();
    }

    #[test]
    fn test_bn256_rescue_params_permutation() {
        let rng = &mut thread_rng();
        let params = Bn256RescueParams::new_2_into_1::<BlakeHasher>();

        for _ in 0..1000 {
            let input: Fr = rng.gen();
            let mut input_arr: [Fr; 1] = [input];
            params.sbox_1().apply(&mut input_arr);
            params.sbox_0().apply(&mut input_arr);
            assert_eq!(input_arr[0], input);
        }

        for _ in 0..1000 {
            let input: Fr = rng.gen();
            let mut input_arr: [Fr; 1] = [input];
            params.sbox_0().apply(&mut input_arr);
            params.sbox_1().apply(&mut input_arr);
            assert_eq!(input_arr[0], input);
        }
    }

    #[test]
    fn test_bn256_rescue_hash() {
        let rng = &mut thread_rng();
        let params = Bn256RescueParams::new_2_into_1::<BlakeHasher>();
        let input: Vec<Fr> = (0..params.rate()).map(|_| rng.gen()).collect();
        let output = rescue_hash::<Bn256>(&params, &input[..]);
        assert!(output.len() == 1);
    }

    #[test]
    fn output_bn256_rescue_hash() {
        let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
        let params = Bn256RescueParams::new_checked_2_into_1();
        for len in 1..=3 {
            let input: Vec<Fr> = (0..len).map(|_| rng.gen()).collect();
            println!("Input = {:?}", input);
            let output = rescue_hash::<Bn256>(&params, &input[..]);
            println!("Output = {:?}", output);
        }
    }

    #[test]
    fn test_bn256_stateful_rescue_hash() {
        let rng = &mut thread_rng();
        let params = Bn256RescueParams::new_2_into_1::<BlakeHasher>();
        let input: Vec<Fr> = (0..params.rate()).map(|_| rng.gen()).collect();
        let output = rescue_hash::<Bn256>(&params, &input[..]);
        assert!(output.len() == 1);

        let mut stateful_rescue = super::super::StatefulRescue::<Bn256>::new(&params);
        stateful_rescue.specialize(input.len() as u8);
        stateful_rescue.absorb(&input);

        let first_output = stateful_rescue.squeeze_out_single();
        assert_eq!(first_output, output[0]);

        let _ = stateful_rescue.squeeze_out_single();
    }

    #[test]
    fn test_bn256_long_input_rescue_hash() {
        let rng = &mut thread_rng();
        let params = Bn256RescueParams::new_2_into_1::<BlakeHasher>();
        let input: Vec<Fr> = (0..((params.rate() * 10) + 1)).map(|_| rng.gen()).collect();
        let output = rescue_hash::<Bn256>(&params, &input[..]);
        assert!(output.len() == 1);

        let mut stateful_rescue = super::super::StatefulRescue::<Bn256>::new(&params);
        stateful_rescue.specialize(input.len() as u8);
        stateful_rescue.absorb(&input);

        let first_output = stateful_rescue.squeeze_out_single();
        assert_eq!(first_output, output[0]);

        let _ = stateful_rescue.squeeze_out_single();
    }

    #[test]
    fn test_bn256_different_specializations() {
        let rng = &mut thread_rng();
        let params = Bn256RescueParams::new_2_into_1::<BlakeHasher>();
        let input: Vec<Fr> = (0..((params.rate() * 10) + 1)).map(|_| rng.gen()).collect();
        let output = rescue_hash::<Bn256>(&params, &input[..]);
        assert!(output.len() == 1);

        let mut stateful_rescue = super::super::StatefulRescue::<Bn256>::new(&params);
        stateful_rescue.specialize(input.len() as u8);
        stateful_rescue.absorb(&input);

        let first_output = stateful_rescue.squeeze_out_single();
        assert_eq!(first_output, output[0]);

        let mut stateful_rescue_other = super::super::StatefulRescue::<Bn256>::new(&params);
        stateful_rescue_other.specialize((input.len() + 1) as u8);
        stateful_rescue_other.absorb(&input);

        let first_output_other = stateful_rescue_other.squeeze_out_single();
        assert!(first_output != first_output_other);
    }

    #[test]
    #[should_panic]
    fn test_bn256_stateful_rescue_depleted_sponge() {
        let rng = &mut thread_rng();
        let params = Bn256RescueParams::new_2_into_1::<BlakeHasher>();
        let input: Vec<Fr> = (0..params.rate()).map(|_| rng.gen()).collect();

        let mut stateful_rescue = super::super::StatefulRescue::<Bn256>::new(&params);
        stateful_rescue.absorb(&input);

        let _ = stateful_rescue.squeeze_out_single();
        let _ = stateful_rescue.squeeze_out_single();
        let _ = stateful_rescue.squeeze_out_single();
    }

    #[test]
    fn print_mds() {
        let params = Bn256RescueParams::new_2_into_1::<BlakeHasher>();
        println!("MDS_MATRIX");
        let mut vec = vec![];
        for i in 0..params.state_width() {
            vec.push(format!("{:?}", params.mds_matrix_row(i)));
        }

        println!("[ {} ]", vec.join(","));
    }
}