Skip to main content

ms_codec/
shares.rs

1//! K-of-N codex32 Shamir share encoding (ms v0.2).
2//!
3//! A secret (`entr` or `mnem`) splits into N shares, any K of which recombine
4//! to the original — using codex32's *native* threshold(k)+index Shamir
5//! mechanism, NOT a payload byte (SPEC_ms_v0_2_kofn §1). The codex32 header
6//! threshold char is the share-vs-single discriminator; the prefix byte
7//! (`0x00`=entr / `0x02`=mnem) remains the payload-KIND discriminator, recovered
8//! only on the secret-at-S after interpolation.
9//!
10//! v0.1/mnem single-strings stay byte-identical: `encode_shares(tag, ZERO, 1, &p)`
11//! reduces to the exact `package()`/`encode()` construction (the Phase-0 gate).
12
13use crate::consts::{HRP, RESERVED_ID_BLOCKLIST, SHARE_INDEX_V01};
14use crate::envelope::{dispatch_payload, extract_wire_fields, payload_wire_bytes, wire_string};
15use crate::error::{Error, Result};
16use crate::payload::Payload;
17use crate::tag::Tag;
18use codex32::{Codex32String, Fe};
19use zeroize::Zeroizing;
20
21/// The codex32 bech32 alphabet (32 chars). Index `s` (position 16) is the
22/// secret-at-S index — never a distributed-share index.
23const CODEX32_ALPHABET: &[u8; 32] = b"qpzry9x8gf2tvdw0s3jn54khce6mua7l";
24
25/// The 31 valid non-`s` share indices, taken from the bech32 alphabet in its
26/// own order with `s` removed (deterministic, front-to-back). `n <= 31` is
27/// enforced by `encode_shares`, so this pool never runs out.
28fn non_s_index_pool() -> Vec<Fe> {
29    CODEX32_ALPHABET
30        .iter()
31        .filter(|&&b| b != b's')
32        .map(|&b| Fe::from_char(b as char).expect("alphabet char is a valid Fe"))
33        .collect()
34}
35
36/// Generate a random 4-char codex32-alphabet `id`, re-rolling while it lands in
37/// `RESERVED_ID_BLOCKLIST` (a v0.1 type-tag-shaped value). Uses `getrandom`
38/// (0.3.x `getrandom::fill`) — no injected-RNG param (the `mk_codec::encode`
39/// precedent).
40fn random_id() -> String {
41    loop {
42        let mut raw = [0u8; 4];
43        getrandom::fill(&mut raw).expect("getrandom::fill must not fail");
44        let id: [u8; 4] = [
45            CODEX32_ALPHABET[(raw[0] & 0x1f) as usize],
46            CODEX32_ALPHABET[(raw[1] & 0x1f) as usize],
47            CODEX32_ALPHABET[(raw[2] & 0x1f) as usize],
48            CODEX32_ALPHABET[(raw[3] & 0x1f) as usize],
49        ];
50        if !RESERVED_ID_BLOCKLIST.contains(&id) {
51            // Every byte is a codex32-alphabet ASCII char → always valid UTF-8.
52            return String::from_utf8(id.to_vec()).expect("codex32 alphabet is ASCII");
53        }
54    }
55}
56
57/// A codex32 share threshold.
58///
59/// `ZERO` is the unshared v0.1 single-string sentinel (codex32 threshold `0`,
60/// share-index `s`); `new(k)` accepts a K-of-N share threshold `k in 2..=9`
61/// (codex32 `from_seed` accepts threshold `0` or `2..=9` only — `1` is invalid).
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63pub struct Threshold(u8);
64
65impl Threshold {
66    /// The unshared single-string sentinel (threshold `0`). A const, NOT
67    /// `new(0)` — `new` only admits the K-of-N share range `2..=9`.
68    pub const ZERO: Threshold = Threshold(0);
69
70    /// Construct a K-of-N share threshold. `k` MUST be in `2..=9`, else
71    /// `Error::InvalidThreshold(k)`.
72    pub fn new(k: u8) -> Result<Threshold> {
73        if (2..=9).contains(&k) {
74            Ok(Threshold(k))
75        } else {
76            Err(Error::InvalidThreshold(k))
77        }
78    }
79
80    /// The threshold value (`0` for `ZERO`, `2..=9` for a share threshold).
81    pub fn get(self) -> u8 {
82        self.0
83    }
84}
85
86/// Split a secret (`entr` or `mnem`) into `n` codex32 K-of-N shares.
87///
88/// - `threshold == ZERO`: `n` MUST be 1; returns a single string **byte-identical**
89///   to `encode(tag, secret)` — the v0.1 single-string construction
90///   (`from_seed(HRP, 0, tag, Fe::S, [prefix]||payload)`, deterministic). The
91///   `id` stays the type `tag` (NOT random) — load-bearing for byte-identity.
92/// - `threshold == k ∈ 2..=9`: validate `k <= n <= 31` (else `InvalidShareCount`).
93///   A random 4-char `id` (not in `RESERVED_ID_BLOCKLIST`) keys the share-set.
94///   The secret-at-S (`Fe::S`) holds the real payload; `k-1` random **defining
95///   shares** at fixed canonical non-`s` indices + `interpolate_at` for the
96///   remaining `n-(k-1)` indices produce the `n` **distributed** shares. The
97///   secret-at-S is NEVER returned (it is the recovery target only).
98///
99/// Works identically for `entr` and `mnem` (byte-agnostic); language survives a
100/// `mnem` split (it rides the secret-at-S wire bytes).
101pub fn encode_shares(
102    tag: Tag,
103    threshold: Threshold,
104    n: usize,
105    secret: &Payload,
106) -> Result<Vec<String>> {
107    secret.validate()?;
108    let bytes = payload_wire_bytes(secret);
109
110    if threshold == Threshold::ZERO {
111        // Unshared single-string: must be n==1; byte-identical to encode().
112        if n != 1 {
113            return Err(Error::InvalidShareCount { k: 0, n });
114        }
115        let single = Codex32String::from_seed(HRP, 0, tag.as_str(), Fe::S, &bytes[..])?;
116        return Ok(vec![single.to_string()]);
117    }
118
119    let k = threshold.get();
120    let k_usize = k as usize;
121    // Bounds (SPEC §1): 2 <= k <= n <= 31 (31 valid non-`s` indices).
122    if !(k_usize <= n && n <= 31) {
123        return Err(Error::InvalidShareCount { k, n });
124    }
125
126    let id = random_id();
127    let pool = non_s_index_pool();
128
129    // 1. secret-at-S carries the real payload at index `s`, threshold `k`.
130    let secret_s = Codex32String::from_seed(HRP, k_usize, &id, Fe::S, &bytes[..])?;
131
132    // 2. k-1 random DEFINING shares at the first k-1 pool indices. Each gets a
133    //    CSPRNG payload of the SAME byte length as the secret (Zeroizing scrub).
134    //    The defining set [secret_s, def_1..def_{k-1}] is k points → fully
135    //    determines the Shamir polynomial.
136    let mut defining: Vec<Codex32String> = Vec::with_capacity(k_usize);
137    defining.push(secret_s);
138    for pool_idx in pool.iter().take(k_usize - 1) {
139        let mut filler: Zeroizing<Vec<u8>> = Zeroizing::new(vec![0u8; bytes.len()]);
140        getrandom::fill(&mut filler[..]).expect("getrandom::fill must not fail");
141        let share = Codex32String::from_seed(HRP, k_usize, &id, *pool_idx, &filler[..])?;
142        defining.push(share);
143    }
144
145    // 3. The n DISTRIBUTED shares: the k-1 defining shares (indices 0..k-1) plus
146    //    interpolation-derived shares at the remaining n-(k-1) pool indices.
147    //    The secret-at-S (defining[0]) is NEVER distributed.
148    let mut distributed: Vec<String> = Vec::with_capacity(n);
149    for share in defining.iter().skip(1) {
150        distributed.push(share.to_string());
151    }
152    for pool_idx in pool.iter().take(n).skip(k_usize - 1) {
153        let derived = Codex32String::interpolate_at(&defining, *pool_idx)?;
154        distributed.push(derived.to_string());
155    }
156
157    debug_assert_eq!(distributed.len(), n);
158    Ok(distributed)
159}
160
161/// Recombine `k` (or more) distributed shares of a K-of-N share-set into the
162/// original secret `(Tag, Payload)`.
163///
164/// Pre-validation runs BEFORE `interpolate_at` because codex32's
165/// `interpolate_at` short-circuits when the target index (`s`) is among the
166/// inputs (`lib.rs:262`) — bypassing its own payload validation. Order:
167/// 1. parse each share (`Error::Codex32` on failure — preserves the
168///    within-one-string mixed-case `InvalidCase` rejection), then re-parse the
169///    lowercased copy into the CANONICAL vector (BIP-173 uppercase QR form
170///    folds to canonical lowercase; codex32's `interpolate_at` does raw
171///    case-sensitive cross-share hrp/id compares, so canonicalization here —
172///    not field extraction — is what makes an uppercase or mixed-case SET
173///    combine, and what lets the index-`s` guard below see `b's'`);
174/// 2. **reject any share at index `s`** → `SecretShareSuppliedToCombine` (C1 —
175///    the secret-at-S is the recovery target, never a combine input);
176/// 3. `shares.len() >= k` (the first share's threshold) else surface
177///    `ThresholdNotPassed`;
178/// 4. distinct share indices else `RepeatedIndex` (codex32's own check is lazy);
179/// 5. `interpolate_at(&parsed, Fe::S)` recovers the secret-at-S (surfaces
180///    `Mismatched{Hrp,Id,Threshold,Length}` on inconsistent inputs).
181///
182/// Returns **`(Tag::ENTR, …)`** always: the recovered secret-at-S carries the
183/// share-set's RANDOM `id` (NOT a type tag); the payload KIND is the prefix byte
184/// (via `dispatch_payload`), so the random id is discarded. (We do NOT route
185/// through `discriminate` — it would rebuild a `Tag` from the random id.)
186pub fn combine_shares(shares: &[String]) -> Result<(Tag, Payload)> {
187    // 1. Parse each share (map codex32 parse/checksum failure via Error::Codex32).
188    let parsed: Vec<Codex32String> = shares
189        .iter()
190        .map(|s| Codex32String::from_string(s.clone()).map_err(Error::Codex32))
191        .collect::<Result<Vec<_>>>()?;
192
193    // 1b. Canonicalize: re-parse each share's lowercased wire copy (NEVER
194    //     lowercase before the first parse above — that would launder the
195    //     within-one-string mixed-case `InvalidCase` rejection). codex32's
196    //     checksum engine case-folds, so this re-parse is infallible in
197    //     practice (probe-proven byte-identical for lowercase input); still
198    //     route the Result via `?`. The canonical vector feeds both the field
199    //     extraction below AND `interpolate_at` (whose raw case-sensitive
200    //     cross-share hrp/id compares are why extraction-side lowercasing
201    //     alone cannot fix combine) — it also makes the recovered output
202    //     lowercase.
203    let parsed: Vec<Codex32String> = parsed
204        .iter()
205        .map(|c| {
206            Codex32String::from_string(c.to_string().to_ascii_lowercase())
207                .map_err(Error::Codex32)
208        })
209        .collect::<Result<Vec<_>>>()?;
210
211    if parsed.is_empty() {
212        // No shares → surface as below-threshold (k unknown; report 1/0).
213        return Err(Error::Codex32(codex32::Error::ThresholdNotPassed {
214            threshold: 1,
215            n_shares: 0,
216        }));
217    }
218
219    // Re-parse wire fields for each → (threshold_byte, share_index_byte). Both
220    // are `u8` (Copy), so this owns nothing that borrows the per-share string.
221    // `wire_string` is subsumed by the canonical vector above (already
222    // lowercase) — kept as harmless defense-in-depth; the canonical vector is
223    // the load-bearing mechanism for combine.
224    let fields: Vec<(u8, u8)> = parsed
225        .iter()
226        .map(|c| {
227            let s = wire_string(c);
228            extract_wire_fields(&s).map(|f| (f.threshold_byte, f.share_index_byte))
229        })
230        .collect::<Result<Vec<_>>>()?;
231
232    // 2. C1: reject any input at index `s` BEFORE interpolate_at (the
233    //    short-circuit at codex32 lib.rs:262 would otherwise bypass validation).
234    if fields.iter().any(|&(_, idx)| idx == SHARE_INDEX_V01) {
235        return Err(Error::SecretShareSuppliedToCombine);
236    }
237
238    // 3. count >= k (the first share's threshold char). codex32 thresholds are
239    //    single ASCII digits ('2'..'9'); '0' (an unshared single) here means the
240    //    caller passed a v0.1 single-string into combine — also below any share
241    //    threshold, surfaced as ThresholdNotPassed.
242    let k = (fields[0].0 - b'0') as usize;
243    if parsed.len() < k {
244        return Err(Error::Codex32(codex32::Error::ThresholdNotPassed {
245            threshold: k,
246            n_shares: parsed.len(),
247        }));
248    }
249
250    // 4. distinct share indices (codex32's RepeatedIndex check is lazy — only
251    //    fires for the i==j Lagrange term — so pre-check exhaustively).
252    for i in 0..fields.len() {
253        for j in (i + 1)..fields.len() {
254            if fields[i].1 == fields[j].1 {
255                let idx = Fe::from_char(fields[i].1 as char).map_err(Error::Codex32)?;
256                return Err(Error::Codex32(codex32::Error::RepeatedIndex(idx)));
257            }
258        }
259    }
260
261    // 5. Recover the secret-at-S. Surfaces Mismatched{Hrp,Id,Threshold,Length}
262    //    via Error::Codex32 on inconsistent inputs.
263    let secret = Codex32String::interpolate_at(&parsed, Fe::S).map_err(Error::Codex32)?;
264
265    // Payload KIND is the recovered prefix byte; the id is random → discard it
266    // and always return Tag::ENTR (the kind lives in the Payload, NOT the tag).
267    let data: Zeroizing<Vec<u8>> = Zeroizing::new(secret.parts().data());
268    let payload = dispatch_payload(&data)?;
269    Ok((Tag::ENTR, payload))
270}
271
272#[cfg(test)]
273mod tests {
274    use super::*;
275
276    #[test]
277    fn new_accepts_2_through_9() {
278        for k in 2u8..=9 {
279            let t = Threshold::new(k).unwrap_or_else(|e| panic!("new({k}) should be Ok, got {e:?}"));
280            assert_eq!(t.get(), k);
281        }
282    }
283
284    #[test]
285    fn new_rejects_zero() {
286        assert!(matches!(Threshold::new(0), Err(Error::InvalidThreshold(0))));
287    }
288
289    #[test]
290    fn new_rejects_one() {
291        assert!(matches!(Threshold::new(1), Err(Error::InvalidThreshold(1))));
292    }
293
294    #[test]
295    fn new_rejects_ten() {
296        assert!(matches!(Threshold::new(10), Err(Error::InvalidThreshold(10))));
297    }
298
299    #[test]
300    fn zero_const_get_is_zero() {
301        assert_eq!(Threshold::ZERO.get(), 0);
302    }
303
304    #[test]
305    fn new_five_get_is_five() {
306        assert_eq!(Threshold::new(5).unwrap().get(), 5);
307    }
308
309    // --- encode_shares tests (Task 1.3) ---
310
311    use crate::consts::RESERVED_PREFIX;
312    use crate::encode::encode;
313    use crate::payload::Payload;
314    use crate::tag::Tag;
315    use codex32::{Codex32String, Fe};
316
317    fn entr_p() -> Payload {
318        Payload::Entr(vec![0xCDu8; 16])
319    }
320    fn mnem_p() -> Payload {
321        Payload::Mnem { language: 1, entropy: vec![0xCDu8; 16] }
322    }
323
324    /// Re-parse a share string and return (threshold_char, share_index_char, id).
325    fn share_header(s: &str) -> (char, char, String) {
326        let sep = s.rfind('1').unwrap();
327        let b = s.as_bytes();
328        let threshold = b[sep + 1] as char;
329        let id: String = s[sep + 2..sep + 6].to_string();
330        let index = b[sep + 6] as char;
331        (threshold, index, id)
332    }
333
334    #[test]
335    fn zero_share_is_byte_identical_to_encode_entr() {
336        let p = entr_p();
337        let shares = encode_shares(Tag::ENTR, Threshold::ZERO, 1, &p).unwrap();
338        assert_eq!(shares, vec![encode(Tag::ENTR, &p).unwrap()]);
339    }
340
341    #[test]
342    fn zero_share_is_byte_identical_to_encode_mnem() {
343        let p = mnem_p();
344        let shares = encode_shares(Tag::ENTR, Threshold::ZERO, 1, &p).unwrap();
345        assert_eq!(shares, vec![encode(Tag::ENTR, &p).unwrap()]);
346    }
347
348    #[test]
349    fn zero_share_requires_n_eq_1() {
350        let p = entr_p();
351        assert!(matches!(
352            encode_shares(Tag::ENTR, Threshold::ZERO, 2, &p),
353            Err(Error::InvalidShareCount { k: 0, n: 2 })
354        ));
355    }
356
357    #[test]
358    fn encode_shares_2_of_3_shape() {
359        let p = entr_p();
360        let shares = encode_shares(Tag::ENTR, Threshold::new(2).unwrap(), 3, &p).unwrap();
361        assert_eq!(shares.len(), 3);
362        // Each parses, threshold char '2', distinct non-`s` indices, same id.
363        let mut indices = Vec::new();
364        let mut ids = Vec::new();
365        for s in &shares {
366            Codex32String::from_string(s.clone()).expect("each share must parse");
367            let (thr, idx, id) = share_header(s);
368            assert_eq!(thr, '2', "threshold char");
369            assert_ne!(idx, 's', "distributed share must not be index s");
370            indices.push(idx);
371            ids.push(id);
372        }
373        // Distinct indices.
374        let mut sorted = indices.clone();
375        sorted.sort_unstable();
376        sorted.dedup();
377        assert_eq!(sorted.len(), indices.len(), "indices must be distinct");
378        // Same id across the set.
379        assert!(ids.windows(2).all(|w| w[0] == w[1]), "id must be shared");
380    }
381
382    #[test]
383    fn encode_shares_rejects_n_below_k() {
384        let p = entr_p();
385        assert!(matches!(
386            encode_shares(Tag::ENTR, Threshold::new(2).unwrap(), 1, &p),
387            Err(Error::InvalidShareCount { k: 2, n: 1 })
388        ));
389    }
390
391    #[test]
392    fn encode_shares_rejects_n_32() {
393        let p = entr_p();
394        assert!(matches!(
395            encode_shares(Tag::ENTR, Threshold::new(2).unwrap(), 32, &p),
396            Err(Error::InvalidShareCount { k: 2, n: 32 })
397        ));
398    }
399
400    #[test]
401    fn encode_shares_id_not_in_blocklist() {
402        // Statistical: across many splits, the random id never lands in the blocklist.
403        let p = entr_p();
404        for _ in 0..64 {
405            let shares = encode_shares(Tag::ENTR, Threshold::new(2).unwrap(), 2, &p).unwrap();
406            let (_, _, id) = share_header(&shares[0]);
407            let id_bytes: [u8; 4] = id.as_bytes().try_into().unwrap();
408            assert!(
409                !crate::consts::RESERVED_ID_BLOCKLIST.contains(&id_bytes),
410                "id {id:?} must not be in RESERVED_ID_BLOCKLIST"
411            );
412        }
413    }
414
415    /// Inline round-trip (combine_shares lands in Task 1.4): any k of the n
416    /// distributed shares, interpolated at S, recover the secret wire bytes.
417    #[test]
418    fn encode_shares_round_trip_via_interpolate_entr_and_mnem() {
419        for p in [entr_p(), mnem_p()] {
420            let secret_wire = crate::envelope::payload_wire_bytes(&p);
421            for k in 2u8..=9 {
422                let n = (k as usize) + 2; // exercise interpolation-derived shares
423                let shares = encode_shares(Tag::ENTR, Threshold::new(k).unwrap(), n, &p).unwrap();
424                assert_eq!(shares.len(), n);
425                let parsed: Vec<Codex32String> = shares
426                    .iter()
427                    .map(|s| Codex32String::from_string(s.clone()).unwrap())
428                    .collect();
429                // First k and last k subsets both recover the secret.
430                for subset in [&parsed[..k as usize], &parsed[n - k as usize..]] {
431                    let recovered = Codex32String::interpolate_at(subset, Fe::S).unwrap();
432                    assert_eq!(
433                        recovered.parts().data(),
434                        secret_wire[..],
435                        "k={k} n={n} kind={:?} must recover secret wire bytes",
436                        p.kind()
437                    );
438                }
439            }
440        }
441    }
442
443    // --- combine_shares tests (Task 1.4) ---
444
445    #[test]
446    fn combine_round_trip_entr_and_mnem_all_lengths() {
447        for ent_len in [16usize, 20, 24, 28, 32] {
448            let entr = Payload::Entr(vec![0x37u8; ent_len]);
449            let mnem = Payload::Mnem { language: 7, entropy: vec![0x91u8; ent_len] };
450            for p in [entr, mnem] {
451                for k in 2u8..=9 {
452                    let n = (k as usize) + 1;
453                    let shares =
454                        encode_shares(Tag::ENTR, Threshold::new(k).unwrap(), n, &p).unwrap();
455                    // First k and last k subsets both combine back to the secret.
456                    for subset in [&shares[..k as usize], &shares[n - k as usize..]] {
457                        let (tag, recovered) = combine_shares(subset).unwrap();
458                        assert_eq!(tag, Tag::ENTR, "combine always returns Tag::ENTR");
459                        assert_eq!(
460                            recovered,
461                            p,
462                            "k={k} n={n} ent_len={ent_len} must recover the exact payload"
463                        );
464                    }
465                }
466            }
467        }
468    }
469
470    #[test]
471    fn combine_rejects_below_threshold() {
472        let p = entr_p();
473        let shares = encode_shares(Tag::ENTR, Threshold::new(3).unwrap(), 4, &p).unwrap();
474        // Only 2 of a 3-of-4 set.
475        let err = combine_shares(&shares[..2]).unwrap_err();
476        assert!(
477            matches!(err, Error::Codex32(codex32::Error::ThresholdNotPassed { .. })),
478            "expected ThresholdNotPassed, got {err:?}"
479        );
480    }
481
482    #[test]
483    fn combine_rejects_duplicate_index() {
484        let p = entr_p();
485        let shares = encode_shares(Tag::ENTR, Threshold::new(2).unwrap(), 3, &p).unwrap();
486        // Same share twice → duplicate index.
487        let dup = vec![shares[0].clone(), shares[0].clone()];
488        assert!(matches!(
489            combine_shares(&dup),
490            Err(Error::Codex32(codex32::Error::RepeatedIndex(_)))
491        ));
492    }
493
494    #[test]
495    fn combine_rejects_secret_share_index_s() {
496        // Hand-build the secret-at-S directly (index `s`, threshold 2). It must
497        // be rejected BEFORE interpolate_at (C1 — the short-circuit would
498        // otherwise bypass payload validation).
499        let bytes = crate::envelope::payload_wire_bytes(&entr_p());
500        let secret_s = Codex32String::from_seed(HRP, 2, "tst7", Fe::S, &bytes[..])
501            .unwrap()
502            .to_string();
503        // Need >= k shares to get past the count check and reach the index check;
504        // but the index-s check runs first regardless, so a single secret-s input
505        // is rejected on the index axis.
506        let p = entr_p();
507        let shares = encode_shares(Tag::ENTR, Threshold::new(2).unwrap(), 2, &p).unwrap();
508        let with_secret = vec![secret_s, shares[0].clone()];
509        assert!(matches!(
510            combine_shares(&with_secret),
511            Err(Error::SecretShareSuppliedToCombine)
512        ));
513    }
514
515    #[test]
516    fn combine_rejects_mismatched_threshold() {
517        // Two shares from different-threshold sets, at DISTINCT indices (so the
518        // distinct-index pre-check passes and interpolate_at's eager
519        // MismatchedThreshold check fires). set2[0]=index q; set3[1]=index p.
520        let p = entr_p();
521        let set2 = encode_shares(Tag::ENTR, Threshold::new(2).unwrap(), 2, &p).unwrap();
522        let set3 = encode_shares(Tag::ENTR, Threshold::new(3).unwrap(), 3, &p).unwrap();
523        let mixed = vec![set2[0].clone(), set3[1].clone()];
524        let err = combine_shares(&mixed).unwrap_err();
525        assert!(
526            matches!(err, Error::Codex32(codex32::Error::MismatchedThreshold(..))),
527            "expected MismatchedThreshold, got {err:?}"
528        );
529    }
530
531    #[test]
532    fn combine_rejects_unparseable() {
533        let bad = vec!["not-an-ms1-string".to_string(), "also-bad".to_string()];
534        assert!(matches!(combine_shares(&bad), Err(Error::Codex32(_))));
535    }
536
537    // --- audit I9: combine must REJECT (not panic on) a non-standard-length
538    // Entr share set. The encode path validates length up front, but codex32
539    // share strings are an open format — an externally-constructed valid-checksum
540    // set with a non-standard payload length must surface a clean error, not abort.
541
542    /// Build a valid-checksum K-of-N Entr share set whose recovered payload has a
543    /// NON-STANDARD entropy length, bypassing `encode_shares`' `secret.validate()`
544    /// guard (which would reject it). Mirrors `encode_shares`' codex32
545    /// construction with a fixed id for determinism.
546    fn nonstandard_entr_distributed(k: usize, n: usize, entropy_len: usize) -> Vec<String> {
547        // wire payload = [RESERVED_PREFIX] || entropy
548        let mut bytes = vec![RESERVED_PREFIX];
549        bytes.extend(std::iter::repeat(0xCDu8).take(entropy_len));
550        let id = "tst7";
551        let secret_s = Codex32String::from_seed(HRP, k, id, Fe::S, &bytes[..]).unwrap();
552        let pool = non_s_index_pool();
553        let mut defining = vec![secret_s];
554        for pidx in pool.iter().take(k - 1) {
555            let filler = vec![0u8; bytes.len()];
556            defining.push(Codex32String::from_seed(HRP, k, id, *pidx, &filler[..]).unwrap());
557        }
558        let mut out = Vec::new();
559        for s in defining.iter().skip(1) {
560            out.push(s.to_string());
561        }
562        for pidx in pool.iter().take(n).skip(k - 1) {
563            out.push(Codex32String::interpolate_at(&defining, *pidx).unwrap().to_string());
564        }
565        out
566    }
567
568    #[test]
569    fn combine_rejects_nonstandard_entr_length_not_panics() {
570        // 17-byte entropy ∉ VALID_ENTR_LENGTHS. Pre-fix `combine_shares` returned
571        // Ok(unvalidated Entr) and `ms combine`'s from_entropy_in panicked
572        // (exit 101). Post-fix: a clean PayloadLengthMismatch, no panic.
573        let shares = nonstandard_entr_distributed(2, 2, 17);
574        let res = combine_shares(&shares);
575        assert!(
576            matches!(res, Err(Error::PayloadLengthMismatch { got: 17, .. })),
577            "expected PayloadLengthMismatch{{got:17}}, got {res:?}"
578        );
579    }
580
581    #[test]
582    fn dispatch_payload_validates_entr_length() {
583        // Unit-level: the Entr arm now validates length (parity with the Mnem arm
584        // and this fn's doc contract). Audit I9.
585        let mut bad = vec![RESERVED_PREFIX];
586        bad.extend(std::iter::repeat(0xCDu8).take(17));
587        assert!(
588            matches!(dispatch_payload(&bad), Err(Error::PayloadLengthMismatch { got: 17, .. })),
589            "non-standard Entr length must Err"
590        );
591        // Positive control: a standard length (16) still decodes Ok — no over-rejection.
592        let mut good = vec![RESERVED_PREFIX];
593        good.extend(std::iter::repeat(0xCDu8).take(16));
594        assert!(
595            matches!(dispatch_payload(&good), Ok(Payload::Entr(_))),
596            "standard Entr length must Ok"
597        );
598    }
599}