research/tools/simulators/blend/tests/test_adversary.py
Marcin Pawlowski f51630e509
Rename the simulator and report from pd to blend
The study started as a peering-degree question and grew well past it: propagation,
adversary exposure, deanonymization and time-to-link, reliability under uniform
and correlated churn, messaging redundancy, and cover traffic. The pd name no
longer describes it.

tools/simulators/blend/pd/ -> tools/simulators/blend/, package src/pd -> src/blend,
and reports/blend/pd/ -> reports/blend/. Moved with git mv so history follows.

The text substitutions are deliberately narrow. pd is also the conventional pandas
alias, and pandas genuinely has a pd.plotting submodule, so a blanket pd. -> blend.
rewrite would have corrupted four files. Only package-unambiguous forms were
changed: from pd.X, -m pd.X, pd.<our module>, PD_BYTES_BUDGET, src/pd, and the
pyproject name. All four import pandas as pd lines are untouched and verified.

Both READMEs reframed: peering degree is now presented as the primary axis that
ties the others together rather than as the subject, and the relative links, which
lost a directory level in the move, are corrected.

Verified after the move: ruff clean, 101 tests, 45 verify anchors, make targets,
the script shims, an end-to-end smoke run, and data/report_numbers.py still
reproducing the report tables from the checked-in evidence.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-06 17:59:59 +02:00

63 lines
2.4 KiB
Python

from itertools import combinations
import numpy as np
from blend.adversary import _greedy_coverage, adversary_metrics, place_adversary
from blend.config import SimConfig
from blend.graph import Graph, build_graph
def _cycle4():
indptr = np.array([0, 2, 4, 6, 8], dtype=np.int64)
indices = np.array([1, 3, 0, 2, 1, 3, 0, 2], dtype=np.int64)
return Graph(n=4, degree=2, indptr=indptr, indices=indices,
base=np.ones(8), src=np.array([0, 0, 1, 1, 2, 2, 3, 3]), p=np.zeros(4))
def test_coverage_eclipse_hand_checked():
g = _cycle4() # 0-1-2-3-0
m = adversary_metrics(g, np.array([False, True, False, True])) # adv {1,3}
assert m["observed_count"] == 2 and m["eclipsed_count"] == 2 # honest {0,2} fully surrounded
m = adversary_metrics(g, np.array([False, True, False, False])) # adv {1}
assert m["observed_count"] == 2 and m["eclipsed_count"] == 0 # {0,2} observed, none eclipsed
def test_random_closed_form():
g = build_graph(SimConfig(n_nodes=5000, degree=6, graph_seed=0))
rng = np.random.default_rng(0)
def _obs():
return adversary_metrics(g, place_adversary(g, 0.2, "random", rng, 10**9))["observed_frac"]
obs = np.mean([_obs() for _ in range(5)])
assert abs(obs - (1 - 0.8 ** 6)) < 0.02
def test_worstcase_coverage_is_an_envelope():
g = build_graph(SimConfig(n_nodes=400, degree=4, graph_seed=0))
rng = np.random.default_rng(0)
rand = adversary_metrics(g, place_adversary(g, 0.2, "random", rng, 10**9))["observed_frac"]
wc = adversary_metrics(
g, place_adversary(g, 0.2, "worstcase_coverage", rng, 10**9))["observed_frac"]
assert wc >= rand - 1e-9
def test_greedy_coverage_near_optimal():
g = build_graph(SimConfig(n_nodes=10, degree=3, graph_seed=0))
best = max(adversary_metrics(g, _mask(10, c))["observed_count"]
for c in combinations(range(10), 2))
idx = _greedy_coverage(g, 2, np.random.default_rng(0))
got = adversary_metrics(g, _mask(10, idx))["observed_count"]
assert got >= (1 - 1 / np.e) * best - 1e-9
def _mask(n, idx):
m = np.zeros(n, dtype=bool)
m[list(idx)] = True
return m
def test_worstcase_cap_raises():
import pytest
g = build_graph(SimConfig(n_nodes=200, degree=4))
with pytest.raises(ValueError):
place_adversary(g, 0.2, "worstcase_coverage", np.random.default_rng(0), worstcase_max_n=100)