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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use crate::guts::{Finalize, Implementation, Job, LastNode, Stride};
use crate::many;
use crate::Count;
use crate::Hash;
use crate::Word;
use crate::BLOCKBYTES;
use crate::KEYBYTES;
use crate::OUTBYTES;
use core::cmp;
use core::fmt;
use core::mem::size_of;
#[cfg(feature = "std")]
use std;
pub(crate) const DEGREE: usize = 4;
pub fn blake2bp(input: &[u8]) -> Hash {
Params::new().hash(input)
}
#[derive(Clone)]
pub struct Params {
hash_length: u8,
key_length: u8,
key: [u8; KEYBYTES],
implementation: Implementation,
}
impl Params {
pub fn new() -> Self {
Self {
hash_length: OUTBYTES as u8,
key_length: 0,
key: [0; KEYBYTES],
implementation: Implementation::detect(),
}
}
fn to_words(&self) -> ([[Word; 8]; DEGREE], [Word; 8]) {
let mut base_params = crate::Params::new();
base_params
.hash_length(self.hash_length as usize)
.key(&self.key[..self.key_length as usize])
.fanout(DEGREE as u8)
.max_depth(2)
.max_leaf_length(0)
.inner_hash_length(OUTBYTES);
let leaf_words = |worker_index| {
base_params
.clone()
.node_offset(worker_index)
.node_depth(0)
.to_words()
};
let leaf_words = [leaf_words(0), leaf_words(1), leaf_words(2), leaf_words(3)];
let root_words = base_params
.clone()
.node_offset(0)
.node_depth(1)
.to_words();
(leaf_words, root_words)
}
pub fn hash(&self, input: &[u8]) -> Hash {
if self.key_length > 0 {
return self.to_state().update(input).finalize();
}
let (mut leaf_words, mut root_words) = self.to_words();
let jobs = leaf_words.iter_mut().enumerate().map(|(i, words)| {
let input_start = cmp::min(input.len(), i * BLOCKBYTES);
Job {
input: &input[input_start..],
words,
count: 0,
last_node: if i == DEGREE - 1 {
LastNode::Yes
} else {
LastNode::No
},
}
});
many::compress_many(jobs, self.implementation, Finalize::Yes, Stride::Parallel);
finalize_root_words(
&leaf_words,
&mut root_words,
self.hash_length,
self.implementation,
)
}
pub fn to_state(&self) -> State {
State::with_params(self)
}
pub fn hash_length(&mut self, length: usize) -> &mut Self {
assert!(
1 <= length && length <= OUTBYTES,
"Bad hash length: {}",
length
);
self.hash_length = length as u8;
self
}
pub fn key(&mut self, key: &[u8]) -> &mut Self {
assert!(key.len() <= KEYBYTES, "Bad key length: {}", key.len());
self.key_length = key.len() as u8;
self.key = [0; KEYBYTES];
self.key[..key.len()].copy_from_slice(key);
self
}
}
impl Default for Params {
fn default() -> Self {
Self::new()
}
}
impl fmt::Debug for Params {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Params {{ hash_length: {}, key_length: {} }}",
self.hash_length,
self.key_length,
)
}
}
#[derive(Clone)]
pub struct State {
leaf_words: [[Word; 8]; DEGREE],
root_words: [Word; 8],
buf: [u8; 2 * DEGREE * BLOCKBYTES],
buf_len: u16,
count: Count,
hash_length: u8,
implementation: Implementation,
is_keyed: bool,
}
impl State {
pub fn new() -> Self {
Self::with_params(&Params::default())
}
fn with_params(params: &Params) -> Self {
let (leaf_words, root_words) = params.to_words();
let mut buf = [0; 2 * DEGREE * BLOCKBYTES];
let mut buf_len = 0;
if params.key_length > 0 {
for i in 0..DEGREE {
let keybytes = ¶ms.key[..params.key_length as usize];
buf[i * BLOCKBYTES..][..keybytes.len()].copy_from_slice(keybytes);
buf_len = BLOCKBYTES * DEGREE;
}
}
Self {
leaf_words,
root_words,
buf,
buf_len: buf_len as u16,
count: 0,
hash_length: params.hash_length,
implementation: params.implementation,
is_keyed: params.key_length > 0,
}
}
fn fill_buf(&mut self, input: &mut &[u8]) {
let take = cmp::min(self.buf.len() - self.buf_len as usize, input.len());
self.buf[self.buf_len as usize..][..take].copy_from_slice(&input[..take]);
self.buf_len += take as u16;
*input = &input[take..];
}
fn compress_to_leaves(
leaves: &mut [[Word; 8]; DEGREE],
input: &[u8],
count: &mut Count,
implementation: Implementation,
) {
let jobs = leaves.iter_mut().enumerate().map(|(i, words)| {
Job {
input: &input[i * BLOCKBYTES..],
words,
count: *count,
last_node: LastNode::No,
}
});
many::compress_many(jobs, implementation, Finalize::No, Stride::Parallel);
*count = count.wrapping_add((input.len() / DEGREE) as Count);
}
pub fn update(&mut self, mut input: &[u8]) -> &mut Self {
if self.buf_len > 0 {
self.fill_buf(&mut input);
if !input.is_empty() {
if input.len() > (DEGREE - 1) * BLOCKBYTES {
Self::compress_to_leaves(
&mut self.leaf_words,
&self.buf,
&mut self.count,
self.implementation,
);
self.buf_len = 0;
} else {
Self::compress_to_leaves(
&mut self.leaf_words,
&self.buf[..DEGREE * BLOCKBYTES],
&mut self.count,
self.implementation,
);
self.buf_len = (DEGREE * BLOCKBYTES) as u16;
let (buf_front, buf_back) = self.buf.split_at_mut(DEGREE * BLOCKBYTES);
buf_front.copy_from_slice(buf_back);
}
}
}
let needed_tail = (DEGREE - 1) * BLOCKBYTES + 1;
let mut bulk_bytes = input.len().saturating_sub(needed_tail);
bulk_bytes -= bulk_bytes % (DEGREE * BLOCKBYTES);
if bulk_bytes > 0 {
Self::compress_to_leaves(
&mut self.leaf_words,
&input[..bulk_bytes],
&mut self.count,
self.implementation,
);
input = &input[bulk_bytes..];
}
self.fill_buf(&mut input);
debug_assert_eq!(0, input.len());
self
}
pub fn finalize(&self) -> Hash {
let buf_len = self.buf_len as usize;
let mut leaves_copy = self.leaf_words;
let jobs = leaves_copy
.iter_mut()
.enumerate()
.map(|(leaf_index, leaf_words)| {
let input = &self.buf[cmp::min(leaf_index * BLOCKBYTES, buf_len)..buf_len];
Job {
input,
words: leaf_words,
count: self.count,
last_node: if leaf_index == DEGREE - 1 {
LastNode::Yes
} else {
LastNode::No
},
}
});
many::compress_many(jobs, self.implementation, Finalize::Yes, Stride::Parallel);
let mut root_words_copy = self.root_words;
finalize_root_words(
&leaves_copy,
&mut root_words_copy,
self.hash_length,
self.implementation,
)
}
pub fn count(&self) -> Count {
let mut ret = self
.count
.wrapping_mul(DEGREE as Count)
.wrapping_add(self.buf_len as Count);
if self.is_keyed {
ret -= (DEGREE * BLOCKBYTES) as Count;
}
ret
}
}
#[cfg(feature = "std")]
impl std::io::Write for State {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.update(buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
impl fmt::Debug for State {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"State {{ count: {}, hash_length: {} }}",
self.count(),
self.hash_length,
)
}
}
impl Default for State {
fn default() -> Self {
Self::with_params(&Params::default())
}
}
fn finalize_root_words(
leaf_words: &[[Word; 8]; DEGREE],
root_words: &mut [Word; 8],
hash_length: u8,
imp: Implementation,
) -> Hash {
debug_assert_eq!(OUTBYTES, 8 * size_of::<Word>());
let mut block = [0; DEGREE * OUTBYTES];
for (word, chunk) in leaf_words
.iter()
.flat_map(|words| words.iter())
.zip(block.chunks_exact_mut(size_of::<Word>()))
{
chunk.copy_from_slice(&word.to_le_bytes());
}
imp.compress1_loop(
&block,
root_words,
0,
LastNode::Yes,
Finalize::Yes,
Stride::Serial,
);
Hash {
bytes: crate::state_words_to_bytes(&root_words),
len: hash_length,
}
}
pub(crate) fn force_portable(params: &mut Params) {
params.implementation = Implementation::portable();
}
#[cfg(test)]
pub(crate) mod test {
use super::*;
use crate::paint_test_input;
fn blake2bp_reference(input: &[u8]) -> Hash {
let mut leaves = arrayvec::ArrayVec::<[_; DEGREE]>::new();
for leaf_index in 0..DEGREE {
leaves.push(
crate::Params::new()
.fanout(DEGREE as u8)
.max_depth(2)
.node_offset(leaf_index as u64)
.inner_hash_length(OUTBYTES)
.to_state(),
);
}
leaves[DEGREE - 1].set_last_node(true);
for (i, chunk) in input.chunks(BLOCKBYTES).enumerate() {
leaves[i % DEGREE].update(chunk);
}
let mut root = crate::Params::new()
.fanout(DEGREE as u8)
.max_depth(2)
.node_depth(1)
.inner_hash_length(OUTBYTES)
.last_node(true)
.to_state();
for leaf in &mut leaves {
root.update(leaf.finalize().as_bytes());
}
root.finalize()
}
#[test]
fn test_against_reference() {
let mut buf = [0; 21 * BLOCKBYTES];
paint_test_input(&mut buf);
for num_blocks in 0..=20 {
for &extra in &[0, 1, BLOCKBYTES - 1] {
for &portable in &[false, true] {
let mut params = Params::new();
if portable {
force_portable(&mut params);
}
let input = &buf[..num_blocks * BLOCKBYTES + extra];
let expected = blake2bp_reference(&input);
let mut state = params.to_state();
let found = state.update(input).finalize();
assert_eq!(expected, found);
let mut state = params.to_state();
let maybe_one = cmp::min(1, input.len());
state.update(&input[..maybe_one]);
assert_eq!(maybe_one as Count, state.count());
state.finalize();
state.update(&input[maybe_one..]);
assert_eq!(input.len() as Count, state.count());
let found = state.finalize();
assert_eq!(expected, found);
assert_eq!(expected, blake2bp(input));
}
}
}
}
}