dalek-ed25519-verified/verification/check-scalar.sh

45 lines
2.9 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Scalar-layer check (Scalar52 arithmetic mod ). Compiles the gen model + the
# proven foundation. add/sub (Range-loop reductions) and the Montgomery mul
# path are in progress — see README. Guarded compiles throughout.
set -uo pipefail
source ~/aeneas-toolchain/env.sh
HERE="$(cd "$(dirname "$0")" && pwd)"
AENEAS_LEAN="$AENEAS_HOME/backends/lean"
GEN=(CurveScalar/TypesExternal CurveScalar/Types CurveScalar/FunsExternal CurveScalar/Funs)
PROOFS=(ScalarDenote ScalarLoop ScalarSubSpec ScalarAddSpec ScalarMulSpec ScalarMontSpec ScalarReduceSpec ScalarFullMulSpec ScalarMain ScalarWideSpec ScalarBytesSpec ScalarUnpackSpec ScalarFromBytesSpec)
echo "=== stub/axiom audit ==="
grep -rnE '^(private |protected |noncomputable )*axiom ' "$HERE"/Proofs/Scalar*.lean 2>/dev/null && { echo "axiom under Proofs/"; exit 1; }
echo " clean"
echo "=== compile (guarded) ==="
cd "$AENEAS_LEAN"
lake env bash -c "
set -uo pipefail
cd '$HERE/gen' && export LEAN_PATH=\"\$LEAN_PATH:\$PWD:$HERE\"
for m in ${GEN[*]}; do echo \" · gen \$m\"; LEAN_TIMEOUT=300 LEAN_MEM_MB=6144 '$HERE/lean-guard' \"\$m.lean\" || exit 1; done
cd '$HERE'
for m in ${PROOFS[*]}; do echo \" · proof \$m\"; LEAN_TIMEOUT=300 LEAN_MEM_MB=4096 '$HERE/lean-guard' \"Proofs/\$m.lean\" || exit 1; done
" || { echo FAIL; exit 1; }
echo "=== Phase 3: axiom audit (kernel-level) ==="
cd "$AENEAS_LEAN"
lake env bash -c "
set -uo pipefail
export LEAN_PATH=\"\$LEAN_PATH:$HERE/gen:$HERE\"
cd '$HERE'
AUD=\$(mktemp '$HERE/.audit-scalar-XXXX.lean')
{ echo 'import Proofs.ScalarMain'; echo 'import Proofs.ScalarWideSpec'; echo 'import Proofs.ScalarUnpackSpec'; echo 'import Proofs.ScalarFromBytesSpec'; echo '#print axioms ScalarProofs.L_val'
scalar layer: prove Scalar52::sub borrow + conditional-add-L carry chains New in Proofs/ScalarSubSpec.lean (all axiom-clean [propext, Classical.choice, Quot.sound], no sorry, no native_decide): - nat_and_mask52 / nat_shift52 / nat_shift63 : 52/63-bit ops -> %,/ - sub_step_arith : isolated per-limb borrow accounting (tiny ℕ context, correction-on-the-left so no truncated subtraction) — the METHOD-4 discipline that keeps 2^260-scale coefficients out of any one certificate - sub_loop_spec : the FULL 5-limb borrow chain of Scalar52::sub, unrolled via loop_step/range_next_*; wrapping_sub, 52-bit mask store, borrow-out bit. This is the loop unroll that blocked the earlier attempt. - csel_step : step-spec for the subtle conditional_select (faithful model) - cond_add_l_zero_spec / cond_add_l_one_spec : BOTH cases of conditional add-of-L, full carry chains; the condition-1 case steps the addend as a clean value so the index_mut write-back matches sub_loop's pattern - sub_telescope / add_telescope : the 2^52i-weighted value telescopes to 2^260, discharged by omega (no kernel-capacity blowup) check-scalar.sh: ScalarSubSpec added to the compile manifest; Phase-3 axiom audit extended to sub_loop_spec + cond_add_l_one_spec (3/3 clean). Full button green. Honest boundary: top-level sub_val_spec (⟦sub a b⟧ = ⟦a⟧-⟦b⟧ in ZMod ℓ) is documented as remaining — every lemma it needs is proven; what's left is the Aeneas binding-arity for destructuring sub_loop_spec's pair-valued multi-existential postcondition inside the do-block, a mechanical not a mathematical gap. No sorry shipped (Invariants H1/H4). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 14:26:24 +00:00
echo '#print axioms ScalarProofs.sub_loop_spec'
Scalar layer complete: Montgomery reduction + full mul proven, scalarImplementation aggregate Phase B - montgomery_reduce (Proofs/ScalarMontSpec.lean + Proofs/ScalarReduceSpec.lean): - mont_key: LFACTOR*L0 + 1 = 214835089243030*2^52 (the -1 inverse identity, norm_num) - mont_cancel: (s + ((s*LFACTOR) % 2^52)*L0) % 2^52 = 0 via Nat.ModEq - every part1 shift is an EXACT division, nothing discarded - part1_spec / part2_spec: per-round helpers (carry*2^52 = sum + p*L0; exact split) - mont_head_telescope (E0-E4) and mont_tail_telescope (E5-E8): linear_combination certificates with weights 2^52k; mont_bound: X' < 2*ell from Z < 2^260*ell - montgomery_reduce_spec: post `scDenote r * 2^260 = Z` in ZMod ell + 52-bit bounds. METHOD-4 split at the round-4/5 boundary: the 74-step monolith is elaboration-pathological; each half compiles in ~25s/3GB. - The single death-spiral line: an omega for the nonce-sum bound with ~110 hypotheses in context never returns; extracted to nonce_sum_bound (5 hypotheses, instant). Bisected with fail-probes; documented in control-repo FAILURES.md. Phase C - mul (Proofs/ScalarFullMulSpec.lean): - RR_limbs/RR_scVal/RR_lt; RR_denote: RR = 2^520 - K*ell kernel-checked, so ⟦RR⟧ = R^2; R_isUnit: 2^260 unit of ZMod ell (coprime oddness witness) - mul_spec: mul_internal -> montgomery_reduce -> mul_internal(*, RR) -> montgomery_reduce composed; column values folded by `ring`; R cancelled via IsUnit.mul_right_cancel. Hypothesis scVal a * scVal b < 2^260*ell (honest Montgomery bound; canonical inputs satisfy it). Aggregate (Proofs/ScalarMain.lean): scalar_add/sub/mul_correct on ScBnd interfaces + canonical_mul_bound + scalarImplementation bundling all three. sub_val_spec/add_val_spec posts strengthened with result-limb 52-bit bounds (the montgomery tail feeds sub's output back into mul_internal). check-scalar.sh: 9 proof files, 10 kernel audits (was 6), all exactly [propext, Classical.choice, Quot.sound]. Button pressed fresh: green.
2026-07-03 19:06:15 +00:00
echo '#print axioms ScalarProofs.cond_add_l_one_spec'; echo '#print axioms ScalarProofs.sub_val_spec'; echo '#print axioms ScalarProofs.add_val_spec'; echo '#print axioms ScalarProofs.mul_internal_spec'
echo '#print axioms ScalarProofs.part1_spec'; echo '#print axioms ScalarProofs.montgomery_reduce_spec'; echo '#print axioms ScalarProofs.mul_spec'; echo '#print axioms ScalarProofs.scalarImplementation'; echo '#print axioms ScalarProofs.montgomery_mul_spec'; echo '#print axioms ScalarProofs.bytes_unpack_spec'; echo '#print axioms ScalarProofs.from_bytes_wide_spec'; } > \"\$AUD\"
OUT=\$(LEAN_TIMEOUT=120 LEAN_MEM_MB=4096 '$HERE/lean-guard' \"\$AUD\" 2>&1)
echo \"\$OUT\"
rm -f \"\$AUD\" \"\${AUD%.lean}.olean\"
scalar layer: prove Scalar52::sub borrow + conditional-add-L carry chains New in Proofs/ScalarSubSpec.lean (all axiom-clean [propext, Classical.choice, Quot.sound], no sorry, no native_decide): - nat_and_mask52 / nat_shift52 / nat_shift63 : 52/63-bit ops -> %,/ - sub_step_arith : isolated per-limb borrow accounting (tiny ℕ context, correction-on-the-left so no truncated subtraction) — the METHOD-4 discipline that keeps 2^260-scale coefficients out of any one certificate - sub_loop_spec : the FULL 5-limb borrow chain of Scalar52::sub, unrolled via loop_step/range_next_*; wrapping_sub, 52-bit mask store, borrow-out bit. This is the loop unroll that blocked the earlier attempt. - csel_step : step-spec for the subtle conditional_select (faithful model) - cond_add_l_zero_spec / cond_add_l_one_spec : BOTH cases of conditional add-of-L, full carry chains; the condition-1 case steps the addend as a clean value so the index_mut write-back matches sub_loop's pattern - sub_telescope / add_telescope : the 2^52i-weighted value telescopes to 2^260, discharged by omega (no kernel-capacity blowup) check-scalar.sh: ScalarSubSpec added to the compile manifest; Phase-3 axiom audit extended to sub_loop_spec + cond_add_l_one_spec (3/3 clean). Full button green. Honest boundary: top-level sub_val_spec (⟦sub a b⟧ = ⟦a⟧-⟦b⟧ in ZMod ℓ) is documented as remaining — every lemma it needs is proven; what's left is the Aeneas binding-arity for destructuring sub_loop_spec's pair-valued multi-existential postcondition inside the do-block, a mechanical not a mathematical gap. No sorry shipped (Invariants H1/H4). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 14:26:24 +00:00
N=\$(echo \"\$OUT\" | grep -cF \"depends on axioms: [propext, Classical.choice, Quot.sound]\" || true)
[ \"\$N\" -eq 13 ] || { echo \"AXIOM AUDIT FAILED: \$N/13 clean\"; exit 1; }
" || { echo FAIL; exit 1; }
echo " L_val axiom-clean"
echo ""
Scalar layer complete: Montgomery reduction + full mul proven, scalarImplementation aggregate Phase B - montgomery_reduce (Proofs/ScalarMontSpec.lean + Proofs/ScalarReduceSpec.lean): - mont_key: LFACTOR*L0 + 1 = 214835089243030*2^52 (the -1 inverse identity, norm_num) - mont_cancel: (s + ((s*LFACTOR) % 2^52)*L0) % 2^52 = 0 via Nat.ModEq - every part1 shift is an EXACT division, nothing discarded - part1_spec / part2_spec: per-round helpers (carry*2^52 = sum + p*L0; exact split) - mont_head_telescope (E0-E4) and mont_tail_telescope (E5-E8): linear_combination certificates with weights 2^52k; mont_bound: X' < 2*ell from Z < 2^260*ell - montgomery_reduce_spec: post `scDenote r * 2^260 = Z` in ZMod ell + 52-bit bounds. METHOD-4 split at the round-4/5 boundary: the 74-step monolith is elaboration-pathological; each half compiles in ~25s/3GB. - The single death-spiral line: an omega for the nonce-sum bound with ~110 hypotheses in context never returns; extracted to nonce_sum_bound (5 hypotheses, instant). Bisected with fail-probes; documented in control-repo FAILURES.md. Phase C - mul (Proofs/ScalarFullMulSpec.lean): - RR_limbs/RR_scVal/RR_lt; RR_denote: RR = 2^520 - K*ell kernel-checked, so ⟦RR⟧ = R^2; R_isUnit: 2^260 unit of ZMod ell (coprime oddness witness) - mul_spec: mul_internal -> montgomery_reduce -> mul_internal(*, RR) -> montgomery_reduce composed; column values folded by `ring`; R cancelled via IsUnit.mul_right_cancel. Hypothesis scVal a * scVal b < 2^260*ell (honest Montgomery bound; canonical inputs satisfy it). Aggregate (Proofs/ScalarMain.lean): scalar_add/sub/mul_correct on ScBnd interfaces + canonical_mul_bound + scalarImplementation bundling all three. sub_val_spec/add_val_spec posts strengthened with result-limb 52-bit bounds (the montgomery tail feeds sub's output back into mul_internal). check-scalar.sh: 9 proof files, 10 kernel audits (was 6), all exactly [propext, Classical.choice, Quot.sound]. Button pressed fresh: green.
2026-07-03 19:06:15 +00:00
echo "SCALAR LAYER COMPLETE: add, sub, mul (Montgomery reduction, double round through RR) proven mod ; aggregate certificate scalarImplementation kernel-audited."