omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / ed25519 / test / run.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Test suite for om/ed25519. Usage: sh om/ed25519/test/run.sh PATH_TO_PINNED_OMC |
| 4 | # Builds sha512.om + ed25519.om + test/driver.om into build/test/ed25519/, runs every static vector |
| 5 | # file in both modes, two library mutants and one expectation mutant (each must fail), the python |
| 6 | # differential corpus when python3 with pynacl and cryptography is present, and one timing series. |
| 7 | # Exit 0 only when every check passes. |
| 8 | # Environment: ED_DIFF_COUNT (default 2000 keypairs), ED_DIFF_SEED (default 1), |
| 9 | # ED_BENCH_N (default 1000 repeats of RFC 8032 TEST 1 per mode). |
| 10 | set -eu |
| 11 | omc=${1:?usage: run.sh OMC} |
| 12 | here=$(cd "$(dirname "$0")" && pwd) |
| 13 | root=$(cd "$here/../../.." && pwd) |
| 14 | out="$root/build/test/ed25519" |
| 15 | rm -rf "$out" |
| 16 | mkdir -p "$out" |
| 17 | fail=0 |
| 18 | |
| 19 | build() { # build SRC_ED25519 OUT_BINARY |
| 20 | cat "$root/om/sha512/sha512.om" "$1" "$here/driver.om" > "$2.unit.om" |
| 21 | if ! "$omc" < "$2.unit.om" > "$2" 2> "$2.err"; then |
| 22 | echo "FAIL build $2: $(cat "$2.err")" |
| 23 | return 1 |
| 24 | fi |
| 25 | chmod +x "$2" |
| 26 | } |
| 27 | |
| 28 | # run BINARY FILE LABEL: the driver must exit 0 with no mismatch |
| 29 | run() { |
| 30 | if "$1" < "$2" > "$out/$3.log" 2>&1; then |
| 31 | echo "PASS $3: $(tail -n 1 "$out/$3.log")" |
| 32 | else |
| 33 | echo "FAIL $3 (exit $?):" |
| 34 | cat "$out/$3.log" |
| 35 | fail=1 |
| 36 | fi |
| 37 | } |
| 38 | |
| 39 | # mutant BINARY FILE LABEL: the driver must finish normally and report a mismatch: exit status |
| 40 | # exactly 1, at least one MISMATCH line, and its summary line. Any other outcome (0, 65 bad |
| 41 | # input, 100+N trap, a signal) fails the run, so a mutant that crashes is not counted as detected. |
| 42 | mutant() { |
| 43 | rc=0 |
| 44 | "$1" < "$2" > "$out/$3.log" 2>&1 || rc=$? |
| 45 | nm=$(grep -c '^MISMATCH ' "$out/$3.log" || true) |
| 46 | if [ "$rc" -eq 1 ] && [ "$nm" -gt 0 ] && grep -q '^cases .* mismatches [1-9]' "$out/$3.log"; then |
| 47 | echo "PASS $3 detected: $nm mismatches, $(tail -n 1 "$out/$3.log")" |
| 48 | else |
| 49 | echo "FAIL $3: exit $rc, $nm MISMATCH lines (need exit 1 and at least one): $(tail -n 1 "$out/$3.log")" |
| 50 | fail=1 |
| 51 | fi |
| 52 | } |
| 53 | |
| 54 | build "$root/om/ed25519/ed25519.om" "$out/driver" |
| 55 | echo "omc $(sha256sum "$omc" | cut -d' ' -f1)" |
| 56 | echo "ed25519.om $(sha256sum "$root/om/ed25519/ed25519.om" | cut -d' ' -f1)" |
| 57 | echo "sha512.om $(sha256sum "$root/om/sha512/sha512.om" | cut -d' ' -f1)" |
| 58 | echo "driver $(sha256sum "$out/driver" | cut -d' ' -f1)" |
| 59 | |
| 60 | for v in rfc8032 zip215 wycheproof edge; do |
| 61 | run "$out/driver" "$here/vectors/$v.txt" "$v" |
| 62 | done |
| 63 | |
| 64 | # expectation mutant: the first expected-valid RFC line flipped to expected-invalid in both modes |
| 65 | awk 'BEGIN { done = 0 } !done && /^1 1 / { sub(/^1 1 /, "0 0 "); done = 1 } { print }' \ |
| 66 | "$here/vectors/rfc8032.txt" > "$out/mut-expect.txt" |
| 67 | mutant "$out/driver" "$out/mut-expect.txt" mutant-expectation |
| 68 | |
| 69 | # library mutant 1: one limb of the constant 2d changed (point addition breaks) |
| 70 | sed 's/^put w 48 61785$/put w 48 61786/' "$root/om/ed25519/ed25519.om" > "$out/mut-d2.om" |
| 71 | if cmp -s "$root/om/ed25519/ed25519.om" "$out/mut-d2.om"; then |
| 72 | echo "FAIL mutant-d2: sed did not change the source" |
| 73 | fail=1 |
| 74 | elif build "$out/mut-d2.om" "$out/mut-d2"; then |
| 75 | mutant "$out/mut-d2" "$here/vectors/rfc8032.txt" mutant-d2-rfc8032 |
| 76 | else |
| 77 | fail=1 |
| 78 | fi |
| 79 | |
| 80 | # library mutant 2: the cofactor multiplication of consensus mode removed |
| 81 | sed '/^edpadd w edq edrr$/,/^ret edpid w edq$/{/^edpdbl w edq$/d;}' \ |
| 82 | "$root/om/ed25519/ed25519.om" > "$out/mut-cofactor.om" |
| 83 | if cmp -s "$root/om/ed25519/ed25519.om" "$out/mut-cofactor.om"; then |
| 84 | echo "FAIL mutant-cofactor: sed did not change the source" |
| 85 | fail=1 |
| 86 | elif build "$out/mut-cofactor.om" "$out/mut-cofactor"; then |
| 87 | mutant "$out/mut-cofactor" "$here/vectors/zip215.txt" mutant-cofactor-zip215 |
| 88 | else |
| 89 | fail=1 |
| 90 | fi |
| 91 | |
| 92 | # differential corpus (python test oracle; skipped when unavailable) |
| 93 | if command -v python3 > /dev/null 2>&1 && |
| 94 | python3 -c 'import nacl.signing, cryptography.hazmat.primitives.asymmetric.ed25519' 2> /dev/null; then |
| 95 | python3 "$here/gen/diff.py" "$out/diff" "${ED_DIFF_COUNT:-2000}" "${ED_DIFF_SEED:-1}" |
| 96 | for f in "$out"/diff/diff-*.txt; do |
| 97 | run "$out/driver" "$f" "$(basename "$f" .txt)" |
| 98 | done |
| 99 | else |
| 100 | echo "NOTE differential corpus skipped: python3 with pynacl and cryptography not found" |
| 101 | fi |
| 102 | |
| 103 | # timing: RFC 8032 TEST 1 (empty message), ED_BENCH_N verifies per mode |
| 104 | n=${ED_BENCH_N:-1000} |
| 105 | grep '^1 1 ' "$here/vectors/rfc8032.txt" | head -n 1 > "$out/t1.txt" |
| 106 | sed 's/^1 1 /1 - /' "$out/t1.txt" > "$out/bench-strict.txt" |
| 107 | sed 's/^1 1 /- 1 /' "$out/t1.txt" > "$out/bench-consensus.txt" |
| 108 | for m in strict consensus; do |
| 109 | if "$out/driver" "$n" < "$out/bench-$m.txt" > "$out/bench-$m.log" 2>&1; then |
| 110 | ns=$(awk '{ print $8 }' "$out/bench-$m.log") |
| 111 | echo "TIME $m: $n verifies, $ns ns total, $((ns / n)) ns per verify" |
| 112 | else |
| 113 | echo "FAIL bench-$m" |
| 114 | cat "$out/bench-$m.log" |
| 115 | fail=1 |
| 116 | fi |
| 117 | done |
| 118 | |
| 119 | if [ "$fail" -ne 0 ]; then |
| 120 | echo "ed25519: FAIL" |
| 121 | exit 1 |
| 122 | fi |
| 123 | echo "ed25519: all checks passed" |