Files
admin 54781b3f83
ci / gate (push) Waiting to run
refactor: 项目改名 ssw-mpc,crate 统一到 mpc-* 前缀
- 项目与文档标题:nax-rs → ssw-mpc(尚数网-多方安全计算),二进制 nax → mpc
- 18 个 crate 及目录 nax-* → mpc-*,Rust 引用 nax_* → mpc_*
- 环境变量、C 符号、构建/部署脚本:NAX_* → MPC_*、libnax_ray.so → libmpc_ray.so
- 保留协议域分隔常量与 refs/nax 参考实现路径,维持 golden 向量与历史数据兼容
- 新增 [profile.dev] incremental=false
2026-11-05 15:06:12 +08:00

170 lines
6.3 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 真跑一遍「两方各自一个业务库 → mpc sql-join --pg-url → 密态聚合」。
#
# 为什么需要它:`mpc sql-join --pg-url` 涉及四段独立的东西——
# 自研 PG 协议客户端 / PSI 求交 / 列分片交换 / 密态聚合。
# 单元测试只能证明各段自洽(假服务端还是我们自己写的,等于自证);
# 这个脚本用 PGlite(真 Postgres,WASM 版)起两个独立库,把四段串起来验。
#
# 用法:
# tools/pg-dev/e2e.sh # 全自动
# tools/pg-dev/e2e.sh --keep # 跑完不关库,方便手工再看
#
# 退出码 0 = 全部断言通过。
set -euo pipefail
cd "$(dirname "$0")/../.." # 仓库根
ROOT=$(pwd)
PORT_A=${PORT_A:-5433}
PORT_B=${PORT_B:-5434}
JOIN_PORT=${JOIN_PORT:-9700}
KEEP=0
[ "${1:-}" = "--keep" ] && KEEP=1
MPC="$ROOT/target/debug/mpc"
URL_A="postgres://[email protected]:${PORT_A}/bank"
URL_B="postgres://[email protected]:${PORT_B}/shop"
Q_A="select uid, balance from customer order by uid"
Q_B="select uid, spend from orders order by uid"
PIDS=()
cleanup() {
if [ "$KEEP" = "1" ]; then
echo "== --keep:库仍在 $PORT_A / $PORT_B =="
return
fi
for p in ${PIDS[@]+"${PIDS[@]}"}; do
kill "$p" 2>/dev/null || true
done
}
trap cleanup EXIT
say() { printf '\n\033[1m== %s ==\033[0m\n' "$*"; }
fail() { printf '\033[31m断言失败:%s\033[0m\n' "$*"; exit 1; }
# ---------------------------------------------------------------- 前置
say "前置检查"
command -v node >/dev/null || fail "需要 node(PGlite 跑在 node 里)"
command -v python3 >/dev/null || fail "需要 python3(解析 JSON 断言)"
if [ ! -d tools/pg-dev/node_modules ]; then
echo " 安装 tools/pg-dev 依赖…"
(cd tools/pg-dev && npm install --no-fund --no-audit >/dev/null)
fi
echo " 编译 mpc(--features secure)…"
cargo build -q -p mpc-cli --features secure
[ -x "$MPC" ] || fail "没编出 $MPC"
# ---------------------------------------------------------------- 起库
say "起两个 PGlite(银行 $PORT_A / 商户 ${PORT_B})"
(cd tools/pg-dev && node server.mjs --port "$PORT_A" --init seed-bank.sql) &
PIDS+=($!)
(cd tools/pg-dev && node server.mjs --port "$PORT_B" --init seed-shop.sql) &
PIDS+=($!)
# 就绪探测:用 /dev/tcp 探端口,探到就说明 socket 已经在 listen
wait_port() {
local port=$1 n=0
until (exec 3<>"/dev/tcp/127.0.0.1/$port") 2>/dev/null; do
n=$((n + 1))
[ "$n" -gt 150 ] && fail "端口 ${port} 等不到(PGlite 没起来?)"
sleep 0.2
done
exec 3>&- 2>/dev/null || true
}
wait_port "$PORT_A"
wait_port "$PORT_B"
echo " 两个库都已 listen"
# 客户端这一侧先自证:连得上、查得出
echo " 客户端连通性:"
"$MPC" pg --url "$URL_A" --sql "select count(*) as n from customer" 2>&1 | sed 's/^/ /'
# ---------------------------------------------------------------- 第一轮
say "第一轮:无谓词,聚合 spend(乙方列)"
"$MPC" sql-join --site bob --sites alice,bob --listen "127.0.0.1:${JOIN_PORT}" \
--pg-url "$URL_B" --pg-query "$Q_B" \
--schema balance,spend --agg-col spend --report json \
>/tmp/mpc_e2e_bob1.json 2>/tmp/mpc_e2e_bob1.err &
BOB1=$!
sleep 1
"$MPC" sql-join --site alice --sites alice,bob --connect "127.0.0.1:${JOIN_PORT}" \
--pg-url "$URL_A" --pg-query "$Q_A" \
--schema balance,spend --agg-col spend --report json \
>/tmp/mpc_e2e_alice1.json 2>/tmp/mpc_e2e_alice1.err
wait "$BOB1" || true
# ---------------------------------------------------------------- 第二轮
say "第二轮:跨方谓词 WHERE spend >= 30,聚合 balance(甲方列)"
"$MPC" sql-join --site bob --sites alice,bob --listen "127.0.0.1:$((JOIN_PORT + 1))" \
--pg-url "$URL_B" --pg-query "$Q_B" \
--schema balance,spend --agg-col balance --pred "spend >= 30" --report json \
>/tmp/mpc_e2e_bob2.json 2>/tmp/mpc_e2e_bob2.err &
BOB2=$!
sleep 1
"$MPC" sql-join --site alice --sites alice,bob --connect "127.0.0.1:$((JOIN_PORT + 1))" \
--pg-url "$URL_A" --pg-query "$Q_A" \
--schema balance,spend --agg-col balance --pred "spend >= 30" --report json \
>/tmp/mpc_e2e_alice2.json 2>/tmp/mpc_e2e_alice2.err
wait "$BOB2" || true
# ---------------------------------------------------------------- 断言
say "断言"
python3 - <<'PY'
import json, sys
def load(path):
"""把 NDJSON 里 kind=table/kv 的对象摊平成 {key: value}。"""
out = {}
with open(path, encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
o = json.loads(line) # 结构不合法直接抛,也算失败
for row in o.get("rows", []):
if isinstance(row, list) and len(row) == 2:
out[str(row[0])] = row[1]
return out
def check(name, got, want, tol=1e-3):
if isinstance(want, str):
ok = str(got) == want
else:
ok = abs(float(got) - float(want)) <= tol
mark = "ok " if ok else "FAIL"
print(f" [{mark}] {name} = {got!r}(期望 {want!r})")
return ok
fails = 0
# 交集 = uid{1,2,3,4}(5 只在甲方、6 只在乙方,必须被 PSI 挡掉)
for side in ("alice", "bob"):
d = load(f"/tmp/mpc_e2e_{side}1.json")
fails += not check(f"{side} 交集行数", d["aligned_rows"], 4)
fails += not check(f"{side} COUNT", d["COUNT"], 4)
fails += not check(f"{side} SUM(spend)", d["SUM"], 140)
fails += not check(f"{side} MIN(spend)", d["MIN"], 20)
fails += not check(f"{side} MAX(spend)", d["MAX"], 50)
# 谓词 spend>=30 命中 uid{2,3,4},聚合的是 balance:400+300+900=1600
for side in ("alice", "bob"):
d = load(f"/tmp/mpc_e2e_{side}2.json")
fails += not check(f"{side} WHERE 后 COUNT", d["COUNT"], 3)
fails += not check(f"{side} WHERE 后 SUM(balance)", d["SUM"], 1600)
fails += not check(f"{side} WHERE 后 MIN(balance)", d["MIN"], 300)
fails += not check(f"{side} WHERE 后 MAX(balance)", d["MAX"], 900)
# 两方必须拿到同一份结果(不一致就是分片交换错位)
for r in ("1", "2"):
a = load(f"/tmp/mpc_e2e_alice{r}.json")
b = load(f"/tmp/mpc_e2e_bob{r}.json")
same = all(abs(float(a[k]) - float(b[k])) <= 1e-9 for k in ("COUNT", "SUM") if k in a)
print(f" [{'ok ' if same else 'FAIL'}] 第{r}轮 两方结果一致")
fails += not same
sys.exit(1 if fails else 0)
PY
say "通过"
echo " 两方各自一个真库、真 TCP、真 PSI,聚合值全部对得上。"