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 / om / meaning / test / run.sh

165 lines · 6543 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# Test suite for om/meaning/meaning.om (FUNCTION_ID and the graph of meaning). Usage (from the
4# omega-mv root):
5# sh om/meaning/test/run.sh seed/out/omc
6# Builds into build/meaning/. Exits 0 only when every vector passes, the FUNCTION_ID goldens match an
7# independent sha256sum of the spec section 9.3 preimage, and every mutant and negative control is
8# detected. Final line: MEANING PASS checks=N mutants=M/M, or MEANING FAIL.
9set -eu
10omc=${1:?usage: run.sh OMC}
11here=$(cd "$(dirname "$0")" && pwd)
12lib=$(cd "$here/.." && pwd)
13root=$(cd "$lib/../.." && pwd)
14out="$root/build/meaning"
15vec="$here/vectors"
16sha2="$root/om/tls/sha2.om"
17mkdir -p "$out"
18nice="nice -n 19 ionice -c3"
19checks=0
20mut=0
21mutok=0
22bad=0
23
24say() { printf '%s\n' "$*"; }
25fail() { say "FAIL $*"; bad=1; }
26
27# build LIB BIN: om2 line, sha2.om, LIB, the driver; compile with omc
28build() {
29 { printf 'om2\n'; cat "$sha2" "$1" "$here/driver.om"; } > "$2.om"
30 if ! $nice "$omc" < "$2.om" > "$2" 2> "$2.err" || [ -s "$2.err" ]; then
31 say "omc refused $2.om: $(cat "$2.err")"
32 return 1
33 fi
34 chmod +x "$2"
35}
36
37# summary LINE -> "N F" when LINE is a complete driver summary
38summary() {
39 printf '%s\n' "$1" | awk '$1 == "meaning" && $2 == "driver:" && $4 == "checks," && $6 == "failed" { print $3, $5 }'
40}
41
42# detected NAME FILE BIN: BIN reported a difference (exit 1 with F > 0 failures) or refused the
43# input with a trap (exit 100..199); a signal (128+N within that range is not produced by omc
44# binaries, which trap through exit) or exit 0 is not detection
45detected() {
46 set +e
47 $nice "$3" < "$2" > "$out/$1.out" 2>&1
48 rc=$?
49 set -e
50 last=$(tail -n 1 "$out/$1.out")
51 s=$(summary "$last")
52 f=${s#* }
53 if [ "$rc" -eq 1 ] && [ -n "$s" ] && [ "$f" -gt 0 ]; then
54 say "PASS $1: detected ($(grep -c '^FAIL' "$out/$1.out") FAIL lines, first: $(grep -m 1 '^FAIL' "$out/$1.out"))"
55 return 0
56 fi
57 if [ "$rc" -ge 100 ] && [ "$rc" -le 127 ]; then
58 say "PASS $1: detected (trap, exit $rc)"
59 return 0
60 fi
61 fail "$1: not detected: exit $rc: $last"
62 return 1
63}
64
65# mutant NAME SED_EXPR: apply SED_EXPR to meaning.om, rebuild, require detection on the vectors
66mutant() {
67 mut=$((mut + 1))
68 sed "$2" "$lib/meaning.om" > "$out/mut-$1.om.lib"
69 if cmp -s "$lib/meaning.om" "$out/mut-$1.om.lib"; then
70 fail "$1: sed changed nothing"
71 return
72 fi
73 build "$out/mut-$1.om.lib" "$out/mut-$1" || { fail "$1: mutant did not compile"; return; }
74 if detected "mutant-$1" "$vec" "$out/mut-$1"; then
75 mutok=$((mutok + 1))
76 fi
77}
78
79# fid W K C... -> SHA-256 of "omega.function.v1" 0x00 || 01 || W u16 || K u16 || C u64 BE each
80fid() {
81 w=$1
82 k=$2
83 shift 2
84 {
85 printf 'omega.function.v1\000\001'
86 printf '%04x%04x' "$w" "$k" | xxd -r -p
87 for c in "$@"; do printf '%016x' "$c" | xxd -r -p; done
88 } | sha256sum | cut -d' ' -f1
89}
90zeros() { i=0; while [ "$i" -lt "$1" ]; do printf ' 0'; i=$((i + 1)); done; }
91
92build "$lib/meaning.om" "$out/driver" || { say "MEANING FAIL build"; exit 1; }
93say "omc sha256 $(sha256sum "$omc" | cut -d' ' -f1)"
94say "sha2.om sha256 $(sha256sum "$sha2" | cut -d' ' -f1)"
95say "meaning.om sha256 $(sha256sum "$lib/meaning.om" | cut -d' ' -f1)"
96say "driver.om sha256 $(sha256sum "$here/driver.om" | cut -d' ' -f1)"
97say "vectors sha256 $(sha256sum "$vec" | cut -d' ' -f1)"
98
99# FUNCTION_ID goldens in the vectors must equal the independently built preimage hashes
100# (x + 2 at w 64: a = 2 1 0...; zero at w 64: 66 zeros; 3x^3 + 5x + 7 at w 8: a = 7 8 9 3 0 0 0 0 0 0,
101# the coefficient vector computed by Wolfram, spec section 9.2)
102g1=$(fid 64 66 2 1 $(zeros 64))
103g2=$(fid 64 66 $(zeros 66))
104g3=$(fid 8 10 7 8 9 3 0 0 0 0 0 0)
105for want in "H 0 $g1" "H 2 $g1" "H 8 $g2" "H 9 $g2" "H 19 $g3"; do
106 if grep -qx "$want" "$vec"; then
107 say "PASS golden: $want"
108 else
109 fail "golden: vectors lack \"$want\""
110 fi
111done
112
113# the vectors
114set +e
115$nice "$out/driver" < "$vec" > "$out/vectors.out" 2>&1
116rc=$?
117set -e
118last=$(tail -n 1 "$out/vectors.out")
119s=$(summary "$last")
120if [ "$rc" -eq 0 ] && [ -n "$s" ] && [ "${s#* }" = 0 ] && [ "${s% *}" -gt 0 ]; then
121 checks=${s% *}
122 grep '^[RG] ' "$out/vectors.out" | while IFS= read -r l; do say " $l"; done
123 say "PASS vectors: $last"
124else
125 fail "vectors: exit $rc: $(tail -n 5 "$out/vectors.out" | tr '\n' ' ')"
126fi
127
128# source mutants: each must be detected by the vectors
129# K one too small: x(x-1)...(x-8) and 0 at w 8 collapse, the iff check sees equal ids for different functions
130mutant kof-minus-1 's/^kk = mg_kof w$/kk = sub mg_kof w 1/'
131# Newton inverse with 4 steps (48 bits): wrong coefficients at w 64, reconstruction fails
132mutant inverse-4-steps 's/^for i ulong 5$/for i ulong 4/'
133# Newton inverse started from 1 instead of u (1 correct bit, 32 after 5 steps)
134mutant inverse-start-1 's/^y = u$/y = ulong 1/'
135# and with the all-ones constant no longer recognised: x and 255 at w 8 falls back, C 15 16 fails
136mutant and-ones-dropped '/^if eq o 9$/,/^end$/s/^if eq c m$/if lt c 0/'
137# and with 1 accepted as the identity: x and 1 treated as x, the iff check or the canon trap fires
138mutant and-one-accepted '/^if eq o 9$/,/^end$/s/^if eq c m$/if eq c 1/'
139# ApplyPayload size 161 -> 160: program_id goldens fail
140mutant apply-payload-160 's/^mg_b32 161$/mg_b32 160/'
141# storage sort comparator always "not greater": reverse rebuild bytes differ or load refuses order
142mutant sort-cmp-never '/^fn mg_gt /,/^fn mg_sget /s/^ret 1$/ret 0/'
143# FUNCTION_ID domain tag changed: the independent goldens fail
144mutant fid-domain 's/^text mgtfd "omega.function.v1"$/text mgtfd "omega.function.v2"/'
145
146# expectation mutants: a must-not-collapse pair asserted equal, a must-collapse pair asserted
147# different, each must fail exactly the changed record
148mut=$((mut + 1))
149sed 's/^N 1 3$/C 1 3/' "$vec" > "$out/mut-expect-c.txt"
150if cmp -s "$vec" "$out/mut-expect-c.txt"; then fail "expect-c changed nothing"
151elif detected mutant-expect-c "$out/mut-expect-c.txt" "$out/driver"; then mutok=$((mutok + 1)); fi
152mut=$((mut + 1))
153sed 's/^C 0 1$/N 0 1/' "$vec" > "$out/mut-expect-n.txt"
154if cmp -s "$vec" "$out/mut-expect-n.txt"; then fail "expect-n changed nothing"
155elif detected mutant-expect-n "$out/mut-expect-n.txt" "$out/driver"; then mutok=$((mutok + 1)); fi
156
157# negative control: a refused program defined with D is reported (pchk code 4: constant 256 at w 8)
158printf 'D 8 x 256 add\n' > "$out/neg-define.txt"
159detected neg-define "$out/neg-define.txt" "$out/driver" || true
160
161if [ "$bad" -ne 0 ]; then
162 say "MEANING FAIL checks=$checks mutants=$mutok/$mut"
163 exit 1
164fi
165say "MEANING PASS checks=$checks mutants=$mutok/$mut"