scrypt-opt 0.3.2

A pure-rust optimized scrypt implementation for moderate to high difficulty cases, with AVX2 and AVX512 intrinsics cores and a portable-simd core.
Documentation
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
use core::marker::PhantomData;

#[cfg(feature = "alloc")]
use generic_array::typenum::{B1, IsGreaterOrEqual};
use generic_array::{
    ArrayLength, GenericArray,
    typenum::{NonZero, U1, U2, U3, U4, U8, U10, U14, U16, U20, U53, U64, Unsigned},
};

use crate::{
    Align64, ValidCostFactor,
    fixed_r::{Block, BufferSet},
    pbkdf2_1::Pbkdf2HmacSha256State,
    pipeline::PipelineContext,
};

/// Test case for P = 1, N = 16, R = 1 in the scrypt specification
pub struct CaseN16R1P1;

impl CaseP1 for CaseN16R1P1 {
    type OutputLen = U64;
    type CF = U4;
    type R = U1;
    const PASSWORD: &'static [u8] = b"";
    const SALT: &'static [u8] = b"";
    const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
        0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20, 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04,
        0x97, 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8, 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2,
        0x14, 0x42, 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8, 0x32, 0x6a, 0x75, 0x3a, 0x0f,
        0xc8, 0x1f, 0x17, 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28, 0xcf, 0x35, 0xe2, 0x0c,
        0x38, 0xd1, 0x89, 0x06,
    ]);
}

/// Test case for P = 1, N = 16384, R = 8 in the scrypt specification
pub struct CaseN16384R8P1;

impl CaseP1 for CaseN16384R8P1 {
    type OutputLen = U64;
    type CF = U14;
    type R = U8;
    const PASSWORD: &'static [u8] = b"pleaseletmein";
    const SALT: &'static [u8] = b"SodiumChloride";
    const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
        0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48, 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38,
        0xeb, 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e, 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d,
        0xa1, 0xf2, 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf, 0x62, 0xd4, 0x97, 0x05, 0x24,
        0x2a, 0x9a, 0xf9, 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40, 0xdf, 0xcf, 0x01, 0x7b,
        0x45, 0x57, 0x58, 0x87,
    ]);
}

/// Test case for P = 1, N = 1048576, R = 8 in the scrypt specification
pub struct CaseN1048576R8P1;

impl CaseP1 for CaseN1048576R8P1 {
    type OutputLen = U64;
    type CF = U20;
    type R = U8;
    const PASSWORD: &'static [u8] = b"pleaseletmein";
    const SALT: &'static [u8] = b"SodiumChloride";
    const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
        0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae, 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8,
        0x81, 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d, 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad,
        0xaa, 0x47, 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f, 0xfa, 0x1c, 0x6d, 0x92, 0x7c,
        0x40, 0xf4, 0xc3, 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb, 0xcb, 0xf4, 0x5c, 0x6f,
        0xa7, 0x7a, 0x41, 0xa4,
    ]);
}

/// Supplementary case for P = 2, N = 1024, R = 1
pub struct CaseN1024R1P2;

impl Case for CaseN1024R1P2 {
    type OutputLen = U64;
    type CF = U10;
    type R = U1;
    type P = U2;
    const PASSWORD: &'static [u8] = b"password";
    const SALT: &'static [u8] = b"NaCl";
    const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
        0x09, 0xc4, 0x23, 0x86, 0xb2, 0x46, 0x97, 0x53, 0xeb, 0x76, 0x27, 0x75, 0x15, 0xbe, 0xff,
        0x09, 0x80, 0x9d, 0x18, 0xd9, 0x3f, 0xb4, 0xd3, 0x16, 0xea, 0xe1, 0xa8, 0x63, 0x43, 0x9a,
        0x48, 0x98, 0x17, 0xcf, 0x56, 0xa5, 0x87, 0x69, 0xcc, 0x13, 0xbd, 0xb3, 0x33, 0x14, 0x11,
        0xcc, 0xd7, 0xd5, 0x7f, 0x8e, 0x43, 0x9b, 0xa1, 0xa4, 0x84, 0x58, 0x0f, 0x41, 0x9f, 0x7c,
        0x8e, 0x34, 0x99, 0x41,
    ]);
}

#[cfg(feature = "alloc")]
/// Test case for P = 16, N = 1024, R = 8 in the scrypt specification
pub struct CaseN1024R8P16;

#[cfg(feature = "alloc")]
impl Case for CaseN1024R8P16 {
    type P = U16;
    type OutputLen = U64;
    type CF = U10;
    type R = U8;
    const PASSWORD: &'static [u8] = b"password";
    const SALT: &'static [u8] = b"NaCl";
    const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
        0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00, 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9,
        0xfe, 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30, 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37,
        0x31, 0x62, 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88, 0x6f, 0xf1, 0x09, 0x27, 0x9d,
        0x98, 0x30, 0xda, 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d, 0x83, 0x60, 0xcb, 0xdf,
        0xa2, 0xcc, 0x06, 0x40,
    ]);
}

/// Supplementary case for exotic parameters
pub struct CaseN1024R53P1;

impl CaseP1 for CaseN1024R53P1 {
    type OutputLen = U64;
    type CF = U10;
    type R = U53;
    const PASSWORD: &'static [u8] = b"password";
    const SALT: &'static [u8] = b"NaCl";
    const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
        0x7c, 0x34, 0x45, 0xf8, 0x73, 0xde, 0x41, 0x9a, 0x96, 0xd7, 0xa3, 0x20, 0x51, 0xd1, 0xb4,
        0x8f, 0xb3, 0xde, 0x94, 0x8d, 0xac, 0x06, 0x78, 0x57, 0xf3, 0x7a, 0x58, 0xdf, 0x2d, 0x71,
        0xeb, 0x4d, 0x4b, 0xeb, 0x87, 0xd6, 0xe8, 0x7d, 0x70, 0xd4, 0xb7, 0xa5, 0x86, 0x66, 0x99,
        0xe4, 0x7a, 0xcd, 0x09, 0x4e, 0x93, 0x4e, 0xc4, 0xa1, 0xd7, 0xb8, 0x7d, 0x53, 0x93, 0x10,
        0x7e, 0x75, 0xdc, 0x70,
    ]);
}

/// Supplementary case for exotic parameters
pub struct CaseN1024R53P3;

impl Case for CaseN1024R53P3 {
    type P = U3;
    type OutputLen = U64;
    type CF = U10;
    type R = U53;
    const PASSWORD: &'static [u8] = b"password";
    const SALT: &'static [u8] = b"NaCl";
    const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
        0x98, 0x13, 0x74, 0xb7, 0x0a, 0x02, 0x9a, 0x5b, 0x8a, 0xe0, 0x36, 0x28, 0x62, 0x84, 0x9f,
        0xed, 0x87, 0xd7, 0x3e, 0x29, 0xf6, 0x08, 0x99, 0xda, 0x4f, 0xc5, 0x37, 0xa3, 0xc0, 0x6b,
        0x36, 0x81, 0x04, 0x4a, 0xbf, 0x68, 0x0c, 0x6f, 0x40, 0x68, 0xf0, 0x4f, 0x5f, 0xa2, 0x8c,
        0x8a, 0x16, 0x32, 0x26, 0x5b, 0xdc, 0x82, 0xa6, 0xa8, 0x06, 0xce, 0x08, 0x9a, 0x62, 0x18,
        0xca, 0x02, 0x93, 0xb2,
    ]);
}

/// Test case for P = 1
pub trait CaseP1 {
    /// The length of the output
    type OutputLen: ArrayLength + NonZero;
    /// The cost factor
    type CF: ValidCostFactor;
    /// The number of rounds
    type R: ArrayLength + NonZero;
    /// The password
    const PASSWORD: &'static [u8];
    /// The salt
    const SALT: &'static [u8];
    /// The known answer
    const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen>;

    /// Test the algorithm implementation
    fn algorithm_self_test() {
        #[cfg(not(feature = "alloc"))]
        let mut buffer0: GenericArray<
            Align64<Block<Self::R>>,
            <Self::CF as ValidCostFactor>::MinimumBlocks,
        > = GenericArray::default();

        #[cfg(feature = "alloc")]
        let mut buffer0: alloc::boxed::Box<
            GenericArray<Align64<Block<Self::R>>, <Self::CF as ValidCostFactor>::MinimumBlocks>,
        > = unsafe { alloc::boxed::Box::new_uninit().assume_init() };

        #[cfg(not(feature = "alloc"))]
        let mut buffer1: GenericArray<
            Align64<Block<Self::R>>,
            <Self::CF as ValidCostFactor>::MinimumBlocks,
        > = GenericArray::default();

        #[cfg(feature = "alloc")]
        let mut buffer1: alloc::boxed::Box<
            GenericArray<Align64<Block<Self::R>>, <Self::CF as ValidCostFactor>::MinimumBlocks>,
        > = unsafe { alloc::boxed::Box::new_uninit().assume_init() };

        let mut buffer_set0 = BufferSet::<_, Self::R>::new(buffer0.as_mut_slice());
        let mut buffer_set1 = BufferSet::<_, Self::R>::new(buffer1.as_mut_slice());

        assert_eq!(buffer_set0.n(), 1 << Self::CF::U8);
        assert_eq!(buffer_set1.n(), 1 << Self::CF::U8);

        let mut output0 = GenericArray::default();
        let mut output1 = GenericArray::default();
        let mut output_dummy = GenericArray::default();

        // compat API test
        #[cfg(feature = "std")]
        {
            crate::compat::scrypt(
                Self::PASSWORD,
                Self::SALT,
                Self::CF::U8.try_into().unwrap(),
                Self::R::U32.try_into().unwrap(),
                1.try_into().unwrap(),
                output0.as_mut_slice(),
            );
            assert_eq!(output0, Self::KNOWN_ANSWER);
            output0.fill(0);
        }

        let hmac_state = Pbkdf2HmacSha256State::new(Self::PASSWORD);
        let hmac_state_dummy = Pbkdf2HmacSha256State::new(b"not a password");

        // exercise basic functionality
        buffer_set0.set_input(&hmac_state, Self::SALT);

        buffer_set0.scrypt_ro_mix();

        buffer_set0.extract_output(&hmac_state, &mut output0);

        assert_eq!(output0, Self::KNOWN_ANSWER);

        // check output is not stuck
        buffer_set0.set_input(&hmac_state_dummy, Self::SALT);
        buffer_set0.scrypt_ro_mix();
        buffer_set0.extract_output(&hmac_state_dummy, &mut output_dummy);
        assert_ne!(output_dummy, Self::KNOWN_ANSWER, "stuck output");

        // basic interleaved functionality
        buffer_set0.set_input(&hmac_state, Self::SALT);
        buffer_set1.set_input(&hmac_state_dummy, Self::SALT);

        buffer_set0.ro_mix_front();
        buffer_set0.ro_mix_interleaved(&mut buffer_set1);
        buffer_set1.ro_mix_back();

        buffer_set0.extract_output(&hmac_state, &mut output0);
        buffer_set1.extract_output(&hmac_state_dummy, &mut output1);

        assert_eq!(output0, Self::KNOWN_ANSWER);
        assert_eq!(output1, output_dummy);

        buffer_set0.as_mut().iter_mut().for_each(|b| {
            b.fill(0);
        });

        buffer_set1.as_mut().iter_mut().for_each(|b| {
            b.fill(0);
        });
    }

    /// Test the pipeline API
    fn pipeline_api_test() {
        #[cfg(not(feature = "alloc"))]
        let mut buffer0: GenericArray<
            Align64<Block<Self::R>>,
            <Self::CF as ValidCostFactor>::MinimumBlocks,
        > = GenericArray::default();

        #[cfg(feature = "alloc")]
        let mut buffer0: alloc::boxed::Box<
            GenericArray<Align64<Block<Self::R>>, <Self::CF as ValidCostFactor>::MinimumBlocks>,
        > = unsafe { alloc::boxed::Box::new_uninit().assume_init() };

        #[cfg(not(feature = "alloc"))]
        let mut buffer1: GenericArray<
            Align64<Block<Self::R>>,
            <Self::CF as ValidCostFactor>::MinimumBlocks,
        > = GenericArray::default();

        #[cfg(feature = "alloc")]
        let mut buffer1: alloc::boxed::Box<
            GenericArray<Align64<Block<Self::R>>, <Self::CF as ValidCostFactor>::MinimumBlocks>,
        > = unsafe { alloc::boxed::Box::new_uninit().assume_init() };

        let hmac_state = Pbkdf2HmacSha256State::new(Self::PASSWORD);
        let hmac_state_dummy = Pbkdf2HmacSha256State::new(b"not a password");

        let mut buffer_set0 = BufferSet::<_, Self::R>::new(buffer0.as_mut_slice());
        let mut buffer_set1 = BufferSet::<_, Self::R>::new(buffer1.as_mut_slice());

        // high level pipeline API
        struct Context<R: ArrayLength + NonZero, OutputLen: ArrayLength + NonZero> {
            i: usize,
            salt: &'static [u8],
            known_answer: GenericArray<u8, OutputLen>,

            hmac_state: Pbkdf2HmacSha256State,
            hmac_state_dummy: Pbkdf2HmacSha256State,
            _marker: PhantomData<R>,
        }

        impl<R: ArrayLength + NonZero, OutputLen: ArrayLength + NonZero>
            PipelineContext<usize, &mut [Align64<Block<R>>], R, ()> for Context<R, OutputLen>
        {
            fn begin(
                &mut self,
                _state: &mut usize,
                buffer_set: &mut BufferSet<&mut [Align64<Block<R>>], R>,
            ) {
                match self.i % 3 {
                    0 | 1 => {
                        buffer_set.set_input(&self.hmac_state, self.salt);
                    }
                    2 => {
                        buffer_set.set_input(&self.hmac_state_dummy, self.salt);
                    }
                    _ => unreachable!(),
                }
            }
            fn drain(
                self,
                state: &mut usize,
                buffer_set: &mut BufferSet<&mut [Align64<Block<R>>], R>,
            ) -> Option<()> {
                match self.i % 3 {
                    0 | 1 => {
                        let mut output = GenericArray::<_, OutputLen>::default();
                        buffer_set.extract_output(&self.hmac_state, &mut output);
                        assert_eq!(output, self.known_answer);
                    }
                    2 => {
                        let mut output_dummy = GenericArray::<_, OutputLen>::default();
                        buffer_set.extract_output(&self.hmac_state_dummy, &mut output_dummy);
                        assert_ne!(output_dummy, self.known_answer, "stuck output");
                    }
                    _ => unreachable!(),
                }
                *state += self.i;

                None
            }
        }

        for pipeline_length in 0..11 {
            let mut counter = 0;
            buffer_set0.pipeline(
                &mut buffer_set1,
                (0..pipeline_length).map(|i| Context {
                    i,
                    hmac_state,
                    hmac_state_dummy,
                    salt: Self::SALT,
                    known_answer: Self::KNOWN_ANSWER,
                    _marker: PhantomData,
                }),
                &mut counter,
            );

            let expected_sum = pipeline_length * (pipeline_length.saturating_sub(1)) / 2;
            assert_eq!(counter, expected_sum);
        }
    }
}

/// Test case for P > 1
#[cfg(feature = "alloc")]
pub trait Case {
    /// The parallel width
    type P: ArrayLength + NonZero + IsGreaterOrEqual<U2, Output = B1>;
    /// The length of the output
    type OutputLen: ArrayLength + NonZero;
    /// The number of blocks
    type CF: ValidCostFactor;
    /// The number of rounds
    type R: ArrayLength + NonZero;
    /// The password
    const PASSWORD: &'static [u8];
    /// The salt
    const SALT: &'static [u8];
    /// The known answer
    const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen>;

    /// Test the algorithm implementation
    fn algorithm_self_test() {
        let hmac_state = Pbkdf2HmacSha256State::new(Self::PASSWORD);
        let mut input_buffers: GenericArray<Align64<Block<Self::R>>, Self::P> =
            GenericArray::default();
        let mut output_buffers: GenericArray<Align64<Block<Self::R>>, Self::P> =
            GenericArray::default();

        let mut buffer0 = BufferSet::<_, Self::R>::new_boxed(Self::CF::U8.try_into().unwrap());
        let mut buffer1 = BufferSet::<_, Self::R>::new_boxed(Self::CF::U8.try_into().unwrap());

        hmac_state.emit_scatter(Self::SALT, input_buffers.iter_mut());

        buffer0.pipeline(
            &mut buffer1,
            input_buffers.iter().zip(output_buffers.iter_mut()),
            &mut (),
        );

        let mut output = GenericArray::default();

        // compat API test
        #[cfg(feature = "std")]
        {
            crate::compat::scrypt(
                Self::PASSWORD,
                Self::SALT,
                Self::CF::U8.try_into().unwrap(),
                Self::R::U32.try_into().unwrap(),
                Self::P::U32.try_into().unwrap(),
                output.as_mut_slice(),
            );
            assert_eq!(output, Self::KNOWN_ANSWER);
            output.fill(0);
        }

        hmac_state.emit_gather(output_buffers.iter(), &mut output);

        assert_eq!(output, Self::KNOWN_ANSWER);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_case_16_1_1_algorithm_self_test() {
        CaseN16R1P1::algorithm_self_test();
    }

    #[test]
    fn test_case_16_1_1_pipeline_api_test() {
        CaseN16R1P1::pipeline_api_test();
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_case_16384_8_1_algorithm_self_test() {
        CaseN16384R8P1::algorithm_self_test();
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_case_1024_8_16_algorithm_self_test() {
        CaseN1024R8P16::algorithm_self_test();
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_case_1024_1_2_algorithm_self_test() {
        CaseN1024R1P2::algorithm_self_test();
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_case_1024_53_1_algorithm_self_test() {
        CaseN1024R53P1::algorithm_self_test();
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_case_1024_53_1_pipeline_api_test() {
        CaseN1024R53P1::pipeline_api_test();
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_case_1024_53_3_algorithm_self_test() {
        CaseN1024R53P3::algorithm_self_test();
    }
}