Skip to content

Commit 2a92dd8

Browse files
committed
working add4
1 parent 86b9f25 commit 2a92dd8

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

OperationFramework/Add4Operation.lean

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This is more explicit than saying that `(a.toNat + b.toNat) % 2^32 = cols.toNat`
99
def spec (cols : Add4Operation (Fin p))
1010
(a b c d : Word (Fin p)) : Prop :=
1111
a.isUInt32 → b.isUInt32 → c.isUInt32 → d.isUInt32 →
12-
(a.toNat + b.toNat) % 2^32 = cols.toNat
12+
(a.toNat + b.toNat + c.toNat + d.toNat) % 2^32 = cols.toNat
1313

1414
-- /-- Constraints on `AddOperation` as extracted from the source code. -/
1515
-- def extractedConstraints (cols : AddOperation (Fin p))
@@ -23,10 +23,10 @@ def spec (cols : Add4Operation (Fin p))
2323

2424
/-- Cleaned up representation of the `AddOperation` constraints. -/
2525
def idealConstraints (cols : Add4Operation (Fin p))
26-
(a b c d : Word (Fin p)) : Prop :=
26+
(aw bw cw dw : Word (Fin p)) : Prop :=
2727
let carry0 := 0
28-
let carry1 := (a[0] + b[0] - cols[0] + carry0) * baseInv
29-
let carry2 := (a[1] + b[1] - cols[1] + carry1) * baseInv
28+
let carry1 := (aw[0] + bw[0] + cw[0] + dw[0] - cols[0] + carry0) * baseInv
29+
let carry2 := (aw[1] + bw[1] + cw[1] + dw[1] - cols[1] + carry1) * baseInv
3030
carry1 * (carry1 - 1) = 0-- isBool check
3131
carry2 * (carry2 - 1) = 0-- isBool check
3232
cols.isUInt32 -- slice range checks
@@ -37,27 +37,23 @@ def idealConstraints (cols : Add4Operation (Fin p))
3737
-- cols.extractedConstraints a b ↔ cols.idealConstraints a b := by
3838
-- simp [extractedConstraints, idealConstraints, Word.isUInt32]
3939

40-
-- /-- The extracted constraints on `AddOperation` imply the spec. -/
41-
-- theorem correct [Fact (Nat.Prime p)]
42-
-- (cols : AddOperation (Fin p))
43-
-- (a : Word (Fin p)) (b : Word (Fin p)) :
44-
-- cols.extractedConstraints a b → cols.spec a b := by
45-
-- -- Unfold the definitions of constraints and spec
46-
-- rw [extractedConstraints_iff_idealConstraints, idealConstraints, spec]
47-
-- simp [sub_eq_zero, mul_eq_zero]
48-
-- -- Introduce all of the hypothesis from the constraints
49-
-- intros h1 h2 hcols_u32 ha_u32 hb_u32
50-
-- -- Simplify the addition using the fact both limbs are u16 vals
51-
-- erw [Word.add_mod_of_isUInt32_of_isUInt32 ha_u32 hb_u32, Word.toNat_add_toNat]
52-
-- -- Split into two cases depending on if there was overflow
53-
-- split_ifs with h_overflow
54-
-- -- Split into cases depending on if the first limb addition had a carry
55-
-- <;> cases h1 with | inl h1 => ?_ | inr h1 => ?_
56-
-- <;> {
57-
-- rw [h1] at h2
58-
-- simp [Fin.add_def, Fin.sub_def, Fin.ext_iff, p, Word.toNat,
59-
-- Word.isUInt32] at *
60-
-- omega
61-
-- }
40+
/-- The extracted constraints on `AddOperation` imply the spec. -/
41+
theorem correct [Fact (Nat.Prime p)]
42+
(cols : Add4Operation (Fin p))
43+
(aw bw cw dw: Word (Fin p)) :
44+
cols.idealConstraints aw bw cw dw → cols.spec aw bw cw dw := by
45+
-- Unfold the definitions of constraints and spec
46+
rw [idealConstraints, spec]
47+
simp [sub_eq_zero, mul_eq_zero]
48+
49+
-- Introduce all of the hypothesis from the constraints
50+
intros h1 h2 hcols_u32 ha_u32 hb_u32
51+
52+
-- Split on if the lower limb leads to a carry
53+
cases h1 with | inl h1 => ?_ | inr h1 => ?_
54+
all_goals
55+
{ rw [h1] at h2
56+
simp [Fin.add_def, Fin.sub_def, Fin.ext_iff, p, Word.toNat, Word.isUInt32] at *
57+
omega }
6258

6359
end Add4Operation

OperationFramework/AddOperation.lean

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,15 @@ theorem correct [Fact (Nat.Prime p)]
4242
-- Unfold the definitions of constraints and spec
4343
rw [extractedConstraints_iff_idealConstraints, idealConstraints, spec]
4444
simp [sub_eq_zero, mul_eq_zero]
45+
4546
-- Introduce all of the hypothesis from the constraints
4647
intros h1 h2 hcols_u32 ha_u32 hb_u32
4748

48-
by_cases h_overflow : a[0].val + b[0].val + base * (a[1].val + b[1].val) < 2 ^ 32
49-
-- Split into two cases depending on if there was overflow
50-
<;> cases h1 with | inl h1 => ?_ | inr h1 => ?_
51-
<;> {
52-
rw [h1] at h2
53-
simp [Fin.add_def, Fin.sub_def, Fin.ext_iff, p, Word.toNat,
54-
Word.isUInt32] at *
55-
omega
56-
}
49+
-- Split on if the lower limb leads to a carry
50+
cases h1 with | inl h1 => ?_ | inr h1 => ?_
51+
all_goals
52+
{ rw [h1] at h2
53+
simp [Fin.add_def, Fin.sub_def, Fin.ext_iff, p, Word.toNat, Word.isUInt32] at *
54+
omega }
5755

5856
end AddOperation

0 commit comments

Comments
 (0)