From 113d76b4bcc6fa7929c917bf1ca18ce32d3f9978 Mon Sep 17 00:00:00 2001 From: xstudio agent Date: Mon, 28 Sep 2026 20:53:44 +0800 Subject: [PATCH] =?UTF-8?q?test(dth:sp1-more):=20T4=2012=20case=20e2e=20+?= =?UTF-8?q?=20operator=20=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dth/experiment/operators/abac-policy.ts | 36 +- .../dth/experiment/operators/data-masking.ts | 7 +- .../experiment/operators/model-watermark.ts | 104 ++--- .../dth/experiment/operators/threshold-sig.ts | 9 +- tests/e2e/dth_sp1_more.test.sh | 403 ++++++++++++++++++ 5 files changed, 498 insertions(+), 61 deletions(-) create mode 100644 tests/e2e/dth_sp1_more.test.sh diff --git a/src/backend/src/dth/experiment/operators/abac-policy.ts b/src/backend/src/dth/experiment/operators/abac-policy.ts index c6b4795..e53d40c 100644 --- a/src/backend/src/dth/experiment/operators/abac-policy.ts +++ b/src/backend/src/dth/experiment/operators/abac-policy.ts @@ -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 diff --git a/src/backend/src/dth/experiment/operators/data-masking.ts b/src/backend/src/dth/experiment/operators/data-masking.ts index b8d16a9..86c17ec 100644 --- a/src/backend/src/dth/experiment/operators/data-masking.ts +++ b/src/backend/src/dth/experiment/operators/data-masking.ts @@ -40,7 +40,10 @@ function maskIdCard(value: string): string { return `${head}${'*'.repeat(middleLen)}${tail}`; } -/** Mask an email, e.g. "alice@example.com" → "al**@example.com". */ +/** Mask an email, e.g. "alice@example.com" → "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". */ diff --git a/src/backend/src/dth/experiment/operators/model-watermark.ts b/src/backend/src/dth/experiment/operators/model-watermark.ts index d10a668..bfb0125 100644 --- a/src/backend/src/dth/experiment/operators/model-watermark.ts +++ b/src/backend/src/dth/experiment/operators/model-watermark.ts @@ -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[] = []; diff --git a/src/backend/src/dth/experiment/operators/threshold-sig.ts b/src/backend/src/dth/experiment/operators/threshold-sig.ts index 3f973b0..b595d79 100644 --- a/src/backend/src/dth/experiment/operators/threshold-sig.ts +++ b/src/backend/src/dth/experiment/operators/threshold-sig.ts @@ -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() diff --git a/tests/e2e/dth_sp1_more.test.sh b/tests/e2e/dth_sp1_more.test.sh new file mode 100644 index 0000000..5ca4503 --- /dev/null +++ b/tests/e2e/dth_sp1_more.test.sh @@ -0,0 +1,403 @@ +#!/usr/bin/env bash +# dth_sp1_more.test.sh — e2e verification for S1..S12 of chg_0009 +# (10 new real-algorithm operators + schema_version巡检 + 算子总数19). +# +# Exit 0 only if all 12 cases pass. Each assertion is a hard-fail. +# +# Usage: +# DTH_BASE=http://127.0.0.1:30000 bash tests/e2e/dth_sp1_more.test.sh +# +# Pre-req: backend is running on DTH_BASE and admin seed is loaded. + +set -uo pipefail + +BASE="${DTH_BASE:-http://127.0.0.1:${PORT:-30000}}" +LOGIN_URL="$BASE/api/ssw/dth/auth/login" +OPS_URL="$BASE/api/ssw/dth/experiment/operators" +RUNS_URL="$BASE/api/ssw/dth/experiment/runs" + +pass=0; fail=0; fails=() + +json_get() { + local field="$1"; local body="$2" + python3 -c "import json,sys; d=json.loads(sys.argv[1]); print(d${field})" "$body" 2>/dev/null +} + +login() { + curl -s -X POST "$LOGIN_URL" -H 'Content-Type: application/json' -d "{\"username\":\"$1\",\"password\":\"$2\"}" +} + +assert() { + local id="$1"; local cond="$2"; local detail="$3" + if eval "$cond"; then + pass=$((pass+1)) + printf " ✓ %s\n" "$id" + else + fail=$((fail+1)) + fails+=("$id: $detail") + printf " ✗ %s — %s\n" "$id" "$detail" + fi +} + +# ----- pre: login admin ----- +ADMIN_BODY="$(login admin 123456)" +ADMIN_TOKEN="$(json_get "['data']['token']" "$ADMIN_BODY")" +assert "pre_admin_login" "[ -n '$ADMIN_TOKEN' ]" "admin token empty; login body=$ADMIN_BODY" + +# ============================================================ +# C1 (S1): paillier-he 加法同态不变性 +# values=[10,20,30], pubkey 固定 → sum_decrypted = 60, invariant_ok = True +# ============================================================ +PAI_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"paillier-he","input":{"values":[10,20,30],"seed":"paillier-c1"}}' "$RUNS_URL")" +PAI_ID="$(json_get "['data']['run_id']" "$PAI_BODY")" +PAI_STATUS="$(json_get "['data']['status']" "$PAI_BODY")" +assert "C1_paillier_run_status" "[ '$PAI_STATUS' = 'success' ]" "paillier status=$PAI_STATUS; body=$PAI_BODY" +PAI_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$PAI_ID")" +PAI_ASSERT="$(python3 -c " +import json,sys +d=json.loads(sys.argv[1]) +p=d['data']['output_json']['paillier'] +sv=d['data']['output_json']['schema_version'] +ok = (p['sum_decrypted'] == 60 and p['homomorphic_invariant_ok'] is True and sv == 'v2') +print('true' if ok else 'false', p.get('sum_decrypted'), p.get('homomorphic_invariant_ok'), sv) +" "$PAI_GET" 2>/dev/null)" +PAI_OK="$(echo "$PAI_ASSERT" | awk '{print $1}')" +assert "C1_paillier_homomorphic_invariant" "[ '$PAI_OK' = 'true' ]" "paillier invariant: $PAI_ASSERT; body=$PAI_GET" + +# ============================================================ +# C2 (S2): threshold-sig 2-of-3 Lagrange 重构 +# 2 share 重构成功;1 share 失败 +# ============================================================ +# Generate 3 Shamir shares for secret 0xdeadbeefcafe via Python helper +SHARES="$(python3 -c " +p = 0x1FFFFFFFFFFFFFFF # 2^61-1 +secret = 0xdeadbeefcafe +import hashlib +r = int(hashlib.sha256(b'threshold-sig-c2-seed').hexdigest()[:16], 16) % (p - 1) + 1 +shares = [] +for x in [1, 2, 3]: + y = (secret + r * x) % p + # Use minimal hex (no zero-padding) to match operator's toHex. + shares.append({'x': x, 'y': hex(y)[2:]}) +print(shares[0]['x'], shares[0]['y'], shares[1]['x'], shares[1]['y'], shares[2]['x'], shares[2]['y']) +")" +SX1="$(echo "$SHARES" | awk '{print $1}')" +SY1="$(echo "$SHARES" | awk '{print $2}')" +SX2="$(echo "$SHARES" | awk '{print $3}')" +SY2="$(echo "$SHARES" | awk '{print $4}')" +SX3="$(echo "$SHARES" | awk '{print $5}')" +SY3="$(echo "$SHARES" | awk '{print $6}')" +TS_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d "{\"operator\":\"threshold-sig\",\"input\":{\"message\":\"hello-threshold\",\"shares\":[{\"x\":$SX1,\"y\":\"$SY1\"},{\"x\":$SX2,\"y\":\"$SY2\"}],\"original_secret\":\"deadbeefcafe\"}}" "$RUNS_URL")" +TS_ID="$(json_get "['data']['run_id']" "$TS_BODY")" +TS_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$TS_ID")" +TS_ASSERT="$(python3 -c " +import json,sys +d=json.loads(sys.argv[1]) +t=d['data']['output_json']['threshold_sig'] +sv=d['data']['output_json']['schema_version'] +ok = (t['reconstructed_signature'] is not None and t['signature_match'] is True and sv == 'v2') +print('true' if ok else 'false') +" "$TS_GET" 2>/dev/null)" +assert "C2_threshold_sig_2shares_reconstruct" "[ '$TS_ASSERT' = 'true' ]" "2-share reconstruct: $TS_ASSERT; body=$TS_GET" + +# 1 share alone → with_one_share_fails = True +TS_ONE_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d "{\"operator\":\"threshold-sig\",\"input\":{\"message\":\"hello-threshold\",\"shares\":[{\"x\":$SX1,\"y\":\"$SY1\"}]}}" "$RUNS_URL")" +TS_ONE_ID="$(json_get "['data']['run_id']" "$TS_ONE_BODY")" +TS_ONE_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$TS_ONE_ID")" +TS_ONE_FAILS="$(json_get "['data']['output_json']['threshold_sig']['with_one_share_fails']" "$TS_ONE_GET")" +assert "C2_threshold_sig_1share_fails" "[ '$TS_ONE_FAILS' = 'True' ]" "1-share should fail; got with_one_share_fails=$TS_ONE_FAILS; body=$TS_ONE_GET" + +# ============================================================ +# C3 (S3): mpc-beaver z == xy (tolerance ≤ 1e-9) +# ============================================================ +BEAVER_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"mpc-beaver","input":{"x":7,"y":11,"a":2,"b":3,"c":6}}' "$RUNS_URL")" +BEAVER_ID="$(json_get "['data']['run_id']" "$BEAVER_BODY")" +BEAVER_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$BEAVER_ID")" +BEAVER_ASSERT="$(python3 -c " +import json,sys +d=json.loads(sys.argv[1]) +m=d['data']['output_json']['mpc'] +sv=d['data']['output_json']['schema_version'] +ok = (abs(m['z'] - 77.0) < 1e-9 and m['invariant_ok'] is True and abs(m['xy_ground_truth'] - 77.0) < 1e-9 and sv == 'v2') +print('true' if ok else 'false', m.get('z'), m.get('invariant_ok')) +" "$BEAVER_GET" 2>/dev/null)" +BEAVER_OK="$(echo "$BEAVER_ASSERT" | awk '{print $1}')" +assert "C3_beaver_xy_invariant" "[ '$BEAVER_OK' = 'true' ]" "beaver invariant: $BEAVER_ASSERT; body=$BEAVER_GET" + +# ============================================================ +# C4 (S4): psi cuckoo PSI intersection = {3,4,5} +# ============================================================ +PSI_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"psi","input":{"set_a":[1,2,3,4,5],"set_b":[3,4,5,6,7],"hash_key":"psi-c4"}}' "$RUNS_URL")" +PSI_ID="$(json_get "['data']['run_id']" "$PSI_BODY")" +PSI_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$PSI_ID")" +PSI_ASSERT="$(python3 -c " +import json,sys +d=json.loads(sys.argv[1]) +p=d['data']['output_json']['psi'] +sv=d['data']['output_json']['schema_version'] +ok = (sorted(p['intersection']) == ['3','4','5'] and p['size'] == 3 and p['leaked_non_intersection'] is False and sv == 'v2') +print('true' if ok else 'false', p.get('intersection'), p.get('size')) +" "$PSI_GET" 2>/dev/null)" +PSI_OK="$(echo "$PSI_ASSERT" | awk '{print $1}')" +assert "C4_psi_intersection_correct" "[ '$PSI_OK' = 'true' ]" "psi intersection: $PSI_ASSERT; body=$PSI_GET" + +# ============================================================ +# C5 (S5): did-identity create + verify +# ============================================================ +DID_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"did-identity","input":{"method":"dth","identifier":"alice","secret":"dth-secret-c5"}}' "$RUNS_URL")" +DID_ID="$(json_get "['data']['run_id']" "$DID_BODY")" +DID_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$DID_ID")" +DID_ASSERT="$(python3 -c " +import json,sys +d=json.loads(sys.argv[1]) +x=d['data']['output_json']['did'] +sv=d['data']['output_json']['schema_version'] +ok = (x['did'] == 'did:dth:alice' and x['verify_with_correct_secret'] is True and x['verify_with_wrong_secret'] is False and sv == 'v2') +print('true' if ok else 'false') +" "$DID_GET" 2>/dev/null)" +assert "C5_did_create_verify" "[ '$DID_ASSERT' = 'true' ]" "did verify: $DID_ASSERT; body=$DID_GET" + +# ============================================================ +# C6 (S6): tds-connector match → token; mismatch → error +# ============================================================ +TDS_MATCH_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"tds-connector","input":{"contract_hash_a":"contract-v1","contract_hash_b":"contract-v1","subject":"party-a","scope":"read"}}' "$RUNS_URL")" +TDS_MATCH_ID="$(json_get "['data']['run_id']" "$TDS_MATCH_BODY")" +TDS_MATCH_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$TDS_MATCH_ID")" +TDS_MATCH_TOKEN="$(json_get "['data']['output_json']['tds']['access_token']" "$TDS_MATCH_GET")" +TDS_MATCH_SCHEMA="$(json_get "['data']['output_json']['schema_version']" "$TDS_MATCH_GET")" +assert "C6_tds_match_token_nonempty" "[ -n '$TDS_MATCH_TOKEN' ]" "tds match token empty; body=$TDS_MATCH_GET" +assert "C6_tds_match_schema_v2" "[ '$TDS_MATCH_SCHEMA' = 'v2' ]" "tds match schema=$TDS_MATCH_SCHEMA" + +# Verify JWT-like token structure externally (header.payload.signature) +TDS_TOKEN_PARTS="$(echo "$TDS_MATCH_TOKEN" | tr '.' '\n' | wc -l)" +assert "C6_tds_match_token_3parts" "[ '$TDS_TOKEN_PARTS' = '3' ]" "expected 3-part JWT-like token, got $TDS_TOKEN_PARTS parts" + +TDS_MISMATCH_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"tds-connector","input":{"contract_hash_a":"contract-v1","contract_hash_b":"contract-different"}}' "$RUNS_URL")" +TDS_MISMATCH_ID="$(json_get "['data']['run_id']" "$TDS_MISMATCH_BODY")" +TDS_MISMATCH_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$TDS_MISMATCH_ID")" +TDS_MISMATCH_ERR="$(json_get "['data']['output_json']['tds']['error']" "$TDS_MISMATCH_GET")" +assert "C6_tds_mismatch_error" "[ '$TDS_MISMATCH_ERR' = 'contract-mismatch' ]" "expected contract-mismatch, got $TDS_MISMATCH_ERR; body=$TDS_MISMATCH_GET" + +# ============================================================ +# C7 (S7): data-masking field-aware +# phone / id_card / email 三种字段 +# ============================================================ +MASK_PHONE_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"data-masking","input":{"field_type":"phone","value":"13800138000"}}' "$RUNS_URL")" +MASK_PHONE_ID="$(json_get "['data']['run_id']" "$MASK_PHONE_BODY")" +MASK_PHONE_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$MASK_PHONE_ID")" +MASK_PHONE_OUT="$(json_get "['data']['output_json']['masking']['masked']" "$MASK_PHONE_GET")" +assert "C7_mask_phone" "[ '$MASK_PHONE_OUT' = '138****8000' ]" "phone masked=$MASK_PHONE_OUT; body=$MASK_PHONE_GET" + +MASK_ID_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"data-masking","input":{"field_type":"id_card","value":"110101199001011234"}}' "$RUNS_URL")" +MASK_ID_ID="$(json_get "['data']['run_id']" "$MASK_ID_BODY")" +MASK_ID_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$MASK_ID_ID")" +MASK_ID_OUT="$(json_get "['data']['output_json']['masking']['masked']" "$MASK_ID_GET")" +assert "C7_mask_id_card" "[ '$MASK_ID_OUT' = '110101********1234' ]" "id_card masked=$MASK_ID_OUT; body=$MASK_ID_GET" + +MASK_EMAIL_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"data-masking","input":{"field_type":"email","value":"alice@example.com"}}' "$RUNS_URL")" +MASK_EMAIL_ID="$(json_get "['data']['run_id']" "$MASK_EMAIL_BODY")" +MASK_EMAIL_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$MASK_EMAIL_ID")" +MASK_EMAIL_OUT="$(json_get "['data']['output_json']['masking']['masked']" "$MASK_EMAIL_GET")" +assert "C7_mask_email" "[ '$MASK_EMAIL_OUT' = 'al**@example.com' ]" "email masked=$MASK_EMAIL_OUT; body=$MASK_EMAIL_GET" + +MASK_SCHEMA="$(json_get "['data']['output_json']['schema_version']" "$MASK_PHONE_GET")" +assert "C7_mask_schema_v2" "[ '$MASK_SCHEMA' = 'v2' ]" "schema=$MASK_SCHEMA" + +# ============================================================ +# C8 (S8): data-watermark zero-width embed + extract roundtrip +# ============================================================ +WM_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"data-watermark","input":{"text":"hello world","payload":"secret-001","action":"embed"}}' "$RUNS_URL")" +WM_ID="$(json_get "['data']['run_id']" "$WM_BODY")" +WM_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$WM_ID")" +WM_ASSERT="$(python3 -c " +import json,sys +d=json.loads(sys.argv[1]) +w=d['data']['output_json']['watermark'] +sv=d['data']['output_json']['schema_version'] +expected_len = len('hello world') + len('secret-001') * 4 +ok = (w['length_invariant_ok'] is True and len(w['watermarked_text']) == expected_len and w['zero_width_chars_appended'] == len('secret-001') * 4 and sv == 'v2') +print('true' if ok else 'false', len(w['watermarked_text']), w['zero_width_chars_appended']) +" "$WM_GET" 2>/dev/null)" +WM_OK="$(echo "$WM_ASSERT" | awk '{print $1}')" +assert "C8_watermark_embed_length_invariant" "[ '$WM_OK' = 'true' ]" "watermark embed: $WM_ASSERT; body=$WM_GET" + +# Extract roundtrip: read watermarked_text via stdin to avoid shell quoting hell. +WM_GET_FILE="/tmp/dth_sp1_more_wm_get.json" +printf '%s' "$WM_GET" > "$WM_GET_FILE" +WM_TEXT_FILE="/tmp/dth_sp1_more_wm_text.txt" +python3 -c " +import json, sys +d=json.load(open(sys.argv[1])) +sys.stdout.write(d['data']['output_json']['watermark']['watermarked_text']) +" "$WM_GET_FILE" > "$WM_TEXT_FILE" + +# Build the extract request payload via python (handles unicode correctly) +WM_EXT_REQ_FILE="/tmp/dth_sp1_more_wm_extract.json" +python3 < "$WM_EXT_REQ_FILE" +import json +with open("$WM_TEXT_FILE",'r') as f: + text = f.read() +print(json.dumps({'operator':'data-watermark','input':{'text':text,'action':'extract'}})) +PYEOF +WM_EXT_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + --data-binary "@$WM_EXT_REQ_FILE" "$RUNS_URL")" +WM_EXT_ID="$(json_get "['data']['run_id']" "$WM_EXT_BODY")" +WM_EXT_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$WM_EXT_ID")" +WM_EXT_OUT="$(json_get "['data']['output_json']['watermark']['extracted_payload']" "$WM_EXT_GET")" +assert "C8_watermark_extract_roundtrip" "[ '$WM_EXT_OUT' = 'secret-001' ]" "watermark extract=$WM_EXT_OUT; body=$WM_EXT_GET" + +# ============================================================ +# C9 (S9): abac-policy decision matrix (3 cases) +# ============================================================ +ABAC_ADMIN_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"abac-policy","input":{"subject":{"role":"admin"},"resource":{"sensitivity":"high"},"env":{},"policy":{"allow_if":{"subject.role":"admin"}}}}' "$RUNS_URL")" +ABAC_ADMIN_ID="$(json_get "['data']['run_id']" "$ABAC_ADMIN_BODY")" +ABAC_ADMIN_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ABAC_ADMIN_ID")" +ABAC_ADMIN_DECISION="$(json_get "['data']['output_json']['abac']['decision']" "$ABAC_ADMIN_GET")" +assert "C9_abac_admin_allow" "[ '$ABAC_ADMIN_DECISION' = 'allow' ]" "admin should be allowed; got=$ABAC_ADMIN_DECISION; body=$ABAC_ADMIN_GET" + +ABAC_GUEST_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"abac-policy","input":{"subject":{"role":"guest"},"resource":{"sensitivity":"high"},"env":{},"policy":{"allow_if":{"subject.role":"admin"}}}}' "$RUNS_URL")" +ABAC_GUEST_ID="$(json_get "['data']['run_id']" "$ABAC_GUEST_BODY")" +ABAC_GUEST_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ABAC_GUEST_ID")" +ABAC_GUEST_DECISION="$(json_get "['data']['output_json']['abac']['decision']" "$ABAC_GUEST_GET")" +assert "C9_abac_guest_deny" "[ '$ABAC_GUEST_DECISION' = 'deny' ]" "guest should be denied; got=$ABAC_GUEST_DECISION; body=$ABAC_GUEST_GET" + +# Env constraint violation: outside time window. Pass env.time="22:00", +# policy has env_constraints.time_window="09:00-09:01" (tiny window). +ABAC_TIME_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"abac-policy","input":{"subject":{"role":"admin"},"resource":{"sensitivity":"high"},"env":{"time":"22:00"},"policy":{"allow_if":{"subject.role":"admin"},"env_constraints":{"time_window":"09:00-09:01"}}}}' "$RUNS_URL")" +ABAC_TIME_ID="$(json_get "['data']['run_id']" "$ABAC_TIME_BODY")" +ABAC_TIME_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ABAC_TIME_ID")" +ABAC_TIME_DECISION="$(json_get "['data']['output_json']['abac']['decision']" "$ABAC_TIME_GET")" +assert "C9_abac_timewindow_deny" "[ '$ABAC_TIME_DECISION' = 'deny' ]" "outside-window should be denied; got=$ABAC_TIME_DECISION; body=$ABAC_TIME_GET" + +ABAC_SCHEMA="$(json_get "['data']['output_json']['schema_version']" "$ABAC_ADMIN_GET")" +assert "C9_abac_schema_v2" "[ '$ABAC_SCHEMA' = 'v2' ]" "schema=$ABAC_SCHEMA" + +# ============================================================ +# C10 (S10): model-watermark LSB embed + extract roundtrip +# payload "wmX" (3 ASCII chars = 24 bits) → 24 positions modified, +# weights length 32, fits 24 bits. +# ============================================================ +MW_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d '{"operator":"model-watermark","input":{"weights":[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,3.0,3.1,3.2],"payload":"wmX","action":"embed"}}' "$RUNS_URL")" +MW_ID="$(json_get "['data']['run_id']" "$MW_BODY")" +MW_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$MW_ID")" +MW_ASSERT="$(python3 -c " +import json,sys +d=json.loads(sys.argv[1]) +w=d['data']['output_json']['watermark'] +sv=d['data']['output_json']['schema_version'] +ww = w['watermarked_weights'] +expected_mod = len('wmX') * 8 # 24 +ok = (w['bit_positions_modified_count'] == expected_mod and len(ww) == 32 and sv == 'v2') +print('true' if ok else 'false', w.get('bit_positions_modified_count'), len(ww), sv) +" "$MW_GET" 2>/dev/null)" +MW_OK="$(echo "$MW_ASSERT" | awk '{print $1}')" +assert "C10_model_watermark_embed_invariant" "[ '$MW_OK' = 'true' ]" "model watermark embed: $MW_ASSERT; body=$MW_GET" + +# Extract roundtrip from watermarked_weights with byte_count=3 +MW_GET_FILE="/tmp/dth_sp1_more_mw_get.json" +printf '%s' "$MW_GET" > "$MW_GET_FILE" +MW_WW_FILE="/tmp/dth_sp1_more_mw_ww.json" +python3 -c " +import json, sys +d=json.load(open(sys.argv[1])) +sys.stdout.write(json.dumps(d['data']['output_json']['watermark']['watermarked_weights'])) +" "$MW_GET_FILE" > "$MW_WW_FILE" +MW_EXT_REQ_FILE="/tmp/dth_sp1_more_mw_extract.json" +python3 < "$MW_EXT_REQ_FILE" +import json +with open("$MW_WW_FILE",'r') as f: + ww = json.load(f) +print(json.dumps({'operator':'model-watermark','input':{'weights':ww,'action':'extract','byte_count':3}})) +PYEOF +MW_EXT_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + --data-binary "@$MW_EXT_REQ_FILE" "$RUNS_URL")" +MW_EXT_ID="$(json_get "['data']['run_id']" "$MW_EXT_BODY")" +MW_EXT_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$MW_EXT_ID")" +MW_EXT_OUT="$(json_get "['data']['output_json']['watermark']['extracted_payload']" "$MW_EXT_GET")" +assert "C10_model_watermark_extract_roundtrip" "[ '$MW_EXT_OUT' = 'wmX' ]" "model extract=$MW_EXT_OUT; body=$MW_EXT_GET" + +# ============================================================ +# C11 (S11): schema_version = v2 持续 — 全 10 个新算子 +# ============================================================ +C11_OK=1 +C11_DETAIL="" +for OP in paillier-he threshold-sig mpc-beaver psi did-identity tds-connector data-masking data-watermark abac-policy model-watermark; do + case "$OP" in + paillier-he) INPUT='{"values":[1,2],"seed":"c11"}' ;; + threshold-sig) INPUT='{"message":"c11","shares":[{"x":1,"y":"000000000000002a"},{"x":2,"y":"0000000000000054"}]}' ;; + mpc-beaver) INPUT='{"x":2,"y":3,"a":1,"b":1,"c":1}' ;; + psi) INPUT='{"set_a":[1,2],"set_b":[2,3],"hash_key":"c11"}' ;; + did-identity) INPUT='{"method":"dth","identifier":"c11","secret":"c11"}' ;; + tds-connector) INPUT='{"contract_hash_a":"c11","contract_hash_b":"c11"}' ;; + data-masking) INPUT='{"field_type":"phone","value":"13800138000"}' ;; + data-watermark) INPUT='{"text":"c11","payload":"c11","action":"embed"}' ;; + abac-policy) INPUT='{"subject":{"role":"admin"},"resource":{},"env":{},"policy":{"allow_if":{"subject.role":"admin"}}}' ;; + model-watermark) INPUT='{"weights":[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0,2.1,2.2,2.3,2.4],"payload":"c11","action":"embed"}' ;; + esac + C11_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \ + -d "{\"operator\":\"$OP\",\"input\":$INPUT}" "$RUNS_URL")" + C11_RID="$(json_get "['data']['run_id']" "$C11_BODY")" + C11_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$C11_RID")" + C11_SV="$(json_get "['data']['output_json']['schema_version']" "$C11_GET")" + if [ "$C11_SV" != "v2" ]; then + C11_OK=0 + C11_DETAIL="$C11_DETAIL $OP=$C11_SV" + fi +done +assert "C11_schema_version_v2_all_10" "[ '$C11_OK' = '1' ]" "schema_version巡检未全部 v2:$C11_DETAIL" + +# ============================================================ +# C12 (S12): 算子总数 = 19(含全部 10 个新算子) +# ============================================================ +LIST_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$OPS_URL")" +LIST_ASSERT="$(python3 -c " +import json,sys +ops=json.loads(sys.argv[1])['data'] +codes={o['code'] for o in ops} +required={'paillier-he','threshold-sig','mpc-beaver','psi','did-identity','tds-connector','data-masking','data-watermark','abac-policy','model-watermark'} +missing=required - codes +ok = (len(ops) >= 19 and not missing) +print('true' if ok else 'false', len(ops), sorted(missing)) +" "$LIST_BODY" 2>/dev/null)" +LIST_OK="$(echo "$LIST_ASSERT" | awk '{print $1}')" +assert "C12_operators_list_19" "[ '$LIST_OK' = 'true' ]" "operator list: $LIST_ASSERT" + +# Verify all 10 new algorithms have -v2 suffix in their meta.algorithm +ALG_ASSERT="$(python3 -c " +import json,sys +ops=json.loads(sys.argv[1])['data'] +new_codes={'paillier-he','threshold-sig','mpc-beaver','psi','did-identity','tds-connector','data-masking','data-watermark','abac-policy','model-watermark'} +bad=[] +for o in ops: + if o['code'] in new_codes: + a=o.get('algorithm','') + if not a.endswith('-v2'): + bad.append(o['code']+'='+a) +print('true' if not bad else 'false', '|'.join(bad)) +" "$LIST_BODY" 2>/dev/null)" +ALG_OK="$(echo "$ALG_ASSERT" | awk '{print $1}')" +assert "C12_algorithms_v2_suffix" "[ '$ALG_OK' = 'true' ]" "alg v2 suffix: $ALG_ASSERT" + +echo "---" +echo "e2e dth_sp1_more: pass=$pass fail=$fail" +if [ $fail -gt 0 ]; then + printf 'FAILED:\n'; printf ' - %s\n' "${fails[@]}" + exit 1 +fi +exit 0