- docs/e2e: 将 DAG 演示面板(dag-panel.mjs/html/data.mjs/sh/DAG演示.command)用 git mv 归入 dag-panel/ 子目录;修正相对依赖 ../lib、../results/dag-runs;清理 _diag_*.mjs 等临时诊断脚本(git rm 已跟踪项,rm 未跟踪项) - web: 修复头部用户名称切账号不变(新增 utils/user.ts 响应式 userName,登录/挂载/路由切换时拉取 /oauth/CurrentUser) - .gitignore: 新增 __pycache__/*.pyc 忽略规则(排除 demo 构建产物) - demo: 多个演示脚本与算子集市用例更新 - .workbuddy/memory: 补充本次工作记录 注意:docs/e2e/sswflow 与 **/__pycache__/*.pyc 按要求排除,未纳入。
98 lines
3.6 KiB
Python
98 lines
3.6 KiB
Python
"""secure_compare 算子:密文比较 lt/gt/eq/select + max/argmax/amin/amax。
|
|
|
|
场景:PSI 找到共同客户 → 找出"共同客户中存款最高者",不泄露任何一方的存款明细。
|
|
- nax API: ``vault.lt/gt/eq/select/max/argmax/amin/amax`` (edaBit + BitLT 协议)
|
|
- 底层:dealer 离线发 64 位随机 r,c = x ⊕ r 统计独立于 x,信息论零泄露
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
import numpy as np
|
|
from flask import Blueprint, jsonify, request, send_from_directory
|
|
|
|
_NAX = Path(__file__).resolve().parents[2] / "nax"
|
|
if str(_NAX) not in sys.path: sys.path.insert(0, str(_NAX))
|
|
|
|
import nax
|
|
|
|
cmp_bp = Blueprint("secure_compare", __name__, url_prefix="/secure_compare")
|
|
|
|
|
|
@cmp_bp.route("/")
|
|
def index():
|
|
return send_from_directory(Path(__file__).parent, "index.html")
|
|
|
|
|
|
def _run(alice_deposits, bob_deposits, threshold=500.0):
|
|
"""alice_deposits = 共同客户在 A 行的存款;bob_deposits = B 行存款。
|
|
总存款 = alice + bob。"""
|
|
nax.init(["alice", "bob"], backend="local", seed=7)
|
|
alice, bob = nax.site("alice"), nax.site("bob")
|
|
box = nax.vault([alice, bob], seed=11)
|
|
|
|
a = np.array(alice_deposits, dtype=np.float64)
|
|
b = np.array(bob_deposits, dtype=np.float64)
|
|
total = a + b
|
|
|
|
a_sec = box.share(alice.put(a))
|
|
b_sec = box.share(bob.put(b))
|
|
total_sec = a_sec + b_sec
|
|
|
|
# ① 密文比较:total > threshold (0/1)
|
|
over = box.gt(total_sec, box.constant(threshold))
|
|
over_mask = box.reveal(over, to="alice").fetch().astype(int)
|
|
|
|
# ② 密文 select:超过阈值保留原值,否则 0
|
|
masked = box.select(over, total_sec, box.constant(0.0))
|
|
masked_arr = box.reveal(masked, to="alice").fetch()
|
|
|
|
# ③ 密文 amax(沿轴归约)
|
|
max_val = float(np.atleast_1d(box.amax(total_sec, axis=0).reveal(to="alice").fetch())[0])
|
|
|
|
# ④ 密文 argmax · argmin
|
|
argmax_res = box.argmax(total_sec, axis=0)
|
|
argmax_idx = int(argmax_res[1]) if isinstance(argmax_res, tuple) else int(argmax_res)
|
|
argmin_res = box.argmin(total_sec, axis=0)
|
|
argmin_idx = int(argmin_res[1]) if isinstance(argmin_res, tuple) else int(argmin_res)
|
|
|
|
# ⑤ 密文 eq
|
|
eq_max = box.eq(total_sec, box.constant(max_val))
|
|
eq_max_mask = box.reveal(eq_max, to="alice").fetch().astype(int)
|
|
|
|
nax.shutdown()
|
|
return {
|
|
"alice_count": len(a),
|
|
"bob_count": len(b),
|
|
"deposits_a": a.tolist(),
|
|
"deposits_b": b.tolist(),
|
|
"total": total.tolist(),
|
|
"threshold": threshold,
|
|
"over_threshold": over_mask.tolist(),
|
|
"over_count": int(over_mask.sum()),
|
|
"masked_total": [float(x) if m else 0.0 for x, m in zip(total, over_mask)],
|
|
"max_value": max_val,
|
|
"max_idx": argmax_idx,
|
|
"max_a": float(a[argmax_idx]),
|
|
"max_b": float(b[argmax_idx]),
|
|
"min_value": float(total[argmin_idx]),
|
|
"min_idx": argmin_idx,
|
|
"min_a": float(a[argmin_idx]),
|
|
"min_b": float(b[argmin_idx]),
|
|
"customers_eq_max": eq_max_mask.tolist(),
|
|
}
|
|
|
|
|
|
@cmp_bp.route("/api/run", methods=["POST"])
|
|
def api_run():
|
|
body = request.get_json(silent=True) or {}
|
|
a = body.get("alice") or []
|
|
b = body.get("bob") or []
|
|
th = float(body.get("threshold", 500))
|
|
if not a or not b or len(a) != len(b):
|
|
return jsonify({"error": "alice/bob 必须非空且等长"}), 400
|
|
try:
|
|
return jsonify(_run([float(x) for x in a], [float(x) for x in b], threshold=th))
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|