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
// SPDX-License-Identifier: Apache-2.0 OR MIT
// SPDX-FileCopyrightText: Copyright (C) 2024 Tsukasa OI <floss_ssdeep@irq.a4lg.com>.
//! Pearson hashing and the TLSH's B (bucket) mapping.
//!
//! See [Pearson, 1990 (doi:10.1145/78973.78978)](https://doi.org/10.1145%2F78973.78978)
//! and the [Wikipedia article](https://en.wikipedia.org/wiki/Pearson_hashing)
//! for details.
//!
//! # On TLSH
//!
//! TLSH's official implementation often use 4-byte Peason hash updates.
//! For instance, we use following 4 bytes to select the bucket to update:
//!
//! 1. A prime (constant)
//! 2. The latest byte
//! 3. An old byte in the sliding window
//! 4. (likewise)
//!
//! And on short fuzzy hashes, it uses the special transformation (that requires
//! a special substitution table [for finalization](final_48())).
//!
//! We don't implement "manual constant folding on the first byte" optimization
//! as seen in the `fast_b_mapping` function in the original implementation
//! (also see `b_mapping` to see the difference) because LLVM is smart enough
//! to perform the equivalent.
//!
//! Consider following snippets are equivalent except in short fuzzy hashes:
//!
//! ```text
//! // TLSH (unoptimized virtual example)
//! b_mapping(2, a4, a3, a2)
//! // TLSH (manually optimized; excerpt from tlsh_impl.cpp)
//! // 49 is the result after processing the first byte (a prime): 2.
//! fast_b_mapping(49, a4, a3, a2)
//! // fast-tlsh (this crate; internal)
//! final_256(update_double(init(0x02), a4, a3), a2)
//! // fast-tlsh (this crate; public)
//! pearson::tlsh_b_mapping_256(0x02, a4, a3, a2)
//! ```
//!
//! On short fuzzy hashes:
//!
//! ```text
//! // fast-tlsh (this crate; internal)
//! final_48(update_double(init(0x02), a4, a3), a2)
//! // fast-tlsh (this crate; public)
//! pearson::tlsh_b_mapping_48(0x02, a4, a3, a2)
//! ```
/// The initial state of Pearson hashing.
pub const INITIAL_STATE: u8 = 0;
/// The substitution table for Pearson hashing.
pub const SUBST_TABLE: = ;
/// The substitution table for 2 bytes of Pearson hashing.
///
/// Note that the first index denotes the byte 2 (not 1) to maximize
/// address calculation efficiency.
const SUBST_TABLE_DOUBLE: = ;
/// The special substitution table for 48-bucket variant of TLSH.
///
/// For each [`SUBST_TABLE`] value (`x`), this is:
///
/// * `x % 48` (when `x < 240`)
/// * `48` (otherwise)
///
/// It avoids bias on the bucket distribution (because 256 values makes an
/// uneven distribution (`256 % 48 != 0`), they only use first
/// `256 / 48 * 48 == 240` values for bucket counting).
///
/// Instead, it increases the bias on the checksum (because values other than
/// `48` will get intermediate frequency of `5/256` but `48` gets `16/256`;
/// `256 / 48 == 5`, `256 - 256 / 48 * 48 == 16`).
const SUBST_TABLE_48: = ;
/// Process one byte (as a initialization) using Pearson hashing.
///
/// # Example
///
/// ```
/// // Requires the `experiment-pearson` feature.
/// # #[cfg(feature = "experiment-pearson")] {
/// use tlsh::pearson;
///
/// let state = pearson::init(0x02);
/// assert_eq!(state, 0x31);
/// # }
/// ```
pub const
/// Process one byte using Pearson hashing.
///
/// # Examples
///
/// ## Usage
///
/// ```
/// // Requires the `experiment-pearson` feature.
/// # #[cfg(feature = "experiment-pearson")] {
/// use tlsh::pearson;
///
/// let state = pearson::init(0x02);
/// let state = pearson::update(state, 0xbe);
/// let state = pearson::update(state, 0xef);
/// assert_eq!(state, 0x63);
/// # }
/// ```
///
/// ## Relation with [`init()`]
///
/// ```
/// // Requires the `experiment-pearson` feature.
/// # #[cfg(feature = "experiment-pearson")] {
/// use tlsh::pearson;
///
/// // init() is equivalent to updating 1 byte from the initial state.
/// let state1 = pearson::init(0x02);
/// let state2 = pearson::INITIAL_STATE;
/// let state2 = pearson::update(state2, 0x02);
/// assert_eq!(state1, state2);
/// # }
/// ```
pub const
/// Process two bytes using Pearson hashing.
///
/// This function updates the Pearson hashing state with two bytes:
/// `b1` and `b2`.
///
/// This is equivalent to two calls to [`update()`] but may be optimized
/// for faster processing.
///
/// # Example
///
/// ```
/// // Requires the `experiment-pearson` feature.
/// # #[cfg(feature = "experiment-pearson")] {
/// use tlsh::pearson;
///
/// let state = pearson::init(0x02);
/// let state1 = pearson::update(pearson::update(state, 0xbe), 0xef);
/// let state2 = pearson::update_double(state, 0xbe, 0xef);
/// assert_eq!(state1, state2);
/// # }
/// ```
pub const
/// Process one byte using Pearson hashing for 256-bucket finalization.
///
/// On the 256-bucket variant, this is the same as regular [`update()`].
///
/// # Example
///
/// ```
/// // Requires the `experiment-pearson` feature.
/// # #[cfg(feature = "experiment-pearson")] {
/// use tlsh::pearson;
///
/// let state = pearson::init(0x02);
/// let state = pearson::update_double(state, 0xbe, 0xef);
/// let state = pearson::final_256(state, 0x00);
/// assert_eq!(state, 0x4b);
/// # }
/// ```
pub const
/// Process one byte using Pearson hashing for 48-bucket finalization.
///
/// Assuming that the return value of [`final_256()`] is `x`,
/// the return value of this function is as follows:
///
/// * `x % 48` (when `x < 240`)
/// * `48` (otherwise)
///
/// It avoids bias on the bucket distribution (because 256 values makes an
/// uneven distribution (`256 % 48 != 0`), they only use first
/// `256 / 48 * 48 == 240` values for bucket counting).
///
/// Instead, it increases the bias on the checksum (because values other than
/// `48` will get intermediate frequency of `5/256` but `48` gets `16/256`;
/// `256 / 48 == 5`, `256 - 256 / 48 * 48 == 16`).
///
/// # Example
///
/// ```
/// // Requires the `experiment-pearson` feature.
/// # #[cfg(feature = "experiment-pearson")] {
/// use tlsh::pearson;
///
/// let state = pearson::init(0x02);
/// let state = pearson::update_double(state, 0xbe, 0xef);
/// let state = pearson::final_48(state, 0x00);
/// assert_eq!(state, 0x1b);
/// # }
/// ```
pub const
/// TLSH's B (bucket) mapping on the 256-bucket variant.
///
/// On TLSH, the first byte `b0` is a constant (a prime when updating the
/// internal bucket and `0` when updating the internal checksum).
///
/// On the 256-bucket variant, this is the same as updating 4 bytes: `b0`
/// through `b3` (in that order) from the initial state.
///
/// # Example
///
/// ```
/// // Requires the `experiment-pearson` feature.
/// # #[cfg(feature = "experiment-pearson")] {
/// use tlsh::pearson;
///
/// assert_eq!(pearson::tlsh_b_mapping_256(0x02, 0xbe, 0xef, 0x00), 0x4b);
/// # }
/// ```
pub const
/// TLSH's B (bucket) mapping on the 48-bucket variant.
///
/// On TLSH, the first byte `b0` is a constant (a prime when updating the
/// internal bucket and `0` when updating the internal checksum).
///
/// Assuming that the return value of [`tlsh_b_mapping_256()`] is `x`,
/// the return value of this function is as follows:
///
/// * `x % 48` (when `x < 240`)
/// * `48` (otherwise)
///
/// It avoids bias on the bucket distribution (because 256 values makes an
/// uneven distribution (`256 % 48 != 0`), they only use first
/// `256 / 48 * 48 == 240` values for bucket counting).
///
/// Instead, it increases the bias on the checksum (because values other than
/// `48` will get intermediate frequency of `5/256` but `48` gets `16/256`;
/// `256 / 48 == 5`, `256 - 256 / 48 * 48 == 16`).
///
/// # Example
///
/// ```
/// // Requires the `experiment-pearson` feature.
/// # #[cfg(feature = "experiment-pearson")] {
/// use tlsh::pearson;
///
/// assert_eq!(pearson::tlsh_b_mapping_48(0x02, 0xbe, 0xef, 0x00), 0x1b);
/// # }
/// ```
pub const