Debian Codegraph @

src/Bedrock/Secp256k1/JacobianCoZ.v

raw
1 Require Import bedrock2.Array.
2 Require Import bedrock2.FE310CSemantics.
3 Require Import bedrock2.Loops.
4 Require Import bedrock2.Map.Separation.
5 Require Import bedrock2.Map.SeparationLogic.
6 Require Import bedrock2.NotationsCustomEntry.
7 Require Import bedrock2.ProgramLogic.
8 Require Import bedrock2.Scalars.
9 Require Import bedrock2.Semantics.
10 Require Import bedrock2.Syntax.
11 Require Import bedrock2.WeakestPrecondition.
12 Require Import bedrock2.WeakestPreconditionProperties.
13 Require Import bedrock2.ZnWords.
14 Require Import coqutil.Byte.
15 Require Import coqutil.Map.Interface.
16 Require Import coqutil.Map.Properties.
17 Require Import coqutil.Map.OfListWord.
18 From coqutil.Tactics Require Import Tactics letexists eabstract rdelta reference_to_string ident_of_string.
19 Require Import coqutil.Word.Bitwidth64.
20 Require Import coqutil.Word.Bitwidth.
21 Require Import coqutil.Word.Interface.
22 Require Import coqutil.Word.Properties.
23 Require Import Coq.Init.Byte.
24 Require Import Coq.Lists.List.
25 Require Import Coq.Strings.String.
26 Require Import Coq.ZArith.ZArith.
27 Require Import Crypto.Arithmetic.PrimeFieldTheorems.
28 Require Import Crypto.Bedrock.Field.Synthesis.New.WordByWordMontgomery.
29 Require Import Crypto.Bedrock.Group.ScalarMult.CSwap.
30 Require Import Crypto.Bedrock.Secp256k1.Field256k1.
31 Require Import Crypto.Bedrock.Specs.Field.
32 Require Import Crypto.Util.Decidable.
33 Require Import Curves.Weierstrass.Jacobian.Jacobian.
34 Require Import Curves.Weierstrass.Jacobian.CoZ.
35 Require Import Lia.
36 Require Crypto.Bedrock.Field.Synthesis.New.Signature.
37 Local Open Scope string_scope.
38 Local Open Scope Z_scope.
39 Import LittleEndianList.
40 Import ListNotations.
41 Import ProgramLogic.Coercions.
42 Import WeakestPrecondition.
43 Import BasicC64Semantics.
44
45 Local Existing Instance field_parameters.
46 Local Existing Instance frep256k1.
47 Local Existing Instance frep256k1_ok.
48
49 Definition secp256k1_jopp :=
50 func! (OX, OY, OZ, X, Y, Z) {
51 secp256k1_felem_copy(OX, X);
52 secp256k1_opp(OY, Y);
53 secp256k1_felem_copy(OZ, Z)
54 }.
55
56 Definition secp256k1_make_co_z :=
57 func! (OX, OY, X, Y, Z) {
58 stackalloc 32 as T;
59 secp256k1_square(T, Z);
60 secp256k1_mul(OX, X, T);
61 secp256k1_mul(T, T, Z);
62 secp256k1_mul(OY, Y, T)
63 }.
64
65 (* TODO: Can we optimize away the felem_copy? *)
66 Definition secp256k1_zaddu :=
67 func! (OX1, OY1, OX2, OY2, OZ, X1, Y1, X2, Y2, Z) {
68 stackalloc 32 as T1;
69 stackalloc 32 as T2;
70 stackalloc 32 as T4;
71 stackalloc 32 as T5;
72 stackalloc 32 as T6;
73 secp256k1_sub(T6, X1, X2); (* let t6 := t1 - t4 in *)
74 secp256k1_mul(OZ, Z, T6); (* let t3 := t3 * t6 in *)
75 secp256k1_square(T6, T6); (* let t6 := t6^2 in *)
76 secp256k1_mul(T1, X1, T6); (* let t1 := t1 * t6 in *)
77 secp256k1_mul(T6, T6, X2); (* let t6 := t6 * t4 in *)
78 secp256k1_sub(T5, Y1, Y2); (* let t5 := t2 - t5 in *)
79 secp256k1_square(T4, T5); (* let t4 := t5^2 in *)
80 secp256k1_sub(T4, T4, T1); (* let t4 := t4 - t1 in *)
81 secp256k1_sub(T4, T4, T6); (* let t4 := t4 - t6 in *)
82 secp256k1_sub(T6, T1, T6); (* let t6 := t1 - t6 in *)
83 secp256k1_mul(T2, Y1, T6); (* let t2 := t2 * t6 in *)
84 secp256k1_sub(T6, T1, T4); (* let t6 := t1 - t4 in *)
85 secp256k1_mul(T5, T5, T6); (* let t5 := t5 * t6 in *)
86 secp256k1_sub(T5, T5, T2); (* let t5 := t5 - t2 in *)
87 secp256k1_felem_copy(OX1, T4); (* ((t4, t5, t3), (t1, t2, t3)) *)
88 secp256k1_felem_copy(OY1, T5);
89 secp256k1_felem_copy(OX2, T1);
90 secp256k1_felem_copy(OY2, T2)
91 }.
92
93 (* TODO: Can we optimize away the felem_copy? *)
94 Definition secp256k1_zaddc :=
95 func! (OX1, OY1, OX2, OY2, OZ, X1, Y1, X2, Y2, Z) {
96 stackalloc 32 as T1;
97 stackalloc 32 as T2;
98 stackalloc 32 as T4;
99 stackalloc 32 as T5;
100 stackalloc 32 as T6;
101 secp256k1_sub(T6, X1, X2); (* let t6 := t1 - t4 in *)
102 secp256k1_mul(OZ, Z, T6); (* let t3 := t3 * t6 in *)
103 secp256k1_square(T6, T6); (* let t6 := t6^2 in *)
104 stackalloc 32 as T7;
105 secp256k1_mul(T7, X1, T6); (* let t7 := t1 * t6 in *)
106 secp256k1_mul(T6, T6, X2); (* let t6 := t6 * t4 in *)
107 secp256k1_add(T1, Y1, Y2); (* let t1 := t2 + t5 in *)
108 secp256k1_square(T4, T1); (* let t4 := t1^2 in *)
109 secp256k1_sub(T4, T4, T7); (* let t4 := t4 - t7 in *)
110 secp256k1_sub(T4, T4, T6); (* let t4 := t4 - t6 in *)
111 secp256k1_sub(T1, Y1, Y2); (* let t1 := t2 - t5 in *)
112 secp256k1_square(T1, T1); (* let t1 := t1^2 in *)
113 secp256k1_sub(T1, T1, T7); (* let t1 := t1 - t7 in *)
114 secp256k1_sub(T1, T1, T6); (* let t1 := t1 - t6 in *)
115 secp256k1_sub(T6, T6, T7); (* let t6 := t6 - t7 in *)
116 secp256k1_mul(T6, T6, Y1); (* let t6 := t6 * t2 in *)
117 secp256k1_sub(T2, Y1, Y2); (* let t2 := t2 - t5 in *)
118 secp256k1_add(T5, Y2, Y2); (* let t5 := t5 + t5 in *)
119 secp256k1_add(T5, T2, T5); (* let t5 := t2 + t5 in *)
120 secp256k1_sub(T7, T7, T4); (* let t7 := t7 - t4 in *)
121 secp256k1_mul(T5, T5, T7); (* let t5 := t5 * t7 in *)
122 secp256k1_add(T5, T5, T6); (* let t5 := t5 + t6 in *)
123 secp256k1_add(T7, T4, T7); (* let t7 := t4 + t7 in *)
124 secp256k1_sub(T7, T7, T1); (* let t7 := t7 - t1 in *)
125 secp256k1_mul(T2, T2, T7); (* let t2 := t2 * t7 in *)
126 secp256k1_add(T2, T2, T6); (* let t2 := t2 + t6 in *)
127 secp256k1_felem_copy(OX1, T1); (* ((t1, t2, t3), (t4, t5, t3)) *)
128 secp256k1_felem_copy(OY1, T2);
129 secp256k1_felem_copy(OX2, T4);
130 secp256k1_felem_copy(OY2, T5)
131 }.
132
133 (* Specialized with a=0 *)
134 Definition secp256k1_dblu :=
135 func! (OX1, OY1, OX2, OY2, OZ, X1, Y1) {
136 stackalloc 32 as T1;
137 stackalloc 32 as T2;
138 secp256k1_add(OZ, Y1, Y1); (* let t3 := t2 + t2 in *)
139 secp256k1_square(T2, Y1); (* let t2 := t2^2 in *)
140 stackalloc 32 as T4;
141 secp256k1_add(T4, X1, T2); (* let t4 := t1 + t2 in *)
142 secp256k1_square(T4, T4); (* let t4 := t4^2 in *)
143 stackalloc 32 as T5;
144 secp256k1_square(T5, X1); (* let t5 := t1^2 in *)
145 secp256k1_sub(T4, T4, T5); (* let t4 := t4 - t5 in *)
146 secp256k1_square(T2, T2); (* let t2 := t2^2 in *)
147 secp256k1_sub(T4, T4, T2); (* let t4 := t4 - t2 in *)
148 secp256k1_add(T1, T4, T4); (* let t1 := t4 + t4 in *)
149 (* secp256k1_add(T0, T0, T5); *) (* let t0 := t0 + t5 in *)
150 stackalloc 32 as T0;
151 secp256k1_felem_copy(T0, T5); (* a = 0 for secp256k1 *)
152 secp256k1_add(T5, T5, T5); (* let t5 := t5 + t5 in *)
153 secp256k1_add(T0, T0, T5); (* let t0 := t0 + t5 in *)
154 secp256k1_square(T4, T0); (* let t4 := t0^2 in *)
155 secp256k1_add(T5, T1, T1); (* let t5 := t1 + t1 in *)
156 secp256k1_sub(T4, T4, T5); (* let t4 := t4 - t5 in *)
157 secp256k1_add(T2, T2, T2);
158 secp256k1_add(T2, T2, T2);
159 secp256k1_add(T2, T2, T2); (* let t2 := 8 * t2 in *)
160 secp256k1_sub(T5, T1, T4); (* let t5 := t1 - t4 in *)
161 secp256k1_mul(T5, T5, T0); (* let t5 := t5 * t0 in *)
162 secp256k1_sub(T5, T5, T2); (* let t5 := t5 - t2 in *)
163 secp256k1_felem_copy(OX1, T4); (* ((t4, t5, t3), (t1, t2, t3)) *)
164 secp256k1_felem_copy(OY1, T5);
165 secp256k1_felem_copy(OX2, T1);
166 secp256k1_felem_copy(OY2, T2)
167 }.
168
169 (* TODO: it should be possible to not use any temp field registers *)
170 Definition secp256k1_tplu :=
171 func! (OX1, OY1, OX2, OY2, OZ, X1, Y1) {
172 stackalloc 32 as T1;
173 stackalloc 32 as T2;
174 stackalloc 32 as T3;
175 stackalloc 32 as T4;
176 stackalloc 32 as T5;
177 secp256k1_dblu(T1, T2, T3, T4, T5, X1, Y1);
178 secp256k1_zaddu(OX1, OY1, OX2, OY2, OZ, T3, T4, T1, T2, T5)
179 }.
180
181 Definition secp256k1_zdau :=
182 func! (X1, Y1, X2, Y2, Z) {
183 stackalloc 32 as T1;
184 stackalloc 32 as T2;
185 stackalloc 32 as T4;
186 stackalloc 32 as T5;
187 stackalloc 32 as T6;
188 secp256k1_sub(T6, X1, X2); (* let t6 := t1 - t4 in *)
189 stackalloc 32 as T7;
190 secp256k1_square(T7, T6); (* let t7 := t6^2 in *)
191 secp256k1_mul(T1, X1, T7); (* let t1 := t1 * t7 in *)
192 secp256k1_mul(T4, X2, T7); (* let t4 := t4 * t7 in *)
193 secp256k1_sub(T5, Y1, Y2); (* let t5 := t2 - t5 in *)
194 stackalloc 32 as T8;
195 secp256k1_sub(T8, T1, T4); (* let t8 := t1 - t4 in *)
196 secp256k1_mul(T2, Y1, T8); (* let t2 := t2 * t8 in *)
197 secp256k1_add(T2, T2, T2); (* let t2 := t2 + t2 in *)
198 secp256k1_square(T8, T5); (* let t8 := t5^2 in *)
199 secp256k1_sub(T4, T8, T4); (* let t4 := t8 - t4 in *)
200 secp256k1_sub(T4, T4, T1); (* let t4 := t4 - t1 in *)
201 secp256k1_sub(T4, T4, T1); (* let t4 := t4 - t1 in *)
202 secp256k1_add(T6, T4, T6); (* let t6 := t4 + t6 in *)
203 secp256k1_square(T6, T6); (* let t6 := t6^2 in *)
204 secp256k1_sub(T6, T6, T7); (* let t6 := t6 - t7 in *)
205 secp256k1_sub(T5, T5, T4); (* let t5 := t5 - t4 in *)
206 secp256k1_square(T5, T5); (* let t5 := t5^2 in *)
207 secp256k1_sub(T5, T5, T8); (* let t5 := t5 - t8 in *)
208 secp256k1_sub(T5, T5, T2); (* let t5 := t5 - t2 in *)
209 secp256k1_square(T7, T4); (* let t7 := t4^2 in *)
210 secp256k1_sub(T5, T5, T7); (* let t5 := t5 - t7 in *)
211 secp256k1_add(T8, T7, T7); (* let t8 := t7 + t7 in *)
212 secp256k1_add(T8, T8, T8); (* let t8 := t8 + t8 in *)
213 secp256k1_sub(T6, T6, T7); (* let t6 := t6 - t7 in *)
214 secp256k1_mul(Z, Z, T6); (* let t3 := t3 * t6 in *)
215 secp256k1_mul(T6, T1, T8); (* let t6 := t1 * t8 in *)
216 secp256k1_add(T1, T1, T4); (* let t1 := t1 + t4 in *)
217 secp256k1_mul(T8, T8, T1); (* let t8 := t8 * t1 in *)
218 secp256k1_add(T7, T2, T5); (* let t7 := t2 + t5 in *)
219 secp256k1_sub(T2, T5, T2); (* let t2 := t5 - t2 in *)
220 secp256k1_sub(T1, T8, T6); (* let t1 := t8 - t6 in *)
221 secp256k1_mul(T5, T5, T1); (* let t5 := t5 * t1 in *)
222 secp256k1_add(T6, T6, T8); (* let t6 := t6 + t8 in *)
223 secp256k1_square(T1, T2); (* let t1 := t2^2 in *)
224 secp256k1_sub(X1, T1, T6); (* let t1 := t1 - t6 in *)
225 secp256k1_sub(T4, T8, X1); (* let t4 := t8 - t1 in *)
226 secp256k1_mul(T2, T2, T4); (* let t2 := t2 * t4 in *)
227 secp256k1_sub(Y1, T2, T5); (* let t2 := t2 - t5 in *)
228 secp256k1_square(T4, T7); (* let t4 := t7^2 in *)
229 secp256k1_sub(X2, T4, T6); (* let t4 := t4 - t6 in *)
230 secp256k1_sub(T8, T8, X2); (* let t8 := t8 - t4 in *)
231 secp256k1_mul(T7, T7, T8); (* let t7 := t7 * t8 in *)
232 secp256k1_sub(Y2, T7, T5) (* let t5 := t7 - t5 in *)
233 }.
234
235 Definition secp256k1_felem_cswap := CSwap.felem_cswap(word:=Naive.word64)(field_parameters:=field_parameters)(field_representaton:=frep256k1).
236
237 (* Compute ToCString.c_func ("secp256k1_zaddu", secp256k1_zaddu). *)
238 (* Compute ToCString.c_func ("secp256k1_felem_cswap", secp256k1_felem_cswap). *)
239
240 Section WithParameters.
241 Context {two_lt_M: 2 < M_pos}.
242 Context {char_ge_3 : (@Ring.char_ge (F M_pos) Logic.eq F.zero F.one F.opp F.add F.sub F.mul (BinNat.N.succ_pos BinNat.N.two))}.
243 Context {field:@Algebra.Hierarchy.field (F M_pos) Logic.eq F.zero F.one F.opp F.add F.sub F.mul F.inv F.div}.
244 Context {a b : F M_pos}.
245 Context {zero_a : id a = F.zero}
246 {seven_b : id b = F.of_Z _ 7}.
247
248 Local Coercion F.to_Z : F >-> Z.
249 Local Notation "m =* P" := ((P%sep) m) (at level 70, only parsing).
250 Local Notation "xs $@ a" := (Array.array ptsto (word.of_Z 1) a xs) (at level 10, format "xs $@ a").
251
252 Local Notation FElem := (FElem(FieldRepresentation:=frep256k1)).
253 Local Notation word := (Naive.word 64).
254 Local Notation felem := (felem(FieldRepresentation:=frep256k1)).
255 Local Notation point := (Jacobian.point(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fadd:=F.add)(Fmul:=F.mul)(a:=a)(b:=b)(Feq_dec:=F.eq_dec)).
256 Local Notation co_z := (Jacobian.co_z(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fadd:=F.add)(Fmul:=F.mul)(a:=a)(b:=b)(Feq_dec:=F.eq_dec)).
257 Local Notation z_of := (Jacobian.z_of(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fadd:=F.add)(Fmul:=F.mul)(a:=a)(b:=b)(Feq_dec:=F.eq_dec)).
258 Local Notation jopp :=
259 (Jacobian.opp(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one)
260 (Fopp:=F.opp)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div)
261 (a:=a)(b:=b)(field:=field)(Feq_dec:=F.eq_dec)).
262 Local Notation make_co_z :=
263 (Jacobian.make_co_z(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one)
264 (Fopp:=F.opp)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div)
265 (a:=a)(b:=b)(field:=field)(Feq_dec:=F.eq_dec)).
266 Local Notation zaddu :=
267 (Jacobian.zaddu(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one)
268 (Fopp:=F.opp)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div)
269 (a:=a)(b:=b)(field:=field)(Feq_dec:=F.eq_dec)).
270 Local Notation zaddc :=
271 (Jacobian.zaddc(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one)
272 (Fopp:=F.opp)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div)
273 (a:=a)(b:=b)(field:=field)(Feq_dec:=F.eq_dec)).
274 Local Notation dblu :=
275 (Jacobian.dblu(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one)
276 (Fopp:=F.opp)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div)
277 (a:=a)(b:=b)(field:=field)(Feq_dec:=F.eq_dec)).
278 Local Notation tplu :=
279 (Jacobian.tplu(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one)
280 (Fopp:=F.opp)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div)
281 (a:=a)(b:=b)(field:=field)(Feq_dec:=F.eq_dec)).
282 Local Notation zdau :=
283 (Jacobian.zdau(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one)
284 (Fopp:=F.opp)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div)
285 (a:=a)(b:=b)(field:=field)(Feq_dec:=F.eq_dec)).
286
287 Global Instance spec_of_jopp : spec_of "secp256k1_jopp" :=
288 fnspec! "secp256k1_jopp"
289 (OXK OYK OZK XK YK ZK : word) / (OX OY OZ X Y Z : felem) (P : point)
290 (R : _ -> Prop),
291 { requires t m :=
292 proj1_sig P = ((feval X), (feval Y), (feval Z)) /\
293 bounded_by loose_bounds X /\
294 bounded_by loose_bounds Y /\
295 bounded_by loose_bounds Z /\
296 m =* (FElem OXK OX) * (FElem OYK OY) * (FElem OZK OZ) *
297 (FElem XK X) * (FElem YK Y) * (FElem ZK Z) * R;
298 ensures t' m' :=
299 t = t' /\
300 exists OX' OY' OZ',
301 proj1_sig (jopp P) = ((feval OX'), (feval OY'), (feval OZ')) /\
302 bounded_by tight_bounds OX' /\
303 bounded_by tight_bounds OY' /\
304 bounded_by tight_bounds OZ' /\
305 m' =* (FElem OXK OX') * (FElem OYK OY') * (FElem OZK OZ') *
306 (FElem XK X) * (FElem YK Y) * (FElem ZK Z) * R
307 }.
308
309 Global Instance spec_of_make_co_z : spec_of "secp256k1_make_co_z" :=
310 fnspec! "secp256k1_make_co_z"
311 (OXK OYK XK YK ZK : word) / (OX OY X Y Z : felem)
312 (P Q : point) (HQaff: z_of Q = 1%F) (R : _ -> Prop),
313 { requires t m :=
314 (exists x y, proj1_sig P = (x, y, (feval Z))) /\
315 (exists z, proj1_sig Q = ((feval X), (feval Y), z)) /\
316 bounded_by loose_bounds X /\
317 bounded_by loose_bounds Y /\
318 bounded_by loose_bounds Z /\
319 m =* (FElem OXK OX) * (FElem OYK OY) *
320 (FElem XK X) * (FElem YK Y) * (FElem ZK Z) * R;
321 ensures t' m' :=
322 t = t' /\
323 exists OX' OY',
324 proj1_sig (snd (make_co_z P Q HQaff)) = ((feval OX'), (feval OY'), (feval Z)) /\
325 bounded_by tight_bounds OX' /\
326 bounded_by tight_bounds OY' /\
327 m' =* (FElem OXK OX') * (FElem OYK OY') *
328 (FElem XK X) * (FElem YK Y) * (FElem ZK Z) * R
329 }.
330
331 Global Instance spec_of_zaddu : spec_of "secp256k1_zaddu" :=
332 fnspec! "secp256k1_zaddu"
333 (OX1K OY1K OX2K OY2K OZK X1K Y1K X2K Y2K ZK : word) /
334 (OX1 OY1 OX2 OY2 OZ X1 Y1 X2 Y2 Z : felem) (P Q : point)
335 (HPQ : co_z P Q) (R : _ -> Prop),
336 { requires t m :=
337 proj1_sig P = ((feval X1), (feval Y1), (feval Z)) /\
338 proj1_sig Q = ((feval X2), (feval Y2), (feval Z)) /\
339 bounded_by loose_bounds X1 /\
340 bounded_by loose_bounds Y1 /\
341 bounded_by loose_bounds X2 /\
342 bounded_by loose_bounds Y2 /\
343 bounded_by loose_bounds Z /\
344 m =* (FElem OX1K OX1) * (FElem OY1K OY1) *
345 (FElem OX2K OX2) * (FElem OY2K OY2) * (FElem OZK OZ) *
346 (FElem X1K X1) * (FElem Y1K Y1) *
347 (FElem X2K X2) * (FElem Y2K Y2) * (FElem ZK Z) * R;
348 ensures t' m' :=
349 t = t' /\
350 exists OX1' OY1' OX2' OY2' OZ',
351 proj1_sig (fst (zaddu P Q HPQ)) = ((feval OX1'), (feval OY1'), (feval OZ')) /\
352 proj1_sig (snd (zaddu P Q HPQ)) = ((feval OX2'), (feval OY2'), (feval OZ')) /\
353 bounded_by tight_bounds OX1' /\
354 bounded_by tight_bounds OY1' /\
355 bounded_by tight_bounds OX2' /\
356 bounded_by tight_bounds OY2' /\
357 bounded_by tight_bounds OZ' /\
358 m' =* (FElem OX1K OX1') * (FElem OY1K OY1') *
359 (FElem OX2K OX2') * (FElem OY2K OY2') * (FElem OZK OZ') *
360 (FElem X1K X1) * (FElem Y1K Y1) *
361 (FElem X2K X2) * (FElem Y2K Y2) * (FElem ZK Z) * R
362 }.
363
364 Global Instance spec_of_zaddc : spec_of "secp256k1_zaddc" :=
365 fnspec! "secp256k1_zaddc"
366 (OX1K OY1K OX2K OY2K OZK X1K Y1K X2K Y2K ZK : word) /
367 (OX1 OY1 OX2 OY2 OZ X1 Y1 X2 Y2 Z : felem) (P Q : point)
368 (HPQ : co_z P Q) (R : _ -> Prop),
369 { requires t m :=
370 proj1_sig P = ((feval X1), (feval Y1), (feval Z)) /\
371 proj1_sig Q = ((feval X2), (feval Y2), (feval Z)) /\
372 bounded_by loose_bounds X1 /\
373 bounded_by loose_bounds Y1 /\
374 bounded_by loose_bounds X2 /\
375 bounded_by loose_bounds Y2 /\
376 bounded_by loose_bounds Z /\
377 m =* (FElem OX1K OX1) * (FElem OY1K OY1) *
378 (FElem OX2K OX2) * (FElem OY2K OY2) * (FElem OZK OZ) *
379 (FElem X1K X1) * (FElem Y1K Y1) *
380 (FElem X2K X2) * (FElem Y2K Y2) * (FElem ZK Z) * R;
381 ensures t' m' :=
382 t = t' /\
383 exists OX1' OY1' OX2' OY2' OZ',
384 proj1_sig (fst (zaddc P Q HPQ)) = ((feval OX1'), (feval OY1'), (feval OZ')) /\
385 proj1_sig (snd (zaddc P Q HPQ)) = ((feval OX2'), (feval OY2'), (feval OZ')) /\
386 bounded_by tight_bounds OX1' /\
387 bounded_by tight_bounds OY1' /\
388 bounded_by tight_bounds OX2' /\
389 bounded_by tight_bounds OY2' /\
390 bounded_by tight_bounds OZ' /\
391 m' =* (FElem OX1K OX1') * (FElem OY1K OY1') *
392 (FElem OX2K OX2') * (FElem OY2K OY2') * (FElem OZK OZ') *
393 (FElem X1K X1) * (FElem Y1K Y1) *
394 (FElem X2K X2) * (FElem Y2K Y2) * (FElem ZK Z) * R
395 }.
396
397 Global Instance spec_of_dblu: spec_of "secp256k1_dblu" :=
398 fnspec! "secp256k1_dblu"
399 (OX1K OY1K OX2K OY2K OZK X1K Y1K : word) /
400 (OX1 OY1 OX2 OY2 OZ X1 Y1 : felem) (P : point)
401 (HPaff : z_of P = F.one) (R : _ -> Prop),
402 { requires t m :=
403 (exists z, proj1_sig P = ((feval X1), (feval Y1), z)) /\
404 bounded_by loose_bounds X1 /\
405 bounded_by loose_bounds Y1 /\
406 m =* (FElem OX1K OX1) * (FElem OY1K OY1) *
407 (FElem OX2K OX2) * (FElem OY2K OY2) * (FElem OZK OZ) *
408 (FElem X1K X1) * (FElem Y1K Y1) * R;
409 ensures t' m' :=
410 t = t' /\
411 exists OX1' OY1' OX2' OY2' OZ',
412 proj1_sig (fst (dblu P HPaff)) = ((feval OX1'), (feval OY1'), (feval OZ')) /\
413 proj1_sig (snd (dblu P HPaff)) = ((feval OX2'), (feval OY2'), (feval OZ')) /\
414 bounded_by tight_bounds OX1' /\
415 bounded_by tight_bounds OY1' /\
416 bounded_by tight_bounds OX2' /\
417 bounded_by tight_bounds OY2' /\
418 bounded_by tight_bounds OZ' /\
419 m' =* (FElem OX1K OX1') * (FElem OY1K OY1') *
420 (FElem OX2K OX2') * (FElem OY2K OY2') * (FElem OZK OZ') *
421 (FElem X1K X1) * (FElem Y1K Y1) * R
422 }.
423
424 Global Instance spec_of_tplu : spec_of "secp256k1_tplu" :=
425 fnspec! "secp256k1_tplu"
426 (OX1K OY1K OX2K OY2K OZK X1K Y1K : word) /
427 (OX1 OY1 OX2 OY2 OZ X1 Y1 : felem) (P : point)
428 (HPaff : z_of P = F.one) (R : _ -> Prop),
429 { requires t m :=
430 (exists z, proj1_sig P = ((feval X1), (feval Y1), z)) /\
431 bounded_by loose_bounds X1 /\
432 bounded_by loose_bounds Y1 /\
433 m =* (FElem OX1K OX1) * (FElem OY1K OY1) *
434 (FElem OX2K OX2) * (FElem OY2K OY2) * (FElem OZK OZ) *
435 (FElem X1K X1) * (FElem Y1K Y1) * R;
436 ensures t' m' :=
437 t = t' /\
438 exists OX1' OY1' OX2' OY2' OZ',
439 proj1_sig (fst (tplu P HPaff)) = ((feval OX1'), (feval OY1'), (feval OZ')) /\
440 proj1_sig (snd (tplu P HPaff)) = ((feval OX2'), (feval OY2'), (feval OZ')) /\
441 bounded_by tight_bounds OX1' /\
442 bounded_by tight_bounds OY1' /\
443 bounded_by tight_bounds OX2' /\
444 bounded_by tight_bounds OY2' /\
445 bounded_by tight_bounds OZ' /\
446 m' =* (FElem OX1K OX1') * (FElem OY1K OY1') *
447 (FElem OX2K OX2') * (FElem OY2K OY2') * (FElem OZK OZ') *
448 (FElem X1K X1) * (FElem Y1K Y1) * R
449 }.
450
451 Global Instance spec_of_zdau : spec_of "secp256k1_zdau" :=
452 fnspec! "secp256k1_zdau"
453 (X1K Y1K X2K Y2K ZK : word) /
454 (X1 Y1 X2 Y2 Z : felem) (P Q : point)
455 (HPQ : co_z P Q) (R : _ -> Prop),
456 { requires t m :=
457 proj1_sig P = ((feval X1), (feval Y1), (feval Z)) /\
458 proj1_sig Q = ((feval X2), (feval Y2), (feval Z)) /\
459 bounded_by loose_bounds X1 /\
460 bounded_by loose_bounds Y1 /\
461 bounded_by loose_bounds X2 /\
462 bounded_by loose_bounds Y2 /\
463 bounded_by loose_bounds Z /\
464 m =* (FElem X1K X1) * (FElem Y1K Y1) *
465 (FElem X2K X2) * (FElem Y2K Y2) * (FElem ZK Z) * R;
466 ensures t' m' :=
467 t = t' /\
468 exists OX1 OY1 OX2 OY2 OZ,
469 proj1_sig (fst (zdau P Q HPQ)) = ((feval OX1), (feval OY1), (feval OZ)) /\
470 proj1_sig (snd (zdau P Q HPQ)) = ((feval OX2), (feval OY2), (feval OZ)) /\
471 bounded_by tight_bounds OX1 /\
472 bounded_by tight_bounds OY1 /\
473 bounded_by tight_bounds OX2 /\
474 bounded_by tight_bounds OY2 /\
475 bounded_by tight_bounds OZ /\
476 m' =* (FElem X1K OX1) * (FElem Y1K OY1) *
477 (FElem X2K OX2) * (FElem Y2K OY2) * (FElem ZK OZ) * R
478 }.
479
480 Local Instance spec_of_secp256k1_opp : spec_of "secp256k1_opp" := Field.spec_of_UnOp un_opp.
481 Local Instance spec_of_secp256k1_square : spec_of "secp256k1_square" := Field.spec_of_UnOp un_square.
482 Local Instance spec_of_secp256k1_mul : spec_of "secp256k1_mul" := Field.spec_of_BinOp bin_mul.
483 Local Instance spec_of_secp256k1_add : spec_of "secp256k1_add" := Field.spec_of_BinOp bin_add.
484 Local Instance spec_of_secp256k1_sub : spec_of "secp256k1_sub" := Field.spec_of_BinOp bin_sub.
485 Local Instance spec_of_secp256k1_felem_copy : spec_of "secp256k1_felem_copy" := Field.spec_of_felem_copy.
486
487 Local Arguments word.rep : simpl never.
488 Local Arguments word.wrap : simpl never.
489 Local Arguments word.unsigned : simpl never.
490 Local Arguments word.of_Z : simpl never.
491
492 Local Ltac solve_mem :=
493 repeat match goal with
494 | |- exists _ : _ -> Prop, _%sep _ => eexists
495 | |- _%sep _ => ecancel_assumption_impl
496 end.
497
498 Local Ltac cbv_bounds H :=
499 cbv [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_outbounds bin_outbounds] in H;
500 cbv [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_outbounds bin_outbounds].
501
502 Local Ltac solve_bounds :=
503 match goal with
504 | H: bounded_by _ ?x |- bounded_by _ ?x => apply H
505 end.
506
507 Local Ltac solve_stack :=
508 (* Rewrites the `stack$@a` term in H to use a Bignum instead *)
509 cbv [FElem];
510 match goal with
511 | H: _%sep ?m |- (Bignum.Bignum felem_size_in_words ?a _ * _)%sep ?m =>
512 seprewrite_in (@Bignum.Bignum_of_bytes _ _ _ _ _ _ 4 a) H
513 end;
514 [> transitivity 32%nat; trivial | ];
515 (* proves the memory matches up *)
516 use_sep_assumption; cancel; cancel_seps_at_indices 0%nat 0%nat; cbn; [> trivial | eapply RelationClasses.reflexivity].
517
518 Local Ltac single_step :=
519 repeat straightline; straightline_call; ssplit; try solve_mem; try solve_bounds; try solve_stack.
520
521 Lemma secp256k1_jopp_ok : program_logic_goal_for_function! secp256k1_jopp.
522 Proof.
523 Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub un_outbounds bin_outbounds].
524
525 do 3 single_step.
526 repeat straightline.
527
528 exists X, x, Z; ssplit. 2-4:solve_bounds.
529 cbv [bin_model bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_model un_square] in *.
530 cbv match beta delta [jopp proj1_sig fst snd].
531 destruct P; cbv [proj1_sig] in H2.
532 rewrite H2; cbv match zeta. rewrite H10. reflexivity.
533
534 ecancel_assumption.
535 Qed.
536
537 Add Ring Private_ring : (F.ring_theory M_pos) (morphism (F.ring_morph M_pos), constants [F.is_constant]).
538
539 Lemma secp256k1_make_co_z_ok : program_logic_goal_for_function! secp256k1_make_co_z.
540 Proof.
541 Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub un_outbounds bin_outbounds].
542
543 do 4 single_step.
544 repeat straightline.
545
546 cbv [FElem] in *.
547 match goal with
548 | |- context [anybytes ?a _ _] =>
549 match goal with
550 | H: _ ?a' |- context [map.split ?a' _ _] =>
551 seprewrite_in (@Bignum.Bignum_to_bytes _ _ _ _ _ _ felem_size_in_words a) H
552 end
553 end.
554 extract_ex1_and_emp_in H26.
555
556 repeat straightline.
557 exists x3, x5; ssplit. 2-3:solve_bounds.
558 cbv [bin_model bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_model un_square] in *.
559 cbv match beta delta [make_co_z proj1_sig fst snd].
560 destruct P; destruct Q; cbv [proj1_sig] in H3, H4.
561 rewrite H3, H4; cbv match zeta.
562 repeat match goal with
563 | H: feval ?x = _ |- context [feval ?x] => rewrite H
564 end.
565 rewrite F.pow_2_r in *; repeat (apply pair_equal_spec; split); ring.
566
567 ecancel_assumption.
568 Qed.
569
570 Lemma secp256k1_zaddu_ok : program_logic_goal_for_function! secp256k1_zaddu.
571 Proof.
572 Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub un_outbounds bin_outbounds].
573
574 do 14 single_step.
575 do 4 single_step.
576
577 repeat straightline.
578 (* Rewrites the FElems for the stack to be about bytes instead *)
579 cbv [FElem] in *.
580 repeat match goal with
581 | |- context [anybytes ?a _ _] =>
582 match goal with
583 | H: _ ?a' |- context [map.split ?a' _ _] =>
584 seprewrite_in (@Bignum.Bignum_to_bytes _ _ _ _ _ _ felem_size_in_words a) H
585 end
586 end.
587 extract_ex1_and_emp_in H92.
588
589 (* Solve stack/memory stuff *)
590 repeat straightline.
591
592 (* Post-conditions *)
593 exists x7,x12,x2,x9,x0; ssplit. 3-7:solve_bounds.
594 (* Correctness: result matches Gallina *)
595 1,2: cbv [bin_model bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_model un_square] in *.
596 1,2: cbv match beta delta [zaddu proj1_sig fst snd].
597 1,2: destruct P; destruct Q; cbv [proj1_sig] in H17, H18.
598 1,2: rewrite H17, H18; cbv match zeta.
599 1,2: rewrite F.pow_2_r in *; congruence.
600
601 ecancel_assumption.
602 Qed.
603
604 Lemma secp256k1_zaddc_ok: program_logic_goal_for_function! secp256k1_zaddc.
605 Proof.
606 Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub un_outbounds bin_outbounds].
607
608 do 25 single_step.
609 do 4 single_step.
610
611 repeat straightline.
612 cbv [FElem] in *.
613 repeat match goal with
614 | |- context [anybytes ?a _ _] =>
615 match goal with
616 | H: _ ?a' |- context [map.split ?a' _ _] =>
617 seprewrite_in (@Bignum.Bignum_to_bytes _ _ _ _ _ _ felem_size_in_words a) H
618 end
619 end.
620 extract_ex1_and_emp_in H140.
621
622 repeat straightline.
623 exists x11,x23,x7,x19,x0; ssplit. 3-7:solve_bounds.
624 1,2: cbv [bin_model bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_model un_square] in *.
625 1,2: cbv match beta delta [zaddc proj1_sig fst snd].
626 1,2: destruct P; destruct Q; cbv [proj1_sig] in H28, H29.
627 1,2: rewrite H28, H29; cbv match zeta.
628 1,2: rewrite F.pow_2_r in *; congruence.
629
630 ecancel_assumption.
631 Qed.
632
633 Lemma secp256k1_dblu_ok: program_logic_goal_for_function! secp256k1_dblu.
634 Proof.
635 Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub un_outbounds bin_outbounds].
636 repeat single_step.
637
638 repeat straightline.
639 cbv [FElem] in *.
640 repeat match goal with
641 | |- context [anybytes ?a _ _] =>
642 match goal with
643 | H: _ ?a' |- context [map.split ?a' _ _] =>
644 seprewrite_in (@Bignum.Bignum_to_bytes _ _ _ _ _ _ felem_size_in_words a) H
645 end
646 end.
647 extract_ex1_and_emp_in H114.
648
649 repeat straightline.
650 exists x13,x19,x8,x16,x0; ssplit. 3-7:solve_bounds.
651 1,2: cbv [bin_model bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_model un_square] in *.
652 1,2: cbv match beta delta [dblu proj1_sig fst snd].
653 1,2: destruct P; cbv [proj1_sig] in H24.
654 1,2: rewrite H24; cbv match zeta.
655 1,2: rewrite F.pow_2_r in *; cbv [id] in zero_a; subst a; repeat (apply pair_equal_spec; split); try congruence.
656 1,2,3: repeat match goal with
657 | H: feval ?x = _ |- context [feval ?x] => rewrite H
658 end; ring.
659
660 ecancel_assumption.
661 Qed.
662
663 Lemma secp256k1_tplu_ok: program_logic_goal_for_function! secp256k1_tplu.
664 Proof.
665 Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub un_outbounds bin_outbounds].
666
667 repeat straightline.
668 eapply Proper_call; cycle -1; [eapply H|try eabstract (solve [ Morphisms.solve_proper ])..]; [ .. | intros ? ? ? ? ].
669 ssplit; [eexists; eassumption|..]; try eassumption.
670 repeat match goal with
671 | H: context [array ptsto _ ?a _] |- context [FElem ?a _] =>
672 seprewrite_in (@Bignum.Bignum_of_bytes _ _ _ _ _ _ 4 a) H; [trivial|]
673 end.
674 multimatch goal with
675 | |- _ ?m1 =>
676 multimatch goal with
677 | H:_ ?m2
678 |- _ =>
679 syntactic_unify._syntactic_unify_deltavar m1 m2;
680 refine (Lift1Prop.subrelation_iff1_impl1 _ _ _ _ _ H); clear H
681 end
682 end; cancel.
683 cancel_seps_at_indices 4%nat 0%nat; [reflexivity|].
684 cancel_seps_at_indices 3%nat 0%nat; [reflexivity|].
685 cancel_seps_at_indices 2%nat 0%nat; [reflexivity|].
686 cancel_seps_at_indices 1%nat 0%nat; [reflexivity|].
687 cancel_seps_at_indices 0%nat 0%nat; [reflexivity|].
688 solve [ecancel].
689
690 repeat straightline.
691 eapply Proper_call; cycle -1; [eapply H0|try eabstract (solve [ Morphisms.solve_proper ])..]; [ .. | intros ? ? ? ? ].
692 ssplit; [exact H28|exact H27|..]; try solve_bounds.
693 ecancel_assumption.
694
695 repeat straightline.
696 cbv [FElem] in *.
697 repeat match goal with
698 | |- context [anybytes ?a _ _] =>
699 match goal with
700 | H: _ ?a' |- context [map.split ?a' _ _] =>
701 seprewrite_in (@Bignum.Bignum_to_bytes _ _ _ _ _ _ felem_size_in_words a) H
702 end
703 end.
704 extract_ex1_and_emp_in H42.
705
706 repeat straightline.
707 exists x5,x6,x7,x8,x9; ssplit. 3-7:solve_bounds.
708 exact H35. exact H36.
709
710 ecancel_assumption.
711 Qed.
712
713 Lemma secp256k1_zdau_ok: program_logic_goal_for_function! secp256k1_zdau.
714 Proof.
715 Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub un_outbounds bin_outbounds].
716
717 do 43 single_step.
718
719 repeat straightline.
720 cbv [FElem] in *.
721 repeat match goal with
722 | |- context [anybytes ?a _ _] =>
723 match goal with
724 | H: _ ?a' |- context [map.split ?a' _ _] =>
725 seprewrite_in (@Bignum.Bignum_to_bytes _ _ _ _ _ _ felem_size_in_words a) H
726 end
727 end.
728 extract_ex1_and_emp_in H208.
729
730 repeat straightline.
731 exists x33,x36,x38,x41,x23; ssplit. 3-7:solve_bounds.
732 1,2: cbv [bin_model bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_model un_square] in *.
733 1,2: cbv match beta delta [zdau proj1_sig fst snd].
734 1,2: destruct P; destruct Q; cbv [proj1_sig] in H42, H43.
735 1,2: rewrite H42, H43; cbv match zeta.
736 1,2: rewrite F.pow_2_r in *; congruence.
737
738 ecancel_assumption.
739 Qed.
740
741 End WithParameters.
742
743 (* Require Import bedrock2.ToCString. *)
744 (* Require Import coqutil.Macros.WithBaseName. *)
745 (* Definition funcs := *)
746 (* List.app *)
747 (* [ secp256k1_opp; *)
748 (* secp256k1_mul; *)
749 (* secp256k1_add; *)
750 (* secp256k1_sub; *)
751 (* secp256k1_square; *)
752 (* secp256k1_to_bytes; *)
753 (* secp256k1_from_bytes; *)
754 (* secp256k1_from_mont; *)
755 (* secp256k1_to_mont; *)
756 (* secp256k1_select_znz] *)
757 (* &[,secp256k1_jopp; *)
758 (* secp256k1_make_co_z; *)
759 (* secp256k1_felem_cswap; *)
760 (* secp256k1_zaddu; *)
761 (* secp256k1_zaddc; *)
762 (* secp256k1_dblu; *)
763 (* secp256k1_tplu; *)
764 (* secp256k1_zdau]. *)
765
766 (* Compute (ToCString.c_module funcs). *)