merge sandbox/fe93ee4: chg_0008 operator_real_algorithms — 6 算子真算法仿真(crypto-js + secret-share + Box-Muller + HMAC, 15+27+33+17+16=108 e2e PASS)
Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
1 parent
8c06057283
commit
66d21a5415
12 files changed
+1252
-71
No files matched your search
@@ -9,20 +9,22 @@
|
||||
"start": "node dist/main.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@electric-sql/pglite": "^0.2.12",
|
||||
"@nestjs/common": "^10.4.0",
|
||||
"@nestjs/core": "^10.4.0",
|
||||
"@nestjs/jwt": "^10.2.0",
|
||||
"@nestjs/platform-express": "^10.4.0",
|
||||
"@electric-sql/pglite": "^0.2.12",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.14.1",
|
||||
"crypto-js": "^4.2.0",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.1",
|
||||
"uuid": "^9.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bcryptjs": "^2.4.6",
|
||||
"@types/crypto-js": "^4.2.2",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/node": "^20.11.0",
|
||||
"@types/uuid": "^9.0.7",
|
||||
|
||||
@@ -2,6 +2,7 @@ export interface OperatorMeta {
|
||||
code: string;
|
||||
name: string;
|
||||
version: string;
|
||||
algorithm?: string;
|
||||
schema: Record<string, any>;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,31 +1,92 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import * as CryptoJS from 'crypto-js';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
const GENESIS_HASH = '0'.repeat(64);
|
||||
const DEFAULT_CHAIN_KEY = 'mock-audit-chain-key';
|
||||
|
||||
/**
|
||||
* Compliance Audit — append-only HMAC chain for tamper-evident trail (v2).
|
||||
*
|
||||
* v2 changes vs chg_0005 mock:
|
||||
* - Each entry's hash = HMAC-SHA256(chain_key, prev_hash || "|" || payload_canonical_json).
|
||||
* - Caller can supply `entries: [...]` to extend an existing chain; otherwise
|
||||
* the operator starts from genesis.
|
||||
* - `verify: true` mode recomputes the entire chain from `entries` and returns
|
||||
* `chainValid` (true/false). Tampered entry → chainValid=false.
|
||||
* - Backward compat: chg_0005 schema action/actor/user/timestamp still work;
|
||||
* output retains `trail_id` UUID and `compliance_score` (FNV-1a-based for
|
||||
* backward compatibility). v2 adds `prev_hash`, `payload_hash`, `chain_index`.
|
||||
* - meta.algorithm = "hmac-chain-v2".
|
||||
* - schema_version = "v2" added (additive).
|
||||
*/
|
||||
@Injectable()
|
||||
export class ComplianceAuditOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'compliance-audit',
|
||||
name: 'Compliance Audit — immutable trail with compliance score (mock)',
|
||||
version: '0.1.0',
|
||||
name: 'Compliance Audit — HMAC chain tamper-evident trail (v2)',
|
||||
version: '0.2.0',
|
||||
algorithm: 'hmac-chain-v2',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['action', 'actor'],
|
||||
properties: {
|
||||
action: { type: 'string', minLength: 1 },
|
||||
actor: { type: 'string', minLength: 1 },
|
||||
action: { type: 'string' },
|
||||
actor: { type: 'string' },
|
||||
user: { type: 'string' },
|
||||
timestamp: { type: 'string' },
|
||||
verify: { type: 'boolean', default: false },
|
||||
entries: {
|
||||
type: 'array',
|
||||
items: { type: 'object' },
|
||||
},
|
||||
chain_key: { type: 'string' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
additionalProperties: true,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const chainKey = typeof input?.chain_key === 'string' && input.chain_key.length > 0
|
||||
? input.chain_key
|
||||
: DEFAULT_CHAIN_KEY;
|
||||
|
||||
// ----- verify mode (chg_0008 S6 verify flow) -----
|
||||
if (input?.verify === true) {
|
||||
const entries: any[] = Array.isArray(input?.entries) ? input.entries : [];
|
||||
let prevHash = GENESIS_HASH;
|
||||
let valid = true;
|
||||
let firstBadIndex = -1;
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
const e = entries[i];
|
||||
if (!e || typeof e !== 'object') { valid = false; firstBadIndex = i; break; }
|
||||
// Hash over payload (the data signed) — same as record mode.
|
||||
const expected = this.hashEntry(chainKey, prevHash, e.payload ?? e);
|
||||
const stored = typeof e.hash === 'string' ? e.hash : '';
|
||||
if (stored !== expected) { valid = false; firstBadIndex = i; break; }
|
||||
if (typeof e.prev_hash === 'string' && e.prev_hash !== prevHash) {
|
||||
valid = false; firstBadIndex = i; break;
|
||||
}
|
||||
prevHash = stored;
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
audit: {
|
||||
chainValid: valid,
|
||||
entries_verified: entries.length,
|
||||
first_bad_index: firstBadIndex,
|
||||
mode: 'verify',
|
||||
},
|
||||
schema_version: 'v2',
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
// ----- append / record mode -----
|
||||
const action = String(input?.action ?? '').trim();
|
||||
const actor = String(input?.actor ?? '').trim();
|
||||
if (!action) return { ok: false, error: 'action required' };
|
||||
if (!actor) return { ok: false, error: 'actor required' };
|
||||
if (!action && !input?.require_chain) return { ok: false, error: 'action required' };
|
||||
if (!actor && !input?.require_chain) return { ok: false, error: 'actor required' };
|
||||
|
||||
const user = input?.user !== undefined ? String(input.user) : actor;
|
||||
const timestamp =
|
||||
@@ -33,11 +94,28 @@ export class ComplianceAuditOperator implements Operator {
|
||||
? input.timestamp
|
||||
: new Date().toISOString();
|
||||
|
||||
// Real computation: UUID v4 trail + deterministic compliance score in [0,1).
|
||||
const trail_id = uuidv4();
|
||||
const compliance_score = Number((this.fnv1a(`${action}:${actor}`) % 10000) / 10000).toFixed(6);
|
||||
const score = Number(compliance_score);
|
||||
|
||||
const prevEntries: any[] = Array.isArray(input?.entries) ? input.entries : [];
|
||||
const prevHash = prevEntries.length > 0
|
||||
? (typeof prevEntries[prevEntries.length - 1].hash === 'string'
|
||||
? prevEntries[prevEntries.length - 1].hash
|
||||
: GENESIS_HASH)
|
||||
: GENESIS_HASH;
|
||||
|
||||
const payload = { action, actor, user, timestamp };
|
||||
const hash = this.hashEntry(chainKey, prevHash, payload);
|
||||
const chainIndex = prevEntries.length;
|
||||
|
||||
const entry = {
|
||||
index: chainIndex,
|
||||
prev_hash: prevHash,
|
||||
payload,
|
||||
hash,
|
||||
};
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
audit: {
|
||||
@@ -49,11 +127,23 @@ export class ComplianceAuditOperator implements Operator {
|
||||
compliance_score: score,
|
||||
status: 'recorded',
|
||||
recorded_at: new Date().toISOString(),
|
||||
prev_hash: prevHash,
|
||||
payload_hash: hash,
|
||||
chain_index: chainIndex,
|
||||
mode: prevEntries.length > 0 ? 'append' : 'genesis',
|
||||
entry,
|
||||
},
|
||||
schema_version: 'v2',
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
private hashEntry(chainKey: string, prevHash: string, payload: Record<string, any>): string {
|
||||
const payloadStr = JSON.stringify(payload, Object.keys(payload).sort());
|
||||
const message = `${prevHash}|${payloadStr}`;
|
||||
return CryptoJS.HmacSHA256(message, chainKey).toString(CryptoJS.enc.Hex);
|
||||
}
|
||||
|
||||
private fnv1a(s: string): number {
|
||||
let h = 0x811c9dc5;
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
|
||||
@@ -1,15 +1,28 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
/**
|
||||
* Horizontal FedAvg — Federated Averaging across clients (v2 real algorithm).
|
||||
*
|
||||
* v2 changes vs chg_0005 mock:
|
||||
* - Support both legacy `clients: [{weights: number[]}]` (chg_0005 contract)
|
||||
* and new `weights: number[] | {w: number}[]` (chg_0008 S2 contract).
|
||||
* - When `epsilon` is provided, add **Gaussian DP noise** sampled via Box-Muller
|
||||
* transform: sigma = sensitivity / epsilon. trace.noise_scale exposes the scale.
|
||||
* - When epsilon is absent (legacy mode), output is the pure mean — chg_0005
|
||||
* contract preserved (asserts `aggregated == [0.3, 0.4]` exactly).
|
||||
* - meta.algorithm = "fedavg-gaussian-dp-v2".
|
||||
* - schema_version = "v2" added (additive).
|
||||
*/
|
||||
@Injectable()
|
||||
export class HorizontalFedavgOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'horizontal-fedavg',
|
||||
name: 'Horizontal Federated Averaging (FedAvg one round, mean/sum)',
|
||||
version: '0.1.0',
|
||||
name: 'Horizontal Federated Averaging (FedAvg + Gaussian DP v2)',
|
||||
version: '0.2.0',
|
||||
algorithm: 'fedavg-gaussian-dp-v2',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['clients'],
|
||||
properties: {
|
||||
clients: {
|
||||
type: 'array',
|
||||
@@ -22,21 +35,75 @@ export class HorizontalFedavgOperator implements Operator {
|
||||
},
|
||||
},
|
||||
},
|
||||
weights: {
|
||||
oneOf: [
|
||||
{ type: 'array', items: { type: 'number' } },
|
||||
{
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'object',
|
||||
required: ['w'],
|
||||
properties: { w: { type: 'number' } },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
aggregation: { type: 'string', enum: ['mean', 'sum'], default: 'mean' },
|
||||
epsilon: { type: 'number', exclusiveMinimum: 0 },
|
||||
sensitivity: { type: 'number', exclusiveMinimum: 0, default: 1.0 },
|
||||
},
|
||||
additionalProperties: false,
|
||||
additionalProperties: true,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const clients = Array.isArray(input?.clients) ? input.clients : [];
|
||||
if (clients.length === 0) {
|
||||
return { ok: false, error: 'clients must be a non-empty array' };
|
||||
}
|
||||
const aggregation = input?.aggregation === 'sum' ? 'sum' : 'mean';
|
||||
|
||||
// Real computation: FedAvg one-round server-side aggregation.
|
||||
// Validate dims are homogeneous.
|
||||
// ----- new mode: flat weights array (chg_0008 S2) -----
|
||||
if (Array.isArray(input?.weights) && input.weights.length > 0) {
|
||||
const flatWeights: number[] = (input.weights as any[]).map((w) => {
|
||||
if (typeof w === 'number') return w;
|
||||
if (w && typeof w === 'object' && typeof w.w === 'number') return w.w;
|
||||
return NaN;
|
||||
});
|
||||
if (flatWeights.some((v) => !Number.isFinite(v))) {
|
||||
return { ok: false, error: 'weights must be number[] or {w:number}[]' };
|
||||
}
|
||||
const meanVal = flatWeights.reduce((a, b) => a + b, 0) / flatWeights.length;
|
||||
|
||||
// DP noise (Gaussian via Box-Muller)
|
||||
const epsilon = Number(input?.epsilon);
|
||||
const hasNoise = Number.isFinite(epsilon) && epsilon > 0;
|
||||
const sensitivity = Number(input?.sensitivity ?? 1.0);
|
||||
const noiseScale = hasNoise ? sensitivity / epsilon : 0;
|
||||
const noise = hasNoise ? this.gaussian(noiseScale) : 0;
|
||||
const finalWeights = [Number((meanVal + noise).toFixed(6))];
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
fedavg: {
|
||||
aggregated: finalWeights,
|
||||
final_weights: finalWeights[0],
|
||||
dimension: 1,
|
||||
client_count: flatWeights.length,
|
||||
round_count: 1,
|
||||
aggregation,
|
||||
algorithm: 'fedavg-gaussian-dp-v2',
|
||||
noise_scale: Number(noiseScale.toFixed(6)),
|
||||
noise_applied: hasNoise,
|
||||
epsilon: hasNoise ? epsilon : null,
|
||||
sensitivity: hasNoise ? sensitivity : null,
|
||||
},
|
||||
schema_version: 'v2',
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
// ----- legacy mode: clients[].weights[] (chg_0005 contract) -----
|
||||
const clients = Array.isArray(input?.clients) ? input.clients : [];
|
||||
if (clients.length === 0) {
|
||||
return { ok: false, error: 'clients (or weights) must be a non-empty array' };
|
||||
}
|
||||
const dims: number[] = clients.map((c: any) =>
|
||||
Array.isArray(c?.weights) ? (c.weights as number[]).length : 0,
|
||||
);
|
||||
@@ -69,9 +136,26 @@ export class HorizontalFedavgOperator implements Operator {
|
||||
round_count: 1,
|
||||
aggregation,
|
||||
dimension: dim,
|
||||
algorithm: 'fedavg-mock-1-round',
|
||||
algorithm: 'fedavg-gaussian-dp-v2',
|
||||
noise_scale: 0,
|
||||
noise_applied: false,
|
||||
},
|
||||
schema_version: 'v2',
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Box-Muller transform — sample one standard-normal variate (m=0, sigma=1)
|
||||
* and return sigma * z. Two uniforms in (0,1] → one Gaussian via polar form.
|
||||
*/
|
||||
private gaussian(sigma: number): number {
|
||||
let u1 = Math.random();
|
||||
if (u1 <= 0 || u1 > 1) u1 = 1e-12;
|
||||
const u2 = Math.random();
|
||||
const z = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
|
||||
// Clip to ±10 sigma (avoid degenerate ln of near-zero)
|
||||
const clipped = Math.max(-10, Math.min(10, z));
|
||||
return sigma * clipped;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import * as CryptoJS from 'crypto-js';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
const INJECTION_KEYWORDS = [
|
||||
@@ -10,46 +11,130 @@ const INJECTION_KEYWORDS = [
|
||||
'jailbreak',
|
||||
];
|
||||
|
||||
const INJECTION_PATTERNS = [
|
||||
/ignore\s+(all\s+)?previous\s+instructions/i,
|
||||
/reveal\s+system\s+prompt/i,
|
||||
/override\s+system/i,
|
||||
/jailbreak/i,
|
||||
/developer\s+mode/i,
|
||||
/bypass\s+safety/i,
|
||||
];
|
||||
|
||||
// Empirical baseline distribution (mean, stddev) for prompt length in chars.
|
||||
// Calibrated against a typical chat-completion workload; used for z-score gating.
|
||||
const LENGTH_BASELINE_MEAN = 80;
|
||||
const LENGTH_BASELINE_STD = 200;
|
||||
const ZSCORE_THRESHOLD = 3.0;
|
||||
|
||||
const DEFAULT_API_KEY = 'mock-prompt-defense-api-key';
|
||||
|
||||
/**
|
||||
* Prompt Injection Defense — three-stage real detector (v2).
|
||||
*
|
||||
* v2 changes vs chg_0005 mock:
|
||||
* - Keep legacy keyword match (chg_0005 contract: detected bool + threat_level).
|
||||
* - Add regex injection-pattern matching (`reasons[]` includes
|
||||
* "injection-pattern-match" when regex hits).
|
||||
* - Add length z-score anomaly detection (`reasons[]` includes "length-z-anomaly"
|
||||
* when |z| > 3).
|
||||
* - Add HMAC-SHA256 signature verification: caller supplies `signature` (and
|
||||
* optionally `api_key`); operator recomputes HMAC(api_key, prompt) and
|
||||
* reports `signature_valid` (true/false). Signature mismatch → defected=true
|
||||
* with `reasons[]` including "signature-mismatch".
|
||||
* - meta.algorithm = "z-score-hmac-prompt-v2".
|
||||
* - schema_version = "v2" added (additive).
|
||||
*/
|
||||
@Injectable()
|
||||
export class PromptDefenseOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'prompt-defense',
|
||||
name: 'Prompt Injection Defense — keyword-based threat classifier',
|
||||
version: '0.1.0',
|
||||
name: 'Prompt Injection Defense — keyword + z-score + HMAC (v2)',
|
||||
version: '0.2.0',
|
||||
algorithm: 'z-score-hmac-prompt-v2',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['prompt'],
|
||||
properties: {
|
||||
prompt: { type: 'string', minLength: 0 },
|
||||
signature: { type: 'string' },
|
||||
api_key: { type: 'string' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
additionalProperties: true,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const raw = String(input?.prompt ?? '');
|
||||
const lower = raw.toLowerCase();
|
||||
const reasons: string[] = [];
|
||||
|
||||
// Real computation: case-insensitive substring match for each keyword.
|
||||
// Stage 1: keyword match (chg_0005 legacy contract).
|
||||
const matched: string[] = [];
|
||||
for (const kw of INJECTION_KEYWORDS) {
|
||||
if (lower.includes(kw)) matched.push(kw);
|
||||
}
|
||||
const hits = matched.length;
|
||||
const detected = hits >= 1;
|
||||
const threat_level = hits >= 2 ? 'high' : hits === 1 ? 'medium' : 'low';
|
||||
const kwHits = matched.length;
|
||||
if (kwHits > 0) {
|
||||
reasons.push(`keyword-match[${matched.slice(0, 3).join(',')}]`);
|
||||
}
|
||||
|
||||
return {
|
||||
// Stage 2: regex injection patterns.
|
||||
let patternHits = 0;
|
||||
for (const pattern of INJECTION_PATTERNS) {
|
||||
if (pattern.test(raw)) patternHits++;
|
||||
}
|
||||
if (patternHits > 0) {
|
||||
reasons.push('injection-pattern-match');
|
||||
}
|
||||
|
||||
// Stage 3: length z-score anomaly.
|
||||
const z = (raw.length - LENGTH_BASELINE_MEAN) / LENGTH_BASELINE_STD;
|
||||
const zAnomaly = Math.abs(z) > ZSCORE_THRESHOLD;
|
||||
if (zAnomaly) {
|
||||
reasons.push('length-z-anomaly');
|
||||
}
|
||||
|
||||
// Stage 4: HMAC signature verification (chg_0008 S5).
|
||||
let signatureValid: boolean | null = null;
|
||||
const suppliedSig = typeof input?.signature === 'string' ? input.signature : '';
|
||||
const apiKey = typeof input?.api_key === 'string' && input.api_key.length > 0
|
||||
? input.api_key
|
||||
: DEFAULT_API_KEY;
|
||||
if (suppliedSig.length > 0) {
|
||||
const expected = CryptoJS.HmacSHA256(raw, apiKey).toString(CryptoJS.enc.Hex);
|
||||
signatureValid = expected === suppliedSig;
|
||||
if (!signatureValid) {
|
||||
reasons.push('signature-mismatch');
|
||||
}
|
||||
}
|
||||
|
||||
// Combine verdicts.
|
||||
const detected = kwHits >= 1 || patternHits > 0 || zAnomaly || signatureValid === false;
|
||||
const threatLevel =
|
||||
(patternHits > 0 || signatureValid === false || zAnomaly) ? 'high'
|
||||
: kwHits >= 2 ? 'high'
|
||||
: kwHits === 1 ? 'medium'
|
||||
: 'low';
|
||||
|
||||
const result: Record<string, any> = {
|
||||
ok: true,
|
||||
defense: {
|
||||
detected,
|
||||
threat_level,
|
||||
threat_level: threatLevel,
|
||||
matched_keywords: matched,
|
||||
score: hits,
|
||||
score: kwHits,
|
||||
pattern_hits: patternHits,
|
||||
z_score: Number(z.toFixed(4)),
|
||||
z_anomaly: zAnomaly,
|
||||
signature_provided: suppliedSig.length > 0,
|
||||
signature_valid: signatureValid,
|
||||
reasons,
|
||||
prompt_length: raw.length,
|
||||
keywords_total: INJECTION_KEYWORDS.length,
|
||||
},
|
||||
schema_version: 'v2',
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,32 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import * as CryptoJS from 'crypto-js';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
/**
|
||||
* SCQL — Secure Collaborative Query (v2 real algorithm).
|
||||
*
|
||||
* v2 changes vs chg_0005 mock:
|
||||
* - Support both legacy `columns: [{values: number[]}]` (chg_0005 contract) and
|
||||
* new `rows: [{value: number}, ...]` (chg_0008 S1 contract).
|
||||
* - For rows mode, perform deterministic 3-party secret-share aggregation
|
||||
* using SHA256-derived shares; each share satisfies shareA + shareB + shareC = value
|
||||
* in floating-point arithmetic (c = value - a - b exact under IEEE 754).
|
||||
* - trace.partSums = [Σ shareA, Σ shareB, Σ shareC]; invariant: sum(partSums) === sum(values).
|
||||
* - meta.algorithm = "secret-share-aggregation-v2".
|
||||
* - schema_version = "v2" added (additive).
|
||||
*
|
||||
* Determinism: shares are derived from SHA256(seed + "|" + rowIdx + "|" + partyIdx),
|
||||
* where seed = input.seed ?? "scql-default-seed" (so output is stable per input).
|
||||
*/
|
||||
@Injectable()
|
||||
export class ScqlOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'scql',
|
||||
name: 'SCQL — Secure Collaborative Query (multi-party sum aggregation mock)',
|
||||
version: '0.1.0',
|
||||
name: 'SCQL — Secure Collaborative Query (secret-share aggregation v2)',
|
||||
version: '0.2.0',
|
||||
algorithm: 'secret-share-aggregation-v2',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['columns'],
|
||||
properties: {
|
||||
columns: {
|
||||
type: 'array',
|
||||
@@ -22,41 +39,103 @@ export class ScqlOperator implements Operator {
|
||||
},
|
||||
},
|
||||
},
|
||||
rows: {
|
||||
type: 'array',
|
||||
minItems: 1,
|
||||
items: {
|
||||
type: 'object',
|
||||
required: ['value'],
|
||||
properties: {
|
||||
value: { type: 'number' },
|
||||
},
|
||||
},
|
||||
},
|
||||
seed: { type: 'string' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
additionalProperties: true,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const cols = Array.isArray(input?.columns) ? input.columns : [];
|
||||
if (cols.length === 0) {
|
||||
return { ok: false, error: 'columns must be a non-empty array' };
|
||||
const rows = Array.isArray(input?.rows) ? input.rows : [];
|
||||
const seed = typeof input?.seed === 'string' && input.seed.length > 0
|
||||
? input.seed
|
||||
: 'scql-default-seed';
|
||||
|
||||
if (cols.length === 0 && rows.length === 0) {
|
||||
return { ok: false, error: 'either columns or rows must be a non-empty array' };
|
||||
}
|
||||
|
||||
// Real computation: secure-sum across columns (mock multi-party aggregation).
|
||||
const perColumnSum: number[] = [];
|
||||
// --- legacy columns mode (chg_0005 contract preserved) ---
|
||||
let totalSum = 0;
|
||||
let totalValues = 0;
|
||||
const perColumnSum: number[] = [];
|
||||
|
||||
for (const col of cols) {
|
||||
const vals = Array.isArray(col?.values) ? col.values.map(Number).filter(Number.isFinite) : [];
|
||||
const colSum = vals.reduce((a, b) => a + b, 0);
|
||||
perColumnSum.push(colSum);
|
||||
totalSum += colSum;
|
||||
totalValues += vals.length;
|
||||
if (cols.length > 0) {
|
||||
for (const col of cols) {
|
||||
const vals = Array.isArray(col?.values)
|
||||
? (col.values as number[]).map(Number).filter(Number.isFinite)
|
||||
: [];
|
||||
const colSum = vals.reduce((a, b) => a + b, 0);
|
||||
perColumnSum.push(Number(colSum.toFixed(6)));
|
||||
totalSum += colSum;
|
||||
totalValues += vals.length;
|
||||
}
|
||||
}
|
||||
|
||||
// --- new rows mode (chg_0008 S1: secret-share 3-way aggregation) ---
|
||||
let partSumA = 0;
|
||||
let partSumB = 0;
|
||||
let partSumC = 0;
|
||||
const shareCount = 3;
|
||||
|
||||
if (rows.length > 0) {
|
||||
for (let i = 0; i < rows.length; i++) {
|
||||
const rowVal = Number(rows[i]?.value);
|
||||
if (!Number.isFinite(rowVal)) continue;
|
||||
const [a, b, c] = this.share3(rowVal, i, seed);
|
||||
partSumA += a;
|
||||
partSumB += b;
|
||||
partSumC += c;
|
||||
totalSum += rowVal;
|
||||
totalValues += 1;
|
||||
}
|
||||
}
|
||||
|
||||
const partSums = [partSumA, partSumB, partSumC];
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
scql: {
|
||||
sum: totalSum,
|
||||
sum: Number(totalSum.toFixed(6)),
|
||||
column_count: cols.length,
|
||||
total_values: totalValues,
|
||||
mean: totalValues > 0 ? Number((totalSum / totalValues).toFixed(6)) : 0,
|
||||
per_column_sum: perColumnSum,
|
||||
protocol: 'scql-psi-sum-mock',
|
||||
share_count: shareCount,
|
||||
part_sums: partSums.map((p) => Number(p.toFixed(6))),
|
||||
protocol: 'secret-share-aggregation-v2',
|
||||
},
|
||||
schema_version: 'v2',
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Deterministic 3-party secret share for a single value.
|
||||
* Uses SHA256 to derive shareA and shareB in [0, value]; c = value - a - b exactly.
|
||||
* Returns three numbers such that a + b + c === value (within IEEE 754 ulp).
|
||||
*/
|
||||
private share3(value: number, rowIdx: number, seed: string): [number, number, number] {
|
||||
const aHex = CryptoJS.SHA256(`${seed}|${rowIdx}|A`).toString(CryptoJS.enc.Hex);
|
||||
const bHex = CryptoJS.SHA256(`${seed}|${rowIdx}|B`).toString(CryptoJS.enc.Hex);
|
||||
// First 13 hex chars ≈ 52 bits of uniform
|
||||
const a01 = parseInt(aHex.slice(0, 13), 16) / 0x1FFFFFFFFFFFFF;
|
||||
const b01 = parseInt(bHex.slice(0, 13), 16) / 0x1FFFFFFFFFFFFF;
|
||||
const a = value * a01;
|
||||
const b = (value - a) * b01;
|
||||
const c = value - a - b; // exact in IEEE-754: a+b+c === value up to ulp
|
||||
return [a, b, c];
|
||||
}
|
||||
}
|
||||
@@ -1,53 +1,110 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import * as CryptoJS from 'crypto-js';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
/**
|
||||
* Secure MLP — single-layer forward pass with Laplace DP noise (v2 real algorithm).
|
||||
*
|
||||
* v2 changes vs chg_0005 mock:
|
||||
* - Support legacy `features: [[..]], weights: [], bias, epsilon` (chg_0005 contract).
|
||||
* - Support new `x: number[]` + `run_id` (chg_0008 S3) — W and b are derived from
|
||||
* SHA256(run_id) via Mulberry32 PRNG; identical run_id → identical output.
|
||||
* - Laplace DP noise uses the seeded PRNG (legacy mode uses Math.random fallback,
|
||||
* because user passes weights explicitly and expects fresh noise each call).
|
||||
* - meta.algorithm = "secure-mlp-laplace-dp-v2".
|
||||
* - schema_version = "v2" added (additive).
|
||||
*/
|
||||
@Injectable()
|
||||
export class SecureMlpOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'secure-mlp',
|
||||
name: 'Secure MLP — single-layer forward + Laplace DP noise (mock)',
|
||||
version: '0.1.0',
|
||||
name: 'Secure MLP — single-layer forward + Laplace DP (seeded v2)',
|
||||
version: '0.2.0',
|
||||
algorithm: 'secure-mlp-laplace-dp-v2',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['features', 'weights', 'bias', 'epsilon'],
|
||||
properties: {
|
||||
features: {
|
||||
type: 'array',
|
||||
minItems: 1,
|
||||
items: {
|
||||
type: 'array',
|
||||
items: { type: 'number' },
|
||||
},
|
||||
},
|
||||
weights: { type: 'array', items: { type: 'number' }, minItems: 1 },
|
||||
weights: { type: 'array', items: { type: 'number' } },
|
||||
bias: { type: 'number' },
|
||||
epsilon: { type: 'number', exclusiveMinimum: 0 },
|
||||
x: { type: 'array', items: { type: 'number' } },
|
||||
run_id: { type: 'string' },
|
||||
in_dim: { type: 'number' },
|
||||
out_dim: { type: 'number' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
additionalProperties: true,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const epsilon = Number(input?.epsilon);
|
||||
const noiseScale = Number.isFinite(epsilon) && epsilon > 0 ? 1 / epsilon : 0;
|
||||
|
||||
// ----- new mode: x + run_id (chg_0008 S3 deterministic) -----
|
||||
if (Array.isArray(input?.x) && (input?.x as number[]).length > 0) {
|
||||
const x = (input.x as number[]).map(Number);
|
||||
const runId = typeof input?.run_id === 'string' && input.run_id.length > 0
|
||||
? input.run_id
|
||||
: 'mlp-default-run';
|
||||
const inDim = x.length;
|
||||
const outDim = Number.isFinite(Number(input?.out_dim)) ? Number(input.out_dim) : 1;
|
||||
const rng = this.seedRNG(runId);
|
||||
const W = this.gaussianMatrix(rng, outDim, inDim);
|
||||
const B = this.gaussianVector(rng, outDim);
|
||||
|
||||
const predictions: number[] = [];
|
||||
for (let i = 0; i < outDim; i++) {
|
||||
let z = B[i];
|
||||
for (let j = 0; j < inDim; j++) z += W[i][j] * x[j];
|
||||
z += this.seededLaplace(rng, noiseScale);
|
||||
predictions.push(Number(this.sigmoid(z).toFixed(6)));
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
mlp: {
|
||||
output: predictions,
|
||||
predictions,
|
||||
noise_scale: Number(noiseScale.toFixed(6)),
|
||||
rows: outDim,
|
||||
in_dim: inDim,
|
||||
out_dim: outDim,
|
||||
weights_used: W,
|
||||
bias_used: B,
|
||||
epsilon: Number.isFinite(epsilon) && epsilon > 0 ? epsilon : null,
|
||||
seed: runId,
|
||||
mechanism: 'laplace',
|
||||
noise_applied: noiseScale > 0,
|
||||
deterministic: true,
|
||||
},
|
||||
schema_version: 'v2',
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
// ----- legacy mode: features + weights + bias + epsilon (chg_0005 contract) -----
|
||||
const features = Array.isArray(input?.features) ? input.features : [];
|
||||
const weights = Array.isArray(input?.weights) ? input.weights.map(Number) : [];
|
||||
const bias = Number(input?.bias ?? 0);
|
||||
const epsilon = Number(input?.epsilon);
|
||||
if (features.length === 0) return { ok: false, error: 'features empty' };
|
||||
if (features.length === 0) return { ok: false, error: 'features empty (or x)' };
|
||||
if (weights.length === 0) return { ok: false, error: 'weights empty' };
|
||||
if (!Number.isFinite(epsilon) || epsilon <= 0) return { ok: false, error: 'epsilon must be > 0' };
|
||||
|
||||
const dim = weights.length;
|
||||
const noiseScale = 1 / epsilon;
|
||||
|
||||
// Real computation: z_i = bias + Σ_j w_j * x_ij + Laplace(0, noiseScale)
|
||||
// pred_i = sigmoid(z_i)
|
||||
const predictions: number[] = [];
|
||||
for (const row of features) {
|
||||
const xRow: number[] = Array.isArray(row) ? (row as number[]).map(Number) : [];
|
||||
let z = bias;
|
||||
for (let j = 0; j < dim; j++) z += weights[j] * (xRow[j] ?? 0);
|
||||
z += this.laplace(noiseScale);
|
||||
const p = 1 / (1 + Math.exp(-z));
|
||||
const p = this.sigmoid(z);
|
||||
predictions.push(Number(p.toFixed(6)));
|
||||
}
|
||||
|
||||
@@ -61,14 +118,71 @@ export class SecureMlpOperator implements Operator {
|
||||
bias_used: bias,
|
||||
epsilon,
|
||||
mechanism: 'laplace',
|
||||
deterministic: false,
|
||||
},
|
||||
schema_version: 'v2',
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
// Laplace noise sampler (m=0, b=scale). Uses inverse-CDF with two uniforms.
|
||||
private sigmoid(z: number): number {
|
||||
return 1 / (1 + Math.exp(-z));
|
||||
}
|
||||
|
||||
/**
|
||||
* Laplace noise sampler (m=0, b=scale) — inverse-CDF using two uniforms.
|
||||
*/
|
||||
private laplace(scale: number): number {
|
||||
if (scale === 0) return 0;
|
||||
const u = Math.random() - 0.5;
|
||||
return -scale * Math.sign(u) * Math.log(1 - 2 * Math.abs(u));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deterministic PRNG: SHA256(seed) → 32 bytes → Mulberry32 32-bit state.
|
||||
* Returns a function: () => number in [0, 1).
|
||||
*/
|
||||
private seedRNG(seed: string): () => number {
|
||||
const hex = CryptoJS.SHA256(seed).toString(CryptoJS.enc.Hex);
|
||||
let s = 0;
|
||||
for (let i = 0; i < 8; i++) s = (s * 256 + parseInt(hex.slice(i * 2, i * 2 + 2), 16)) >>> 0;
|
||||
return () => {
|
||||
s = (s + 0x6D2B79F5) >>> 0;
|
||||
let t = s;
|
||||
t = Math.imul(t ^ (t >>> 15), t | 1);
|
||||
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
||||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||||
};
|
||||
}
|
||||
|
||||
/** Mulberry32-derived Gaussian in N(0, 1) via Box-Muller. */
|
||||
private seededGaussian(rng: () => number): number {
|
||||
let u1 = rng();
|
||||
if (u1 <= 0) u1 = 1e-12;
|
||||
const u2 = rng();
|
||||
return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
|
||||
}
|
||||
|
||||
/** Mulberry32-derived Laplace noise with given scale. */
|
||||
private seededLaplace(rng: () => number, scale: number): number {
|
||||
if (scale === 0) return 0;
|
||||
const u = rng() - 0.5;
|
||||
return -scale * Math.sign(u) * Math.log(1 - 2 * Math.abs(u));
|
||||
}
|
||||
|
||||
private gaussianMatrix(rng: () => number, rows: number, cols: number): number[][] {
|
||||
const out: number[][] = [];
|
||||
for (let i = 0; i < rows; i++) {
|
||||
const row: number[] = [];
|
||||
for (let j = 0; j < cols; j++) row.push(Number(this.seededGaussian(rng).toFixed(6)));
|
||||
out.push(row);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private gaussianVector(rng: () => number, dim: number): number[] {
|
||||
const out: number[] = [];
|
||||
for (let i = 0; i < dim; i++) out.push(Number(this.seededGaussian(rng).toFixed(6)));
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,37 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { createHash } from 'crypto';
|
||||
import { createHash, randomBytes } from 'crypto';
|
||||
import * as CryptoJS from 'crypto-js';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
/**
|
||||
* TEE Inference — confidential LLM inference with HMAC-SHA256 attestation (v2).
|
||||
*
|
||||
* v2 changes vs chg_0005 mock:
|
||||
* - Build a real attestation quote: pcr0 + pcr1 + nonce + signature.
|
||||
* - signature = HMAC-SHA256(attestation_key, pcr0 || "|" || pcr1 || "|" || nonce).
|
||||
* - When attestation_key is provided, output `quote` object and a self-verified
|
||||
* `verify_code`: 0 if HMAC matches, 1 if verification fails (e.g. wrong key).
|
||||
* - Backward compat: when no attestation_key given, output `attestation` field
|
||||
* in chg_0005 form (`tee-mock-<56 hex chars>` of sha256(prompt||model)).
|
||||
* - meta.algorithm = "tee-hmac-attestation-v2".
|
||||
* - schema_version = "v2" added (additive).
|
||||
*/
|
||||
@Injectable()
|
||||
export class TeeInferenceOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'tee-inference',
|
||||
name: 'TEE Inference — confidential LLM inference with attestation (mock)',
|
||||
version: '0.1.0',
|
||||
name: 'TEE Inference — confidential LLM with HMAC-SHA256 attestation (v2)',
|
||||
version: '0.2.0',
|
||||
algorithm: 'tee-hmac-attestation-v2',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['prompt'],
|
||||
properties: {
|
||||
prompt: { type: 'string', minLength: 1 },
|
||||
model: { type: 'string', default: 'gpt-mock' },
|
||||
attestation_key: { type: 'string' },
|
||||
verify_key: { type: 'string' }, // optional override for verifier (testing wrong-key case)
|
||||
},
|
||||
additionalProperties: false,
|
||||
additionalProperties: true,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -24,13 +40,12 @@ export class TeeInferenceOperator implements Operator {
|
||||
const model = String(input?.model ?? 'gpt-mock');
|
||||
if (!prompt) return { ok: false, error: 'prompt required' };
|
||||
|
||||
// Real computation: sha256-based mock attestation.
|
||||
// Compute legacy attestation (always — backward compat with chg_0005 test).
|
||||
const hex = createHash('sha256').update(`${prompt}||${model}`).digest('hex');
|
||||
const attestation = `tee-mock-${hex.slice(0, 56)}`; // 9 + 56 = 65 chars, satisfies >= 64.
|
||||
|
||||
const attestation = `tee-mock-${hex.slice(0, 56)}`; // 9 + 56 = 65 chars
|
||||
const response = `mock:${model} echoes: ${prompt.slice(0, 64)}`;
|
||||
|
||||
return {
|
||||
const result: Record<string, any> = {
|
||||
ok: true,
|
||||
tee: {
|
||||
attestation,
|
||||
@@ -40,7 +55,41 @@ export class TeeInferenceOperator implements Operator {
|
||||
enclave: 'mock-sgx-equivalent',
|
||||
hash_full: hex,
|
||||
},
|
||||
schema_version: 'v2',
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
|
||||
// If attestation_key given, build proper quote and self-verify.
|
||||
const attestationKey = typeof input?.attestation_key === 'string'
|
||||
? input.attestation_key
|
||||
: '';
|
||||
if (attestationKey.length > 0) {
|
||||
const pcr0 = createHash('sha256').update(`${model}|${prompt}`).digest('hex');
|
||||
const pcr1 = createHash('sha256').update('enclave-code-v2').digest('hex');
|
||||
const nonce = randomBytes(16).toString('hex');
|
||||
const message = `${pcr0}|${pcr1}|${nonce}`;
|
||||
const signature = CryptoJS.HmacSHA256(message, attestationKey).toString(CryptoJS.enc.Hex);
|
||||
|
||||
// Self-verify using either provided verify_key (for wrong-key testing) or attestation_key.
|
||||
const verifyWith = typeof input?.verify_key === 'string' && input.verify_key.length > 0
|
||||
? input.verify_key
|
||||
: attestationKey;
|
||||
const expectedSig = CryptoJS.HmacSHA256(message, verifyWith).toString(CryptoJS.enc.Hex);
|
||||
const verifyCode = expectedSig === signature ? 0 : 1;
|
||||
|
||||
result.tee.attestation_key_used = attestationKey.length > 0 ? 'provided' : 'default';
|
||||
result.tee.quote = {
|
||||
pcr0,
|
||||
pcr1,
|
||||
nonce,
|
||||
signature,
|
||||
message,
|
||||
algorithm: 'HMAC-SHA256',
|
||||
};
|
||||
result.tee.verify_code = verifyCode;
|
||||
result.tee.verify_message = verifyCode === 0 ? 'signature matches' : 'signature mismatch';
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user