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 / oracle / s5s7 / oracle.py

152 lines · 5120 bytes Raw Blame History
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.
7import hashlib
8import os
9import struct
10
11Q = "seed/qualification/om2/oracle/s5s7"
12E = Q + "/exp"
13M64 = (1 << 64) - 1
14M32 = (1 << 32) - 1
15
16
17def w(name, data):
18 with open(os.path.join(E, name), "wb") as f:
19 f.write(data)
20
21
22def hexl(v):
23 return ("%016x\n" % (v & M64)).encode()
24
25
26def decl(v):
27 return ("%d\n" % v).encode()
28
29
30def rotr(x, n, wd):
31 m = (1 << wd) - 1
32 n %= wd
33 return ((x >> n) | (x << (wd - n))) & m
34
35
36def rotl(x, n, wd):
37 return rotr(x, (wd - n) % wd, wd)
38
39
40def dup(x):
41 return ((x & M32) << 32) | (x & M32)
42
43
44os.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.
48X, Y, Z = 0x89ABCDEF, 0x01234567, 0x0F0F00FF
49d = dup(X)
50out = hexl(d) + hexl(d + 1)
51for 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)
55assert dup(X) ^ dup(Y) == dup(X ^ Y)
56out += hexl(dup(X) ^ dup(Y))
57w("a5-exact.out", out)
58
59out = b""
60for n in (8, 40):
61 a = rotl(d, n, 64)
62 assert a == dup(rotl(X, n % 32, 32)), n
63 out += hexl(a)
64e = dup(Z)
65out += hexl(d & e) + hexl(d | e) + hexl(d ^ e)
66a = rotr(d, 5, 64)
67assert a == dup(rotr(X, 5, 32))
68out += hexl(a)
69w("a5-allops.out", out)
70
71# kept-check mutant prediction (constructor body or shl ulong x 32 ulong add x 1, x = 5)
72w("a5-kept.mutant", hexl((5 << 32) | 6))
73# commuted exact body: or ulong x shl ulong x 32 == dup(x)
74w("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.
78img = b"".join(struct.pack("<I", 0x10203040 + i) for i in range(8))
79img += struct.pack("<Q", 0x0123456789ABCDEF)
80img += bytes((3 * j + 1) & 255 for j in range(64))
81assert len(img) == 104
82w("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
85A, B, C0, C1 = 0x0A0B0C0D, 0x0102030405060708, 0xBEEF, 0x1234
86img = struct.pack(">I", A) + struct.pack(">Q", B) + struct.pack("<HH", C0, C1)
87assert len(img) == 16
88w("l5-endian.out", img + hexl(A) + hexl(B) + hexl(C0) + hexl(C1))
89w("l5-scalar.out", struct.pack(">I", 0x01020304) + hexl(0x01020304))
90
91# section 6 views
92s = sum(i * 7 for i in range(10))
93w("v6-sum.out", decl(10) + decl(s) + decl(3) + decl(3 * 5))
94row = bytearray(b"." * 10)
95for i in range(3, 7):
96 row[i] = ord("X")
97w("v6-part-empty.out", decl(0))
98w("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
100assert (M64 - 1 + 4) & M64 == 2
101w("v6-part-wrap.mutant", decl(4))
102# second-check-dropped mutant on o = 8, k = 4: o <= 10 holds, callee prints 4
103w("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
105assert (10 - 11) & M64 == M64
106w("v6-part-o.mutant", decl(0))
107
108# section 5 fresh
109w("f5-line.out", decl(42))
110w("f5-unroll-all.out", decl(6) + decl(sum(range(1, 9))))
111w("f5-n64.out", decl(64) + decl(sum(range(1, 65))))
112w("f5-ifelse.out", decl(11) + decl(22))
113w("f5-inline.out", decl(5 + 2) + decl(sum(i + 2 for i in range(8))))
114w("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
118w("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)
121def seq(n):
122 return bytes(i % 251 for i in range(n))
123
124
125def hx(b):
126 return hashlib.sha256(b).hexdigest()
127
128
129lines = ["/* generated by oracle.py (python3 hashlib); do not edit */"]
130lines.append('#define EXP_EMPTY "%s"' % hx(b""))
131lines.append('#define EXP_ABC "%s"' % hx(b"abc"))
132lines.append('#define EXP_SEQ1000 "%s"' % hx(seq(1000)))
133lines.append('#define EXP_SEQ64 "%s"' % hx(seq(64)))
134ns = (0, 1, 55, 56, 63, 64, 65, 119, 120, 1000)
135lines.append("static const unsigned long exp_hash_n[%d] = {%s};" % (len(ns), ", ".join(str(n) for n in ns)))
136lines.append("static const char *const exp_hash_d[%d] = {" % len(ns))
137for n in ns:
138 lines.append(' "%s",' % hx(seq(n)))
139lines.append("};")
140BIG = (1 << 32) + 3
141h = hashlib.sha256()
142chunk = bytes(1 << 24)
143left = BIG
144while left:
145 k = min(left, len(chunk))
146 h.update(chunk[:k])
147 left -= k
148lines.append("#define EXP_BIG_LEN %dULL" % BIG)
149lines.append('#define EXP_BIG_ZERO "%s"' % h.hexdigest())
150with open(Q + "/sha/expected.h", "w") as f:
151 f.write("\n".join(lines) + "\n")
152print("oracle ok")