- 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
178 lines
6.7 KiB
Python
178 lines
6.7 KiB
Python
"""安全计算域 Vault 的算子正确性测试。"""
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
import nax
|
|
from nax.errors import ProtocolError
|
|
|
|
from helpers import rand
|
|
|
|
TOL = 2e-3
|
|
|
|
|
|
def r(secret, to="alice"):
|
|
return secret.reveal(to=to).fetch()
|
|
|
|
|
|
class TestBasicOps:
|
|
def test_share_reveal_roundtrip(self, box):
|
|
v = rand((4, 5), seed=1)
|
|
s = box.share(nax.site("alice").put(v))
|
|
assert s.shape == (4, 5)
|
|
assert np.allclose(r(s), v, atol=TOL)
|
|
|
|
def test_shares_are_distributed(self, box):
|
|
s = box.share(nax.site("alice").put(rand((3,), seed=2)))
|
|
assert sorted(s.shares) == sorted(box.sites)
|
|
|
|
def test_add_sub_neg(self, box):
|
|
a, b = rand((3, 3), 1), rand((3, 3), 2)
|
|
x, y = box.share(nax.site("alice").put(a)), box.share(nax.site("bob").put(b))
|
|
assert np.allclose(r(x + y), a + b, atol=TOL)
|
|
assert np.allclose(r(x - y), a - b, atol=TOL)
|
|
assert np.allclose(r(-x), -a, atol=TOL)
|
|
|
|
def test_public_add_mul(self, box):
|
|
a = rand((4,), 3)
|
|
x = box.share(nax.site("alice").put(a))
|
|
assert np.allclose(r(x + 2.5), a + 2.5, atol=TOL)
|
|
assert np.allclose(r(x * 3.0), a * 3.0, atol=TOL)
|
|
assert np.allclose(r(2.5 + x), a + 2.5, atol=TOL)
|
|
assert np.allclose(r(3.0 * x), a * 3.0, atol=TOL)
|
|
assert np.allclose(r(x / 4.0), a / 4.0, atol=TOL)
|
|
|
|
def test_rsub(self, box):
|
|
a = rand((4,), 9)
|
|
x = box.share(nax.site("alice").put(a))
|
|
assert np.allclose(r(5.0 - x), 5.0 - a, atol=TOL)
|
|
|
|
def test_secret_mul(self, box):
|
|
a, b = rand((5,), 4), rand((5,), 5)
|
|
x, y = box.share(nax.site("alice").put(a)), box.share(nax.site("bob").put(b))
|
|
assert np.allclose(r(x * y), a * b, atol=TOL)
|
|
|
|
def test_mul_broadcast(self, box):
|
|
a, b = rand((3, 4), 6), rand((4,), 7)
|
|
x, y = box.share(nax.site("alice").put(a)), box.share(nax.site("bob").put(b))
|
|
assert np.allclose(r(x * y), a * b, atol=TOL)
|
|
|
|
def test_matmul_secret_secret(self, box):
|
|
a, b = rand((4, 3), 8), rand((3, 2), 9)
|
|
x, y = box.share(nax.site("alice").put(a)), box.share(nax.site("bob").put(b))
|
|
out = x @ y
|
|
assert out.shape == (4, 2)
|
|
assert np.allclose(r(out), a @ b, atol=1e-2)
|
|
|
|
def test_matmul_with_public(self, box):
|
|
a, m = rand((4, 3), 10), rand((3, 2), 11)
|
|
x = box.share(nax.site("alice").put(a))
|
|
assert np.allclose(r(x @ m), a @ m, atol=1e-2)
|
|
assert np.allclose(r(m.T @ x.T), m.T @ a.T, atol=1e-2)
|
|
|
|
def test_chained_expression(self, box):
|
|
a, b, c = rand((3,), 12), rand((3,), 13), rand((3,), 14)
|
|
x = box.share(nax.site("alice").put(a))
|
|
y = box.share(nax.site("bob").put(b))
|
|
z = box.share(nax.site("carol").put(c))
|
|
assert np.allclose(r((x * y + z) * 2.0 - 1.0), (a * b + c) * 2.0 - 1.0, atol=TOL)
|
|
|
|
|
|
class TestReductions:
|
|
def test_sum_and_mean(self, box):
|
|
a = rand((4, 5), 15)
|
|
x = box.share(nax.site("alice").put(a))
|
|
assert np.allclose(r(x.sum()), a.sum(), atol=1e-2)
|
|
assert np.allclose(r(x.sum(axis=0)), a.sum(axis=0), atol=1e-2)
|
|
assert np.allclose(r(x.mean()), a.mean(), atol=TOL)
|
|
assert np.allclose(r(x.mean(axis=1)), a.mean(axis=1), atol=TOL)
|
|
|
|
def test_transpose_reshape(self, box):
|
|
a = rand((2, 6), 16)
|
|
x = box.share(nax.site("alice").put(a))
|
|
assert np.allclose(r(x.T), a.T, atol=TOL)
|
|
assert np.allclose(r(x.reshape(3, 4)), a.reshape(3, 4), atol=TOL)
|
|
|
|
def test_concat(self, box):
|
|
a, b = rand((3, 2), 17), rand((3, 4), 18)
|
|
x = box.share(nax.site("alice").put(a))
|
|
y = box.share(nax.site("bob").put(b))
|
|
out = box.concat([x, y], axis=1)
|
|
assert out.shape == (3, 6)
|
|
assert np.allclose(r(out), np.hstack([a, b]), atol=TOL)
|
|
|
|
|
|
class TestNonLinear:
|
|
@pytest.mark.parametrize("degree,rng_scale,tol", [(3, 3.0, 0.05), (5, 5.0, 0.05), (7, 5.0, 0.03), (9, 5.0, 0.02)])
|
|
def test_sigmoid_accuracy(self, box, degree, rng_scale, tol):
|
|
a = np.linspace(-rng_scale, rng_scale, 21)
|
|
x = box.share(nax.site("alice").put(a))
|
|
got = r(box.sigmoid(x, degree=degree))
|
|
assert np.abs(got - 1 / (1 + np.exp(-a))).max() < tol
|
|
|
|
def test_polynomial(self, box):
|
|
a = rand((5,), 19, scale=1.5)
|
|
x = box.share(nax.site("alice").put(a))
|
|
coeffs = [1.0, -2.0, 0.5, 0.25]
|
|
expect = sum(c * a ** i for i, c in enumerate(coeffs))
|
|
assert np.allclose(r(box.polynomial(x, coeffs)), expect, atol=1e-2)
|
|
|
|
def test_square(self, box):
|
|
a = rand((4,), 20, scale=2.0)
|
|
x = box.share(nax.site("alice").put(a))
|
|
assert np.allclose(r(x.square()), a ** 2, atol=TOL)
|
|
|
|
def test_bad_sigmoid_degree(self, box):
|
|
x = box.share(nax.site("alice").put(np.zeros(2)))
|
|
with pytest.raises(ProtocolError):
|
|
box.sigmoid(x, degree=4)
|
|
|
|
|
|
class TestVaultSetup:
|
|
def test_needs_two_sites(self, cluster):
|
|
with pytest.raises(ProtocolError):
|
|
nax.vault(["alice"])
|
|
|
|
def test_rejects_duplicates(self, cluster):
|
|
with pytest.raises(ProtocolError):
|
|
nax.vault(["alice", "alice"])
|
|
|
|
def test_unknown_site(self, cluster):
|
|
with pytest.raises(nax.UnknownSite):
|
|
nax.vault(["alice", "dave"])
|
|
|
|
def test_cross_vault_op_rejected(self, cluster):
|
|
v1 = nax.vault(["alice", "bob"], seed=1)
|
|
v2 = nax.vault(["bob", "carol"], seed=2)
|
|
x = v1.share(nax.site("alice").put(np.ones(3)))
|
|
y = v2.share(nax.site("bob").put(np.ones(3)))
|
|
with pytest.raises(ProtocolError):
|
|
_ = x + y
|
|
|
|
def test_external_data_owner(self, cluster):
|
|
"""数据方不必是安全域成员:carol 出数据,alice/bob 出算力。"""
|
|
v = nax.vault(["alice", "bob"], seed=3)
|
|
a = rand((4,), 21)
|
|
s = v.share(nax.site("carol").put(a))
|
|
assert sorted(s.shares) == ["alice", "bob"]
|
|
assert np.allclose(s.reveal(to="carol").fetch(), a, atol=TOL)
|
|
|
|
def test_dealer_on_third_party(self, cluster):
|
|
"""把离线物料生成放到独立的第三方 carol 上。"""
|
|
v = nax.vault(["alice", "bob"], dealer_site="carol", seed=4)
|
|
a, b = rand((3,), 22), rand((3,), 23)
|
|
x = v.share(nax.site("alice").put(a))
|
|
y = v.share(nax.site("bob").put(b))
|
|
assert np.allclose(v.reveal(x * y, "alice").fetch(), a * b, atol=TOL)
|
|
|
|
def test_constant(self, box):
|
|
c = box.constant(np.array([1.0, 2.0]))
|
|
assert np.allclose(r(c), [1.0, 2.0], atol=TOL)
|
|
|
|
def test_stats_counted(self, box):
|
|
x = box.share(nax.site("alice").put(np.ones(2)))
|
|
before = box.stats["mul"]
|
|
_ = x * x
|
|
assert box.stats["mul"] == before + 1
|
|
assert box.stats["open_rounds"] > 0
|