diff --git a/.github/workflows/atoma.yml b/.github/workflows/atoma.yml index 099f068..bbcf124 100644 --- a/.github/workflows/atoma.yml +++ b/.github/workflows/atoma.yml @@ -258,40 +258,52 @@ jobs: if-no-files-found: error retention-days: 30 - coq_pin_selftest: - name: Pinned Coq rejects a corrupted checksum + native_word: + name: Proven native word replay runs-on: ubuntu-24.04 - timeout-minutes: 10 + timeout-minutes: 20 steps: - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - name: Cache pinned Coq packages - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + - name: Provision pinned Zig and independent Coq checker + run: | + set -euo pipefail + sudo apt-get update + sudo apt-get install -y --no-install-recommends coq=8.18.0+dfsg-1build2 binutils xxd + bash scripts/atoma-download-zig.sh "$RUNNER_TEMP/atoma-native-word-zig" + - name: Capture and replay the linked Proven decoder + run: | + set -euo pipefail + mkdir -p build/atoma-evidence + bash scripts/atoma-native-word.sh check build/atoma-evidence/native-word + bash scripts/atoma-native-word.sh replay build/atoma-evidence/native-word + - uses: actions/upload-artifact@v7.0.2 + if: always() with: - path: ${{ runner.temp }}/atoma-coq-debs - key: coq-8.18-debs-${{ hashFiles('ci/coq-8.18.debs.sha256') }} - - name: Negative self-test (a flipped sha256 must fail verification) - run: bash scripts/atoma-provision-coq.sh --self-test-bad-sha "$RUNNER_TEMP/atoma-coq-debs" + name: proven-native-word-evidence-${{ github.sha }} + path: build/atoma-evidence/native-word/ + if-no-files-found: error + retention-days: 30 soundness: name: Atoma soundness gate - needs: [core_soundness, heap_consumers, byte_translation, coq_pin_selftest] + needs: [core_soundness, heap_consumers, byte_translation, native_word] if: ${{ always() }} runs-on: ubuntu-24.04 timeout-minutes: 2 steps: - - name: Require existing replay, heap pilots and native byte validation + - name: Require replay, heap pilots, byte validation and native decoder env: CORE_RESULT: ${{ needs.core_soundness.result }} HEAP_RESULT: ${{ needs.heap_consumers.result }} TRANSLATION_RESULT: ${{ needs.byte_translation.result }} - COQ_PIN_RESULT: ${{ needs.coq_pin_selftest.result }} + NATIVE_WORD_RESULT: ${{ needs.native_word.result }} run: | test "$CORE_RESULT" = success test "$HEAP_RESULT" = success test "$TRANSLATION_RESULT" = success - test "$COQ_PIN_RESULT" = success + test "$NATIVE_WORD_RESULT" = success gate: name: CI/gate diff --git a/docs/decisions/ATOMA-BOUNDARY-SOURCE.adoc b/docs/decisions/ATOMA-BOUNDARY-SOURCE.adoc index 2266cd0..51da9f3 100644 --- a/docs/decisions/ATOMA-BOUNDARY-SOURCE.adoc +++ b/docs/decisions/ATOMA-BOUNDARY-SOURCE.adoc @@ -58,6 +58,43 @@ These are re-proved legs, not transported proof objects. Statement correspondence is BRIDGE-PENDING; no PWG upgrade, external verified-checker qualification or Mathesis admission follows automatically. +== Compiled Proven decoder observation + +The additional native gate uses the Proven decoder and its literal imported +encoder at the commit in `tests/consumer-word/proven.pin`. It checks both SHA-256 +locks and builds only those snapshots. A test-only Zig C ABI bridge calls +`decodeWord(u64)` and returns a `u16` with values 0 through 255 for successful +decodes and 256 for `InvalidRefcResult`. The gate compiles the pure Zig observer +and bridge into separate objects, checks their undefined and defined ELF +symbols, and statically links them. It uses Zig 0.15.2, `x86_64-linux-musl`, +baseline CPU, Debug, LLVM and no LTO. + +For each byte in ascending order, the observer visits its canonical word, +every single-bit corruption at positions 0–31 and 40–63 in ascending order, and +the combined corruption at positions 1 and 40. The final input is the largest +`u64`. Each of the 14,849 ten-byte records carries the actual `u64` argument +and `u16` result in little-endian order. Coq derives the ordered corpus +independently, checks both fields against the model, and applies the admitted +source correspondence theorem to the 256 accepted and 14,593 refused calls. + +A separately compiled decoder mutation accepts one planned mixed corruption +through the same bridge symbol and the proof rejects its observed group. Other controls exchange +two refused inputs without changing their results, replace a returned 255 with +a refusal, truncate the trace, and change each pinned snapshot. The evidence +binds the original sources, tool identities and build commands, both objects, +the executable, the raw capture and its checked Coq capsule. CI runs the +archived executable again and requires byte-for-byte agreement. + +The gate keeps an `INCOMPLETE` receipt for the bounded observation. All-input +machine-code and compiler correspondence, foreign ABI refinement and the RefC +runtime path remain open. The existing Proven and UAA Idris-oracle pilots and +strict theory checks remain required. + +[source,bash] +---- +bash scripts/atoma-native-word.sh replay PATH_TO_TRUSTED_NATIVE_EVIDENCE +---- + == Ownership and execution premises still owed * A live registry has one serialized owner; concurrent operations are outside scope. diff --git a/docs/status/PROOF-STATUS.adoc b/docs/status/PROOF-STATUS.adoc index 1074deb..bd2bb35 100644 --- a/docs/status/PROOF-STATUS.adoc +++ b/docs/status/PROOF-STATUS.adoc @@ -114,3 +114,12 @@ The policy claims table is not promoted by these additions. See link:../decisions/ATOMA-HEAP-BOUNDARY.adoc[scope and native correspondence premises]. All hand re-proof correspondence remains BRIDGE-PENDING. +== Compiled Proven word-decoder slice + +The required native job checks 14,849 ordered calls to the linked +`decodeWord` bridge against the exact source capsule and arithmetic model. +The exact workflow head and its artifact own the execution status. The +`INCOMPLETE` receipt leaves all-input native, compiler, foreign ABI and RefC +runtime refinement open. See link:../decisions/ATOMA-BOUNDARY-SOURCE.adoc[scope +and replay instructions]. + diff --git a/examples/atoma/proven-word/bridge.zig b/examples/atoma/proven-word/bridge.zig new file mode 100644 index 0000000..9b561d8 --- /dev/null +++ b/examples/atoma/proven-word/bridge.zig @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell +const decoder = @import("bits8_decode_word.zig"); + +// This test-only C ABI retains all 256 byte values and a separate refusal code. +export fn atoma_proven_decode_word(raw: u64) callconv(.c) u16 { + const byte = decoder.decodeWord(raw) catch return 256; + return byte; +} diff --git a/examples/atoma/proven-word/observer.zig b/examples/atoma/proven-word/observer.zig new file mode 100644 index 0000000..e46f6a9 --- /dev/null +++ b/examples/atoma/proven-word/observer.zig @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell +const std = @import("std"); + +comptime { + _ = std.start; +} + +extern fn atoma_proven_decode_word(raw: u64) callconv(.c) u16; + +// Each record carries the actual argument and result, both little-endian. +fn record(out: *[10]u8, raw: u64) void { + std.mem.writeInt(u64, out[0..8], raw, .little); + std.mem.writeInt(u16, out[8..10], atoma_proven_decode_word(raw), .little); +} + +pub fn main() !void { + const stdout = std.fs.File.stdout(); + for (0..256) |byte| { + const canonical = (@as(u64, byte) << 32) | 1; + var rows: [58][10]u8 = undefined; + record(&rows[0], canonical); + var index: usize = 1; + for (0..64) |bit| { + if (32 <= bit and bit < 40) continue; + record(&rows[index], canonical ^ (@as(u64, 1) << @as(u6, @intCast(bit)))); + index += 1; + } + record(&rows[index], canonical ^ ((@as(u64, 1) << 1) | (@as(u64, 1) << 40))); + try stdout.writeAll(std.mem.asBytes(&rows)); + } + var maximum: [10]u8 = undefined; + record(&maximum, std.math.maxInt(u64)); + try stdout.writeAll(&maximum); +} diff --git a/scripts/atoma-native-word.sh b/scripts/atoma-native-word.sh new file mode 100644 index 0000000..e35c2f9 --- /dev/null +++ b/scripts/atoma-native-word.sh @@ -0,0 +1,339 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell +set -euo pipefail +export LC_ALL=C + +root=$(cd "$(dirname "$0")/.." && pwd) +fixtures="$root/examples/atoma/proven-word" +theory="$root/verification/atoma/consumer/coq" +pins="$root/tests/consumer-word" +symbol=atoma_proven_decode_word +flags=(-target x86_64-linux-musl -mcpu=baseline -O Debug -fllvm -fno-lto) +sources=(bits8_decode_word.zig bits8_word.zig) +libraries=(ScalarSource CanonicalWord WordSource AtomaWord NativeWord TraceData AtomaNativeWord NegativeControls) + +fail() { echo "NATIVE-WORD-REFUSED: $*" >&2; exit 1; } +digest() { sha256sum -- "$1" | cut -d ' ' -f 1; } +tool() { + local path + path=$(command -v "$1") || fail "missing tool $1" + realpath -- "$path" +} + +check_sources() { + cmp -s -- "$pins/proven.pin" proven.pin && + cmp -s -- "$pins/proven-native.sha256" proven-native.sha256 && + sha256sum --strict --quiet -c proven-native.sha256 || + fail 'Proven revision, decoder or imported encoder differs from the locked snapshot' +} + +source_capsule() { + bash "$root/scripts/atoma-word-capsule.sh" "${sources[@]}" +} + +# od exposes every raw byte; assemble literals by spelling hex digits in reverse +# byte order without asking awk to represent a 64-bit integer. +trace_source() { + local trace=$1 group_index=${2:--1} name=${3:-observed} + [[ -f $trace && $(wc -c < "$trace") -eq 148490 ]] || + fail 'expected exactly 14,849 ten-byte native records' + od -An -v -w10 -tx1 -- "$trace" | awk -v selected="$group_index" -v name="$name" ' + NF != 10 { bad = 1; exit 1 } + { + group = int((NR - 1) / 58) + position = (NR - 1) % 58 + if (selected >= 0 && group != selected) next + if (position == 0) { + if (selected >= 0) printf "Definition %s : list row := [\n", name + else printf "Definition group_%03d : list row := [\n", group + } + printf " (0x%s%s%s%s%s%s%s%s, 0x%s%s)%s\n", + $8,$7,$6,$5,$4,$3,$2,$1,$10,$9, (position == 57 || group == 256 ? "]." : ";") + } + END { + if (bad || NR != 14849) exit 1 + if (selected < 0) { + print "Definition observed : list (list row) := [" + for (group = 0; group <= 256; group++) + printf " group_%03d%s\n", group, (group == 256 ? "]." : ";") + } + }' +} + +trace_prelude() { + printf '%s\n' 'From Coq Require Import List NArith.' 'Require Import NativeWord.' \ + 'Import ListNotations.' 'Open Scope N_scope.' +} + +check_trace_encoding() { + # Invert the generated literal spelling and compare with the actual capture. + awk ' + /^ \(0x/ { + word = substr($0, 6, 16) + result = substr($0, 26, 4) + for (i = 15; i > 0; i -= 2) printf "%s", substr(word, i, 2) + for (i = 3; i > 0; i -= 2) printf "%s", substr(result, i, 2) + }' TraceData.v | xxd -r -p | cmp -s -- trace.bin - || + fail 'trace literals do not round-trip to the captured bytes' +} + +zig_build() { + local log=$1 + shift + { printf '%q ' "$zig" "$@"; printf '\n'; } >> build-commands.txt + timeout -k 5 120 "$zig" "$@" > "$log" 2>&1 || + { cat "$log" >&2; fail "build failed ($log)"; } +} + +inspect_elf() { + local object=$1 table=$2 + "$elf" --wide --file-header --program-headers --dynamic --syms "$object" > "$table" + grep -Eq '^ *Class: +ELF64$' "$table" && + grep -Eq '^ *Machine: +Advanced Micro Devices X86-64$' "$table" || + fail "$object is not ELF64 x86-64" +} + +require_definition() { + awk -v sym="$symbol" ' + $8 == sym && $5 == "GLOBAL" { total++; if ($4 == "FUNC" && $6 == "DEFAULT" && $7 != "UND") defined++ } + END { exit !(total == 1 && defined == 1) } + ' "$1" || fail "$1 does not define exactly one global function $symbol" +} + +build_and_link() { + local bridge=$1 prefix=$2 + zig_build "$prefix-bridge.log" build-obj "$bridge" "${flags[@]}" "-femit-bin=$prefix-bridge.o" + inspect_elf "$prefix-bridge.o" "$prefix-bridge-elf.txt" + require_definition "$prefix-bridge-elf.txt" + sha256sum observer.o "$prefix-bridge.o" > "$prefix-link.sha256" + zig_build "$prefix-link.log" build-exe observer.o "$prefix-bridge.o" "${flags[@]}" -static "-femit-bin=$prefix-observer" + sha256sum --strict --quiet -c "$prefix-link.sha256" || fail 'link inputs changed' + inspect_elf "$prefix-observer" "$prefix-observer-elf.txt" + require_definition "$prefix-observer-elf.txt" + awk '$1 == "INTERP" || $2 == "(NEEDED)" { dynamic = 1 } + END { exit dynamic }' "$prefix-observer-elf.txt" || fail "$prefix-observer depends on a dynamic runtime" +} + +observe() { + local executable=$1 trace=$2 + timeout -k 5 30 "./$executable" > "$trace" 2> "$trace.stderr" || + { cat "$trace.stderr" >&2; fail "execution of $executable failed"; } + [[ ! -s $trace.stderr && $(wc -c < "$trace") -eq 148490 ]] || fail "invalid capture from $executable" +} + +control_receipt() ( + mkdir controls mixed-source + cp bridge.zig bits8_word.zig mixed-source/ + sed '/^pub fn decodeWord(/a\ if (raw == ((@as(u64, 1) << 40) | 3)) return 0;' \ + bits8_decode_word.zig > mixed-source/bits8_decode_word.zig + build_and_link mixed-source/bridge.zig mixed + observe mixed-observer controls/mixed.bin + + cp trace.bin controls/swapped.bin + # Exchange only the arguments of the first two refusal records. + dd if=trace.bin bs=1 skip=20 count=8 status=none | + dd of=controls/swapped.bin bs=1 seek=10 conv=notrunc status=none + dd if=trace.bin bs=1 skip=10 count=8 status=none | + dd of=controls/swapped.bin bs=1 seek=20 conv=notrunc status=none + + cp trace.bin controls/alias.bin + # Replace result 255 by refusal code 256 on the canonical byte-255 input. + printf '\000\001' | dd of=controls/alias.bin bs=1 seek=$((255 * 58 * 10 + 8)) conv=notrunc status=none + + head -c 148489 trace.bin > controls/truncated.bin + if (trace_source controls/truncated.bin) > controls/truncated.log 2>&1; then + fail 'accepted a truncated trace' + fi + grep -Fq 'expected exactly 14,849 ten-byte native records' controls/truncated.log + + cp -- "${sources[@]}" proven.pin proven-native.sha256 controls/ + ( + cd controls + printf '\n// changed decoder import provenance\n' >> bits8_decode_word.zig + if (check_sources) > decoder.log 2>&1; then fail 'accepted changed decoder bytes'; fi + grep -Fq 'differs from the locked snapshot' decoder.log + cp ../bits8_decode_word.zig . + printf '\n// changed imported encoder provenance\n' >> bits8_word.zig + if (check_sources) > encoder.log 2>&1; then fail 'accepted changed imported encoder bytes'; fi + grep -Fq 'differs from the locked snapshot' encoder.log + ) + + { + trace_prelude + trace_source controls/mixed.bin 0 mixed + trace_source controls/swapped.bin 0 swapped + trace_source controls/alias.bin 255 alias + cat <<'COQ' +Lemma mixed_returned_byte : + nth 57%nat mixed (0, 256) = (1099511627779, 0). +Proof. reflexivity. Qed. +Lemma mixed_rejected : NativeWord.check_group (NativeWord.cases 0) mixed = false. +Proof. vm_compute. reflexivity. Qed. +Lemma refused_input_swap_rejected : NativeWord.check_group (NativeWord.cases 0) swapped = false. +Proof. vm_compute. reflexivity. Qed. +Lemma refusal_alias_rejected : NativeWord.check_group (NativeWord.cases 255) alias = false. +Proof. vm_compute. reflexivity. Qed. +COQ + } > NegativeControls.v +) + +model_capsule() { + cat <<'COQ' +From Coq Require Import List NArith. +Require Import CanonicalWord WordSource AtomaWord NativeWord TraceData. +Import ListNotations. + +Lemma native_observation_accepted : NativeWord.check TraceData.observed = true. +Proof. vm_compute. reflexivity. Qed. + +Theorem native_trace_model : + TraceData.observed = map (map NativeWord.expected_row) NativeWord.corpus. +Proof. apply NativeWord.check_sound. exact native_observation_accepted. Qed. + +Theorem native_trace_source : forall raw result, + In (raw, result) (concat TraceData.observed) -> + WordSource.run decoder_bytes encoder_bytes raw = Some (CanonicalWord.decode raw) /\ + result = NativeWord.wire_result (CanonicalWord.decode raw). +Proof. + intros raw result H. + exact (NativeWord.check_source decoder_bytes encoder_bytes TraceData.observed + raw result source_accepted native_observation_accepted H). +Qed. + +Print Assumptions native_trace_model. +Print Assumptions native_trace_source. +COQ +} + +compile_coq() { + local module=$1 + timeout -k 5 180 "$coqc" -q -Q . '' "$module.v" > "$module.log" 2>&1 || + { cat "$module.log" >&2; fail "Coq rejected $module"; } +} + +kernel_check() { + timeout -k 5 180 "$coqchk" -silent -Q . '' "${libraries[@]}" +} + +receipt() { + jq -cnS --arg revision "$(cat proven.pin)" --arg manifest "$(digest inputs.sha256)" \ + --arg decoder "$(digest "${sources[0]}")" --arg encoder "$(digest "${sources[1]}")" \ + --arg executable "$(digest native-observer)" --arg trace "$(digest trace.bin)" \ + --arg proof "$(digest AtomaNativeWord.vo)" --arg symbol "$symbol" ' + {schema:"atoma.native.proven-word/1",status:"INCOMPLETE", + proven_revision:$revision,decoder_sha256:$decoder,encoder_sha256:$encoder, + executable_sha256:$executable,trace_sha256:$trace,proof_sha256:$proof,manifest_sha256:$manifest, + scope:"ordered observed calls through a test C ABI to Proven decodeWord", + inputs_checked:14849,canonical_inputs:256,refused_inputs:14593,record_bytes:10,byte_order:"little", + export_symbol:$symbol,refusal_code:256,zig_version:"0.15.2",target:"x86_64-linux-musl", + cpu:"baseline",mode:"Debug",linkage:"static",lto:false, + coqchk:"checked",assumptions:[],all_u64_native_refinement:"unproved", + compiler_refinement:"unproved",foreign_abi_refinement:"unproved",refc_runtime_refinement:"unproved"}' +} + +check() ( + local revision module + mkdir -- "$1" || fail 'evidence directory must be new' + cd "$1" + [[ $(uname -sm) == 'Linux x86_64' ]] || fail 'Linux x86-64 execution is required' + uname -sm > host.txt + "$zig" version > zig-version.txt + [[ $(cat zig-version.txt) == 0.15.2 ]] || fail 'Zig 0.15.2 required' + "$coqc" --version > coq-version.txt + "$elf" --version > readelf-version.txt + printf '%s %s\n' "$(digest "$zig")" zig "$(digest "$elf")" readelf \ + "$(digest "$coqc")" coqc "$(digest "$coqchk")" coqchk > tool-digests.txt + + cp -- "$pins/"{proven.pin,proven-native.sha256} . + revision=$(cat proven.pin) + [[ $revision =~ ^[0-9a-f]{40}$ ]] || fail 'expected a full Proven commit pin' + for source in "${sources[@]}"; do + curl --proto '=https' --proto-redir '=https' -fL --retry 2 --connect-timeout 20 --max-time 120 \ + "https://raw.githubusercontent.com/hyperpolymath/proven/$revision/ffi/zig/src/refc/$source" -o "$source" + done + check_sources + cp -- "$fixtures/"{bridge,observer}.zig "$theory/"{ScalarSource,CanonicalWord,WordSource,NativeWord}.v . + cp -- "$root/scripts/atoma-native-word.sh" gate.sh + source_capsule > AtomaWord.v + for module in ScalarSource CanonicalWord WordSource AtomaWord NativeWord; do compile_coq "$module"; done + [[ $(grep -cx 'Closed under the global context' AtomaWord.log) -eq 2 ]] || fail 'source assumptions audit failed' + sha256sum -- proven.pin proven-native.sha256 "${sources[@]}" {bridge,observer}.zig gate.sh \ + {ScalarSource,CanonicalWord,WordSource,AtomaWord,NativeWord}.{v,vo,log} > source-inputs.sha256 + + zig_build observer-build.log build-obj observer.zig "${flags[@]}" -femit-bin=observer.o + inspect_elf observer.o observer-elf.txt + awk -v sym="$symbol" ' + $8 == sym && $5 == "GLOBAL" { total++; if ($7 == "UND") undefined++ } + $8 == "_start" && $4 == "FUNC" && $5 == "GLOBAL" && $7 != "UND" { start++ } + END { exit !(total == 1 && undefined == 1 && start == 1) } + ' observer-elf.txt || fail 'observer must define _start and import the bridge symbol' + build_and_link bridge.zig native + sha256sum native-observer > executable.sha256 + observe native-observer trace.bin + { trace_prelude; trace_source trace.bin; } > TraceData.v + check_trace_encoding + model_capsule > AtomaNativeWord.v + control_receipt + sha256sum trace.bin trace.bin.stderr {TraceData,AtomaNativeWord,NegativeControls}.v > trace-inputs.sha256 + + for module in TraceData AtomaNativeWord NegativeControls; do compile_coq "$module"; done + [[ $(grep -cx 'Closed under the global context' AtomaNativeWord.log) -eq 2 ]] || + fail 'native assumptions audit failed' + ! grep -q '^Axioms:' AtomaNativeWord.log || fail 'axioms in native theorem closure' + kernel_check > coqchk.log 2>&1 || { cat coqchk.log >&2; fail 'kernel check failed'; } + sha256sum --strict --quiet -c source-inputs.sha256 native-link.sha256 mixed-link.sha256 \ + executable.sha256 trace-inputs.sha256 || fail 'inputs changed during execution or proof' + [[ $(digest "$zig") == "$(awk '$2 == "zig" {print $1}' tool-digests.txt)" ]] || + fail 'Zig executable changed during the check' + cat source-inputs.sha256 native-link.sha256 mixed-link.sha256 executable.sha256 trace-inputs.sha256 > inputs.sha256 + sha256sum -- {source-inputs,native-link,mixed-link,executable,trace-inputs}.sha256 \ + host.txt zig-version.txt coq-version.txt readelf-version.txt tool-digests.txt build-commands.txt \ + observer-{build.log,elf.txt} {native,mixed}-{bridge.log,bridge-elf.txt,link.log,observer-elf.txt} \ + mixed-source/* mixed-observer controls/* {TraceData,AtomaNativeWord,NegativeControls}.{log,vo} \ + coqchk.log >> inputs.sha256 + sha256sum --strict --quiet -c inputs.sha256 || fail 'inputs changed before receipt' + receipt > receipt.json.tmp + mv -- receipt.json.tmp receipt.json + echo 'NATIVE-WORD-CHECKED: kernel-checked 14,849 ordered observations; INCOMPLETE' +) + +replay() ( + local scratch + cd "$1" + [[ $(uname -sm) == 'Linux x86_64' ]] || fail 'Linux x86-64 execution is required' + sha256sum --strict --quiet -c inputs.sha256 || fail 'archive digest mismatch' + check_sources + cmp -s -- gate.sh "$root/scripts/atoma-native-word.sh" && + cmp -s -- bridge.zig "$fixtures/bridge.zig" && + cmp -s -- observer.zig "$fixtures/observer.zig" || + fail 'archive requires the matching Atoma checkout' + source_capsule | cmp -s -- AtomaWord.v - || fail 'archived source capsule differs from original sources' + { trace_prelude; trace_source trace.bin; } | cmp -s -- TraceData.v - || fail 'archived Coq data differs from raw trace' + check_trace_encoding + model_capsule | cmp -s -- AtomaNativeWord.v - || fail 'archived native capsule changed' + receipt | cmp -s -- receipt.json - || fail 'archive receipt mismatch' + kernel_check || fail 'archived kernel check failed' + scratch=$(mktemp -d) + trap 'rm -f -- "$scratch/native-observer" "$scratch/trace.bin" "$scratch/trace.bin.stderr"; rmdir -- "$scratch"' EXIT + cp -- native-observer "$scratch/" + cmp -s -- native-observer "$scratch/native-observer" || fail 'replay executable copy changed' + chmod u+x "$scratch/native-observer" + (cd "$scratch"; observe native-observer trace.bin) + cmp -s -- trace.bin "$scratch/trace.bin" || fail 'archived executable produced a different trace' + sha256sum --strict --quiet -c inputs.sha256 || fail 'archive changed during replay' + echo 'NATIVE-WORD-REPLAYED: archived executable reproduced all 14,849 observations; INCOMPLETE' +) + +[[ $# == 2 ]] || fail 'usage: atoma-native-word.sh {check NEW_DIRECTORY|replay TRUSTED_DIRECTORY}' +coqchk=$(tool "${COQCHK:-coqchk}") +case "$1" in + check) + zig=$(tool "${ZIG:-zig}") + elf=$(tool readelf) + coqc=$(tool "${COQC:-coqc}") + check "$2" + ;; + replay) replay "$2" ;; + *) fail 'expected check or replay' ;; +esac diff --git a/tests/consumer-word/proven-native.sha256 b/tests/consumer-word/proven-native.sha256 new file mode 100644 index 0000000..00fa1ed --- /dev/null +++ b/tests/consumer-word/proven-native.sha256 @@ -0,0 +1,2 @@ +e626b881e9a27b283a971675a15afcc6a70087b7a903abd49a479cd0f2a5019f bits8_decode_word.zig +8aeaf7268fd143e3524eb3d54e0a42a7021e5067d4dc55a431f446630559a335 bits8_word.zig diff --git a/verification/atoma/consumer/coq/NativeWord.v b/verification/atoma/consumer/coq/NativeWord.v new file mode 100644 index 0000000..583411c --- /dev/null +++ b/verification/atoma/consumer/coq/NativeWord.v @@ -0,0 +1,90 @@ +(* SPDX-License-Identifier: MPL-2.0 *) +(* Copyright (c) 2026 Jonathan D.A. Jewell *) +From Coq Require Import Bool List NArith. +Require Import CanonicalWord WordSource. +Import ListNotations. +Open Scope N_scope. + +Definition row := (N * N)%type. + +Definition wire_result (result : option N) : N := + match result with Some byte => byte | None => 256 end. + +Definition expected_row (raw : N) : row := + (raw, wire_result (CanonicalWord.decode raw)). + +Definition flip_mask (bit : nat) : N := N.shiftl 1 (N.of_nat bit). + +Definition cases (byte : nat) : list N := + let raw := CanonicalWord.encode (N.of_nat byte) in + let single_bit := map (fun bit => N.lxor raw (flip_mask bit)) + (seq 0 32 ++ seq 40 24) in + raw :: single_bit ++ [N.lxor raw (N.lor (flip_mask 1) (flip_mask 40))]. + +Definition corpus : list (list N) := + map cases (seq 0 256) ++ [[18446744073709551615]]. + +Definition check_row (raw : N) (actual : row) : bool := + let '(input, result) := actual in + (input =? raw) && (result =? wire_result (CanonicalWord.decode raw)). + +Fixpoint check_group (inputs : list N) (actual : list row) : bool := + match inputs, actual with + | [], [] => true + | raw :: inputs', current :: actual' => + check_row raw current && check_group inputs' actual' + | _, _ => false + end. + +Fixpoint check_groups (inputs : list (list N)) (actual : list (list row)) : bool := + match inputs, actual with + | [], [] => true + | group :: inputs', observed :: actual' => + check_group group observed && check_groups inputs' actual' + | _, _ => false + end. + +Definition check (actual : list (list row)) : bool := check_groups corpus actual. + +Lemma check_group_sound : forall inputs actual, + check_group inputs actual = true -> actual = map expected_row inputs. +Proof. + induction inputs as [|raw inputs IH]; intros [|[input result] actual] H; + try discriminate. + - reflexivity. + - cbn [check_group check_row] in H. + apply andb_true_iff in H as [Hrow Hrest]. + apply andb_true_iff in Hrow as [Hinput Hresult]. + apply N.eqb_eq in Hinput. apply N.eqb_eq in Hresult. + subst input result. cbn. now rewrite (IH actual Hrest). +Qed. + +Lemma check_groups_sound : forall inputs actual, + check_groups inputs actual = true -> actual = map (map expected_row) inputs. +Proof. + induction inputs as [|group inputs IH]; intros [|current actual] H; + try discriminate. + - reflexivity. + - cbn [check_groups] in H. apply andb_true_iff in H as [Hgroup Hrest]. + cbn. now rewrite (check_group_sound group current Hgroup), (IH actual Hrest). +Qed. + +Theorem check_sound : forall actual, + check actual = true -> actual = map (map expected_row) corpus. +Proof. apply check_groups_sound. Qed. + +Theorem check_source : forall source encoder actual raw result, + WordSource.check source encoder = true -> + check actual = true -> + In (raw, result) (concat actual) -> + WordSource.run source encoder raw = Some (CanonicalWord.decode raw) /\ + result = wire_result (CanonicalWord.decode raw). +Proof. + intros source encoder actual raw result Hsource Hchecked Hin. + rewrite (check_sound actual Hchecked) in Hin. + apply in_concat in Hin as [group [Hgroup Hin]]. + apply in_map_iff in Hgroup as [inputs [<- _]]. + apply in_map_iff in Hin as [input [E _]]. + unfold expected_row in E. inversion E; subst. + split; [now apply WordSource.source_corresponds | reflexivity]. +Qed.