Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/decisions/ATOMA-BETA-SCALAR.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: CC-BY-SA-4.0
= First beta slice for new Zig source

Atoma's pilot alpha qualifies pinned Proven and UAA profiles. The first beta
slice exercises the ordinary source checker on a function written after those
profiles. It admits nested wrapping arithmetic by inspecting Zig syntax, so
admission does not depend on recognising the function name or its body text.

[source,zig]
----
/// @ensures result == value +% 5
export fn advance_five(value: u8) callconv(.c) u8 {
return (value +% 253) +% 8;
}
----

Keep a closed module with one exported function, one named `u8` parameter and
one return. Lower the returned Zig AST expression into a bounded typed tree.
Admit the declared parameter, literals in the byte range, grouping, and
wrapping addition with a byte-typed operand. Track source spans for diagnostics.
Refuse imports, calls, extra declarations, other operators, and expressions
that would require rules for untyped comptime arithmetic. The separate
`@ensures` directive still describes the result as the input plus one byte
literal modulo 256.

The evaluator traverses the body for all 256 inputs. SMT and Coq obligations
compare the lowered body with the independently parsed contract. ECHIDNA
compiles the generated theorem, coqchk checks the compiled library, and the
replay audit restates the expected theorem from the source. A separately
compiled native witness compares the actual exported function with widened
arithmetic over the same domain.

Changing only the outer constant from eight to seven must expose input zero
with actual result four and expected result five. A nested ordinary addition
must produce a source diagnostic. Replay must reject reused proof text after a
source edit. The native evidence records the compiler, target and build mode.
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.
4 changes: 4 additions & 0 deletions docs/decisions/ATOMA-SOURCE-TO-PROOF.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ cycles; imports, aliases and higher-order calls are not generally resolved.

== Next semantic work

The first beta extension is scoped in
link:ATOMA-BETA-SCALAR.adoc[the new Zig source slice]. It exercises a nested
expression under the same closed byte contract and evidence boundary.

The R1 finite-model replay slice adds `source-coq` and
`scripts/atoma-replay.sh`: actual ECHIDNA compilation, independent `coqchk`,
and a closed-assumptions audit, with exact regenerated-proof/source binding.
Expand Down
6 changes: 6 additions & 0 deletions examples/atoma/source-u8/advance-five-unsupported.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
/// @ensures result == value +% 5
export fn advance_five(value: u8) callconv(.c) u8 {
return (value + 253) +% 8;
}
12 changes: 12 additions & 0 deletions examples/atoma/source-u8/advance-five-witness.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
const std = @import("std");
extern fn advance_five(value: u8) callconv(.c) u8;

test "compiled advance_five matches the widened oracle over all 256 inputs" {
for (0..256) |i| {
const value: u8 = @intCast(i);
const expected: u16 = (@as(u16, value) + 5) % 256;
try std.testing.expectEqual(expected, @as(u16, advance_five(value)));
}
}
6 changes: 6 additions & 0 deletions examples/atoma/source-u8/advance-five-wrong-body.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
/// @ensures result == value +% 5
export fn advance_five(value: u8) callconv(.c) u8 {
return (value +% 253) +% 7;
}
6 changes: 6 additions & 0 deletions examples/atoma/source-u8/advance-five.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
/// @ensures result == value +% 5
export fn advance_five(value: u8) callconv(.c) u8 {
return (value +% 253) +% 8;
}
43 changes: 43 additions & 0 deletions scripts/atoma-gate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,49 @@ if "$ZIG" test "$pilot/compiled-witness.zig" "$evidence/wrong-body.o" > "$eviden
fi
grep -q 'TestExpectedEqual' "$evidence/compiled-bad.txt"

# Compositional scalar body with independent contract and native oracle.
beta="$evidence/beta-scalar"
mkdir -p "$beta"
positive="$pilot/advance-five.zig"
mutant="$pilot/advance-five-wrong-body.zig"
unsupported="$pilot/advance-five-unsupported.zig"
witness="$pilot/advance-five-witness.zig"
sed 's/+% 8;/+% 7;/' "$positive" | cmp - "$mutant"
sed 's/value +% 253/value + 253/' "$positive" | cmp - "$unsupported"
"$atoma" source-check "$positive" > "$beta/source-good.txt"
grep -q '^SOURCE-PILOT-CHECKED inputs=256 ' "$beta/source-good.txt"
status=0
"$atoma" source-check "$mutant" > "$beta/source-counterexample.txt" || status=$?
[[ $status == 1 ]]
grep -qx 'SOURCE-COUNTEREXAMPLE input=0 actual=4 expected=5' "$beta/source-counterexample.txt"
status=0
"$atoma" source-vc "$unsupported" > "$beta/source-unsupported.txt" || status=$?
[[ $status == 1 ]]
grep -Eq '^SOURCE-REFUSED .*UnsupportedSource at 5:19 \(byte [0-9]+\): unsupported return expression' "$beta/source-unsupported.txt"
"$atoma" source-vc "$positive" > "$beta/source-good.smt2"
"$atoma" source-vc "$mutant" > "$beta/source-bad.smt2"
beta_good=$("$Z3" -T:15 -smt2 "$beta/source-good.smt2")
beta_bad=$("$Z3" -T:15 -smt2 "$beta/source-bad.smt2")
printf 'advance-five %s\nsource-only mutant %s\n' "$beta_good" "$beta_bad" > "$beta/source-verdicts.txt"
[[ $beta_good == unsat && $beta_bad == sat ]]

native_flags=(-target native -O Debug)
{
printf 'target=native\nmode=Debug\n'
"$ZIG" env | grep -E '^[[:space:]]*\.target = '
"$ZIG" version
sha256sum "$(realpath "$(command -v "$ZIG")")"
} > "$beta/native-profile.txt"
"$ZIG" build-obj "$positive" "${native_flags[@]}" -femit-bin="$beta/advance-five.o"
"$ZIG" test "$witness" "$beta/advance-five.o" "${native_flags[@]}" > "$beta/compiled-good.txt" 2>&1
"$ZIG" build-obj "$mutant" "${native_flags[@]}" -femit-bin="$beta/advance-five-mutant.o"
if "$ZIG" test "$witness" "$beta/advance-five-mutant.o" "${native_flags[@]}" > "$beta/compiled-bad.txt" 2>&1; then
echo 'FAIL advance-five mutant survived exhaustive native witness'; exit 1
fi
grep -Fq 'expected 5, found 4' "$beta/compiled-bad.txt"
grep -q 'TestExpectedEqual' "$beta/compiled-bad.txt"
sha256sum "$positive" "$mutant" "$unsupported" "$witness" "$beta"/{native-profile.txt,advance-five.o,advance-five-mutant.o,compiled-good.txt,compiled-bad.txt} > "$beta/native-inputs.sha256"

bash scripts/atoma-gate-falsifiers.sh | tee "$evidence/falsifiers.txt"
bash scripts/atoma-replay-gate.sh | tee "$evidence/replay.txt"
bash scripts/atoma-consumer-scalar-gate.sh | tee "$evidence/scalar-consumers.txt"
Expand Down
10 changes: 9 additions & 1 deletion scripts/atoma-replay-gate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ reject() {
}
"$ATOMA" source-coq "$pilot/wrong-body.zig" > "$work/wrong.v"
reject wrong-body 'ECHIDNA did not verify' bash "$replay" "$pilot/wrong-body.zig" "$work/wrong.v" "$work/wrong-body"
"$ATOMA" source-coq "$pilot/advance-five.zig" > "$work/advance-five.v"
bash "$replay" "$pilot/advance-five.zig" "$work/advance-five.v" "$work/advance-five"
(cd "$work/advance-five" && sha256sum -c inputs.sha256)
reject advance-five-stale 'proof does not match current source' \
bash "$replay" "$pilot/advance-five-wrong-body.zig" "$work/advance-five.v" "$work/advance-five-stale"
"$ATOMA" source-coq "$pilot/advance-five-wrong-body.zig" > "$work/advance-five-wrong.v"
reject advance-five-wrong 'ECHIDNA did not verify' \
bash "$replay" "$pilot/advance-five-wrong-body.zig" "$work/advance-five-wrong.v" "$work/advance-five-wrong"
printf '/// @ensures result == x +%% 0\nexport fn constant(x: u8) callconv(.c) u8 { return 0; }\n' > "$work/literal.zig"
"$ATOMA" source-coq "$work/literal.zig" > "$work/literal.v"
reject literal 'ECHIDNA did not verify' bash "$replay" "$work/literal.zig" "$work/literal.v" "$work/literal"
Expand Down Expand Up @@ -83,4 +91,4 @@ printf 'Theorem admitted_control : False. Admitted.\nPrint Assumptions admitted_
grep -q '^Axioms:' assumptions.txt
! grep -q '^Closed under the global context$' assumptions.txt
)
echo 'REPLAY GATE PASS: two source-derived models checked, 11 rejection controls, axiom control; INCOMPLETE'
echo 'REPLAY GATE PASS: three source-derived models checked, 13 rejection controls, axiom control; INCOMPLETE'
8 changes: 5 additions & 3 deletions tools/atoma/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ pub fn main() !void {
print("SOURCE-IO-ERROR {s}: {s}\n", .{ args[2], @errorName(e) });
std.process.exit(3);
};
const p = source.lower(gpa, src) catch |e| {
print("SOURCE-REFUSED {s}: {s}\n", .{ args[2], @errorName(e) });
var diagnostic: source.Diagnostic = .{};
const p = source.lower(gpa, src, &diagnostic) catch |e| {
const loc = diagnostic.location(src);
sourceReport("SOURCE-REFUSED {s}: {s} at {d}:{d} (byte {d}): {s}\n", .{ args[2], @errorName(e), loc.line, loc.column, diagnostic.offset, diagnostic.reason });
std.process.exit(switch (e) {
error.ParseError, error.OutOfMemory => @as(u8, 3),
else => @as(u8, 1),
Expand All @@ -132,7 +134,7 @@ pub fn main() !void {
std.process.exit(3);
};
} else if (p.counterexample()) |x| {
print("SOURCE-COUNTEREXAMPLE input={d} actual={d} expected={d}\n", .{ x, p.body.eval(x), x +% p.ensures_add });
sourceReport("SOURCE-COUNTEREXAMPLE input={d} actual={d} expected={d}\n", .{ x, p.body.eval(x), p.contract.expected(x) });
std.process.exit(1);
} else {
const hash = std.fmt.bytesToHex(p.source_hash, .lower);
Expand Down
Loading
Loading