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
//! State fingerprints and exact-store keys.
//!
//! A state is the tuple of its variables' values in the IR's variable order.
//! [`state_key`] concatenates the variables' canonical value encodings
//! ([`dive_value::codec`]): within one run, two states have equal keys exactly when
//! they are equal.
//!
//! [`StateFingerprinter`] computes the state fingerprint: dive's
//! two-level fingerprint (FP64 value fingerprints combined by [`combine`]), or
//! TLC's own.
use ;
use ;
use crateStoreError;
/// Appends the canonical exact-store key of a state.
///
/// # Errors
/// TLC's fingerprinting errors (a variable holding `Nat`, say), as
/// [`StoreError::Value`].
/// The state fingerprint, in one of two schemes.
///
/// - **dive's** ([`StateFingerprinter::new`], the default profile): each
/// variable's value is fingerprinted with FP64 under the run's polynomial,
/// and the value fingerprints are combined, in the IR's variable order, by a
/// non-linear keyed chain ([`combine`]): every value fingerprint is mixed
/// with its position's key by a 64-bit bijective mixer ([`mix64`]), and the
/// chain mixes again after each one. Value fingerprints are cached in shared
/// compound values ([`TlcFingerprint::fingerprint_cached`]), so a successor
/// pays only for the variables whose values it changed; combining costs two
/// mixer calls per variable.
/// - **TLC's** ([`StateFingerprinter::tlc`], `--compat=tlc`):
/// `TLCStateMut.fingerPrint` without `VIEW` or symmetry, one FP64 stream over
/// the values in TLC's variable order. It equals TLC's fingerprint bit for
/// bit, so the fingerprint set, and the "actual" collision figure computed
/// from it, are TLC's. It cannot reuse cached value fingerprints.
///
/// Both take a state in the IR's variable order. TLC's order is a permutation
/// of it (`dive_ir::Ir::tlc_var_order`), passed in as IR variable indices:
/// `order[i]` is the IR index of the variable whose TLC `varLoc` is `i`.
///
/// The scheme decides only which fingerprints are produced. Verdicts and
/// counts are the same under both, except through a collision.
///
/// **Why the combination is not FP64.** FP64 is affine over GF(2), so FP64
/// over FP64 value fingerprints is affine in every value's bytes: the same
/// change made to two variables cancels whenever its two positional factors
/// agree modulo the polynomial, and such states collide with certainty
/// (issue #42). The mixed chain has no such structure. For two states that
/// differ in exactly one variable whose value fingerprints differ, the chain
/// cannot collide at all (each step is a bijection); otherwise a collision
/// needs the mixer outputs to agree, which for unrelated inputs happens with
/// probability about `2^-64`, the per-pair figure TLC's estimates assume.
/// The key of variable position `i`: `(i + 1)` times the 64-bit golden ratio,
/// so every position has a distinct, dense key.
const
/// The chain's starting value (the first 64 bits of π's fraction).
const CHAIN_SEED: u64 = 0x243F_6A88_85A3_08D3;
/// `SplitMix64`'s output function (Steele, Lea and Flood, 2014; the constants of
/// David Stafford's "Mix13"), as in Sebastiano Vigna's public-domain
/// `splitmix64.c`: a bijection of `u64` with full avalanche.
///
/// ```
/// use dive_store::state::mix64;
/// assert_eq!(mix64(0), 0);
/// assert_eq!(mix64(1), 0x5692_161D_100B_05E5);
/// // `splitmix64.c`'s first output from seed 0 (state `0x9E3779B97F4A7C15`).
/// assert_eq!(mix64(0x9E37_79B9_7F4A_7C15), 0xE220_A839_7B1D_CDAF);
/// ```
pub const
/// The running state of [`combine`].
/// dive's combination of per-variable value fingerprints into a state
/// fingerprint, in variable order:
///
/// ```text
/// h₀ = SEED; hᵢ₊₁ = mix64(hᵢ ⊕ mix64(fpᵢ ⊕ Kᵢ)); result hₙ
/// ```
///
/// where `Kᵢ` is a distinct key per position and [`mix64`] is `SplitMix64`'s
/// bijective output function. It is not affine over GF(2) (nor over the
/// integers modulo `2^64`), so equal changes in two variables do not cancel,
/// and it is a bijection in each `fpᵢ` when the others are fixed.
///
/// ```
/// use dive_store::state::combine;
/// // Position-keyed: swapping two variables' values changes the fingerprint.
/// assert_ne!(combine([1, 2]), combine([2, 1]));
/// assert_ne!(combine([7]), combine([7, 7]));
/// ```