lib-q-k12 0.0.9

Pure Rust implementation of the KangarooTwelve hash function for lib-Q
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
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::fmt;

use digest::block_api::{
    AlgorithmName,
    Block,
    BlockSizeUser,
    Buffer,
    BufferKindUser,
    Eager,
    ExtendableOutputCore,
    UpdateCore,
    XofReaderCore,
};
use digest::consts::{
    U128,
    U136,
    U168,
};
use digest::{
    ExtendableOutputReset,
    HashMarker,
    Reset,
    Update,
    XofReader,
};
#[cfg(feature = "alloc")]
use lib_q_sha3::parallel::{
    turbo_shake128_x4,
    turbo_shake256_x4,
};
use lib_q_sha3::{
    TurboShake128,
    TurboShake128Reader,
    TurboShake256,
    TurboShake256Reader,
};

const CHUNK_SIZE: usize = 8192;
/// Number of leaves hashed together by the AVX2-accelerated batched path.
#[cfg(feature = "alloc")]
const LEAF_BATCH: usize = 4;
const LENGTH_ENCODE_SIZE: usize = 255;

macro_rules! impl_k12_core {
    (
        $name:ident, $reader_name:ident, $ts_name:ident, $ts_reader_name:ident, $batch_fn:path,
        $cv_size:literal, $alg_name:literal,
    ) => {
        #[doc = "Core"]
        #[doc = $alg_name]
        #[doc = "hasher state."]
        #[derive(Clone)]
        #[allow(non_camel_case_types)]
        pub struct $name<'cs> {
            customization: &'cs [u8],
            buffer: [u8; CHUNK_SIZE],
            bufpos: usize,
            final_tshk: $ts_name<0x06>,
            chain_tshk: $ts_name<0x0B>,
            chain_length: usize,
            /// Completed full leaf chunks awaiting batched (×4) hashing.
            #[cfg(feature = "alloc")]
            pending: Vec<[u8; CHUNK_SIZE]>,
            /// Whether the K12 inner-node separator has been emitted to `final_tshk`.
            #[cfg(feature = "alloc")]
            separator_emitted: bool,
        }

        impl<'cs> $name<'cs> {
            const CHAINING_VALUE_SIZE: usize = $cv_size;

            #[doc = "Creates a new"]
            #[doc = $alg_name]
            #[doc = "instance with the given customization."]
            pub fn new(customization: &'cs [u8]) -> Self {
                Self {
                    customization,
                    buffer: [0u8; CHUNK_SIZE],
                    bufpos: 0usize,
                    final_tshk: Default::default(),
                    chain_tshk: Default::default(),
                    chain_length: 0usize,
                    #[cfg(feature = "alloc")]
                    pending: Vec::new(),
                    #[cfg(feature = "alloc")]
                    separator_emitted: false,
                }
            }

            // ---- Batched (×4) leaf path — enabled with the `alloc` feature ----
            //
            // A completed full leaf is queued in `pending` rather than hashed
            // immediately; once four have accumulated they are hashed together
            // through the AVX2 `p1600x4` permutation. The chaining values are
            // emitted to `final_tshk` in strict chunk order with the inner-node
            // separator written exactly once, so the digest is byte-identical to
            // the scalar path.
            #[cfg(feature = "alloc")]
            fn process_chunk(&mut self) {
                debug_assert!(self.bufpos == CHUNK_SIZE);
                if self.chain_length == 0 {
                    self.final_tshk.update(&self.buffer);
                } else {
                    self.pending.push(self.buffer);
                    if self.pending.len() == LEAF_BATCH {
                        self.flush_full_batches();
                    }
                }

                self.chain_length += 1;
                self.buffer = [0u8; CHUNK_SIZE];
                self.bufpos = 0;
            }

            /// Emit a chaining value, writing the one-time inner-node separator first.
            #[cfg(feature = "alloc")]
            fn emit_chaining_value(&mut self, cv: &[u8]) {
                if !self.separator_emitted {
                    self.final_tshk
                        .update(&[0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
                    self.separator_emitted = true;
                }
                self.final_tshk.update(cv);
            }

            /// Hash queued leaves four at a time via the batched TurboSHAKE.
            #[cfg(feature = "alloc")]
            fn flush_full_batches(&mut self) {
                while self.pending.len() >= LEAF_BATCH {
                    let mut cvs = [[0u8; Self::CHAINING_VALUE_SIZE]; LEAF_BATCH];
                    {
                        let [c0, c1, c2, c3] = &mut cvs;
                        let p = &self.pending;
                        $batch_fn(
                            0x0B,
                            [&p[0][..], &p[1][..], &p[2][..], &p[3][..]],
                            [
                                c0.as_mut_slice(),
                                c1.as_mut_slice(),
                                c2.as_mut_slice(),
                                c3.as_mut_slice(),
                            ],
                        );
                    }
                    for cv in &cvs {
                        self.emit_chaining_value(cv);
                    }
                    self.pending.drain(0..LEAF_BATCH);
                }
            }

            /// Hash any remaining (< `LEAF_BATCH`) queued leaves with scalar TurboSHAKE.
            #[cfg(feature = "alloc")]
            fn flush_remaining_scalar(&mut self) {
                let pending = core::mem::take(&mut self.pending);
                for chunk in pending {
                    let mut result = [0u8; Self::CHAINING_VALUE_SIZE];
                    self.chain_tshk.update(&chunk);
                    self.chain_tshk.finalize_xof_reset_into(&mut result);
                    self.emit_chaining_value(&result);
                }
            }

            #[cfg(feature = "alloc")]
            fn process_chaining_chunk(&mut self) {
                debug_assert!(self.bufpos != 0);
                let mut result = [0u8; Self::CHAINING_VALUE_SIZE];
                self.chain_tshk.update(&self.buffer[..self.bufpos]);
                self.chain_tshk.finalize_xof_reset_into(&mut result);
                self.emit_chaining_value(&result);
            }

            // ---- Scalar leaf path — used when `alloc` is disabled ----
            #[cfg(not(feature = "alloc"))]
            fn process_chunk(&mut self) {
                debug_assert!(self.bufpos == CHUNK_SIZE);
                if self.chain_length == 0 {
                    self.final_tshk.update(&self.buffer);
                } else {
                    self.process_chaining_chunk();
                }

                self.chain_length += 1;
                self.buffer = [0u8; CHUNK_SIZE];
                self.bufpos = 0;
            }

            #[cfg(not(feature = "alloc"))]
            fn process_chaining_chunk(&mut self) {
                debug_assert!(self.bufpos != 0);
                if self.chain_length == 1 {
                    self.final_tshk
                        .update(&[0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
                }

                let mut result = [0u8; Self::CHAINING_VALUE_SIZE];
                self.chain_tshk.update(&self.buffer[..self.bufpos]);
                self.chain_tshk.finalize_xof_reset_into(&mut result);
                self.final_tshk.update(&result);
            }
        }

        impl HashMarker for $name<'_> {}

        impl BlockSizeUser for $name<'_> {
            type BlockSize = U128;
        }

        impl BufferKindUser for $name<'_> {
            type BufferKind = Eager;
        }

        impl UpdateCore for $name<'_> {
            #[inline]
            fn update_blocks(&mut self, blocks: &[Block<Self>]) {
                for block in blocks {
                    if self.bufpos == CHUNK_SIZE {
                        self.process_chunk();
                    }

                    self.buffer[self.bufpos..self.bufpos + 128].clone_from_slice(block);
                    self.bufpos += 128;
                }
            }
        }

        impl ExtendableOutputCore for $name<'_> {
            type ReaderCore = $reader_name;

            #[inline]
            fn finalize_xof_core(&mut self, buffer: &mut Buffer<Self>) -> Self::ReaderCore {
                let mut lenbuf = [0u8; LENGTH_ENCODE_SIZE];

                // Digest customization
                buffer.digest_blocks(self.customization, |block| self.update_blocks(block));
                buffer.digest_blocks(
                    length_encode(self.customization.len(), &mut lenbuf),
                    |block| self.update_blocks(block),
                );

                if self.bufpos == CHUNK_SIZE && buffer.get_pos() != 0 {
                    self.process_chunk();
                }

                // Read leftover data from buffer
                self.buffer[self.bufpos..(self.bufpos + buffer.get_pos())]
                    .copy_from_slice(buffer.get_data());
                self.bufpos += buffer.get_pos();

                // Calculate final node
                if self.chain_length == 0 {
                    // Input did not exceed a single chaining value
                    let tshk = $ts_name::<0x07>::default()
                        .chain(&self.buffer[..self.bufpos])
                        .finalize_xof_reset();
                    return $reader_name { tshk };
                }

                // Flush queued full leaves (batched, then any scalar remainder)
                // before the final leftover leaf so chaining values stay in order.
                #[cfg(feature = "alloc")]
                {
                    self.flush_full_batches();
                    self.flush_remaining_scalar();
                }

                // Calculate last chaining value
                self.process_chaining_chunk();

                // Pad final node calculation
                self.final_tshk
                    .update(length_encode(self.chain_length, &mut lenbuf));
                self.final_tshk.update(&[0xFF, 0xFF]);

                $reader_name {
                    tshk: self.final_tshk.finalize_xof_reset(),
                }
            }
        }

        impl Default for $name<'_> {
            #[inline]
            fn default() -> Self {
                Self {
                    customization: &[],
                    buffer: [0u8; CHUNK_SIZE],
                    bufpos: 0usize,
                    final_tshk: Default::default(),
                    chain_tshk: Default::default(),
                    chain_length: 0usize,
                    #[cfg(feature = "alloc")]
                    pending: Vec::new(),
                    #[cfg(feature = "alloc")]
                    separator_emitted: false,
                }
            }
        }

        impl Reset for $name<'_> {
            #[inline]
            fn reset(&mut self) {
                *self = Self::new(self.customization);
            }
        }

        impl AlgorithmName for $name<'_> {
            fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str($alg_name)
            }
        }

        impl fmt::Debug for $name<'_> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
                f.write_str(concat!(stringify!($name), " { ... }"))
            }
        }

        impl Drop for $name<'_> {
            fn drop(&mut self) {
                #[cfg(feature = "zeroize")]
                {
                    use digest::zeroize::Zeroize;
                    self.buffer.zeroize();
                    self.bufpos.zeroize();
                    self.chain_length.zeroize();
                    #[cfg(feature = "alloc")]
                    {
                        for chunk in self.pending.iter_mut() {
                            chunk.zeroize();
                        }
                        self.pending.clear();
                        self.separator_emitted.zeroize();
                    }
                }
            }
        }

        #[cfg(feature = "zeroize")]
        impl digest::zeroize::ZeroizeOnDrop for $name<'_> {}

        #[doc = "Core"]
        #[doc = $alg_name]
        #[doc = "reader state."]
        #[derive(Clone)]
        pub struct $reader_name {
            tshk: $ts_reader_name,
        }

        impl XofReaderCore for $reader_name {
            #[inline]
            fn read_block(&mut self) -> Block<Self> {
                let mut block = Block::<Self>::default();
                self.tshk.read(&mut block);
                block
            }
        }

        #[cfg(feature = "zeroize")]
        impl digest::zeroize::ZeroizeOnDrop for $reader_name {}
    };
}

impl_k12_core!(
    Kt128Core,
    Kt128ReaderCore,
    TurboShake128,
    TurboShake128Reader,
    turbo_shake128_x4,
    32,
    "KT128",
);
impl_k12_core!(
    Kt256Core,
    Kt256ReaderCore,
    TurboShake256,
    TurboShake256Reader,
    turbo_shake256_x4,
    64,
    "KT256",
);

impl BlockSizeUser for Kt128ReaderCore {
    type BlockSize = U168; // TurboSHAKE128 block size
}

impl BlockSizeUser for Kt256ReaderCore {
    type BlockSize = U136; // TurboSHAKE256 block size
}

/// Length-encodes `len` for KangarooTwelve per RFC 9861 Section 3.3.
pub fn length_encode(mut length: usize, buffer: &mut [u8; LENGTH_ENCODE_SIZE]) -> &mut [u8] {
    let mut bufpos = 0usize;
    while length > 0 {
        buffer[bufpos] = (length % 256) as u8;
        length /= 256;
        bufpos += 1;
    }
    buffer[..bufpos].reverse();

    buffer[bufpos] = bufpos as u8;
    bufpos += 1;

    &mut buffer[..bufpos]
}

#[test]
fn test_length_encode() {
    let mut buffer = [0u8; LENGTH_ENCODE_SIZE];
    assert_eq!(length_encode(0, &mut buffer), &[0x00]);
    assert_eq!(length_encode(12, &mut buffer), &[0x0C, 0x01]);
    assert_eq!(length_encode(65538, &mut buffer), &[0x01, 0x00, 0x02, 0x03]);
}