Q-Trust Crypto Detector (v4)

Binary classifier that labels a source file as crypto-containing or not, across C, C++, Java, Go, Rust, Python, JavaScript/TypeScript, Solidity, C#, PHP, Zig and Swift. It is the ML layer of the Q-Trust cryptographic-discovery pipeline (CBOM generation for post-quantum migration).

  • Base model: huggingface/CodeBERTa-small-v1 (fine-tuned for sequence classification, 2 labels)
  • Input: source code (truncated to 256 tokens; the detector chunks long files at inference)
  • Output: LABEL_1 = crypto present, LABEL_0 = not crypto

Intended use

Flag source files that use cryptographic primitives (RSA/ECC, AES/ChaCha20, SHA/BLAKE, X.509, and PQC: ML-KEM/Kyber, ML-DSA/Dilithium, SLH-DSA/SPHINCS+, Falcon, HQC) so they can be inventoried in a CycloneDX CBOM and prioritized for post-quantum migration.

This is usage discovery, not cryptographic misuse detection. It answers "does this file use crypto?", not "is this crypto used correctly?".

Training

Corpus Q-Trust code corpus v4 — 17,171 files / 58 source repositories / 8,814 crypto-labelled
Split repo-disjoint, digest-keyed (SHA-256 source buckets), seed 42
Epochs 3
Max length 256 tokens
Batch size 16
Optimizer AdamW, lr 2e-5
Hardware CPU
Export onnx/model.onnx (fp32) + onnx/model_quantized.onnx (int8, ~84 MB)

Try it in the browser

The in-browser scanner loads the int8 ONNX export locally and sends no code to a server. Source for the Space lives in deploy/hf-space/ in the Q-Trust repository.

Evaluation (honest, leakage-audited)

All numbers are on held-out repositories with zero exact-path overlap with training (verified by an evaluation-leakage audit — 87.6% / 92.2% of a naive shared-split eval would have been memorized by earlier checkpoints; this v4 checkpoint is 0.0%).

Split Gate Precision Recall F1 n
Digest, de-duplicated (primary) 0.50 0.9264 0.9146 0.9205 1,779
Digest, raw records 0.50 0.9201 0.9088 0.9144 2,238
Digest, de-duplicated (legacy gate) 0.85 0.9492 0.8626 0.9039 1,779
Digest, raw records (legacy gate) 0.85 0.9429 0.8546 0.8966 2,238
Native, raw (10 repos, incl. new) 0.50 0.9232 0.9238 0.9235 2,566
Native, de-duplicated 0.50 0.9287 0.9309 0.9298 2,107

The primary rows use ml_decisive_threshold = 0.50, selected on the native split and then reported on the pinned/de-duplicated split (E-16). The 0.85 rows are the legacy hand-set gate retained for lineage. The primary digest row keeps one record per (source, path) — the longest copy, chosen without looking at the label — because the raw held-out set repeats 197 paths (459 extra records, 22 label conflicts). Both views are reported: de-duplication changes the metric by +0.0073 F1 at the legacy gate (+0.0061 at the selected 0.50 gate), not the model (EXPERIMENTS.md E-15/E-16, qtrust_ai/artifacts/eval_dedup_v4.json, ensemble_scores_v4_{native,pinned}.json). A stricter exact-content view (removes only 14 exact copies, n=2,224) scores F1 0.9167 at the same gate. The three one-per-path policies score 0.9208 (keep-first), 0.9223 (keep-last) and 0.9205 (keep-longest, primary); keep-longest was chosen a priori as the most complete copy, not as the maximum, so the defensible clean range is 0.9167–0.9223 and the headline is 0.9205. The extras are stale snapshots, not distinct cases.

  • Beats a TF-IDF + LogisticRegression baseline (F1 0.6675 on the native split) by +0.24 F1.
  • Calibration: temperature T = 1.8416 (Brier 0.0853 → 0.0845).

Limitations (read before trusting the number)

  • Labels are regex-derived (is_crypto = "contains a crypto keyword"). The attainable F1 is therefore bounded by the labeler's own error, not model capacity. More epochs (5), more data (v5, +8 repos) and two training-corpus de-duplication policies were all tried and regressed or tied — a GPU control on the unmodified corpus scored 0.9169 pinned / 0.9251 native, on par with this checkpoint, so the ceiling is the label and the data weighting, not the learner.
  • Cross-language accuracy varies; C dominates the evaluation set.
  • Not a vulnerability scanner and not a substitute for human review.

Usage

from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

name = "KRISHNAPURI/q-trust-crypto-detector"
tok = AutoTokenizer.from_pretrained(name)
model = AutoModelForSequenceClassification.from_pretrained(name)

code = "from cryptography.hazmat.primitives.asymmetric import rsa\nkey = rsa.generate_private_key(public_exponent=65537, key_size=2048)"
enc = tok(code, return_tensors="pt", truncation=True, max_length=256)
with torch.no_grad():
    p = torch.softmax(model(**enc).logits, -1)[0, 1].item()
print("crypto probability:", round(p, 3))

Provenance

Part of the Q-Trust project (post-quantum migration assurance). Training/eval artifacts: training_report_v4.json, eval_dedup_v4.json, eval_leakage_audit.json, calibration_v4.json. The ONNX export is reproducible with scripts/export_detector_onnx.py; the browser demo source is in deploy/hf-space/. Code: humoge7502/q-trust.

Downloads last month
24
Safetensors
Model size
83.5M params
Tensor type
F32
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for KRISHNAPURI/q-trust-crypto-detector

Quantized
(5)
this model

Dataset used to train KRISHNAPURI/q-trust-crypto-detector

Space using KRISHNAPURI/q-trust-crypto-detector 1

Evaluation results

  • F1 (de-duplicated held-out, 0% train overlap) on Q-Trust code corpus v4 (repo-disjoint digest split)
    self-reported
    0.920
  • Precision on Q-Trust code corpus v4 (repo-disjoint digest split)
    self-reported
    0.926
  • Recall on Q-Trust code corpus v4 (repo-disjoint digest split)
    self-reported
    0.915