Repository navigation
Use stdlib bits as the word type everywhere (word interface removal, PR 3) - #565
Conversation
c1ad544 to
5be4070
Compare
5be4070 to
a78f121
Compare
|
What is holding up porting the rest of the repository? |
|
Nothing structural; it is the long tail. LiveVerif is at 55 of 57 files: what remains is critbit.v (its Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm). |
|
prioritize end2end, treat LiveVerif as a background task |
|
Done: processor/end2end are now being ported on their own lane against the current tip, and critbit continues in the background. Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm). |
7a1f0ed to
1e6d87b
Compare
| (found = /[1] -> P) -> | ||
| found = /[0] -> | ||
| /[0] = /[1] -> P. | ||
| (/[0] : word) = /[1] -> P. |
There was a problem hiding this comment.
Please find a fix that does not need to add the : word casts at conversion notations. One idea: maybe the notations should be resolved by looking up the BitWidth typeclass?
There was a problem hiding this comment.
Working on it. The port added 116 : word casts (50 at /[..]/\[..] sites) because the notations elaborate to bits.of_Z _ x and nothing fixes the modulus when there is no expected type. Probing your idea now: a notation whose width is found by resolving Bitwidth ?w, so that the elaborated term is still literally bits.of_Z width x (syntactic matching against expr.literal's value is what the rest of the port relies on) and the casts can all go.
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
There was a problem hiding this comment.
Done in 8a50706: the conversion notations now take their width from a hole of a class type WordWidth (a name for Z) that a Hint Extern resolves through the Bitwidth instance in scope when elaboration leaves it open; unification with the surrounding types still fills it first. The hole is the width rather than the modulus so that a plain modulus evar (an implicit of a Zmod operation) cannot absorb it, and _ :> WordWidth leaves no cast node, so the elaborated terms are exactly bits.of_Z width x and Zmod.unsigned x, the same as the semantics produce. All 50 casts at conversion notations are gone, and 31 binder types that only existed to give \[x] its modulus are back to their pre-port untyped form; two compiler binders keep a type because the statement needs it before any notation could fix it. Wrapper definitions and tactic-driven holes were tried first and rejected (a wrapper breaks syntactic matching against expr.literal's value; a tactic hole runs before the expected type is known and lets the instance beat it).
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
| eexists; split; cbv [expr expr_body localsmap get]. | ||
| { rewrite ?Properties.map.get_put_dec. eexists; cbn; split; reflexivity. } | ||
| rewrite !word.unsigned_ltu. | ||
| idtac. |
There was a problem hiding this comment.
why is this idtac here?
There was a problem hiding this comment.
A leftover placeholder from removing rewrite !word.unsigned_ltu. there. Removing it, and sweeping the whole diff for other no-op leftovers of the same kind.
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
There was a problem hiding this comment.
Removed, together with the five other placeholders of the same kind the port had left (LiveRules.v twice, LAN9250.v twice, FlatToRiscvCommon.v once); the remaining idtac lines in the diff are pre-existing or meaningful (intuition idtac, a default match arm, progress (idtac; ...)). No other leftover no-ops found.
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
a6393bd to
c07250b
Compare
| (* A power that is the modulus of a word type is left alone: the word tactics | ||
| match it syntactically. *) |
There was a problem hiding this comment.
Is this really necessary? could the tactics be appropriately generalized instead, either to support non-power-of-two modulus or to recognize an evaluated power of two, whichever is easier?
There was a problem hiding this comment.
Not necessary in principle: it exists only because the word tactics match the modulus syntactically as 2 ^ w, so anything that evaluates it to a literal (here the constant folding in this test file, elsewhere simpl/cbn) takes the goal out of their reach. Generalizing the recognizer to accept an evaluated power of two (a closed literal m with 2 ^ Z.log2 m = m) in ZnWords, unzify, bottom_up_simpl and the LiveVerif side-condition tactics is the easier direction; doing that now, and then removing this guard and the other workarounds of the same origin across the PR (the file-local Arguments Z.pow : simpl never lines, the change 4294967296 with (2 ^ 32) steps and similar).
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
There was a problem hiding this comment.
Done (landing with the next push): one recognizer, fold_pow2_moduli, runs at the entry of ZnWords, the zify tactics, bottom_up_simpl and the LiveVerif steps; it turns any word modulus that has been evaluated to Z.pow_pos 2 p or to a literal m with 2 ^ Z.log2 m = m back into 2 ^ k in that hypothesis or goal (about 6 ms on a 200-hypothesis context). With it, this guard, its twin in PrintSmt.v, 26 of critbit's 36 cbn -[Z.pow] and two other workarounds are removed. What stays, with reasons in the commit message: groundcbv's power-of-two case (folding back after it costs 3.4 s per call in FlatConstMem), one file-local Arguments Z.pow : simpl never in insertionsort.v (a list-arithmetic shape, not a modulus issue), and ten cbn -[Z.pow] in critbit where the next step is a hand-written \[k] or a syntactic destruct/subst on a term that must still occur verbatim before any word tactic runs. Arbitrary moduli would have meant restating every lemma table at Zmod m and giving lia the width separately, so recognizing evaluated powers of two was the easier direction.
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
| (* Kami's word type is indexed by [nwidth], so [Z.of_nat nwidth] is the width | ||
| every word-typed term elaborates to; the instance is stated at that spelling. *) | ||
| #[global] Instance BW: Bitwidth (Z.of_nat nwidth) := {| width_cases := or_introl eq_refl |}. |
There was a problem hiding this comment.
Are you sure we need this instance? And does it really need to be global?
There was a problem hiding this comment.
Measured: needed, but not globally. riscv-coq's MachineWidth (bits ?w) instance is resolved before ?w is unified with the processor's word, so with only coqutil's Bitwidth 32 in scope run1's arithmetic elaborates at 2 ^ 32 while every Kami-side term is at 2 ^ Z.of_nat (Z.to_nat width), and the first MMIO-contradiction lia in KamiRiscvStep.v (and everything after it) fails with the instance deleted. #[export] is also wrong: once activated by Import it wins for Context (spec: ProgramSpec) in End2EndPipeline.v, which then disagrees with the compiler's initial_conditions at Bitwidth 32. So it becomes a plain Definition in KamiProc.v with #[local] Existing Instance in exactly the two files whose statements do that resolution (KamiRiscvStep.v, KamiRiscv.v); Consistency.v and end2end see no instance. Spelling the processor's word as bits 32 instead was tried and is worse: every Kami lemma is keyed on 2 ^ Z.of_nat ?sz, which never unifies with the literal, so each of the ~40 Kami rewrites would need (sz := 32%nat) plus a change at every riscv-side boundary. Landing with the current round.
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
| @@ -1,598 +1,116 @@ | |||
| Require Import Coq.ZArith.ZArith Coq.ZArith.BinIntDef Coq.ZArith.BinInt coqutil.Z.Lia. | |||
| Require Import coqutil.sanity coqutil.Tactics.forward coqutil.Word.Interface. Import word. | |||
| Require Import Coq.ZArith.ZArith. | |||
There was a problem hiding this comment.
It would be nice if we could get rid of this file entirely now that bedrock2 and kami use the same word type. For each lemma, first investigate its usages and see if the lemma could be disposed of. If the lemma is still needed, consider whether there would be a better place to put it -- either at a standard location, to be reusable, or next to the usages.
There was a problem hiding this comment.
Auditing the 13 remaining lemmas by use site now: what is trivial once both sides are bits (Z.of_nat n) gets inlined at the use, what is genuinely about Kami's wordToN/split/combine/weq goes into kami#51's Word.v, and anything used by one file moves next to it, with the goal of deleting the file. The Bitwidth instance question above gets answered by measurement in the same round.
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
There was a problem hiding this comment.
Done: the file is gone. Of its 13 lemmas, two were disposable (weqb_eqb holds by the definition of Zmod.eqb; unsigned_split1_mod is three stdlib rewrites at its sites), two are local to KamiRiscvStep.v (instances of the others), and the seven genuinely about Kami's own operations (Z_of_N_wordToN, wordToN_split1/2, wordToN_eq_rect, split1_wplus_silent, if_weq_eqb, Z_of_N_wordToN_combine) now live in kami#51's Word.v next to their relatives; the processor pins that. Landing with the current round.
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
| (* Stated on its own rather than discharged inline: Kami's Word.v registers | ||
| ZifyNat/ZifyN globally, so inside the [end2end] proof below [lia] translates | ||
| every [nat] and [N] hypothesis of a very large context, and the certificate | ||
| it builds there overflows the kernel stack at [Qed]. *) |
There was a problem hiding this comment.
double-check that this is still accurate and consider whether there is a more straightforward workaround
There was a problem hiding this comment.
Checking: whether kami#51's Word.v still registers the zify instances globally (if so, making them non-global there is the straightforward fix and this lemma goes back inline), and whether a clear before lia or a Z-only statement is enough otherwise. Will report with the measurement.
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
There was a problem hiding this comment.
Checked. The effect is real but the attribution was off: Kami's Word.v Requires stdlib's ZifyNat/ZifyN, whose registrations are global by design (its own Op_Npow2 is not the cost), so inside end2end lia translates every nat/N hypothesis of the large context; reproduced with the lemmas inlined: both lia calls succeed and Qed then dies with "Stack overflow" after 24 minutes at 6.5 GB. The straightforward fix is clear - the four bounds before lia at the two sites; the standalone lemmas and the comment are gone. No Kami change needed.
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
andres-erbsen
left a comment
There was a problem hiding this comment.
Make sure that pull requests to submodules of fiat-crypto (or their submodules) already include include a merge commit, unless the entire PR is just one commit or no adaptations to parent repositories are needed. This is so that fast-forwarding all submodules at once leaves the expected git history.
|
Understood on the merge commits: every multi-commit submodule PR in this stack now ends in a merge commit (first parent the current master, second the series), and each downstream pin will point at that merge commit, so fast-forwarding all submodules leaves merge history. coqutil#187, riscv-coq#54 and quartz#2 are done; bedrock2#565, rupicola#226, kami#51/#53 and quartz#3 follow when their current review rounds land. Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm). |
…he word type coqutil e9309319 (coqutil#187) deletes Word.Interface and Word.Naive, represents machine words as the stdlib type `bits width` (Zmod (2^width)), and drops the lemmas and modules that the standard library already provides (among them coqutil.Z.div_mod_to_equations; the tactic Z.div_mod_to_equations comes from Lia). riscv-coq 44fc5b8c is the corresponding riscv-coq tip, and kami 9217329e (kami#51) represents its words as the same stdlib type and provides the transport lemmas the processor proofs use. bedrock2 follows in the next commits. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm
coqutil no longer has a word interface: the word type is `bits width` from the stdlib. Sections keep the name `word` as a local notation for `bits width`, the `word.ok` hypotheses disappear, and `Bitwidth width` is the only assumption left about the width. Operations are the stdlib ones (Zmod.add, Zmod.udiv, ...) and lemmas are the stdlib lemmas or the few that coqutil's Word.Properties keeps. Semantics: the stdlib shifts take an unmasked Z amount, so the language-level masking of shift amounts is written out in interp_binop (`Zmod.slu a (Zmod.unsigned b mod 2 ^ Z.log2 width)`); mulhuu is written out as `bits.of_Z width (unsigned a * unsigned b / 2 ^ width)`; divu/remu are Zmod.udiv/umod, whose behaviour on a zero divisor is the RISC-V one; the comparison operators return Zmod.one/Zmod.zero. Word constants in specifications and lemmas are spelled `bits.of_Z _ k` (or `/[k]` with WordNotations), which is the term `expr.literal k` evaluates to, so that program-derived terms and specification terms agree syntactically. Zmod.one/Zmod.zero appear only as the boolean results of comparisons. ZnWords restates its lemma table on the stdlib lemmas, matches `@Zmod.unsigned (2 ^ ?w) _` to find word-valued subterms, and eliminates Z.smodulo through coqutil's bridge lemma. bottom_up_simpl, unzify and WordPushDownLemmas match the stdlib heads; the `unsigned zero`/`unsigned one` cases are handled by their own rules. WordNotations binds ^+, ^-, ^*, ^<<, ^>> to the stdlib operations. groundcbv leaves a bare power of two alone: it is the modulus of the bits types and the stdlib lemmas are stated with `2 ^ n`, so evaluating it would only stop them from matching. With the word operations now transparent, MetricSemantics' `firstorder` unfolded through them (2.4 s); the goal is closed directly instead. Zmod.unsigned and Zmod.signed are `simpl never` globally in coqutil's Bitwidth.v; bedrock2/ needs no further simpl control. ZnWords keeps word disequalities as disequalities on the unsigned values, and its bitwise rules use the [mod m] forms (stdlib's Zmod.unsigned_and, and unsigned_or_modwrap/unsigned_xor_modwrap in WordPushDownLemmas) so that Z.div_mod_to_equations gives lia the range of the result, as it did for word.wrap; Zmod.not uses bits.unsigned_not', whose `Z.ones n - x` is linear. ZnWordsTests covers both points with 64-bit goals. lia saturates on the `2 ^ width` inside the map.ok section hypothesis, so a lemma proved with lia in a section with the word parameters acquires an implicit width its statement does not mention. ArrayCasts keeps its byte and list material in a section without those parameters, with the simpl settings as file-level Local Arguments. LiveVerif's framework support: HeapletwiseAutoSplitMerge's @array patterns take the current implicit-argument count, tweak_tacs' swap takes open_constr so that /[0] resolves against the hypothesis, and unzify's get_bitwidth also finds the width from any Zmod (2 ^ w)-typed hypothesis or goal (LiveVerif's Bitwidth instance is global, not a hypothesis). Rocq master ignores non-reference terms in `using` clauses, so FlatConstMem applies map.adjacent_arrays_disjoint directly instead of passing an applied term to `auto using`. Z.div_mod_to_equations, Z.testbit_mod_pow2 and Z.testbit_ones are the standard library's; coqutil 8b40a6c1 has no copies of them, so nothing requires coqutil.Z.div_mod_to_equations. WordNotations resolves the width of /[x] and \[x] through Bitwidth: the width is a hole of the class type WordWidth (a name for Z), filled by unification like any hole, and a hole still open at the end of elaboration is resolved through the Bitwidth instance in scope to the literal width; so the notations need neither an expected type nor a `: word` ascription. The hole is the width rather than the modulus so that a plain modulus evar (an implicit argument of a Zmod operation) unifies with the application 2 ^ ?w instead of absorbing the hole, and `_ :> WordWidth` ascribes the type without a cast node, so the elaborated terms are exactly bits.of_Z width x and Zmod.unsigned x, as the semantics produces them. Printing is unchanged. The word tactics recognise a modulus by its shape 2 ^ w, and simpl, cbn, injection and cbv evaluate a closed one to Z.pow_pos 2 p or a literal; fold_pow2_moduli (FoldPow2Moduli.v) folds such moduli back in the hypotheses and the goal (by change, so that the dependent types of the variables follow) and runs at the entry of ZnWords_pre, zify_goal, zify_hyps and bottom_up_simpl_in_goal/_in_hyp, so that a plain cbn or simpl before a word tactic is harmless. groundcbv still leaves a bare power of two alone: FlatConstMem's simpl_lengths matches the modulus right after it, and folding back there costs seconds per call. Semantics names the shifts and the comparisons of the language (slu, sru, srs with the masked amount; ltu, lts as booleans on unsigned and signed) and interp_binop uses them, so that downstream code can state and reify against these heads rather than the inline lambdas; the constant-amount lemmas unsigned_slu_shamtZ, unsigned_sru_shamtZ, signed_srs_shamtZ and shamt_small live next to them. The five carry the simpl hint (Arguments ... /) so that simpl and cbn expose the masked amount and the Z comparison as the program logic's automation expects, and the restricted cbn/cbv lists that unfold interp_binop name them too. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm
The word type is `bits width`; the `word.ok` and `word.riscv_ok` hypotheses and NaiveRiscvWordProperties.v are gone. RiscvWordProperties.v keeps the three lemmas that relate riscv-coq's spelling of mulhuu, divu and remu (with their zero-divisor tests) to bedrock2's semantics; the zero-divisor cases are now the stdlib's Zmod.udiv_0_r and Zmod.umod_0_r. Places that depended on the abstract word instance: - riscv-coq's shift-immediate instructions shift by the raw immediate, while the source semantics masks the amount; the two agree because the compiled instruction is valid only for amounts below the bitwidth, which `shift_imm_range` extracts from the ptsto_instr clause. - `simpl` on an `execute` term now reduces `Zmod.eqb` to a match on the representation, which `simulate'` does not recognise; RunInstruction.v and FlatToRiscvFunctions.v make Zmod.eqb `simpl never` locally, the only two files whose automation needs it. - `lia` saturates on the `2 ^ width` that `map.ok mem` now exposes, so width- free lemmas inside a section with a `Bitwidth` context acquire a `width` parameter nothing can infer at `eapply`; Spilling.v's max_var lemmas move above that context, and Pipeline's `exec` applications lose the word argument. - coqutil's solve_word_eq no longer calls simpl, so the few proofs that relied on it to unfold framelength, List.app or a Let-bound instruction list do so explicitly. - MMIO's register names come from the reg_class equation rather than from automatically generated names. GoFlatToRiscv and MMIO use the standard library's Z.div_mod_to_equations; coqutil 8b40a6c1 has no copy of it. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm
The word type is now stdlib's bits, so the framework's patterns and the examples follow the ops and lemma names of coqutil's fable/bits, and the Bitwidth instance for the examples is a global instance rather than a section hypothesis (LiveVerifBitwidth.v). Points that needed more than renaming: - Standalone word constants in statements need a type ascription (`(/[k] : word)`, `\[(/[k] : word)]`): the modulus of `Zmod.of_Z` is only inferred from a typed position. - `simpl`, `cbn` and `injection` reduce the `2 ^ 32` modulus in word types to `Z.pow_pos 2 32`, after which the word tactics and even `rewrite` no longer recognise the terms; those calls become `cbn -[Z.pow]`, `cbn [f]`, or an `assert ... by congruence`. - critbit's `bit_at` masks its shift amount like the semantics does (`(bw-1 - i) mod 2 ^ log2 bw`); the `bit_at_step` rules only match the normalised amount shapes, because matching the raw program term makes unification reduce closed word arithmetic (an exponential blow-up through the stdlib bounds checks). - Auto-generated hypothesis names changed (the word type is `Zmod`, so `r`, `r0` became `z`, `z0`); proofs that relied on them bind the variable explicitly. - PrintSmt/SampleSideconds: the SMT export filters the Bitwidth instance, spells unsigned comparison with `Z.ltb`, and leaves the moduli of word types alone when evaluating constant powers. - The bits 32 ring comes from coqutil, so SampleSimpls' own Add Ring is gone. The coqutil pin 8b40a6c1 removed List.compare, List.assoc_app_cons and bitblast.Z.testbit_ones_nonneg; the examples use the standard library's ListDef.list_compare, List.app_assoc and Z.testbit_ones_nonneg instead. In insertion_sort the app_assoc instance is posed with the cons-shaped type it is convertible to, so that `rewrite in *` still instantiates it per hypothesis; note that `[x]` is the list-indexing notation in LiveVerif, not a singleton list. step and steps fold evaluated word moduli back (fold_pow2_moduli) before doing anything, so the examples use plain cbn and the SMT tests' eval_constant_pows no longer spares the moduli. Ten cbn -[Z.pow] stay in critbit, where a hand-written \[k] or a syntactic step (destruct, subst, match on a hypothesis, rewrite in) follows the cbn before any word tactic could fold the moduli back. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm
Kami's word n is the standard library's bits (Z.of_nat n) and the processor's machine word is bits 32, so nothing here needs a word instance any more: the KamiWord.word record, its ok proof, the wordW instance, the Spelling lemmas and the kunsigned/kofZ/ksigned wrappers are gone, and KamiWord.v keeps only the lemmas that carry Kami's unsigned_* specification over to the wordToN, split1/split2 and combine spellings the processor proofs meet. The machine word is spelled bits (Z.of_nat (Z.to_nat width)), with width the Z definition 32 that KamiProc always had: this is what riscv-coq's generic code produces when its bits width is instantiated from the types of the section variables, and the Bitwidth instance is stated at the same spelling so that typeclass resolution (which fixes the width from the instance) and type-driven unification agree. Any second spelling of the same modulus is fatal in KamiRiscvStep.v, whose tactics compare terms syntactically: lia, destruct ... in *, and the folding of Z.of_nat literals in the kitchen-sink tactics. Two alternatives were tried and rejected. With width := Z.of_nat nwidth (nwidth := 32), that folding rewrites the modulus of every word, section variables included, to 2 ^ 32, after which Kami's lemmas keyed on 2 ^ Z.of_nat ?sz no longer unify and fetch_ok silently fails inside its try. With Word.word nwidth as the type, riscv-coq's map.get and the processor's map.get of one register read are two syntactically different terms and destruct splits them into two variables. KamiRiscvStep.v additionally declares Z.to_nat simpl never, next to its existing Z.pow and Z.of_nat lines: cbn in decodeI computed Z.to_nat width inside the let-bound decoder while the hypotheses derived from it earlier still read Z.to_nat width, so destruct (Z.eqb_spec ..) in * no longer reached the body and eval_decodeI's repeat loop ran for ten minutes without terminating. Kami's own kinds (Bit 32, Bit BitsPerByte) still cbn to unary literals, so BitsPerByte is excluded from the literal folding, the folding covers let-bound bodies as well as hypotheses, and the load-value blocks rewrite unsigned to wordToN by the word's type rather than by unifying its modulus. The remaining alignments are at the places the old proof also aligned: Kami-side terms carry Pos.to_nat 32 after its cbv and are changed to nwidth before the MMIO contradictions; lui/auipc change Z.of_nat nwidth to 32 before their lia; unsigned_of_Z is rewritten at syntactic occurrences only because a bare rewrite sees through Kami's combine (an of_Z after one unfolding); signExtend and the bitSlice range facts are stated at the Z.of_nat nwidth spelling. riscvMemInit_get_None takes its pair equality apart with pair_equal_spec, since inversion and injection simpl the components and compute the modulus. wordToN_bound at Word.word rv32RfIdx gives Npow2 rv32RfIdx. coqutil 8b40a6c1 states bitSlice_alt in division form, so the bitSlice' unfoldings after it are gone. Statements are unchanged up to the rename; admits are unchanged. KamiProc's Bitwidth (Z.of_nat nwidth) is a plain definition now, made an instance only in KamiRiscvStep.v and KamiRiscv.v, the two files whose statements resolve riscv-coq's MachineWidth (bits ?w) for the processor's word. That resolution happens before ?w is unified with the word, so it is the instance that decides the spelling of every modulus in the riscv arithmetic of run1. Measured on c07250b, processor/ and end2end/ from clean each time. With no instance, Consistency.v still compiles (its statements get the width from the word type by unification) but KamiRiscvStep.v fails at the first MMIO-contradiction lia in execLd (line 1730) and in every later case: coqutil's Bitwidth 32 is the only candidate, ?w becomes 32, the riscv side of run1 comes out at 2 ^ 32 (Hlv) while every Kami-side term is at 2 ^ Z.of_nat (Z.to_nat width) (Heqic), and lia sees two atoms. With an export-scoped instance, processor/ builds but End2EndPipeline.v fails at line 371: activated at Import, the instance is tried first for Context (spec: ProgramSpec), spec is elaborated at Z.of_nat (Z.to_nat width), the compiler's initial_conditions hypothesis has datamem_start at 32, and rewrite heap_start_agree in H no longer applies. The global instance only worked because it was tried after Bitwidth 32 there. Consistency.v and end2end see no instance now, and end2end elaborates spec at the compiler's own width as before. Same 8 axioms, admits unchanged, times unchanged (KamiRiscvStep 494 s). bedrock2 and Kami use the same word type now, so the file had shrunk to a dozen transport lemmas, and each has a better home. Two are disposable: Zmod.eqb is Z.eqb on unsigned by definition, so weqb_eqb was reflexivity, and unsigned_split1_mod's three sites rewrite with wordToN_split1, N2Z.inj_mod and Z_of_N_Npow2 instead. Seven state facts about Kami's own wordToN, split1/split2, combine, eq_rect, wplus and weq and are now provided by Kami's Word.v (Z_of_N_wordToN, wordToN_split1, wordToN_split2, Z_of_N_wordToN_combine, wordToN_eq_rect, split1_wplus_silent, if_weq_eqb; kami#51 at 9217329e), so their copies and the Z_pow_add_lor helper behind the combine lemma go. The two that remain, sumbool_rect_bool_weq and unsigned_eqb, are about the shapes KamiRiscvStep.v's own tactics produce and are local to that file. Admits are unchanged. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm
Bedrock2SemanticsForKami keeps only the memory map instance, since the word and its ok and riscv_ok instances have no counterpart on bits; KamiRiscvWordProperties.v goes with them. The compiler-facing side stays at bits 32 and meets the processor's bits (Z.of_nat (Z.to_nat width)) by conversion, as the two instances did before. In end2end's memory-layout cancellation the generic cancel no longer pre-matches any pair, because the addresses differ in that width spelling, so each of the four pairs gets its own step: the instruction pair is convertible, the unused-instruction pair needs only simpl_word_exprs, and the heap pair needs its length arithmetic, stated on its own as imem_heap_lengths for the same reason as imem_heap_stack_lengths (lia over the whole context overflows the kernel stack at Qed). lightbulb_spec's goodHlTrace and OP take no word argument any more. RelyNotations writes its demo words as bits 32. Print Assumptions end2end_lightbulb lists the same 8 axioms as before. The two length lemmas stated on their own for lia's sake are gone; the goals are proved in place after clearing everything but the four bounds they need. Requiring Kami's Word.v loads stdlib's ZifyNat and ZifyN (no other dependency does), whose registrations are global by design, so lia inside the end2end proof translates every nat and N hypothesis of the whole context; with the context cleared the two calls take well under a second and End2EndPipeline.v compiles in 10 s, whereas with the two calls inlined over the full context the file ran 24 minutes at 6.5 GB and then Qed of end2end failed with a stack overflow. Kami's own Op_Npow2 registration could be made local, but it is not what costs here. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm
Use stdlib bits as the word type throughout bedrock2 (coqutil#187, riscv-coq#54, kami#51). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm
c07250b to
24b234f
Compare
Simulate the processor regenerated from Kami's Bluespec output (kami#54), on top of the stdlib bits port (mit-plv#565). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm
| Definition ltu (a b : word) : bool := Z.ltb (Zmod.unsigned a) (Zmod.unsigned b). | ||
| Definition lts (a b : word) : bool := Z.ltb (Zmod.signed a) (Zmod.signed b). |
There was a problem hiding this comment.
In a follow-up PR, inline and remove these definitions (just ltu and lts). Or is that a really bad idea?
There was a problem hiding this comment.
Tried it in a scratch tree (bedrock2 with ltu/lts inlined, rupicola re-keyed on Z.ltb (Zmod.unsigned _) (Zmod.unsigned _) / Z.ltb (Zmod.signed _) (Zmod.signed _)). Workable, but it has real costs, so I would keep the two definitions:
- Without a head symbol, a word comparison is distinguished from a Z comparison only by the shape of its operands.
\[a] <? \[b]still compiles toexpr.op ltu, and a pure-Zz <? 3still takes the Z lemma, but\[a] <? 5makes the Z lemma fire and leavesDEXPR ?e (bits.of_Z _ (Zmod.unsigned a))unsolvable, and\[a] <? \[b] + 1matches nothing; todayltu a 5andltu a (b + 1)compile. - A comparison against a numeral loses its modulus:
Z.ltb (Zmod.unsigned (bits.of_Z _ 11)) (Zmod.unsigned idx)elaborates with an unresolved2 ^ ?y, and theComputein rupicola'sExamples/Loops.vthen hangs invm_computeinstead of failing; Utf8's0x10FFFF <w c/c3 >w 1114111need an explicit width. With the width spelled out at those three sites everything builds and the generated programs are byte-identical. - The definitions cost nothing on the bedrock2 side:
Arguments ltu /already givessimpl/cbnthe inline form, and bedrock2's own references are all in unfold lists. Diff for the inlining would be 10 rupicola files (+28/-28) plus three unfold lists in bedrock2; no lemma is lost.
Inlining only one of the two has the same issues for that operator (ltu: 14 rupicola sites plus the three numeral sites; lts: 12 sites, none against a numeral). Happy to do it as a follow-up anyway if you prefer the smaller Semantics.
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
|
Please investigate usage of Semantics and LeakageSemantics (and other Leakage- files) throughout the repository and present a plan and considerations for using Leakage-aware specificaitons for fiat-crypto GarageDoor.v and all bedrock2 functions transitively called from there. |
|
Leakage-aware specs for GarageDoor: inventory, plan, considerations. Status
Plan (leaves first)
Considerations: loop leakage functions are hand-written; no leakage-to-plain lifting Effort: 6-8 person-weeks to a per-iteration RISC-V constant-time theorem, plus 3-4 Full reportFollow-up F: leakage-aware specifications for GarageDoorRequest: Andres Erbsen, #565, 2026-09-17 06:02Z. "Investigate usage of Sources read (read-only clones, after the stdlib-bits port):
One measurement was compiled in scratch ( 1. Inventory of the leakage machinery in bedrock2Dates from Semantics layer
Program logic layer
Examples (bedrock2Examples)
LiveVerif and Rupicola have no leakage support at all ( CompilerThe whole pipeline is leakage-aware and proven:
Event loop (what GarageDoor actually uses)
So: leakage is threaded through the event-loop pipeline but never stated. There is no 2. GarageDoor call graph and current spec styles
Important head start: GarageDoor.v is already written in the leakage program logic Statement counts (measured; "stmts" = leaf commands; w/c/call/io = while / cond / call /
Secret data in GarageDoor: the 32-byte 3. PlanThe target statement, at the source level: for every function, Order: leaves first, so that Phase 0: fix the statement (1-2 days, design only)Decide (a) the per-function spec form above, (b) how driver leakage is expressed (as a Phase 1: bedrock2 leaves: stdlib and drivers (about 2 person-weeks, in bedrock2)
Phase 2: generated field functions (1-2 weeks, fiat-crypto, reusable)Three options; recommend A.
Phase 3: Rupicola-generated functions (1-2 weeks, fiat-crypto)Rupicola has no leakage support (10.3k LOC library, zero hits). Recommend proving Phase 4: handwritten fiat-crypto wrappers (1-2 days)
Phase 5: GarageDoor.v (about 1 week)Add Phase 6: top-level theorem
Total: about 6-8 person-weeks to T1 (Phases 0-6/T1), plus 3-4 weeks for T2. Phases 1, 2, 4. Considerations and risks
GitHub-ready summary (for bedrock2#565)Leakage-aware specs for GarageDoor: inventory, plan, considerations. Status
Plan (leaves first)
Considerations: loop leakage functions are hand-written; no leakage-to-plain lifting Effort: 6-8 person-weeks to a per-iteration RISC-V constant-time theorem, plus 3-4 Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm). |
|
@JasonGross-fiat-crypto-bot implement the first two steps of this plan, one PR per file |
|
Working on it: one PR each for memswap.v, memconst.v, memmove.v, SPI.v, LAN9250.v and lightbulb.v (recvEthernet), with the SemanticsRelations leakage-to-plain helper as a small first PR so plain callers keep building. Drivers stack on each other (SPI, then LAN9250, then recvEthernet). Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm). |
|
Design note for the SPI/LAN9250 leakage PRs: Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm). |
|
First batch of the plan is open, one PR per file, all against master:
Still coming: SPI, LAN9250 (with the precise timeout specs from the note above), recvEthernet stacked on those; lightbulb's other functions stay plain and take the stackalloc-free hypothesis. Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm). |
|
The rest of the first two steps is up, so every PR in the plan is now open and ready:
As described in the design note above, the SPI and LAN9250 timeout branches are now stated precisely. A lemma recovers the old Compile times: LAN9250.v goes from 35 s to 59 s. lightbulb.v stays at about 36 s. Unfolding the recvEthernet loop with Written by Claude Opus 5.5 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm). |
PR 3 of the word-interface removal (after mit-plv/coqutil#187 and mit-plv/riscv-coq#54): the word type is stdlib
bits widtheverywhere in bedrock2, withBitwidth widththe only assumption about the width. One commit per subproject: pins,bedrock2/,compiler/,LiveVerif/,processor/,end2end/, ending in a merge commit so that fast-forwarding leaves the usual history. Absorbs #562 and supersedes #563; the Kami side is mit-plv/kami#51 (pinned at its merge commit, like coqutil and riscv-coq).What changed in substance: Semantics defines
slu,sru,srs(shift amount masked to the bitwidth),ltu,ltsand mulhuu, with the shift-amount lemmas next to them (rupicola's copies go away in mit-plv/rupicola#226); divu/modu are the stdlib operations (their by-zero results are the RISC-V ones, soriscv_okis gone); ZnWords restates its table on the stdlib lemmas; the conversion notations/[x]and\[x]take their width from aWordWidthclass hole resolved through theBitwidthinstance in scope, so statements need no: wordcasts while the elaborated terms stay literallybits.of_Z width x/Zmod.unsigned x; afold_pow2_modulistep at the entry of ZnWords, the zify tactics,bottom_up_simpland the LiveVerif steps turns an evaluated power-of-two modulus back into2 ^ k, replacing the per-file guards against evaluating2 ^ width.processor/KamiWord.vis deleted (its Kami-side lemmas moved to Kami'sWord.v), theBitwidthinstance there is file-local, and the two End2EndPipeline arithmetic goals clear the Kami hypotheses beforeliainstead of going through helper lemmas.Print Assumptions end2end_lightbulblists the same 8 axioms as master; admits unchanged; no file slower than 1.3x.Two judgment calls for review:
bits.of_Z _ k(or/[k]), not the numeral notation from coqutil#187, becauseexpr.literal kevaluates tobits.of_Z width kand spelling spec constants asZmod.onebroke seven example proofs on syntactic matching.Zmod.zero/Zmod.oneappear only as comparison results.processor/keeps the width as a named constant,Definition width : Z := 32, and derivesnwidth := Z.to_nat widthper file, so that riscv-coq's generic code (which takes the width from the section variables' types) and typeclass resolution produce the samebits (Z.of_nat nwidth)syntactically; KamiRiscvStep.v adds a file-localArguments Z.to_nat : simpl nevernext to its existingZ.pow/Z.of_natlines, because onecbncomputingZ.to_nat widthwhile another does not made theeval_decodeIproof loop. Details in the processor commit message.Written by Claude Fable 5.1.
🤖 Generated with Claude Code
https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm