Files
ssw-mpc/tools/bench_numpy.py
T
ssw c5b2cf69a5 feat: nax-rs P0/P1/P1.5 —— Rust 重写隐私计算框架
算子核(nax-core):零依赖 no_std
- Z_2^64 定点算术 + AVX2 矩阵乘,与 Python 参考实现 215 条逐位一致
- 加性分享 / Beaver 三元组 / 截断对 / edaBit,ChaCha20 PRG,SHA-256
- 自实现银行家舍入(f64::round_ties_even 是 std-only,no_std 编不过)

协议栈
- nax-proto:手写帧协议(12 字节头 + u64 payload),无 protobuf 依赖
- nax-vault:Beaver 乘 / 定点截断 / open / reveal
- nax-node:TCP 传输 + 密文会话 + PSI 会话
- nax-psi:X25519 ECDH-PSI(唯一外部依赖 dalek,因恒定时间约束)
- nax-tee:HAL trait(seal/unseal/attest/sign)+ Mock + TrustPlan 四级降级

验收
- 83 用例全绿;armv7 全 workspace 编译通过
- 两进程密文计算:(1·4+1)+(2·5+1)+(3·6+1) = 35
- 两进程 PSI:[1..5] ∩ [3..7] = {3,4,5}
- 单二进制 450 KB,零运行时依赖

文档
- docs/ARCHITECTURE.md:分层、Ray 定位、TEE 分工与降级梯度
- docs/ZYNQ.md:A9 性能预算(比 i5 慢 8-12x,非 50x)与部署形态
2026-10-29 11:10:54 +08:00

83 lines
2.5 KiB
Python
Raw 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 python3
"""numpy/nax 参考实现的微基准,与 `crates/nax-core/examples/bench.rs` 对齐。
同一个机器、同样规模、同样算子,唯一变量是"numpy 的 u64 路径" vs
"Rust 的 u64 路径"。这个对比的意义在于:ML 框架里所有性能优化都建立在
浮点 BLAS 上,而 MPC 用的是 **u64 环算术** —— BLAS 完全帮不上忙,
numpy 只能走通用迭代器循环。这正是 Rust 算子核最大的价值来源。
用法:
.venv/bin/python tools/bench_numpy.py
"""
from __future__ import annotations
import importlib.util
import pathlib
import time
import numpy as np
ROOT = pathlib.Path(__file__).resolve().parent.parent
REF_RING = ROOT / "refs" / "nax" / "nax" / "crypto" / "ring.py"
def load_ring():
spec = importlib.util.spec_from_file_location("ref_ring", REF_RING)
mod = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(mod)
return mod
def bench(label: str, iters: int, fn) -> float:
fn() # warmup
best = float("inf")
for _ in range(iters):
t = time.perf_counter()
fn()
dt = time.perf_counter() - t
best = min(best, dt)
print(f"{label:<42} {best * 1e3:>10.3f} ms")
return best * 1e3
def main() -> int:
ring = load_ring()
rng = np.random.default_rng(0xC0FFEE)
print("numpy 参考实现微基准")
print(f"numpy {np.__version__}")
print("=" * 30)
print()
print("[矩阵乘 u64 @ u64(mod 2^64)]")
for n in (64, 128, 256):
a = rng.integers(0, 1 << 63, size=(n, n), dtype=np.uint64)
b = rng.integers(0, 1 << 63, size=(n, n), dtype=np.uint64)
iters = 20 if n <= 64 else (5 if n <= 128 else 2)
with np.errstate(over="ignore"):
ms = bench(f" matmul {n}x{n}x{n}", iters, lambda: ring.matmul(a, b))
ops = n**3
print(f" -> {ops / (ms / 1e3) / 1e9:.2f} Gop/s")
print()
print("[逐元素算子,16M 元素]")
length = 16 * 1024 * 1024
a = rng.integers(0, 1 << 63, size=length, dtype=np.uint64)
b = rng.integers(0, 1 << 63, size=length, dtype=np.uint64)
for name, fn in (
("mul (16M)", lambda: ring.mul(a, b)),
("add (16M)", lambda: ring.add(a, b)),
("arshift 18 (16M)", lambda: ring.arshift(a, 18)),
):
with np.errstate(over="ignore"):
ms = bench(f" {name}", 5, fn)
print(f" -> {length / (ms / 1e3) / 1e9:.2f} Gop/s")
return 0
if __name__ == "__main__":
raise SystemExit(main())