Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a6a4778

Browse files
committedMar 13, 2024·
Fixed gates
1 parent f7bd0d1 commit a6a4778

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed
 

‎ProvenZk/Gates.lean

+10-12
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ open BigOperators
66

77
namespace GatesDef
88
variable {N : Nat}
9-
-- variable [Fact (Nat.Prime N)]
109
def is_bool (a : ZMod N): Prop := (1-a)*a = 0
1110
def add (a b : ZMod N): ZMod N := a + b
1211
def mul_acc (a b c : ZMod N): ZMod N := a + (b * c)
1312
def neg (a : ZMod N): ZMod N := a * (-1)
1413
def sub (a b : ZMod N): ZMod N := a - b
1514
def mul (a b : ZMod N): ZMod N := a * b
16-
def div_unchecked [Fact (Nat.Prime N)] (a b out : ZMod N): Prop := (b ≠ 0 ∧ out*b = a) ∨ (a = 0 ∧ b = 0 ∧ out = 0)
17-
def div [Fact (Nat.Prime N)] (a b out : ZMod N): Prop := b ≠ 0 ∧ out*b = a
15+
def div_unchecked (a b out : ZMod N): Prop := (b ≠ 0 ∧ out*b = a) ∨ (a = 0 ∧ b = 0 ∧ out = 0)
16+
def div (a b out : ZMod N): Prop := b ≠ 0 ∧ out*b = a
1817
def inv (a out : ZMod N): Prop := a ≠ 0 ∧ out*a = 1
1918
def xor (a b out : ZMod N): Prop := is_bool a ∧ is_bool b ∧ out = a+b-a*b-a*b
2019
def or (a b out : ZMod N): Prop := is_bool a ∧ is_bool b ∧ out = a+b-a*b
@@ -28,16 +27,15 @@ def lookup (b0 b1 i0 i1 i2 i3 out : ZMod N): Prop :=
2827
-- however this doesn't guarantee that the number is unique.
2928
def cmp_8 (a b out : ZMod N): Prop :=
3029
∃z w: Fin (binary_length N), z.val % N = a.val ∧ w.val % N = b.val ∧
31-
((a = b ∧ out = 0) ∨
32-
(a.val < b.val ∧ out = -1) ∨
33-
(a.val > b.val ∧ out = 1))
30+
((z = w ∧ out = 0) ∨
31+
(z.val < w.val ∧ out = -1) ∨
32+
(z.val > w.val ∧ out = 1))
3433

3534
-- In gnark 9 the number is reduced to the smallest representation, ensuring it is unique.
3635
def cmp_9 (a b out : ZMod N): Prop :=
37-
∃z w: Fin (binary_length N), z.val = a.val ∧ w.val = b.val ∧
38-
((a = b ∧ out = 0) ∨
36+
(a = b ∧ out = 0) ∨
3937
(a.val < b.val ∧ out = -1) ∨
40-
(a.val > b.val ∧ out = 1))
38+
(a.val > b.val ∧ out = 1)
4139

4240
def is_zero (a out: ZMod N): Prop := (a ≠ 0 ∧ out = 0) ∨ (a = 0 ∧ out = 1)
4341
def eq (a b : ZMod N): Prop := a = b
@@ -80,7 +78,7 @@ structure Gates_base (α : Type) : Type where
8078
to_binary : α → (n : Nat) → Vector α n → Prop
8179
from_binary : Vector α d → α → Prop
8280

83-
def GatesGnark_8 (N : Nat) [Fact (Nat.Prime N)] : Gates_base (ZMod N) := {
81+
def GatesGnark8 (N : Nat) [Fact (Nat.Prime N)] : Gates_base (ZMod N) := {
8482
is_bool := GatesDef.is_bool,
8583
add := GatesDef.add,
8684
mul_acc := GatesDef.mul_acc,
@@ -104,8 +102,8 @@ def GatesGnark_8 (N : Nat) [Fact (Nat.Prime N)] : Gates_base (ZMod N) := {
104102
from_binary := GatesDef.from_binary
105103
}
106104

107-
def GatesGnark_9 (N : Nat) [Fact (Nat.Prime N)] : Gates_base (ZMod N) := {
108-
GatesGnark_8 N with
105+
def GatesGnark9 (N : Nat) [Fact (Nat.Prime N)] : Gates_base (ZMod N) := {
106+
GatesGnark8 N with
109107
cmp := GatesDef.cmp_9
110108
le := GatesDef.le_9
111109
}

0 commit comments

Comments
 (0)
Please sign in to comment.