Files
ssw-mpc/crates/nax-core/examples/bench.rs
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

137 lines
3.9 KiB
Rust
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.
//! 算子核微基准 —— 用来看清 Rust 相对 numpy 到底赢在哪、赢多少。
//!
//! 不引 criterion(那是十几条依赖 + 几分钟编译):这里的量级差是
//! 5× 到 50×,用 `Instant` + 重复取最小值已经足够稳定。
//!
//! 运行:
//! ```bash
//! cargo run -p nax-core --release --example bench
//! cargo run -p nax-core --release --features simd --example bench # AVX2
//! ```
//!
//! 对应的 numpy 侧基线:`tools/bench_numpy.py`。
use std::time::Instant;
use nax_core::ring;
/// 确定性伪随机(xorshift64*),避免为基准引 rand。
struct Rng(u64);
impl Rng {
fn new(seed: u64) -> Self {
Self(seed | 1)
}
fn next(&mut self) -> u64 {
let mut x = self.0;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
self.0 = x;
x.wrapping_mul(0x2545_F491_4F6C_DD1D)
}
fn fill(&mut self, out: &mut [u64]) {
for o in out.iter_mut() {
*o = self.next();
}
}
}
fn bench<F: FnMut()>(label: &str, iters: usize, mut f: F) -> f64 {
// 预热
f();
let mut best = f64::MAX;
for _ in 0..iters {
let t = Instant::now();
f();
let dt = t.elapsed().as_secs_f64();
if dt < best {
best = dt;
}
}
println!("{label:<42} {:>10.3} ms", best * 1e3);
best * 1e3
}
fn main() {
println!("nax-core 算子核微基准");
println!("==============================");
#[cfg(feature = "simd")]
println!("SIMD: 已启用(x86_64 AVX2,运行时探测)");
#[cfg(not(feature = "simd"))]
println!("SIMD: 未启用(纯标量)");
println!();
let mut rng = Rng::new(0xC0FFEE);
// ---- 矩阵乘 ----
println!("[矩阵乘 u64 @ u64(mod 2^64)]");
for &n in &[64usize, 128, 256] {
let mut a = vec![0u64; n * n];
let mut b = vec![0u64; n * n];
rng.fill(&mut a);
rng.fill(&mut b);
let mut out = vec![0u64; n * n];
let ops = (n as f64).powi(3);
let iters = if n <= 64 { 20 } else { 3 };
let ms = bench(&format!(" matmul {n}x{n}x{n}"), iters, || {
ring::matmul_into(n, n, n, &a, &b, &mut out).unwrap();
std::hint::black_box(&out);
});
println!(
" -> {:.2} Gop/s (校验: out[0]={:#x})",
ops / (ms / 1e3) / 1e9,
out[0]
);
}
println!();
// ---- 逐元素 ----
println!("[逐元素算子,16M 元素]");
let len = 16 * 1024 * 1024;
let mut a = vec![0u64; len];
let mut b = vec![0u64; len];
let mut out = vec![0u64; len];
rng.fill(&mut a);
rng.fill(&mut b);
let ms = bench(" mul (16M)", 5, || {
ring::mul_into(&a, &b, &mut out).unwrap();
std::hint::black_box(&out);
});
println!(" -> {:.2} Gop/s", len as f64 / (ms / 1e3) / 1e9);
let ms = bench(" add (16M)", 5, || {
ring::add_into(&a, &b, &mut out).unwrap();
std::hint::black_box(&out);
});
println!(" -> {:.2} Gop/s", len as f64 / (ms / 1e3) / 1e9);
let ms = bench(" arshift 18 (16M)", 5, || {
ring::arshift_into(&a, 18, &mut out).unwrap();
std::hint::black_box(&out);
});
println!(" -> {:.2} Gop/s", len as f64 / (ms / 1e3) / 1e9);
println!();
// ---- PRG ----
println!("[ChaCha20 PRG]");
let seed = [7u8; 32];
let ms = bench(" fill_bytes 16MB", 5, || {
let mut s = nax_core::prg::Stream::new(&seed);
let mut buf = vec![0u8; 16 * 1024 * 1024];
s.fill_bytes(&mut buf);
std::hint::black_box(&buf);
});
println!(" -> {:.2} MB/s", 16.0 / (ms / 1e3));
println!();
// ---- SHA-256 ----
println!("[SHA-256]");
let data = vec![0xA5u8; 16 * 1024 * 1024];
let ms = bench(" sha256 16MB", 5, || {
std::hint::black_box(nax_core::hash::sha256(&data));
});
println!(" -> {:.2} MB/s", 16.0 / (ms / 1e3));
}