- nax-he(零外部依赖,no_std + alloc):手写 bigint(Montgomery CIOS 模幂 / Knuth D 除法 / Miller-Rabin)+ Paillier + OLE + PIR + n 方三元组 - 50 条 golden 逐位对照 Python(显式 r 的密文 / 解密 / 同态链 / 负明文 % 向下取整 / n//2 边界),13 条协议语义测试 - 三元组输出布局与 share::mul_triple_into 一致,可直接接 nax-vault - 性能实测修正:Paillier 加密 n=1024 是 21ms→7.3ms(2.9×), 不是早前预估的 280×;HE 真正收益是可用性 + TEE 可编入 - 修复:MontCtx::modpow 未归约底数(大底数被静默截断)、 小指数走 1-bit 幂梯(mul_plain 360→159μs) - 110 用例全绿,no_std + armv7 全通过
448 lines
16 KiB
Python
448 lines
16 KiB
Python
#!/usr/bin/env python3
|
||
"""从参考实现 `refs/nax`(Python/numpy)导出 golden vectors,供 Rust 侧逐位对照。
|
||
|
||
# 为什么要这个脚本
|
||
|
||
"Rust 重写"最大的风险不是性能,是**语义漂移**:一个舍入模式、
|
||
一个符号位处理、一处截断时机不同,结果就在小数第 8 位上分叉,
|
||
而这类 bug 在单元测试里几乎抓不到(测试往往只看"接近")。
|
||
|
||
所以凡是**确定性**的纯算子,都从参考实现导出一份逐位基线;
|
||
Rust 侧用 `assert_eq!` 硬比,不给"误差容忍"留口子。
|
||
|
||
# 为什么绕开 `import nax`
|
||
|
||
`nax/__init__.py` 会拉起 pandas 整条链,但 `nax/crypto/ring.py` 与
|
||
`nax/crypto/paillier.py` 只依赖 numpy。这里直接用 importlib 按文件加载,
|
||
保持依赖最小、也避免参考仓库的 `__init__` 将来变动影响基线生成。
|
||
|
||
用法:
|
||
.venv/bin/python tools/gen_golden.py
|
||
输出:
|
||
crates/nax-core/tests/golden_vectors.rs (环算子)
|
||
crates/nax-he/tests/golden_paillier.rs (Paillier / 大数)
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import importlib.util
|
||
import pathlib
|
||
import sys
|
||
|
||
import numpy as np
|
||
|
||
ROOT = pathlib.Path(__file__).resolve().parent.parent
|
||
REF_RING = ROOT / "refs" / "nax" / "nax" / "crypto" / "ring.py"
|
||
REF_PAILLIER = ROOT / "refs" / "nax" / "nax" / "crypto" / "paillier.py"
|
||
OUT = ROOT / "crates" / "nax-core" / "tests" / "golden_vectors.rs"
|
||
OUT_HE = ROOT / "crates" / "nax-he" / "tests" / "golden_paillier.rs"
|
||
|
||
DEFAULT_FXP = 18
|
||
|
||
# 固定测试密钥(一次性生成后写死,保证再生成时 diff 稳定)。
|
||
# 仅测试用途 —— 生产密钥必须 keygen 时现生成。
|
||
P512 = 115143333177955593351170883930350830275481352656898832985553126680305234857641
|
||
Q512 = 105915694769767988741462729257215332633525696031712010731853285168725452852463
|
||
P1024 = 13185077936874614465251607502897794108295134688804334693335211059545410538575957469278704721156024331182839602882257300151033818320780324968404181570428423
|
||
Q1024 = 10094728724297416245149492696441592499653382859672861820017232790240265217185766588377851889644878419712866423425923577932070971179003630402571429925806007
|
||
|
||
|
||
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 load_paillier():
|
||
spec = importlib.util.spec_from_file_location("ref_paillier", REF_PAILLIER)
|
||
mod = importlib.util.module_from_spec(spec)
|
||
# dataclass 需要模块已注册在 sys.modules 里,否则 _is_type 取不到命名空间
|
||
sys.modules["ref_paillier"] = mod
|
||
assert spec.loader is not None
|
||
spec.loader.exec_module(mod)
|
||
return mod
|
||
|
||
|
||
def hx(v: int) -> str:
|
||
"""u64 的十六进制字面量。"""
|
||
return f"0x{v & 0xFFFFFFFFFFFFFFFF:016X}u64"
|
||
|
||
|
||
def fb(v: float) -> str:
|
||
"""f64 的位模式字面量 —— 精确往返,不经过十进制解析。"""
|
||
bits = int(np.float64(v).view(np.uint64))
|
||
return f"f64::from_bits(0x{bits:016X})"
|
||
|
||
|
||
def emit_encode(ring) -> list[str]:
|
||
lines = []
|
||
vals = [
|
||
0.0, 1.0, -1.0, 0.5, -0.5, 1.5, -1.5, 2.5, 3.5, -2.5,
|
||
123.456, -987.654, 1e-6, -1e-6, 3.14159265358979,
|
||
1e-9, 65.0, 66.0, 0.25, 0.125,
|
||
]
|
||
for fxp in (18, 0, 10):
|
||
for v in vals:
|
||
try:
|
||
e = int(ring.encode(np.float64(v), fxp))
|
||
except Exception:
|
||
continue
|
||
lines.append(
|
||
f" assert_eq!(encode({fb(v)}, {fxp}).unwrap(), {hx(e)}, "
|
||
f"\"encode({v!r}, fxp={fxp})\");"
|
||
)
|
||
return lines
|
||
|
||
|
||
def emit_decode(ring) -> list[str]:
|
||
lines = []
|
||
raw = [0, 1, 2, 1 << 18, (1 << 18) - 1, 0xFFFFFFFFFFFFFFFF,
|
||
0x7FFFFFFFFFFFFFFF, 0x8000000000000000, 123456789012345]
|
||
for fxp in (18, 0):
|
||
for u in raw:
|
||
d = float(ring.decode(np.uint64(u), fxp))
|
||
lines.append(
|
||
f" assert_eq!(decode({hx(u)}, {fxp}).to_bits(), {fb(d)}.to_bits(), "
|
||
f"\"decode(0x{u:016X}, fxp={fxp})\");"
|
||
)
|
||
return lines
|
||
|
||
|
||
def emit_arith(ring) -> list[str]:
|
||
lines = []
|
||
rng = np.random.default_rng(20241029)
|
||
pairs = []
|
||
# 边界值 + 随机
|
||
edge = [0, 1, 2, (1 << 63) - 1, 1 << 63, 0xFFFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF]
|
||
for a in edge:
|
||
for b in edge:
|
||
pairs.append((a, b))
|
||
for _ in range(40):
|
||
pairs.append((int(rng.integers(0, 1 << 63)), int(rng.integers(0, 1 << 63))))
|
||
|
||
for a, b in pairs:
|
||
A = np.array([a], dtype=np.uint64)
|
||
B = np.array([b], dtype=np.uint64)
|
||
s = int(ring.add(A, B)[0])
|
||
d = int(ring.sub(A, B)[0])
|
||
m = int(ring.mul(A, B)[0])
|
||
lines.append(
|
||
f" check_binop({hx(a)}, {hx(b)}, {hx(s)}, {hx(d)}, {hx(m)});"
|
||
)
|
||
return lines
|
||
|
||
|
||
def emit_arshift(ring) -> list[str]:
|
||
lines = []
|
||
vals = [0, 1, 0xFFFFFFFFFFFFFFFF, 0x8000000000000000, 0x7FFFFFFFFFFFFFFF,
|
||
(-8) & 0xFFFFFFFFFFFFFFFF, (-1) & 0xFFFFFFFFFFFFFFFF, 123456789012345]
|
||
for v in vals:
|
||
for bits in (0, 1, 18, 32, 63):
|
||
r = int(ring.arshift(np.array([v], dtype=np.uint64), bits)[0])
|
||
lines.append(f" check_arshift({hx(v)}, {bits}, {hx(r)});")
|
||
return lines
|
||
|
||
|
||
def emit_matmul(ring) -> list[str]:
|
||
lines = []
|
||
shapes = [(2, 2, 2), (3, 4, 5), (7, 13, 5), (1, 6, 9), (16, 16, 16)]
|
||
rng = np.random.default_rng(7)
|
||
for (m, k, n) in shapes:
|
||
a = rng.integers(0, 1 << 62, size=(m, k), dtype=np.uint64)
|
||
b = rng.integers(0, 1 << 62, size=(k, n), dtype=np.uint64)
|
||
want = ring.matmul(a, b)
|
||
av = ",".join(hx(int(x)) for x in a.ravel())
|
||
bv = ",".join(hx(int(x)) for x in b.ravel())
|
||
wv = ",".join(hx(int(x)) for x in want.ravel())
|
||
lines.append(f" check_matmul({m}, {k}, {n}, &[{av}], &[{bv}], &[{wv}]);")
|
||
return lines
|
||
|
||
|
||
def emit_colsum(ring) -> list[str]:
|
||
lines = []
|
||
rng = np.random.default_rng(11)
|
||
for (rows, cols) in [(2, 3), (10, 7), (33, 5)]:
|
||
a = rng.integers(0, 1 << 62, size=(rows, cols), dtype=np.uint64)
|
||
want = a.sum(axis=0, dtype=np.uint64)
|
||
av = ",".join(hx(int(x)) for x in a.ravel())
|
||
wv = ",".join(hx(int(x)) for x in want.ravel())
|
||
lines.append(f" check_col_sum({rows}, {cols}, &[{av}], &[{wv}]);")
|
||
return lines
|
||
|
||
|
||
def hx_big(v: int) -> str:
|
||
"""大整数的十六进制字符串字面量。"""
|
||
return f'"{v:x}"'
|
||
|
||
|
||
# --------------------------------------------------------------------------- #
|
||
# Paillier / 大数部分(输出 crates/nax-he/tests/golden_paillier.rs)
|
||
# --------------------------------------------------------------------------- #
|
||
def emit_bigint() -> list[str]:
|
||
"""大数 modpow / divmod / mod_inverse 的 Python int 基线。"""
|
||
lines = []
|
||
cases_modpow = [
|
||
(2, 10, 1000003),
|
||
(123456, 1000, 1000003),
|
||
((1 << 200) + 5, (1 << 97) + 3, (1 << 130) + 77),
|
||
(0xDEADBEEFCAFEBABE, (1 << 70) + 1, (1 << 160) + 21),
|
||
]
|
||
for base, exp, m in cases_modpow:
|
||
want = pow(base, exp, m)
|
||
lines.append(
|
||
f' assert_eq!(h({hx_big(base)}).modpow(&h({hx_big(exp)}), &h({hx_big(m)}))'
|
||
f'.unwrap().to_hex(), {hx_big(want)}.to_string(), "modpow");'
|
||
)
|
||
cases_divmod = [
|
||
((1 << 200) + 12345, (1 << 97) + 77),
|
||
(0xFFFFFFFFFFFFFFFFFFFF, 7),
|
||
((1 << 128) - 1, (1 << 64) - 3),
|
||
]
|
||
for a, d in cases_divmod:
|
||
q, r = divmod(a, d)
|
||
lines.append(
|
||
f' {{ let (q, r) = h({hx_big(a)}).divmod(&h({hx_big(d)})).unwrap(); '
|
||
f'assert_eq!(q.to_hex(), {hx_big(q)}.to_string(), "divmod q"); '
|
||
f'assert_eq!(r.to_hex(), {hx_big(r)}.to_string(), "divmod r"); }}'
|
||
)
|
||
cases_inv = [(3, 1000003), (77, 1000003), ((1 << 80) + 5, (1 << 89) - 1)] # 2^89-1 是梅森素数
|
||
for a, m in cases_inv:
|
||
want = pow(a, -1, m)
|
||
lines.append(
|
||
f' assert_eq!(h({hx_big(a)}).mod_inverse(&h({hx_big(m)}))'
|
||
f'.unwrap().to_hex(), {hx_big(want)}.to_string(), "mod_inverse");'
|
||
)
|
||
return lines
|
||
|
||
|
||
def emit_paillier_cases(pai, p: int, q: int, tag: str) -> list[str]:
|
||
"""确定性 Paillier 算子:encrypt(m, r) / decrypt_signed / add / mul_plain。
|
||
|
||
全部显式指定随机因子 r,密文确定性 —— 这就是能做逐位对照的原因。
|
||
"""
|
||
n = p * q
|
||
pub = pai.PublicKey(n)
|
||
priv = pai.PrivateKey(pub, p, q)
|
||
lines = [f" // ---- {tag}: n = {n.bit_count()} bit ----"]
|
||
|
||
# encrypt(m, r) + decrypt_signed 往返(含负明文与 n//2 边界)
|
||
ms = [0, 1, 123456, n - 1, n // 2, n // 2 + 1, -777, -(n - 1)]
|
||
rs = [2, 3, 12345, n - 2, (n // 3) + 1, n - 5, 99991, 65537]
|
||
for m, r in zip(ms, rs):
|
||
c = pub.encrypt(m, r) # Python 内部 m %= n(向下取整)
|
||
lines.append(
|
||
f' check_encrypt(&{tag}(), &h({hx_big(m % n)}), &h({hx_big(r)}), &h({hx_big(c)}));'
|
||
)
|
||
dec = priv.decrypt_signed(c)
|
||
want_neg = dec < 0
|
||
want_mag = abs(dec)
|
||
lines.append(
|
||
f' check_decrypt(&{tag}(), &h({hx_big(c)}), {str(want_neg).lower()}, '
|
||
f'&h({hx_big(want_mag)}), "{m}");'
|
||
)
|
||
|
||
# 同态链:add / add_plain / mul_plain(密文确定 → 结果确定)
|
||
c1 = pub.encrypt(300, 7)
|
||
c2 = pub.encrypt(45, 11)
|
||
lines.append(
|
||
f' check_hom(&{tag}(), &h({hx_big(c1)}), &h({hx_big(c2)}), '
|
||
f'&h({hx_big(pub.add(c1, c2))}), &h({hx_big(pub.add_plain(c1, 17))}), '
|
||
f'&h({hx_big(pub.mul_plain(c2, 2))}));'
|
||
)
|
||
# 负明文编码(Python 的 m %= n 向下取整语义)
|
||
for m in (-777, -1, -(n - 1)):
|
||
r = 5
|
||
c = pub.encrypt(m, r)
|
||
lines.append(
|
||
f' check_encrypt(&{tag}(), &h({hx_big(m % n)}), &h({hx_big(r)}), &h({hx_big(c)}));'
|
||
)
|
||
return lines
|
||
|
||
|
||
def main() -> int:
|
||
ring = load_ring()
|
||
body: list[str] = []
|
||
body += [" // ---- encode / decode(含 np.rint 银行家舍入边界)----"]
|
||
body += emit_encode(ring)
|
||
body += ["", " // ---- decode ----"]
|
||
body += emit_decode(ring)
|
||
body += ["", " // ---- 逐元素 add / sub / mul(环回绕)----"]
|
||
body += emit_arith(ring)
|
||
body += ["", " // ---- 算术右移(定点截断)----"]
|
||
body += emit_arshift(ring)
|
||
body += ["", " // ---- 矩阵乘 ----"]
|
||
body += emit_matmul(ring)
|
||
body += ["", " // ---- 列求和 ----"]
|
||
body += emit_colsum(ring)
|
||
|
||
n_cases = sum(1 for line in body if line.strip().startswith("check_")
|
||
or line.strip().startswith("assert_eq!"))
|
||
|
||
header = f'''//! **自动生成,请勿手改。**
|
||
//!
|
||
//! 由 `tools/gen_golden.py` 从参考实现 `refs/nax/nax/crypto/ring.py` 导出。
|
||
//! 参考侧 numpy 版本:{np.__version__}
|
||
//! 用例数:{n_cases}
|
||
//!
|
||
//! 重新生成:
|
||
//! ```bash
|
||
//! .venv/bin/python tools/gen_golden.py
|
||
//! ```
|
||
//!
|
||
//! 这个文件的意义:Rust 算子核与 Python 参考实现必须给出**逐位相同**的 u64。
|
||
//! 一旦某次改动让这里变红,说明语义漂移了 —— 不要去"放宽断言",
|
||
//! 先确认哪一边才是对的。
|
||
|
||
#![allow(clippy::unreadable_literal)]
|
||
|
||
use nax_core::compare::pack_bits_into;
|
||
use nax_core::ring::{{arshift_into, decode, encode, matmul_into, col_sum_into, add_into, sub_into, mul_into}};
|
||
|
||
fn check_binop(a: u64, b: u64, want_add: u64, want_sub: u64, want_mul: u64) {{
|
||
let (mut s, mut d, mut m) = (0u64, 0u64, 0u64);
|
||
add_into(&[a], &[b], std::slice::from_mut(&mut s)).unwrap();
|
||
sub_into(&[a], &[b], std::slice::from_mut(&mut d)).unwrap();
|
||
mul_into(&[a], &[b], std::slice::from_mut(&mut m)).unwrap();
|
||
assert_eq!(s, want_add, "add({{a:#x}}, {{b:#x}})");
|
||
assert_eq!(d, want_sub, "sub({{a:#x}}, {{b:#x}})");
|
||
assert_eq!(m, want_mul, "mul({{a:#x}}, {{b:#x}})");
|
||
}}
|
||
|
||
fn check_arshift(v: u64, bits: u32, want: u64) {{
|
||
let mut o = 0u64;
|
||
arshift_into(&[v], bits, std::slice::from_mut(&mut o)).unwrap();
|
||
assert_eq!(o, want, "arshift({{v:#x}}, {{bits}})");
|
||
}}
|
||
|
||
fn check_matmul(m: usize, k: usize, n: usize, a: &[u64], b: &[u64], want: &[u64]) {{
|
||
let mut out = vec![0u64; m * n];
|
||
matmul_into(m, k, n, a, b, &mut out).unwrap();
|
||
assert_eq!(out, want, "matmul {{m}}x{{k}}x{{n}}");
|
||
}}
|
||
|
||
fn check_col_sum(rows: usize, cols: usize, a: &[u64], want: &[u64]) {{
|
||
let mut out = vec![0u64; cols];
|
||
col_sum_into(rows, cols, a, &mut out).unwrap();
|
||
assert_eq!(out, want, "col_sum {{rows}}x{{cols}}");
|
||
}}
|
||
|
||
#[test]
|
||
fn conformance_with_python_reference() {{
|
||
{chr(10).join(body)}
|
||
}}
|
||
'''
|
||
|
||
OUT.parent.mkdir(parents=True, exist_ok=True)
|
||
OUT.write_text(header, encoding="utf-8")
|
||
print(f"wrote {OUT.relative_to(ROOT)} ({n_cases} cases)")
|
||
return 0
|
||
|
||
|
||
def main_he() -> int:
|
||
"""生成 crates/nax-he/tests/golden_paillier.rs。"""
|
||
pai = load_paillier()
|
||
body: list[str] = []
|
||
body += [" // ---- bigint: modpow / divmod / mod_inverse ----"]
|
||
body += emit_bigint()
|
||
body += ["", " // ---- Paillier 512-bit n ----"]
|
||
body += emit_paillier_cases(pai, P512, Q512, "small")
|
||
body += ["", " // ---- Paillier 1024-bit n ----"]
|
||
body += emit_paillier_cases(pai, P1024, Q1024, "big")
|
||
|
||
n_cases = sum(1 for line in body if line.strip().startswith(("check_", "assert_eq!", "{ let")))
|
||
|
||
header = f'''//! **自动生成,请勿手改。**
|
||
//!
|
||
//! 由 `tools/gen_golden.py` 从参考实现 `refs/nax/nax/crypto/paillier.py` 导出。
|
||
//! 用例数:{n_cases}
|
||
//!
|
||
//! 重新生成:
|
||
//! ```bash
|
||
//! .venv/bin/python tools/gen_golden.py
|
||
//! ```
|
||
//!
|
||
//! 这个文件的意义:Rust 的 Paillier 与 Python 参考实现必须给出**逐位相同**
|
||
//! 的密文与解密结果(给定相同明文与随机因子 r)。一旦某次改动让这里变红,
|
||
//! 说明语义漂移了 —— 不要去"放宽断言",先确认哪一边才是对的。
|
||
|
||
#![allow(clippy::unreadable_literal)]
|
||
|
||
use nax_core::prg::Stream;
|
||
use nax_he::bigint::{{BigUint, Signed}};
|
||
use nax_he::paillier::{{KeyPair, PrivateKey, PublicKey}};
|
||
|
||
fn h(s: &str) -> BigUint {{
|
||
BigUint::from_hex(s).unwrap()
|
||
}}
|
||
|
||
fn dead_stream() -> Stream {{
|
||
Stream::new(&[0u8; 32])
|
||
}}
|
||
|
||
fn build_kp(p: &str, q: &str) -> KeyPair {{
|
||
let (p, q) = (h(p), h(q));
|
||
let pubk = PublicKey::new(p.mul(&q)).unwrap();
|
||
let privk = PrivateKey::new(pubk.clone(), p, q).unwrap();
|
||
KeyPair {{ public: pubk, private: privk }}
|
||
}}
|
||
|
||
fn small() -> KeyPair {{
|
||
build_kp("{P512:x}", "{Q512:x}")
|
||
}}
|
||
|
||
fn big() -> KeyPair {{
|
||
build_kp("{P1024:x}", "{Q1024:x}")
|
||
}}
|
||
|
||
/// encrypt(m, r) 逐位对照(r 显式给定 → 密文确定性)。
|
||
fn check_encrypt(kp: &KeyPair, m: &BigUint, r: &BigUint, want: &BigUint) {{
|
||
let c = kp.public.encrypt(m, Some(r), &mut dead_stream()).unwrap();
|
||
assert_eq!(c.to_hex(), want.to_hex(), "encrypt(m={{}}, r={{}})", m.to_hex(), r.to_hex());
|
||
}}
|
||
|
||
/// decrypt_signed 逐位对照。
|
||
fn check_decrypt(kp: &KeyPair, c: &BigUint, want_neg: bool, want_mag: &BigUint, label: &str) {{
|
||
let s = kp.private.decrypt_signed(c);
|
||
assert_eq!(s.neg, want_neg, "decrypt_signed({{}}).neg", label);
|
||
assert_eq!(s.mag.to_hex(), want_mag.to_hex(), "decrypt_signed({{}}).mag", label);
|
||
}}
|
||
|
||
/// 同态算子逐位对照:add(c1,c2) / add_plain(c1,17) / mul_plain(c2,2)。
|
||
fn check_hom(
|
||
kp: &KeyPair,
|
||
c1: &BigUint,
|
||
c2: &BigUint,
|
||
want_add: &BigUint,
|
||
want_add_plain: &BigUint,
|
||
want_mul_plain: &BigUint,
|
||
) {{
|
||
assert_eq!(kp.public.add(c1, c2).to_hex(), want_add.to_hex(), "add");
|
||
assert_eq!(kp.public.add_plain(c1, &BigUint::from_u64(17)).to_hex(), want_add_plain.to_hex(), "add_plain");
|
||
assert_eq!(
|
||
kp.public.mul_plain(c2, &BigUint::from_u64(2)).unwrap().to_hex(),
|
||
want_mul_plain.to_hex(),
|
||
"mul_plain"
|
||
);
|
||
let _ = Signed::from_i64(0); // 保持 use 引用
|
||
}}
|
||
|
||
#[test]
|
||
fn conformance_with_python_reference() {{
|
||
{chr(10).join(body)}
|
||
}}
|
||
'''
|
||
|
||
OUT_HE.parent.mkdir(parents=True, exist_ok=True)
|
||
OUT_HE.write_text(header, encoding="utf-8")
|
||
print(f"wrote {OUT_HE.relative_to(ROOT)} ({n_cases} cases)")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
rc = main()
|
||
if rc == 0:
|
||
rc = main_he()
|
||
sys.exit(rc)
|