omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / tls / test / x25519 / gen_wycheproof.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | """Convert Wycheproof x25519_test.json to om/tls/test/x25519/vectors/wycheproof.txt (test tooling only). |
| 3 | |
| 4 | Usage: python3 gen_wycheproof.py WYCHEPROOF_CHECKOUT OUT_TXT |
| 5 | Every test becomes one driver line "X R KHEX UHEX OHEX": R = 1 for tests flagged ZeroSharedSecret |
| 6 | (their shared secret is all zero and x25519_scalarmult must report it), else 0. Wycheproof results |
| 7 | 'valid' and 'acceptable' are both compared exactly: acceptable tests here (twist points, |
| 8 | non-canonical u, low-order u) have the RFC 7748 output, which x25519_scalarmult computes. There are |
| 9 | no 'invalid' tests in this file; the converter stops if one appears or a field is not 32 bytes. |
| 10 | """ |
| 11 | import hashlib |
| 12 | import json |
| 13 | import os |
| 14 | import subprocess |
| 15 | import sys |
| 16 | |
| 17 | src, out = sys.argv[1], sys.argv[2] |
| 18 | path = os.path.join(src, "testvectors_v1", "x25519_test.json") |
| 19 | raw = open(path, "rb").read() |
| 20 | commit = subprocess.run(["git", "-C", src, "rev-parse", "HEAD"], capture_output=True, text=True, |
| 21 | check=True).stdout.strip() |
| 22 | doc = json.loads(raw) |
| 23 | lines = [] |
| 24 | counts = {} |
| 25 | for group in doc["testGroups"]: |
| 26 | if group["curve"] != "curve25519" or group["type"] != "XdhComp": |
| 27 | sys.exit("unexpected group %s %s" % (group["curve"], group["type"])) |
| 28 | for t in group["tests"]: |
| 29 | for f in ("public", "private", "shared"): |
| 30 | if len(t[f]) != 64: |
| 31 | sys.exit("tcId %d: %s is not 32 bytes" % (t["tcId"], f)) |
| 32 | if t["result"] not in ("valid", "acceptable"): |
| 33 | sys.exit("tcId %d: result %s not handled" % (t["tcId"], t["result"])) |
| 34 | zero = "ZeroSharedSecret" in t["flags"] |
| 35 | if zero != (t["shared"] == "00" * 32): |
| 36 | sys.exit("tcId %d: ZeroSharedSecret flag disagrees with the shared secret" % t["tcId"]) |
| 37 | counts[t["result"]] = counts.get(t["result"], 0) + 1 |
| 38 | lines.append("# tcId %d %s %s" % (t["tcId"], t["result"], ",".join(t["flags"]))) |
| 39 | lines.append("X %d %s %s %s" % (1 if zero else 0, t["private"], t["public"], t["shared"])) |
| 40 | if doc["numberOfTests"] != sum(counts.values()): |
| 41 | sys.exit("numberOfTests %d but %d tests read" % (doc["numberOfTests"], sum(counts.values()))) |
| 42 | head = [ |
| 43 | "# Wycheproof x25519_test.json, https://github.com/C2SP/wycheproof/blob/", |
| 44 | "# %s/testvectors_v1/x25519_test.json" % commit, |
| 45 | "# sha256 %s, %d tests: %s." % (hashlib.sha256(raw).hexdigest(), doc["numberOfTests"], |
| 46 | ", ".join("%s %d" % kv for kv in sorted(counts.items()))), |
| 47 | "# Expected rc 1 for the ZeroSharedSecret tests, else 0; every shared secret compared exactly.", |
| 48 | "# Copyright 2016-2026 The Wycheproof Authors, licensed under the Apache License 2.0", |
| 49 | "# (wycheproof.LICENSE, upstream LICENSE verbatim). Modified: converted from JSON to this line", |
| 50 | "# format by gen_wycheproof.py.", |
| 51 | ] |
| 52 | with open(out, "w") as f: |
| 53 | f.write("\n".join(head + lines) + "\n") |