sfinx

sphincs+ inspired signing utility
git clone git://git.finwo.net/app/sfinx
Log | Files | Refs | README | LICENSE

test_format.sh (4066B)


      1 #!/bin/sh
      2 # Pins the wire layout E4M4(N) | path | E4M4(L) | data against committed vectors.
      3 # Fixed inputs: seed 00..1f, message "sfinx golden vector", paths 0a (N1) and 0a1b (N2).
      4 set -eu
      5 HERE=$(cd "$(dirname "$0")" && pwd)
      6 ROOT=$(cd "${HERE}/.." && pwd)
      7 . "${HERE}/_tap.sh"
      8 
      9 BIN=$(ensure_bin "${ROOT}")
     10 TMP=$(mktemp -d)
     11 trap 'rm -rf "${TMP}"' EXIT
     12 
     13 V="${ROOT}/test/vectors"
     14 
     15 tap_begin "cli: wire format"
     16 
     17 missing=""
     18 for f in seed.bin sig-L28-N0.hex sig-L28-N1.hex sig-L32-N0.hex sig-L32-N1.hex sig-L32-N2.hex \
     19   sig-L48-N0.hex sig-L48-N1.hex sig-L64-N0.hex sig-L64-N1.hex; do
     20   [ -f "${V}/${f}" ] || missing="${missing} ${f}"
     21 done
     22 tap "$([ -z "${missing}" ] && echo 1 || echo 0)" "all vectors present" "missing:${missing}"
     23 
     24 sign_hex() {
     25   # sign_hex <bits> <path|single>
     26   if [ "$2" = single ]; then
     27     "${BIN}" sign --key-fmt raw -k "${V}/seed.bin" -L "$1" --single -m 'sfinx golden vector' --sig-fmt hex
     28   else
     29     "${BIN}" sign --key-fmt raw -k "${V}/seed.bin" -L "$1" -P "$2" -m 'sfinx golden vector' --sig-fmt hex
     30   fi
     31 }
     32 
     33 check_vector() {
     34   # check_vector <bits> <path|single> <L> <N> <bytes>
     35   got=$(sign_hex "$1" "$2")
     36   if printf '%s\n' "${got}" | cmp -s - "${V}/sig-L$3-N$4.hex"; then ok=1; else ok=0; fi
     37   tap "${ok}" "vector L$3 N$4 bytes match" "differs from sig-L$3-N$4.hex"
     38   n=$(printf '%s' "${got}" | wc -c | tr -d '[:space:]')
     39   tap "$([ "${n}" = "$(( $5 * 2 ))" ] && echo 1 || echo 0)" "vector L$3 N$4 hex length" "got ${n} want $(( $5 * 2 ))"
     40 }
     41 
     42 check_vector 224 single 28 0 842
     43 check_vector 224 0a     28 1 1067
     44 check_vector 256 single 32 0 1090
     45 check_vector 256 0a     32 1 1347
     46 check_vector 256 0a1b   32 2 2692
     47 check_vector 384 single 48 0 2402
     48 check_vector 384 0a     48 1 2787
     49 check_vector 512 single 64 0 4226
     50 check_vector 512 0a     64 1 4739
     51 
     52 # Derive the public halves the vectors must verify against, one per path length
     53 for L in 28 32 48 64; do
     54   bits=$((L * 8))
     55   "${BIN}" printkey --in-fmt raw -k "${V}/seed.bin" -L $bits -p 1 --public-only --out-fmt rawpub -o "${TMP}/pub-$L.bin"
     56   "${BIN}" printkey --in-fmt raw -k "${V}/seed.bin" -L $bits --single --public-only --out-fmt rawpub \
     57     -o "${TMP}/pub0-$L.bin"
     58 done
     59 
     60 # The hex decoder wants bare hex, strip the vectors' trailing newline
     61 tr -d '\n' <"${V}/sig-L32-N1.hex" >"${TMP}/v-L32-N1.hex"
     62 tr -d '\n' <"${V}/sig-L32-N0.hex" >"${TMP}/v-L32-N0.hex"
     63 
     64 "${BIN}" verify --key-fmt rawpub -k "${TMP}/pub-32.bin" -p 1 -m 'sfinx golden vector' --sig-fmt hex \
     65   -S "${TMP}/v-L32-N1.hex" >/dev/null && ok=1 || ok=0
     66 tap "${ok}" "committed tree vector verifies" "failed"
     67 
     68 "${BIN}" verify --key-fmt rawpub -k "${TMP}/pub0-32.bin" -m 'sfinx golden vector' --sig-fmt hex \
     69   -S "${TMP}/v-L32-N0.hex" >/dev/null && ok=1 || ok=0
     70 tap "${ok}" "committed single vector verifies" "failed"
     71 
     72 # Raw signature is the same bytes, check the byte count too
     73 "${BIN}" sign --key-fmt raw -k "${V}/seed.bin" -L 256 -P 0a -m 'sfinx golden vector' --sig-fmt raw \
     74   -o "${TMP}/sig-raw.bin"
     75 n=$(wc -c <"${TMP}/sig-raw.bin" | tr -d '[:space:]')
     76 tap "$([ "${n}" = 1347 ] && echo 1 || echo 0)" "raw N1 signature is 1347 bytes" "got ${n}"
     77 
     78 patch_byte() {
     79   # patch_byte <file> <offset> <decimal>
     80   printf "\\$(printf '%03o' "$3")" | dd of="$1" bs=1 seek="$2" count=1 conv=notrunc 2>/dev/null
     81 }
     82 
     83 verify_raw() {
     84   # verify_raw <pub> <sig>
     85   "${BIN}" verify --key-fmt rawpub -k "$1" -p 1 -m 'sfinx golden vector' -S "$2" >/dev/null 2>&1
     86 }
     87 
     88 head -c 1346 "${TMP}/sig-raw.bin" >"${TMP}/short.bin"
     89 rc=0
     90 verify_raw "${TMP}/pub-32.bin" "${TMP}/short.bin" || rc=$?
     91 tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "a truncated signature fails" "exit ${rc}"
     92 
     93 cp "${TMP}/sig-raw.bin" "${TMP}/bad-n.bin"
     94 patch_byte "${TMP}/bad-n.bin" 0 2
     95 rc=0
     96 verify_raw "${TMP}/pub-32.bin" "${TMP}/bad-n.bin" || rc=$?
     97 tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "a patched N fails" "exit ${rc}"
     98 
     99 cp "${TMP}/sig-raw.bin" "${TMP}/bad-path.bin"
    100 patch_byte "${TMP}/bad-path.bin" 1 11
    101 rc=0
    102 verify_raw "${TMP}/pub-32.bin" "${TMP}/bad-path.bin" || rc=$?
    103 tap "$([ "${rc}" != 0 ] && echo 1 || echo 0)" "a patched path byte fails" "exit ${rc}"
    104 
    105 tap_plan