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 / s5s7 / oracle.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # OM-2 qualification, lane s5s7 (#748, ledger L836): independent expected values for the |
| 3 | # fixtures in sec/s5s7/. Run from the worktree root: |
| 4 | # /usr/bin/python3 seed/qualification/om2/oracle/s5s7/oracle.py |
| 5 | # Writes seed/qualification/om2/oracle/s5s7/exp/* and sha/expected.h. Nothing here reads a |
| 6 | # compiler output; every value is computed from the contract's stated semantics. |
| 7 | import hashlib |
| 8 | import os |
| 9 | import struct |
| 10 | |
| 11 | Q = "seed/qualification/om2/oracle/s5s7" |
| 12 | E = Q + "/exp" |
| 13 | M64 = (1 << 64) - 1 |
| 14 | M32 = (1 << 32) - 1 |
| 15 | |
| 16 | |
| 17 | def w(name, data): |
| 18 | with open(os.path.join(E, name), "wb") as f: |
| 19 | f.write(data) |
| 20 | |
| 21 | |
| 22 | def hexl(v): |
| 23 | return ("%016x\n" % (v & M64)).encode() |
| 24 | |
| 25 | |
| 26 | def decl(v): |
| 27 | return ("%d\n" % v).encode() |
| 28 | |
| 29 | |
| 30 | def rotr(x, n, wd): |
| 31 | m = (1 << wd) - 1 |
| 32 | n %= wd |
| 33 | return ((x >> n) | (x << (wd - n))) & m |
| 34 | |
| 35 | |
| 36 | def rotl(x, n, wd): |
| 37 | return rotr(x, (wd - n) % wd, wd) |
| 38 | |
| 39 | |
| 40 | def dup(x): |
| 41 | return ((x & M32) << 32) | (x & M32) |
| 42 | |
| 43 | |
| 44 | os.makedirs(E, exist_ok=True) |
| 45 | |
| 46 | # contract 140-141: rotr/rotl of a dup32 value is a 64-bit rotation, equal to the dup of the |
| 47 | # 32-bit rotation by n mod 32. Both sides are computed and must agree. |
| 48 | X, Y, Z = 0x89ABCDEF, 0x01234567, 0x0F0F00FF |
| 49 | d = dup(X) |
| 50 | out = hexl(d) + hexl(d + 1) |
| 51 | for n in (0, 1, 8, 31, 32, 33, 63): |
| 52 | a = rotr(d, n, 64) |
| 53 | assert a == dup(rotr(X, n % 32, 32)), n |
| 54 | out += hexl(a) |
| 55 | assert dup(X) ^ dup(Y) == dup(X ^ Y) |
| 56 | out += hexl(dup(X) ^ dup(Y)) |
| 57 | w("a5-exact.out", out) |
| 58 | |
| 59 | out = b"" |
| 60 | for n in (8, 40): |
| 61 | a = rotl(d, n, 64) |
| 62 | assert a == dup(rotl(X, n % 32, 32)), n |
| 63 | out += hexl(a) |
| 64 | e = dup(Z) |
| 65 | out += hexl(d & e) + hexl(d | e) + hexl(d ^ e) |
| 66 | a = rotr(d, 5, 64) |
| 67 | assert a == dup(rotr(X, 5, 32)) |
| 68 | out += hexl(a) |
| 69 | w("a5-allops.out", out) |
| 70 | |
| 71 | # kept-check mutant prediction (constructor body or shl ulong x 32 ulong add x 1, x = 5) |
| 72 | w("a5-kept.mutant", hexl((5 << 32) | 6)) |
| 73 | # commuted exact body: or ulong x shl ulong x 32 == dup(x) |
| 74 | w("a5-swap.out", hexl(dup(0xDEADBEEF))) |
| 75 | |
| 76 | # contract 163-165: shactx image; struct layout from sha256.h (uint32 state[8], uint64 count, |
| 77 | # uint8 buffer[64]) on LP64 AArch64 little endian. offsetof.c checks the same image with gcc. |
| 78 | img = b"".join(struct.pack("<I", 0x10203040 + i) for i in range(8)) |
| 79 | img += struct.pack("<Q", 0x0123456789ABCDEF) |
| 80 | img += bytes((3 * j + 1) & 255 for j in range(64)) |
| 81 | assert len(img) == 104 |
| 82 | w("l5-shactx.out", img) |
| 83 | |
| 84 | # endianness: layout be16 16 / fa uint 1 at 0 big / fb ulong 1 at 4 big / fc ushort 2 at 12 little |
| 85 | A, B, C0, C1 = 0x0A0B0C0D, 0x0102030405060708, 0xBEEF, 0x1234 |
| 86 | img = struct.pack(">I", A) + struct.pack(">Q", B) + struct.pack("<HH", C0, C1) |
| 87 | assert len(img) == 16 |
| 88 | w("l5-endian.out", img + hexl(A) + hexl(B) + hexl(C0) + hexl(C1)) |
| 89 | w("l5-scalar.out", struct.pack(">I", 0x01020304) + hexl(0x01020304)) |
| 90 | |
| 91 | # section 6 views |
| 92 | s = sum(i * 7 for i in range(10)) |
| 93 | w("v6-sum.out", decl(10) + decl(s) + decl(3) + decl(3 * 5)) |
| 94 | row = bytearray(b"." * 10) |
| 95 | for i in range(3, 7): |
| 96 | row[i] = ord("X") |
| 97 | w("v6-part-empty.out", decl(0)) |
| 98 | w("v6-edit.out", decl(2) + bytes(row) + b"\n" + decl(4)) |
| 99 | # wrapped-sum mutant: (2^64-2 + 4) mod 2^64 = 2 <= 10 accepts, callee prints len 4 |
| 100 | assert (M64 - 1 + 4) & M64 == 2 |
| 101 | w("v6-part-wrap.mutant", decl(4)) |
| 102 | # second-check-dropped mutant on o = 8, k = 4: o <= 10 holds, callee prints 4 |
| 103 | w("v6-part-tail.mutant", decl(4)) |
| 104 | # first-check-dropped mutant on o = 11, k = 0: len - o wraps to 2^64-1 >= 0, callee prints 0 |
| 105 | assert (10 - 11) & M64 == M64 |
| 106 | w("v6-part-o.mutant", decl(0)) |
| 107 | |
| 108 | # section 5 fresh |
| 109 | w("f5-line.out", decl(42)) |
| 110 | w("f5-unroll-all.out", decl(6) + decl(sum(range(1, 9)))) |
| 111 | w("f5-n64.out", decl(64) + decl(sum(range(1, 65)))) |
| 112 | w("f5-ifelse.out", decl(11) + decl(22)) |
| 113 | w("f5-inline.out", decl(5 + 2) + decl(sum(i + 2 for i in range(8)))) |
| 114 | w("f5-join-diff.mutant", decl(99)) |
| 115 | |
| 116 | # OM-1 names control (contract 15-16): at seal open abstract holds layout field little big part |
| 117 | # export out rotr hold 1..13; fresh is a parameter of a function named dup32mk |
| 118 | w("t-names.out", decl(sum(range(1, 14))) + hexl(dup(7))) |
| 119 | |
| 120 | # section 7 SHA consumer expectations (C driver, NOT_RUN until the generated header exists) |
| 121 | def seq(n): |
| 122 | return bytes(i % 251 for i in range(n)) |
| 123 | |
| 124 | |
| 125 | def hx(b): |
| 126 | return hashlib.sha256(b).hexdigest() |
| 127 | |
| 128 | |
| 129 | lines = ["/* generated by oracle.py (python3 hashlib); do not edit */"] |
| 130 | lines.append('#define EXP_EMPTY "%s"' % hx(b"")) |
| 131 | lines.append('#define EXP_ABC "%s"' % hx(b"abc")) |
| 132 | lines.append('#define EXP_SEQ1000 "%s"' % hx(seq(1000))) |
| 133 | lines.append('#define EXP_SEQ64 "%s"' % hx(seq(64))) |
| 134 | ns = (0, 1, 55, 56, 63, 64, 65, 119, 120, 1000) |
| 135 | lines.append("static const unsigned long exp_hash_n[%d] = {%s};" % (len(ns), ", ".join(str(n) for n in ns))) |
| 136 | lines.append("static const char *const exp_hash_d[%d] = {" % len(ns)) |
| 137 | for n in ns: |
| 138 | lines.append(' "%s",' % hx(seq(n))) |
| 139 | lines.append("};") |
| 140 | BIG = (1 << 32) + 3 |
| 141 | h = hashlib.sha256() |
| 142 | chunk = bytes(1 << 24) |
| 143 | left = BIG |
| 144 | while left: |
| 145 | k = min(left, len(chunk)) |
| 146 | h.update(chunk[:k]) |
| 147 | left -= k |
| 148 | lines.append("#define EXP_BIG_LEN %dULL" % BIG) |
| 149 | lines.append('#define EXP_BIG_ZERO "%s"' % h.hexdigest()) |
| 150 | with open(Q + "/sha/expected.h", "w") as f: |
| 151 | f.write("\n".join(lines) + "\n") |
| 152 | print("oracle ok") |