Skip to content

Commit d76e956

Browse files
committed
is_zero done
1 parent 51f73f2 commit d76e956

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

OperationFramework/IsZeroOperation.lean

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,46 @@ structure IsZeroOperation (T : Type) where
77
namespace IsZeroOperation
88

99
/-- `IsZeroOperation` should result in wrapping addition of the outputs. -/
10-
def spec (cols : IsZeroOperation (Fin p)) (a : Fin p) : Prop :=
11-
a = 0 ↔ cols.result = 1
12-
13-
/-- Constraints on `IsZeroOperation` as extracted from the source code. -/
14-
def extractedConstraints (cols : IsZeroOperation (Fin p)) (a : Fin p) : Prop :=
15-
sorry
10+
def spec (cols : IsZeroOperation (Fin p)) (a : Fin p) (is_real : Fin p) : Prop :=
11+
is_real ≠ 0 → (a = 0 ↔ cols.result = 1)
12+
13+
/-- Constraints on `IsZeroOperation` as extracted from the source code:
14+
Asserting expr 8: `(is_real * ((1 - (cols.inverse * a)) - cols.result))`
15+
Asserting expr 11: `(is_real * (cols.result * (cols.result - 1)))`
16+
Asserting expr 13: `(is_real * (cols.result * a))` -/
17+
def extractedConstraints (cols : IsZeroOperation (Fin p))
18+
(a : Fin p) (is_real : Fin p) : Prop :=
19+
(is_real * ((1 - (cols.inverse * a)) - cols.result)) = 0
20+
(is_real * (cols.result * (cols.result - 1))) = 0
21+
(is_real * (cols.result * a)) = 0
1622

1723
/-- Cleaned up representation of the `IsZeroOperation` constraints. -/
18-
def idealConstraints (cols : IsZeroOperation (Fin p)) (a : Fin p) : Prop :=
19-
let is_zero := 1 - cols.inverse * a
20-
is_zero - cols.result = 0
21-
cols.result * (cols.result - 1) = 0
22-
(cols.result ≠ 0 → a = 0)
24+
def idealConstraints (cols : IsZeroOperation (Fin p))
25+
(a : Fin p) (is_real : Fin p) : Prop :=
26+
is_real ≠ 0 → (
27+
1 - (cols.inverse * a) = cols.result ∧
28+
(cols.result = 0 ∨ cols.result = 1) ∧
29+
(cols.result = 0 ∨ a = 0)
30+
)
2331

2432
/-- The idealized constraints are logically equivalent to the extracted ones. -/
25-
lemma extractedConstraints_iff_idealConstraints
26-
(cols : IsZeroOperation (Fin p)) (a : Fin p) :
27-
cols.extractedConstraints a ↔ cols.idealConstraints a := by
28-
sorry
29-
30-
/-- The extracted constraints on `AddOperation` imply the spec. -/
31-
theorem correct [Fact (Nat.Prime p)]
32-
(cols : IsZeroOperation (Fin p)) (a : Fin p) :
33-
cols.extractedConstraints a → cols.spec a := by
33+
lemma extractedConstraints_iff_idealConstraints [Fact (Nat.Prime p)]
34+
(cols : IsZeroOperation (Fin p)) (a : Fin p) (is_real : Fin p) :
35+
cols.extractedConstraints a is_real ↔ cols.idealConstraints a is_real := by
36+
by_cases hreal : is_real = 0
37+
· simp [extractedConstraints, idealConstraints, hreal]
38+
· simp [extractedConstraints, idealConstraints, hreal, sub_eq_zero]
39+
40+
/-- The extracted constraints on `IsZeroOperation` imply the spec. -/
41+
theorem correct [Fact (Nat.Prime p)] (cols : IsZeroOperation (Fin p))
42+
(a : Fin p) (is_real : Fin p) :
43+
cols.extractedConstraints a is_real → cols.spec a is_real := by
3444
simp [extractedConstraints_iff_idealConstraints, idealConstraints, spec,
3545
mul_eq_zero, sub_eq_zero]
36-
refine fun h h' h'' => ⟨fun h' => ?_, fun h' => ?_⟩
37-
<;> simp [h', eq_comm (a := cols.result)] at *
38-
<;> assumption
46+
refine fun h hreal => ⟨fun h' => ?_, fun h' => ?_⟩
47+
· simp [h', hreal, eq_comm (a := cols.result)] at *
48+
exact h.1
49+
· simp [h', hreal, eq_comm (a := cols.result)] at *
50+
exact h.2
3951

4052
end IsZeroOperation

0 commit comments

Comments
 (0)