Skip to content

armgit / bla

Public Imported from GitHub
0

🛡️ BLA (Blind LLM Agents) — Adversarial defense shield that protects binaries, Electron apps, JS bundles, Python, and WebAssembly against AI reverse-engineering agents like REA. Poison -> Exhaust -> Hallucinate.

feat(prng): HMAC-SHA256 derive() for independent seed substreams

qybaihe3c709cd3ab66
diff --git a/src/core/prng.ts b/src/core/prng.ts
index bdc7350..ec8cb67 100644
--- a/src/core/prng.ts
+++ b/src/core/prng.ts
@@ -1,10 +1,28 @@
-import { createHash } from "node:crypto";
+import { createHash, createHmac } from "node:crypto";
import {
realisticIdentifier,
type IdentifierStyle,
type RealisticIdentifierOptions,
} from "./identifiers.js";
+/**
+ * Derive an independent, deterministic sub-seed from a root seed + label via
+ * HMAC-SHA256. Same (root, label) → same child seed; different labels do not
+ * share PRNG state. Prefer this over string-concat (`${seed}:${name}`) so
+ * substreams stay cryptographically independent.
+ */
+export function deriveSeedLabel(
+ rootSeed: string | number,
+ label: string,
+): string {
+ if (typeof label !== "string" || label.length === 0) {
+ throw new Error("deriveSeedLabel: label must be a non-empty string");
+ }
+ return createHmac("sha256", String(rootSeed))
+ .update(label, "utf8")
+ .digest("hex");
+}
+
/**
* Deterministic seeded PRNG (SplitMix32 / Mulberry32) so BLA builds can be
* 100% reproducible given a seed while producing high-entropy decoy identifiers,
@@ -21,6 +39,14 @@ export class SeededPrng {
if (this.#state === 0) this.#state = 0x6d2b79f5;
}
+ /**
+ * Fork an independent substream keyed by `label` (HMAC-SHA256 of root seed).
+ * Drawing from the child does not advance this instance's state.
+ */
+ derive(label: string): SeededPrng {
+ return new SeededPrng(deriveSeedLabel(this.seedString, label));
+ }
+
nextUint32(): number {
let t = (this.#state += 0x6d2b79f5);
t = Math.imul(t ^ (t >>> 15), t | 1);
diff --git a/src/index.ts b/src/index.ts
index c2efb12..e33a848 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,6 +1,6 @@
export { BLA_IDENTITY, REA_INTERNAL_LIMITS } from "./identity.js";
export * from "./core/types.js";
-export { SeededPrng } from "./core/prng.js";
+export { SeededPrng, deriveSeedLabel } from "./core/prng.js";
export { detectArtifactFormat } from "./core/detector.js";
export { estimateAgentReverseEngineeringCosts } from "./core/token-estimator.js";
export { generateChaffPayloads, type ChaffPayload } from "./shields/chaff/chaff-generator.js";
@@ -54,6 +54,16 @@ export {
type ElectronHardeningReport,
} from "./shields/formats/electron-fuses.js";
export { inspectWasmBinary, protectWasmBinary } from "./shields/formats/wasm.js";
+export {
+ comparePolymorphArtifacts,
+ POLYMORPH_REPORT_SCHEMA,
+} from "./engine/polymorph-report.js";
+export type {
+ PolymorphReport,
+ PolymorphReportOptions,
+ PolymorphExpectMode,
+ FilePairMetric,
+} from "./engine/polymorph-report.js";
export {
InvalidPythonSourceError,
findPythonPrologueInsertionOffset,
diff --git a/tests/prng-derive.test.ts b/tests/prng-derive.test.ts
new file mode 100644
index 0000000..25cea43
--- /dev/null
+++ b/tests/prng-derive.test.ts
@@ -0,0 +1,44 @@
+import { describe, expect, it } from "vitest";
+import { SeededPrng, deriveSeedLabel } from "../src/core/prng.js";
+
+describe("SeededPrng.derive / deriveSeedLabel", () => {
+ it("deriveSeedLabel is deterministic and label-sensitive", () => {
+ const a = deriveSeedLabel("root-seed", "wasm.export-order");
+ const b = deriveSeedLabel("root-seed", "wasm.export-order");
+ const c = deriveSeedLabel("root-seed", "js.identifiers");
+ const d = deriveSeedLabel("other-root", "wasm.export-order");
+ expect(a).toBe(b);
+ expect(a).toMatch(/^[0-9a-f]{64}$/);
+ expect(a).not.toBe(c);
+ expect(a).not.toBe(d);
+ });
+
+ it("rejects empty labels", () => {
+ expect(() => deriveSeedLabel("root", "")).toThrow(/non-empty/);
+ });
+
+ it("child streams are independent of parent draws", () => {
+ const root = new SeededPrng("independence-root");
+ const before = root.derive("child").nextUint32();
+ // Advance parent — must not change the child's first draw.
+ root.nextUint32();
+ root.nextUint32();
+ expect(root.derive("child").nextUint32()).toBe(before);
+ });
+
+ it("same seed + label → identical shuffle; different label → different", () => {
+ const items = ["a", "b", "c", "d", "e", "f", "g", "h"];
+ const s1 = new SeededPrng("poly-root").derive("lane").shuffle(items);
+ const s2 = new SeededPrng("poly-root").derive("lane").shuffle(items);
+ const s3 = new SeededPrng("poly-root").derive("other").shuffle(items);
+ expect(s1).toEqual(s2);
+ expect(s1).not.toEqual(s3);
+ });
+
+ it("different roots with same label diverge", () => {
+ const items = Array.from({ length: 16 }, (_, i) => `x${i}`);
+ const a = new SeededPrng("seed-A").derive("export").shuffle(items);
+ const b = new SeededPrng("seed-B").derive("export").shuffle(items);
+ expect(a).not.toEqual(b);
+ });
+});