aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / specs / verified-crumb / tools / make-golden.sh
| 1 | #!/bin/bash |
| 2 | # make-golden.sh OUT_DIR: write the VC1 golden vectors (hex files) and |
| 3 | # expected.txt from explicit hex fragments. It does NOT use the C tool, so the |
| 4 | # vectors are built independently of the encoder under test. Shell + coreutils |
| 5 | # + xxd only. |
| 6 | set -eu |
| 7 | export LC_ALL=C |
| 8 | out=${1:?usage: make-golden.sh OUT_DIR} |
| 9 | mkdir -p "$out" |
| 10 | |
| 11 | hexs() { printf '%s' "$1" | xxd -p -c 99999 | tr -d '\n'; } # ASCII -> hex |
| 12 | rep() { local i s=""; for ((i = 0; i < 32; i++)); do s+="$1"; done; printf '%s' "$s"; } # byte -> 32 bytes |
| 13 | u32() { printf '%08x' "$1"; } |
| 14 | str() { printf '%016x%s' "${#1}" "$(hexs "$1")"; } # u64 BE length + bytes |
| 15 | TAG=$(hexs "AIEN_VERIFIED_CRUMB_V1") |
| 16 | |
| 17 | # vc VERSION SEM CON KIND SRC "REALS" "DEPS" RECEIPT PROFILE VER ROOT "EXPORTS" "CAPS" |
| 18 | # ids are one byte token (e.g. 11) repeated 32 times; KIND = one byte token |
| 19 | # (01 source, 02 IR); REALS = tokens; |
| 20 | # DEPS = tokens "sem/contract" ; EXPORTS and CAPS = ASCII words. |
| 21 | vc() { |
| 22 | local ver=$1 sem=$2 con=$3 kind=$4 src=$5 reals=$6 deps=$7 rcpt=$8 prof=$9 pver=${10} root=${11} exps=${12} caps=${13} |
| 23 | local s="$TAG$(u32 "$ver")$(rep "$sem")$(rep "$con")$kind$(rep "$src")" r d e c n |
| 24 | n=0; for r in $reals; do n=$((n + 1)); done; s+=$(u32 $n) |
| 25 | for r in $reals; do s+=$(rep "$r"); done |
| 26 | n=0; for d in $deps; do n=$((n + 1)); done; s+=$(u32 $n) |
| 27 | for d in $deps; do s+="$(rep "${d%/*}")$(rep "${d#*/}")"; done |
| 28 | s+="$(rep "$rcpt")$(str "$prof")$(str "$pver")$(rep "$root")" |
| 29 | n=0; for e in $exps; do n=$((n + 1)); done; s+=$(u32 $n) |
| 30 | for e in $exps; do s+=$(str "$e"); done |
| 31 | n=0; for c in $caps; do n=$((n + 1)); done; s+=$(u32 $n) |
| 32 | for c in $caps; do s+=$(str "$c"); done |
| 33 | printf '%s' "$s" |
| 34 | } |
| 35 | emit() { printf '%s\n' "$2" > "$out/$1.hex"; } |
| 36 | |
| 37 | : > "$out/expected.txt" |
| 38 | # expected ACCEPT ids are computed with sha256sum over the raw bytes |
| 39 | # (independent of the C tool's own SHA-256). |
| 40 | accept() { |
| 41 | emit "$1" "$2" |
| 42 | local id; id=$(printf '%s' "$2" | xxd -r -p | sha256sum | cut -d' ' -f1) |
| 43 | echo "$1 ACCEPT $id" >> "$out/expected.txt" |
| 44 | } |
| 45 | refuse() { emit "$1" "$2"; echo "$1 REFUSE $3" >> "$out/expected.txt"; } |
| 46 | |
| 47 | P=test-profile |
| 48 | accept v01_minimal "$(vc 1 11 22 01 33 "44" "" 55 $P 1.0.0 66 "" "")" |
| 49 | accept v02_deps "$(vc 1 11 22 01 33 "44 45" "a1/b1 a2/b2" 55 $P 1.0.0 66 "main" "")" |
| 50 | accept v03_capabilities "$(vc 1 12 22 01 33 "44" "a1/b1" 56 $P 1.0.0 67 "add sub" "cap.clock.read cap.fs.read")" |
| 51 | |
| 52 | refuse r01_duplicate_dep "$(vc 1 11 22 01 33 "44" "a1/b1 a1/b2" 55 $P 1.0.0 66 "" "")" DUPLICATE_DEPENDENCY |
| 53 | refuse r02_unsorted_deps "$(vc 1 11 22 01 33 "44" "a2/b2 a1/b1" 55 $P 1.0.0 66 "" "")" UNSORTED_DEPENDENCIES |
| 54 | refuse r03_zero_receipt_id "$(vc 1 11 22 01 33 "44" "" 00 $P 1.0.0 66 "" "")" MISSING_RECEIPT |
| 55 | refuse r04_self_dependency "$(vc 1 11 22 01 33 "44" "11/b1" 55 $P 1.0.0 66 "" "")" DEPENDENCY_CYCLE |
| 56 | refuse r05_trailing_byte "$(vc 1 11 22 01 33 "44" "" 55 $P 1.0.0 66 "" "")00" TRAILING_BYTES |
| 57 | refuse r06_unknown_version "$(vc 2 11 22 01 33 "44" "" 55 $P 1.0.0 66 "" "")" UNKNOWN_FORMAT_VERSION |
| 58 | v=$(vc 1 11 22 01 33 "44" "" 55 $P 1.0.0 66 "" "") |
| 59 | refuse r07_truncated "${v%??}" TRUNCATED |
| 60 | refuse r08_bad_domain_tag "$(printf '%s' "$v" | sed 's/^\(.\{42\}\)../\10a/')" BAD_DOMAIN_TAG |
| 61 | refuse r09_unsorted_exports "$(vc 1 11 22 01 33 "44" "" 55 $P 1.0.0 66 "sub add" "")" NONCANONICAL_SET |
| 62 | |
| 63 | # digest_kind: 0x02 (IR) with the same digest bytes as v01 must give a different id |
| 64 | accept v04_ir_kind "$(vc 1 11 22 02 33 "44" "" 55 $P 1.0.0 66 "" "")" |
| 65 | refuse r10_bad_digest_kind "$(vc 1 11 22 03 33 "44" "" 55 $P 1.0.0 66 "" "")" BAD_DIGEST_KIND |
| 66 | refuse r11_zero_id "$(vc 1 00 22 01 33 "44" "" 55 $P 1.0.0 66 "" "")" ZERO_ID |
| 67 | refuse r12_bad_string "$(vc 1 11 22 01 33 "44" "" 55 "bad profile" 1.0.0 66 "" "")" BAD_STRING |
| 68 | # 257 realizations declared (limit 256): refused at the count, before any body |
| 69 | refuse r13_too_many_entries "$TAG$(u32 1)$(rep 11)$(rep 22)01$(rep 33)$(u32 257)" TOO_MANY_ENTRIES |