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.
Files changed (134) hide show
  1. guldo/__init__.py +0 -0
  2. guldo/_src/COMPILER.md +329 -0
  3. guldo/_src/DEPLOY.md +200 -0
  4. guldo/_src/LAUNCH.md +85 -0
  5. guldo/_src/PROTOCOL.md +294 -0
  6. guldo/_src/RULES.md +25 -0
  7. guldo/_src/SUBMIT.md +170 -0
  8. guldo/_src/__init__.py +0 -0
  9. guldo/_src/attest.py +257 -0
  10. guldo/_src/build_site.py +958 -0
  11. guldo/_src/bundle_templates/check.py +1382 -0
  12. guldo/_src/bundle_templates/qcheck.py +253 -0
  13. guldo/_src/bundle_templates/qscore.py +402 -0
  14. guldo/_src/bundle_templates/score.py +649 -0
  15. guldo/_src/certificate.py +828 -0
  16. guldo/_src/certs_site.py +108 -0
  17. guldo/_src/claims.py +343 -0
  18. guldo/_src/compile_domain.py +1312 -0
  19. guldo/_src/compile_quality.py +286 -0
  20. guldo/_src/count_runner.py +248 -0
  21. guldo/_src/counted.py +274 -0
  22. guldo/_src/declared.py +215 -0
  23. guldo/_src/deploy/Caddyfile +48 -0
  24. guldo/_src/deploy/ledger-backup.service +9 -0
  25. guldo/_src/deploy/ledger-backup.sh +73 -0
  26. guldo/_src/deploy/ledger-backup.timer +10 -0
  27. guldo/_src/deploy/ledger-export.service +12 -0
  28. guldo/_src/deploy/ledger-export.timer +9 -0
  29. guldo/_src/deploy/ledger-web.service +38 -0
  30. guldo/_src/deploy/pin_cpu.sh +34 -0
  31. guldo/_src/deploy/provision_eval.sh +128 -0
  32. guldo/_src/deploy_smoke.py +148 -0
  33. guldo/_src/dlog_experiment.py +165 -0
  34. guldo/_src/domains/aidecode/compiled.json +586 -0
  35. guldo/_src/domains/aidecode/decl.py +134 -0
  36. guldo/_src/domains/aidecode/reference.py +73 -0
  37. guldo/_src/domains/aiinfer/compiled.json +722 -0
  38. guldo/_src/domains/aiinfer/decl.py +91 -0
  39. guldo/_src/domains/aiinfer/reference.py +42 -0
  40. guldo/_src/domains/dlog/compiled.json +248 -0
  41. guldo/_src/domains/dlog/decl.py +41 -0
  42. guldo/_src/domains/dlog/reference.py +10 -0
  43. guldo/_src/domains/maxcut/compiled.json +291 -0
  44. guldo/_src/domains/maxcut/decl.py +46 -0
  45. guldo/_src/domains/maxcut/reference.py +16 -0
  46. guldo/_src/drift_check.py +89 -0
  47. guldo/_src/export_bundle.py +750 -0
  48. guldo/_src/extrapolate.py +326 -0
  49. guldo/_src/families.py +53 -0
  50. guldo/_src/gen_cheat_table.py +36 -0
  51. guldo/_src/gpu_meter.py +634 -0
  52. guldo/_src/gpu_runner.py +63 -0
  53. guldo/_src/harness.py +273 -0
  54. guldo/_src/instances.py +86 -0
  55. guldo/_src/instances_factor.py +40 -0
  56. guldo/_src/instances_mlwe.py +69 -0
  57. guldo/_src/judge.py +278 -0
  58. guldo/_src/limits_plan.py +158 -0
  59. guldo/_src/official.py +189 -0
  60. guldo/_src/ops.py +281 -0
  61. guldo/_src/portal.py +313 -0
  62. guldo/_src/portal_core.py +57 -0
  63. guldo/_src/power.py +174 -0
  64. guldo/_src/prereg.py +173 -0
  65. guldo/_src/public_portal.py +500 -0
  66. guldo/_src/quality_core.py +126 -0
  67. guldo/_src/recheck.py +236 -0
  68. guldo/_src/remeasure.py +88 -0
  69. guldo/_src/report_html.py +493 -0
  70. guldo/_src/results/.keep +0 -0
  71. guldo/_src/rounds.py +846 -0
  72. guldo/_src/runner.py +52 -0
  73. guldo/_src/sandbox/Dockerfile +11 -0
  74. guldo/_src/sandbox/Dockerfile.v2 +19 -0
  75. guldo/_src/sandbox/README.md +40 -0
  76. guldo/_src/sandbox/linux_selftest.py +117 -0
  77. guldo/_src/sandbox/meter.py +89 -0
  78. guldo/_src/sandbox/meter_core.py +28 -0
  79. guldo/_src/site_assets.py +176 -0
  80. guldo/_src/site_live_check.py +79 -0
  81. guldo/_src/solvers/aidecode_flops_kv.py +73 -0
  82. guldo/_src/solvers/aidecode_flops_lossy.py +73 -0
  83. guldo/_src/solvers/aidecode_flops_ref.py +64 -0
  84. guldo/_src/solvers/aidecode_kv.py +76 -0
  85. guldo/_src/solvers/aidecode_kv_f32.py +77 -0
  86. guldo/_src/solvers/aidecode_lossy.py +76 -0
  87. guldo/_src/solvers/aidecode_repeat.py +5 -0
  88. guldo/_src/solvers/aiinfer_batched.py +40 -0
  89. guldo/_src/solvers/aiinfer_f32.py +41 -0
  90. guldo/_src/solvers/aiinfer_gpu_batched.py +42 -0
  91. guldo/_src/solvers/aiinfer_gpu_ref.py +45 -0
  92. guldo/_src/solvers/aiinfer_lossy.py +42 -0
  93. guldo/_src/solvers/baseline_bkz.py +14 -0
  94. guldo/_src/solvers/cheat_lookup.py +24 -0
  95. guldo/_src/solvers/cheat_table.json +1 -0
  96. guldo/_src/solvers/cheat_table_v1_stale.json +1 -0
  97. guldo/_src/solvers/daemon_cpu_hider.py +17 -0
  98. guldo/_src/solvers/dlog_bsgs.py +19 -0
  99. guldo/_src/solvers/dlog_stride4.py +18 -0
  100. guldo/_src/solvers/egg_base.py +11 -0
  101. guldo/_src/solvers/egg_eightaxis.py +68 -0
  102. guldo/_src/solvers/egg_template.py +11 -0
  103. guldo/_src/solvers/factor_brent.py +34 -0
  104. guldo/_src/solvers/factor_floyd.py +19 -0
  105. guldo/_src/solvers/factor_trial_fast_py.py +21 -0
  106. guldo/_src/solvers/factor_trial_np.py +21 -0
  107. guldo/_src/solvers/factor_trial_py.py +12 -0
  108. guldo/_src/solvers/fixed_overhead_ref.py +21 -0
  109. guldo/_src/solvers/fork_bomb.py +18 -0
  110. guldo/_src/solvers/gpu_noop.py +6 -0
  111. guldo/_src/solvers/hostile_probe.py +36 -0
  112. guldo/_src/solvers/kill_meter.py +27 -0
  113. guldo/_src/solvers/memory_bomb.py +10 -0
  114. guldo/_src/solvers/noop.py +5 -0
  115. guldo/_src/solvers/orphan_cpu_hider.py +16 -0
  116. guldo/_src/solvers/planted_peek.py +23 -0
  117. guldo/_src/solvers/sleeping_daemon_hang.py +12 -0
  118. guldo/_src/solvers/small_n_trick.py +22 -0
  119. guldo/_src/solvers/starter_template.py +21 -0
  120. guldo/_src/solvers/stdout_flood.py +9 -0
  121. guldo/_src/solvers/tmp_fill.py +10 -0
  122. guldo/_src/solvers/tuned_bkz.py +25 -0
  123. guldo/_src/static_check.py +214 -0
  124. guldo/_src/submit_check.py +166 -0
  125. guldo/_src/svp.py +889 -0
  126. guldo/_src/tensorops.py +304 -0
  127. guldo/_src/tflops.py +453 -0
  128. guldo/_src/tflops_runner.py +134 -0
  129. guldo/cli.py +60 -0
  130. guldo_cli-0.1.0.dist-info/METADATA +30 -0
  131. guldo_cli-0.1.0.dist-info/RECORD +134 -0
  132. guldo_cli-0.1.0.dist-info/WHEEL +5 -0
  133. guldo_cli-0.1.0.dist-info/entry_points.txt +2 -0
  134. guldo_cli-0.1.0.dist-info/top_level.txt +1 -0
guldo/_src/svp.py ADDED
@@ -0,0 +1,889 @@
1
+ """svp: submit a solver to a round and verify the result, entirely from the command line.
2
+
3
+ svp.py rounds [--server URL] the rounds a server is running
4
+ svp.py round ID [--server URL] one round: deadline, family, commitments, submissions
5
+ svp.py submit FILE --round ID --handle NAME --accept-rules [--publish-source] [--token T]
6
+ check the file locally, upload it, and confirm your receipt in the public log
7
+ svp.py status --round ID [--id SID | --handle NAME] [--wait SECONDS]
8
+ svp.py certificate --round ID --id SID [--out FILE] [--full] download the public certificate (after the reveal); --full = YOUR full one, with your source
9
+ svp.py verify --round ID --id SID [--rerun K --execute] download it AND check it against the server's log and your own receipt
10
+ svp.py verify FILE.cert.json [--rerun K --execute] check a certificate file you already have (offline)
11
+ svp.py verify ... --source SOLVER.py a private-solver certificate + the solver its owner published: hash-check it, then re-run it
12
+ svp.py receipts what this machine has submitted
13
+ svp.py practice SOLVER --family F [--n N] [--yes] no server: run your solver on three seeds of your own and certify the run (PROVISIONAL)
14
+
15
+ The server is --server URL or $SVP_SERVER. Receipts and tokens live in $SVP_HOME (default ~/.svp), mode 0600.
16
+
17
+ PRIVATE SOLVERS. Submit without --publish-source and your solver stays private: the public certificate carries its hash but not its source (and no
18
+ failure messages, which quote code). Everyone can still verify the log, the fingerprints, the questions and the credit arithmetic; nobody but the
19
+ operator can inspect or re-run it, so its timings rest on the operator's sandbox. If you later publish the file, `verify --source FILE` upgrades the
20
+ certificate to fully re-checkable. A private solver is hidden from the public, not from the operator, who ran it.
21
+
22
+ What `verify` proves: the seeds' fingerprints were public before any submission, the questions are exactly the seeds' own, the rules are the ones
23
+ named, the credit follows from the recorded timings, the log the certificate carries is the server's own, and (when you have a receipt) the file
24
+ that was evaluated is the one you uploaded. What it does NOT prove: that the recorded timings are what a real run took. A certificate says whether
25
+ they came from the locked-box sandbox or from a bare local run (then it is only PROVISIONAL); `--rerun K --execute` re-measures K instances on
26
+ your machine, running the certificate's code unsandboxed (use a throwaway VM).
27
+
28
+ Exit status: 0 = done / verified, 1 = a check failed, 2 = could not do it (bad usage, network, not ready yet).
29
+ """
30
+ import argparse
31
+ import contextlib
32
+ import fcntl
33
+ import json
34
+ import os
35
+ import re
36
+ import secrets
37
+ import subprocess
38
+ import sys
39
+ import time
40
+ import urllib.error
41
+ import urllib.parse
42
+ import urllib.request
43
+
44
+ import certificate
45
+ import rounds
46
+ import submit_check
47
+
48
+ PROG = "svp.py" # the name shown in --help; the installed package's `guldo` command sets it to "guldo"
49
+
50
+ MAX_JSON = 16 * 2 ** 20 # bytes accepted from a server in one response (a log or a round description)
51
+ MAX_CERT = 64 * 2 ** 20 # a certificate carries every raw timing record
52
+ TIMEOUT = 30
53
+ LOOPBACK = ("127.0.0.1", "localhost", "::1", "[::1]")
54
+ RULES_URL_HINT = "RULES.md in this repository (the version a server shows is in its submission form)"
55
+
56
+
57
+ class SvpError(Exception):
58
+ """Something the user can act on: shown as one line, exit status 2."""
59
+
60
+
61
+ def clean(text, n=300):
62
+ """Server-supplied text goes to a terminal: keep printable ASCII only, so it cannot drive the terminal."""
63
+ if isinstance(text, bytes):
64
+ text = text.decode("utf-8", "replace")
65
+ return re.sub(r"[^\x20-\x7e]", "?", str(text))[:n]
66
+
67
+
68
+ # ------------------------------------------------------------------------------------------------------------- network
69
+ class _NoRedirect(urllib.request.HTTPRedirectHandler):
70
+ def redirect_request(self, *a, **k):
71
+ return None # a redirect would forward the Authorization header (your token) to wherever the server points: refused instead
72
+
73
+
74
+ _OPENER = urllib.request.build_opener(_NoRedirect)
75
+
76
+
77
+ def normalize_server(url):
78
+ if not url:
79
+ raise SvpError("no server: pass --server https://host or set SVP_SERVER")
80
+ u = urllib.parse.urlsplit(url if "://" in url else "https://" + url)
81
+ if u.scheme not in ("http", "https") or not u.hostname:
82
+ raise SvpError(f"{clean(url, 80)!r} is not an http(s) server address")
83
+ if u.username or u.password:
84
+ raise SvpError("put no credentials in the server address")
85
+ try:
86
+ u.port # a malformed port raises here, with an unhelpful message later if we let it through
87
+ except ValueError:
88
+ raise SvpError(f"{clean(url, 80)!r} has an invalid port")
89
+ return f"{u.scheme}://{u.netloc.lower()}"
90
+
91
+
92
+ def _loopback(server):
93
+ return (urllib.parse.urlsplit(server).hostname or "") in LOOPBACK
94
+
95
+
96
+ def check_transport(server, sends_secret, allow_insecure_http=False):
97
+ if sends_secret and server.startswith("http://") and not _loopback(server) and not allow_insecure_http:
98
+ raise SvpError(f"{server} is plain http: your token or source would cross the network unencrypted. Use https, or pass --allow-insecure-http if you "
99
+ "really mean it")
100
+
101
+
102
+ MAX_SECONDS = 120 # for a whole small response; a certificate may take longer (MAX_SECONDS_CERT)
103
+ MAX_SECONDS_CERT = 900
104
+
105
+
106
+ def _read_limited(resp, limit, seconds):
107
+ """Read at most limit+1 bytes within `seconds` IN TOTAL. The socket timeout only bounds each read, so a server that drips one byte every 29 s
108
+ would otherwise hold this client for hours."""
109
+ end, chunks, n = time.monotonic() + seconds, [], 0
110
+ while n <= limit:
111
+ if time.monotonic() > end:
112
+ raise SvpError(f"the server is too slow (more than {seconds} s for one response): giving up")
113
+ b = resp.read(min(65536, limit + 1 - n))
114
+ if not b:
115
+ break
116
+ chunks.append(b)
117
+ n += len(b)
118
+ return b"".join(chunks)
119
+
120
+
121
+ def request(server, path, method="GET", data=None, headers=None, limit=MAX_JSON):
122
+ req = urllib.request.Request(server + path, data=data, method=method, headers=dict(headers or {}))
123
+ seconds = MAX_SECONDS if limit <= MAX_JSON else MAX_SECONDS_CERT
124
+ try:
125
+ with _OPENER.open(req, timeout=TIMEOUT) as r:
126
+ status, body = r.status, _read_limited(r, limit, seconds)
127
+ except urllib.error.HTTPError as e:
128
+ status, body = e.code, _read_limited(e, limit, seconds)
129
+ if 300 <= status < 400:
130
+ raise SvpError(f"the server redirected {path} (HTTP {status}); refused, because a redirect could send your token elsewhere. Use the final address")
131
+ except (urllib.error.URLError, OSError) as e:
132
+ raise SvpError(f"cannot reach {server}: {clean(getattr(e, 'reason', e), 120)}")
133
+ if len(body) > limit:
134
+ raise SvpError(f"the response to {path} is larger than {limit // 2 ** 20} MiB: refused")
135
+ return status, body
136
+
137
+
138
+ def get_json(server, path, headers=None, limit=MAX_JSON):
139
+ status, body = request(server, path, headers=headers, limit=limit)
140
+ if status != 200:
141
+ raise SvpError(f"{path}: HTTP {status}: {clean(body, 160)}")
142
+ try:
143
+ return json.loads(body)
144
+ except ValueError:
145
+ raise SvpError(f"{path}: the server did not return JSON")
146
+
147
+
148
+ def multipart(fields, filename, content):
149
+ filename = re.sub(r"[^A-Za-z0-9._-]", "_", filename)[:80] or "solver.py" # a file NAME can hold quotes or CRLF: never put it in a header raw
150
+ boundary = "svp" + secrets.token_hex(12)
151
+ parts = []
152
+ for k, v in fields.items():
153
+ parts.append(f'--{boundary}\r\nContent-Disposition: form-data; name="{k}"\r\n\r\n'.encode() + v.encode() + b"\r\n")
154
+ parts.append(f'--{boundary}\r\nContent-Disposition: form-data; name="file"; filename="{filename}"\r\nContent-Type: text/x-python\r\n\r\n'.encode()
155
+ + content + b"\r\n")
156
+ parts.append(f"--{boundary}--\r\n".encode())
157
+ return b"".join(parts), f"multipart/form-data; boundary={boundary}"
158
+
159
+
160
+ # ------------------------------------------------------------------------------------------------------------- local store
161
+ class Store:
162
+ """Receipts and handle tokens on THIS machine: the receipt is what lets `verify` say the evaluated file is the one you uploaded."""
163
+
164
+ def __init__(self, home=None):
165
+ self.home = os.path.abspath(os.path.expanduser(home or os.environ.get("SVP_HOME") or "~/.svp"))
166
+ self.path = os.path.join(self.home, "receipts.json")
167
+
168
+ def load(self):
169
+ try:
170
+ with open(self.path, encoding="utf-8") as f:
171
+ d = json.load(f)
172
+ except FileNotFoundError:
173
+ return {"receipts": {}, "tokens": {}}
174
+ except (OSError, ValueError):
175
+ d = None
176
+ if isinstance(d, dict) and isinstance(d.get("receipts"), dict) and isinstance(d.get("tokens"), dict):
177
+ return d
178
+ # an unreadable store is NEVER treated as empty: the next save would overwrite it, destroying tokens that were shown only once
179
+ raise SvpError(f"{self.path} is damaged. Move it aside (it may still hold tokens you need) and run again; nothing was changed")
180
+
181
+ def save(self, d):
182
+ os.makedirs(self.home, mode=0o700, exist_ok=True)
183
+ tmp = self.path + f".{os.getpid()}.tmp"
184
+ fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
185
+ with os.fdopen(fd, "w", encoding="utf-8") as f:
186
+ json.dump(d, f, indent=1, sort_keys=True)
187
+ os.replace(tmp, self.path)
188
+
189
+ @contextlib.contextmanager
190
+ def locked(self):
191
+ """Two svp processes at once must not overwrite each other's receipts: a token is shown only once."""
192
+ os.makedirs(self.home, mode=0o700, exist_ok=True)
193
+ fd = os.open(os.path.join(self.home, ".lock"), os.O_CREAT | os.O_RDWR, 0o600)
194
+ try:
195
+ fcntl.flock(fd, fcntl.LOCK_EX)
196
+ yield
197
+ finally:
198
+ fcntl.flock(fd, fcntl.LOCK_UN)
199
+ os.close(fd)
200
+
201
+ @staticmethod
202
+ def rkey(server, rid, sid):
203
+ return f"{server}|{rid}|{sid}"
204
+
205
+ @staticmethod
206
+ def tkey(server, rid, handle):
207
+ return f"{server}|{rid}|{handle.lower()}"
208
+
209
+ def add(self, server, rid, receipt, token=None):
210
+ with self.locked():
211
+ d = self.load()
212
+ d["receipts"][self.rkey(server, rid, receipt["id"])] = receipt
213
+ if token:
214
+ d["tokens"][self.tkey(server, rid, receipt["handle"])] = token
215
+ self.save(d)
216
+
217
+ def token(self, server, rid, handle):
218
+ return self.load()["tokens"].get(self.tkey(server, rid, handle))
219
+
220
+ def receipt(self, server, rid, sid):
221
+ return self.load()["receipts"].get(self.rkey(server, rid, sid))
222
+
223
+ def receipts(self, server=None, rid=None):
224
+ out = []
225
+ for k, v in sorted(self.load()["receipts"].items()):
226
+ s, r, _ = k.split("|", 2)
227
+ if (server is None or s == server) and (rid is None or r == rid):
228
+ out.append((s, r, v))
229
+ return out
230
+
231
+
232
+ # ------------------------------------------------------------------------------------------------------------- the public log
233
+ def parse_log(body):
234
+ out = []
235
+ for line in body.decode("utf-8", "replace").splitlines():
236
+ if line.strip():
237
+ try:
238
+ out.append(json.loads(line))
239
+ except ValueError:
240
+ raise SvpError("the server's log is not valid JSON lines")
241
+ return out
242
+
243
+
244
+ def check_receipt_in_log(entries, commitments, receipt, local_sha):
245
+ """-> (ok, message): the log's hash chain verifies, and entry `seq` is a submission carrying exactly the hash of the file you uploaded."""
246
+ ok, why = certificate.commit_published(entries, commitments)
247
+ if not ok:
248
+ return False, f"the server's public log does not verify: {why}"
249
+ seq = receipt.get("seq")
250
+ if not isinstance(seq, int) or not 0 <= seq < len(entries):
251
+ return False, f"log entry #{clean(seq, 20)} does not exist"
252
+ e = entries[seq]
253
+ if e.get("kind") != "submission" or (e.get("data") or {}).get("sha256") != local_sha:
254
+ return False, f"log entry #{seq} is not a submission of the file you uploaded (hash {local_sha[:16]}...)"
255
+ if e.get("hash") != receipt.get("hash"):
256
+ return False, f"log entry #{seq} has a different hash from your receipt"
257
+ return True, f"entry #{seq} of {len(entries)} carries your file's hash ({local_sha[:16]}...) and the chain verifies"
258
+
259
+
260
+ def log_is_prefix(cert_log, live):
261
+ """The certificate's log must be the server's own log (or the start of it, if the round has moved on): same entries, same order."""
262
+ if len(cert_log) > len(live):
263
+ return False
264
+ return all(rounds.canonical(a) == rounds.canonical(b) for a, b in zip(cert_log, live))
265
+
266
+
267
+ # ------------------------------------------------------------------------------------------------------------- commands
268
+ def _server(a):
269
+ return normalize_server(getattr(a, "server", None) or os.environ.get("SVP_SERVER"))
270
+
271
+
272
+ def _fmt_time(ts):
273
+ return time.strftime("%Y-%m-%d %H:%M UTC", time.gmtime(ts)) if isinstance(ts, (int, float)) else "?"
274
+
275
+
276
+ def cmd_rounds(a):
277
+ server = _server(a)
278
+ data = get_json(server, "/api/rounds.json")
279
+ if not isinstance(data, list) or not all(isinstance(r, dict) for r in data):
280
+ raise SvpError("the server's list of rounds is malformed")
281
+ if a.json:
282
+ print(json.dumps(data, indent=1))
283
+ return 0
284
+ if not data:
285
+ print("no rounds")
286
+ for r in data:
287
+ subs = r.get("submissions")
288
+ count = len(subs) if isinstance(subs, list) else subs if isinstance(subs, int) else "?" # /api/rounds.json lists them; an export index counts them
289
+ print(f"{clean(r.get('id'), 41):24s} {clean(r.get('family'), 12):10s} {clean(r.get('state'), 12):11s} closes {_fmt_time(r.get('closes_at'))} "
290
+ f"{count} submission(s) {clean(r.get('title'), 60)}")
291
+ return 0
292
+
293
+
294
+ def _round(server, rid):
295
+ if not rounds.ROUND_ID_RE.match(rid or ""):
296
+ raise SvpError("a round id is 2-41 characters of a-z, 0-9 and '-'")
297
+ r = get_json(server, f"/api/round/{rid}.json")
298
+ subs = r.get("submissions") if isinstance(r, dict) else None
299
+ commits = r.get("commitments") if isinstance(r, dict) else None
300
+ if not (isinstance(subs, list) and all(isinstance(s, dict) for s in subs) and isinstance(commits, list)
301
+ and all(isinstance(c, str) for c in commits)):
302
+ raise SvpError(f"the server's description of round {rid} is malformed") # a hostile or buggy server must cost one line, not a traceback
303
+ return r
304
+
305
+
306
+ def cmd_round(a):
307
+ server = _server(a)
308
+ r = _round(server, a.id)
309
+ if a.json:
310
+ print(json.dumps(r, indent=1))
311
+ return 0
312
+ print(f"{clean(r.get('title'), 80)} [{clean(r.get('id'), 41)}]\n family {clean(r.get('family'), 12)}, state {clean(r.get('state'), 12)}, "
313
+ f"{clean(r.get('n', '?'), 10)} questions per size, {clean(r.get('budget', '?'), 10)} s CPU limit\n opens {_fmt_time(r.get('opens_at'))}, closes {_fmt_time(r.get('closes_at'))}")
314
+ print(f" log: {r.get('log_entries', '?')} entries, {'verifies' if r.get('log_ok') else 'DOES NOT VERIFY'}, head {clean(r.get('log_head'), 16)}")
315
+ print(" solvers: " + ("every submission must publish its source (after the reveal)" if r.get("allow_private") is False else
316
+ "may stay private (then the public certificate carries only the solver's hash and its timings rest on the operator's sandbox)"))
317
+ cnt = r.get("count") if r.get("cost_model") == "count" and isinstance(r.get("count"), dict) else None
318
+ if cnt: # a COUNTED round: the cost is an instruction count, so the "CPU limit" above is in counted seconds and the whole ladder is a public rule
319
+ print(f" cost model: STEP COUNTING (not seconds) on {clean(cnt.get('python'), 30)}: sizes {clean(cnt.get('dims'), 80)}, {clean(cnt.get('n'), 10)} per size, cap "
320
+ f"{clean(cnt.get('cap'), 10)} counted seconds (one counted second = {clean(cnt.get('steps_per_second'), 12)} instructions). Declared before any submission; anyone can re-execute "
321
+ "the counts (`svp.py verify ... --rerun K --execute`, on that Python). Solvers run unsandboxed, so test yours with `python3 counted.py check` first.")
322
+ print(" seed fingerprints (public from the first log entry):")
323
+ for c in r.get("commitments", []):
324
+ print(" " + clean(c, 64))
325
+ for s in r.get("submissions", []):
326
+ res = s.get("result")
327
+ shown = ("pending" if "result" not in s else f"{clean(res.get('credit'), 20)} bits, {clean(res.get('status'), 40)}" if isinstance(res, dict)
328
+ else "no result: " + clean(s.get("reason"), 60))
329
+ print(f" #{clean(s.get('seq'), 10)} {clean(s.get('handle'), 32):20s} {clean(s.get('id'), 12)} {clean(s.get('state'), 10):9s} {shown}")
330
+ return 0
331
+
332
+
333
+ def cmd_submit(a):
334
+ server = _server(a)
335
+ if not a.accept_rules:
336
+ raise SvpError(f"you must pass --accept-rules to confirm you have read the rules ({RULES_URL_HINT}); the server records that you did")
337
+ try:
338
+ with open(a.file, "rb") as f:
339
+ content = f.read()
340
+ except OSError as e:
341
+ raise SvpError(f"cannot read {a.file}: {e.strerror}")
342
+ if not content:
343
+ raise SvpError("the file is empty")
344
+ if len(content) > rounds.MAX_UPLOAD:
345
+ raise SvpError(f"the file is {len(content)} bytes; the limit is {rounds.MAX_UPLOAD}")
346
+ local_sha = rounds.sha256(content)
347
+ if not a.no_check:
348
+ try:
349
+ warnings, fatal = submit_check.lint(a.file)
350
+ except (SyntaxError, ValueError, RecursionError) as e:
351
+ warnings, fatal = [], [f"could not parse: {type(e).__name__}"]
352
+ if fatal:
353
+ raise SvpError("the file would be rejected: " + "; ".join(fatal) + " (fix it, or use --no-check to let the server decide)")
354
+ for w in warnings:
355
+ print(f" note: {w}")
356
+ r = _round(server, a.round)
357
+ if r.get("state") != "open":
358
+ raise SvpError(f"round {a.round} is {clean(r.get('state'), 12)}, not open: nothing was sent")
359
+ if r.get("allow_private") is False and not a.publish_source:
360
+ raise SvpError(f"round {a.round} requires every solver to be published (a public rule logged in its first entry): add --publish-source. Your source "
361
+ "becomes public only after the seeds are revealed. Nothing was sent")
362
+ if r.get("cost_model") == "count" and not a.no_check: # the same rule the server applies to a counted round, so a rejection still costs nothing
363
+ import counted
364
+ tam = counted.source_problems(content.decode("utf-8", "replace"))
365
+ if tam:
366
+ raise SvpError(f"round {a.round} counts instructions and runs solvers in a restricted subset of Python (pure computation: no sys, files, frames, getattr/type/eval, "
367
+ "dunder attributes); your source is outside it: " + "; ".join(tam[:5]) + ". Fix it, or use --no-check to let the server decide. Nothing was sent")
368
+ store = Store(a.home)
369
+ try:
370
+ with store.locked(): # can the receipt and token be KEPT? Find out before anything is sent: the token is shown only once
371
+ store.load()
372
+ except OSError as e:
373
+ raise SvpError(f"cannot keep your receipt and token in {store.home} ({e.strerror or e}): nothing was sent. Use --home or $SVP_HOME for a writable directory")
374
+ token = a.token or store.token(server, a.round, a.handle)
375
+ check_transport(server, True, a.allow_insecure_http) # the source itself is private until the reveal, so plain http to a remote host is refused regardless
376
+ fields = {"handle": a.handle, "agree": "on"}
377
+ if token:
378
+ fields["token"] = token
379
+ if a.publish_source:
380
+ fields["publish_source"] = "on"
381
+ body, ctype = multipart(fields, os.path.basename(a.file), content)
382
+ status, resp = request(server, f"/round/{a.round}/submit", method="POST", data=body, headers={"Content-Type": ctype, "Accept": "application/json"})
383
+ try:
384
+ out = json.loads(resp)
385
+ except ValueError:
386
+ raise SvpError(f"HTTP {status}: {clean(resp, 160)}")
387
+ if status != 200 or not out.get("ok"):
388
+ raise SvpError(f"the server did not accept it (HTTP {status}): {clean(out.get('error', resp), 200)}")
389
+ if out.get("sha256") != local_sha:
390
+ print(f"FAIL: the server says it stored a file with hash {clean(out.get('sha256'), 64)}, but the file you sent hashes to {local_sha}. "
391
+ "Do not trust this submission.", file=sys.stderr)
392
+ return 1
393
+ # what we are about to SAVE (and later put in a header or a store key) must have the exact shape a real receipt has
394
+ tok = out.get("token")
395
+ if not (isinstance(out.get("id"), str) and re.fullmatch(r"[0-9a-f]{12}", out["id"]) and isinstance(out.get("seq"), int) and not isinstance(out.get("seq"), bool)
396
+ and isinstance(out.get("hash"), str) and re.fullmatch(r"[0-9a-f]{64}", out["hash"]) and isinstance(out.get("handle"), str)
397
+ and (tok is None or (isinstance(tok, str) and re.fullmatch(r"[A-Za-z0-9_-]{8,128}", tok)))):
398
+ raise SvpError("the server's receipt is malformed: nothing was saved (your file may still have been accepted: check `svp.py status`)"
399
+ + (f". The reply also carried this token, which you may need if it was accepted: {clean(tok, 64)}" if isinstance(tok, str) and tok else ""))
400
+ receipt = {k: out.get(k) for k in ("id", "handle", "seq", "hash", "sha256", "round")}
401
+ receipt.update({"file": os.path.basename(a.file), "submitted_at_local": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "confirmed_in_log": False})
402
+ try:
403
+ store.add(server, a.round, receipt, out.get("token")) # saved BEFORE the log check: the token is shown once and must never be lost to a network error
404
+ saved = True
405
+ except (OSError, SvpError) as e:
406
+ # the upload already succeeded: whatever happens to the local file, the user must still SEE the receipt and the one-time token
407
+ saved = False
408
+ print(f"WARNING: could not save your receipt and token on this machine ({clean(e, 120)}). WRITE THEM DOWN NOW:\n"
409
+ f" round {a.round} submission {receipt['id']} handle {receipt['handle']} token {out.get('token') or '(none: the handle already existed)'}", file=sys.stderr)
410
+ print(f"accepted: submission {receipt['id']} as {receipt['handle']}, log entry #{receipt['seq']}")
411
+ for w in out.get("warnings") or []:
412
+ print(f" warning: {clean(w)}")
413
+ if not a.publish_source:
414
+ print(" Your solver stays PRIVATE: after the reveal the public certificate carries only its hash (no source, no failure messages). The operator still ran it,\n"
415
+ " and others must trust the operator's sandbox for your timings. If you publish the file later, anyone can check it with `svp.py verify ... --source FILE`.")
416
+ if out.get("token"):
417
+ print(f"TOKEN (shown once, {'saved in ' + store.path if saved else 'NOT SAVED: write it down'}): {out['token']}\n Keep it: it is the only way to submit again as "
418
+ f"{receipt['handle']} in this round, and to download your certificate if you did not publish your source.")
419
+ # the receipt is worthless unless the PUBLIC log shows it: check that now, with your own copy of the chain
420
+ status, logbody = request(server, f"/api/log/{a.round}.jsonl")
421
+ if status != 200:
422
+ print(f"could not fetch the public log to confirm the receipt (HTTP {status}); run `svp.py status` later", file=sys.stderr)
423
+ return 2
424
+ ok, msg = check_receipt_in_log(parse_log(logbody), r.get("commitments", []), receipt, local_sha)
425
+ print(("CONFIRMED in the public log: " if ok else "FAIL: ") + msg)
426
+ if ok:
427
+ receipt["confirmed_in_log"] = True
428
+ try:
429
+ store.add(server, a.round, receipt)
430
+ except (OSError, SvpError):
431
+ pass # the receipt was already saved (or the warning above was shown); failing to mark it confirmed must not turn a good submission into an error
432
+ print(f"Your file is read, never run, until the round closes ({_fmt_time(r.get('closes_at'))}). Then: svp.py status --round {a.round} --id {receipt['id']}")
433
+ return 0 if ok else 1
434
+
435
+
436
+ def _find(round_json, a, store, server):
437
+ subs = round_json.get("submissions", [])
438
+ if getattr(a, "id", None):
439
+ hit = [s for s in subs if s.get("id") == a.id]
440
+ elif getattr(a, "handle", None):
441
+ hit = [s for s in subs if str(s.get("handle", "")).lower() == a.handle.lower()]
442
+ else:
443
+ mine = {rc["id"] for _, _, rc in store.receipts(server, round_json.get("id"))}
444
+ hit = [s for s in subs if s.get("id") in mine]
445
+ if not hit:
446
+ raise SvpError("no matching submission in that round (give --id or --handle, or submit from this machine first)")
447
+ return hit
448
+
449
+
450
+ def cmd_status(a):
451
+ server, store = _server(a), Store(a.home)
452
+ deadline = time.time() + (a.wait or 0)
453
+ last = None
454
+ while True:
455
+ r = _round(server, a.round)
456
+ hit = _find(r, a, store, server)
457
+ done = r.get("state") in ("evaluated", "revealed")
458
+ if a.json:
459
+ shown = json.dumps({"round_state": r.get("state"), "submissions": hit}, indent=1)
460
+ else:
461
+ lines = [f"round {a.round}: {clean(r.get('state'), 12)}"]
462
+ for s in hit:
463
+ res = s.get("result")
464
+ line = f" {clean(s.get('handle'), 32)} {clean(s.get('id'), 12)} {clean(s.get('state'), 10)}"
465
+ if "result" in s:
466
+ line += (f" credit {clean(res.get('credit'), 20)} bits, {clean(res.get('status'), 40)}" if isinstance(res, dict)
467
+ else f" no result ({clean(s.get('reason'), 80)})")
468
+ lines.append(line)
469
+ if s.get("certificate"):
470
+ lines.append(f" certificate ready: svp.py verify --round {a.round} --id {s.get('id')}")
471
+ shown = "\n".join(lines)
472
+ if shown != last: # while waiting, print only when something changed
473
+ print(shown, flush=True)
474
+ last = shown
475
+ if done or time.time() >= deadline:
476
+ break
477
+ time.sleep(min(30, max(1, deadline - time.time())))
478
+ return 0 if done or not a.wait else 2
479
+
480
+
481
+ def _cert_path(rid, sid, full=False):
482
+ if not rounds.ROUND_ID_RE.match(rid or "") or not re.fullmatch(r"[0-9a-f]{12}", sid or ""):
483
+ raise SvpError("a submission id is 12 hex characters (svp.py status shows it) and a round id is 2-41 characters of a-z, 0-9 and '-'")
484
+ return f"/api/cert-full/{rid}/{sid}.json" if full else f"/api/cert/{rid}/{sid}.json"
485
+
486
+
487
+ def fetch_certificate(server, rid, sid, store, token=None, allow_insecure_http=False, full=False):
488
+ """The PUBLIC certificate (default): no token is ever sent, and a damaged receipts file cannot matter. It carries the solver's source only if its owner
489
+ published it; otherwise it is a private-solver copy. `full`: YOUR full certificate with the source, which needs the handle's token."""
490
+ headers = None
491
+ if full:
492
+ token = token or None
493
+ if token is None:
494
+ rc = store.receipt(server, rid, sid)
495
+ if rc:
496
+ token = store.token(server, rid, rc["handle"])
497
+ if not token:
498
+ raise SvpError("the full certificate contains the solver's source, which only its owner may fetch: it needs the handle's token "
499
+ "(use the machine you submitted from, or --token)")
500
+ check_transport(server, True, allow_insecure_http)
501
+ headers = {"Authorization": "Bearer " + token}
502
+ status, body = request(server, _cert_path(rid, sid, full), headers=headers, limit=MAX_CERT)
503
+ if status == 404:
504
+ raise SvpError("no certificate for that submission yet: certificates exist only after the seeds are revealed (svp.py status shows when)")
505
+ if status == 403:
506
+ if full:
507
+ raise SvpError("that token is not accepted for the full certificate of this submission (it must be the token of the handle that submitted it)")
508
+ try: # the PUBLIC certificate was refused: no token was involved, so say what the server said (e.g. it is not ready for public release)
509
+ why = json.loads(body).get("error", "") if isinstance(json.loads(body), dict) else ""
510
+ except ValueError:
511
+ why = ""
512
+ raise SvpError("the server refused to release this certificate" + (f": {clean(why, 200)}" if why else ""))
513
+ if status != 200:
514
+ raise SvpError(f"HTTP {status}: {clean(body, 160)}")
515
+ try:
516
+ cert = json.loads(body)
517
+ except ValueError:
518
+ raise SvpError("the server did not return a certificate")
519
+ if not isinstance(cert, dict) or cert.get("format") != certificate.FORMAT:
520
+ raise SvpError("what the server returned is not a speedup certificate")
521
+ return cert, body
522
+
523
+
524
+ def cmd_certificate(a):
525
+ server, store = _server(a), Store(a.home)
526
+ cert, body = fetch_certificate(server, a.round, a.id, store, a.token, getattr(a, "allow_insecure_http", False), getattr(a, "full", False))
527
+ out = a.out or f"{a.round}-{a.id}" + (".full" if getattr(a, "full", False) else "") + ".cert.json"
528
+ if os.path.exists(out):
529
+ raise SvpError(f"{out} exists; a certificate is never overwritten")
530
+ fd = os.open(out, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o644)
531
+ with os.fdopen(fd, "wb") as f:
532
+ f.write(body)
533
+ print(f"saved {out}. Check it with: svp.py verify {out}")
534
+ return 0
535
+
536
+
537
+ def _sandbox_of(cert):
538
+ try:
539
+ lad = next(iter(cert["ladders"].values()))
540
+ return json.loads(lad["submission"].splitlines()[0]).get("sandbox")
541
+ except (KeyError, ValueError, IndexError, StopIteration, AttributeError):
542
+ return None
543
+
544
+
545
+ def verify_certificate(cert, a, extra=None):
546
+ """Run every check and print them. `extra` = checks that only a server round-trip can make. -> (ok, [checks])"""
547
+ if a.execute:
548
+ print("NOTE: --execute runs code from the certificate on this machine, unsandboxed. Do this only in a throwaway VM or container.", file=sys.stderr)
549
+ res = certificate.verify(cert, rerun=a.rerun, execute=a.execute, tolerance=a.tolerance, sample_seed=a.sample_seed, source=getattr(a, "source_data", None))
550
+ checks = list(extra or []) + res["checks"]
551
+ ok = all(c["status"] != "fail" for c in checks)
552
+ return ok, checks, res
553
+
554
+
555
+ def cmd_verify(a):
556
+ store = Store(a.home)
557
+ extra, cert = [], None
558
+ a.source_data = None
559
+ if getattr(a, "source", None): # a solver its owner has published, to check against a private-solver certificate (BYTES: the committed hash is of the exact file)
560
+ try:
561
+ with open(a.source, "rb") as f:
562
+ a.source_data = f.read()
563
+ except OSError as e:
564
+ raise SvpError(f"cannot read {clean(a.source, 80)}: {e.strerror}")
565
+ if a.file:
566
+ try:
567
+ with open(a.file, encoding="utf-8") as f:
568
+ cert = json.load(f)
569
+ except (OSError, ValueError) as e:
570
+ raise SvpError(f"cannot read {clean(a.file, 80)} as a certificate: {type(e).__name__}")
571
+ server = rid = sid = None
572
+ else:
573
+ if not (a.round and a.id):
574
+ raise SvpError("give a certificate file, or --round and --id (and --server)")
575
+ server, rid, sid = _server(a), a.round, a.id
576
+ cert, _ = fetch_certificate(server, rid, sid, store, a.token, getattr(a, "allow_insecure_http", False), getattr(a, "full", False))
577
+ r = _round(server, rid)
578
+ status, logbody = request(server, f"/api/log/{rid}.jsonl")
579
+ live = parse_log(logbody) if status == 200 else None
580
+ add = lambda name, ok, detail: extra.append({"check": name, "status": "pass" if ok else "fail", "detail": detail})
581
+ add("server: round revealed", r.get("state") == "revealed", f"the round is {clean(r.get('state'), 12)}")
582
+ add("server: same seed fingerprints", sorted(cert.get("commitments", [])) == sorted(r.get("commitments", [])),
583
+ "the certificate's commitments are the round's own" if sorted(cert.get("commitments", [])) == sorted(r.get("commitments", []))
584
+ else "the certificate names different seed fingerprints from the round")
585
+ if live is None:
586
+ extra.append({"check": "server: log matches", "status": "skip", "detail": f"could not fetch the server's log (HTTP {status})"})
587
+ else:
588
+ same = log_is_prefix(cert.get("log") or [], live)
589
+ add("server: log matches", same, "the certificate's log is the server's own log" if same else
590
+ "the certificate's log DIFFERS from the log the server publishes (the operator may be showing different logs to different people)")
591
+ # your own receipt: was the evaluated file the one you uploaded?
592
+ cert_sha = (cert.get("submission") or {}).get("sha256") if isinstance(cert.get("submission"), dict) else None
593
+ log = cert.get("log") if isinstance(cert.get("log"), list) else []
594
+ in_log = lambda rc: any(isinstance(e, dict) and e.get("kind") == "submission" and e.get("hash") == rc.get("hash")
595
+ and (e.get("data") or {}).get("sha256") == rc.get("sha256") for e in log)
596
+ try:
597
+ cands = [rc for _, _, rc in store.receipts(server, rid) if rc.get("sha256") == cert_sha and (sid is None or rc.get("id") == sid)]
598
+ store_problem = None
599
+ except SvpError as e: # your receipts are an extra check, not a requirement: a damaged file must not stop the verification itself
600
+ cands, store_problem = [], str(e)
601
+ mine = next((rc for rc in cands if in_log(rc)), None)
602
+ if store_problem:
603
+ extra.append({"check": "your receipt", "status": "skip", "detail": "not checked: " + clean(store_problem, 200)})
604
+ elif mine:
605
+ extra.append({"check": "your receipt", "status": "pass",
606
+ "detail": f"the evaluated file is the one you uploaded ({mine['sha256'][:16]}...) and your logged entry #{mine.get('seq')} is in the certificate's log"})
607
+ elif cands and sid is not None:
608
+ # you asked for THIS submission and you have a receipt for exactly this file, yet the log entry you were given is not in the certificate
609
+ extra.append({"check": "your receipt", "status": "fail",
610
+ "detail": "your receipt's log entry is NOT in this certificate's log: this may not be the evaluation of your file"})
611
+ else:
612
+ # offline, or no receipt for this file: a file submitted to several rounds must not make an unrelated certificate FAIL
613
+ extra.append({"check": "your receipt", "status": "skip", "detail": "none of the receipts on this machine is for this certificate, so it is not "
614
+ "checked that the evaluated file is yours"})
615
+ ok, checks, res = verify_certificate(cert, a, extra)
616
+ report(ok, checks, res, cert, a.json)
617
+ return 0 if ok else 1
618
+
619
+
620
+ def report(ok, checks, res, cert, as_json):
621
+ sandbox = _sandbox_of(cert)
622
+ private, revealed, counted = bool(res.get("private")), bool(res.get("source_revealed")), bool(res.get("counted"))
623
+ if as_json:
624
+ print(json.dumps({"verified": ok, "claim": res.get("claim"), "measured_in": "step counting" if counted else (sandbox or "local mode"),
625
+ "cost_model": "count" if counted else "time", "private_solver": private, "source_checked": revealed, "checks": checks}, indent=1))
626
+ return
627
+ for c in checks:
628
+ print(f" {c['status'].upper():4s} {clean(c['check'], 60)}: {clean(c['detail'], 400)}")
629
+ if ok and res.get("claim"):
630
+ c = res["claim"]
631
+ print(f"\nVERIFIED{' (PRIVATE SOLVER)' if private and not revealed else ''}: {c['credit_bits']} bits, {c['status']}.")
632
+ if counted:
633
+ print(" measured by: deterministic STEP COUNTING (Python bytecode instructions), not by a clock.")
634
+ log0 = cert["log"][0] if isinstance(cert, dict) and isinstance(cert.get("log"), list) and cert["log"] and isinstance(cert["log"][0], dict) else {}
635
+ declared = isinstance(log0.get("data"), dict) and log0["data"].get("cost_model") == "count" # (a verified certificate: the log check ties this to the ladder)
636
+ print(" This proves the arithmetic and the fairness. The costs are exactly reproducible: `--rerun K --execute` on the same Python version must give IDENTICAL "
637
+ "step counts, and that equality is a proof of the recorded costs that trusts nobody's machine or sandbox. It is NOT a timing, it cannot see work done inside "
638
+ "a C function (one instruction however long it runs), and "
639
+ + ("the ladder (sizes, questions per size, cap) was declared by the round in its first log entry before any submission, the same for every participant."
640
+ if declared else "the ladder was chosen by whoever ran it, so it is not an official round result."))
641
+ else:
642
+ print(f" measured in: {'the ' + sandbox + ' sandbox' if sandbox else 'bare local mode (so at most PROVISIONAL)'}.")
643
+ print(" This proves the arithmetic and the fairness. It does not prove the recorded timings were honest: only the sandbox, or re-measuring"
644
+ " (`--rerun K --execute`), addresses that.")
645
+ logged = bool(cert.get("log")) if isinstance(cert, dict) else False
646
+ if private and not revealed:
647
+ print(" PRIVATE SOLVER: its source is not part of this certificate, so you could not inspect it or re-run it. The timings therefore rest on the operator's "
648
+ "sandbox alone. " + ("The solver's hash is the one the round's log committed to before the seeds were revealed: " if logged else
649
+ "The solver's hash is recorded in this certificate (it was built outside a round, so no log committed to it): ")
650
+ + "if its owner publishes the file, check it with `--source FILE`.")
651
+ elif private and revealed:
652
+ print(" The solver you supplied is exactly the one this certificate measured" + (" (same hash as the round's log committed to)." if logged else "."))
653
+ else:
654
+ print("\nNOT VERIFIED. Do not rely on this certificate.")
655
+
656
+
657
+ # ------------------------------------------------------------------------------------------------------------- practice: certify a local run, no server
658
+ _RUN = subprocess.run # tests replace these two
659
+ _RESULTS_DIR = None # where judge.py writes ladders (None = this project's results/)
660
+
661
+
662
+ def _c_extension_imports(path):
663
+ """Libraries whose work happens inside C and so counts as ONE instruction however long it runs (see counted.py): a counted run that leans on them says almost nothing."""
664
+ import counted # one list for the client and for round creation (counted.family_problem)
665
+ return counted.c_extension_imports(path)
666
+
667
+
668
+ def cmd_practice(a):
669
+ """Run YOUR solver against a family on three fresh seeds of your own, in bare local mode, and build a certificate you can verify. No server
670
+ and no sandbox, so the result is PROVISIONAL and the seeds were yours (nothing was committed publicly first): it shows what your solver earns
671
+ under this protocol, not an official result."""
672
+ import families
673
+ import official
674
+ if a.family not in families.FAMILIES:
675
+ raise SvpError(f"unknown family {clean(a.family, 40)!r}; one of {', '.join(sorted(families.FAMILIES))}")
676
+ solver = os.path.abspath(a.solver)
677
+ if not os.path.isfile(solver):
678
+ raise SvpError(f"cannot read {clean(a.solver, 80)}")
679
+ try:
680
+ warnings, fatal = submit_check.lint(solver)
681
+ except (SyntaxError, ValueError, RecursionError) as e:
682
+ warnings, fatal = [], [f"could not parse: {type(e).__name__}"]
683
+ if fatal:
684
+ raise SvpError("the file would be rejected: " + "; ".join(fatal))
685
+ if (a.n is not None and a.n < 1) or (a.budget is not None and a.budget < 1):
686
+ raise SvpError("--n and --budget must be positive")
687
+ counting = bool(getattr(a, "count", False))
688
+ out = a.out or f"{os.path.splitext(os.path.basename(solver))[0]}.{a.family}.practice.cert.json"
689
+ if os.path.exists(out):
690
+ raise SvpError(f"{out} exists; a certificate is never overwritten")
691
+ if counting:
692
+ import counted
693
+ import judge
694
+ if not counted.supported():
695
+ raise SvpError(f"--count needs Python {counted.MIN_PYTHON[0]}.{counted.MIN_PYTHON[1]} or newer (this is {counted.python_version()})")
696
+ dims = list(getattr(a, "dims", None) or [])
697
+ if not dims or min(dims) < 1:
698
+ raise SvpError("--count needs --dims: the sizes to count, small enough that counting (about 100x slower than running) finishes, e.g. --family factor --dims 38 40 42 --cap 40")
699
+ n = a.n or 8
700
+ cap = a.cap if getattr(a, "cap", None) is not None else 20.0
701
+ if n < 1 or not (0 < cap <= certificate.MAX_COUNTED_CAP): # `not (...)` also catches NaN; the verifier refuses any larger cap, so the run would be wasted
702
+ raise SvpError(f"--n must be positive and --cap a number between 0 and {certificate.MAX_COUNTED_CAP:g} counted seconds")
703
+ if a.budget is not None:
704
+ print("note: --budget is for clock measurement and is ignored with --count (use --cap).", file=sys.stderr)
705
+ baseline = judge.default_baseline(a.family)
706
+ bext = _c_extension_imports(baseline)
707
+ if bext: # a credit is a ratio against the baseline: if its work hides in C (1 instruction), any counted "speedup" over it is an artefact
708
+ raise SvpError(f"REFUSED: the default baseline for {a.family} imports {', '.join(bext)}, so its work happens inside C and counts as almost nothing: a counted "
709
+ "credit against it would be meaningless. Step counting needs a pure-Python baseline; measure this family by clock instead (drop --count).")
710
+ with open(solver, encoding="utf-8", errors="replace") as f:
711
+ tam = counted.source_problems(f.read())
712
+ if tam: # the counter runs solvers in a restricted subset of Python and enforces it itself: find out before spending any time
713
+ raise SvpError("REFUSED: the counter runs solvers in a restricted subset of Python (pure computation: no sys, files, frames, getattr/type/eval, dunder attributes) "
714
+ "and your solver is outside it: " + "; ".join(tam[:5]))
715
+ for label, path in (("your solver", solver),):
716
+ ext = _c_extension_imports(path)
717
+ if ext:
718
+ print(f"WARNING: {label} imports {', '.join(ext)}. Work done inside C libraries counts as ONE instruction however long it runs, so counted costs for "
719
+ "it say almost nothing: step counting is meaningful only for pure-Python work on both sides.", file=sys.stderr)
720
+ print(f"{a.family}: STEP COUNTING on sizes {dims}, {n} questions per size, cap {cap:g} counted seconds ({int(cap * counted.STEPS_PER_SECOND):,} instructions), "
721
+ f"3 seeds of your own.\n Counting is roughly 100x slower than running: worst case about {counted.worst_case_seconds(dims, n, cap) / 60:.0f} minutes "
722
+ "(every question spending its whole cap), usually far less. The baseline is counted too.")
723
+ else:
724
+ n = a.n or families.OFFICIAL[a.family]["n"]
725
+ budget = a.budget or families.OFFICIAL[a.family]["budget"]
726
+ plan = official.plan_cost(a.family, 3, n=n, budget=budget)
727
+ print(f"{a.family}: sizes {plan['dims']}, {n} questions per size, {budget:g} s CPU limit, 3 seeds of your own, bare local mode.\n"
728
+ f" Worst case {plan['worst_case_cpu_hours']:.1f} CPU-hours (every question failing at the limit); a solver that works costs far less. "
729
+ "The baseline is measured too.")
730
+ if not a.yes:
731
+ print("Nothing was run. Re-run with --yes to start (it can take a long time; --n and --budget shrink it, and the certificate records what you used)."
732
+ if not counting else "Nothing was run. Re-run with --yes to start (--n, --cap and --dims shrink it, and the certificate records what you used).")
733
+ return 2
734
+ if counting: # a program whose count changes between two identical runs cannot be certified this way: find out BEFORE spending the time
735
+ for label, path in (("the baseline", baseline), ("your solver", solver)):
736
+ same, x, y = counted.selfcheck(path, a.family, min(dims), cap)
737
+ problem = counted.selfcheck_problem(label, same, x, y)
738
+ if problem:
739
+ raise SvpError(problem)
740
+ import shutil
741
+ import tempfile
742
+ work = tempfile.mkdtemp(prefix="svp-practice-") # the seeds live outside the project tree
743
+ results = _RESULTS_DIR or os.path.join(os.path.dirname(os.path.abspath(__file__)), "results")
744
+ tags = set()
745
+ try:
746
+ seed_dir = os.path.join(work, "seeds")
747
+ tags = {c[:8] for c in official.make_commitments(seed_dir, 3)} # the ladders this run writes are named by these, and only these
748
+ env = dict(os.environ, SVP_BOARD=os.path.join(work, "board.json")) # never touch the project's own leaderboard
749
+ for i, (seed_file, _) in enumerate(official.verified_seeds(seed_dir), 1):
750
+ if counting:
751
+ print(f"seed {i}/3: counting (baseline, then your solver) ...", flush=True)
752
+ counted.evaluate_seed(a.family, seed_file, solver, baseline, dims, n, cap, results)
753
+ continue
754
+ print(f"seed {i}/3: evaluating (baseline, then your solver) ...", flush=True)
755
+ cmd = [sys.executable, "judge.py", "evaluate", "--seed", seed_file, "--submission", solver, "--family", a.family, "--insecure-local",
756
+ "--reuse-baseline", "--n", str(n), "--budget", str(budget)]
757
+ r = _RUN(cmd, cwd=os.path.dirname(os.path.abspath(__file__)), env=env, capture_output=True, text=True)
758
+ if r.returncode:
759
+ raise SvpError(f"the evaluation failed on seed {i}: {clean((r.stdout + r.stderr)[-300:], 300)}")
760
+ try:
761
+ cert = certificate.build(a.family, solver, seed_dir, reveal_seeds=True, results_dir=_RESULTS_DIR, private=getattr(a, "private", False))
762
+ except (certificate.CertificateError, SystemExit) as e:
763
+ raise SvpError(f"could not build the certificate: {clean(e, 200)}")
764
+ fd = os.open(out, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o644)
765
+ with os.fdopen(fd, "w", encoding="utf-8") as f:
766
+ json.dump(cert, f, indent=1, sort_keys=True)
767
+ finally:
768
+ # the certificate carries every raw timing record, so the ladder files this run created are redundant. Remove exactly the files named by THIS
769
+ # run's own seed tags: anything else in results/ (another process's ladders, even for the same family) is never touched
770
+ for fn in (os.listdir(results) if os.path.isdir(results) else []):
771
+ if any(fn.startswith(f"judged_{a.family}_{t}_") for t in tags):
772
+ try:
773
+ os.remove(os.path.join(results, fn))
774
+ except OSError:
775
+ pass
776
+ shutil.rmtree(work, ignore_errors=True)
777
+ print(f"wrote {out}\n")
778
+ args = argparse.Namespace(execute=False, rerun=0, tolerance=certificate.RERUN_TOLERANCE, sample_seed=0)
779
+ ok, checks, res = verify_certificate(cert, args)
780
+ report(ok, checks, res, cert, False)
781
+ if counting:
782
+ print(f"\nThis was YOUR counted run on YOUR seeds. Anyone on the same Python can re-execute it and must get IDENTICAL step counts: "
783
+ f"svp.py verify {out} --rerun 5 --execute (throwaway VM: it runs the solvers).")
784
+ else:
785
+ print(f"\nThis was YOUR run on YOUR seeds in bare local mode: PROVISIONAL by construction. Anyone can re-check it: svp.py verify {out}")
786
+ return 0 if ok else 1
787
+
788
+
789
+ def cmd_receipts(a):
790
+ store = Store(a.home)
791
+ rows = store.receipts()
792
+ if not rows:
793
+ print(f"no receipts in {store.path}")
794
+ for s, r, rc in rows:
795
+ print(f"{s} round {r} submission {rc['id']} as {rc['handle']} log #{rc['seq']} "
796
+ f"{'confirmed in the public log' if rc.get('confirmed_in_log') else 'NOT confirmed in the log'} file {clean(rc.get('file'), 60)} sha256 {rc['sha256'][:16]}...")
797
+ return 0
798
+
799
+
800
+ # ------------------------------------------------------------------------------------------------------------- entry point
801
+ def build_parser():
802
+ ap = argparse.ArgumentParser(prog=PROG, description=__doc__.split("\n")[0], formatter_class=argparse.RawDescriptionHelpFormatter,
803
+ epilog="\n".join(__doc__.split("\n")[2:]))
804
+ common = argparse.ArgumentParser(add_help=False)
805
+ common.add_argument("--server", help="https://host of the submission service (or $SVP_SERVER)")
806
+ common.add_argument("--home", help="where receipts and tokens are kept (default $SVP_HOME or ~/.svp)")
807
+ sub = ap.add_subparsers(dest="cmd", required=True)
808
+ p = sub.add_parser("rounds", parents=[common])
809
+ p.add_argument("--json", action="store_true")
810
+ p = sub.add_parser("round", parents=[common])
811
+ p.add_argument("id")
812
+ p.add_argument("--json", action="store_true")
813
+ p = sub.add_parser("submit", parents=[common])
814
+ p.add_argument("file")
815
+ p.add_argument("--round", required=True)
816
+ p.add_argument("--handle", required=True)
817
+ p.add_argument("--accept-rules", action="store_true")
818
+ p.add_argument("--publish-source", action="store_true", help="publish your source after the seeds are revealed (needed for anyone else to verify your certificate)")
819
+ p.add_argument("--token", help="the handle's token, if you submitted under it before (saved automatically on this machine)")
820
+ p.add_argument("--no-check", action="store_true", help="skip the local pre-flight lint")
821
+ p.add_argument("--allow-insecure-http", action="store_true")
822
+ p = sub.add_parser("status", parents=[common])
823
+ p.add_argument("--round", required=True)
824
+ p.add_argument("--id")
825
+ p.add_argument("--handle")
826
+ p.add_argument("--wait", type=int, default=0, metavar="SECONDS", help="poll until the round is evaluated, up to this long")
827
+ p.add_argument("--json", action="store_true")
828
+ p = sub.add_parser("certificate", parents=[common])
829
+ p.add_argument("--round", required=True)
830
+ p.add_argument("--id", required=True)
831
+ p.add_argument("--out")
832
+ p.add_argument("--token")
833
+ p.add_argument("--full", action="store_true", help="YOUR full certificate, with your solver's source (needs your handle's token; the default is the public certificate)")
834
+ p.add_argument("--allow-insecure-http", action="store_true", help="send the token over plain http to a remote host (never do this on a real network)")
835
+ p = sub.add_parser("verify", parents=[common])
836
+ p.add_argument("file", nargs="?", help="a certificate file (offline); or give --round and --id to download it")
837
+ p.add_argument("--round")
838
+ p.add_argument("--id")
839
+ p.add_argument("--token")
840
+ p.add_argument("--rerun", type=certificate.rerun_count, default=0, metavar="K|all", help="re-measure K instances here, or `all` of them (needs --execute)")
841
+ p.add_argument("--execute", action="store_true", help="run the certificate's code on this machine, unsandboxed: a throwaway VM only")
842
+ p.add_argument("--tolerance", type=float, default=certificate.RERUN_TOLERANCE)
843
+ p.add_argument("--sample-seed", type=int, default=None, help="which instances --rerun re-measures (default: a fresh random draw: yours alone)")
844
+ p.add_argument("--full", action="store_true", help="YOUR full certificate, with your solver's source (needs your handle's token)")
845
+ p.add_argument("--source", metavar="FILE", help="a solver its owner has PUBLISHED, to check against a private-solver certificate: it must hash to the value the round's log "
846
+ "committed to; with --rerun --execute it is then re-measured")
847
+ p.add_argument("--allow-insecure-http", action="store_true", help="send the token over plain http to a remote host (never do this on a real network)")
848
+ p.add_argument("--json", action="store_true")
849
+ sub.add_parser("receipts", parents=[common])
850
+ p = sub.add_parser("practice", help="run YOUR solver on three seeds of your own, locally, and certify it (no server; PROVISIONAL)")
851
+ p.add_argument("solver")
852
+ p.add_argument("--family", required=True)
853
+ p.add_argument("--n", type=int, help="questions per size (default: the family's official number; smaller = faster, recorded in the certificate)")
854
+ p.add_argument("--budget", type=int, help="CPU limit per question in seconds (default: the family's official limit)")
855
+ p.add_argument("--out")
856
+ p.add_argument("--private", action="store_true", help="write a PRIVATE-SOLVER certificate: no source and no failure text, so you can share it without revealing your solver")
857
+ p.add_argument("--count", action="store_true", help="measure by exact bytecode-instruction COUNTING instead of by clock: the costs are then exactly reproducible by anyone "
858
+ "on the same Python (needs 3.12+, pure-Python solvers, and --dims). See `python3 counted.py --help` for what that proves and what it cannot")
859
+ p.add_argument("--dims", type=int, nargs="+", help="with --count: the sizes to count (small: counting is ~100x slower than running), e.g. 16 18 20")
860
+ p.add_argument("--cap", type=float, help="with --count: the most counted seconds (x 1e6 instructions) one question may use (default 20). A size is only usable if "
861
+ "the solver's typical cost there reaches 0.5, so the baseline must cost a few counted seconds")
862
+ p.add_argument("--yes", action="store_true", help="start the run (it can take hours: read the plan first)")
863
+ return ap
864
+
865
+
866
+ def main(argv=None):
867
+ a = build_parser().parse_args(argv)
868
+ if a.cmd == "verify" and a.sample_seed is None:
869
+ a.sample_seed = secrets.randbelow(2 ** 31) # the operator cannot know which instances you will re-measure
870
+ try:
871
+ return {"rounds": cmd_rounds, "round": cmd_round, "submit": cmd_submit, "status": cmd_status, "certificate": cmd_certificate,
872
+ "verify": cmd_verify, "receipts": cmd_receipts, "practice": cmd_practice}[a.cmd](a)
873
+ except SvpError as e:
874
+ print(f"svp: {e}", file=sys.stderr)
875
+ return 2
876
+ except (KeyError, TypeError, ValueError, AttributeError, IndexError) as e:
877
+ # a server (or a file) that sends data in a shape we did not expect is a failed request, never a traceback
878
+ print(f"svp: unexpected data ({type(e).__name__}: {clean(e, 100)}): the server or the file is not what this client expects", file=sys.stderr)
879
+ return 2
880
+ except OSError as e: # a file or directory we cannot read or write: one line (network errors are turned into SvpError in request())
881
+ print(f"svp: file error: {clean(e.strerror or e, 120)}", file=sys.stderr)
882
+ return 2
883
+ except KeyboardInterrupt:
884
+ print("svp: interrupted", file=sys.stderr)
885
+ return 130
886
+
887
+
888
+ if __name__ == "__main__":
889
+ sys.exit(main())