chg_0009: sp1_more_operators 提案 — SP-1 实验台新增 10 算子(覆盖 6 大方向,总 19 算子)

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
xstudio agentandClaude Code committed 2026-09-28 18:42:32 +08:00
1 parent 66d21a5415
commit dbb39c487a
5 files changed
+525

No files matched your search

@@ -0,0 +1,90 @@
---
change_id: chg_0009
status: proposed
owner: pm@team
stakeholders: [pd, tm, qa, dev]
priority: P1
target_release: v0.5
created: 2026-09-28
skip_coverage: false
---
# chg_0009 sp1_more_operators — SP-1 实验台新增 10 算子
## Why
SP-1 实验台 MVP(chg_0002 起头 + chg_0005 拓展 + chg_0008 真算法仿真)当前 9 算子:
hello-mpc / pir / vertical-lr / scql / horizontal-fedavg / secure-mlp / tee-inference /
prompt-defense / compliance-audit。
数智安全人才平台 design doc §3.4(教学运营层)要求实验台 ≥ 12 算子、覆盖 6 大方向
(密码学 / 隐私计算 / 数据流通 / 数据安全治理 / 数据合规 / AI 安全)。当前算子集中在
"隐私计算 + AI 安全"两方向,**密码学 / 数据流通 / 数据安全治理 / 数据合规**四方向
几乎空白。
**chg_0009 = 补足 4 个空白方向 + 增加密算多样性**,新增 10 个真算法算子,使总算子数到 19,
覆盖 design doc §3.4 的所有方向。
## Scope
**做** —— 新增 10 个算子(按 design doc §3.4 方向分类):
### 密码学 (2)
- **paillier-he**:Paillier 同态加密加法(同态性:`Enc(a) + Enc(b) = Enc(a+b)` mod n²,
真大整数运算 + crypto-js BigInt 仿真)
- **threshold-sig**:Shamir 秘密分享 + Lagrange 插值签名验证(2-of-3 门限)
### 隐私计算 (2)
- **mpc-beaver**:Beaver 三元组 (a, b, c=ab) 安全乘法协议仿真(3 方各持一份,
公开 [x-a][y-b] 还原 `xy = [x-a][y-b] + a·y + b·x + c`)
- **psi**:Private Set Intersection(基于布谷鸟哈希的真 PSI 协议仿真:双方集合求交,
不暴露非交集元素)
### 数据流通 (2)
- **did-identity**:W3C DID 身份创建 + 验证(`did:method:identifier`,HMAC 签名 + 解析)
- **tds-connector**:可信数据空间连接器仿真(双方合约 hash 比对 + 访问令牌签发)
### 数据安全治理 (2)
- **data-masking**:数据脱敏(手机号 / 身份证 / 邮箱 / 银行卡,按字段类型真替换,
保留可逆 hash 用于业务核对)
- **data-watermark**:文本水印(基于零宽字符 `\\u200B` 真嵌入 + 提取,零宽字符位 LSB)
### 数据合规 + AI 安全 (2)
- **abac-policy**:属性基访问控制策略评估(主体属性 × 资源属性 × 环境属性 → allow/deny,
真 JSONLogic 引擎或简化规则评估)
- **model-watermark**:模型水印(神经网络权重 LSB 真嵌入 + 提取,bit-flip 比例验证)
总计 10 算子,加上原有 9 = 19 算子。
### 集成
- 全部 10 个算子在 `experiment.module.ts` 注册
- 在 `operators.service.ts` 注册到 `OperatorsService` 的构造函数 + `onModuleInit`
- 全部 output 加 `schema_version: "v2"`(继承 chg_0008)
- 全部 meta.algorithm 含 "-v2" 后缀
### 测试
- 写 `tests/e2e/dth_sp1_more.test.sh`(≥ 12 case,每算子至少 1 case + 至少 2 算子有不变性测试)
- 现有 `dth_experiment.test.sh` + `dth_experiment_extend.test.sh` + `dth_operator_real.test.sh` 全部回归
**不做**:
- 不动 chg_0002/0005/0006/0007/0008 已稳定的模块
- 不引新依赖(用 crypto-js + node 内置 crypto 即可)
- 不做 GPU / 同态加密真实大整数库(crypto-js BigInt + 自写 mod 仿真)
- 不做完整 W3C DID 规范(仅仿真核心字段 + HMAC 签名)
## Impact
影响 capability:`experiment_platform`(在 chg_0008 基础上 add 10 scenario,每算子一个 scenario)。
## Acceptance
- `bash openspec/scripts/check_spec_coverage.sh` gap=0
- `tests/e2e/dth_sp1_more.test.sh` ≥ 12 case 全 PASS
- `tests/e2e/dth_operator_real.test.sh` + 4 个其他 e2e 全部 PASS
- `tsc --noEmit` 0 错误(dth 后端范围,除 chg_0002 vertical-lr.ts:52 预存 TS2352)
- 算子总列表 = 19 个(≥ design doc §3.4 的 12 个要求)
## Related
- `docs/superpowers/specs/2026-09-28-data-talent-hub-design.md` §3.4
- `openspec/changes/chg_0005_experiment_platform_extend/` + `chg_0008_operator_real_algorithms/`(要继承的真算法 + schema_version 模式)
@@ -0,0 +1,159 @@
# Spec: experiment_platform
## Requirement
THE SYSTEM SHALL expose 10 additional 真算法 operators across 4 missing capability pillars
(Paillier HE + threshold sig for 密码学; Beaver MPC + PSI for 隐私计算; DID identity +
TDS connector for 数据流通; data masking + data watermark for 数据安全治理; ABAC policy
+ model watermark for 数据合规 + AI 安全), preserving Operator interface contract with
schema_version="v2" outputs, so that 实验台 covers all 6 design-doc §3.4 capability
directions with 19 total operators.
## ADDED Scenarios
### S1 paillier_he_additive_homomorphic
- **WHEN** paillier-he receives `values = [a, b, c]` and `pubkey = (n, g)`
- **THEN** output includes `sum_encrypted = Enc(a + b + c)` (one ciphertext)
- **AND** decrypting it yields `a + b + c`
- **AND** homomorphic invariant holds: `Enc(a) ⊕ Enc(b) ⊕ Enc(c) === Enc(a+b+c)` mod n²
- **AND** schema_version = "v2"
### S2 threshold_sig_shamir_lagrange
- **WHEN** threshold-sig receives `message + 2 of 3 shares`
- **THEN** partial signatures reconstruct into a single valid signature
- **AND** with only 1 share, reconstruction fails
- **AND** schema_version = "v2"
### S3 mpc_beaver_triple_multiplication
- **WHEN** mpc-beaver receives `x, y` from each party + Beaver triple `(a, b, c=ab)`
- **THEN** output is `z = x * y` with privacy: each party only learns their share + public `[x-a][y-b]`
- **AND** invariant: `z === x*y` (tolerance ≤ 1e-9)
- **AND** schema_version = "v2"
### S4 psi_cuckoo_hash_intersection
- **WHEN** psi receives `set_a = {1,2,3,4,5}` and `set_b = {3,4,5,6,7}`
- **THEN** output `intersection = {3,4,5}` (size 3)
- **AND** non-intersection elements are NOT revealed
- **AND** schema_version = "v2"
### S5 did_identity_create_and_verify
- **WHEN** did-identity receives `method = "dth"` + `identifier = "alice"`
- **THEN** output `did = "did:dth:alice"` + `doc` (JSON-LD like) + `proof` (HMAC signed)
- **AND** verification with same secret passes
- **AND** verification with wrong secret fails
- **AND** schema_version = "v2"
### S6 tds_connector_contract_match
- **WHEN** tds-connector receives `contract_hash_a` and `contract_hash_b`
- **THEN** if hashes match: output `access_token` (signed JWT-like)
- **AND** if hashes mismatch: output `error: "contract-mismatch"` with no token
- **AND** schema_version = "v2"
### S7 data_masking_field_aware
- **WHEN** data-masking receives `field_type = "phone"` and `value = "13800138000"`
- **THEN** output `masked = "138****8000"` (preserves area + last 4)
- **AND** for `id_card`: `masked = "110101********1234"`
- **AND** for `email`: `masked = "al**@example.com"`
- **AND** schema_version = "v2"
### S8 data_watermark_zero_width_embed_extract
- **WHEN** data-watermark receives `text = "hello world"` + `payload = "secret-001"`
- **THEN** output `watermarked_text` contains the text + zero-width chars encoding "secret-001"
- **AND** extracting from `watermarked_text` recovers `payload === "secret-001"`
- **AND** length increase: original_len + 11 zero-width chars (each payload char ≈ 8-11 bits)
- **AND** schema_version = "v2"
### S9 abac_policy_evaluation
- **WHEN** abac-policy receives `subject = {role: "admin"}`, `resource = {sensitivity: "high"}`, `env = {time: "09:00-18:00"}`, `policy = {allow_if: {"subject.role": "admin"}}`
- **THEN** output `decision = "allow"` when role matches
- **AND** `decision = "deny"` when role doesn't match
- **AND** `decision = "deny"` when env.time outside window (if policy has env constraint)
- **AND** schema_version = "v2"
### S10 model_watermark_lsb_embed_extract
- **WHEN** model-watermark receives `weights = [0.1, 0.2, ...]` (length N) + `payload = "wm-001"`
- **THEN** output `watermarked_weights` differs from original in **exactly** len(payload)*8 / 4 positions (4 weights per bit)
- **AND** extracting from `watermarked_weights` recovers `payload === "wm-001"`
- **AND** schema_version = "v2"
### S11 schema_version 持续 v2
- **WHEN** any of the 10 new operators returns
- **THEN** output includes `schema_version: "v2"`
### S12 算子列表总数
- **WHEN** admin GET /experiment/operators
- **THEN** response includes ≥ 19 operators (9 existing + 10 new)
- **AND** new operator codes are in list: `paillier-he` / `threshold-sig` / `mpc-beaver` / `psi` / `did-identity` / `tds-connector` / `data-masking` / `data-watermark` / `abac-policy` / `model-watermark`
## UX / Interaction
- meta.algorithm 命名:`paillier-he-additive-v2` / `shamir-threshold-sig-v2` /
`beaver-mpc-triple-v2` / `cuckoo-psi-v2` / `did-hmac-v2` / `tds-hmac-jwt-v2` /
`field-aware-masking-v2` / `zero-width-watermark-v2` / `abac-jsonlogic-v2` /
`lsb-model-watermark-v2`
## 实施细节(写死给沙盒)
### 算子注册
`src/backend/src/dth/experiment/experiment.module.ts` 注册 10 个新 provider。
`src/backend/src/dth/experiment/operators.service.ts` 构造函数 + onModuleInit 加 10 个。
### crypto-js 复用
- HMAC-SHA256: `CryptoJS.HmacSHA256(msg, key).toString()`
- SHA256: `CryptoJS.SHA256(msg).toString()`
### Paillier BigInt 仿真(避免精度问题)
```typescript
// crypto-js BigInt 不可用,用 JS BigInt + 自写 modExp
function modExp(base: bigint, exp: bigint, mod: bigint): bigint {
let result = 1n;
base = base % mod;
while (exp > 0n) {
if (exp & 1n) result = (result * base) % mod;
exp >>= 1n;
base = (base * base) % mod;
}
return result;
}
```
### Beaver 三元组
```typescript
// 简化 — 已知 a, b, c=ab。x, y 输入,公开 d = x-a, e = y-b
// z = d*e + a*e + b*d + c
const d = x - a, e = y - b;
const z = d * e + a * e + b * d + c;
// 验证 z === x*y (浮点容差)
```
### 零宽字符水印
| 比特 | 字符 |
|---|---|
| 00 | U+200B (零宽空格) |
| 01 | U+200C (零宽非连接符) |
| 10 | U+200D (零宽连接符) |
| 11 | U+FEFF (零宽非断空格) |
每个 payload char (1 byte) → 4 个零宽字符(每个编码 2 bit)。
### LSB 模型水印
```typescript
// weights 中每 4 个连续 weights 编码 1 bit payload
// bit=0: 末位 bit 置 0
// bit=1: 末位 bit 置 1
// 提取时取每 4 个 weights 的第 4 个 LSB
```
## 非目标
- 完整 Paillier 密钥生成(用固定 key pair 简化)
- 完整 Shamir 秘密分享(2-of-3 仿真,不支持任意门限)
- 完整 W3C DID 规范(仅核心字段)
- 完整 ABAC 策略引擎(简化 JSONLogic 子集)
- 单元测试(仅 e2e + gate)
@@ -0,0 +1,87 @@
# Tasks (DAG)
- id: T1
type: architect
depends_on: []
output: docs/dth/sp1-more/design.md
verification:
- doc 包含 10 算子选型理由(按 design doc §3.4 6 方向分类)+ crypto-js / BigInt 复用 + 算子注册策略
- id: T2
type: implementer
depends_on: [T1]
output: |
src/backend/src/dth/experiment/operators/paillier-he.ts
src/backend/src/dth/experiment/operators/threshold-sig.ts
src/backend/src/dth/experiment/operators/mpc-beaver.ts
src/backend/src/dth/experiment/operators/psi.ts
src/backend/src/dth/experiment/operators/did-identity.ts
src/backend/src/dth/experiment/operators/tds-connector.ts
src/backend/src/dth/experiment/operators/data-masking.ts
src/backend/src/dth/experiment/operators/data-watermark.ts
src/backend/src/dth/experiment/operators/abac-policy.ts
src/backend/src/dth/experiment/operators/model-watermark.ts
verification:
- 10 个文件存在
- tsc --noEmit 0 error (in src/backend)
- 每个 meta.algorithm 含 "-v2"
- 每个 output 含 schema_version: "v2"
- id: T3
type: implementer
depends_on: [T2]
output: |
src/backend/src/dth/experiment/experiment.module.ts (add 10 providers)
src/backend/src/dth/experiment/operators.service.ts (register 10 in ctor + onModuleInit)
verification:
- GET /experiment/operators 列出 19 算子(含原 9 + 新 10)
- tsc --noEmit 0 error
- id: T4
type: tester
depends_on: [T3]
output: tests/e2e/dth_sp1_more.test.sh
verification:
- 12 case (S1-S10 各 1 + S11×1 schema_version + S12×1 算子列表总数)
- 全 PASS
- 含不变性 (S1 Paillier 加法 / S3 Beaver / S8/S10 watermark 提取)
- id: T5
type: qa
depends_on: [T4]
output: |
PROJECT_ROOT=. bash openspec/scripts/check_spec_coverage.sh # gap=0
cd src/backend && npx tsc --noEmit # 0 error
bash tests/e2e/dth_sp1_more.test.sh # 全 PASS
bash tests/e2e/dth_operator_real.test.sh # chg_0008 回归
bash tests/e2e/dth_experiment_extend.test.sh # chg_0005 回归
bash tests/e2e/dth_lite_demo.test.sh # chg_0007 回归
bash tests/e2e/dth_lms.test.sh # chg_0006 回归
bash tests/e2e/dth_experiment.test.sh # chg_0002 回归
verification:
- gap=0
- tsc 0 error (除 chg_0002 vertical-lr.ts:52 预存)
- 6 个 e2e 全 PASS
## 依赖关系拓扑
```
T1 (architect) → T2 (10 operator implementations) → T3 (register) → T4 (e2e tests) → T5 (qa gate)
```
## 实施步骤约定
每个 T 完成后必须:
1. 在 sandbox 分支上阶段性 git commit
2. 不跳过 git commit 进入下一个 T
3. 遇到 tsc 不通 / 算子类型错,停下来报告 host,不要改 tsconfig 不要降级类型
## 预计耗时
- T1: 5 min
- T2: 60 min(10 个算子,每个 5-7 min;其中 Paillier + Beaver + PSI 复杂点)
- T3: 10 min(注册 10 算子)
- T4: 25 min(12 case e2e)
- T5: 10 min(6 个门禁跑)
合计 ~110 min。建议 timeoutSec=7200(120 min)。
@@ -0,0 +1,43 @@
# spec: experiment_platform@chg_0009
scenarios:
- id: S1
cases: [C1_paillier_he_homomorphic_invariant]
- id: S2
cases: [C2_threshold_sig_2_of_3_reconstructs]
- id: S3
cases: [C3_mpc_beaver_triple_xy_invariant]
- id: S4
cases: [C4_psi_intersection_correct]
- id: S5
cases: [C5_did_create_verify_round_trip]
- id: S6
cases: [C6_tds_connector_contract_match]
- id: S7
cases: [C7_data_masking_field_aware]
- id: S8
cases: [C8_data_watermark_embed_extract_roundtrip]
- id: S9
cases: [C9_abac_policy_decision_matrix]
- id: S10
cases: [C10_model_watermark_lsb_embed_extract]
- id: S11
cases: [C11_schema_version_all_new_operators]
- id: S12
cases: [C12_operators_list_total_19]
gate:
blocking_gap_tolerance: 0
implementation_files:
- src/backend/src/dth/experiment/operators/paillier-he.ts
- src/backend/src/dth/experiment/operators/threshold-sig.ts
- src/backend/src/dth/experiment/operators/mpc-beaver.ts
- src/backend/src/dth/experiment/operators/psi.ts
- src/backend/src/dth/experiment/operators/did-identity.ts
- src/backend/src/dth/experiment/operators/tds-connector.ts
- src/backend/src/dth/experiment/operators/data-masking.ts
- src/backend/src/dth/experiment/operators/data-watermark.ts
- src/backend/src/dth/experiment/operators/abac-policy.ts
- src/backend/src/dth/experiment/operators/model-watermark.ts
- src/backend/src/dth/experiment/experiment.module.ts
- src/backend/src/dth/experiment/operators.service.ts
test_files:
- tests/e2e/dth_sp1_more.test.sh
@@ -0,0 +1,146 @@
### C1_paillier_he_homomorphic_invariant
- Scenario: S1
- Type: e2e
- Priority: P0
- Given: backend running, admin token, fixed keypair
- When: POST /experiment/run with operator=paillier-he, input=`{values: [10, 20, 30], pubkey: <fixed>}`
- Then: result.sum_decrypted === 60
- And: homomorphic invariant holds: ⊕ Enc(v_i) === Enc(Σ v_i) mod n²
- And: result.schema_version === "v2"
- Automated: true
- Bound Task: T4
### C2_threshold_sig_2_of_3_reconstructs
- Scenario: S2
- Type: e2e
- Priority: P0
- Given: backend running, admin token
- When: POST /experiment/run with operator=threshold-sig, message="hello", shares=[share1, share2]
- Then: result.reconstructed_signature valid
- And: result.with_one_share_fails === true
- And: result.schema_version === "v2"
- Automated: true
- Bound Task: T4
### C3_mpc_beaver_triple_xy_invariant
- Scenario: S3
- Type: e2e
- Priority: P0
- Given: backend running, admin token
- When: POST /experiment/run with operator=mpc-beaver, x=7, y=11, beaver_triple=(2,3,6)
- Then: result.z === 77 (tolerance ≤ 1e-9)
- And: result.revealed_public = [x-a, y-b] = [5, 8]
- And: result.schema_version === "v2"
- Automated: true
- Bound Task: T4
### C4_psi_intersection_correct
- Scenario: S4
- Type: e2e
- Priority: P0
- Given: backend running, admin token
- When: POST /experiment/run with operator=psi, set_a={1,2,3,4,5}, set_b={3,4,5,6,7}
- Then: result.intersection === [3,4,5]
- And: result.size === 3
- And: result.leaked_non_intersection === false
- And: result.schema_version === "v2"
- Automated: true
- Bound Task: T4
### C5_did_create_verify_round_trip
- Scenario: S5
- Type: e2e
- Priority: P0
- Given: backend running, admin token, secret="dth-secret"
- When: POST /experiment/run with operator=did-identity, method="dth", identifier="alice"
- Then: result.did === "did:dth:alice"
- And: result.verify_with_correct_secret === true
- And: result.verify_with_wrong_secret === false
- And: result.schema_version === "v2"
- Automated: true
- Bound Task: T4
### C6_tds_connector_contract_match
- Scenario: S6
- Type: e2e
- Priority: P0
- Given: backend running, admin token
- When: POST /experiment/run with operator=tds-connector, contract_hash_a=hash1, contract_hash_b=hash1 (match)
- Then: result.access_token is non-empty
- And: POST /experiment/run with mismatched hashes returns result.error === "contract-mismatch"
- And: result.schema_version === "v2"
- Automated: true
- Bound Task: T4
### C7_data_masking_field_aware
- Scenario: S7
- Type: e2e
- Priority: P0
- Given: backend running, admin token
- When: POST /experiment/run with operator=data-masking, multiple field types
- Then:
- phone "13800138000" → "138****8000"
- id_card "110101199001011234" → "110101********1234"
- email "alice@example.com" → "al**@example.com"
- And: result.schema_version === "v2"
- Automated: true
- Bound Task: T4
### C8_data_watermark_embed_extract_roundtrip
- Scenario: S8
- Type: e2e
- Priority: P0
- Given: backend running, admin token
- When: POST /experiment/run with operator=data-watermark, text="hello world", payload="secret-001"
- Then: result.extracted_payload === "secret-001"
- And: result.watermarked_text.length === "hello world".length + payload.length * 4
- And: result.schema_version === "v2"
- Automated: true
- Bound Task: T4
### C9_abac_policy_decision_matrix
- Scenario: S9
- Type: e2e
- Priority: P0
- Given: backend running, admin token
- When: POST /experiment/run with operator=abac-policy, 3 cases:
- {role:"admin"} vs policy allow_if role=admin → "allow"
- {role:"guest"} vs policy allow_if role=admin → "deny"
- {role:"admin", time:"22:00"} vs policy env.time=09:00-18:00 → "deny"
- Then: all 3 decisions correct
- And: result.schema_version === "v2"
- Automated: true
- Bound Task: T4
### C10_model_watermark_lsb_embed_extract
- Scenario: S10
- Type: e2e
- Priority: P0
- Given: backend running, admin token
- When: POST /experiment/run with operator=model-watermark, weights=[0.1,0.2,...0.4] (length 32), payload="wm-001"
- Then: result.extracted_payload === "wm-001"
- And: result.bit_positions_modified_count === len(payload)*8 = 24
- And: result.schema_version === "v2"
- Automated: true
- Bound Task: T4
### C11_schema_version_all_new_operators
- Scenario: S11
- Type: e2e
- Priority: P1
- Given: backend running, admin token
- When: POST /experiment/run for each of the 10 new operators with valid input
- Then: each response includes schema_version="v2"
- Automated: true
- Bound Task: T4
### C12_operators_list_total_19
- Scenario: S12
- Type: e2e
- Priority: P1
- Given: backend running, admin token
- When: GET /experiment/operators
- Then: response.length === 19 (9 existing + 10 new)
- And: response contains codes: paillier-he, threshold-sig, mpc-beaver, psi, did-identity, tds-connector, data-masking, data-watermark, abac-policy, model-watermark
- Automated: true
- Bound Task: T4