vector-core 0.12.0

Core library for Vector: the single source of truth for all Vector clients, SDKs and interfaces.
Documentation
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
571
572
573
574
//! Adversarial checks on the vault's dump resistance: what an attacker who knows the scheme
//! but not the ASLR slide can recover from snapshots of the VAULTS block alone.
//!
//! Run: `CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS=true cargo test --release -p vector-core --lib
//! guarded_key::attack -- --ignored --nocapture --test-threads=1`

use super::*;
use super::fixtures::{setup, HotPaths, PARTNER, SETTER};
use std::collections::HashMap;
use crate::rt::time_std::Instant;


type Dump = Vec<[usize; ARRAY_SIZE]>;

fn dump() -> Dump {
    VAULTS
        .iter()
        .map(|row| {
            let mut out = [0usize; ARRAY_SIZE];
            for (d, s) in out.iter_mut().zip(row) {
                *d = s.load(Ordering::Relaxed);
            }
            out
        })
        .collect()
}

fn changed(a: &Dump, b: &Dump) -> Vec<VaultPos> {
    let mut out = Vec::new();
    for array in 0..ARRAY_COUNT {
        for slot in 0..ARRAY_SIZE {
            if a[array][slot] != b[array][slot] {
                out.push(VaultPos { array, slot });
            }
        }
    }
    out
}

fn slot_lane(slot: usize) -> usize {
    slot / LANE_WIDTH
}

/// `shield()` computed from a dump instead of the live vault.
fn pad_from(d: &Dump, lane: usize, prekey: &[usize]) -> [u8; 32] {
    let mut hasher = blake3::Hasher::new();
    for &array in prekey {
        let row = &d[array][lane * LANE_WIDTH..(lane + 1) * LANE_WIDTH];
        // SAFETY: an initialised [usize] viewed as its bytes, for hashing only.
        let bytes = unsafe { std::slice::from_raw_parts(row.as_ptr() as *const u8, std::mem::size_of_val(row)) };
        hasher.update(bytes);
    }
    *hasher.finalize().as_bytes()
}

fn word(b: &[u8; 32], u: usize) -> usize {
    usize::from_ne_bytes(b[u * 8..u * 8 + 8].try_into().unwrap())
}

/// Every 4-subset of `cands` whose (D1 ^ D2) XOR equals `target`, by meet-in-the-middle on pairs.
struct PairIndex {
    deltas: Vec<usize>,
    pairs: HashMap<usize, Vec<(u16, u16)>>,
}

impl PairIndex {
    fn new(cands: &[VaultPos], d1: &Dump, d2: &Dump) -> Self {
        let deltas: Vec<usize> = cands.iter().map(|p| d1[p.array][p.slot] ^ d2[p.array][p.slot]).collect();
        let mut pairs: HashMap<usize, Vec<(u16, u16)>> = HashMap::new();
        for i in 0..deltas.len() {
            for j in i + 1..deltas.len() {
                pairs.entry(deltas[i] ^ deltas[j]).or_default().push((i as u16, j as u16));
            }
        }
        Self { deltas, pairs }
    }

    fn find4(&self, target: usize) -> Vec<[usize; 4]> {
        let mut out = Vec::new();
        let n = self.deltas.len();
        for i in 0..n {
            for j in i + 1..n {
                let want = target ^ self.deltas[i] ^ self.deltas[j];
                if let Some(list) = self.pairs.get(&want) {
                    for &(k, l) in list {
                        let (k, l) = (k as usize, l as usize);
                        if j < k {
                            out.push([i, j, k, l]);
                        }
                    }
                }
            }
        }
        out
    }
}

struct XorShift(u64);
impl XorShift {
    fn next(&mut self) -> u64 {
        self.0 ^= self.0 << 13;
        self.0 ^= self.0 >> 7;
        self.0 ^= self.0 << 17;
        self.0
    }
    fn ordering(&mut self) -> [usize; PREKEY_ROWS] {
        let mut out = [0usize; PREKEY_ROWS];
        let mut i = 0;
        while i < PREKEY_ROWS {
            let c = (self.next() as usize) & (ARRAY_COUNT - 1);
            if !out[..i].contains(&c) {
                out[i] = c;
                i += 1;
            }
        }
        out
    }
}


/// A partner rewritten in place by someone else's set() would sit in one diff under two pads.
/// For the right prekey ordering, pad1 ^ pad2 picks out its 4 slots per word among the lane's
/// changed slots, and for a wrong one nothing matches: lanes × ordered prekeys, a 64-bit filter
/// and no curve operation per candidate. The partner must stay out of the diff entirely.
#[test]
#[ignore = "attack demo: run by name with --nocapture"]
fn partner_reshield_two_dump_attack() {
    let _l = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
    let partner_key = [0x5Au8; 32];
    setup(partner_key);
    SETTER.set([0x11; 32], &[&PARTNER]);

    let d1 = dump();
    SETTER.set([0x22; 32], &[&PARTNER]);
    let d2 = dump();

    let diff = changed(&d1, &d2);
    let mut per_lane = [0usize; LANE_COUNT];
    for p in &diff {
        per_lane[slot_lane(p.slot)] += 1;
    }
    let lane = PARTNER.lane();
    let truth = share_positions(PARTNER.instance_addr(), lane);
    let in_diff = truth.iter().filter(|p| diff.contains(p)).count();
    println!("changed slots: {}  per lane: {:?}", diff.len(), per_lane);
    println!("partner lane {lane}: {in_diff}/{} of its share slots are in the diff", truth.len());
    if in_diff == SHARE_ENTRIES {
        recover_partner(&diff, &d1, &d2, lane, partner_key);
    }
    assert_eq!(in_diff, 0, "a partner never appears in another key's diff");


    // The key just set has no old copy in D1, so the same filter finds nothing for it.
    let sl = SETTER.lane();
    let scands: Vec<VaultPos> = diff.iter().copied().filter(|p| slot_lane(p.slot) == sl).collect();
    let sindex = PairIndex::new(&scands, &d1, &d2);
    let sprekey = layout(SETTER.instance_addr(), sl).prekey;
    let (s1, s2) = (pad_from(&d1, sl, &sprekey), pad_from(&d2, sl, &sprekey));
    let shits = sindex.find4(word(&s1, 0) ^ word(&s2, 0)).len();
    println!("same filter on the freshly set key: {shits} hits");

    PARTNER.clear(&[&SETTER]);
    SETTER.clear(&[&PARTNER]);
}

/// Over two diffs, decoys rarely land on the same slot twice, so anything rewritten in both
/// stands out. A partner's slots must not be among them.
#[test]
#[ignore = "attack demo: run by name with --nocapture"]
fn partner_slots_fall_out_of_two_diffs() {
    let _l = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
    setup([0x77; 32]);
    let d0 = dump();
    SETTER.set([0x01; 32], &[&PARTNER]);
    let d1 = dump();
    SETTER.set([0x02; 32], &[&PARTNER]);
    let d2 = dump();
    SETTER.clear(&[&PARTNER]);
    let d3 = dump();

    let a = changed(&d0, &d1);
    let b = changed(&d1, &d2);
    let c = changed(&d2, &d3);
    let ab: Vec<VaultPos> = a.iter().copied().filter(|p| b.contains(p)).collect();
    let abc: Vec<VaultPos> = ab.iter().copied().filter(|p| c.contains(p)).collect();
    let lane = PARTNER.lane();
    let truth = share_positions(PARTNER.instance_addr(), lane);
    for (name, set) in [("2 diffs", &ab), ("3 diffs", &abc)] {
        let mut per_lane = [0usize; LANE_COUNT];
        for p in set.iter() {
            per_lane[slot_lane(p.slot)] += 1;
        }
        let hit = truth.iter().filter(|p| set.contains(p)).count();
        println!(
            "{name}: {} slots in the intersection, {hit}/{} are partner shares; per lane {:?} (partner lane {lane})",
            set.len(),
            truth.len(),
            per_lane
        );
        assert_eq!(hit, 0, "a partner's slots never change under another key's writes");
    }
    PARTNER.clear(&[]);
}


/// The one-diff attack on a partner rewritten in place.
fn recover_partner(diff: &[VaultPos], d1: &Dump, d2: &Dump, lane: usize, partner_key: [u8; 32]) {
    let cands: Vec<VaultPos> = diff.iter().copied().filter(|p| slot_lane(p.slot) == lane).collect();
    let t = Instant::now();
    let index = PairIndex::new(&cands, d1, d2);
    println!("candidates in lane: {}  pair index built in {:?}", cands.len(), t.elapsed());

    let prekey = layout(PARTNER.instance_addr(), lane).prekey;
    let (p1, p2) = (pad_from(d1, lane, &prekey), pad_from(d2, lane, &prekey));
    let mut recovered = [0u8; 32];
    for u in 0..USIZES_PER_SHARE {
        let sols = index.find4(word(&p1, u) ^ word(&p2, u));
        assert_eq!(sols.len(), 1, "word {u}: the true prekey ordering yields exactly one 4-subset");
        let mut w = word(&p2, u);
        for &i in &sols[0] {
            w ^= d2[cands[i].array][cands[i].slot];
        }
        recovered[u * 8..u * 8 + 8].copy_from_slice(&w.to_ne_bytes());
    }
    assert_eq!(recovered, partner_key, "partner key recovered from two VAULTS snapshots");
    println!("recovered the partner key from the diff with the true ordering");

    let mut rng = XorShift(0x9E37_79B9_7F4A_7C15);
    let trials = 20_000;
    let mut false_hits = 0;
    let t = Instant::now();
    for _ in 0..trials {
        let guess = rng.ordering();
        if guess == prekey {
            continue;
        }
        let (g1, g2) = (pad_from(d1, lane, &guess), pad_from(d2, lane, &guess));
        if !index.find4(word(&g1, 0) ^ word(&g2, 0)).is_empty() {
            false_hits += 1;
        }
    }
    let per = t.elapsed() / trials;
    let t = Instant::now();
    for _ in 0..trials {
        let guess = rng.ordering();
        std::hint::black_box((pad_from(d1, lane, &guess), pad_from(d2, lane, &guess)));
    }
    let pads = t.elapsed() / trials;
    println!("wrong orderings: {false_hits}/{trials} false hits; {per:?} per guess with pair MITM, {pads:?} for the two pads alone");
    assert_eq!(false_hits, 0);

    let orderings: f64 = (0..PREKEY_ROWS).map(|i| (ARRAY_COUNT - i) as f64).product();
    let space = orderings * LANE_COUNT as f64;
    println!(
        "search space: {LANE_COUNT} lanes × {orderings:.3e} ordered prekeys = 2^{:.1}; \
         at the two-pad cost that is {:.1} core-years (a 4-subset table per lane makes the lookup O(1))",
        space.log2(),
        space * pads.as_secs_f64() / 3.156e7
    );

}

/// `layout_with` for a hypothetical VAULTS base, as an attacker brute-forcing the slide runs it.
fn layout_at(instance: usize, base: usize, lane: usize) -> Layout {
    let h0 = (instance ^ base).wrapping_mul(instance | 1);
    let iters = 16 + ((h0 >> 7) & 15);
    let m1 = (instance as u64) | 1;
    let m2 = (base as u64) | 1;
    let mut h = addr_mix((instance as u64) ^ (base as u64).rotate_left(19), m1, m2, iters);
    let mut prekey = [0usize; PREKEY_ROWS];
    for i in 0..PREKEY_ROWS {
        loop {
            let c = ((h >> 32) as usize) & (ARRAY_COUNT - 1);
            h = addr_mix(h, m1, m2, iters);
            if !prekey[..i].contains(&c) {
                prekey[i] = c;
                break;
            }
        }
    }
    let mut positions = [VaultPos { array: 0, slot: 0 }; 3];
    for i in 0..3 {
        loop {
            let c = VaultPos { array: ((h >> 32) as usize) & (ARRAY_COUNT - 1), slot: lane_slot(h as usize, lane) };
            h = addr_mix(h, m1, m2, iters);
            if !positions[..i].contains(&c) && !prekey.contains(&c.array) {
                positions[i] = c;
                break;
            }
        }
    }
    Layout { prekey, config: (positions[0], positions[1], positions[2]) }
}

/// `share_positions_from` reading a dump.
fn shares_at(d: &Dump, instance: usize, lane: usize, layout: &Layout) -> [VaultPos; SHARE_ENTRIES] {
    let (s, a, b) = layout.config;
    let seed = d[s.array][s.slot] as u64;
    let mul1 = d[a.array][a.slot] as u64;
    let mul2 = d[b.array][b.slot] as u64;
    let mut h = seed ^ (instance as u64).rotate_left(23);
    let mut out = [VaultPos { array: 0, slot: 0 }; SHARE_ENTRIES];
    let config = [s, a, b];
    for i in 0..SHARE_ENTRIES {
        loop {
            h ^= h >> 17;
            h = h.wrapping_mul(mul1 | 1);
            h ^= h >> 13;
            h = h.wrapping_mul(mul2 | 1);
            h ^= h >> 16;
            let c = VaultPos { array: ((h >> 32) as usize) & (ARRAY_COUNT - 1), slot: lane_slot(h as usize, lane) };
            if !config.contains(&c) && !out[..i].contains(&c) && !layout.prekey.contains(&c.array) {
                out[i] = c;
                break;
            }
        }
    }
    out
}

/// Brute-forcing the ASLR slide against two VAULTS snapshots, with the build's static offsets
/// known: a candidate (slide, lane) for the key just set survives only if all its share slots
/// changed. Times a
/// candidate under v2's 16–31 rounds and under v1's 4096–8191.
#[test]
#[ignore = "attack demo: run by name with --nocapture"]
fn aslr_slide_brute_with_a_diff_filter() {
    let _l = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
    setup([0x33; 32]);
    let d1 = dump();
    SETTER.set([0x44; 32], &[&PARTNER]);
    let d2 = dump();
    let mut is_changed = vec![false; ARRAY_COUNT * ARRAY_SIZE];
    for p in changed(&d1, &d2) {
        is_changed[p.array * ARRAY_SIZE + p.slot] = true;
    }

    let base = VAULTS.as_ptr() as usize;
    let offset = SETTER.instance_addr().wrapping_sub(base);
    let real = layout(SETTER.instance_addr(), SETTER.lane());
    let mine = layout_at(SETTER.instance_addr(), base, SETTER.lane());
    assert!(real.prekey == mine.prekey && real.config == mine.config, "replica matches the vault");

    let span = 1usize << 16;
    let t = Instant::now();
    let mut survivors = Vec::new();
    for k in 0..span {
        let cand_base = base.wrapping_add(k.wrapping_sub(span / 2) << 12);
        let inst = cand_base.wrapping_add(offset);
        for lane in 0..LANE_COUNT {
            let lay = layout_at(inst, cand_base, lane);
            let pos = shares_at(&d2, inst, lane, &lay);
            if pos.iter().all(|p| is_changed[p.array * ARRAY_SIZE + p.slot]) {
                survivors.push((k, lane));
            }
        }
    }
    let per = t.elapsed() / (span * LANE_COUNT) as u32;
    println!("{} candidates, survivors {:?} (true: k={}, lane {}), {per:?} each", span * LANE_COUNT, survivors, span / 2, SETTER.lane());
    assert_eq!(survivors, vec![(span / 2, SETTER.lane())]);

    // v1 cost per candidate: config positions alone took 3+ addr_mix calls at 4096–8191 rounds.
    let n = 2_000u32;
    let t = Instant::now();
    for k in 0..n as u64 {
        let mut h = k;
        for _ in 0..3 {
            h = addr_mix(h, 0x9E37_79B9_7F4A_7C15 ^ k | 1, 0xC2B2_AE3D_27D4_EB4F | 1, 6144);
        }
        std::hint::black_box(h);
    }
    let v1 = t.elapsed() / n;
    let cands = (1u64 << 28) as f64 * LANE_COUNT as f64;
    println!(
        "2^28 slides × 16 lanes: v2 {:.0} core-seconds, v1 {:.0} core-seconds",
        cands * per.as_secs_f64(),
        cands * v1.as_secs_f64()
    );
    PARTNER.clear(&[&SETTER]);
    SETTER.clear(&[&PARTNER]);
}

#[inline(never)]
fn scrub_stack() {
    let mut buf = [0u8; 160 * 1024];
    buf.zeroize();
    std::hint::black_box(&buf);
}

/// What one operation left behind on the dead stack, in each form a scan could look for.
struct Residue {
    /// The key's bytes, as a buffer, a cipher's words or a keypair hold them.
    key: usize,
    /// A 16-byte half outside any whole copy: a vector register spill.
    halves: usize,
    /// The key as a 256-bit little-endian integer: secp256k1's scalar limbs.
    limbs: usize,
    /// The pad: XOR it with the shares in the vault and the key falls out.
    pad: usize,
    /// How far below the caller the operation wrote, in bytes.
    depth: usize,
}

/// Run `op` over freshly zeroed stack and count what it left below this frame. `key` and `pad`
/// live on the heap, so the needles are never part of the haystack.
#[inline(never)]
fn residue_of(key: &[u8; 32], pad: &[u8; 32], op: &dyn Fn()) -> Residue {
    const SPAN: usize = 128 * 1024;
    let mut snap = vec![0u8; SPAN];
    let marker = 0u8;
    let top = std::hint::black_box(&marker) as *const u8 as usize;
    scrub_stack();
    op();
    // SAFETY: reads this thread's own stack below the live frame; the region is mapped.
    unsafe {
        for (i, b) in snap.iter_mut().enumerate() {
            *b = std::ptr::read_volatile((top - SPAN + i) as *const u8);
        }
    }
    let find = |needle: &[u8]| snap.windows(needle.len()).filter(|w| *w == needle).count();
    let mut limbs = *key;
    limbs.reverse();
    let whole = find(key);
    Residue {
        key: whole,
        halves: find(&key[..16]) + find(&key[16..]) - 2 * whole,
        limbs: find(&limbs),
        pad: find(pad),
        depth: SPAN - snap.iter().position(|&b| b != 0).unwrap_or(SPAN),
    }
}

fn pad_of(vault: &GuardedKey) -> Box<[u8; 32]> {
    let lane = vault.lane();
    Box::new(shield(lane, &layout(vault.instance_addr(), lane).prekey))
}


/// Every hot path that reads a vault, and what each leaves on the dead stack once it returns.
/// A copy here outlives the operation until something else happens to overwrite it.
#[test]
#[ignore = "attack demo: run by name with --nocapture"]
fn stack_residue_by_path() {
    let _l = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
    let hot = HotPaths::new();
    for (name, vault, op) in hot.paths() {
        let pad = pad_of(vault);
        let r = residue_of(&hot.key, &pad, &*op);
        println!(
            "{name:<40} key {:>2}  halves {:>2}  limbs {:>2}  pad {:>2}  depth {:>6}",
            r.key, r.halves, r.limbs, r.pad, r.depth
        );
    }
}

/// The cost of keeping decoys off live prekey rows: those rows go quiet. Counts the 256-slot
/// rows that no decoy touched across N diffs, and how many of them are the partner's prekey.
#[test]
#[ignore = "attack demo: run by name with --nocapture"]
fn quiet_prekey_rows_by_diff_count() {
    let _l = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
    setup([0x66; 32]);
    let lane = PARTNER.lane();
    let prekey = layout(PARTNER.instance_addr(), lane).prekey;
    let mut touched = vec![false; ARRAY_COUNT * LANE_COUNT];
    let mut prev = dump();
    for n in 1..=4u8 {
        if n % 2 == 1 { SETTER.set([n; 32], &[&PARTNER]) } else { SETTER.clear(&[&PARTNER]) }
        let next = dump();
        for p in changed(&prev, &next) {
            touched[p.array * LANE_COUNT + slot_lane(p.slot)] = true;
        }
        let quiet: Vec<(usize, usize)> = (0..ARRAY_COUNT * LANE_COUNT)
            .filter(|&i| !touched[i])
            .map(|i| (i / LANE_COUNT, i % LANE_COUNT))
            .collect();
        let real = quiet.iter().filter(|(a, l)| *l == lane && prekey.contains(a)).count();
        println!("{n} diff(s): {} quiet rows, {real}/{PREKEY_ROWS} of them the partner's prekey", quiet.len());
        prev = next;
    }
    SETTER.clear(&[&PARTNER]);
    PARTNER.clear(&[]);
}

/// A key set and later replaced or cleared would, if its shares were scrubbed, show up in both
/// diffs: written in the first, rewritten in the second. The dump between holds it live.
#[test]
#[ignore = "attack demo: run by name with --nocapture"]
fn retired_slots_stay_out_of_two_diffs() {
    let _l = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
    setup([0x55; 32]);
    for retire_by_clear in [false, true] {
        let d0 = dump();
        let first = [0x99u8; 32];
        SETTER.set(first, &[&PARTNER]);
        let lane = SETTER.lane();
        let live = share_positions(SETTER.instance_addr(), lane);
        let prekey = layout(SETTER.instance_addr(), lane).prekey;
        let d1 = dump();
        if retire_by_clear { SETTER.clear(&[&PARTNER]) } else { SETTER.set([0x98; 32], &[&PARTNER]) }
        let d2 = dump();

        let b = changed(&d1, &d2);
        let both: Vec<VaultPos> = changed(&d0, &d1).into_iter().filter(|p| b.contains(p)).collect();
        let hit = live.iter().filter(|p| both.contains(p)).count();
        let mut stale = pad_from(&d2, lane, &prekey);
        for (i, p) in live.iter().enumerate() {
            let off = (i % USIZES_PER_SHARE) * 8;
            for (a, b) in stale[off..off + 8].iter_mut().zip(d2[p.array][p.slot].to_ne_bytes()) { *a ^= b; }
        }
        println!(
            "retired by {}: {} slots in both diffs, {hit}/{} of them the key's live slots; old key readable after: {}",
            if retire_by_clear { "clear" } else { "set" },
            both.len(),
            live.len(),
            stale == first
        );
        // Decoys hit any slot with p = 1/64 per pass, so stray hits are ~Binomial(16, 1/64);
        // a scrubbed retirement shows all 16.
        assert!(hit <= 8, "a retired key's slots are not rewritten: {hit}/16");
        assert_ne!(stale, first, "a retired key cannot be read back from what it left behind");
        if !retire_by_clear { SETTER.clear(&[&PARTNER]); }
    }
    PARTNER.clear(&[]);
}

/// A slot whose value changed by its lowest bit alone was forced odd, not decoyed: random
/// decoys almost never do that, so it names a key's multiplier slots.
#[test]
fn low_bit_only_changes_across_sets() {
    let _l = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
    setup([0x21; 32]);
    let mut flagged = 0;
    let sets = 40u8;
    for n in 0..sets {
        let d1 = dump();
        SETTER.set([n; 32], &[&PARTNER]);
        let d2 = dump();
        flagged += changed(&d1, &d2).iter().filter(|p| d1[p.array][p.slot] ^ d2[p.array][p.slot] == 1).count();
    }
    println!("{sets} sets: {flagged} slots changed by their low bit alone");
    assert_eq!(flagged, 0, "every write is a whole random word");
    SETTER.clear(&[&PARTNER]);
    PARTNER.clear(&[]);
}

/// A key set again into a lane it held before re-derives that lane's positions. Unless they
/// move, its new shares land on its old slots, which then change in two diffs.
#[test]
fn a_lane_held_again_gets_fresh_slots() {
    let _l = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
    setup([0x31; 32]);
    let mut seen: HashMap<usize, [VaultPos; SHARE_ENTRIES]> = HashMap::new();
    let (mut reuses, mut same) = (0, 0);
    for n in 0..200u8 {
        SETTER.set([n; 32], &[&PARTNER]);
        let lane = SETTER.lane();
        let now = share_positions(SETTER.instance_addr(), lane);
        if let Some(before) = seen.get(&lane) {
            reuses += 1;
            let overlap = now.iter().filter(|p| before.contains(p)).count();
            if overlap == SHARE_ENTRIES {
                same += 1;
            }
        }
        seen.insert(lane, now);
    }
    println!("{reuses} sets into a lane held before: {same} landed on exactly the same 16 slots");
    assert!(reuses > 0, "the run reused a lane");
    assert_eq!(same, 0, "a fresh seed moves the shares");
    SETTER.clear(&[&PARTNER]);
    PARTNER.clear(&[]);
}