interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / qualification / om2 / fixtures / oracle / vectors.py

80 lines · 3867 bytes Raw Blame History
1# EXTERNAL ORACLE RECIPE (outside the delivered closure; not first-party automation).
2# Reproduces seed/qualification/om2/fixtures/sha256-vectors.txt, fixtures/msg/*.bin and
3# fixtures/gen/*.bin. Primary digest oracle: /usr/bin/sha256sum (GNU coreutils). Each digest is
4# cross-checked against Python hashlib, and the three SHA-256 digests NIST publishes in FIPS 180-2
5# Appendix B are checked against both. Messages are extracted from NIST text (pdftotext output),
6# never typed. Usage, see ../PROVENANCE.txt:
7# python3 vectors.py SHA256.txt SHA512.txt fips180-2.txt FIXTURE_DIR
8import hashlib, os, re, subprocess, sys
9
10sha256_txt, sha512_txt, fips2_txt, out = sys.argv[1:5]
11
12def sha256sum(data):
13 r = subprocess.run(["/usr/bin/sha256sum"], input=data, capture_output=True, check=True)
14 d = r.stdout.decode().split()[0]
15 assert d == hashlib.sha256(data).hexdigest(), "sha256sum and hashlib disagree"
16 return d
17
18# Generators, byte for byte the same as gate.om: seq:N byte i = i mod 251; rnd:SEED:N splitmix64.
19def seq(n):
20 return bytes(i % 251 for i in range(n))
21M64 = (1 << 64) - 1
22def splitmix_bytes(seed, n):
23 s, outb = seed, bytearray()
24 while len(outb) < n:
25 s = (s + 0x9E3779B97F4A7C15) & M64
26 z = s
27 z = ((z ^ (z >> 30)) * 0xBF58476D1CE4E5B9) & M64
28 z = ((z ^ (z >> 27)) * 0x94D049BB133111EB) & M64
29 z ^= z >> 31
30 outb += z.to_bytes(8, "little")
31 return bytes(outb[:n])
32
33t256 = open(sha256_txt).read()
34t512 = open(sha512_txt).read()
35quoted = re.findall(r'"([a-z]+)"', t256)
36assert len(quoted) == 2 and quoted[0] == "abc"
37abc, m448 = quoted[0].encode(), quoted[1].encode()
38q512 = re.findall(r'"([a-z\n]+)"', t512)
39assert len(q512) == 2 and q512[0] == "abc", q512
40m896 = q512[1].replace("\n", "").encode()
41assert (len(abc), len(m448), len(m896)) == (3, 56, 112), "448-bit and 896-bit message lengths"
42
43lines = open(fips2_txt).read().split("\n")
44pub = [l.strip().rstrip(".").replace(" ", "") for l in lines[1507:1887]
45 if re.fullmatch(r"\s*([0-9a-f]{8} ){7}[0-9a-f]{8}\.", l)]
46assert len(pub) == 3, pub
47million = b"a" * 1000000
48for msg, want in zip((abc, m448, million), pub):
49 assert sha256sum(msg) == want, "published FIPS 180-2 Appendix B digest"
50
51os.makedirs(os.path.join(out, "msg"), exist_ok=True)
52os.makedirs(os.path.join(out, "gen"), exist_ok=True)
53for name, msg in (("abc", abc), ("m448", m448), ("m896", m896)):
54 open(os.path.join(out, "msg", name + ".bin"), "wb").write(msg)
55
56rows = []
57def row(name, spec, data):
58 rows.append("%s %s %d %s" % (name, spec, len(data), sha256sum(data)))
59
60P = "seed/qualification/om2/fixtures/msg/"
61row("fips-empty", "-", b"")
62row("fips-abc", "file:" + P + "abc.bin", abc)
63row("fips-448", "file:" + P + "m448.bin", m448)
64row("fips-896", "file:" + P + "m896.bin", m896)
65row("fips-million-a", "rep:97:1000000", million)
66for n in list(range(0, 131)) + [1000, 4095, 4096, 4097, 8191, 8192, 8193, 65536]:
67 row("seq-%d" % n, "seq:%d" % n, seq(n))
68for n in (55, 56, 63, 64, 65, 4096):
69 row("rep-a-%d" % n, "rep:97:%d" % n, b"a" * n)
70# Random cases: seeds 1..8, lengths chosen to straddle the 55/56 padding edge, block and
71# 4096-byte piece boundaries, and one large multi-piece message.
72for seed, n in ((1, 3), (2, 55), (3, 56), (4, 64), (5, 111), (6, 4097), (7, 12345), (8, 100003)):
73 row("rnd-%d-%d" % (seed, n), "rnd:%d:%d" % (seed, n), splitmix_bytes(seed, n))
74open(os.path.join(out, "sha256-vectors.txt"), "w").write("\n".join(rows) + "\n")
75
76# Generator self-test inputs for gate.om (expected stdout of an identity program).
77open(os.path.join(out, "gen", "seq-300.bin"), "wb").write(seq(300))
78open(os.path.join(out, "gen", "rnd-7-64.bin"), "wb").write(splitmix_bytes(7, 64))
79open(os.path.join(out, "gen", "rep-0-5.bin"), "wb").write(b"\0" * 5)
80print("PASS %d vectors; FIPS 180-2 B.1, B.2, B.3 published digests matched" % len(rows))