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

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 mpc-core --release --example bench
//! cargo run -p mpc-core --release --features simd --example bench # AVX2
//! ```
//!
//! 对应的 numpy 侧基线:`tools/bench_numpy.py`。
use std::time::Instant;
use mpc_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!("mpc-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 = mpc_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(mpc_core::hash::sha256(&data));
});
println!(" -> {:.2} MB/s", 16.0 / (ms / 1e3));
}