aboutsummaryrefslogtreecommitdiff
path: root/src/Experiments/Ed25519.v
blob: c09ee445e1c7cf442b5a4c62c4a4b732a428649e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
Require Import Crypto.EdDSARepChange.
Require Import Crypto.Spec.Ed25519.
Require Import Crypto.Util.Decidable.
Require Crypto.Specific.GF25519.
Require Crypto.CompleteEdwardsCurve.ExtendedCoordinates.
Require Crypto.Encoding.PointEncoding.
Import Morphisms.

Context {H: forall n : nat, Word.word n -> Word.word (b + b)}.
Context {feSign : GF25519.fe25519 -> bool}.

Definition a : GF25519.fe25519 :=
  Eval vm_compute in ModularBaseSystem.encode a.
Definition d : GF25519.fe25519 :=
  Eval vm_compute in ModularBaseSystem.encode d.

Lemma phi_a : ModularBaseSystem.eq (ModularBaseSystem.encode Ed25519.a) a.
Proof. reflexivity. Qed.
Lemma phi_d : ModularBaseSystem.eq (ModularBaseSystem.encode Ed25519.d) d.
Proof. vm_decide_no_check. Qed.

Let Erep := (@ExtendedCoordinates.Extended.point
         GF25519.fe25519
         ModularBaseSystem.eq
         GF25519.zero_
         GF25519.one_
         GF25519.add
         GF25519.mul
         ModularBaseSystem.div
         a
         d
      ).


Global Instance Proper_onCurve {F eq one add mul a d} :
  Proper (Tuple.fieldwise (n := 2) eq==> iff) (@Pre.onCurve F eq one add mul a d) | 0.
Admitted.

Definition coord_to_extended (xy :  GF25519.fe25519 * GF25519.fe25519) : @Pre.onCurve _ (@ModularBaseSystem.eq GF25519.modulus
                           GF25519.params25519)
                        (@ModularBaseSystem.one GF25519.modulus
                           GF25519.params25519) GF25519.add
                        GF25519.mul a d xy -> Erep.
Proof.
  intros.
  destruct xy as [X Y].
  exists (X, Y, GF25519.one_, GF25519.mul X Y).
  repeat split.
  + cbv [ModularBaseSystem.div].
    rewrite !(@Algebra.Field.div_one _ _ _ _ _ _ _ _ _ _ (PrimeFieldTheorems.F.field_modulo GF25519.modulus)).
    match goal with H: Pre.onCurve (?X1, ?Y1) |- Pre.onCurve (?X2, ?Y2) =>
                    assert (Tuple.fieldwise (n := 2) ModularBaseSystem.eq (X2, Y2) (X1, Y1)) by (econstructor; cbv [fst snd Tuple.fieldwise' ModularBaseSystem.eq]; apply ModularBaseSystemProofs.encode_rep) end.
    pose proof @Proper_onCurve.
    cbv [Proper respectful] in *.
    match goal with H: Tuple.fieldwise _ ?x ?y, H1: Pre.onCurve ?y |- _  =>
                    pose proof (proj2
                                  (@Proper_onCurve _
                                  ModularBaseSystem.eq
                                  GF25519.one_
                                  GF25519.add
                                  GF25519.mul
                                  a d x y H) H1) end.
    assumption.
  + intro A. symmetry in A.
    apply (Algebra.field_is_zero_neq_one (field := GF25519.field25519)).
    rewrite GF25519.one_subst, GF25519.zero_subst.
    auto.
  + pose proof GF25519.field25519.
    rewrite (Algebra.left_identity (id := GF25519.one_)).
    reflexivity.
Defined.

Definition extended_to_coord (P : Erep) : (GF25519.fe25519 * GF25519.fe25519) :=
  CompleteEdwardsCurve.E.coordinates (ExtendedCoordinates.Extended.to_twisted P).

Lemma encode_eq_iff :  forall x y : ModularArithmetic.F.F GF25519.modulus,
                    ModularBaseSystem.eq
                      (ModularBaseSystem.encode x)
                      (ModularBaseSystem.encode y) <->  x = y.
Proof.
  intros.
  cbv [ModularBaseSystem.eq].
  rewrite !ModularBaseSystemProofs.encode_rep.
  reflexivity.
Qed.

Let EToRep := PointEncoding.point_phi
      (Kfield := GF25519.field25519)
      (phi_homomorphism := GF25519.homomorphism_F25519)
      (Kpoint := Erep)
      (phi_a := phi_a)
      (phi_d := phi_d)
      (Kcoord_to_point := coord_to_extended)
      (phi_bijective := encode_eq_iff)
.

Let ZNWord sz x := Word.NToWord sz (BinInt.Z.to_N x).

Definition feEnc (x : GF25519.fe25519) : Word.word 255 := 
  let '(x0, x1, x2, x3, x4, x5, x6, x7) :=
      (GF25519.pack x) in
  Word.combine (ZNWord 32 x0)
    (Word.combine (ZNWord 32 x1)
      (Word.combine (ZNWord 32 x2)
        (Word.combine (ZNWord 32 x3)
          (Word.combine (ZNWord 32 x4)
            (Word.combine (ZNWord 32 x5)
              (Word.combine (ZNWord 32 x6) (ZNWord 31 x7))))))). 

Let ERepEnc :=
  (PointEncoding.Kencode_point
         (Ksign := feSign)
         (Kenc := feEnc) 
         (Kpoint := Erep)
         (Kpoint_to_coord :=  fun P => CompleteEdwardsCurve.E.coordinates
                                (ExtendedCoordinates.Extended.to_twisted P))
  ).

Let S2Rep := fun (x : ModularArithmetic.F.F l) =>
               Tuple.map (ZNWord 32)
               (Tuple.from_list_default (BinInt.Z.of_nat 0) 8
                  (Pow2Base.encodeZ
                  (List.repeat (BinInt.Z.of_nat 32) 8)
                  (ModularArithmetic.F.to_Z x))).
               



Check @sign_correct
      (* E := *) E
      (* Eeq := *) CompleteEdwardsCurveTheorems.E.eq
      (* Eadd := *) CompleteEdwardsCurve.E.add
      (* Ezero := *) CompleteEdwardsCurve.E.zero
      (* Eopp := *) CompleteEdwardsCurveTheorems.E.opp
      (* EscalarMult := *) CompleteEdwardsCurve.E.mul
      (* b := *) b
      (* H := *) H
      (* c := *) c
      (* n := *) n
      (* l := *) l
      (* B := *) B
      (* Eenc := *) Eenc
      (* Senc := *) Senc
      (* prm := *) ed25519
      (* Erep := *) Erep
      (* ErepEq := *) ExtendedCoordinates.Extended.eq
      (* ErepAdd := *) ExtendedCoordinates.Extended.add
      (* ErepId := *) ExtendedCoordinates.Extended.zero
      (* ErepOpp := *) ExtendedCoordinates.Extended.opp
      (* Agroup := *) ExtendedCoordinates.Extended.extended_group
      (* EToRep := *) EToRep
      (* ERepEnc := *) ERepEnc
      (* ERepEnc_correct := *) PointEncoding.Kencode_point_correct
      (* Proper_ERepEnc := *) PointEncoding.Proper_Kencode_point
      (* SRep := *) (Tuple.tuple (Word.word 32) 8)
      (* SRepEq := *) (Tuple.fieldwise Logic.eq)
      (* H0 := *) Tuple.Equivalence_fieldwise
      (* S2Rep := *) S2Rep
      (* SRepDecModL := *) _
      (* SRepDecModL_correct := *) _
      (* SRepERepMul := *) _
      (* SRepERepMul_correct := *) _
      (* Proper_SRepERepMul := *) _
      (* SRepEnc := *) _
      (* SRepEnc_correct := *) _
      (* Proper_SRepEnc := *) _
      (* SRepAdd := *) _
      (* SRepAdd_correct := *) _
      (* Proper_SRepAdd := *) _
      (* SRepMul := *) _
      (* SRepMul_correct := *) _
      (* Proper_SRepMul := *) _
      (* ErepB := *) _
      (* ErepB_correct := *) _
      (* wbRepKeepLow := *) _
      (* wbRepClearLow := *) _
      (* wbRepSetBit := *) _
      (* wbRepSetBit_correct := *) _
      (* wbRepClearLow_correct := *) _
      (* wbRepKeepLow_correct := *) _
      (* SRepDecModLShort := *) _
      (* SRepDecModLShort_correct := *) _
      .


Check verify_correct.