aboutsummaryrefslogtreecommitdiff
path: root/src/Assembly/GF25519.v
blob: 081ddc00844c27dd9aae5da8339af727a0080768 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
Require Import Crypto.Assembly.Pipeline.

Require Import Crypto.Spec.ModularArithmetic.
Require Import Crypto.ModularArithmetic.ModularBaseSystem.
Require Import Crypto.Specific.GF25519.
Require Import Crypto.Util.Tuple.

Module GF25519.
  Definition bits: nat := 32.
  Definition width: Width bits := W32.

  Fixpoint makeBoundList {n} k (b: @WordRangeOpt n) :=
    match k with
    | O => nil
    | S k' => cons b (makeBoundList k' b)
    end.

  Section DefaultBounds.
    Import ListNotations.
    Local Notation rr exp := (makeRange 0 (2^exp + 2^exp/10)%Z).

    Definition feBound: list (@WordRangeOpt bits) :=
      [rr 26; rr 27; rr 26; rr 27; rr 26;
       rr 27; rr 26; rr 27; rr 26; rr 27].
  End DefaultBounds.

  Definition FE: type.
  Proof.
    let T := eval vm_compute in fe25519 in
    let t := HL.reify_type T in
    exact t.
  Defined.

  Definition liftFE {T} (F: @interp_type Z FE -> T) :=
    fun (a b c d e f g h i j: Z) => F (a, b, c, d, e, f, g, h, i, j).

  Module AddExpr <: Expression.
    Definition bits: nat := bits.
    Definition inputs: nat := 20.
    Definition width: Width bits := width.
    Definition ResultType := FE.
    Definition inputUpperBounds: list (@WordRangeOpt bits) := feBound ++ feBound.

    Definition ge25519_add_expr :=
        Eval cbv beta delta [fe25519 add mul sub Let_In] in add.

    Definition ge25519_add' (P Q: @interp_type Z FE) :
        { r: @HL.expr Z (@interp_type Z) FE
        | HL.interp (E := ZEvaluable) (t := FE) r = ge25519_add_expr P Q }.
    Proof.
        vm_compute in P, Q; repeat
        match goal with
        | [x:?T |- _] =>
            lazymatch T with
            | Z => fail
            | prod _ _ => destruct x
            | _ => clear x
            end
        end.

        eexists.
        cbv beta delta [ge25519_add_expr].

        let R := HL.rhs_of_goal in
        let X := HL.reify (@interp_type Z) R in
        transitivity (HL.interp (E := ZEvaluable) (t := ResultType) X);
        [reflexivity|].

        cbv iota beta delta [
            interp_type interp_binop HL.interp
            Z.land ZEvaluable eadd esub emul eshiftr eand].
        reflexivity.
    Defined.

    Definition ge25519_add (P Q: @interp_type Z ResultType) :=
        proj1_sig (ge25519_add' P Q).

    Definition hlProg'': NAry 20 Z (@HL.expr Z (@interp_type Z) ResultType) :=
        liftFE (fun p => (liftFE (fun q => ge25519_add p q))).

    Definition hlProg': NAry 20 Z (@HL.expr Z (@LL.arg Z Z) ResultType).
        refine (liftN (HLConversions.mapVar _ _) hlProg''); intro t;
        [ refine LL.uninterp_arg | refine LL.interp_arg ].
    Defined.

    Definition hlProg: NAry 20 Z (@HL.expr Z (@LL.arg Z Z) ResultType) :=
      Eval vm_compute in hlProg'.
  End AddExpr.

  Module SubExpr <: Expression.
    Definition bits: nat := bits.
    Definition inputs: nat := 20.
    Definition width: Width bits := width.
    Definition ResultType := FE.
    Definition inputUpperBounds: list (@WordRangeOpt bits) := feBound ++ feBound.

    Definition ge25519_sub_expr :=
        Eval cbv beta delta [fe25519 add mul sub Let_In] in sub.

    Definition ge25519_sub' (P Q: @interp_type Z FE) :
        { r: @HL.expr Z (@interp_type Z) FE
        | HL.interp (E := ZEvaluable) (t := FE) r = ge25519_sub_expr P Q }.
    Proof.
        vm_compute in P, Q; repeat
        match goal with
        | [x:?T |- _] =>
            lazymatch T with
            | Z => fail
            | prod _ _ => destruct x
            | _ => clear x
            end
        end.

        eexists.
        cbv beta delta [ge25519_sub_expr].

        let R := HL.rhs_of_goal in
        let X := HL.reify (@interp_type Z) R in
        transitivity (HL.interp (E := ZEvaluable) (t := ResultType) X);
        [reflexivity|].

        cbv iota beta delta [
            interp_type interp_binop HL.interp
            Z.land ZEvaluable eadd esub emul eshiftr eand].
        reflexivity.
    Defined.

    Definition ge25519_sub (P Q: @interp_type Z ResultType) :=
        proj1_sig (ge25519_sub' P Q).

    Definition hlProg'': NAry 20 Z (@HL.expr Z (@interp_type Z) ResultType) :=
        liftFE (fun p => (liftFE (fun q => ge25519_sub p q))).

    Definition hlProg': NAry 20 Z (@HL.expr Z (@LL.arg Z Z) ResultType).
        refine (liftN (HLConversions.mapVar _ _) hlProg''); intro t;
        [ refine LL.uninterp_arg | refine LL.interp_arg ].
    Defined.

    Definition hlProg: NAry 20 Z (@HL.expr Z (@LL.arg Z Z) ResultType) :=
      Eval vm_compute in hlProg'.
  End SubExpr.

  Module MulExpr <: Expression.
    Definition bits: nat := bits.
    Definition inputs: nat := 20.
    Definition width: Width bits := width.
    Definition ResultType := FE.
    Definition inputUpperBounds: list (@WordRangeOpt bits) := feBound ++ feBound.

    Definition ge25519_mul_expr :=
        Eval cbv beta delta [fe25519 add mul sub Let_In] in mul.

    Definition ge25519_mul' (P Q: @interp_type Z FE) :
        { r: @HL.expr Z (@interp_type Z) FE
        | HL.interp (E := ZEvaluable) (t := FE) r = ge25519_mul_expr P Q }.
    Proof.
        vm_compute in P, Q; repeat
        match goal with
        | [x:?T |- _] =>
            lazymatch T with
            | Z => fail
            | prod _ _ => destruct x
            | _ => clear x
            end
        end.

        eexists.
        cbv beta delta [ge25519_mul_expr].

        let R := HL.rhs_of_goal in
        let X := HL.reify (@interp_type Z) R in
        transitivity (HL.interp (E := ZEvaluable) (t := ResultType) X);
        [reflexivity|].

        cbv iota beta delta [
            interp_type interp_binop HL.interp
            Z.land ZEvaluable eadd esub emul eshiftr eand].
        reflexivity.
    Defined.

    Definition ge25519_mul (P Q: @interp_type Z ResultType) :=
        proj1_sig (ge25519_mul' P Q).

    Definition hlProg'': NAry 20 Z (@HL.expr Z (@interp_type Z) ResultType) :=
        liftFE (fun p => (liftFE (fun q => ge25519_mul p q))).

    Definition hlProg': NAry 20 Z (@HL.expr Z (@LL.arg Z Z) ResultType).
        refine (liftN (HLConversions.mapVar _ _) hlProg''); intro t;
        [ refine LL.uninterp_arg | refine LL.interp_arg ].
    Defined.

    Definition hlProg: NAry 20 Z (@HL.expr Z (@LL.arg Z Z) ResultType) :=
      Eval vm_compute in hlProg'.
  End MulExpr.

  Module OppExpr <: Expression.
    Definition bits: nat := bits.
    Definition inputs: nat := 10.
    Definition width: Width bits := width.
    Definition ResultType := FE.
    Definition inputUpperBounds: list (@WordRangeOpt bits) := feBound.

    Definition ge25519_opp_expr :=
        Eval cbv beta delta [fe25519 add mul sub opp Let_In] in opp.

    Definition ge25519_opp' (P: @interp_type Z FE) :
        { r: @HL.expr Z (@interp_type Z) FE
        | HL.interp (E := ZEvaluable) (t := FE) r = ge25519_opp_expr P }.
    Proof.
        vm_compute in P; repeat
        match goal with
        | [x:?T |- _] =>
            lazymatch T with
            | Z => fail
            | prod _ _ => destruct x
            | _ => clear x
            end
        end.

        eexists.
        cbv beta delta [ge25519_opp_expr zero_].

        let R := HL.rhs_of_goal in
        let X := HL.reify (@interp_type Z) R in
        transitivity (HL.interp (E := ZEvaluable) (t := ResultType) X);
        [reflexivity|].

        cbv iota beta delta [
            interp_type interp_binop HL.interp
            Z.land ZEvaluable eadd esub emul eshiftr eand].
        reflexivity.
    Defined.

    Definition ge25519_opp (P: @interp_type Z ResultType) :=
        proj1_sig (ge25519_opp' P).

    Definition hlProg'': NAry 10 Z (@HL.expr Z (@interp_type Z) ResultType) :=
        liftFE (fun p => ge25519_opp p).

    Definition hlProg': NAry 10 Z (@HL.expr Z (@LL.arg Z Z) ResultType).
        refine (liftN (HLConversions.mapVar _ _) hlProg''); intro t;
        [ refine LL.uninterp_arg | refine LL.interp_arg ].
    Defined.

    Definition hlProg: NAry 10 Z (@HL.expr Z (@LL.arg Z Z) ResultType) :=
      Eval vm_compute in hlProg'.
  End OppExpr.

  Module Add := Pipeline AddExpr.
  Module Sub := Pipeline SubExpr.
  Module Mul := Pipeline MulExpr.
  Module Opp := Pipeline OppExpr.
End GF25519.

Extraction "GF25519Add" GF25519.Add.
Extraction "GF25519Sub" GF25519.Sub.
Extraction "GF25519Mul" GF25519.Mul.
Extraction "GF25519Opp" GF25519.Opp.