keyhog-scanner 0.5.73

keyhog-scanner: high-performance SIMD-accelerated secret detection engine
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
//! Canonical fixed-point feature and model ABI shared by CPU and accelerator routes.

use sha2::{Digest, Sha256};
use std::sync::LazyLock;

use crate::ml_scorer::model_arch::{
    EXPERTS_OFF, EXPERT_COUNT, EXPERT_FC1_B_COUNT, EXPERT_FC1_OUT, EXPERT_FC1_W_COUNT,
    EXPERT_FC2_B_COUNT, EXPERT_FC2_OUT, EXPERT_FC2_W_COUNT, EXPERT_FC3_B_COUNT, EXPERT_FC3_W_COUNT,
    EXPERT_PARAM_COUNT, GATE_B_COUNT, GATE_B_OFF, GATE_W_COUNT, GATE_W_OFF, INPUT_DIM,
    TOTAL_F32_COUNT,
};

pub const FEATURE_SCHEMA_VERSION: u16 = 1;
pub const QUANTIZED_MODEL_FORMAT_VERSION: u16 = 1;
pub const QUANTIZED_SCORE_ABI_VERSION: u16 = 1;
pub const FRACTIONAL_BITS: u8 = 7;
pub const SCALE: i32 = 1 << FRACTIONAL_BITS;
pub const MAX_CANDIDATES_PER_BATCH: usize = 1 << 16;
pub const MODEL_BYTES: &[u8] = include_bytes!("../quantized_moe.bin");

const MAGIC: &[u8; 8] = b"KHQMOE\0\x01";
const HEADER_LEN: usize = 60;
const ROUND_TIES_AWAY_FROM_ZERO: u8 = 1;
const MAX_ACTIVATION: i32 = i16::MAX as i32;
const SIGMOID_SATURATION: i32 = 6 * SCALE;
const GATE_DECAY: i32 = 8 * SCALE;

/// Registry order is the serialized feature order. Tests enumerate this value,
/// so adding a model input requires an explicit ABI name rather than a silent width bump.
pub const FEATURE_NAMES: [&str; INPUT_DIM] = [
    "normalized_length",
    "length_at_least_20",
    "length_at_least_40",
    "length_at_least_100",
    "normalized_entropy",
    "entropy_at_least_low",
    "entropy_at_least_high",
    "entropy_at_least_very_high",
    "has_upper",
    "has_lower",
    "has_digit",
    "has_symbol",
    "has_known_prefix",
    "normalized_prefix_length",
    "openai_prefix",
    "aws_access_key_prefix",
    "has_assignment",
    "has_secret_keyword",
    "has_test_keyword",
    "comment_context",
    "has_placeholder_keyword",
    "low_byte_variety",
    "hex_placeholder",
    "has_url_scheme",
    "normalized_unique_bytes",
    "unique_bigram_ratio",
    "normalized_dot_count",
    "normalized_dash_count",
    "reserved_28",
    "reserved_29",
    "reserved_30",
    "reserved_31",
    "file_type_config",
    "file_type_source",
    "file_type_ci",
    "file_type_infra",
    "file_type_other",
    "file_type_binary",
    "comment_context_extra",
    "assignment_extra",
    "test_file_context",
    "decoded_binary_structure",
    "specific_service_context",
    "active_service_context",
    "generic_detector",
    "weak_anchor",
    "live_verifier",
    "required_companion",
    "structural_password_slot",
    "phase2_generic",
    "entropy_channel",
    "entropy_generic",
    "entropy_password",
    "entropy_token",
    "entropy_api_key",
];

pub const EXPERT_IDS: [u8; EXPERT_COUNT] = [0, 1, 2, 3, 4, 5];

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum CandidateScoreOwnership {
    Accelerated,
    Cpu,
}

/// Classify bytes before feature extraction. Invalid UTF-8, empty candidates,
/// and values outside the scanner's bounded chunk ABI never enter a GPU buffer.
pub(crate) fn candidate_score_ownership(bytes: &[u8]) -> CandidateScoreOwnership {
    if bytes.is_empty()
        || bytes.len() > crate::types::MAX_SCAN_CHUNK_BYTES
        || std::str::from_utf8(bytes).is_err()
    {
        CandidateScoreOwnership::Cpu
    } else {
        CandidateScoreOwnership::Accelerated
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QuantizedFeatureRow(pub [i16; INPUT_DIM]);

impl QuantizedFeatureRow {
    pub fn from_float(features: &[f32; INPUT_DIM]) -> Result<Self, QuantizedConfidenceError> {
        let mut row = [0i16; INPUT_DIM];
        for (index, (&value, output)) in features.iter().zip(&mut row).enumerate() {
            *output =
                quantize_f32(value).ok_or(QuantizedConfidenceError::InvalidFeature { index })?;
        }
        Ok(Self(row))
    }

    pub fn canonical_bytes(&self) -> [u8; INPUT_DIM * 2] {
        let mut bytes = [0u8; INPUT_DIM * 2];
        for (index, value) in self.0.iter().enumerate() {
            bytes[index * 2..index * 2 + 2].copy_from_slice(&value.to_le_bytes());
        }
        bytes
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct QuantizedScore(pub u16);

impl QuantizedScore {
    pub const fn as_f64(self) -> f64 {
        self.0 as f64 / u16::MAX as f64
    }
}

#[cfg(any(all(feature = "gpu", feature = "ml"), test))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct AcceleratedCandidateScore {
    pub(crate) candidate_id: u32,
    pub(crate) score: QuantizedScore,
}

#[cfg(any(all(feature = "gpu", feature = "ml"), test))]
pub(crate) fn validate_accelerated_output(
    expected_candidates: usize,
    output: Result<Vec<AcceleratedCandidateScore>, QuantizedConfidenceError>,
) -> Result<Vec<QuantizedScore>, QuantizedConfidenceError> {
    if expected_candidates > MAX_CANDIDATES_PER_BATCH {
        return Err(QuantizedConfidenceError::BatchTooLarge {
            candidates: expected_candidates,
            maximum: MAX_CANDIDATES_PER_BATCH,
        });
    }
    let output = output?;
    if output.len() != expected_candidates {
        return Err(QuantizedConfidenceError::ScoreCardinality {
            expected: expected_candidates,
            actual: output.len(),
        });
    }
    let mut scores = Vec::new();
    scores.try_reserve_exact(output.len()).map_err(|_| {
        QuantizedConfidenceError::BackendFailure(
            "score allocation failed within the candidate bound".into(),
        )
    })?;
    for (expected_id, candidate) in output.into_iter().enumerate() {
        if candidate.candidate_id != expected_id as u32 {
            return Err(QuantizedConfidenceError::CandidateId {
                expected: expected_id as u32,
                actual: candidate.candidate_id,
            });
        }
        scores.push(candidate.score);
    }
    Ok(scores)
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum QuantizedConfidenceError {
    InvalidFeature { index: usize },
    InvalidModel(&'static str),
    BatchTooLarge { candidates: usize, maximum: usize },
    ScoreCardinality { expected: usize, actual: usize },
    CandidateId { expected: u32, actual: u32 },
    CandidateNotRepresentable,
    BackendFailure(String),
}

impl std::fmt::Display for QuantizedConfidenceError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidFeature { index } => write!(formatter, "confidence feature {index} is outside the quantized ABI"),
            Self::InvalidModel(reason) => write!(formatter, "quantized confidence artifact is invalid: {reason}"),
            Self::BatchTooLarge { candidates, maximum } => write!(formatter, "quantized confidence batch has {candidates} candidates, above the {maximum}-candidate bound"),
            Self::ScoreCardinality { expected, actual } => write!(formatter, "quantized confidence backend returned {actual} scores for {expected} candidates"),
            Self::CandidateId { expected, actual } => write!(formatter, "quantized confidence backend returned candidate ID {actual}, expected {expected}"),
            Self::CandidateNotRepresentable => formatter.write_str("candidate cannot be represented exactly by the quantized confidence ABI"),
            Self::BackendFailure(reason) => write!(formatter, "selected quantized confidence backend failed: {reason}"),
        }
    }
}

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

#[derive(Debug)]
pub struct QuantizedModel {
    params: Box<[i16]>,
    artifact_digest: [u8; 32],
    payload_digest: [u8; 32],
}

impl QuantizedModel {
    pub fn parse(bytes: &[u8]) -> Result<Self, QuantizedConfidenceError> {
        if bytes.len() < HEADER_LEN || &bytes[..8] != MAGIC {
            return Err(QuantizedConfidenceError::InvalidModel(
                "bad magic or truncated header",
            ));
        }
        let u16_at = |offset| u16::from_le_bytes([bytes[offset], bytes[offset + 1]]);
        if u16_at(8) != QUANTIZED_MODEL_FORMAT_VERSION
            || u16_at(10) != FEATURE_SCHEMA_VERSION
            || u16_at(12) as usize != INPUT_DIM
            || u16_at(14) as usize != EXPERT_COUNT
            || u16_at(16) as usize != EXPERT_FC1_OUT
            || u16_at(18) as usize != EXPERT_FC2_OUT
            || bytes[20] != FRACTIONAL_BITS
            || bytes[21] != ROUND_TIES_AWAY_FROM_ZERO
        {
            return Err(QuantizedConfidenceError::InvalidModel(
                "unsupported schema, dimensions, scale, or rounding",
            ));
        }
        if bytes[22..24] != [0, 0] {
            return Err(QuantizedConfidenceError::InvalidModel(
                "reserved header bytes are nonzero",
            ));
        }
        let payload_len = u32::from_le_bytes(
            bytes[24..28]
                .try_into()
                .map_err(|_| QuantizedConfidenceError::InvalidModel("truncated payload length"))?,
        ) as usize;
        let expected_len =
            TOTAL_F32_COUNT
                .checked_mul(2)
                .ok_or(QuantizedConfidenceError::InvalidModel(
                    "parameter length overflow",
                ))?;
        if payload_len != expected_len || bytes.len() != HEADER_LEN + payload_len {
            return Err(QuantizedConfidenceError::InvalidModel(
                "parameter count or artifact length mismatch",
            ));
        }
        let expected_payload_digest: [u8; 32] = bytes[28..60]
            .try_into()
            .map_err(|_| QuantizedConfidenceError::InvalidModel("truncated payload digest"))?;
        let payload = &bytes[HEADER_LEN..];
        let payload_digest: [u8; 32] = Sha256::digest(payload).into();
        if payload_digest != expected_payload_digest {
            return Err(QuantizedConfidenceError::InvalidModel(
                "payload digest mismatch",
            ));
        }
        let mut params = Vec::new();
        params
            .try_reserve_exact(TOTAL_F32_COUNT)
            .map_err(|_| QuantizedConfidenceError::InvalidModel("parameter allocation failed"))?;
        for pair in payload.chunks_exact(2) {
            params.push(i16::from_le_bytes([pair[0], pair[1]]));
        }
        Ok(Self {
            params: params.into_boxed_slice(),
            artifact_digest: Sha256::digest(bytes).into(),
            payload_digest,
        })
    }

    pub const fn artifact_digest(&self) -> [u8; 32] {
        self.artifact_digest
    }
    pub const fn payload_digest(&self) -> [u8; 32] {
        self.payload_digest
    }

    #[cfg(all(feature = "gpu", feature = "ml"))]
    pub(crate) fn parameters(&self) -> &[i16] {
        &self.params
    }

    pub fn score(&self, row: &QuantizedFeatureRow) -> QuantizedScore {
        let input = row.0.map(i32::from);
        let mut gate_logits = [0i32; EXPERT_COUNT];
        let gate_weights = &self.params[GATE_W_OFF..GATE_W_OFF + GATE_W_COUNT];
        let gate_bias = &self.params[GATE_B_OFF..GATE_B_OFF + GATE_B_COUNT];
        for expert in 0..EXPERT_COUNT {
            gate_logits[expert] = dense_signed(
                &gate_weights[expert * INPUT_DIM..(expert + 1) * INPUT_DIM],
                gate_bias[expert],
                &input,
            );
        }

        let mut expert_logits = [0i32; EXPERT_COUNT];
        for (expert, output) in expert_logits.iter_mut().enumerate() {
            let base = EXPERTS_OFF + expert * EXPERT_PARAM_COUNT;
            let fc1_w = &self.params[base..base + EXPERT_FC1_W_COUNT];
            let fc1_b_offset = base + EXPERT_FC1_W_COUNT;
            let fc1_b = &self.params[fc1_b_offset..fc1_b_offset + EXPERT_FC1_B_COUNT];
            let h1 = dense_relu::<INPUT_DIM, EXPERT_FC1_OUT>(fc1_w, fc1_b, &input);
            let fc2_w_offset = fc1_b_offset + EXPERT_FC1_B_COUNT;
            let fc2_w = &self.params[fc2_w_offset..fc2_w_offset + EXPERT_FC2_W_COUNT];
            let fc2_b_offset = fc2_w_offset + EXPERT_FC2_W_COUNT;
            let fc2_b = &self.params[fc2_b_offset..fc2_b_offset + EXPERT_FC2_B_COUNT];
            let h2 = dense_relu::<EXPERT_FC1_OUT, EXPERT_FC2_OUT>(fc2_w, fc2_b, &h1);
            let fc3_w_offset = fc2_b_offset + EXPERT_FC2_B_COUNT;
            let fc3_w = &self.params[fc3_w_offset..fc3_w_offset + EXPERT_FC3_W_COUNT];
            let fc3_b_offset = fc3_w_offset + EXPERT_FC3_W_COUNT;
            debug_assert_eq!(EXPERT_FC3_B_COUNT, 1);
            *output = dense_signed(fc3_w, self.params[fc3_b_offset], &h2);
        }
        QuantizedScore(fixed_sigmoid(mix_experts(&gate_logits, &expert_logits)))
    }
}

static MODEL: LazyLock<Result<QuantizedModel, QuantizedConfidenceError>> =
    LazyLock::new(|| QuantizedModel::parse(MODEL_BYTES));

pub fn model() -> Result<&'static QuantizedModel, QuantizedConfidenceError> {
    MODEL
        .as_ref()
        .map_err(|_| QuantizedConfidenceError::InvalidModel("embedded artifact was rejected"))
}

pub fn model_artifact_digest() -> [u8; 32] {
    Sha256::digest(MODEL_BYTES).into()
}

pub fn feature_schema_digest() -> [u8; 32] {
    let mut hasher = Sha256::new();
    hasher.update(b"keyhog-quantized-feature-schema\0");
    hasher.update(FEATURE_SCHEMA_VERSION.to_le_bytes());
    hasher.update([FRACTIONAL_BITS, ROUND_TIES_AWAY_FROM_ZERO]);
    for name in FEATURE_NAMES {
        hasher.update((name.len() as u16).to_le_bytes());
        hasher.update(name.as_bytes());
    }
    hasher.finalize().into()
}

pub fn score_batch(
    rows: &[QuantizedFeatureRow],
) -> Result<Vec<QuantizedScore>, QuantizedConfidenceError> {
    if rows.len() > MAX_CANDIDATES_PER_BATCH {
        return Err(QuantizedConfidenceError::BatchTooLarge {
            candidates: rows.len(),
            maximum: MAX_CANDIDATES_PER_BATCH,
        });
    }
    let model = model()?;
    let mut scores = Vec::new();
    scores
        .try_reserve_exact(rows.len())
        .map_err(|_| QuantizedConfidenceError::BatchTooLarge {
            candidates: rows.len(),
            maximum: MAX_CANDIDATES_PER_BATCH,
        })?;
    if rows.len() < crate::ml_scorer::ML_PARALLEL_BATCH_THRESHOLD {
        scores.extend(rows.iter().map(|row| model.score(row)));
    } else {
        scores.resize(rows.len(), QuantizedScore(0));
        use rayon::prelude::*;
        scores
            .par_iter_mut()
            .zip(rows.par_iter())
            .for_each(|(score, row)| *score = model.score(row));
    }
    Ok(scores)
}

fn quantize_f32(value: f32) -> Option<i16> {
    if !value.is_finite() || value < 0.0 {
        return None;
    }
    let scaled = value * SCALE as f32;
    if !scaled.is_finite() {
        return None;
    }
    let rounded = if scaled >= 0.0 {
        (scaled + 0.5).floor()
    } else {
        (scaled - 0.5).ceil()
    };
    Some((rounded as i32).clamp(i16::MIN as i32, i16::MAX as i32) as i16)
}

fn round_div_ties_away(numerator: i32, denominator: i32) -> i32 {
    debug_assert!(denominator > 0);
    if numerator >= 0 {
        numerator.saturating_add(denominator / 2) / denominator
    } else {
        numerator.saturating_sub(denominator / 2) / denominator
    }
}

fn dense_signed<const INPUT: usize>(weights: &[i16], bias: i16, input: &[i32; INPUT]) -> i32 {
    let mut acc = i32::from(bias).saturating_mul(SCALE);
    for (&value, &weight) in input.iter().zip(weights) {
        acc = acc.saturating_add(value.saturating_mul(i32::from(weight)));
    }
    round_div_ties_away(acc, SCALE).clamp(i16::MIN as i32, i16::MAX as i32)
}

fn dense_relu<const INPUT: usize, const OUTPUT: usize>(
    weights: &[i16],
    bias: &[i16],
    input: &[i32; INPUT],
) -> [i32; OUTPUT] {
    let mut output = [0i32; OUTPUT];
    for row in 0..OUTPUT {
        output[row] = dense_signed(&weights[row * INPUT..(row + 1) * INPUT], bias[row], input)
            .clamp(0, MAX_ACTIVATION);
    }
    output
}

fn mix_experts(gate_logits: &[i32; EXPERT_COUNT], expert_logits: &[i32; EXPERT_COUNT]) -> i32 {
    let maximum = gate_logits.iter().copied().max().unwrap_or(0);
    let mut weighted_sum = 0i32;
    let mut weight_sum = 0i32;
    for index in 0..EXPERT_COUNT {
        let delta = maximum.saturating_sub(gate_logits[index]);
        let weight = (SCALE.saturating_mul(GATE_DECAY) / GATE_DECAY.saturating_add(delta)).max(1);
        weighted_sum = weighted_sum.saturating_add(expert_logits[index].saturating_mul(weight));
        weight_sum = weight_sum.saturating_add(weight);
    }
    round_div_ties_away(weighted_sum, weight_sum.max(1))
}

fn fixed_sigmoid(logit: i32) -> u16 {
    if logit <= -SIGMOID_SATURATION {
        return 0;
    }
    if logit >= SIGMOID_SATURATION {
        return u16::MAX;
    }
    let magnitude = logit.unsigned_abs() as i32;
    let base = SCALE.saturating_add(magnitude);
    let numerator = if logit < 0 {
        SCALE
    } else {
        SCALE.saturating_add(logit.saturating_mul(2))
    };
    let denominator = base.saturating_mul(2);
    let scaled = numerator.saturating_mul(i32::from(u16::MAX));
    round_div_ties_away(scaled, denominator).clamp(0, i32::from(u16::MAX)) as u16
}