Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/om/tls/test/mlkem/run.sh # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers 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. 15 set -eu 16 omc=${1:?usage: run.sh OMC} 17 here=$(cd "$(dirname "$0")" && pwd) 18 root=$(cd "$here/../../../.." && pwd) 19 sha3="$root/om/tls/sha3.om" 20 lib="$root/om/tls/mlkem.om" 21 out="$root/build/tls/mlkem" 22 rm -rf "$out" 23 mkdir -p "$out" 24 fail=0 25 vectors=0 26 wyche=0 27 fips=0 28 s3=0 29 mut=0 30 mutok=0 31 nice="nice -n 19 ionice -c3" 32 33 build() { # 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 44 calls() { awk '/^cases / { print $4 }' "$1"; } 45 46 # run BINARY FILE LABEL: the driver must exit 0 with no mismatch 47 run() { 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. 60 mutant() { 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 75 libmut() { 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 94 if ! build "$sha3" "$lib" "$out/driver"; then 95 echo "MLKEM FAIL build" 96 exit 1 97 fi 98 echo "omc $(sha256sum "$omc" | cut -d' ' -f1)" 99 echo "sha3.om $(sha256sum "$sha3" | cut -d' ' -f1)" 100 echo "mlkem.om $(sha256sum "$lib" | cut -d' ' -f1)" 101 echo "driver $(sha256sum "$out/driver" | cut -d' ' -f1)" 102 103 printf 'L\n' > "$out/length.txt" 104 for 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 120 done 121 echo "NOT_RUN SHA3_224*, SHA3_384*: not implemented (ML-KEM needs only SHA3-256/512 and SHAKE128/256)" 122 echo "NOT_RUN SHA3_*Monte.rsp, SHAKE*Monte.rsp: Monte Carlo chains not converted by gen_vectors.py" 123 echo "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) 126 awk '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" 130 if 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 133 else 134 mutant "$out/driver" "$out/neg-d.txt" negative-control 135 fi 136 137 # expectation mutant: the first Wycheproof encaps line expected to fail the modulus check (E 1) 138 # expected to succeed instead 139 awk 'BEGIN { done = 0 } !done && /^E 1 / { sub(/^E 1 /, "E 0 "); done = 1 } { print }' \ 140 "$here/vectors/wycheproof.txt" > "$out/mut-expect.txt" 141 mutant "$out/driver" "$out/mut-expect.txt" mutant-expectation 142 143 # library mutants 144 libmut mut-zeta mlkem 's/^put z 1 1729$/put z 1 1730/' "$here/vectors/acvp.txt" 145 libmut 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" 147 libmut mut-comp mlkem 's/^x = add shl get f i d 1664$/x = add shl get f i d 1665/' "$here/vectors/acvp.txt" 148 libmut 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" 150 libmut mut-ekchk mlkem 's/^if ne mlkem_ekchk ek 0$/if eq mlkem_ekchk ek 2/' "$here/vectors/wycheproof.txt" 151 libmut mut-dkchk mlkem 's/^if ne mlkem_dkchk dk 0$/if gt mlkem_dkchk dk 255/' "$here/vectors/wycheproof.txt" 152 libmut 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) 157 printf 'fn main to int\nret 0\nend\n' > "$out/comp-main.om" 158 comp() { # 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 } 163 if 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" 165 else 166 echo "FAIL compose: $(cat "$out/compose.err")" 167 fail=1 168 fi 169 if 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" 171 else 172 echo "FAIL compose-tls: $(cat "$out/compose-tls.err")" 173 fail=1 174 fi 175 176 # timing (informational): N keygen_seed, N encaps, N decaps calls after one discarded warm-up series 177 n=${MLKEM_BENCH_N:-200} 178 printf 'T %s\nT %s\n' "$n" "$n" > "$out/timing.txt" 179 if $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" 182 else 183 echo "FAIL timing: $(cat "$out/timing.log")" 184 fail=1 185 fi 186 187 line="vectors=$vectors (wycheproof=$wyche fips203=$fips) sha3=$s3 mutants=$mutok/$mut" 188 if [ "$fail" -ne 0 ] || [ "$mutok" -ne "$mut" ] || [ "$mut" -lt 5 ]; then 189 echo "MLKEM FAIL $line" 190 exit 1 191 fi 192 echo "MLKEM PASS $line"