Files
ssw-mvp/nax/tests/test_compare.py
T
admin 3247e9fd15 init: ssw-mvp initial commit
- boot/ (Java/Maven, JNPF 7.0) — 移除旧的 boot/.git,源码直接纳入仓库
- web/ (Vue 3 + Vite + Ant Design Vue, pnpm)
- docs/, scard/, nax/
- refs/INDEX.md + refs/国标/(其余 refs/* 开源子仓库不纳入)
- 已忽略: connector_data/, jweb/, log/, web/{dist,node_modules,.pnpm}, boot/*/target/, .DS_Store, .idea, .vscode
2026-08-05 18:29:12 +08:00

126 lines
4.6 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.
"""安全比较原语(edaBit + BitLT)的正确性验证。
核心断言:``vault.lt/gt/eq/...`` 返回的共享 0/1 位,reveal 后必须等于明文对照;
覆盖负数、相等、边界与随机样本。
约定:比较结果是**定点尺度**的 0/1(即 0.0 / 1.0),因此可以直接参与
``mul`` / ``add`` / ``select``,无需额外的布尔-算术转换。
协议在线阶段只打开 ``c = x + r``(``r`` 在 Z_2^64 上均匀),因此是信息论
意义上的零泄露;本文件只验证正确性,不验证泄露(那需要信息论论证)。
"""
import numpy as np
import pytest
import nax
def _plain(vault, sref):
"""把密文按定点解码成明文 numpy。"""
return np.asarray(
vault.reveal(sref, to=vault.sites[0]).fetch(force=True)
).reshape(sref.shape)
@pytest.fixture
def box(two_party):
return nax.vault(["alice", "bob"], seed=3)
def _secret(box, arr):
return box.share(nax.site("alice").put(np.asarray(arr, dtype=np.float64)))
def test_lt_against_plain(box):
rng = np.random.default_rng(0)
a = rng.integers(-10, 10, size=30).astype(np.float64)
b = rng.integers(-10, 10, size=30).astype(np.float64)
A, B = _secret(box, a), _secret(box, b)
assert np.array_equal(_plain(box, box.lt(A, B)), (a < b).astype(float))
def test_gt_le_ge(box):
a = np.array([-3.0, 0.0, 5.0, 5.0, -100.0, 2.0])
b = np.array([-3.0, 1.0, 4.0, 5.0, -99.0, 2.0])
A, B = _secret(box, a), _secret(box, b)
assert np.array_equal(_plain(box, box.gt(A, B)), (a > b).astype(float))
assert np.array_equal(_plain(box, box.le(A, B)), (a <= b).astype(float))
assert np.array_equal(_plain(box, box.ge(A, B)), (a >= b).astype(float))
def test_eq_ne(box):
a = np.array([1.0, 2.0, 2.0, -7.0])
b = np.array([1.0, 3.0, 2.0, -7.0])
A, B = _secret(box, a), _secret(box, b)
assert np.array_equal(_plain(box, box.eq(A, B)), (a == b).astype(float))
assert np.array_equal(_plain(box, box.ne(A, B)), (a != b).astype(float))
def test_compare_survives_large_magnitude(box):
"""定点范围内的大数(含跨符号)比较仍然精确——协议不依赖输入有界。"""
a = np.array([1e5, -1e5, 1e5, 0.25, -0.25])
b = np.array([-1e5, 1e5, 1e5, 0.5, -0.5])
A, B = _secret(box, a), _secret(box, b)
assert np.array_equal(_plain(box, box.lt(A, B)), (a < b).astype(float))
def test_max_min_elementwise(box):
rng = np.random.default_rng(1)
a = rng.integers(-20, 20, size=12).astype(np.float64)
b = rng.integers(-20, 20, size=12).astype(np.float64)
A, B = _secret(box, a), _secret(box, b)
assert np.allclose(_plain(box, box.max(A, B)), np.maximum(a, b), atol=1e-3)
assert np.allclose(_plain(box, box.min(A, B)), np.minimum(a, b), atol=1e-3)
def test_select_picks_branch(box):
# cond 为共享 0/1:cond=True 取 x,否则取 y,全程密文
cond = _secret(box, np.array([1.0, 0.0, 1.0, 0.0]))
x = _secret(box, np.array([10.0, 20.0, 30.0, 40.0]))
y = _secret(box, np.array([1.0, 2.0, 3.0, 4.0]))
out = _plain(box, box.select(cond, x, y))
assert np.allclose(out, [10.0, 2.0, 30.0, 4.0], atol=1e-3)
def test_amax_amin_axis0(box):
m = np.array([[2.0, 8.0, 3.0], [5.0, 1.0, 9.0], [-4.0, 0.0, 7.0]])
M = _secret(box, m)
assert np.allclose(_plain(box, box.amax(M, axis=0)), m.max(axis=0), atol=1e-3)
assert np.allclose(_plain(box, box.amin(M, axis=0)), m.min(axis=0), atol=1e-3)
def test_argmax_1d(box):
v = np.array([3.0, 9.0, 1.0, 7.0, 9.0, -2.0])
V = _secret(box, v)
val, idx = box.argmax(V, axis=0)
assert idx == 1 # 并列时取最小下标:第一个 9 在下标 1
assert abs(_plain(box, val) - 9.0) < 1e-3
def test_argmin_1d(box):
v = np.array([3.0, 9.0, 1.0, 7.0, 9.0, -2.0])
V = _secret(box, v)
val, idx = box.argmin(V, axis=0)
assert idx == 5 # -2.0 在下标 5
assert abs(_plain(box, val) - (-2.0)) < 1e-3
def test_argmax_axis1(box):
m = np.array([[2.0, 8.0, 3.0], [5.0, 1.0, 9.0]])
M = _secret(box, m)
_, idx = box.argmax(M, axis=1)
assert np.array_equal(idx, [1, 2]) # 第一行 argmax 列 1;第二行 列 2
def test_comparison_is_not_revealed(box):
"""比较结果以共享形式存在:只有一方的分片不构成明文。"""
a, b = _secret(box, np.array([1.0, 5.0])), _secret(box, np.array([3.0, 2.0]))
c = box.lt(a, b)
shares = [
np.asarray(nax.SiteRef(c.shares[s]).fetch(force=True)) for s in box.sites
]
# 任一方单独持有的分片都不是 0/1(以压倒性概率)
for sh in shares:
assert not np.all(np.isin(sh.view(np.int64), [0, 1 << 18]))