test(dth:sp1-more): T4 12 case e2e + operator 微调

This commit is contained in:
xstudio agent committed 2026-09-28 20:53:44 +08:00
1 parent 931f4c9a36
commit 113d76b4bc
5 files changed
+498 -61

No files matched your search

@@ -34,6 +34,29 @@ function resolvePath(obj: any, path: string): any {
return cur;
}
/**
* Resolve an attribute path against (subject, resource, env). If the path starts with
* one of "subject.", "resource.", or "env.", only that namespace is searched. Otherwise
* all three are tried (subject → resource → env).
*/
function resolveAttr(subject: any, resource: any, env: any, path: string): any {
if (path.startsWith('subject.')) {
return resolvePath(subject, path.slice('subject.'.length));
}
if (path.startsWith('resource.')) {
return resolvePath(resource, path.slice('resource.'.length));
}
if (path.startsWith('env.')) {
return resolvePath(env, path.slice('env.'.length));
}
// Bare path: try subject → resource → env.
let v = resolvePath(subject, path);
if (v !== undefined) return v;
v = resolvePath(resource, path);
if (v !== undefined) return v;
return resolvePath(env, path);
}
/** Convert "HH:MM" to total minutes. */
function parseTime(s: string): number {
const m = /^(\d{2}):(\d{2})$/.exec(s);
@@ -122,9 +145,7 @@ export class AbacPolicyOperator implements Operator {
if (allowIf) {
for (const [path, expected] of Object.entries(allowIf)) {
// Path can be prefixed with `subject.`, `resource.`, `env.`; otherwise try all three.
let actual: any = resolvePath(subject, path);
if (actual === undefined) actual = resolvePath(resource, path);
if (actual === undefined) actual = resolvePath(env, path);
const actual: any = resolveAttr(subject, resource, env, path);
if (actual === expected) {
allowMatchedKeys.push(path);
} else {
@@ -140,9 +161,7 @@ export class AbacPolicyOperator implements Operator {
let denyMatchedKeys: string[] = [];
if (denyIf) {
for (const [path, expected] of Object.entries(denyIf)) {
let actual: any = resolvePath(subject, path);
if (actual === undefined) actual = resolvePath(resource, path);
if (actual === undefined) actual = resolvePath(env, path);
const actual: any = resolveAttr(subject, resource, env, path);
if (actual === expected) {
denyMatches = true;
denyMatchedKeys.push(path);
@@ -167,7 +186,10 @@ export class AbacPolicyOperator implements Operator {
} else {
const startMin = parseInt(m[1], 10) * 60 + parseInt(m[2], 10);
const endMin = parseInt(m[3], 10) * 60 + parseInt(m[4], 10);
const cur = nowHHMM();
// Use env.time if provided, else current local time.
const cur = typeof env.time === 'string' && /^\d{2}:\d{2}$/.test(env.time)
? env.time
: nowHHMM();
const curMin = parseTime(cur);
const inWindow = startMin <= endMin
? curMin >= startMin && curMin <= endMin
@@ -40,7 +40,10 @@ function maskIdCard(value: string): string {
return `${head}${'*'.repeat(middleLen)}${tail}`;
}
/** Mask an email, e.g. "[email protected]" → "al**@example.com". */
/** Mask an email, e.g. "[email protected]" → "al**@example.com".
* Strategy: preserve first 2 chars of local-part + always exactly 2 asterisks + domain.
* For local-part length ≤ 2, mask entirely with "**".
*/
function maskEmail(value: string): string {
const at = value.indexOf('@');
if (at <= 0) return '*'.repeat(value.length);
@@ -48,7 +51,7 @@ function maskEmail(value: string): string {
const domain = value.slice(at); // includes '@'
if (local.length <= 2) return `**${domain}`;
const head = local.slice(0, 2);
return `${head}${'*'.repeat(Math.max(2, local.length - 2))}${domain}`;
return `${head}**${domain}`;
}
/** Mask a bank card number, e.g. "6222021234567890" → "**************7890". */
@@ -5,18 +5,16 @@ import { Operator, OperatorMeta } from '../operators';
* Model Watermark — Neural network weight LSB steganography (v2 real algorithm).
*
* Real-algorithm v2 implementation:
* - Encode each payload byte (8 bits) into 8 consecutive weight positions (4 weights per bit,
* where the 4th weight's LSB carries the bit).
* - Per-bit encoding:
* bit = 0 → set 4th weight's IEEE-754 last bit to 0
* bit = 1 → set 4th weight's IEEE-754 last bit to 1
* (Clearing LSB then adding bit*ulp works because the LSB is the lowest mantissa bit.)
* - Extraction: scan every 4th weight, read its LSB, reconstruct bytes.
* - Bit positions modified = payload.length * 8 (one bit per payload bit, in 4-weight blocks).
* - Each payload BIT (8 bits per byte) is encoded into the IEEE-754 LSB of one
* consecutive weight. MSB-first packing.
* - bit_positions_modified_count = payload_byte_length * 8.
* - Embedding clears the LSB of the target weight (via floor-to-LSB-grid), then
* adds bit * ulp (where ulp = 2^-52 is the LSB step for normalized doubles).
* - Extraction reads each weight's LSB, then packs bits MSB-first into bytes.
*
* v2 differences vs chg_0005 mock:
* - Real IEEE-754 last-bit manipulation (not a mock placeholder).
* - Roundtrip invariant: extract(watermarked_weights, length) === payload.
* - Roundtrip invariant: extract(watermarked_weights, byte_count) === payload.
* - meta.algorithm = "lsb-model-watermark-v2".
* - schema_version = "v2".
*/
@@ -24,25 +22,42 @@ import { Operator, OperatorMeta } from '../operators';
/** IEEE-754 double: a positive normal number's LSB is the 2^-52 mantissa bit. */
const LSB = Math.pow(2, -52);
/**
* Clear the LSB (last mantissa bit) of a positive finite number and return both
* the cleared value and the original LSB (0 or 1).
*/
function clearLSB(x: number): { cleared: number; lsb: number } {
if (!Number.isFinite(x)) return { cleared: x, lsb: 0 };
if (x === 0) return { cleared: 0, lsb: 0 };
const abs = Math.abs(x);
// Floor to the previous representable LSB step.
const cleared = Math.sign(x) * Math.floor(abs / LSB) * LSB;
const lsb = Math.round((abs - Math.floor(abs / LSB) * LSB) / LSB) & 1;
return { cleared, lsb };
// Use a Float64Array/DataView buffer to inspect/modify the raw 64-bit IEEE-754 layout.
// This avoids float-arithmetic round-off in the LSB manipulation (Math.floor * LSB is
// unreliable near representable grid boundaries).
const F64_BUF = new ArrayBuffer(8);
const F64_F64 = new Float64Array(F64_BUF);
const F64_U32 = new Uint32Array(F64_BUF); // [lo, hi] on little-endian
/** Read the LSB (bit 0 of mantissa) of x. */
function readLSB(x: number): number {
if (!Number.isFinite(x) || x === 0) return 0;
F64_F64[0] = x;
// Mantissa low 32 bits live in F64_U32[0] (little-endian). The LSB of the entire 52-bit
// mantissa is bit 0 of F64_U32[0] when the value is normalized; for denormals (|x| < 2^-1022)
// the mantissa is un-biased and the LSB is still bit 0 of F64_U32[0].
return F64_U32[0] & 1;
}
/** Set the LSB of x to bit (0 or 1) and return the modified value. */
function setLSB(cleared: number, bit: number): number {
/** Return x with the LSB (mantissa bit 0) forced to 0. */
function clearLSBValue(x: number): number {
if (!Number.isFinite(x) || x === 0) return 0;
F64_F64[0] = x;
F64_U32[0] = F64_U32[0] & 0xfffffffe; // clear bit 0
return F64_F64[0];
}
/** Return cleared (with LSB already 0) but with LSB forced to bit. */
function setLSBValue(cleared: number, bit: number): number {
if (!Number.isFinite(cleared)) return cleared;
if (cleared === 0) return bit * LSB;
return cleared + bit * LSB;
if (cleared === 0) {
F64_F64[0] = 0;
F64_U32[0] = (bit & 1);
return F64_F64[0];
}
F64_F64[0] = cleared;
F64_U32[0] = (F64_U32[0] & 0xfffffffe) | (bit & 1);
return F64_F64[0];
}
@Injectable()
@@ -59,10 +74,11 @@ export class ModelWatermarkOperator implements Operator {
type: 'array',
minItems: 1,
items: { type: 'number' },
description: 'flat list of model weights (must be multiple of 4 in length)',
description: 'flat list of model weights (length ≥ payload_byte_length * 8)',
},
payload: { type: 'string', description: 'payload string to embed (UTF-8)' },
action: { type: 'string', enum: ['embed', 'extract'], default: 'embed' },
byte_count: { type: 'integer', description: 'extract only: number of payload bytes to recover' },
},
required: ['weights'],
additionalProperties: true,
@@ -78,15 +94,14 @@ export class ModelWatermarkOperator implements Operator {
const payload = String(input?.payload ?? '');
if (!payload) return { ok: false, error: 'payload required for embed' };
// Convert payload to bytes.
// Convert payload to bytes (UTF-8).
const bytes = Array.from(Buffer.from(payload, 'utf-8'));
const bitsNeeded = bytes.length * 8;
const positionsNeeded = bitsNeeded * 4; // 4 weights per bit
if (positionsNeeded > weights.length) {
if (bitsNeeded > weights.length) {
return {
ok: false,
error: `weights too short: need ${positionsNeeded} (${bitsNeeded} bits × 4), have ${weights.length}`,
error: `weights too short: need ${bitsNeeded} (${bytes.length} bytes × 8), have ${weights.length}`,
};
}
@@ -94,16 +109,14 @@ export class ModelWatermarkOperator implements Operator {
const watermarked = weights.slice();
const modifiedPositions: number[] = [];
// For each byte, embed 8 bits MSB-first into consecutive weights.
for (let byteIdx = 0; byteIdx < bytes.length; byteIdx++) {
const byte = bytes[byteIdx];
for (let bitIdx = 0; bitIdx < 8; bitIdx++) {
// MSB first.
const bit = (byte >> (7 - bitIdx)) & 1;
const globalBitIdx = byteIdx * 8 + bitIdx;
// The 4th weight of the block (indices 3, 7, 11, …) carries this bit.
const weightIdx = globalBitIdx * 4 + 3;
const { cleared } = clearLSB(watermarked[weightIdx]);
watermarked[weightIdx] = setLSB(cleared, bit);
const weightIdx = byteIdx * 8 + bitIdx;
const cleared = clearLSBValue(watermarked[weightIdx]);
watermarked[weightIdx] = setLSBValue(cleared, bit);
modifiedPositions.push(weightIdx);
}
}
@@ -118,8 +131,7 @@ export class ModelWatermarkOperator implements Operator {
watermarked_weights: watermarked,
bit_positions_modified: modifiedPositions,
bit_positions_modified_count: modifiedPositions.length,
// Invariant: count = payload.length × 8 (assuming single-byte UTF-8; for multi-byte
// payloads use payload_byte_length × 8 — the e2e test uses ASCII).
// Invariant: count = payload_byte_length × 8 = len(payload) × 8 for ASCII payloads.
algorithm: 'lsb-model-watermark-v2',
},
schema_version: 'v2',
@@ -127,30 +139,26 @@ export class ModelWatermarkOperator implements Operator {
};
}
// extract mode: read LSB of every 4th weight.
// We need a `byte_count` to know how many bytes to extract.
const byteCount = Number(input?.byte_count ?? input?.payload_byte_length ?? 0);
// extract mode: read LSB of consecutive weights, MSB-first into bytes.
const byteCount = Number(input?.byte_count ?? 0);
if (!Number.isFinite(byteCount) || byteCount <= 0) {
return {
ok: false,
error: 'extract requires byte_count (or payload_byte_length)',
error: 'extract requires byte_count > 0',
};
}
const totalBits = byteCount * 8;
const neededPositions = totalBits * 4;
if (neededPositions > weights.length) {
if (totalBits > weights.length) {
return {
ok: false,
error: `weights too short for byte_count=${byteCount}: need ${neededPositions}, have ${weights.length}`,
error: `weights too short for byte_count=${byteCount}: need ${totalBits}, have ${weights.length}`,
};
}
const bits: number[] = [];
for (let i = 0; i < totalBits; i++) {
const weightIdx = i * 4 + 3;
const { lsb } = clearLSB(weights[weightIdx]);
bits.push(lsb);
bits.push(readLSB(weights[i]));
}
const bytes: number[] = [];
@@ -35,14 +35,15 @@ function evalPoly(secret: bigint, r: bigint, x: bigint, p: bigint): bigint {
/**
* Lagrange interpolation: recover f(0) given two shares (x1, y1), (x2, y2).
* f(0) = y1·(0-x2)/(x1-x2) + y2·(0-x1)/(x2-x1) mod p.
* Algebraically simplified to (y1·x2 - y2·x1) / (x2 - x1).
*/
function lagrangeAtZero(
x1: bigint, y1: bigint,
x2: bigint, y2: bigint,
p: bigint,
): bigint {
const numerator = (y1 * ((-x2) % p) + y2 * ((-x1) % p)) % p;
const denominator = ((x1 - x2) % p + p) % p;
const numerator = ((y1 * x2 - y2 * x1) % p + p) % p;
const denominator = ((x2 - x1) % p + p) % p;
const denomInv = modInverse(denominator, p);
return ((numerator * denomInv) % p + p) % p;
}
@@ -67,9 +68,9 @@ function deriveCoeff(seed: string): bigint {
return (BigInt('0x' + hex.slice(0, 16)) % (SHAMIR_P - 1n)) + 1n;
}
/** Encode BigInt as a fixed-width hex string (32 chars = 128 bits, fits easily in Mersenne). */
/** Encode BigInt as a hex string without leading-zero padding. */
function toHex(n: bigint): string {
return n.toString(16).padStart(16, '0');
return n.toString(16);
}
@Injectable()