Debian Codegraph @

src/Bedrock/Secp256k1/Addchain.v

raw
1 Require Import bedrock2.Array.
2 Require Import bedrock2.BasicC64Semantics.
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.Bitwidth.
20 Require Import coqutil.Word.Interface.
21 Require Import coqutil.Word.Properties.
22 Require Import Coq.Init.Byte.
23 Require Import Coq.Lists.List.
24 Require Import Coq.Strings.String.
25 Require Import Coq.ZArith.ZArith.
26 Require Import Coq.ZArith.Znumtheory.
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.Arithmetic.FLia.
33 Require Import Crypto.Algebra.Hierarchy.
34 Require Import Numbers.DecimalString.
35 Require Import Crypto.Util.Decidable.
36 Require Import Lia.
37 Require Crypto.Bedrock.Field.Synthesis.New.Signature.
38 Local Open Scope string_scope.
39 Local Open Scope Z_scope.
40 Import LittleEndianList.
41 Import ListNotations.
42 Import ProgramLogic.Coercions.
43 Import WeakestPrecondition.
44
45 (*
46 addchain: expr: "2^256 - 2^32 - 977 - 2"
47 addchain: hex: fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d
48 addchain: dec: 115792089237316195423570985008687907853269984665640564039457584007908834671661
49 addchain: best: opt(dictionary(hybrid(3,0),continued_fractions(dichotomic)))
50 addchain: cost: 270
51 *)
52 (*
53 tmp t0 t1 t2 t3 t4
54 double t0 x
55 double z t0
56 add z x z
57 add t1 t0 z
58 double t0 t1
59 shift t2 t0 2
60 add t2 t1 t2
61 shift t2 t2 4
62 add t0 t0 t2
63 shift t2 t0 2
64 add t2 t1 t2
65 shift t2 t2 10
66 add t0 t0 t2
67 add t0 x t0
68 double t3 t0
69 shift t2 t3 2
70 shift t4 t2 22
71 add t2 t2 t4
72 shift t4 t2 20
73 add t3 t3 t4
74 shift t3 t3 46
75 add t2 t2 t3
76 shift t3 t2 110
77 add t2 t2 t3
78 add t1 t1 t2
79 shift t1 t1 23
80 add t0 t0 t1
81 shift t0 t0 7
82 add t0 z t0
83 shift t0 t0 3
84 add z z t0
85 *)
86
87 Local Existing Instance field_parameters.
88 Local Existing Instance frep256k1.
89 Local Existing Instance frep256k1_ok.
90
91 (* TODO: This could probably be automated *)
92
93 (* Renders program way too long for the program logic to handle. *)
94 (* Unroll the loop *)
95 (* Fixpoint secp256k1_shift x y n := *)
96 (* match n with *)
97 (* | O => cmd.skip *)
98 (* | S n => (cmd.seq bedrock_cmd:(secp256k1_square(coq:(expr.var x), coq:(expr.var y))) *)
99 (* bedrock_cmd:(coq:(secp256k1_shift x x n))) *)
100 (* end. *)
101
102 (* Peel off one iteration in all cases. *)
103 Definition secp256k1_shift x y (n: nat) :=
104 cmd.seq bedrock_cmd:(secp256k1_square(coq:(expr.var x), coq:(expr.var y)))
105 (cmd.seq bedrock_cmd:(i = $1)
106 bedrock_cmd:(while (i < coq:(n)) {
107 secp256k1_square(coq:(expr.var x), coq:(expr.var x));
108 i = i + $1
109 })).
110
111 Definition secp256k1_inv := Eval cbv in
112 func! (z, x) {
113 stackalloc 32 as t0;
114 stackalloc 32 as t1;
115 stackalloc 32 as t2;
116 stackalloc 32 as t3;
117 stackalloc 32 as t4;
118 secp256k1_square(t0, x); (* double t0 x *)
119 secp256k1_square(z, t0); (* double z t0 *)
120 secp256k1_mul(z, x, z); (* add z x z *)
121 secp256k1_mul(t1, t0, z); (* add t1 t0 z *)
122 secp256k1_square(t0, t1); (* double t0 t1 *)
123 coq:(secp256k1_shift "t2" "t0" 2); (* shift t2 t0 2 *)
124 secp256k1_mul(t2, t1, t2); (* add t2 t1 t2 *)
125 coq:(secp256k1_shift "t2" "t2" 4); (* shift t2 t2 4 *)
126 secp256k1_mul(t0, t0, t2); (* add t0 t0 t2 *)
127 coq:(secp256k1_shift "t2" "t0" 2); (* shift t2 t0 2 *)
128 secp256k1_mul(t2, t1, t2); (* add t2 t1 t2 *)
129 coq:(secp256k1_shift "t2" "t2" 10); (* shift t2 t2 10 *)
130 secp256k1_mul(t0, t0, t2); (* add t0 t0 t2 *)
131 secp256k1_mul(t0, x, t0); (* add t0 x t0 *)
132 secp256k1_square(t3, t0); (* double t3 t0 *)
133 coq:(secp256k1_shift "t2" "t3" 2); (* shift t2 t3 2 *)
134 coq:(secp256k1_shift "t4" "t2" 22); (* shift t4 t2 22 *)
135 secp256k1_mul(t2, t2, t4); (* add t2 t2 t4 *)
136 coq:(secp256k1_shift "t4" "t2" 20); (* shift t4 t2 20 *)
137 secp256k1_mul(t3, t3, t4); (* add t3 t3 t4 *)
138 coq:(secp256k1_shift "t3" "t3" 46); (* shift t3 t3 46 *)
139 secp256k1_mul(t2, t2, t3); (* add t2 t2 t3 *)
140 coq:(secp256k1_shift "t3" "t2" 110);(* shift t3 t2 110 *)
141 secp256k1_mul(t2, t2, t3); (* add t2 t2 t3 *)
142 secp256k1_mul(t1, t1, t2); (* add t1 t1 t2 *)
143 coq:(secp256k1_shift "t1" "t1" 23); (* shift t1 t1 23 *)
144 secp256k1_mul(t0, t0, t1); (* add t0 t0 t1 *)
145 coq:(secp256k1_shift "t0" "t0" 7); (* shift t0 t0 7 *)
146 secp256k1_mul(t0, z, t0); (* add t0 z t0 *)
147 coq:(secp256k1_shift "t0" "t0" 3); (* shift t0 t0 3 *)
148 secp256k1_mul(z, z, t0) (* add z z t0 *)
149 }.
150
151 (* Compute ToCString.c_func ("secp256k1_inv", secp256k1_inv). *)
152
153 Section WithParameters.
154 Context {two_lt_M: 2 < M_pos}.
155 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))}.
156 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}.
157 Context {secp256k1_prime: prime m}.
158 Context {F_M_pos : Z.pos M_pos = m}.
159
160 Local Coercion F.to_Z : F >-> Z.
161 Local Notation "m =* P" := ((P%sep) m) (at level 70, only parsing).
162 Local Notation "xs $@ a" := (Array.array ptsto (word.of_Z 1) a xs) (at level 10, format "xs $@ a").
163
164 Local Notation FElem := (FElem(FieldRepresentation:=frep256k1)).
165 Local Notation word := (BasicC64Semantics.word).
166 Local Notation felem := (felem(FieldRepresentation:=frep256k1)).
167
168 Local Instance spec_of_secp256k1_square : spec_of "secp256k1_square" := Field.spec_of_UnOp un_square.
169 Local Instance spec_of_secp256k1_mul : spec_of "secp256k1_mul" := Field.spec_of_BinOp bin_mul.
170
171 Global Instance spec_of_inv : spec_of "secp256k1_inv" :=
172 fnspec! "secp256k1_inv"
173 (zK xK : word) / z (x : felem) (vx : F M_pos) (R : _ -> Prop),
174 { requires t m :=
175 vx = feval x /\
176 bounded_by loose_bounds x /\
177 m =* Placeholder zK z * (FElem xK x) * R;
178 ensures t' m' :=
179 t = t' /\
180 exists z',
181 feval z' = (F.inv vx) /\
182 bounded_by tight_bounds z' /\
183 m' =* (FElem zK z') * (FElem xK x) * R
184 }.
185
186 Local Arguments word.rep : simpl never.
187 Local Arguments word.wrap : simpl never.
188 Local Arguments word.unsigned : simpl never.
189 Local Arguments word.of_Z : simpl never.
190
191 Local Ltac solve_mem :=
192 repeat match goal with
193 | |- exists _ : _ -> Prop, _%sep _ => eexists
194 | |- _%sep _ => ecancel_assumption_impl
195 end.
196
197 Local Ltac cbv_bounds H :=
198 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;
199 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].
200
201 Local Ltac solve_bounds :=
202 match goal with
203 | H: bounded_by _ ?x |- bounded_by _ ?x => apply H
204 end.
205
206 Local Ltac solve_stack :=
207 (* Rewrites the `stack$@a` term in H to use a Bignum instead *)
208 cbv [FElem];
209 match goal with
210 | H: _%sep ?m |- (Bignum.Bignum felem_size_in_words ?a _ * _)%sep ?m =>
211 seprewrite_in (@Bignum.Bignum_of_bytes _ _ _ _ _ _ 4 a) H
212 end;
213 [> transitivity 32%nat; trivial | ];
214 (* proves the memory matches up *)
215 use_sep_assumption; cancel; cancel_seps_at_indices 0%nat 0%nat; cbn; [> trivial | eapply RelationClasses.reflexivity].
216
217 Local Ltac single_step :=
218 repeat straightline; straightline_call; ssplit; try solve_mem; try solve_bounds; try solve_stack.
219
220 Lemma spec_of_shift functions tr mem loc post :
221 forall var to (Hvar: var <> "i") (Hto: 2 <= to < 2^32) pvar vvar R,
222 spec_of_secp256k1_square functions ->
223 map.get loc var = Some pvar ->
224 (FElem pvar vvar * R)%sep mem ->
225 bounded_by un_outbounds vvar ->
226 (forall tr' mem' loc',
227 (tr' = tr /\
228 loc' = map.put loc "i" (word.of_Z to) /\
229 exists vvar',
230 (FElem pvar vvar' * R)%sep mem' /\
231 feval vvar' = F.pow (feval vvar) (N.pow 2%N (Z.to_N (to - 1))) /\
232 bounded_by un_outbounds vvar') ->
233 post tr' mem' loc'
234 ) ->
235 cmd functions
236 bedrock_func_body:(
237 {$"i" = coq:(expr.literal 1)};
238 while coq:(expr.var "i") < coq:(expr.literal to) {
239 {$"secp256k1_square"(coq:(expr.var var), coq:(expr.var var))};
240 $"i" = coq:(expr.var "i") + coq:(expr.literal 1)
241 }) tr mem loc post.
242 Proof.
243 intros. repeat straightline.
244 pose (inv := fun (v: nat) (t: trace) (m: @map.rep word byte _) (l: @map.rep string word locals) => t = tr /\
245 exists i (Hi: 1 <= i <= to),
246 v = Z.to_nat (to - i) /\
247 (exists vx, ((FElem pvar vx) * R)%sep m /\
248 feval vx = F.pow (feval vvar) (N.pow 2%N (Z.to_N (i - 1))) /\
249 bounded_by un_outbounds vx) /\
250 l = map.put loc "i" (word.of_Z i)).
251 eapply wp_while. exists nat, lt, inv. ssplit; [eapply lt_wf|..].
252 eexists. unfold inv; ssplit; [reflexivity|..].
253 exists 1. exists (ltac:(lia): 1 <= 1 <= to). ssplit; [reflexivity|..].
254 eexists. ssplit.
255 ecancel_assumption. rewrite F.pow_1_r. reflexivity. auto.
256 reflexivity.
257 intros fuel * Hinv.
258 destruct Hinv as (-> & vi & Hvi & -> & Hmem & ->).
259 eexists ?[b]; ssplit.
260 eexists; split; [apply map.get_put_same|].
261 eapply Core.WeakestPrecondition_dexpr_expr; [|apply ExprCompiler.expr_compile_Z_literal].
262 cbn. rewrite <- Core.word.morph_ltu by lia.
263 reflexivity.
264 all: pose proof Zlt_cases vi to;
265 intros Hnz; destruct (vi <? to);
266 try (rewrite ?word.unsigned_of_Z_0, ?word.unsigned_of_Z_1 in Hnz;
267 congruence); [].
268 repeat straightline.
269 eexists. split. eexists. split.
270 rewrite map.get_put_diff by congruence. eauto.
271 eexists. split. rewrite map.get_put_diff by congruence. eauto.
272 reflexivity.
273 single_step. repeat straightline.
274 eexists. split.
275 eexists. split. apply map.get_put_same. cbn. reflexivity.
276 eexists. split. split; [reflexivity|].
277 exists (vi + 1). exists (ltac:(lia): 1 <= vi + 1 <= to).
278 ssplit; [reflexivity|..]. eexists; ssplit; [ecancel_assumption|..].
279 repeat match goal with
280 | H : feval ?a = _ |- context [feval ?a] => rewrite H
281 end. cbv [un_model un_square].
282 rewrite F.pow_pow_l, N.mul_comm, <- N.pow_succ_r', <- Z2N.inj_succ.
283 f_equal. f_equal. lia. lia. auto.
284 unfold l'. rewrite <- word.ring_morph_add, map.put_put_same. reflexivity.
285 lia. assert (vi = to) as -> by lia.
286 destruct Hmem as (? & ? & ? & ?).
287 apply H3.
288 ssplit; [reflexivity|reflexivity|].
289 eexists; ssplit; eassumption.
290 Qed.
291
292 Lemma secp256k1_inv_ok : program_logic_goal_for_function! secp256k1_inv.
293 Proof.
294 Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub un_outbounds bin_outbounds].
295
296 repeat single_step.
297 destruct H64 as (-> & -> & ? & ? & ? & ?).
298 eexists; split; [reflexivity|].
299 eapply spec_of_shift. congruence. lia. auto. reflexivity.
300 ecancel_assumption. eassumption.
301 repeat single_step.
302 destruct H82 as (-> & -> & ? & ? & ? & ?).
303 eexists; split; [reflexivity|].
304 eapply spec_of_shift. congruence. lia. auto. reflexivity.
305 ecancel_assumption. eassumption.
306 repeat single_step.
307 destruct H91 as (-> & -> & ? & ? & ? & ?).
308 eexists; split; [reflexivity|].
309 eapply spec_of_shift. congruence. lia. auto. reflexivity.
310 ecancel_assumption. eassumption.
311 repeat single_step.
312 destruct H100 as (-> & -> & ? & ? & ? & ?).
313 eexists; split; [reflexivity|].
314 eapply spec_of_shift. congruence. lia. auto. reflexivity.
315 ecancel_assumption. eassumption.
316 repeat single_step.
317 destruct H109 as (-> & -> & ? & ? & ? & ?).
318 eexists; split; [reflexivity|].
319 eapply spec_of_shift. congruence. lia. auto. reflexivity.
320 ecancel_assumption. eassumption.
321 repeat single_step.
322 destruct H124 as (-> & -> & ? & ? & ? & ?).
323 eexists; split; [reflexivity|].
324 eapply spec_of_shift. congruence. lia. auto. reflexivity.
325 ecancel_assumption. eassumption.
326 repeat single_step.
327 destruct H130 as (-> & -> & ? & ? & ? & ?).
328 eexists; split; [reflexivity|].
329 eapply spec_of_shift. congruence. lia. auto. reflexivity.
330 ecancel_assumption. eassumption.
331 repeat single_step.
332 destruct H139 as (-> & -> & ? & ? & ? & ?).
333 eexists; split; [reflexivity|].
334 eapply spec_of_shift. congruence. lia. auto. reflexivity.
335 ecancel_assumption. eassumption.
336 repeat single_step.
337 destruct H148 as (-> & -> & ? & ? & ? & ?).
338 eexists; split; [reflexivity|].
339 eapply spec_of_shift. congruence. lia. auto. reflexivity.
340 ecancel_assumption. eassumption.
341 repeat single_step.
342 destruct H157 as (-> & -> & ? & ? & ? & ?).
343 eexists; split; [reflexivity|].
344 eapply spec_of_shift. congruence. lia. auto. reflexivity.
345 ecancel_assumption. eassumption.
346 repeat single_step.
347 destruct H169 as (-> & -> & ? & ? & ? & ?).
348 eexists; split; [reflexivity|].
349 eapply spec_of_shift. congruence. lia. auto. reflexivity.
350 ecancel_assumption. eassumption.
351 repeat single_step.
352 destruct H178 as (-> & -> & ? & ? & ? & ?).
353 eexists; split; [reflexivity|].
354 eapply spec_of_shift. congruence. lia. auto. reflexivity.
355 ecancel_assumption. eassumption.
356 repeat single_step.
357
358 repeat straightline.
359 cbv [FElem] in *.
360 repeat match goal with
361 | |- context [anybytes ?a _ _] =>
362 match goal with
363 | H: _ ?a' |- context [map.split ?a' _ _] =>
364 seprewrite_in (Bignum.Bignum_to_bytes felem_size_in_words a) H
365 end
366 end.
367 extract_ex1_and_emp_in H194.
368
369 repeat straightline.
370 exists x42. ssplit; [|solve_bounds|ecancel_assumption].
371 repeat match goal with
372 | H : feval ?a = _ |- context [feval ?a] => rewrite H
373 end.
374 cbv [un_model bin_model un_square bin_mul].
375 unfold vx. rewrite (@F.Fq_inv_fermat _ _ two_lt_M).
376 rewrite F_M_pos.
377 repeat match goal with
378 | |- context [F.pow (F.pow (feval x) ?a) ?b] => rewrite (F.pow_pow_l (feval x) a b)
379 | |- context [F.mul (feval x) (F.pow (feval x) ?n)] => rewrite <- (F.pow_succ_r (feval x) n)
380 | |- context [F.mul (F.pow (feval x) ?n1) (F.pow (feval x) ?n2)] => rewrite <- (F.pow_add_r (feval x) n1 n2)
381 end.
382 f_equal.
383 Qed.
384
385 End WithParameters.