From 94d768d1e3f69010a6e7228588ce9d3e975aa3eb Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 30 Nov 2026 03:52:35 +0000 Subject: [PATCH 1/2] Validate compiled byte models against an independent Zig source interpretation --- .github/workflows/atoma.yml | 31 ++- docs/decisions/ATOMA-BETA-SCALAR.adoc | 4 + docs/decisions/ATOMA-BYTE-TRANSLATION.adoc | 42 +++ scripts/atoma-translation-gate.sh | 133 ++++++++++ scripts/atoma-translation-validate.sh | 59 +++++ verification/atoma/consumer/coq/U8Source.v | 244 ++++++++++++++++++ .../atoma/consumer/coq/U8Translation.v | 26 ++ 7 files changed, 537 insertions(+), 2 deletions(-) create mode 100644 docs/decisions/ATOMA-BYTE-TRANSLATION.adoc create mode 100644 scripts/atoma-translation-gate.sh create mode 100644 scripts/atoma-translation-validate.sh create mode 100644 verification/atoma/consumer/coq/U8Source.v create mode 100644 verification/atoma/consumer/coq/U8Translation.v diff --git a/.github/workflows/atoma.yml b/.github/workflows/atoma.yml index 76c7d6f..9962460 100644 --- a/.github/workflows/atoma.yml +++ b/.github/workflows/atoma.yml @@ -203,20 +203,47 @@ jobs: if-no-files-found: error retention-days: 30 + byte_translation: + name: Byte source translation validation + runs-on: ubuntu-24.04 + timeout-minutes: 25 + steps: + - uses: actions/checkout@v7.0.1 + with: + persist-credentials: false + - name: Provision pinned Zig + run: bash scripts/atoma-download-zig.sh "$RUNNER_TEMP/atoma-translation-zig" + - name: Provision pinned replay tools + run: bash scripts/atoma-ci-replay-toolchain.sh + - name: Build the actual Atoma emitter + working-directory: tools/atoma + run: zig build + - name: Validate compiled models against the original source bytes + run: bash scripts/atoma-translation-gate.sh + - uses: actions/upload-artifact@v7.0.2 + if: always() + with: + name: byte-translation-evidence-${{ github.sha }} + path: build/atoma-evidence/translation.*/ + if-no-files-found: error + retention-days: 30 + soundness: name: Atoma soundness gate - needs: [core_soundness, heap_consumers] + needs: [core_soundness, heap_consumers, byte_translation] if: ${{ always() }} runs-on: ubuntu-24.04 timeout-minutes: 2 steps: - - name: Require both existing and heap-boundary qualification + - name: Require existing replay, heap pilots and byte translation validation env: CORE_RESULT: ${{ needs.core_soundness.result }} HEAP_RESULT: ${{ needs.heap_consumers.result }} + TRANSLATION_RESULT: ${{ needs.byte_translation.result }} run: | test "$CORE_RESULT" = success test "$HEAP_RESULT" = success + test "$TRANSLATION_RESULT" = success gate: name: CI/gate diff --git a/docs/decisions/ATOMA-BETA-SCALAR.adoc b/docs/decisions/ATOMA-BETA-SCALAR.adoc index dca5de9..419d07e 100644 --- a/docs/decisions/ATOMA-BETA-SCALAR.adoc +++ b/docs/decisions/ATOMA-BETA-SCALAR.adoc @@ -38,3 +38,7 @@ The model replay remains target independent and its receipt remains INCOMPLETE. The Zig frontend, lowering and compiler are still trusted for correspondence with native execution. Proven and UAA retain their alpha proof and consumer gates. + +The follow-on link:ATOMA-BYTE-TRANSLATION.adoc[translation validation profile] +adds an independent check of the closed source and contract against the +compiled model. diff --git a/docs/decisions/ATOMA-BYTE-TRANSLATION.adoc b/docs/decisions/ATOMA-BYTE-TRANSLATION.adoc new file mode 100644 index 0000000..0fd6550 --- /dev/null +++ b/docs/decisions/ATOMA-BYTE-TRANSLATION.adoc @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: CC-BY-SA-4.0 += Independent translation validation for the byte source profile + +The existing source replay audits a theorem whose statement is regenerated by +Atoma's emitter. A mistake shared by the emitter and the audit can survive +that check. The byte translation gate adds an independent interpretation of +the original source and contract bytes. It compares that interpretation with +the actual compiled model used by replay. + +The new checker accepts a deliberately small subset of the experimental byte +profile. It reads a closed module with one exported function, a named byte +argument, C calling convention, one byte return expression, and an explicit +`@ensures` clause. Its independent grammar handles ASCII source, decimal byte +literals, variable references, grouping and wrapping addition. Unsupported +syntax is refused before a correspondence theorem can be issued. + +For all inputs from 0 to 255, the compiled artifact's body must agree with the +raw-source evaluator and its specification must agree with the independently +parsed contract. The two comparisons are separate so an error in either the +body or contract translation can be diagnosed. The raw bytes and compiled +library contribute to the evidence identity. Coq checks the theorem and +coqchk verifies the libraries on the hosted runner. + +The negative controls deliberately supply self-consistent compiled models. +One mistranslates a changed body while keeping the original contract. Another +mistranslates a changed contract while leaving the body alone. The semantic +comparisons must reject those models even though their internal model theorem +checks. A source edit also invalidates saved byte evidence, and unsupported +arithmetic must fail admission. + +The checker admits at most 2,048 source bytes, including comments. Names use +letters and underscores within the profile, byte literals are decimal with +no leading zero, and the expression parser bounds grouping and addition +chains. A renamed function and a comment-only edit qualify independently. +The comment edit requires a new receipt because its byte identity changed. + +This is translation validation for each checked artifact against the +independent byte semantics. The native witness from the first beta slice +still supplies execution evidence for the pinned Zig compiler and target. +General Zig parsing, compiler and native correspondence, wider types, +imports, memory and effects remain open. Receipts remain `INCOMPLETE`. +The required workflow retains the Proven and UAA source and native campaigns. diff --git a/scripts/atoma-translation-gate.sh b/scripts/atoma-translation-gate.sh new file mode 100644 index 0000000..5051bb4 --- /dev/null +++ b/scripts/atoma-translation-gate.sh @@ -0,0 +1,133 @@ +#!/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) +export ATOMA=${ATOMA:-$root/tools/atoma/zig-out/bin/atoma} +export COQC=${COQC:-coqc} COQCHK=${COQCHK:-coqchk} +replay="$root/scripts/atoma-replay.sh" +validate="$root/scripts/atoma-translation-validate.sh" +pilot="$root/examples/atoma/source-u8" +mkdir -p "$root/build/atoma-evidence" +work=$(mktemp -d "$root/build/atoma-evidence/translation.XXXXXX") +echo "Translation evidence directory: $work" + +check_translation() { + local source=$1 case_dir="$work/$2" + mkdir "$case_dir" + "$ATOMA" source-coq "$source" > "$case_dir/generated.v" + bash "$replay" "$source" "$case_dir/generated.v" "$case_dir/replay" + bash "$validate" "$case_dir/replay/source.zig" "$case_dir/replay/AtomaProof.vo" "$case_dir/translation" + (cd "$case_dir/replay" && sha256sum -c inputs.sha256) + (cd "$case_dir/translation" && sha256sum -c inputs.sha256) +} + +check_translation "$pilot/advance-five.zig" advance-five +cat > "$work/nudge.zig" <<'ZIG' +/// @ensures result == sample +% 7 +export fn nudge_byte(sample: u8) callconv(.c) u8 { + return (7) +% (sample +% 247) +% 9; +} +ZIG +check_translation "$work/nudge.zig" nudge +cp "$work/nudge.zig" "$work/comment-change.zig" +printf '%s\n' '// Equivalent source with different bytes.' >> "$work/comment-change.zig" +check_translation "$work/comment-change.zig" comment-change +grep ' source\.zig$' "$work/nudge/translation/inputs.sha256" > "$work/comment-change/old-source.sha256" +( + cd "$work/comment-change/translation" + if sha256sum -c ../old-source.sha256 > ../old-source-check.txt 2>&1; then + echo 'FAIL old source digest accepted changed bytes' >&2 + exit 1 + fi + grep -qFx 'source.zig: FAILED' ../old-source-check.txt +) + +# Model a common-mode emitter defect in both legacy generation and legacy +# audit. ECHIDNA, coqc and coqchk remain the genuine tools. +cat > "$work/paired-emitter" <<'SH' +#!/usr/bin/env bash +set -euo pipefail +[[ $# == 2 && -f $2 ]] || exit 2 +case $1 in + source-coq) cat "$CONTROL_MODEL" ;; + source-coq-audit) cat "$CONTROL_AUDIT" ;; + *) exit 2 ;; +esac +SH +chmod +x "$work/paired-emitter" + +expect_refusal() { + local case_dir=$1 source=$2 model=$3 verdict=$4 + local translation="$case_dir/translation" + if bash "$validate" "$source" "$model" "$translation" > "$case_dir/refusal.log" 2>&1; then + echo "FAIL translation accepted $case_dir" >&2 + exit 1 + fi + grep -qFx 'TRANSLATION-REFUSED: source and compiled model did not validate' "$case_dir/refusal.log" \ + || { cat "$case_dir/refusal.log" >&2; return 1; } + [[ ! -e $translation/receipt.json && ! -e $translation/U8Translation.vo ]] + ( + cd "$translation" + cmp -- "$source" source.zig + cmp -- "$model" AtomaProof.vo + sha256sum -c submitted.sha256 + sha256sum -c dependencies.sha256 + cat > Refusal.v < refusal-assumptions.txt 2> refusal-assumptions.stderr \ + || { cat refusal-assumptions.stderr >&2; exit 1; } + [[ $(grep -c '^Closed under the global context$' refusal-assumptions.txt) == 2 ]] + ! grep -q '^Axioms:' refusal-assumptions.txt + timeout -k 5 120 "$COQCHK" -silent -Q . '' ScalarSource U8Source SourceBytes AtomaProof Refusal \ + > refusal-coqchk.txt 2>&1 || { cat refusal-coqchk.txt >&2; exit 1; } + sha256sum -c submitted.sha256 + sha256sum -c dependencies.sha256 + sha256sum source.zig AtomaProof.vo ./*.v ./*.vo assumptions.stderr ../refusal.log \ + refusal-assumptions.txt refusal-coqchk.txt > refusal.sha256 + ) + printf 'TRANSLATION-FALSIFIER-KILLED %s (%s)\n' "$(basename "$case_dir")" "$verdict" +} + +wrong_model_control() { + local source=$1 case_dir="$work/$2" verdict=$3 + local correct="$work/advance-five/replay" + mkdir "$case_dir" + "$ATOMA" source-coq "$source" > "$case_dir/actual-model.v" + "$ATOMA" source-coq-audit "$source" > "$case_dir/actual-audit.v" + # Preserve the true source marker while substituting the self-consistent + # advance-five model and its audit. A digest difference cannot kill it. + { + head -n 1 "$case_dir/actual-model.v" + tail -n +2 "$correct/AtomaProof.v" + } > "$case_dir/wrong-model.v" + { + head -n 1 "$case_dir/actual-audit.v" + tail -n +2 "$correct/Audit.v" + } > "$case_dir/wrong-audit.v" + CONTROL_MODEL="$case_dir/wrong-model.v" CONTROL_AUDIT="$case_dir/wrong-audit.v" \ + ATOMA="$work/paired-emitter" bash "$replay" "$source" "$case_dir/wrong-model.v" "$case_dir/replay" + expect_refusal "$case_dir" "$case_dir/replay/source.zig" "$case_dir/replay/AtomaProof.vo" "$verdict" + (cd "$case_dir/replay" && sha256sum -c inputs.sha256) +} + +wrong_model_control "$pilot/advance-five-wrong-body.zig" wrong-body 'Some (false, true)' +cat > "$work/wrong-contract.zig" <<'ZIG' +/// @ensures result == value +% 4 +export fn advance_five(value: u8) callconv(.c) u8 { + return (value +% 253) +% 8; +} +ZIG +wrong_model_control "$work/wrong-contract.zig" wrong-contract 'Some (true, false)' +mkdir "$work/unsupported" +expect_refusal "$work/unsupported" "$pilot/advance-five-unsupported.zig" \ + "$work/advance-five/replay/AtomaProof.vo" 'None' + +echo 'TRANSLATION GATE PASS: validated raw bytes, equivalent edit, and three independent refusals; INCOMPLETE' diff --git a/scripts/atoma-translation-validate.sh b/scripts/atoma-translation-validate.sh new file mode 100644 index 0000000..73a1c1d --- /dev/null +++ b/scripts/atoma-translation-validate.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell +set -euo pipefail +export LC_ALL=C +[[ $# == 3 ]] || { echo 'usage: atoma-translation-validate.sh SOURCE ATOMA_PROOF_VO NEW_EVIDENCE_DIRECTORY' >&2; exit 2; } +root=$(cd "$(dirname "$0")/.." && pwd) +coqc=$(command -v "${COQC:-coqc}") || { echo 'TRANSLATION-REFUSED: missing compiler' >&2; exit 1; } +coqchk=$(command -v "${COQCHK:-coqchk}") || { echo 'TRANSLATION-REFUSED: missing kernel checker' >&2; exit 1; } +coqc=$(realpath "$coqc") +coqchk=$(realpath "$coqchk") +fail() { echo "TRANSLATION-REFUSED: $*" >&2; exit 1; } +[[ -f $1 && -r $1 && -s $1 && $(wc -c < "$1") -le 2048 ]] || fail 'source must be a readable regular file of 1..2048 bytes' +[[ -f $2 && -r $2 && -s $2 ]] || fail 'compiled model must be a readable regular file' +source=$(realpath -- "$1") +model=$(realpath -- "$2") +out=$(realpath -m -- "$3") +mkdir -- "$out" || fail 'evidence directory must be new' +cp -- "$source" "$out/source.zig" +cp -- "$model" "$out/AtomaProof.vo" +for library in ScalarSource U8Source U8Translation; do + cp "$root/verification/atoma/consumer/coq/$library.v" "$out/" +done +cd "$out" +[[ -s source.zig && $(wc -c < source.zig) -le 2048 ]] || fail 'source snapshot is outside the byte limit' +{ + printf '%s\n' 'From Coq Require Import List.' 'Import ListNotations.' 'Definition bytes : list nat := [' + od -An -v -tu1 -- source.zig | awk '{for (i=1; i<=NF; i++) {if (n++) printf "; "; printf "%s", $i} print ""}' + printf '%s\n' '].' +} > SourceBytes.v +sha256sum source.zig AtomaProof.vo SourceBytes.v ScalarSource.v U8Source.v U8Translation.v > submitted.sha256 +for library in ScalarSource U8Source SourceBytes; do + timeout -k 5 120 "$coqc" -q -Q . '' "$library.v" > "$library.log" 2>&1 \ + || { cat "$library.log" >&2; fail "could not compile $library"; } +done +sha256sum ScalarSource.vo U8Source.vo SourceBytes.vo > dependencies.sha256 +timeout -k 5 120 "$coqc" -q -Q . '' U8Translation.v > assumptions.txt 2> assumptions.stderr \ + || { cat assumptions.stderr >&2; fail 'source and compiled model did not validate'; } +[[ -s U8Translation.vo ]] || fail 'missing compiled translation capsule' +[[ $(grep -c '^Closed under the global context$' assumptions.txt) == 2 ]] || fail 'translation theorems are not closed' +! grep -q '^Axioms:' assumptions.txt || fail 'axioms in translation theorem closure' +timeout -k 5 120 "$coqchk" -silent -Q . '' ScalarSource U8Source SourceBytes AtomaProof U8Translation \ + > coqchk.txt 2>&1 || { cat coqchk.txt >&2; fail 'compiled translation kernel check failed'; } +sha256sum -c submitted.sha256 > submitted-recheck.txt || fail 'source, model or checker changed' +sha256sum -c dependencies.sha256 > dependencies-recheck.txt || fail 'compiled checker dependencies changed' +cmp -s -- "$source" source.zig || fail 'source changed during validation' +cmp -s -- "$model" AtomaProof.vo || fail 'model changed during validation' +sha256sum source.zig AtomaProof.vo ./*.v ScalarSource.vo U8Source.vo SourceBytes.vo U8Translation.vo \ + assumptions.txt assumptions.stderr coqchk.txt > inputs.sha256 +sha256sum "$coqc" "$coqchk" > tools.sha256 +source_digest=$(sha256sum source.zig | cut -d ' ' -f 1) +model_digest=$(sha256sum AtomaProof.vo | cut -d ' ' -f 1) +jq -cnS --arg source "$source_digest" --arg model "$model_digest" \ + '{schema:"atoma.translation.u8/1",status:"INCOMPLETE",inputs_checked:256, + scope:"independent byte-profile body and postcondition agree with the compiled Coq model", + source_sha256:$source,model_sha256:$model,coqchk:"checked",assumptions:[], + zig_compiler_refinement:"unproved"}' > receipt.json.tmp +mv receipt.json.tmp receipt.json +echo 'TRANSLATION-CHECKED: 256 inputs; INCOMPLETE, Zig compiler refinement unproved' diff --git a/verification/atoma/consumer/coq/U8Source.v b/verification/atoma/consumer/coq/U8Source.v new file mode 100644 index 0000000..0b9c2c1 --- /dev/null +++ b/verification/atoma/consumer/coq/U8Source.v @@ -0,0 +1,244 @@ +(* SPDX-License-Identifier: MPL-2.0 *) +(* SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell *) +(* An independent interpretation of the closed one-export byte profile. *) +From Coq Require Import List String Ascii Arith Bool. +Require ScalarSource. +Import ListNotations. +Open Scope string_scope. +Open Scope nat_scope. + +(* Allow only printable ASCII, tab, LF and CRLF, including inside comments. *) +Fixpoint valid_bytes (bytes : list nat) : bool := + match bytes with + | [] => true + | 13 :: 10 :: rest => valid_bytes rest + | c :: rest => + ((c =? 9) || (c =? 10) || ((32 <=? c) && (c <=? 126))) && + valid_bytes rest + end. + +Inductive token := +| symbol (text : string) +| documentation (line : list nat). + +Fixpoint comment_line (bytes : list nat) : list nat * list nat := + match bytes with + | [] => ([], []) + | 10 :: rest => ([], rest) + | c :: rest => + let '(line, tail) := comment_line rest in (c :: line, tail) + end. + +Definition next_symbol (bytes : list nat) : option (string * list nat) := + match bytes with + | [] => None + | 43 :: 37 :: rest => Some ("+%", rest) + | c :: rest => + if ScalarSource.wordchar c then Some (ScalarSource.take_word bytes) + else if existsb (Nat.eqb c) [40; 41; 46; 58; 59; 123; 125] + then Some (String (ascii_of_nat c) EmptyString, rest) + else None + end. + +Fixpoint lex (fuel : nat) (bytes : list nat) : option (list token) := + match fuel, bytes with + | _, [] => Some [] + | 0, _ => None + | S fuel', 47 :: 47 :: 47 :: rest => + let '(line, tail) := comment_line rest in + option_map (cons (documentation line)) (lex fuel' tail) + | S _, 47 :: 47 :: 33 :: _ => None + | S fuel', 47 :: 47 :: rest => + lex fuel' (ScalarSource.after_comment rest) + | S fuel', c :: rest => + if ScalarSource.white c then lex fuel' rest else + match next_symbol bytes with + | Some (text, tail) => option_map (cons (symbol text)) (lex fuel' tail) + | None => None + end + end. + +Fixpoint symbols (tokens : list token) : option (list string) := + match tokens with + | [] => Some [] + | symbol text :: rest => option_map (cons text) (symbols rest) + | documentation _ :: _ => None + end. + +Fixpoint letters (text : string) : bool := + match text with + | EmptyString => true + | String c rest => ScalarSource.letter (nat_of_ascii c) && letters rest + end. + +Definition identifier (text : string) : bool := + (* The restricted alphabet also excludes numeric primitive type names. *) + let reserved := + ["_"; "addrspace"; "align"; "allowzero"; "and"; "anyerror"; "anyframe"; + "anyopaque"; "anytype"; "asm"; "async"; "await"; "bool"; "break"; + "c_char"; "c_int"; "c_long"; "c_longdouble"; "c_longlong"; "c_short"; + "c_uint"; "c_ulong"; "c_ulonglong"; "c_ushort"; "callconv"; "catch"; + "comptime"; "comptime_float"; "comptime_int"; "const"; "continue"; + "defer"; "else"; "enum"; "errdefer"; "error"; "export"; "extern"; + "false"; "fn"; "for"; "if"; "inline"; "isize"; "linksection"; "noalias"; + "noinline"; "noreturn"; "nosuspend"; "null"; "opaque"; "or"; "orelse"; + "packed"; "pub"; "resume"; "return"; "struct"; "suspend"; "switch"; + "test"; "threadlocal"; "true"; "try"; "type"; "undefined"; "union"; + "unreachable"; "usize"; "usingnamespace"; "var"; "void"; "volatile"; + "while"] in + negb (String.eqb text EmptyString) && letters text && + negb (existsb (String.eqb text) reserved). + +Definition byte_literal (text : string) : option nat := + let leading_zero := match text with + | String c (String _ _) => nat_of_ascii c =? 48 + | _ => false + end in + if leading_zero then None else + match ScalarSource.decimal text with + | Some n => if n None + end. + +Fixpoint contract_word (bytes : list nat) : string * list nat := + match bytes with + | [] => (EmptyString, []) + | c :: rest => + if ScalarSource.white c then (EmptyString, bytes) else + let '(word, tail) := contract_word rest in + (String (ascii_of_nat c) word, tail) + end. + +Fixpoint contract_words (fuel : nat) (bytes : list nat) : option (list string) := + match fuel, bytes with + | _, [] => Some [] + | 0, _ => None + | S fuel', c :: rest => + if ScalarSource.white c then contract_words fuel' rest else + let '(word, tail) := contract_word bytes in + option_map (cons word) (contract_words fuel' tail) + end. + +Definition parse_contract (parameter : string) (line : list nat) : option nat := + match contract_words (List.length line) line with + | Some ["@ensures"; "result"; "=="; name; "+%"; literal] => + if String.eqb parameter name then byte_literal literal else None + | _ => None + end. + +Inductive expression := +| parameter_value +| byte_constant (value : nat) +| wrapping_add (left right : expression). + +Definition has_byte_operand (e : expression) : bool := + match e with byte_constant _ => false | _ => true end. + +Definition parse_atom (parameter : string) + (parse_group : list string -> option (expression * list string)) + (tokens : list string) : option (expression * list string) := + match tokens with + | [] => None + | "(" :: rest => + match parse_group rest with + | Some (inner, ")" :: tail) => Some (inner, tail) + | _ => None + end + | word :: rest => + if String.eqb word parameter then Some (parameter_value, rest) else + option_map (fun n => (byte_constant n, rest)) (byte_literal word) + end. + +(* Fold successive additions to the left. A grouped expression is parsed + separately as an atom. A plus requires an already typed byte operand. *) +Fixpoint parse_chain (fuel : nat) + (atom : list string -> option (expression * list string)) + (left : expression) (tokens : list string) : option (expression * list string) := + match tokens with + | "+%" :: rest => + match fuel, atom rest with + | S fuel', Some (right, tail) => + if has_byte_operand left || has_byte_operand right + then parse_chain fuel' atom (wrapping_add left right) tail else None + | _, _ => None + end + | _ => Some (left, tokens) + end. + +Fixpoint parse_expression (depth : nat) (parameter : string) + (tokens : list string) : option (expression * list string) := + match depth with + | 0 => None + | S depth' => + let atom := parse_atom parameter (fun ts => parse_expression depth' parameter ts) in + match atom tokens with + | Some (first, rest) => parse_chain 32 atom first rest + | None => None + end + end. + +Definition parse_module (line : list nat) (tokens : list string) + : option (expression * nat) := + match tokens with + | "export" :: "fn" :: name :: "(" :: parameter :: ":" :: "u8" :: ")" :: + "callconv" :: "(" :: "." :: "c" :: ")" :: "u8" :: "{" :: "return" :: rest => + if identifier name && identifier parameter && negb (String.eqb name parameter) + then match parse_expression 16 parameter rest, parse_contract parameter line with + | Some (e, [";"; "}"]), Some n => Some (e, n) + | _, _ => None + end + else None + | _ => None + end. + +Definition parse (bytes : list nat) : option (expression * nat) := + if (List.length bytes <=? 2048) && valid_bytes bytes then + match lex (List.length bytes) bytes with + | Some (documentation line :: rest) => + match symbols rest with Some ts => parse_module line ts | None => None end + | _ => None + end + else None. + +Fixpoint evaluate (e : expression) (x : nat) : nat := + match e with + | parameter_value => x + | byte_constant n => n + | wrapping_add lhs rhs => (evaluate lhs x + evaluate rhs x) mod 256 + end. + +Definition run (bytes : list nat) (x : nat) : option (nat * nat) := + match parse bytes with + | Some (e, add) => Some (evaluate e x, (x + add) mod 256) + | None => None + end. + +Definition check (bytes : list nat) (body spec : nat -> nat) : option (bool * bool) := + match parse bytes with + | None => None + | Some (e, add) => + let domain := seq 0 256 in + Some (forallb (fun x => Nat.eqb (evaluate e x) (body x)) domain, + forallb (fun x => Nat.eqb ((x + add) mod 256) (spec x)) domain) + end. + +Theorem check_agrees : forall bytes body spec, + check bytes body spec = Some (true, true) -> + forall x, x < 256 -> run bytes x = Some (body x, spec x). +Proof. + intros bytes body spec H x Hx. + unfold check in H. unfold run. + destruct (parse bytes) as [[e add]|]; [|discriminate]. + pose proof (f_equal (fun verdict => + match verdict with Some (b, _) => b | None => false end) H) as Hbody. + pose proof (f_equal (fun verdict => + match verdict with Some (_, s) => s | None => false end) H) as Hspec. + change (forallb (fun i => Nat.eqb (evaluate e i) (body i)) (seq 0 256) = true) in Hbody. + change (forallb (fun i => Nat.eqb ((i + add) mod 256) (spec i)) (seq 0 256) = true) in Hspec. + assert (Hin : In x (seq 0 256)). + { apply in_seq. split; [apply Nat.le_0_l | exact Hx]. } + rewrite forallb_forall in Hbody, Hspec. + specialize (Hbody x Hin). specialize (Hspec x Hin). + apply Nat.eqb_eq in Hbody. apply Nat.eqb_eq in Hspec. + now rewrite Hbody, Hspec. +Qed. diff --git a/verification/atoma/consumer/coq/U8Translation.v b/verification/atoma/consumer/coq/U8Translation.v new file mode 100644 index 0000000..1216369 --- /dev/null +++ b/verification/atoma/consumer/coq/U8Translation.v @@ -0,0 +1,26 @@ +(* SPDX-License-Identifier: MPL-2.0 *) +(* SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell *) +From Coq Require Import Arith. +Require AtomaProof SourceBytes U8Source. + +Lemma evaluated_agreement : + U8Source.check SourceBytes.bytes AtomaProof.body AtomaProof.specification = + Some (true, true). +Proof. vm_compute. reflexivity. Qed. + +Theorem byte_translation_agrees : forall x, x < 256 -> + U8Source.run SourceBytes.bytes x = + Some (AtomaProof.body x, AtomaProof.specification x). +Proof. apply U8Source.check_agrees. exact evaluated_agreement. Qed. + +Theorem interpreted_contract_holds : forall x, x < 256 -> + exists result, U8Source.run SourceBytes.bytes x = Some (result, result). +Proof. + intros x Hx. + rewrite (byte_translation_agrees x Hx). + exists (AtomaProof.specification x). + now rewrite (AtomaProof.atoma_u8 x Hx). +Qed. + +Print Assumptions byte_translation_agrees. +Print Assumptions interpreted_contract_holds. From bb8890057ff136fc9e1007cd57f5a6546e95afd4 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 30 Nov 2026 03:59:12 +0000 Subject: [PATCH 2/2] Avoid Coq constructor names in byte parser patterns --- verification/atoma/consumer/coq/U8Source.v | 12 ++++++------ verification/atoma/consumer/coq/U8Translation.v | 5 ++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/verification/atoma/consumer/coq/U8Source.v b/verification/atoma/consumer/coq/U8Source.v index 0b9c2c1..ca53fe1 100644 --- a/verification/atoma/consumer/coq/U8Source.v +++ b/verification/atoma/consumer/coq/U8Source.v @@ -129,7 +129,7 @@ Definition parse_contract (parameter : string) (line : list nat) : option nat := Inductive expression := | parameter_value | byte_constant (value : nat) -| wrapping_add (left right : expression). +| wrapping_add (lhs rhs : expression). Definition has_byte_operand (e : expression) : bool := match e with byte_constant _ => false | _ => true end. @@ -153,16 +153,16 @@ Definition parse_atom (parameter : string) separately as an atom. A plus requires an already typed byte operand. *) Fixpoint parse_chain (fuel : nat) (atom : list string -> option (expression * list string)) - (left : expression) (tokens : list string) : option (expression * list string) := + (lhs : expression) (tokens : list string) : option (expression * list string) := match tokens with | "+%" :: rest => match fuel, atom rest with - | S fuel', Some (right, tail) => - if has_byte_operand left || has_byte_operand right - then parse_chain fuel' atom (wrapping_add left right) tail else None + | S fuel', Some (rhs, tail) => + if has_byte_operand lhs || has_byte_operand rhs + then parse_chain fuel' atom (wrapping_add lhs rhs) tail else None | _, _ => None end - | _ => Some (left, tokens) + | _ => Some (lhs, tokens) end. Fixpoint parse_expression (depth : nat) (parameter : string) diff --git a/verification/atoma/consumer/coq/U8Translation.v b/verification/atoma/consumer/coq/U8Translation.v index 1216369..50173c3 100644 --- a/verification/atoma/consumer/coq/U8Translation.v +++ b/verification/atoma/consumer/coq/U8Translation.v @@ -11,7 +11,10 @@ Proof. vm_compute. reflexivity. Qed. Theorem byte_translation_agrees : forall x, x < 256 -> U8Source.run SourceBytes.bytes x = Some (AtomaProof.body x, AtomaProof.specification x). -Proof. apply U8Source.check_agrees. exact evaluated_agreement. Qed. +Proof. + exact (@U8Source.check_agrees SourceBytes.bytes AtomaProof.body + AtomaProof.specification evaluated_agreement). +Qed. Theorem interpreted_contract_holds : forall x, x < 256 -> exists result, U8Source.run SourceBytes.bytes x = Some (result, result).