patronus-ark 0.1.17

Hybrid Rust security scanners for prompt injection, DLP, PII, and agentic tool risks.
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
// SPDX-License-Identifier: GPL-3.0-only
//! Test-time materialization of secret-safe golden recipes.
//!
//! DLP goldens store secret values as `{"gen": "<generator>", "seed": <u64>}` recipes
//! plus a carrier template with `<<secret:N>>` placeholders. This module mirrors
//! `python/patronus_ark/dlp_synth/recipes.py` on the shared spec
//! `python/patronus_ark/dlp_synth/generators.json`: SplitMix64 seeded with the recipe
//! seed, alphabets concatenated from their chunks, literal segments copied,
//! alphabet segments drawing `alphabet[next_u64() % alphabet.len()]` per
//! character, checksum segments (`crc32_base62`, `iso7064_mod11_10`, `bech32`
//! under the HRP of the last literal) over the text since the last literal
//! (`crc32_hex` over the last literal and that text, `crc32_hex_le` over the text before the
//! last literal as little-endian hex), and encoded segments that build bytes from
//! hex/ASCII/random/alphabet parts and emit them as hex, base64 (padded,
//! optionally wrapped), unpadded base64, base64url, bcrypt radix-64,
//! Base58Check or a BIP 39 mnemonic (`bip39_english`, `bip39_italian`,
//! `bip39_portuguese`, `bip39_czech`, `bip39_spanish`, `bip39_french`, through
//! `patronus_ark_core::detectors::dlp::seed_phrase::mnemonic`); seed word
//! segments draw wallet seed phrases (Electrum, Monero) from the words of one
//! length of a word list. Placeholders may encode (`<<secret:N|base64>>`, `hex`, `url`,
//! `json`) or split (`<<secret:N[a:b]>>`) the value they insert (golden schema
//! v2). A recipe with `fill` yields the documented example/test variant of the
//! generator's shape for hard negatives (T06). A generator with `entropy_above`
//! redraws its whole value from the same PRNG until the Shannon entropy exceeds
//! that limit (fill recipes excepted). The Python module documents the format.
//! Not public API.

use std::{collections::BTreeMap, fmt, path::Path};

use serde::Deserialize;

/// SplitMix64 (Steele, Lea & Flood 2014), the shared golden PRNG.
pub struct SplitMix64 {
    state: u64,
}

impl SplitMix64 {
    pub fn new(seed: u64) -> Self {
        Self { state: seed }
    }

    pub fn next_u64(&mut self) -> u64 {
        self.state = self.state.wrapping_add(0x9E37_79B9_7F4A_7C15);
        let mut z = self.state;
        z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
        z ^ (z >> 31)
    }
}

#[derive(Debug)]
pub enum RecipeError {
    Spec(String),
    UnknownGenerator(String),
    Template(String),
}

impl fmt::Display for RecipeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Spec(message) => write!(f, "invalid generator spec: {message}"),
            Self::UnknownGenerator(name) => write!(f, "unknown generator {name:?}"),
            Self::Template(message) => write!(f, "invalid template: {message}"),
        }
    }
}

impl std::error::Error for RecipeError {}

/// One secret recipe as stored in a golden row.
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SecretRecipe {
    pub gen: String,
    pub seed: u64,
    /// Example/test fill word (`EXAMPLEKEY`, `deadbeef` …) of a hard negative.
    #[serde(default)]
    pub fill: Option<String>,
}

impl SecretRecipe {
    pub fn new(gen: &str, seed: u64) -> Self {
        Self {
            gen: gen.to_owned(),
            seed,
            fill: None,
        }
    }

    /// The example/test variant of `gen` filled with `fill`.
    pub fn filled(gen: &str, seed: u64, fill: &str) -> Self {
        Self {
            fill: Some(fill.to_owned()),
            ..Self::new(gen, seed)
        }
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Literal {
    lit: String,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Random {
    alphabet: String,
    len: usize,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Checksum {
    checksum: String,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Encoded {
    encode: String,
    bytes: Vec<BytePart>,
    #[serde(default)]
    wrap: Option<usize>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct HexPart {
    hex: String,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Utf8Part {
    utf8: String,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RandomBytes {
    random: usize,
}

#[derive(Deserialize)]
#[serde(untagged)]
enum BytePart {
    Hex(HexPart),
    Utf8(Utf8Part),
    RandomBytes(RandomBytes),
    Chars(Random),
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct SeedWords {
    seed_words: String,
    word_len: usize,
}

#[derive(Deserialize)]
#[serde(untagged)]
enum Segment {
    Literal(Literal),
    Random(Random),
    Checksum(Checksum),
    Encoded(Encoded),
    SeedWords(SeedWords),
}

const BIP39_ENGLISH: &str = include_str!("../../ark-core/src/detectors/dlp/bip39/english.txt");
const MONERO_ENGLISH: &str =
    include_str!("../../ark-core/src/detectors/dlp/rules/data/monero_english.txt");

/// A seed word kind: word list, words drawn, words in the value and the
/// Electrum version prefix (none for Monero, which appends a checksum word).
fn seed_kind(kind: &str) -> Option<(&'static str, usize, usize, Option<&'static str>)> {
    match kind {
        "electrum_standard" => Some((BIP39_ENGLISH, 12, 12, Some("01"))),
        "electrum_segwit" => Some((BIP39_ENGLISH, 12, 12, Some("100"))),
        "monero" => Some((MONERO_ENGLISH, 24, 25, None)),
        _ => None,
    }
}

fn seed_word_list(list: &'static str, word_len: usize) -> Vec<&'static str> {
    list.lines().filter(|word| word.len() == word_len).collect()
}

/// Draw a seed phrase of `kind` from the words of `word_len` letters: 12
/// BIP39 words until the HMAC-SHA512 under `Seed version` starts with the
/// Electrum prefix, or 24 Monero words and the word at CRC-32 of their
/// 3-letter prefixes modulo 24.
fn seed_words(kind: &str, word_len: usize, rng: &mut SplitMix64) -> String {
    let (list, drawn_count, _, prefix) = seed_kind(kind).expect("validated seed kind");
    let words = seed_word_list(list, word_len);
    let key = ring::hmac::Key::new(ring::hmac::HMAC_SHA512, b"Seed version");
    loop {
        let mut drawn: Vec<&str> = (0..drawn_count)
            .map(|_| words[(rng.next_u64() % words.len() as u64) as usize])
            .collect();
        let Some(prefix) = prefix else {
            let prefixes: String = drawn.iter().map(|word| &word[..3]).collect();
            drawn.push(drawn[crc32(prefixes.as_bytes()) as usize % 24]);
            return drawn.join(" ");
        };
        let phrase = drawn.join(" ");
        let tag: String = ring::hmac::sign(&key, phrase.as_bytes())
            .as_ref()
            .iter()
            .map(|byte| format!("{byte:02x}"))
            .collect();
        if tag.starts_with(prefix) {
            return phrase;
        }
    }
}

#[derive(Deserialize)]
struct Generator {
    segments: Vec<Segment>,
    entropy_above: Option<f64>,
}

#[derive(Deserialize)]
struct Spec {
    version: u32,
    prng: String,
    alphabets: BTreeMap<String, Vec<String>>,
    generators: BTreeMap<String, Generator>,
}

const BASE62: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const BASE58: &[u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const BASE64: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BASE64URL: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
const BCRYPT64: &[u8] = b"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const ENCODINGS: &[&str] = &[
    "hex",
    "base64",
    "base64_unpadded",
    "base64url",
    "bcrypt64",
    "base58check",
    "bip39_english",
    "bip39_italian",
    "bip39_portuguese",
    "bip39_czech",
    "bip39_spanish",
    "bip39_french",
];

/// Entropy sizes in bytes that BIP 39 encodes (12 to 24 words).
const BIP39_ENTROPY_BYTES: [usize; 5] = [16, 20, 24, 28, 32];

/// The word list language of a `bip39_<language>` encoding.
fn bip39_language(encoding: &str) -> Option<&str> {
    encoding.strip_prefix("bip39_")
}

/// Output alphabet of an encoding that a fill word may repeat over (T06).
fn encoded_alphabet(encoding: &str) -> Option<&'static [u8]> {
    match encoding {
        "hex" => Some(b"0123456789abcdef"),
        "base64" | "base64_unpadded" => Some(BASE64),
        "base64url" => Some(BASE64URL),
        "bcrypt64" => Some(BCRYPT64),
        _ => None,
    }
}

/// `word` repeated to `length` characters.
fn repeat_word(word: &str, length: usize) -> String {
    word.chars().cycle().take(length).collect()
}

/// Shannon entropy in bits per character, summed in code point order like
/// `recipes.shannon_entropy`.
fn shannon_entropy(value: &str) -> f64 {
    let mut counts = BTreeMap::<char, usize>::new();
    for character in value.chars() {
        *counts.entry(character).or_default() += 1;
    }
    let length = value.chars().count() as f64;
    -counts
        .values()
        .map(|&count| count as f64 / length * (count as f64 / length).log2())
        .sum::<f64>()
}

fn checksum_length(kind: &str) -> Option<usize> {
    match kind {
        "crc32_base62" => Some(6),
        "crc32_hex" => Some(8),
        "crc32_hex_le" => Some(8),
        "iso7064_mod11_10" => Some(1),
        "bech32" => Some(6),
        _ => None,
    }
}

fn decode_hex(hex: &str) -> Option<Vec<u8>> {
    if hex.is_empty() || !hex.len().is_multiple_of(2) || !hex.is_ascii() {
        return None;
    }
    (0..hex.len())
        .step_by(2)
        .map(|index| u8::from_str_radix(&hex[index..index + 2], 16).ok())
        .collect()
}

/// CRC-32/ISO-HDLC (zlib `crc32`).
fn crc32(data: &[u8]) -> u32 {
    let mut crc = !0u32;
    for byte in data {
        crc ^= u32::from(*byte);
        for _ in 0..8 {
            crc = if crc & 1 == 1 {
                (crc >> 1) ^ 0xEDB8_8320
            } else {
                crc >> 1
            };
        }
    }
    !crc
}

fn crc32_base62(text: &str) -> String {
    let mut checksum = crc32(text.as_bytes());
    let mut digits = [b'0'; 6];
    for digit in digits.iter_mut().rev() {
        *digit = BASE62[(checksum % 62) as usize];
        checksum /= 62;
    }
    String::from_utf8(digits.to_vec()).expect("base62 is ASCII")
}

fn iso7064_mod11_10(digits: &str) -> Result<String, RecipeError> {
    let mut product = 10;
    for character in digits.chars() {
        let digit = character
            .to_digit(10)
            .ok_or_else(|| RecipeError::Spec(format!("iso7064 input {digits:?} is not numeric")))?;
        let total = match (digit + product) % 10 {
            0 => 10,
            total => total,
        };
        product = 2 * total % 11;
    }
    Ok(((11 - product) % 10).to_string())
}

const BECH32: &[u8] = b"qpzry9x8gf2tvdw0s3jn54khce6mua7l";

/// BIP 173 checksum of `data` (Bech32 characters) under the HRP that `literal`
/// ends with (`AGE-SECRET-KEY-1`, `age1`); upper case if `literal` is.
fn bech32_checksum(literal: &str, data: &str) -> Result<String, RecipeError> {
    let hrp = literal
        .strip_suffix('1')
        .filter(|hrp| !hrp.is_empty())
        .ok_or_else(|| {
            RecipeError::Spec(format!(
                "a bech32 checksum needs a preceding literal ending in HRP + '1', got {literal:?}"
            ))
        })?
        .to_ascii_lowercase();
    let mut values: Vec<u32> = hrp.bytes().map(|byte| u32::from(byte >> 5)).collect();
    values.push(0);
    values.extend(hrp.bytes().map(|byte| u32::from(byte & 31)));
    for character in data.bytes() {
        let index = BECH32
            .iter()
            .position(|&symbol| symbol == character.to_ascii_lowercase())
            .ok_or_else(|| {
                RecipeError::Spec(format!("bech32 data {data:?} leaves the Bech32 alphabet"))
            })?;
        values.push(index as u32);
    }
    values.extend([0; 6]);
    const GENERATOR: [u32; 5] = [0x3B6A57B2, 0x26508E6D, 0x1EA119FA, 0x3D4233DD, 0x2A1462B3];
    let mut check: u32 = 1;
    for value in values {
        let top = check >> 25;
        check = ((check & 0x1FF_FFFF) << 5) ^ value;
        for (bit, generator) in GENERATOR.iter().enumerate() {
            if (top >> bit) & 1 == 1 {
                check ^= generator;
            }
        }
    }
    check ^= 1;
    let upper = literal.bytes().any(|byte| byte.is_ascii_uppercase())
        && !literal.bytes().any(|byte| byte.is_ascii_lowercase());
    Ok((0..6)
        .map(|index| {
            let symbol = BECH32[((check >> (5 * (5 - index))) & 31) as usize];
            char::from(if upper {
                symbol.to_ascii_uppercase()
            } else {
                symbol
            })
        })
        .collect())
}

fn base58check(payload: &[u8]) -> String {
    let checksum = ring::digest::digest(
        &ring::digest::SHA256,
        ring::digest::digest(&ring::digest::SHA256, payload).as_ref(),
    );
    let mut raw = payload.to_vec();
    raw.extend_from_slice(&checksum.as_ref()[..4]);
    let leading_zeros = raw.iter().take_while(|byte| **byte == 0).count();
    // Big-endian base-58 digits, built by repeated multiply-add.
    let mut digits: Vec<u8> = Vec::new();
    for byte in &raw {
        let mut carry = u32::from(*byte);
        for digit in digits.iter_mut() {
            carry += u32::from(*digit) << 8;
            *digit = (carry % 58) as u8;
            carry /= 58;
        }
        while carry > 0 {
            digits.push((carry % 58) as u8);
            carry /= 58;
        }
    }
    std::iter::repeat_n(b'1', leading_zeros)
        .chain(digits.iter().rev().map(|digit| BASE58[*digit as usize]))
        .map(char::from)
        .collect()
}

fn radix64(raw: &[u8], alphabet: &[u8], pad: bool) -> String {
    let mut encoded = String::with_capacity(raw.len().div_ceil(3) * 4);
    for chunk in raw.chunks(3) {
        let bits = (u32::from(chunk[0]) << 16)
            | (u32::from(*chunk.get(1).unwrap_or(&0)) << 8)
            | u32::from(*chunk.get(2).unwrap_or(&0));
        for index in 0..chunk.len() + 1 {
            encoded.push(char::from(
                alphabet[((bits >> (18 - 6 * index)) & 63) as usize],
            ));
        }
        if pad {
            for _ in chunk.len()..3 {
                encoded.push('=');
            }
        }
    }
    encoded
}

fn wrap_lines(encoded: &str, width: usize) -> String {
    encoded
        .as_bytes()
        .chunks(width)
        .map(|line| std::str::from_utf8(line).expect("base64 is ASCII"))
        .collect::<Vec<_>>()
        .join("\n")
}

impl Encoded {
    fn byte_length(&self) -> usize {
        self.bytes
            .iter()
            .map(|part| match part {
                BytePart::Hex(HexPart { hex }) => hex.len() / 2,
                BytePart::Utf8(Utf8Part { utf8 }) => utf8.len(),
                BytePart::RandomBytes(RandomBytes { random }) => *random,
                BytePart::Chars(Random { len, .. }) => *len,
            })
            .sum()
    }

    /// Encoded length in characters, `None` for a mnemonic (its words differ in length).
    fn length(&self) -> Option<usize> {
        if bip39_language(&self.encode).is_some() {
            return None;
        }
        let size = self.byte_length();
        Some(match self.encode.as_str() {
            "hex" => 2 * size,
            "base64" => {
                let length = size.div_ceil(3) * 4;
                self.wrap
                    .map_or(length, |width| length + length.saturating_sub(1) / width)
            }
            // Constant leading bytes (up to 8 of the first hex part) keep the
            // value in one base58 digit range: below (lead + 1) * 256^rest.
            "base58check" => {
                let lead = match self.bytes.first() {
                    Some(BytePart::Hex(HexPart { hex })) => decode_hex(hex)
                        .unwrap_or_default()
                        .into_iter()
                        .take(8)
                        .collect(),
                    _ => Vec::new(),
                };
                let value = lead
                    .iter()
                    .fold(0_u64, |value, byte| (value << 8) | u64::from(*byte));
                let rest = size + 4 - lead.len();
                (((value as f64 + 1.0).log2() + 8.0 * rest as f64) / 58f64.log2()).ceil() as usize
            }
            _ => (size * 4).div_ceil(3),
        })
    }
}

/// Parsed `generators.json`.
pub struct GeneratorSet {
    alphabets: BTreeMap<String, Vec<char>>,
    generators: BTreeMap<String, Generator>,
}

impl GeneratorSet {
    pub fn load(path: impl AsRef<Path>) -> Result<Self, RecipeError> {
        let raw = std::fs::read_to_string(path.as_ref())
            .map_err(|error| RecipeError::Spec(error.to_string()))?;
        Self::parse(&raw)
    }

    pub fn parse(raw: &str) -> Result<Self, RecipeError> {
        let spec: Spec =
            serde_json::from_str(raw).map_err(|error| RecipeError::Spec(error.to_string()))?;
        if spec.version != 2 || spec.prng != "splitmix64" {
            return Err(RecipeError::Spec("unsupported version or prng".into()));
        }
        let mut alphabets = BTreeMap::new();
        for (name, chunks) in spec.alphabets {
            let alphabet = chunks.concat();
            let chars = alphabet.chars().collect::<Vec<_>>();
            let unique = chars.iter().collect::<std::collections::BTreeSet<_>>();
            if chars.is_empty() || !alphabet.is_ascii() || unique.len() != chars.len() {
                return Err(RecipeError::Spec(format!(
                    "alphabet {name} must be non-empty, ASCII and duplicate-free"
                )));
            }
            alphabets.insert(name, chars);
        }
        for (name, generator) in &spec.generators {
            for segment in &generator.segments {
                let valid = match segment {
                    Segment::Literal(Literal { lit }) => !lit.is_empty() && lit.is_ascii(),
                    Segment::Random(Random { alphabet, len }) => {
                        *len > 0 && alphabets.contains_key(alphabet)
                    }
                    Segment::Checksum(Checksum { checksum }) => checksum_length(checksum).is_some(),
                    Segment::Encoded(encoded) => Self::valid_encoded(encoded, &alphabets),
                    Segment::SeedWords(SeedWords {
                        seed_words,
                        word_len,
                    }) => seed_kind(seed_words)
                        .is_some_and(|(list, ..)| !seed_word_list(list, *word_len).is_empty()),
                };
                if !valid {
                    return Err(RecipeError::Spec(format!("{name}: invalid segment")));
                }
            }
            if let Some(limit) = generator.entropy_above {
                let length = Self::segments_length(&generator.segments);
                if !length.is_some_and(|length| {
                    limit > 0.0 && limit < (length as f64).log2() && Self::draws_randomly(generator)
                }) {
                    return Err(RecipeError::Spec(format!(
                        "{name}: invalid entropy_above {limit}"
                    )));
                }
            }
        }
        Ok(Self {
            alphabets,
            generators: spec.generators,
        })
    }

    fn valid_encoded(encoded: &Encoded, alphabets: &BTreeMap<String, Vec<char>>) -> bool {
        let parts_valid = !encoded.bytes.is_empty()
            && encoded.bytes.iter().all(|part| match part {
                BytePart::Hex(HexPart { hex }) => decode_hex(hex).is_some(),
                BytePart::Utf8(Utf8Part { utf8 }) => !utf8.is_empty() && utf8.is_ascii(),
                BytePart::RandomBytes(RandomBytes { random }) => *random > 0,
                BytePart::Chars(Random { alphabet, len }) => {
                    *len > 0 && alphabets.contains_key(alphabet)
                }
            });
        // Base58Check needs a constant non-zero lead byte for a fixed length.
        let lead_valid = encoded.encode != "base58check"
            || matches!(
                encoded.bytes.first(),
                Some(BytePart::Hex(HexPart { hex })) if decode_hex(hex).is_some_and(|raw| raw[0] != 0)
            );
        let wrap_valid = encoded
            .wrap
            .is_none_or(|width| width > 0 && encoded.encode == "base64");
        let entropy_valid = bip39_language(&encoded.encode).is_none()
            || BIP39_ENTROPY_BYTES.contains(&encoded.byte_length());
        parts_valid
            && lead_valid
            && wrap_valid
            && entropy_valid
            && ENCODINGS.contains(&encoded.encode.as_str())
    }

    pub fn names(&self) -> Vec<&str> {
        self.generators.keys().map(String::as_str).collect()
    }

    fn generator(&self, name: &str) -> Result<&Generator, RecipeError> {
        self.generators
            .get(name)
            .ok_or_else(|| RecipeError::UnknownGenerator(name.to_owned()))
    }

    /// Fixed output length in characters, `None` for a generator with a BIP 39 mnemonic.
    pub fn length(&self, name: &str) -> Result<Option<usize>, RecipeError> {
        Ok(Self::segments_length(&self.generator(name)?.segments))
    }

    fn draws_randomly(generator: &Generator) -> bool {
        generator.segments.iter().any(|segment| match segment {
            Segment::Random(_) | Segment::SeedWords(_) => true,
            Segment::Encoded(encoded) => encoded
                .bytes
                .iter()
                .any(|part| matches!(part, BytePart::RandomBytes(_) | BytePart::Chars(_))),
            Segment::Literal(_) | Segment::Checksum(_) => false,
        })
    }

    fn segments_length(segments: &[Segment]) -> Option<usize> {
        segments
            .iter()
            .map(|segment| match segment {
                Segment::Literal(Literal { lit }) => Some(lit.chars().count()),
                Segment::Random(Random { len, .. }) => Some(*len),
                Segment::Checksum(Checksum { checksum }) => {
                    Some(checksum_length(checksum).expect("validated checksum kind"))
                }
                Segment::Encoded(encoded) => encoded.length(),
                Segment::SeedWords(SeedWords {
                    seed_words,
                    word_len,
                }) => {
                    let count = seed_kind(seed_words).expect("validated seed kind").2;
                    Some(count * word_len + count - 1)
                }
            })
            .sum()
    }

    fn draw(&self, rng: &mut SplitMix64, alphabet: &str, len: usize, out: &mut String) {
        let alphabet = &self.alphabets[alphabet];
        for _ in 0..len {
            out.push(alphabet[(rng.next_u64() % alphabet.len() as u64) as usize]);
        }
    }

    fn encode(&self, encoded: &Encoded, rng: &mut SplitMix64) -> String {
        let mut raw = Vec::with_capacity(encoded.byte_length());
        for part in &encoded.bytes {
            match part {
                BytePart::Hex(HexPart { hex }) => {
                    raw.extend(decode_hex(hex).expect("validated hex part"));
                }
                BytePart::Utf8(Utf8Part { utf8 }) => raw.extend_from_slice(utf8.as_bytes()),
                BytePart::RandomBytes(RandomBytes { random }) => {
                    raw.extend((0..*random).map(|_| (rng.next_u64() % 256) as u8));
                }
                BytePart::Chars(Random { alphabet, len }) => {
                    let mut chars = String::new();
                    self.draw(rng, alphabet, *len, &mut chars);
                    raw.extend_from_slice(chars.as_bytes());
                }
            }
        }
        Self::encode_raw(encoded, &raw)
    }

    fn encode_raw(encoded: &Encoded, raw: &[u8]) -> String {
        if let Some(language) = bip39_language(&encoded.encode) {
            return patronus_ark_core::detectors::dlp::seed_phrase::mnemonic(language, raw)
                .expect("validated BIP 39 language and entropy size");
        }
        match encoded.encode.as_str() {
            "hex" => raw.iter().map(|byte| format!("{byte:02x}")).collect(),
            "base58check" => base58check(raw),
            "base64url" => radix64(raw, BASE64URL, false),
            "base64_unpadded" => radix64(raw, BASE64, false),
            "bcrypt64" => radix64(raw, BCRYPT64, false),
            _ => {
                let base64 = radix64(raw, BASE64, true);
                match encoded.wrap {
                    Some(width) => wrap_lines(&base64, width),
                    None => base64,
                }
            }
        }
    }

    fn fits(&self, word: &str, alphabet: &str) -> bool {
        let alphabet = &self.alphabets[alphabet];
        word.chars().all(|character| alphabet.contains(&character))
    }

    /// An encoded segment of a fill recipe and whether the fill word went into it.
    fn encode_filled(
        &self,
        encoded: &Encoded,
        rng: &mut SplitMix64,
        fill: &str,
    ) -> Result<(String, bool), RecipeError> {
        if encoded.encode == "base58check" || bip39_language(&encoded.encode).is_some() {
            return Err(RecipeError::Template(format!(
                "a fill recipe cannot use {} segments (the check value stays valid)",
                encoded.encode
            )));
        }
        let all_random = encoded
            .bytes
            .iter()
            .all(|part| matches!(part, BytePart::RandomBytes(_)));
        let fits_output = encoded_alphabet(&encoded.encode)
            .is_some_and(|alphabet| fill.bytes().all(|byte| alphabet.contains(&byte)));
        if all_random && fits_output {
            let shape = Self::encode_raw(encoded, &vec![0; encoded.byte_length()]);
            let mut characters = fill.chars().cycle();
            let value = shape
                .chars()
                .map(|character| match character {
                    '=' | '\n' => character,
                    _ => characters.next().expect("cycle of a non-empty word"),
                })
                .collect();
            return Ok((value, true));
        }
        let mut raw = Vec::with_capacity(encoded.byte_length());
        let mut filled = false;
        for part in &encoded.bytes {
            match part {
                BytePart::Hex(HexPart { hex }) => {
                    raw.extend(decode_hex(hex).expect("validated hex part"));
                }
                BytePart::Utf8(Utf8Part { utf8 }) => raw.extend_from_slice(utf8.as_bytes()),
                BytePart::RandomBytes(RandomBytes { random }) => {
                    raw.extend_from_slice(repeat_word(fill, *random).as_bytes());
                    filled = true;
                }
                BytePart::Chars(Random { alphabet, len }) if self.fits(fill, alphabet) => {
                    raw.extend_from_slice(repeat_word(fill, *len).as_bytes());
                    filled = true;
                }
                BytePart::Chars(Random { alphabet, len }) => {
                    let mut chars = String::new();
                    self.draw(rng, alphabet, *len, &mut chars);
                    raw.extend_from_slice(chars.as_bytes());
                }
            }
        }
        Ok((Self::encode_raw(encoded, &raw), filled))
    }

    pub fn generate(&self, name: &str, seed: u64) -> Result<String, RecipeError> {
        self.generate_filled(name, seed, None)
    }

    /// Generate `name`, or with `fill` its documented example/test variant:
    /// literals kept, alphabet segments and parts that hold every character of
    /// `fill` repeat it, the others are drawn, check values are zeros (ones if
    /// zeros are valid) and random-only encoded segments repeat `fill` over
    /// their output alphabet (padding and line breaks kept) or encode its bytes.
    pub fn generate_filled(
        &self,
        name: &str,
        seed: u64,
        fill: Option<&str>,
    ) -> Result<String, RecipeError> {
        if let Some(word) = fill {
            if word.is_empty() || !word.bytes().all(|byte| byte.is_ascii_alphanumeric()) {
                return Err(RecipeError::Template(format!(
                    "fill must be a non-empty ASCII alphanumeric word, got {word:?}"
                )));
            }
        }
        let generator = self.generator(name)?;
        let mut rng = SplitMix64::new(seed);
        let mut value = self.draw_value(name, generator, &mut rng, fill)?;
        // Fill recipes are low-entropy hard negatives by design and are never redrawn.
        if let Some(limit) = generator.entropy_above.filter(|_| fill.is_none()) {
            while shannon_entropy(&value) <= limit {
                value = self.draw_value(name, generator, &mut rng, fill)?;
            }
        }
        Ok(value)
    }

    fn draw_value(
        &self,
        name: &str,
        generator: &Generator,
        rng: &mut SplitMix64,
        fill: Option<&str>,
    ) -> Result<String, RecipeError> {
        let mut value = String::new();
        let mut filled = false;
        // Checksums cover the text emitted since the most recent literal
        // (`crc32_hex_le`: the text before it).
        let mut since_literal = String::new();
        let mut literal = "";
        let mut before_literal = 0;
        for segment in &generator.segments {
            let mut part = String::new();
            match segment {
                Segment::Literal(Literal { lit }) => {
                    before_literal = value.len();
                    value.push_str(lit);
                    since_literal.clear();
                    literal = lit;
                    continue;
                }
                Segment::Random(Random { alphabet, len }) => match fill {
                    Some(word) if self.fits(word, alphabet) => {
                        part = repeat_word(word, *len);
                        filled = true;
                    }
                    _ => self.draw(rng, alphabet, *len, &mut part),
                },
                Segment::Checksum(Checksum { checksum }) => {
                    match checksum.as_str() {
                        "crc32_base62" => part = crc32_base62(&since_literal),
                        "crc32_hex" => {
                            part = format!(
                                "{:08x}",
                                crc32(format!("{literal}{since_literal}").as_bytes())
                            )
                        }
                        "crc32_hex_le" => {
                            part = crc32(&value.as_bytes()[..before_literal])
                                .to_le_bytes()
                                .iter()
                                .map(|byte| format!("{byte:02x}"))
                                .collect()
                        }
                        "bech32" => part = bech32_checksum(literal, &since_literal)?,
                        _ => part = iso7064_mod11_10(&since_literal)?,
                    }
                    if fill.is_some() {
                        let zeros = "0".repeat(part.len());
                        part = if zeros == part {
                            "1".repeat(part.len())
                        } else {
                            zeros
                        };
                    }
                }
                Segment::SeedWords(SeedWords {
                    seed_words: kind,
                    word_len,
                }) => part = seed_words(kind, *word_len, rng),
                Segment::Encoded(encoded) => match fill {
                    Some(word) => {
                        let (encoded, filled_segment) = self.encode_filled(encoded, rng, word)?;
                        part = encoded;
                        filled |= filled_segment;
                    }
                    None => part = self.encode(encoded, rng),
                },
            }
            value.push_str(&part);
            since_literal.push_str(&part);
        }
        if let Some(word) = fill.filter(|_| !filled) {
            return Err(RecipeError::Template(format!(
                "fill {word:?} fits no segment of {name}"
            )));
        }
        Ok(value)
    }
}

const PLACEHOLDER_OPEN: &str = "<<secret:";
const PLACEHOLDER_CLOSE: &str = ">>";

/// One inserted placeholder: code points `start..end` of the text hold part
/// `part` of secret `secret` (0 unless the secret is split), encoded with
/// `encoding` if the placeholder names one.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Fragment {
    pub secret: usize,
    pub part: usize,
    pub start: usize,
    pub end: usize,
    pub encoding: Option<String>,
}

enum Placement {
    Whole(Option<String>),
    Slice(usize, usize),
}

/// Canonical non-negative decimal (no sign, no leading zeros).
fn parse_index(digits: &str) -> Option<usize> {
    let canonical = !digits.is_empty()
        && digits.bytes().all(|byte| byte.is_ascii_digit())
        && (digits == "0" || !digits.starts_with('0'));
    digits.parse().ok().filter(|_| canonical)
}

fn parse_placeholder(body: &str) -> Option<(usize, Placement)> {
    if let Some((index, encoding)) = body.split_once('|') {
        let valid = !encoding.is_empty()
            && encoding
                .bytes()
                .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit());
        if !valid {
            return None;
        }
        return Some((parse_index(index)?, Placement::Whole(Some(encoding.into()))));
    }
    if let Some((index, range)) = body.split_once('[') {
        let (start, end) = range.strip_suffix(']')?.split_once(':')?;
        let placement = Placement::Slice(parse_index(start)?, parse_index(end)?);
        return Some((parse_index(index)?, placement));
    }
    Some((parse_index(body)?, Placement::Whole(None)))
}

/// Encode `value` like a `<<secret:N|encoding>>` placeholder.
pub fn transform(value: &str, encoding: &str) -> Result<String, RecipeError> {
    let raw = value.as_bytes();
    Ok(match encoding {
        "base64" => radix64(raw, BASE64, true),
        "hex" => raw.iter().map(|byte| format!("{byte:02x}")).collect(),
        "url" => raw
            .iter()
            .map(|&byte| match byte {
                b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
                    char::from(byte).to_string()
                }
                _ => format!("%{byte:02X}"),
            })
            .collect(),
        "json" => value
            .chars()
            .map(|character| match character {
                '"' => "\\\"".into(),
                '\\' => "\\\\".into(),
                '\n' => "\\n".into(),
                '\r' => "\\r".into(),
                '\t' => "\\t".into(),
                '\u{8}' => "\\b".into(),
                '\u{c}' => "\\f".into(),
                control if u32::from(control) < 0x20 => {
                    format!("\\u{:04x}", u32::from(control))
                }
                other => other.to_string(),
            })
            .collect(),
        other => {
            return Err(RecipeError::Template(format!(
                "unknown placeholder encoding {other:?}"
            )))
        }
    })
}

/// Materialize `template`, returning the text and every inserted fragment in
/// text order, with half-open Unicode-code-point offsets.
pub fn materialize_fragments(
    generators: &GeneratorSet,
    template: &str,
    secrets: &[SecretRecipe],
) -> Result<(String, Vec<Fragment>), RecipeError> {
    let bad = |message: String| RecipeError::Template(format!("{message} in {template:?}"));
    let mut pieces = Vec::new();
    let mut rest = template;
    while let Some(open) = rest.find(PLACEHOLDER_OPEN) {
        let (before, after) = rest.split_at(open);
        let after = &after[PLACEHOLDER_OPEN.len()..];
        let close = after
            .find(PLACEHOLDER_CLOSE)
            .ok_or_else(|| bad("unterminated placeholder".into()))?;
        let (index, placement) = parse_placeholder(&after[..close])
            .filter(|(index, _)| *index < secrets.len())
            .ok_or_else(|| bad(format!("bad placeholder {:?}", &after[..close])))?;
        pieces.push((before, index, placement));
        rest = &after[close + PLACEHOLDER_CLOSE.len()..];
    }
    if pieces.is_empty() {
        return Err(bad("no placeholder".into()));
    }
    let mut values = Vec::with_capacity(secrets.len());
    for (index, recipe) in secrets.iter().enumerate() {
        let value = generators.generate_filled(&recipe.gen, recipe.seed, recipe.fill.as_deref())?;
        let uses = pieces
            .iter()
            .filter(|(_, used, _)| *used == index)
            .map(|(_, _, placement)| placement)
            .collect::<Vec<_>>();
        let slices = uses
            .iter()
            .filter_map(|placement| match placement {
                Placement::Slice(start, end) => Some((*start, *end)),
                Placement::Whole(_) => None,
            })
            .collect::<Vec<_>>();
        let valid = if slices.is_empty() {
            uses.len() == 1
        } else {
            let mut cursor = 0;
            let ordered = slices.iter().all(|&(start, end)| {
                let next = start == cursor && start < end;
                cursor = end;
                next
            });
            slices.len() == uses.len()
                && slices.len() >= 2
                && ordered
                && cursor == value.chars().count()
        };
        if !valid {
            return Err(bad(format!(
                "secret {index} must be used once or split in order"
            )));
        }
        values.push(value);
    }
    let mut text = String::with_capacity(template.len());
    let mut fragments = Vec::with_capacity(pieces.len());
    let mut parts = vec![0; secrets.len()];
    let mut chars = 0;
    for (before, index, placement) in pieces {
        text.push_str(before);
        chars += before.chars().count();
        let value = &values[index];
        let (inserted, encoding) = match placement {
            Placement::Slice(start, end) => (
                value
                    .chars()
                    .skip(start)
                    .take(end - start)
                    .collect::<String>(),
                None,
            ),
            Placement::Whole(None) => (value.clone(), None),
            Placement::Whole(Some(encoding)) => {
                let encoded = transform(value, &encoding)?;
                if &encoded == value {
                    return Err(bad(format!(
                        "encoding {encoding:?} leaves secret {index} unchanged"
                    )));
                }
                (encoded, Some(encoding))
            }
        };
        let length = inserted.chars().count();
        fragments.push(Fragment {
            secret: index,
            part: parts[index],
            start: chars,
            end: chars + length,
            encoding,
        });
        parts[index] += 1;
        text.push_str(&inserted);
        chars += length;
    }
    text.push_str(rest);
    Ok((text, fragments))
}

/// Materialize `template`, returning the text and each secret's half-open
/// Unicode-code-point span, indexed like `secrets`. Split secrets have no single
/// span; use [`materialize_fragments`] for them.
pub fn materialize(
    generators: &GeneratorSet,
    template: &str,
    secrets: &[SecretRecipe],
) -> Result<(String, Vec<(usize, usize)>), RecipeError> {
    let (text, fragments) = materialize_fragments(generators, template, secrets)?;
    if fragments.iter().any(|fragment| fragment.part > 0) {
        return Err(RecipeError::Template(format!(
            "template splits a secret, use materialize_fragments: {template:?}"
        )));
    }
    let mut spans = vec![(0, 0); secrets.len()];
    for fragment in fragments {
        spans[fragment.secret] = (fragment.start, fragment.end);
    }
    Ok((text, spans))
}

#[cfg(test)]
mod tests {
    use super::{bech32_checksum, GeneratorSet};

    fn spec(generator: &str) -> String {
        format!(
            r#"{{"version": 2, "prng": "splitmix64", "alphabets": {{"hex": ["0123456789abcdef"]}}, "generators": {{"g": {generator}}}}}"#
        )
    }

    #[test]
    fn entropy_limit_must_be_reachable() {
        // Four characters hold at most 2 bits each.
        let random = r#"[{"alphabet": "hex", "len": 4}]"#;
        for limit in ["0", "-1.0", "2.0", "2.5"] {
            let generator = format!(r#"{{"segments": {random}, "entropy_above": {limit}}}"#);
            assert!(GeneratorSet::parse(&spec(&generator)).is_err(), "{limit}");
        }
        let literal = r#"{"segments": [{"lit": "abcd"}], "entropy_above": 1.0}"#;
        assert!(GeneratorSet::parse(&spec(literal)).is_err());
        let generator = format!(r#"{{"segments": {random}, "entropy_above": 1.5}}"#);
        let generators = GeneratorSet::parse(&spec(&generator)).unwrap();
        for seed in 0..256 {
            let value = generators.generate("g", seed).unwrap();
            assert!(super::shannon_entropy(&value) > 1.5, "seed={seed}");
        }
    }

    #[test]
    fn bech32_checksum_matches_the_bip173_vectors() {
        // BIP 173 valid strings: "A12UEL5L" and "abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw".
        assert_eq!(bech32_checksum("A1", "").unwrap(), "2UEL5L");
        assert_eq!(
            bech32_checksum("abcdef1", "qpzry9x8gf2tvdw0s3jn54khce6mua7l").unwrap(),
            "mqqqxw"
        );
        assert!(bech32_checksum("age", "qq").is_err());
        assert!(bech32_checksum("age1", "qb").is_err());
    }
}