- `demo/` 统一入口:server.py (Flask 30000→18850) + index.html (导航页 + 尚数网 logo + 3 段 L1/L2/L3/L4 卡片)
- 12 个子模块:psi / scql / pir / vertical_lr / horizontal_fedavg / dp_fedavg / secure_mlp / hetero_binning / policy_audit / hello / dry_run / k8s_two_party
- 每个子模块 `{__init__.py, app.py, index.html}` 蓝图模式
- 共享 `_shared/demo.css` 暗色主题
- 八大算子用 nax 真接口(Crypto PSI / Lattice PIR / Paillier 等)跑,跨三方不直接给用户看的中间数据
- PSI 重点演示:7 阶段动画 (hash → 自盲 → 传输 → 重盲 → 比较 → 求交 → 输出) + 6 列状态表 + manual/nax_data/compare 3 路径
- 场景:两家银行 VIP 客户姓名 + 存款场景,A 行 + B 行存款相加 = 共同客户总存款
- 启动:cd demo && python server.py → http://0.0.0.0:18850/
gitignore: 过滤 demo/**/screenshots/(playwright 验证产物)
81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
"""Hello MPC:三方密文算术入门。"""
|
||
from __future__ import annotations
|
||
import sys, time
|
||
from pathlib import Path
|
||
import numpy as np
|
||
from flask import Blueprint, jsonify, request
|
||
|
||
_NAX = Path(__file__).resolve().parents[2] / "nax"
|
||
if str(_NAX) not in sys.path: sys.path.insert(0, str(_NAX))
|
||
|
||
import nax
|
||
|
||
hello_bp = Blueprint("hello", __name__, url_prefix="/hello")
|
||
|
||
|
||
@hello_bp.route("/")
|
||
def index():
|
||
from flask import send_from_directory
|
||
return send_from_directory(Path(__file__).parent, "index.html")
|
||
|
||
|
||
@hello_bp.route("/api/run", methods=["POST"])
|
||
def api_run():
|
||
body = request.get_json(silent=True) or {}
|
||
a_vals = body.get("a") or [1.5, -2.0, 3.25, 0.5]
|
||
b_vals = body.get("b") or [2.0, 4.0, -1.0, 8.0]
|
||
c_vals = body.get("c") or [0.5, 1.5, 2.0, -0.5]
|
||
|
||
a = np.array(a_vals, dtype=float)
|
||
b = np.array(b_vals, dtype=float)
|
||
c = np.array(c_vals, dtype=float)
|
||
n = len(a)
|
||
|
||
out = {"meta": {"n": n, "parties": ["alice", "bob", "carol"]}, "stages": {}}
|
||
|
||
t0 = time.perf_counter()
|
||
try:
|
||
nax.init(["alice", "bob", "carol"], backend="local", seed=2024)
|
||
alice, bob, carol = nax.site("alice"), nax.site("bob"), nax.site("carol")
|
||
box = nax.vault([alice, bob, carol], seed=7)
|
||
|
||
# 阶段 1:share
|
||
x = box.share(alice.put(a))
|
||
y = box.share(bob.put(b))
|
||
z = box.share(carol.put(c))
|
||
|
||
out["stages"]["s1_share"] = {
|
||
"alice": a.tolist(), "bob": b.tolist(), "carol": c.tolist(),
|
||
"shares_x": {name: nax.get_cluster().fetch(h, force=True).tolist()[:3] for name, h in x.shares.items()},
|
||
}
|
||
|
||
# 阶段 2:密文运算 z = x*y + 1.0(参与方 carol 不参与)
|
||
z_op = x * y + 1.0
|
||
|
||
out["stages"]["s2_compute"] = {
|
||
"expression": "z = x·y + 1.0 (alice 的 x × bob 的 y + 公开常数 1.0)",
|
||
"intermediate": "全程密文,无明文出域",
|
||
}
|
||
|
||
# 阶段 3:reveal to alice
|
||
revealed = z_op.reveal(to="alice").fetch()
|
||
out["stages"]["s3_reveal"] = {
|
||
"reveal_to": "alice",
|
||
"value": revealed.tolist(),
|
||
}
|
||
|
||
# 阶段 4:明文对照
|
||
plain = (a * b + 1.0)
|
||
out["stages"]["s4_compare"] = {
|
||
"plain": plain.tolist(),
|
||
"max_deviation": float(np.max(np.abs(revealed - plain))),
|
||
}
|
||
except Exception as e:
|
||
return jsonify({"error": str(e)}), 500
|
||
finally:
|
||
try: nax.shutdown()
|
||
except: pass
|
||
|
||
out["meta"]["elapsed_ms"] = round((time.perf_counter() - t0) * 1000, 1)
|
||
return jsonify(out)
|