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 / tls / test / mlkem / run.sh

192 lines · 7675 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# Test suite for om/tls/sha3.om and om/tls/mlkem.om (ML-KEM-768, FIPS 203).
4# Usage: sh om/tls/test/mlkem/run.sh PATH_TO_PINNED_OMC
5# Builds om2 + sha3.om + mlkem.om + driver.om into build/tls/mlkem/, runs the NIST CAVP SHA-3/SHAKE,
6# NIST ACVP ML-KEM-768 and Wycheproof ML-KEM-768 record files (regenerate them with gen_vectors.py),
7# the length checks, a negative control, an expectation mutant and seven library mutants (each must
8# be detected: exit 1 with MISMATCH lines), a composition check against the other om/tls units, and
9# a keygen / encaps / decaps timing (informational, not a constant-time proof).
10# Environment: MLKEM_BENCH_N (default 200) calls per timed operation.
11# Exit 0 only when every check passes; the final line is
12# MLKEM PASS vectors=N (wycheproof=W fips203=K) sha3=S mutants=M/M or MLKEM FAIL ...
13# N counts every checked call (ML-KEM and SHA-3 records plus the length checks); W the calls from the
14# Wycheproof file, K from the NIST ACVP FIPS 203 file, S from the NIST CAVP SHA-3/SHAKE file.
15set -eu
16omc=${1:?usage: run.sh OMC}
17here=$(cd "$(dirname "$0")" && pwd)
18root=$(cd "$here/../../../.." && pwd)
19sha3="$root/om/tls/sha3.om"
20lib="$root/om/tls/mlkem.om"
21out="$root/build/tls/mlkem"
22rm -rf "$out"
23mkdir -p "$out"
24fail=0
25vectors=0
26wyche=0
27fips=0
28s3=0
29mut=0
30mutok=0
31nice="nice -n 19 ionice -c3"
32
33build() { # build SRC_SHA3 SRC_MLKEM OUT_BINARY
34 { echo om2; cat "$1" "$2" "$here/driver.om"; } > "$3.unit.om"
35 if ! $nice "$omc" < "$3.unit.om" > "$3" 2> "$3.err" || [ -s "$3.err" ]; then
36 echo "FAIL build $3: $(cat "$3.err")"
37 rm -f "$3"
38 return 1
39 fi
40 chmod +x "$3"
41}
42
43# calls LOG: the checked-call count from the driver's summary line
44calls() { awk '/^cases / { print $4 }' "$1"; }
45
46# run BINARY FILE LABEL: the driver must exit 0 with no mismatch
47run() {
48 if $nice "$1" < "$2" > "$out/$3.log" 2>&1; then
49 echo "PASS $3: $(tail -n 1 "$out/$3.log")"
50 else
51 echo "FAIL $3 (exit $?):"
52 cat "$out/$3.log"
53 fail=1
54 fi
55}
56
57# mutant BINARY FILE LABEL: the driver must finish normally and report a mismatch: exit status
58# exactly 1, at least one MISMATCH line, and its summary line. Any other outcome (0, 65 bad
59# input, 100+N trap, a signal) fails the run, so a mutant that crashes is not counted as detected.
60mutant() {
61 mut=$((mut + 1))
62 rc=0
63 $nice "$1" < "$2" > "$out/$3.log" 2>&1 || rc=$?
64 nm=$(grep -c '^MISMATCH ' "$out/$3.log" || true)
65 if [ "$rc" -eq 1 ] && [ "$nm" -gt 0 ] && grep -q '^cases .* mismatches [1-9]' "$out/$3.log"; then
66 echo "PASS $3 detected: $nm mismatches, $(tail -n 1 "$out/$3.log")"
67 mutok=$((mutok + 1))
68 else
69 echo "FAIL $3: exit $rc, $nm MISMATCH lines (need exit 1 and at least one): $(tail -n 1 "$out/$3.log")"
70 fail=1
71 fi
72}
73
74# libmut LABEL UNIT(sha3|mlkem) SED_SCRIPT VECTORS: a library mutant built from a sed edit of one unit
75libmut() {
76 if [ "$2" = sha3 ]; then src=$sha3; else src=$lib; fi
77 sed "$3" "$src" > "$out/$1.om"
78 if cmp -s "$src" "$out/$1.om"; then
79 mut=$((mut + 1))
80 echo "FAIL $1: sed did not change the source"
81 fail=1
82 return
83 fi
84 if [ "$2" = sha3 ]; then ok=0; build "$out/$1.om" "$lib" "$out/$1" || ok=1
85 else ok=0; build "$sha3" "$out/$1.om" "$out/$1" || ok=1; fi
86 if [ "$ok" -eq 0 ]; then
87 mutant "$out/$1" "$4" "$1"
88 else
89 mut=$((mut + 1))
90 fail=1
91 fi
92}
93
94if ! build "$sha3" "$lib" "$out/driver"; then
95 echo "MLKEM FAIL build"
96 exit 1
97fi
98echo "omc $(sha256sum "$omc" | cut -d' ' -f1)"
99echo "sha3.om $(sha256sum "$sha3" | cut -d' ' -f1)"
100echo "mlkem.om $(sha256sum "$lib" | cut -d' ' -f1)"
101echo "driver $(sha256sum "$out/driver" | cut -d' ' -f1)"
102
103printf 'L\n' > "$out/length.txt"
104for v in nist-sha3 acvp wycheproof length; do
105 f="$here/vectors/$v.txt"
106 [ "$v" = length ] && f="$out/length.txt"
107 if [ ! -s "$f" ]; then
108 echo "FAIL $v: $f missing (NOT_RUN is not PASS; regenerate with gen_vectors.py)"
109 fail=1
110 continue
111 fi
112 run "$out/driver" "$f" "$v"
113 n=$(calls "$out/$v.log")
114 vectors=$((vectors + ${n:-0}))
115 case $v in
116 nist-sha3) s3=${n:-0} ;;
117 acvp) fips=${n:-0} ;;
118 wycheproof) wyche=${n:-0} ;;
119 esac
120done
121echo "NOT_RUN SHA3_224*, SHA3_384*: not implemented (ML-KEM needs only SHA3-256/512 and SHAKE128/256)"
122echo "NOT_RUN SHA3_*Monte.rsp, SHAKE*Monte.rsp: Monte Carlo chains not converted by gen_vectors.py"
123echo "NOT_RUN ACVP ML-KEM-512 and ML-KEM-1024 groups: only ML-KEM-768 is implemented"
124
125# negative control: one input byte of the first ACVP keyGen record flipped (d byte 0 +1 in its low nibble)
126awk 'BEGIN { done = 0 } !done && /^K / {
127 c = substr($2, 2, 1); n = index("0123456789abcdef", c) % 16
128 $2 = substr($2, 1, 1) substr("0123456789abcdef", n + 1, 1) substr($2, 3); done = 1 } { print }' \
129 "$here/vectors/acvp.txt" > "$out/neg-d.txt"
130if cmp -s "$here/vectors/acvp.txt" "$out/neg-d.txt"; then
131 echo "FAIL negative-control: awk did not change the vectors"
132 fail=1
133else
134 mutant "$out/driver" "$out/neg-d.txt" negative-control
135fi
136
137# expectation mutant: the first Wycheproof encaps line expected to fail the modulus check (E 1)
138# expected to succeed instead
139awk 'BEGIN { done = 0 } !done && /^E 1 / { sub(/^E 1 /, "E 0 "); done = 1 } { print }' \
140 "$here/vectors/wycheproof.txt" > "$out/mut-expect.txt"
141mutant "$out/driver" "$out/mut-expect.txt" mutant-expectation
142
143# library mutants
144libmut mut-zeta mlkem 's/^put z 1 1729$/put z 1 1730/' "$here/vectors/acvp.txt"
145libmut mut-ninv mlkem 's/^put f i mlkem_red mul get f i 3303$/put f i mlkem_red mul get f i 3302/' \
146 "$here/vectors/acvp.txt"
147libmut mut-comp mlkem 's/^x = add shl get f i d 1664$/x = add shl get f i d 1665/' "$here/vectors/acvp.txt"
148libmut mut-reject mlkem 's/^mk = sub 0 shr or df sub 0 df 63$/mk = sub shr or df sub 0 df 63 1/' \
149 "$here/vectors/acvp.txt"
150libmut mut-ekchk mlkem 's/^if ne mlkem_ekchk ek 0$/if eq mlkem_ekchk ek 2/' "$here/vectors/wycheproof.txt"
151libmut mut-dkchk mlkem 's/^if ne mlkem_dkchk dk 0$/if gt mlkem_dkchk dk 255/' "$here/vectors/wycheproof.txt"
152libmut mut-keccak-rc sha3 's/^put k 1 0x0000000000008082$/put k 1 0x0000000000008083/' \
153 "$here/vectors/nist-sha3.txt"
154
155# composition: every om/tls unit in build.sh order with sha3 and mlkem after x25519 (name collisions,
156# call order); tls.om is included when it composes, else reported (it is edited by the TLS lane)
157printf 'fn main to int\nret 0\nend\n' > "$out/comp-main.om"
158comp() { # comp LABEL UNITS...
159 l=$1; shift
160 { echo om2; for u in "$@"; do cat "$root/om/tls/$u.om"; done; cat "$out/comp-main.om"; } > "$out/$l.unit.om"
161 $nice "$omc" < "$out/$l.unit.om" > "$out/$l" 2> "$out/$l.err"
162}
163if comp compose sha2 hkdf x25519 sha3 mlkem chacha aesgcm ec rsa x509 && [ ! -s "$out/compose.err" ]; then
164 echo "PASS compose: sha2 hkdf x25519 sha3 mlkem chacha aesgcm ec rsa x509"
165else
166 echo "FAIL compose: $(cat "$out/compose.err")"
167 fail=1
168fi
169if comp compose-tls sha2 hkdf drbg x25519 sha3 mlkem chacha aesgcm ec ecdh rsa x509 rev fips tls && [ ! -s "$out/compose-tls.err" ]; then
170 echo "PASS compose-tls: ... x509 tls"
171else
172 echo "FAIL compose-tls: $(cat "$out/compose-tls.err")"
173 fail=1
174fi
175
176# timing (informational): N keygen_seed, N encaps, N decaps calls after one discarded warm-up series
177n=${MLKEM_BENCH_N:-200}
178printf 'T %s\nT %s\n' "$n" "$n" > "$out/timing.txt"
179if $nice "$out/driver" < "$out/timing.txt" > "$out/timing.log" 2>&1; then
180 awk -v n="$n" '/^TIME line 2 / { printf "TIME %s: %d calls, %d ns, %d ns per call\n", $4, n, $8, $8 / n }' \
181 "$out/timing.log"
182else
183 echo "FAIL timing: $(cat "$out/timing.log")"
184 fail=1
185fi
186
187line="vectors=$vectors (wycheproof=$wyche fips203=$fips) sha3=$s3 mutants=$mutok/$mut"
188if [ "$fail" -ne 0 ] || [ "$mutok" -ne "$mut" ] || [ "$mut" -lt 5 ]; then
189 echo "MLKEM FAIL $line"
190 exit 1
191fi
192echo "MLKEM PASS $line"