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
//! Incremental lanehash, equal to the one-shot function at every split.
use super::dispatch::{backend, Backend};
use super::spec::{self, Words, BLOCK, CHAINS, STRIPE};
/// Carry, one block not yet known to be leading, and the 64 bytes that make it so.
const BUF: usize = 64 + BLOCK + 64;
pub struct Stream {
seed: u64,
ks: u64,
total: usize,
/// Whole leading blocks absorbed into `st`.
blocks: usize,
st: Words,
/// Input from byte `512 * blocks - carry` on: the last 64 bytes of the last absorbed
/// block (the closing stripe may reach into it), then unabsorbed input.
buf: [u8; BUF],
buf_len: usize,
backend: &'static Backend,
}
impl Stream {
pub fn new(seed: u64) -> Self {
let ks = spec::ks(seed);
Stream {
seed,
ks,
total: 0,
blocks: 0,
st: Words::from_state(&spec::init(ks)),
buf: [0u8; BUF],
buf_len: 0,
backend: backend(),
}
}
#[inline(always)]
fn carry(&self) -> usize {
if self.blocks > 0 { 64 } else { 0 }
}
/// A block is absorbed only once 64 more bytes follow it, so the closing stripe
/// (the last 64 bytes of the input) is never inside an absorbed block. Bytes already
/// buffered complete their block through the buffer (two blocks at most per call),
/// everything after that is absorbed straight from `data`: with a buffer that kept
/// 64 bytes of look-ahead, a stream fed in 4 KiB pieces went through the buffer block
/// by block, at a third of the one-shot rate.
pub fn update(&mut self, mut data: &[u8]) {
self.total += data.len();
loop {
let carry = self.carry();
let pending = self.buf_len - carry;
if pending == 0 && data.len() >= BLOCK + 64 {
let k = (data.len() - 64) / BLOCK;
// SAFETY: `data` holds `k` whole blocks plus 64 bytes.
unsafe { (self.backend.absorb)(&mut self.st, data.as_ptr(), k) };
self.blocks += k;
let keep = &data[k * BLOCK - 64..];
self.buf[..keep.len()].copy_from_slice(keep);
self.buf_len = keep.len();
return;
}
if pending + data.len() < BLOCK + 64 {
// no block has its 64 bytes of look-ahead yet
self.buf[self.buf_len..self.buf_len + data.len()].copy_from_slice(data);
self.buf_len += data.len();
return;
}
// one block through the buffer: take only the bytes that complete it
if pending < BLOCK {
let take = BLOCK - pending;
self.buf[self.buf_len..self.buf_len + take].copy_from_slice(&data[..take]);
self.buf_len += take;
data = &data[take..];
}
// SAFETY: one whole block at `buf[carry..]`.
unsafe { (self.backend.absorb)(&mut self.st, self.buf.as_ptr().add(carry), 1) };
self.blocks += 1;
// the block's last 64 bytes become the carry, what followed it stays pending
self.buf.copy_within(carry + BLOCK - 64..self.buf_len, 0);
self.buf_len -= carry + BLOCK - 64;
}
}
pub fn finish128(&self) -> u128 {
let n = self.total;
if n <= spec::SHORT_MAX {
return super::short::short128(&self.buf[..self.buf_len], self.seed);
}
if self.blocks == 0 {
// SAFETY: the whole input (65..=639 bytes) is buffered.
return unsafe { (self.backend.hash128)(self.buf.as_ptr(), n, self.ks) };
}
let m = (n - 1) / STRIPE;
let r = m - CHAINS * self.blocks;
debug_assert!(r <= CHAINS && self.buf_len >= 64 + r * STRIPE);
// SAFETY: `buf[64..]` contains the remaining leading stripes and the final
// 64 bytes of the input are at `buf[buf_len - 64..]`.
unsafe {
(self.backend.finish)(
&self.st,
self.buf.as_ptr().add(64),
r,
self.buf.as_ptr().add(self.buf_len - 64),
n,
self.ks,
)
}
}
/// The low half of [`finish128`](Self::finish128).
pub fn finish64(&self) -> u64 {
if self.total <= spec::SHORT_MAX {
return super::short::short64(&self.buf[..self.buf_len], self.seed);
}
self.finish128() as u64
}
}