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 / seed / qualification / om2 / oracle / sha / vecoracle.py
| 1 | #!/usr/bin/python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # ORACLE (outside the delivered closure), OM-2 qualification L836 (#748), phaseB/sha-f5d14cb2. |
| 4 | # Re-derives every row of fixtures/sha256-vectors.txt today: regenerates the message from its spec |
| 5 | # (generators of fixtures/PROVENANCE.txt), hashes it with Python hashlib and with /usr/bin/sha256sum |
| 6 | # (fed on stdin), and requires both to equal the fixture's digest column and the length column. |
| 7 | # Usage (repository root): vecoracle.py seed/qualification/om2/fixtures/sha256-vectors.txt |
| 8 | import hashlib |
| 9 | import subprocess |
| 10 | import sys |
| 11 | |
| 12 | M = (1 << 64) - 1 |
| 13 | |
| 14 | |
| 15 | def rnd(seed, n): |
| 16 | s, out = seed & M, bytearray() |
| 17 | while len(out) < n: |
| 18 | s = (s + 0x9E3779B97F4A7C15) & M |
| 19 | z = ((s ^ (s >> 30)) * 0xBF58476D1CE4E5B9) & M |
| 20 | z = ((z ^ (z >> 27)) * 0x94D049BB133111EB) & M |
| 21 | out += (z ^ (z >> 31)).to_bytes(8, "little") |
| 22 | return bytes(out[:n]) |
| 23 | |
| 24 | |
| 25 | def gen(spec): |
| 26 | if spec == "-": |
| 27 | return b"" |
| 28 | kind, rest = spec.split(":", 1) |
| 29 | if kind == "file": |
| 30 | return open(rest, "rb").read() |
| 31 | if kind == "seq": |
| 32 | return bytes(i % 251 for i in range(int(rest))) |
| 33 | if kind == "rep": |
| 34 | b, n = rest.split(":") |
| 35 | return bytes([int(b)]) * int(n) |
| 36 | if kind == "rnd": |
| 37 | s, n = rest.split(":") |
| 38 | return rnd(int(s), int(n)) |
| 39 | raise SystemExit(f"bad spec {spec}") |
| 40 | |
| 41 | |
| 42 | ok = bad = 0 |
| 43 | for line in open(sys.argv[1]): |
| 44 | name, spec, n, dig = line.split() |
| 45 | m = gen(spec) |
| 46 | h = hashlib.sha256(m).hexdigest() |
| 47 | s = subprocess.run(["/usr/bin/sha256sum", "-b"], input=m, capture_output=True, check=True).stdout.split()[0].decode() |
| 48 | if len(m) == int(n) and h == dig and s == dig: |
| 49 | ok += 1 |
| 50 | else: |
| 51 | bad += 1 |
| 52 | print(f"FAIL {name} len={len(m)}/{n} hashlib={h} sha256sum={s} fixture={dig}") |
| 53 | print(f"{'PASS' if bad == 0 else 'FAIL'} vecoracle rows={ok + bad} agree={ok} disagree={bad}") |
| 54 | sys.exit(1 if bad else 0) |