guldo-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- guldo/__init__.py +0 -0
- guldo/_src/COMPILER.md +329 -0
- guldo/_src/DEPLOY.md +200 -0
- guldo/_src/LAUNCH.md +85 -0
- guldo/_src/PROTOCOL.md +294 -0
- guldo/_src/RULES.md +25 -0
- guldo/_src/SUBMIT.md +170 -0
- guldo/_src/__init__.py +0 -0
- guldo/_src/attest.py +257 -0
- guldo/_src/build_site.py +958 -0
- guldo/_src/bundle_templates/check.py +1382 -0
- guldo/_src/bundle_templates/qcheck.py +253 -0
- guldo/_src/bundle_templates/qscore.py +402 -0
- guldo/_src/bundle_templates/score.py +649 -0
- guldo/_src/certificate.py +828 -0
- guldo/_src/certs_site.py +108 -0
- guldo/_src/claims.py +343 -0
- guldo/_src/compile_domain.py +1312 -0
- guldo/_src/compile_quality.py +286 -0
- guldo/_src/count_runner.py +248 -0
- guldo/_src/counted.py +274 -0
- guldo/_src/declared.py +215 -0
- guldo/_src/deploy/Caddyfile +48 -0
- guldo/_src/deploy/ledger-backup.service +9 -0
- guldo/_src/deploy/ledger-backup.sh +73 -0
- guldo/_src/deploy/ledger-backup.timer +10 -0
- guldo/_src/deploy/ledger-export.service +12 -0
- guldo/_src/deploy/ledger-export.timer +9 -0
- guldo/_src/deploy/ledger-web.service +38 -0
- guldo/_src/deploy/pin_cpu.sh +34 -0
- guldo/_src/deploy/provision_eval.sh +128 -0
- guldo/_src/deploy_smoke.py +148 -0
- guldo/_src/dlog_experiment.py +165 -0
- guldo/_src/domains/aidecode/compiled.json +586 -0
- guldo/_src/domains/aidecode/decl.py +134 -0
- guldo/_src/domains/aidecode/reference.py +73 -0
- guldo/_src/domains/aiinfer/compiled.json +722 -0
- guldo/_src/domains/aiinfer/decl.py +91 -0
- guldo/_src/domains/aiinfer/reference.py +42 -0
- guldo/_src/domains/dlog/compiled.json +248 -0
- guldo/_src/domains/dlog/decl.py +41 -0
- guldo/_src/domains/dlog/reference.py +10 -0
- guldo/_src/domains/maxcut/compiled.json +291 -0
- guldo/_src/domains/maxcut/decl.py +46 -0
- guldo/_src/domains/maxcut/reference.py +16 -0
- guldo/_src/drift_check.py +89 -0
- guldo/_src/export_bundle.py +750 -0
- guldo/_src/extrapolate.py +326 -0
- guldo/_src/families.py +53 -0
- guldo/_src/gen_cheat_table.py +36 -0
- guldo/_src/gpu_meter.py +634 -0
- guldo/_src/gpu_runner.py +63 -0
- guldo/_src/harness.py +273 -0
- guldo/_src/instances.py +86 -0
- guldo/_src/instances_factor.py +40 -0
- guldo/_src/instances_mlwe.py +69 -0
- guldo/_src/judge.py +278 -0
- guldo/_src/limits_plan.py +158 -0
- guldo/_src/official.py +189 -0
- guldo/_src/ops.py +281 -0
- guldo/_src/portal.py +313 -0
- guldo/_src/portal_core.py +57 -0
- guldo/_src/power.py +174 -0
- guldo/_src/prereg.py +173 -0
- guldo/_src/public_portal.py +500 -0
- guldo/_src/quality_core.py +126 -0
- guldo/_src/recheck.py +236 -0
- guldo/_src/remeasure.py +88 -0
- guldo/_src/report_html.py +493 -0
- guldo/_src/results/.keep +0 -0
- guldo/_src/rounds.py +846 -0
- guldo/_src/runner.py +52 -0
- guldo/_src/sandbox/Dockerfile +11 -0
- guldo/_src/sandbox/Dockerfile.v2 +19 -0
- guldo/_src/sandbox/README.md +40 -0
- guldo/_src/sandbox/linux_selftest.py +117 -0
- guldo/_src/sandbox/meter.py +89 -0
- guldo/_src/sandbox/meter_core.py +28 -0
- guldo/_src/site_assets.py +176 -0
- guldo/_src/site_live_check.py +79 -0
- guldo/_src/solvers/aidecode_flops_kv.py +73 -0
- guldo/_src/solvers/aidecode_flops_lossy.py +73 -0
- guldo/_src/solvers/aidecode_flops_ref.py +64 -0
- guldo/_src/solvers/aidecode_kv.py +76 -0
- guldo/_src/solvers/aidecode_kv_f32.py +77 -0
- guldo/_src/solvers/aidecode_lossy.py +76 -0
- guldo/_src/solvers/aidecode_repeat.py +5 -0
- guldo/_src/solvers/aiinfer_batched.py +40 -0
- guldo/_src/solvers/aiinfer_f32.py +41 -0
- guldo/_src/solvers/aiinfer_gpu_batched.py +42 -0
- guldo/_src/solvers/aiinfer_gpu_ref.py +45 -0
- guldo/_src/solvers/aiinfer_lossy.py +42 -0
- guldo/_src/solvers/baseline_bkz.py +14 -0
- guldo/_src/solvers/cheat_lookup.py +24 -0
- guldo/_src/solvers/cheat_table.json +1 -0
- guldo/_src/solvers/cheat_table_v1_stale.json +1 -0
- guldo/_src/solvers/daemon_cpu_hider.py +17 -0
- guldo/_src/solvers/dlog_bsgs.py +19 -0
- guldo/_src/solvers/dlog_stride4.py +18 -0
- guldo/_src/solvers/egg_base.py +11 -0
- guldo/_src/solvers/egg_eightaxis.py +68 -0
- guldo/_src/solvers/egg_template.py +11 -0
- guldo/_src/solvers/factor_brent.py +34 -0
- guldo/_src/solvers/factor_floyd.py +19 -0
- guldo/_src/solvers/factor_trial_fast_py.py +21 -0
- guldo/_src/solvers/factor_trial_np.py +21 -0
- guldo/_src/solvers/factor_trial_py.py +12 -0
- guldo/_src/solvers/fixed_overhead_ref.py +21 -0
- guldo/_src/solvers/fork_bomb.py +18 -0
- guldo/_src/solvers/gpu_noop.py +6 -0
- guldo/_src/solvers/hostile_probe.py +36 -0
- guldo/_src/solvers/kill_meter.py +27 -0
- guldo/_src/solvers/memory_bomb.py +10 -0
- guldo/_src/solvers/noop.py +5 -0
- guldo/_src/solvers/orphan_cpu_hider.py +16 -0
- guldo/_src/solvers/planted_peek.py +23 -0
- guldo/_src/solvers/sleeping_daemon_hang.py +12 -0
- guldo/_src/solvers/small_n_trick.py +22 -0
- guldo/_src/solvers/starter_template.py +21 -0
- guldo/_src/solvers/stdout_flood.py +9 -0
- guldo/_src/solvers/tmp_fill.py +10 -0
- guldo/_src/solvers/tuned_bkz.py +25 -0
- guldo/_src/static_check.py +214 -0
- guldo/_src/submit_check.py +166 -0
- guldo/_src/svp.py +889 -0
- guldo/_src/tensorops.py +304 -0
- guldo/_src/tflops.py +453 -0
- guldo/_src/tflops_runner.py +134 -0
- guldo/cli.py +60 -0
- guldo_cli-0.1.0.dist-info/METADATA +30 -0
- guldo_cli-0.1.0.dist-info/RECORD +134 -0
- guldo_cli-0.1.0.dist-info/WHEEL +5 -0
- guldo_cli-0.1.0.dist-info/entry_points.txt +2 -0
- guldo_cli-0.1.0.dist-info/top_level.txt +1 -0
guldo/_src/gpu_runner.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""Child process of gpu_meter.py: load a solver file, then answer one request per line. It supplies NO timing: the parent times every request itself.
|
|
2
|
+
|
|
3
|
+
argv[1] the solver file (a top-level `solve`; torch and numpy are available)
|
|
4
|
+
stdin one JSON request per line: {"op": "run", "args": [...]} or {"op": "quit"}
|
|
5
|
+
stdout first {"status": "ready", "torch": ..., "device": ...}; then, per request, {"status": "ok", "answer": ...} or {"status": "error", "err": "..."}
|
|
6
|
+
|
|
7
|
+
The parent measures from the moment it writes a request to the moment it reads the reply, on its own clock. This process therefore cannot under-report: it has nothing to report
|
|
8
|
+
except the answer. Each request is bracketed by torch.cuda.synchronize() so asynchronous kernels finish inside the measured interval. Importing the solver (and so any one-time
|
|
9
|
+
setup at module level) happens before `ready` and is not measured, like loading a model; per-instance work must happen in solve(). cuDNN autotuning is off and deterministic
|
|
10
|
+
algorithms are requested (best effort: a warning where none exists).
|
|
11
|
+
|
|
12
|
+
UNRUN: this file was written on a machine with no GPU and no torch. The parent-side protocol is tested against a fake transport in test_gpu_meter.py; this child never ran.
|
|
13
|
+
The solver runs unsandboxed with the operator's rights: this is an operator-trusted meter (see gpu_meter.py), not a containment.
|
|
14
|
+
"""
|
|
15
|
+
import importlib.util
|
|
16
|
+
import json
|
|
17
|
+
import sys
|
|
18
|
+
|
|
19
|
+
import runner
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _emit(obj):
|
|
23
|
+
sys.__stdout__.write("\n" + json.dumps(obj) + "\n") # a leading blank line: the parent skips blank lines, so stray output cannot glue itself to a reply
|
|
24
|
+
sys.__stdout__.flush()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def main():
|
|
28
|
+
sys.stdout = sys.stderr # a solver's print() must not reach the reply channel
|
|
29
|
+
try:
|
|
30
|
+
import torch
|
|
31
|
+
except ImportError:
|
|
32
|
+
_emit({"status": "error", "err": "torch is not installed"})
|
|
33
|
+
return
|
|
34
|
+
if not torch.cuda.is_available():
|
|
35
|
+
_emit({"status": "error", "err": "torch reports no CUDA device"})
|
|
36
|
+
return
|
|
37
|
+
try:
|
|
38
|
+
torch.backends.cudnn.benchmark = False
|
|
39
|
+
torch.use_deterministic_algorithms(True, warn_only=True)
|
|
40
|
+
spec = importlib.util.spec_from_file_location("submission", sys.argv[1])
|
|
41
|
+
mod = importlib.util.module_from_spec(spec)
|
|
42
|
+
spec.loader.exec_module(mod)
|
|
43
|
+
except BaseException as e:
|
|
44
|
+
_emit({"status": "error", "err": f"could not load the solver: {type(e).__name__}: {str(e)[:300]}"})
|
|
45
|
+
return
|
|
46
|
+
_emit({"status": "ready", "torch": torch.__version__, "device": torch.cuda.get_device_name(0)})
|
|
47
|
+
for line in sys.stdin:
|
|
48
|
+
try:
|
|
49
|
+
req = json.loads(line)
|
|
50
|
+
if req.get("op") == "quit":
|
|
51
|
+
break
|
|
52
|
+
if req.get("op") != "run":
|
|
53
|
+
raise ValueError(f"unknown op {req.get('op')!r}")
|
|
54
|
+
torch.cuda.synchronize()
|
|
55
|
+
answer = runner.normalize(mod.solve(*req["args"]))
|
|
56
|
+
torch.cuda.synchronize()
|
|
57
|
+
_emit({"status": "ok", "answer": answer})
|
|
58
|
+
except BaseException as e:
|
|
59
|
+
_emit({"status": "error", "err": f"{type(e).__name__}: {str(e)[:300]}"})
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
if __name__ == "__main__":
|
|
63
|
+
main()
|
guldo/_src/harness.py
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
"""Run a solver over a size ladder on derived instances of any registered domain (families.py); verify every answer; log CPU.
|
|
2
|
+
|
|
3
|
+
Cost = user+sys CPU seconds of the solver process tree: that child's own wait4 rusage (bare local mode) or the root meter's
|
|
4
|
+
RUSAGE_CHILDREN (Docker), minus the cost of a no-op solver (interpreter + import overhead). The solver receives only
|
|
5
|
+
solve(*instance["args"]) - for lattices (basis, bound2), for factoring (N,) - never the master seed or the planted witness.
|
|
6
|
+
With --jobs K, K instances run concurrently (local mode only; see drift_check.py / remeasure.py for the measured bias).
|
|
7
|
+
"""
|
|
8
|
+
import argparse
|
|
9
|
+
import hashlib
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
import platform
|
|
13
|
+
import resource
|
|
14
|
+
import select
|
|
15
|
+
import signal
|
|
16
|
+
import statistics
|
|
17
|
+
import subprocess
|
|
18
|
+
import sys
|
|
19
|
+
import tempfile
|
|
20
|
+
import threading
|
|
21
|
+
import time
|
|
22
|
+
import uuid
|
|
23
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
24
|
+
|
|
25
|
+
import families
|
|
26
|
+
import instances
|
|
27
|
+
|
|
28
|
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
29
|
+
NOOP = os.path.join(HERE, "solvers", "noop.py")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
TAIL_LIMIT = {"out": 4 << 20, "err": 64 << 10} # bytes kept per stream in local mode (the docker meter keeps its last 4 MB too)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def backstop(budget):
|
|
36
|
+
"""Wall-clock limit for a local run: the backstop for a solver that ignores SIGXCPU (macOS) or just sleeps."""
|
|
37
|
+
return budget * 1.5 + 10
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def max_safe_jobs():
|
|
41
|
+
"""Largest --jobs that keeps every worker on a performance core with two cores to spare. Counting efficiency cores
|
|
42
|
+
would let workers land on slower cores and bias timings; validated range is 4-8 jobs on a 10-P-core machine."""
|
|
43
|
+
n = os.cpu_count() or 1
|
|
44
|
+
if sys.platform == "darwin":
|
|
45
|
+
try:
|
|
46
|
+
perf = int(subprocess.run(["sysctl", "-n", "hw.perflevel0.logicalcpu"], capture_output=True, text=True,
|
|
47
|
+
timeout=10).stdout.strip())
|
|
48
|
+
n = min(n, perf)
|
|
49
|
+
except (ValueError, OSError, subprocess.TimeoutExpired):
|
|
50
|
+
pass
|
|
51
|
+
return max(1, n - 2)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _exec_local(solver, payload, budget):
|
|
55
|
+
"""Bare local process. CPU is that child's OWN rusage from wait4 (user+sys, including descendants it waited for), not
|
|
56
|
+
a process-wide delta, so several instances can run concurrently without corrupting each other's timings."""
|
|
57
|
+
env = {"PATH": os.environ.get("PATH", ""), "PYTHONHASHSEED": "0", "OMP_NUM_THREADS": "1",
|
|
58
|
+
"SVP_CPU_LIMIT": str(int(budget) + 1)} # applied by runner.py; no preexec_fn (unsafe with threads)
|
|
59
|
+
bufs = {}
|
|
60
|
+
stop = threading.Event()
|
|
61
|
+
with tempfile.TemporaryDirectory() as cwd:
|
|
62
|
+
# own session: leftover orphans (they share the child's process group) can be killed when the child is done
|
|
63
|
+
p = subprocess.Popen([sys.executable, os.path.join(HERE, "runner.py"), solver],
|
|
64
|
+
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
65
|
+
cwd=cwd, env=env, start_new_session=True)
|
|
66
|
+
|
|
67
|
+
def feed():
|
|
68
|
+
try:
|
|
69
|
+
p.stdin.write(payload.encode())
|
|
70
|
+
except OSError:
|
|
71
|
+
pass
|
|
72
|
+
finally:
|
|
73
|
+
try:
|
|
74
|
+
p.stdin.close()
|
|
75
|
+
except OSError:
|
|
76
|
+
pass
|
|
77
|
+
|
|
78
|
+
def drain(name, stream):
|
|
79
|
+
"""Non-blocking, stoppable reader. A blocking read() would sit forever if an orphan holds the pipe open, and
|
|
80
|
+
closing the stream from another thread then blocks on its lock (a sleeping orphan used to hang the harness)."""
|
|
81
|
+
fd = stream.fileno()
|
|
82
|
+
os.set_blocking(fd, False)
|
|
83
|
+
limit = TAIL_LIMIT[name]
|
|
84
|
+
buf = bytearray() # only the TAIL is kept: the answer is the last stdout line and only the end of stderr is reported
|
|
85
|
+
while True:
|
|
86
|
+
ready, _, _ = select.select([fd], [], [], 0.2)
|
|
87
|
+
if ready:
|
|
88
|
+
try:
|
|
89
|
+
data = os.read(fd, 1 << 16)
|
|
90
|
+
except BlockingIOError:
|
|
91
|
+
continue
|
|
92
|
+
except OSError:
|
|
93
|
+
break
|
|
94
|
+
if not data: # EOF: every writer closed
|
|
95
|
+
break
|
|
96
|
+
buf += data
|
|
97
|
+
if len(buf) > 2 * limit: # a solver that prints without end must not grow the harness's memory without bound
|
|
98
|
+
del buf[:len(buf) - limit]
|
|
99
|
+
elif stop.is_set():
|
|
100
|
+
break
|
|
101
|
+
bufs[name] = bytes(buf[-limit:])
|
|
102
|
+
|
|
103
|
+
threads = [threading.Thread(target=feed, daemon=True),
|
|
104
|
+
threading.Thread(target=drain, args=("out", p.stdout), daemon=True),
|
|
105
|
+
threading.Thread(target=drain, args=("err", p.stderr), daemon=True)]
|
|
106
|
+
for t in threads:
|
|
107
|
+
t.start()
|
|
108
|
+
|
|
109
|
+
def kill():
|
|
110
|
+
try:
|
|
111
|
+
p.kill()
|
|
112
|
+
except OSError:
|
|
113
|
+
pass
|
|
114
|
+
|
|
115
|
+
# Wall backstop for a solver that ignores SIGXCPU (macOS does not SIGKILL at the hard limit; Linux does). CPU-bound
|
|
116
|
+
# solvers have wall ~ CPU as long as jobs <= cores (main() refuses oversubscription), so 1.5x + 10 s is generous.
|
|
117
|
+
timer = threading.Timer(backstop(budget), kill)
|
|
118
|
+
timer.start()
|
|
119
|
+
try:
|
|
120
|
+
_, status, ru = os.wait4(p.pid, 0)
|
|
121
|
+
finally:
|
|
122
|
+
timer.cancel()
|
|
123
|
+
grace = time.time() + 2.0 # normally the readers see EOF at once; an orphan still writing gets 2 s
|
|
124
|
+
for t in threads[1:]:
|
|
125
|
+
t.join(timeout=max(grace - time.time(), 0))
|
|
126
|
+
try:
|
|
127
|
+
os.killpg(p.pid, signal.SIGKILL) # orphans left in the child's session (bare local mode cannot bill them)
|
|
128
|
+
except OSError:
|
|
129
|
+
pass
|
|
130
|
+
stop.set()
|
|
131
|
+
for t in threads:
|
|
132
|
+
t.join(timeout=5)
|
|
133
|
+
for stream in (p.stdin, p.stdout, p.stderr): # we manage the pipes ourselves: close them, or each run leaks 3 fds
|
|
134
|
+
try:
|
|
135
|
+
stream.close()
|
|
136
|
+
except OSError:
|
|
137
|
+
pass
|
|
138
|
+
rc = -os.WTERMSIG(status) if os.WIFSIGNALED(status) else os.WEXITSTATUS(status)
|
|
139
|
+
p.returncode = rc
|
|
140
|
+
return (rc, bufs.get("out", b"").decode(errors="replace"), bufs.get("err", b"").decode(errors="replace"),
|
|
141
|
+
ru.ru_utime + ru.ru_stime)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
DOCKER_SLACK = 60 # seconds beyond the meter's own wall deadline before the host gives up on `docker run`
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def cpu_brand():
|
|
148
|
+
"""Human-readable CPU model, so ladders run on different machines can be refused."""
|
|
149
|
+
try:
|
|
150
|
+
if sys.platform == "darwin":
|
|
151
|
+
return subprocess.run(["sysctl", "-n", "machdep.cpu.brand_string"], capture_output=True, text=True,
|
|
152
|
+
timeout=10).stdout.strip() or "unknown"
|
|
153
|
+
with open("/proc/cpuinfo") as f:
|
|
154
|
+
for line in f:
|
|
155
|
+
if line.startswith("model name"):
|
|
156
|
+
return line.split(":", 1)[1].strip()
|
|
157
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
158
|
+
pass
|
|
159
|
+
return platform.processor() or "unknown"
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def image_id(image="svp-solver"):
|
|
163
|
+
try:
|
|
164
|
+
r = subprocess.run(["docker", "image", "inspect", "--format", "{{.Id}}", image], capture_output=True,
|
|
165
|
+
text=True, timeout=30)
|
|
166
|
+
return r.stdout.strip() if r.returncode == 0 else "unknown"
|
|
167
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
168
|
+
return "unknown"
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _exec_docker(solver, payload, budget, image="svp-solver"):
|
|
172
|
+
"""Run in the hardened container; CPU is metered inside it by sandbox/meter.py (see sandbox/README.md).
|
|
173
|
+
UNTESTED against a real Docker daemon since the meter rewrite; the control flow is tested with a fake `docker`."""
|
|
174
|
+
name = "svp-" + uuid.uuid4().hex[:12]
|
|
175
|
+
cmd = ["docker", "run", "--rm", "-i", "--name", name, "--network", "none", "--read-only", "--tmpfs", "/tmp",
|
|
176
|
+
"--cpus", "1", "--memory", "4g", "--pids-limit", "64", "--cap-drop", "ALL", "--cap-add", "SETUID",
|
|
177
|
+
"--cap-add", "SETGID", "--cap-add", "KILL", "--security-opt", "no-new-privileges", "-e", f"BUDGET={budget}",
|
|
178
|
+
"-v", f"{solver}:/work/submission.py:ro", image, "/work/submission.py"]
|
|
179
|
+
try:
|
|
180
|
+
r = subprocess.run(cmd, input=payload.encode(), capture_output=True, timeout=budget * 3 + DOCKER_SLACK)
|
|
181
|
+
except subprocess.TimeoutExpired:
|
|
182
|
+
# subprocess.run kills only the docker CLI; the container would keep burning CPU and skew later timings
|
|
183
|
+
subprocess.run(["docker", "kill", name], capture_output=True)
|
|
184
|
+
subprocess.run(["docker", "rm", "-f", name], capture_output=True)
|
|
185
|
+
return -signal.SIGKILL, "", "docker timeout", budget * 3
|
|
186
|
+
try:
|
|
187
|
+
m = json.loads(r.stdout.decode().strip().splitlines()[-1])
|
|
188
|
+
except Exception:
|
|
189
|
+
return r.returncode or 1, "", "meter produced no result: " + r.stderr.decode(errors="replace")[-300:], 0.0
|
|
190
|
+
return (-signal.SIGXCPU if m.get("overrun") else m["rc"]), m["out"], m["err"], m["cpu"]
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def run_one(solver, inst, budget, sandbox=None):
|
|
194
|
+
payload = json.dumps({"args": inst["args"]}) # domain-neutral: the solver is called as solve(*args)
|
|
195
|
+
t0 = time.time()
|
|
196
|
+
rc, out, err, cpu = (_exec_docker if sandbox == "docker" else _exec_local)(solver, payload, budget)
|
|
197
|
+
rec = {"id": inst["id"], "dim": inst["dim"], "cpu": cpu, "wall": time.time() - t0}
|
|
198
|
+
if rc == -signal.SIGXCPU or cpu > budget or (sandbox != "docker" and rc == -signal.SIGKILL and rec["wall"] >= backstop(budget) - 1):
|
|
199
|
+
rec["status"] = "timeout" # CPU cap, CPU over budget, or killed by the wall backstop (e.g. a solver that only sleeps)
|
|
200
|
+
elif rc != 0:
|
|
201
|
+
rec["status"] = "crash"
|
|
202
|
+
rec["err"] = err[-1500:]
|
|
203
|
+
elif not out.strip():
|
|
204
|
+
# exit status 0 but nothing on the answer channel: the runner never reached its write (e.g. os._exit(0) inside solve).
|
|
205
|
+
# That is not a wrong answer, and it must not look like one on the board.
|
|
206
|
+
rec["status"] = "crash"
|
|
207
|
+
rec["err"] = "solver exited 0 without writing an answer (no answer line)"
|
|
208
|
+
else:
|
|
209
|
+
try:
|
|
210
|
+
ans = json.loads(out.strip().splitlines()[-1]) # the answer is the LAST line; anything earlier is noise
|
|
211
|
+
ok = families.verify(inst, ans)
|
|
212
|
+
except Exception:
|
|
213
|
+
ans, ok = 0, False
|
|
214
|
+
rec["status"] = "solved" if ok else ("failed" if ans is None else "wrong")
|
|
215
|
+
return rec
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def calibrate(sandbox=None, reps=7):
|
|
219
|
+
inst = families.derive("gm", instances.PRACTICE_SEED, 10, 0)
|
|
220
|
+
return statistics.median(run_one(NOOP, inst, 30, sandbox)["cpu"] for _ in range(reps))
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def main():
|
|
224
|
+
ap = argparse.ArgumentParser()
|
|
225
|
+
ap.add_argument("--solver", required=True)
|
|
226
|
+
ap.add_argument("--dims", type=int, nargs="+", required=True)
|
|
227
|
+
ap.add_argument("--n", type=int, default=5, help="instances per dimension")
|
|
228
|
+
ap.add_argument("--budget", type=float, default=60, help="CPU-second cap per instance")
|
|
229
|
+
ap.add_argument("--seed-file", help="file holding the master seed bytes (default: public practice seed)")
|
|
230
|
+
ap.add_argument("--family", default="gm", choices=sorted(families.FAMILIES))
|
|
231
|
+
ap.add_argument("--sandbox", choices=["docker"], help="run each instance in the hardened container")
|
|
232
|
+
ap.add_argument("--jobs", type=int, default=1, help="instances run concurrently (local mode only). Timings stay "
|
|
233
|
+
"valid only while the machine is not oversubscribed; validate with `drift_check.py` first")
|
|
234
|
+
ap.add_argument("--out", required=True)
|
|
235
|
+
a = ap.parse_args()
|
|
236
|
+
if a.jobs > 1 and a.sandbox:
|
|
237
|
+
raise SystemExit("REFUSED: --jobs > 1 is local-mode only (concurrent sandbox runs are untested)")
|
|
238
|
+
if a.jobs > max_safe_jobs():
|
|
239
|
+
raise SystemExit(f"REFUSED: --jobs {a.jobs} exceeds the safe maximum {max_safe_jobs()} for this machine (performance "
|
|
240
|
+
"cores minus 2): contention or efficiency cores would distort timings, and the wall backstop "
|
|
241
|
+
"assumes wall time ~ CPU time")
|
|
242
|
+
a.solver = os.path.abspath(a.solver)
|
|
243
|
+
with open(a.solver, "rb") as f:
|
|
244
|
+
solver_sha = hashlib.sha256(f.read()).hexdigest() # recorded so seeds scored with different code are never combined
|
|
245
|
+
master = open(a.seed_file, "rb").read().strip() if a.seed_file else instances.PRACTICE_SEED
|
|
246
|
+
if not a.sandbox:
|
|
247
|
+
print("WARNING: local mode is NOT safe for untrusted solvers (can read the seed, can orphan processes to hide "
|
|
248
|
+
"CPU). Use --sandbox docker for anything you did not write.", file=sys.stderr, flush=True)
|
|
249
|
+
overhead = calibrate(a.sandbox)
|
|
250
|
+
print(f"family={a.family} sandbox={a.sandbox} commitment sha256={instances.commit(master)} overhead={overhead:.3f}s", flush=True)
|
|
251
|
+
with open(a.out, "w") as f:
|
|
252
|
+
f.write(json.dumps({"meta": True, "solver": os.path.basename(a.solver), "commit": instances.commit(master),
|
|
253
|
+
"overhead": overhead, "budget": a.budget, "family": a.family, "sandbox": a.sandbox,
|
|
254
|
+
"instance_version": families.VERSIONS[a.family],
|
|
255
|
+
"image": image_id() if a.sandbox == "docker" else None,
|
|
256
|
+
"host": platform.platform() + " / " + platform.processor(),
|
|
257
|
+
"cpu": cpu_brand(), "ncpu": os.cpu_count(), "jobs": a.jobs, "solver_sha256": solver_sha}) + "\n")
|
|
258
|
+
def work(task):
|
|
259
|
+
dim, idx = task
|
|
260
|
+
rec = run_one(a.solver, families.derive(a.family, master, dim, idx), a.budget, a.sandbox)
|
|
261
|
+
rec["cpu_net"] = max(rec["cpu"] - overhead, 1e-3)
|
|
262
|
+
return rec
|
|
263
|
+
|
|
264
|
+
tasks = [(dim, idx) for dim in a.dims for idx in range(a.n)]
|
|
265
|
+
with ThreadPoolExecutor(max_workers=max(a.jobs, 1)) as ex:
|
|
266
|
+
for rec in ex.map(work, tasks): # results come back in task order, so the file is deterministic
|
|
267
|
+
f.write(json.dumps(rec) + "\n")
|
|
268
|
+
f.flush()
|
|
269
|
+
print(f'{a.solver.split("/")[-1]:22s} {rec["id"]:>7s} {rec["status"]:8s} {rec["cpu_net"]:8.2f}s', flush=True)
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
if __name__ == "__main__":
|
|
273
|
+
main()
|
guldo/_src/instances.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""Commit-reveal planted-free SVP instances (Goldstein-Mayer lattices) with NP-checkable solutions.
|
|
2
|
+
|
|
3
|
+
Every instance is a pure function of (master_seed, dim, idx). The judge publishes
|
|
4
|
+
sha256(master_seed) before the submission deadline and reveals master_seed after,
|
|
5
|
+
so a submission cannot be tuned to, or contain answers for, the judged instances.
|
|
6
|
+
"""
|
|
7
|
+
import hashlib
|
|
8
|
+
import hmac
|
|
9
|
+
import math
|
|
10
|
+
import random
|
|
11
|
+
|
|
12
|
+
PRACTICE_SEED = b"practice-seed-public"
|
|
13
|
+
GAP = 1.05 # solution must have norm <= GAP * Gaussian heuristic (Darmstadt SVP-challenge convention)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def commit(master: bytes) -> str:
|
|
17
|
+
return hashlib.sha256(master).hexdigest()
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _is_prime(n: int) -> bool:
|
|
21
|
+
if n < 2:
|
|
22
|
+
return False
|
|
23
|
+
small = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53)
|
|
24
|
+
for q in small:
|
|
25
|
+
if n % q == 0:
|
|
26
|
+
return n == q
|
|
27
|
+
d, s = n - 1, 0
|
|
28
|
+
while d % 2 == 0:
|
|
29
|
+
d //= 2
|
|
30
|
+
s += 1
|
|
31
|
+
for a in small:
|
|
32
|
+
x = pow(a, d, n)
|
|
33
|
+
if x in (1, n - 1):
|
|
34
|
+
continue
|
|
35
|
+
for _ in range(s - 1):
|
|
36
|
+
x = x * x % n
|
|
37
|
+
if x == n - 1:
|
|
38
|
+
break
|
|
39
|
+
else:
|
|
40
|
+
return False
|
|
41
|
+
return True
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _next_prime(n: int) -> int:
|
|
45
|
+
n |= 1
|
|
46
|
+
while not _is_prime(n):
|
|
47
|
+
n += 2
|
|
48
|
+
return n
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def derive(master: bytes, dim: int, idx: int) -> dict:
|
|
52
|
+
h = hmac.new(master, f"svp-gm-v2:{dim}:{idx}".encode(), hashlib.sha256).digest()
|
|
53
|
+
rng = random.Random(h)
|
|
54
|
+
p = _next_prime(rng.getrandbits(10 * dim) | (1 << (10 * dim - 1)))
|
|
55
|
+
log_gh2 = (2.0 / dim) * math.lgamma(dim / 2 + 1) - math.log(math.pi) + (2.0 / dim) * math.log(p)
|
|
56
|
+
gh2 = math.exp(log_gh2)
|
|
57
|
+
bound2 = int(GAP * GAP * gh2)
|
|
58
|
+
# Plant a typical-length (~Gaussian heuristic) vector w so every instance is guaranteed solvable. Without this,
|
|
59
|
+
# roughly the shortest vector can exceed 1.05*GH at small dimension (observed at dim 40 and 45).
|
|
60
|
+
sigma = math.sqrt(gh2 / dim)
|
|
61
|
+
while True:
|
|
62
|
+
w = [int(round(rng.gauss(0, sigma))) for _ in range(dim)]
|
|
63
|
+
if w[1] != 0 and 0.8 * gh2 <= sum(c * c for c in w) <= bound2:
|
|
64
|
+
break
|
|
65
|
+
xs = [rng.randrange(p) for _ in range(dim - 1)] # xs[i-1] is x_i, the first coordinate of basis row i
|
|
66
|
+
rest = sum(w[i] * xs[i - 1] for i in range(2, dim))
|
|
67
|
+
xs[0] = ((w[0] - rest) * pow(w[1] % p, -1, p)) % p # forces w[0] - sum_i w[i]*x_i == 0 (mod p)
|
|
68
|
+
basis = [[p] + [0] * (dim - 1)]
|
|
69
|
+
for i, x in enumerate(xs, start=1):
|
|
70
|
+
row = [x] + [0] * (dim - 1)
|
|
71
|
+
row[i] = 1
|
|
72
|
+
basis.append(row)
|
|
73
|
+
return {"id": f"{dim}:{idx}", "dim": dim, "idx": idx, "p": p, "basis": basis, "bound2": bound2, "planted": w}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def verify(inst: dict, v) -> bool:
|
|
77
|
+
"""Solution check: nonzero lattice vector with squared norm <= bound2. Exact integer arithmetic."""
|
|
78
|
+
n, p = inst["dim"], inst["p"]
|
|
79
|
+
if not isinstance(v, list) or len(v) != n or not all(isinstance(c, int) and not isinstance(c, bool) for c in v):
|
|
80
|
+
return False
|
|
81
|
+
if all(c == 0 for c in v):
|
|
82
|
+
return False
|
|
83
|
+
x = inst["basis"]
|
|
84
|
+
if (v[0] - sum(v[i] * x[i][0] for i in range(1, n))) % p != 0:
|
|
85
|
+
return False
|
|
86
|
+
return sum(c * c for c in v) <= inst["bound2"]
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Second domain: integer factoring. Commit-reveal semiprimes N = p*q with an exact, trivial answer check.
|
|
2
|
+
|
|
3
|
+
Same contract as the lattice families (see families.py): an instance is a pure function of (master seed, size, index), the
|
|
4
|
+
solver sees only `args` (here [N]), and the judge verifies with exact arithmetic. An answer is ANY nontrivial divisor d of N
|
|
5
|
+
(1 < d < N), so a solver is not required to return the planted factor; the check is one modulo operation.
|
|
6
|
+
|
|
7
|
+
size = bits of N. p and q are random primes of about size/2 bits each, so the smaller factor is ~size/2 bits and Pollard-rho
|
|
8
|
+
style algorithms need ~2^(size/4) steps: cost grows smoothly and, because rho's run time is geometric-like, per-instance
|
|
9
|
+
difficulty is heavy-tailed, which is exactly the regime the difficulty-stratified claim is built for.
|
|
10
|
+
"""
|
|
11
|
+
import hashlib
|
|
12
|
+
import hmac
|
|
13
|
+
import random
|
|
14
|
+
|
|
15
|
+
import instances # reuse the Miller-Rabin primality test
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _prime(rng, bits):
|
|
19
|
+
while True:
|
|
20
|
+
c = rng.getrandbits(bits) | (1 << (bits - 1)) | 1
|
|
21
|
+
if instances._is_prime(c):
|
|
22
|
+
return c
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def derive(master: bytes, size: int, idx: int) -> dict:
|
|
26
|
+
if size < 16:
|
|
27
|
+
raise ValueError("size (bits of N) must be at least 16")
|
|
28
|
+
rng = random.Random(hmac.new(master, f"factor-v1:{size}:{idx}".encode(), hashlib.sha256).digest())
|
|
29
|
+
pb = size // 2
|
|
30
|
+
while True:
|
|
31
|
+
p, q = _prime(rng, pb), _prime(rng, size - pb)
|
|
32
|
+
if p != q:
|
|
33
|
+
break
|
|
34
|
+
n = p * q
|
|
35
|
+
return {"id": f"{size}:{idx}", "dim": size, "idx": idx, "args": [n], "n": n, "planted": sorted([p, q])}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def verify(inst: dict, ans) -> bool:
|
|
39
|
+
n = inst["n"]
|
|
40
|
+
return isinstance(ans, int) and not isinstance(ans, bool) and 1 < ans < n and n % ans == 0
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""Module-LWE-shaped planted instances as Kannan-embedding lattices, with an exact NP-checkable verifier.
|
|
2
|
+
|
|
3
|
+
A = k x k block matrix of negacyclic ring multiplications over Z_q[x]/(x^N+1) (Kyber's ring structure, small N).
|
|
4
|
+
b = A s + e mod q with centered-binomial s and rounded-Gaussian e (sigma SIGMA). Lattice of (x, y, t) in Z^(m+n+1) with x = t*b - A*y (mod q);
|
|
5
|
+
the planted vector is (e, s, 1). The judge accepts ANY nonzero lattice vector with squared norm <= bound2, so a
|
|
6
|
+
competitor is not required to recover the planted secret, and the check is just modular arithmetic.
|
|
7
|
+
Lattice dimension d = 2*N*k + 1.
|
|
8
|
+
"""
|
|
9
|
+
import hashlib
|
|
10
|
+
import hmac
|
|
11
|
+
import random
|
|
12
|
+
|
|
13
|
+
Q = 3329
|
|
14
|
+
N = 4 # ring dimension; dim = 2*N*k + 1, so the ladder steps by 8 (N=8 stepped by 16: too coarse, 1 usable dimension)
|
|
15
|
+
ETA = 2 # secret: centered binomial
|
|
16
|
+
SIGMA = 8.0 # error: rounded Gaussian std (noise rate alpha = SIGMA / Q); calibrated so BKZ cost grows with dimension
|
|
17
|
+
SLACK = 1.5 # accepted norm = SLACK * (expected norm of the planted vector)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _cbd(rng, eta):
|
|
21
|
+
return sum(rng.getrandbits(1) for _ in range(eta)) - sum(rng.getrandbits(1) for _ in range(eta))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def derive(master: bytes, dim: int, idx: int) -> dict:
|
|
25
|
+
if dim < 2 * N + 1 or (dim - 1) % (2 * N):
|
|
26
|
+
raise ValueError(f"mlwe dim must be 2*{N}*k+1, got {dim}")
|
|
27
|
+
k = (dim - 1) // (2 * N)
|
|
28
|
+
n = m = N * k
|
|
29
|
+
rng = random.Random(hmac.new(master, f"mlwe-v2:{dim}:{idx}".encode(), hashlib.sha256).digest())
|
|
30
|
+
A = [[0] * n for _ in range(m)]
|
|
31
|
+
for bi in range(k):
|
|
32
|
+
for bj in range(k):
|
|
33
|
+
a = [rng.randrange(Q) for _ in range(N)]
|
|
34
|
+
for r in range(N):
|
|
35
|
+
for c in range(N):
|
|
36
|
+
i = r - c
|
|
37
|
+
A[bi * N + r][bj * N + c] = a[i] if i >= 0 else (-a[i + N]) % Q
|
|
38
|
+
s = [_cbd(rng, ETA) for _ in range(n)]
|
|
39
|
+
e = [int(round(rng.gauss(0, SIGMA))) for _ in range(m)]
|
|
40
|
+
b = [(sum(A[i][j] * s[j] for j in range(n)) + e[i]) % Q for i in range(m)]
|
|
41
|
+
d = m + n + 1
|
|
42
|
+
basis = []
|
|
43
|
+
for i in range(m):
|
|
44
|
+
row = [0] * d
|
|
45
|
+
row[i] = Q
|
|
46
|
+
basis.append(row)
|
|
47
|
+
for j in range(n):
|
|
48
|
+
row = [0] * d
|
|
49
|
+
for i in range(m):
|
|
50
|
+
row[i] = -A[i][j]
|
|
51
|
+
row[m + j] = 1
|
|
52
|
+
basis.append(row)
|
|
53
|
+
basis.append(list(b) + [0] * n + [1])
|
|
54
|
+
expected2 = m * SIGMA ** 2 + n * ETA / 2 + 1
|
|
55
|
+
return {"id": f"{dim}:{idx}", "dim": d, "idx": idx, "q": Q, "m": m, "n": n, "A": A, "b": b,
|
|
56
|
+
"basis": basis, "bound2": int(SLACK * SLACK * expected2), "planted": e + s + [1]}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def verify(inst: dict, v) -> bool:
|
|
60
|
+
d, m, n, q, A, b = inst["dim"], inst["m"], inst["n"], inst["q"], inst["A"], inst["b"]
|
|
61
|
+
if not isinstance(v, list) or len(v) != d or not all(isinstance(c, int) and not isinstance(c, bool) for c in v):
|
|
62
|
+
return False
|
|
63
|
+
if all(c == 0 for c in v):
|
|
64
|
+
return False
|
|
65
|
+
x, y, t = v[:m], v[m:m + n], v[m + n]
|
|
66
|
+
for i in range(m):
|
|
67
|
+
if (x[i] - t * b[i] + sum(A[i][j] * y[j] for j in range(n))) % q:
|
|
68
|
+
return False
|
|
69
|
+
return sum(c * c for c in v) <= inst["bound2"]
|