Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9acff44
Miller–Rabin x86-64 IFMA: the round's code (2-bit windows) and the wi…
claude Nov 7, 2026
5fa3bb8
Miller–Rabin x86-64 IFMA: 52-bit setup, register counters, ctlOk-shap…
claude Nov 7, 2026
cded62d
Miller–Rabin x86-64 IFMA: proof progress (WIP, contains sorry)
claude Nov 7, 2026
976c82c
Miller–Rabin x86-64 IFMA: proof progress (WIP, contains sorry)
claude Nov 7, 2026
68965f6
Miller–Rabin x86-64 IFMA: the multiplications, selection and table, p…
claude Nov 7, 2026
655ae1b
Miller–Rabin x86-64 IFMA: the windows, proven
claude Nov 7, 2026
4473e72
Miller–Rabin x86-64 IFMA: setup proof progress (WIP)
claude Nov 7, 2026
cee6528
Miller–Rabin x86-64 IFMA: the setup, proven
claude Nov 7, 2026
459f716
Merge remote-tracking branch 'origin/main' into claude/perf-4141-mr-ifma
claude Nov 7, 2026
154691f
Miller–Rabin x86-64: RoundImpl with a round of its own; use Mxcsr's c…
claude Nov 7, 2026
f39b1be
RSA key generation x86-64: MXCSR restored by the round (ctlC/ctlOk) r…
claude Nov 7, 2026
9f7b24c
Miller–Rabin x86-64 IFMA: work in progress (callers' MXCSR proofs, ro…
claude Nov 7, 2026
ce9a7e0
DSA and FFDH parameter generation x86-64: MXCSR restored by the round…
claude Nov 7, 2026
e1cf7e2
Miller–Rabin x86-64 IFMA: the round's correctness and constant time, …
claude Nov 7, 2026
c7dad24
Miller–Rabin x86-64: the AVX512_IFMA variant; the code as a literal
claude Nov 7, 2026
11dd845
Miller–Rabin x86-64 IFMA: arithFeatures for callers that never call t…
claude Nov 7, 2026
c539bbd
README: the algorithms tables with the IFMA Miller–Rabin variant
claude Nov 7, 2026
1af4466
Rust: choose the IFMA Miller–Rabin backend (WIP)
claude Nov 7, 2026
896a715
Rust: choose the IFMA Miller–Rabin backend for RSA key generation, DS…
claude Nov 7, 2026
e146f54
FFDH: test every runnable domain implementation without a branch only…
claude Nov 7, 2026
f141448
Merge remote-tracking branch 'origin/main' into claude/perf-4141-mr-ifma
claude Nov 7, 2026
eab3c93
Regenerate the x86-64 dispatchers for DSA, FFDH and RSA key generatio…
claude Nov 7, 2026
692fb90
Merge remote-tracking branch 'origin/main' into claude/perf-4141-mr-ifma
claude Nov 7, 2026
6110236
Miller–Rabin x86-64 IFMA: 1024- and 1536-bit candidates (WIP: crates …
claude Nov 7, 2026
56af7f3
Regenerate the x86-64 crate: the IFMA Miller–Rabin round for 1024- an…
claude Nov 7, 2026
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
12,766 changes: 12,568 additions & 198 deletions crates/verified-garbage-asm-x86_64/assembly/internal/vg_miller_rabin_round_ifma.s

Large diffs are not rendered by default.

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions lean/VerifiedGarbage/Impl/MillerRabin/X86_64/Ifma.lean
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public import VerifiedGarbage.Impl.Rsa.X86_64.PubIfma
/-!
# The shared Miller–Rabin round with AVX512_IFMA on x86-64

`code mul`: for a candidate of 32, 48 or 64 words (`PubIfma`'s layouts
`pay2048`, `pay3072`, `pay4096`), the round of `Round.lean` in radix `2⁵²`
`code mul`: for a candidate of 16, 24, 32, 48 or 64 words (`PubIfma`'s
layouts `pay1024`, `pay1536`, `pay2048`, `pay3072`, `pay4096`), the round of `Round.lean` in radix `2⁵²`
with `CrtIfma`'s one-lane multiplication (`ammMul`), whose Montgomery radix
is `R' = 2^(52 S)`; otherwise `round mul`.

Expand Down Expand Up @@ -256,8 +256,10 @@ def isW (n : Nat) : List Instr := [.mov .rax (.mem (hdr sW)), .alu .cmp .rax (.i

/-- The round of the layout of the candidate's size, `round mul` otherwise. -/
def code (mul : Nat → Nat → Nat → Prog isa) : Prog isa :=
.seq (.block (isW 32)) (.ite .e (ifma pay2048)
(.seq (.block (isW 48)) (.ite .e (ifma pay3072)
(.seq (.block (isW 64)) (.ite .e (ifma pay4096) (round mul))))))
.seq (.block (isW 16)) (.ite .e (ifma pay1024)
(.seq (.block (isW 24)) (.ite .e (ifma pay1536)
(.seq (.block (isW 32)) (.ite .e (ifma pay2048)
(.seq (.block (isW 48)) (.ite .e (ifma pay3072)
(.seq (.block (isW 64)) (.ite .e (ifma pay4096) (round mul))))))))))

end VG.Impl.MillerRabin.X86_64.Ifma
4 changes: 4 additions & 0 deletions lean/VerifiedGarbage/Impl/Rsa/X86_64/PubIfma.lean
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ namespace VG.Impl.Rsa.X86_64.PubIfma

open VG VG.X86_64 VG.Impl.Bignum VG.Impl.Bignum.X86_64 VG.Impl.Bignum.X86_64.Public VG.Impl.Rsa.X86_64.CrtIfma

/-- 1024-bit moduli: 16 words, numbers of 20 limbs. -/
def pay1024 : Lay := ⟨5, 16, 20, 4, 1⟩
/-- 1536-bit moduli: 24 words, numbers of 32 limbs. -/
def pay1536 : Lay := ⟨8, 24, 32, 4, 1⟩
/-- 2048-bit moduli: 32 words, numbers of 40 limbs. -/
def pay2048 : Lay := ⟨10, 32, 40, 4, 1⟩
/-- 3072-bit moduli: 48 words, numbers of 60 limbs. -/
Expand Down
4 changes: 2 additions & 2 deletions lean/VerifiedGarbage/Proof/Bignum/X86_64/Ifma/ArrSel.lean
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ abbrev arrJ (l : VG.Impl.Rsa.X86_64.CrtIfma.Lay) (i : Nat) : Nat := ((arrs l).ge
abbrev arrC (l : VG.Impl.Rsa.X86_64.CrtIfma.Lay) (i : Nat) : Nat := ((arrs l).getD i (0, 0)).2

theorem arrs_ok (hl : AmmOk l) : ∀ i < 5, arrJ l i < 8 ∧ arrC l i + l.NB ≤ l.D := by
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide

theorem arrs_sep (hl : AmmOk l) :
∀ k < 5, ∀ i < 5, k ≠ i → arrC l k + l.NB ≤ arrC l i ∨ arrC l i + l.NB ≤ arrC l k := by
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide

/-- `cmp r8, k`. -/
theorem cmpR8_ok (s : State) {n k : Nat} (h8 : s.gpr .r8 = BitVec.ofNat 64 n) (hn : n < 2 ^ 31) (hk : k < 2 ^ 31) :
Expand Down
6 changes: 3 additions & 3 deletions lean/VerifiedGarbage/Proof/Bignum/X86_64/Ifma/Block.lean
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ open VG.Proof.Bignum.X86_64.AmmSym (ops Ctx Keeps ofNat_add64)
variable {l : VG.Impl.Rsa.X86_64.CrtIfma.Lay}

theorem AmmOk.bounds (hl : AmmOk l) : 2 ≤ l.R ∧ l.R ≤ 20 := by
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide

/-- The vector registers: the lanes' roles, each lane's `b` and `u`, the zero
register and the temporary, of the 32. -/
theorem AmmOk.regs (hl : AmmOk l) : 1 ≤ l.nl ∧ l.nl * l.R + 2 * l.nl + 1 < 32 := by
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide

/-- The steps: `S / R` blocks, then `S % R` more, at most `4 R`. -/
theorem AmmOk.steps (hl : AmmOk l) :
1 ≤ l.S / l.R ∧ l.S / l.R ≤ 4 ∧ 2 ≤ l.S ∧ l.S ≤ l.L ∧ l.R * (l.S / l.R) + l.S % l.R = l.S ∧
l.S % l.R < l.R := by
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide

theorem LayOk.bounds (hl : LayOk l) : 2 ≤ l.R ∧ l.R ≤ 10 := by
rcases hl with rfl | rfl | rfl <;> decide
Expand Down
6 changes: 3 additions & 3 deletions lean/VerifiedGarbage/Proof/Bignum/X86_64/Ifma/CTRegion.lean
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ structure RegT (l : VG.Impl.Rsa.X86_64.CrtIfma.Lay) : Prop where
rl : TOk [.r8, .r9, .rcx] (.loop (.block (recodeBody l)) .ne)

theorem regT (hl : AmmOk l) : RegT l := by
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;>
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;>
exact ⟨⟨_, by taint_decide⟩, ⟨_, by taint_decide⟩, ⟨_, by taint_decide⟩, ⟨_, by taint_decide⟩,
⟨_, by taint_decide⟩, ⟨_, by taint_decide⟩, ⟨_, by taint_decide⟩, ⟨_, by taint_decide⟩,
⟨_, by taint_decide⟩, ⟨_, by taint_decide⟩, ⟨_, by taint_decide⟩, ⟨_, by taint_decide⟩⟩
Expand Down Expand Up @@ -317,7 +317,7 @@ theorem recBlk_ok (hl : AmmOk l) (hw : l.w ≠ 4) {p sp sl : Nat} {q : RegPub} {
obtain ⟨d1, d2⟩ := lay_oD l hw
obtain ⟨hDb1, hDb2⟩ := hl.D_bounds
have hDp : l.D * p ≤ l.D ∧ l.D * p + l.D ≤ l.nl * l.D ∧ l.nl * l.D ≤ 2 * l.D := ⟨D_le hl hp, lane_end hl hp, (nlD_le hl).2⟩
have hG : l.P / l.w < 2 ^ 31 := by rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
have hG : l.P / l.w < 2 ^ 31 := by rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide
refine WP.mono (WP.keep [.rcx, .r8, .r9] (Q := fun t =>
t.gpr .r8 = off (off q.B q.a) (l.D * p + l.oE) ∧ t.gpr .r9 = off (off q.B q.a) (l.D * p + l.oD) ∧
t.gpr .rcx = BitVec.ofNat 64 (l.P / l.w)) (by
Expand Down Expand Up @@ -383,7 +383,7 @@ theorem k1_chain (hl : AmmOk l) {p sp sl : Nat} {q : RegPub} {s : State} (h : Re
have lT := slot_le (w := l.W) (show aT < 8 by decide)
have lN := slot_le (w := l.W) (show Public.aN < 8 by decide)
have sTN := slot_sep (w := l.W) (show aT ≠ Public.aN by decide)
have hdb : 1 ≤ l.dbls ∧ l.dbls < 2 ^ 31 := by rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
have hdb : 1 ≤ l.dbls ∧ l.dbls < 2 ^ 31 := by rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide
have hk1 : ∀ r ∈ k1Ranges l.W, r.1 + r.2 ≤ slot l.W 8 := by
have := slot_le (w := l.W) (show Public.aAcc < 8 by decide)
have := slot_le (w := l.W) (show Public.aTmp < 8 by decide)
Expand Down
2 changes: 1 addition & 1 deletion lean/VerifiedGarbage/Proof/Bignum/X86_64/Ifma/Conv.lean
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ theorem conv_bounds (hl : LayOk l) : 1 ≤ l.W ∧ 64 * l.W ≤ 52 * l.L ∧ l.L

theorem conv_boundsN (hl : AmmOk l) : 1 ≤ l.W ∧ 64 * l.W ≤ 52 * l.L ∧ l.L ≤ 80 ∧ 64 * l.W + 2 ≤ 52 * l.S ∧
128 * l.W ≤ 104 * l.S ∧ l.S ≤ l.L := by
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide

theorem and_mask' (x : BitVec 64) : x &&& mask52 = BitVec.ofNat 64 (x.toNat % 2 ^ 52) := AmmSym.and_mask' x

Expand Down
2 changes: 1 addition & 1 deletion lean/VerifiedGarbage/Proof/Bignum/X86_64/Ifma/Core.lean
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def checkZeros (l : VG.Impl.Rsa.X86_64.CrtIfma.Lay) : Bool :=
| none => false

theorem checkZeros_ok (hl : AmmOk l) : checkZeros l = true := by
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide +kernel
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide +kernel

theorem zeros_ok (hl : AmmOk l) {s : State} :
WP isa (.block (zeros l)) s fun s' => (∀ r < l.nl * l.R + 2 * l.nl + 1, ∀ t < 4, qv s' (vreg r) t = 0) ∧ Keeps s s' := by
Expand Down
6 changes: 5 additions & 1 deletion lean/VerifiedGarbage/Proof/Bignum/X86_64/Ifma/CoreU.lean
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ def checkZerosU (l : VG.Impl.Rsa.X86_64.CrtIfma.Lay) : Bool :=

theorem checkZerosU_2048 : checkZerosU lay2048 = true := by decide +kernel
theorem checkZerosU_p2048 : checkZerosU VG.Impl.Rsa.X86_64.PubIfma.pay2048 = true := by decide +kernel
theorem checkZerosU_p1024 : checkZerosU VG.Impl.Rsa.X86_64.PubIfma.pay1024 = true := by decide +kernel
theorem checkZerosU_p1536 : checkZerosU VG.Impl.Rsa.X86_64.PubIfma.pay1536 = true := by decide +kernel

theorem UOk.checkZeros (hu : UOk l) : checkZerosU l = true := by
rcases hu with rfl | rfl
rcases hu with rfl | rfl | rfl | rfl
· exact checkZerosU_2048
· exact checkZerosU_p2048
· exact checkZerosU_p1024
· exact checkZerosU_p1536

theorem zerosU_ok (hu : UOk l) {s : State} :
WP isa (.block (zerosU l)) s fun s' =>
Expand Down
8 changes: 4 additions & 4 deletions lean/VerifiedGarbage/Proof/Bignum/X86_64/Ifma/Exp.lean
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ theorem pref_step {e i : Nat} (hi : i < l.ND) :
theorem pref_k0 (hl : AmmOk l) {e : Nat} (he : e < 2 ^ (8 * l.E)) : e / l.T ^ (l.ND - l.k0) = 0 := by
refine Nat.div_eq_of_lt (Nat.lt_of_lt_of_le he ?_)
rw [Lay.T, ← Nat.pow_mul]
exact Nat.pow_le_pow_right (by decide) (by rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide)
exact Nat.pow_le_pow_right (by decide) (by rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide)

/-- The digit bytes outside `Y`, `S` and `V`. -/
theorem OutW.dig (hl : AmmOk l) {B : Addr} {m m' : Mem} (h : OutW l B m m') {p i : Nat} (hp : p < l.nl)
Expand All @@ -65,7 +65,7 @@ theorem r13Inc_ok (hl : AmmOk l) {s : State} {i : Nat} (hi : i ≤ l.ND) (h13 :
WP isa (.block [.alu .add .r13 (.imm 1), .alu .cmp .r13 (.imm (BitVec.ofNat 32 (l.ND + 1)))]) s fun s' =>
s'.gpr .r13 = BitVec.ofNat 64 (i + 1) ∧ s'.zf = some (decide (i + 1 = l.ND + 1)) ∧
VG.Proof.MlKem.X86_64.Keep [.r13] s s' ∧ s'.mem = s.mem ∧ s'.mxcsr = s.mxcsr := by
have hE : l.ND + 1 < 2 ^ 31 := by rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
have hE : l.ND + 1 < 2 ^ 31 := by rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide
refine WP.mono (VG.Proof.MlKem.X86_64.WP.keep [.r13] (Q := fun s' =>
s'.gpr .r13 = BitVec.ofNat 64 (i + 1) ∧ s'.zf = some (decide (i + 1 = l.ND + 1)) ∧ s'.mem = s.mem ∧
s'.mxcsr = s.mxcsr) (by
Expand Down Expand Up @@ -236,7 +236,7 @@ theorem digLoop_ok (hl : AmmOk l) {t₀ : State} {B : Addr} {M k x : Nat → Nat
refine .inr ⟨by simp, n - 1, by omega_using [hn], by omega_using [hn], by omega_using [hE],
by rw [show l.ND + 1 - (n - 1) = l.ND + 1 - n + 1 by omega_using [hE, hn]]; exact hI'⟩

theorem k0_le (hl : AmmOk l) : l.k0 ≤ l.ND := by rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
theorem k0_le (hl : AmmOk l) : l.k0 ≤ l.ND := by rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide

/-- The exponentiations from digit `i₀`, which the block `st` puts in `r13`:
`Y ≡ x^e R` for the exponents `e` whose digits before `i₀` are zero, whose
Expand Down Expand Up @@ -309,7 +309,7 @@ theorem expLoop_ok (hl : AmmOk l) {s : State} {B : Addr} {M k x : Nat → Nat} {
(∀ r, r ≠ .rax → r ≠ .rcx → r ≠ .rdx → r ≠ .rsi → r ≠ .r8 → r ≠ .r9 → r ≠ .r10 → r ≠ .r11 → r ≠ .r12 →
r ≠ .r13 → r ≠ .r15 → s'.gpr r = s.gpr r) ∧
s'.rd = s.rd ∧ s'.wr = s.wr ∧ s'.mxcsr = s.mxcsr := by
have hk32 : l.k0 < 2 ^ 32 := by rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
have hk32 : l.k0 < 2 ^ 32 := by rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide
refine expLoopFrom_ok hl hc gf (k0_le hl) (fun p hp => pref_k0 hl (he p hp)) hd fun t _ _ _ _ _ => ?_
rw [WP.block_cons_iff]
refine ⟨t.setReg32 .r13 (BitVec.ofNat 32 l.k0), rfl, WP.block_nil ⟨rfl, rfl, rfl, rfl, ?_,
Expand Down
6 changes: 3 additions & 3 deletions lean/VerifiedGarbage/Proof/Bignum/X86_64/Ifma/Out.lean
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ open VG.Proof.Bignum.X86_64.AmmSym (wrList writeW256_outside word_wrList_unique
variable {l : VG.Impl.Rsa.X86_64.CrtIfma.Lay}

theorem AmmOk.D_bounds (hl : AmmOk l) : l.NB ≤ l.D ∧ l.D + l.NB ≤ 2 ^ 20 := by
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide

theorem LayOk.D_bounds (hl : LayOk l) : l.NB ≤ l.D ∧ l.D + l.NB ≤ 2 ^ 20 := hl.amm.D_bounds

/-- At most two lanes, whose carries are `rdx` and `rsi`. -/
theorem AmmOk.nl_le (hl : AmmOk l) : 1 ≤ l.nl ∧ l.nl ≤ 2 := by
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> decide
rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> decide

theorem off_ltN (hl : AmmOk l) {j : Nat} (hj : j < l.L) : l.off j + 8 ≤ l.NB := by
obtain ⟨hR, _⟩ := hl.bounds
Expand Down Expand Up @@ -488,7 +488,7 @@ theorem ammCore_ok (hl : AmmOk l) {s : State} {B : Addr} {a m : Nat → Nat →
k₁.mem, k₁.rd, k₁.wr⟩
have hst := hl.steps
refine WP.seq (WP.mono (loop_ok hl e (l.S / l.R) s₁ hst.1 (Nat.le_refl _) (by rw [Nat.sub_self]; exact i₁)
(by rw [k₁.gpr _ (by decide)]; rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl <;> rfl))
(by rw [k₁.gpr _ (by decide)]; rcases hl with (rfl | rfl | rfl) | rfl | rfl | rfl | rfl | rfl <;> rfl))
fun s₂ₐ ⟨i₂ₐ, g₂ₐ, x₂ₐ⟩ => ?_)
rw [accStores_code, List.append_assoc, List.append_assoc, WP.block_append_iff]
refine WP.mono (steps_chain hl e (l.S % l.R) (by omega_arith) (by omega_arith) s₂ₐ i₂ₐ) fun s₂ ⟨i₂, g₂', x₂'⟩ => ?_
Expand Down
21 changes: 15 additions & 6 deletions lean/VerifiedGarbage/Proof/Bignum/X86_64/Ifma/PubExp.lean
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ theorem setup_ok (hl : PubOk l) {a : State} {P : Addr} {Z : Nat} {minv : BitVec
-- The modulus' limbs.
rw [WP.block_append_iff]
have hdi₁ : t₁.gpr .rdi = P := (k₁.gpr (by decide)).trans hg.rdi
refine WP.mono (ld52_ok hl (s := t₁) (B := B) (H := P) (j := aN) (o := 0) b₁ hdi₁ r12₁ (hj aN (by decide))
refine WP.mono (ld52_ok hl.amm (s := t₁) (B := B) (H := P) (j := aN) (o := 0) b₁ hdi₁ r12₁ (hj aN (by decide))
(fun i hi => by rw [k₁.2.1, k₁.2.2, AmmSym.off_add, harr]; exact hs.ld (by omega_arith))
(fun q hq => by rw [k₁.2.2]; exact hwB _ (by have := hF q hq; omega_arith)) (by omega_arith)
(fun m m' f i hi => by
Expand Down Expand Up @@ -150,7 +150,7 @@ theorem setup_ok (hl : PubOk l) {a : State} {P : Addr} {Z : Nat} {minv : BitVec
rw [low_word o₄ hn (by omega_arith) he, m₃, low_word o₂ hn (by omega_arith) he, m₁]
have hdi₄ : t₃.gpr .rdi = P := (k₃.gpr (by decide)).trans hdi₂
-- `R² mod m` into `A`.
refine WP.mono (ld52_ok hl (s := { t₃ with mem := M₄ }) (B := B) (H := P) (j := aR2)
refine WP.mono (ld52_ok hl.amm (s := { t₃ with mem := M₄ }) (B := B) (H := P) (j := aR2)
(o := oA l) hb₃ hdi₄ (by rw [k₃.gpr (by decide), k₂.gpr (by decide)]; exact r12₁) (hj aR2 (by decide))
(fun i hi => by
show InRegions (t₃.rd ++ t₃.wr) _ 8
Expand Down Expand Up @@ -351,13 +351,14 @@ theorem fin_ok (hl : PubOk l) {s : State} {P : Addr} {Z N : Nat} (hs : Scr s P Z

/-! ## The exponentiation by 65537 -/

/-- The modulus' limbs and `k₀` as `setup` leaves them. -/
theorem mod_of (hl : PubOk l) {m : Mem} {B : Addr} {N : Nat} {minv : BitVec 64}
/-- The modulus' limbs and `k₀` as `setup` leaves them, for any layout of
the multiplication whose radix is `2^(52 L)`, above `4 n`. -/
theorem mod_ofN (ha : AmmOk l) (a7 : l.S = l.L) (a8 : 64 * l.W + 2 ≤ 52 * l.S) (a11 : l.L = 4 * l.R)
{m : Mem} {B : Addr} {N : Nat} {minv : BitVec 64}
(hN : ∀ j < l.L, word m B (0 + l.off j) = BitVec.ofNat 64 (limbN N j)) (hNlt : N < 2 ^ (64 * l.W))
(hk : ∀ i < 4, word m B (l.NB + 8 * i) = BitVec.ofNat 64 (minv.toNat % 2 ^ 52))
(hinv : (N % 2 ^ 64 * minv.toNat + 1) % 2 ^ 64 = 0) : Mod l m B N (minv.toNat % 2 ^ 52) := by
obtain ⟨-, -, -, -, -, -, a7, a8, -, -, a11, -⟩ := hl.area
have hL := (conv_boundsN hl.amm).2.1
have hL := (conv_boundsN ha).2.1
have hNL : N < 2 ^ (52 * l.L) := Nat.lt_of_lt_of_le hNlt (AmmSym.pow2_le hL)
refine ⟨fun j hj => by simp only [limb]; rw [hN j hj]; exact limbN_lt64 _ _, val52_of hN hNL,
fun t ht => hk t ht, Nat.mod_lt _ (by decide), ?_, ?_⟩
Expand All @@ -379,6 +380,14 @@ theorem mod_of (hl : PubOk l) {m : Mem} {B : Addr} {N : Nat} {minv : BitVec 64}
exact AmmSym.pow2_le (by omega_arith)
omega_arith

/-- The modulus' limbs and `k₀` as `setup` leaves them. -/
theorem mod_of (hl : PubOk l) {m : Mem} {B : Addr} {N : Nat} {minv : BitVec 64}
(hN : ∀ j < l.L, word m B (0 + l.off j) = BitVec.ofNat 64 (limbN N j)) (hNlt : N < 2 ^ (64 * l.W))
(hk : ∀ i < 4, word m B (l.NB + 8 * i) = BitVec.ofNat 64 (minv.toNat % 2 ^ 52))
(hinv : (N % 2 ^ 64 * minv.toNat + 1) % 2 ^ 64 = 0) : Mod l m B N (minv.toNat % 2 ^ 52) :=
have a := hl.area
mod_ofN hl.amm a.2.2.2.2.2.2.1 a.2.2.2.2.2.2.2.1 a.2.2.2.2.2.2.2.2.2.2.1 hN hNlt hk hinv

/-- A change within ranges that a list of ranges covers. -/
theorem Frm.cover {P : Addr} {rs rs' : List (Nat × Nat)} {m m' : Mem} (h : Frm P rs m m')
(hc : ∀ x : Nat, (∀ r ∈ rs', x < r.1 ∨ r.1 + r.2 ≤ x) → ∀ r ∈ rs, x < r.1 ∨ r.1 + r.2 ≤ x) :
Expand Down
16 changes: 10 additions & 6 deletions lean/VerifiedGarbage/Proof/Bignum/X86_64/Ifma/PubVec.lean
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ variable {l : VG.Impl.Rsa.X86_64.CrtIfma.Lay}
/-- The public operation's layouts. -/
def PubOk (l : VG.Impl.Rsa.X86_64.CrtIfma.Lay) : Prop := l = pay2048 ∨ l = pay3072 ∨ l = pay4096

theorem PubOk.amm (hl : PubOk l) : AmmOk l := .inr hl
theorem PubOk.amm (hl : PubOk l) : AmmOk l :=
.inr (hl.elim .inl fun h => .inr (h.elim .inl fun h => .inr (.inl h)))

theorem PubOk.nl (hl : PubOk l) : l.nl = 1 := by rcases hl with rfl | rfl | rfl <;> rfl

Expand Down Expand Up @@ -172,12 +173,15 @@ theorem Mod.of_outside (hl : PubOk l) {m m' : Mem} {B : Addr} {M k n : Nat} (h :
· rw [← h.val]; exact lval_congr hlimb
· rw [f.word (by simp only [Lay.oK0]; omega_arith) (by simp only [Lay.oK0]; omega_arith)]; exact h.k0 t ht

theorem OutN.one (hl : PubOk l) {B : Addr} {o n : Nat} {m m' : Mem} (h : OutN l B o n m m') :
theorem OutN.one' (hn : l.nl = 1) {B : Addr} {o n : Nat} {m m' : Mem} (h : OutN l B o n m m') :
Outside B o n m m' := fun x hx => h x fun p hp => by
rw [hl.nl] at hp
rw [hn] at hp
obtain rfl : p = 0 := by omega_arith
simpa only [Nat.mul_zero, Nat.zero_add] using hx

theorem OutN.one (hl : PubOk l) {B : Addr} {o n : Nat} {m m' : Mem} (h : OutN l B o n m m') :
Outside B o n m m' := OutN.one' hl.nl h

/-- `ammMul` with `r8`, `r9` at offsets `a`, `b` of `B = rbx`, into `A`. -/
theorem core_ok (hl : PubOk l) {s : State} {B : Addr} {M k a b : Nat} (hB : s.gpr .rbx = B)
(r8 : s.gpr .r8 = off B a) (r9 : s.gpr .r9 = off B b) (r11 : s.gpr .r11 = off B (oA l))
Expand Down Expand Up @@ -237,7 +241,7 @@ theorem inc_ok {s : State} {c : Nat} (hc : c < 20) (h13 : s.gpr .r13 = BitVec.of
fun s' ⟨⟨a, b, c, d⟩, k⟩ => ⟨a, b, k, c, d⟩

/-- `ld52 j o`: array `j`'s `W` words, at `rdi + arr j`, into the limbs at `B + o`. -/
theorem ld52_ok (hl : PubOk l) {s : State} {B H : Addr} {j o : Nat} (hB : s.gpr .rbx = B) (hdi : s.gpr .rdi = H)
theorem ld52_ok (hl : AmmOk l) {s : State} {B H : Addr} {j o : Nat} (hB : s.gpr .rbx = B) (hdi : s.gpr .rdi = H)
(h12 : s.gpr .r12 = mask52) (hj : arr l j < 2 ^ 31)
(hrd : ∀ i < l.W, InRegions (s.rd ++ s.wr) (off H (arr l j) + BitVec.ofNat 64 (8 * i)) 8)
(hwr : ∀ q < l.L, InRegions s.wr (off B (o + l.off q)) 8) (ho : o + l.NB < 2 ^ 31)
Expand All @@ -254,7 +258,7 @@ theorem ld52_ok (hl : PubOk l) {s : State} {B H : Addr} {j o : Nat} (hB : s.gpr
simp only [List.cons_append, List.nil_append]
xrun [hdi, hB, se_ofNat hj, se_ofNat (show o < 2 ^ 31 by omega_arith)]
all_goals rfl) rfl) fun t ⟨⟨hsi, h11, hm, hx⟩, kt⟩ => ?_
refine WP.mono (to52_ok hl.amm hsi h11 ((kt.gpr (by decide)).trans h12)
refine WP.mono (to52_ok hl hsi h11 ((kt.gpr (by decide)).trans h12)
(fun i hi => by rw [kt.2.1, kt.2.2]; exact hrd i hi)
(fun q hq => by rw [kt.2.2, AmmSym.off_add]; exact hwr q hq)
(fun m m' f => hsep m m' (AmmSym.Outside.rebase f (by omega_arith) (by omega_arith))))
Expand Down Expand Up @@ -360,7 +364,7 @@ theorem sel_ok (hl : PubOk l) {t₀ t : State} {B H X : Addr} {M k R0 c : Nat} (
rw [Nat.zero_add]; exact e.sepX _ _ (h.frame.mono (Nat.le_refl _) (by omega_arith)) i hi
rw [WP.block_append_iff]
have hXv' : wv t₂.mem (off H (arr l aX)) 0 l.W = wv t₀.mem X 0 l.W := by rw [← e.hX]; exact hXv
refine WP.mono (ld52_ok hl (s := t₂) (B := B) (H := H) (j := aX) (o := oB l)
refine WP.mono (ld52_ok hl.amm (s := t₂) (B := B) (H := H) (j := aX) (o := oB l)
(by rw [g₂, g₁]; exact h.rbx) (by rw [g₂, g₁]; exact h.rdi) (by rw [g₂, g₁]; exact h.r12) e.hx
(fun i hi => by rw [rd₂, wr₂, rd₁, wr₁, h.keep.2.1, h.keep.2.2, ← e.hX]; exact e.hrd i hi)
(fun q hq => by rw [wr₂, wr₁]; exact hs.st (show oB l + l.off q + 8 ≤ _ by have := hF q hq; omega_arith))
Expand Down
Loading
Loading