Debian Codegraph @

src/Bedrock/Secp256k1/JoyeLadder.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.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.Secp256k1.JacobianCoZ.
32 Require Import Crypto.Bedrock.Secp256k1.Addchain.
33 Require Import Crypto.Bedrock.Specs.Field.
34 Require Import Crypto.Util.Decidable.
35 Require Import Curves.Weierstrass.Jacobian.Jacobian.
36 Require Import Curves.Weierstrass.Jacobian.CoZ.
37 Require Import Curves.Weierstrass.Jacobian.ScalarMult.
38 Require Import Lia.
39 Require Crypto.Bedrock.Field.Synthesis.New.Signature.
40 Local Open Scope string_scope.
41 Local Open Scope Z_scope.
42 Import LittleEndianList.
43 Import ListNotations.
44 Import ProgramLogic.Coercions.
45 Import WeakestPrecondition.
46
47 Local Existing Instance field_parameters.
48 Local Existing Instance frep256k1.
49 Local Existing Instance frep256k1_ok.
50
51 Section WithParameters.
52 Context {two_lt_M: 2 < M_pos}.
53 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))}.
54 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}.
55 Context {secp256k1_prime: Znumtheory.prime m}.
56 Context {F_M_pos : Z.pos M_pos = m}.
57 Context {a b : F M_pos}.
58 Context {zero_a : id a = F.zero}
59 {seven_b : id b = F.of_Z _ 7}.
60 Context {scalarbitsz : Z} {scalarbitsz_small : word.wrap scalarbitsz = scalarbitsz}.
61
62 Definition secp256k1_laddermul :=
63 func! (oX, oY, k, X, Y) {
64 i = coq:(1);
65 swap = (load1(k+i>>coq:(3))>>(i&coq:(7)))&coq:(1);
66 stackalloc 32 as X0;
67 stackalloc 32 as Y0;
68 stackalloc 32 as X1;
69 stackalloc 32 as Y1;
70 stackalloc 32 as Z;
71 stackalloc 32 as oZ;
72 secp256k1_tplu(X1, Y1, X0, Y0, Z, X, Y);
73 i = coq:(2);
74 while (i < coq:(scalarbitsz)) {
75 b = (load1(k+i>>coq:(3))>>(i&coq:(7)))&coq:(1);
76 swap = swap ^ b;
77 felem_cswap(swap, X0, X1);
78 felem_cswap(swap, Y0, Y1);
79 secp256k1_zdau(X1, Y1, X0, Y0, Z);
80 swap = b;
81 i = i+coq:(1)
82 };
83 felem_cswap(swap, X0, X1);
84 felem_cswap(swap, Y0, Y1);
85 stackalloc 32 as tX;
86 stackalloc 32 as tY;
87 secp256k1_make_co_z(tX, tY, X, Y, Z);
88 secp256k1_opp(tY, tY);
89 secp256k1_zaddu(oX, oY, X1, Y1, oZ, X0, Y0, tX, tY, Z);
90 i = coq:(0);
91 swap = (load1(k+i>>coq:(3))>>(i&coq:(7)))&coq:(1);
92 felem_cswap(swap, oX, X1);
93 felem_cswap(swap, oY, Y1);
94 secp256k1_inv(Z, oZ);
95 secp256k1_mul(oY, oY, Z);
96 secp256k1_mul(Z, Z, Z);
97 secp256k1_mul(oX, oX, Z);
98 secp256k1_mul(oY, oY, Z)
99 }.
100
101 Add Ring Private_ring : (F.ring_theory M_pos) (morphism (F.ring_morph M_pos), constants [F.is_constant]).
102
103 Local Coercion F.to_Z : F >-> Z.
104 Local Notation "m =* P" := ((P%sep) m) (at level 70, only parsing).
105 Local Notation "xs $@ a" := (Array.array ptsto (word.of_Z 1) a xs) (at level 10, format "xs $@ a").
106
107 Local Notation FElem := (FElem(FieldRepresentation:=frep256k1)).
108 Local Notation word := (Naive.word 64).
109 Local Notation felem := (felem(FieldRepresentation:=frep256k1)).
110 Local Notation Wpoint := (WeierstrassCurve.W.point(F:=F M_pos)(Feq:=Logic.eq)(Fadd:=F.add)(Fmul:=F.mul)(a:=a)(b:=b)).
111 Local Notation Wzero := (WeierstrassCurve.W.zero(F:=F M_pos)(Feq:=Logic.eq)(Fadd:=F.add)(Fmul:=F.mul)(a:=a)(b:=b)).
112 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)).
113 Local Notation co_z_points := (ScalarMult.co_z_points(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)).
114 Local Notation zaddu_co_z_points := (ScalarMult.zaddu_co_z_points(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one)(Fopp:=F.opp)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div)(a:=a)(b:=b)(field:=field)(Feq_dec:=F.eq_dec)).
115 Local Notation zdau_co_z_points := (ScalarMult.zdau_co_z_points(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div)(a:=a)(b:=b)(field:=field)(Feq_dec:=F.eq_dec)).
116 Local Notation cswap_co_z_points := (ScalarMult.cswap_co_z_points(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div)(a:=a)(b:=b)(field:=field)(Feq_dec:=F.eq_dec)).
117 Local Notation make_co_z_points := (ScalarMult.make_co_z_points(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div)(a:=a)(b:=b)(field:=field)(Feq_dec:=F.eq_dec)).
118
119 Local Instance spec_of_secp256k1_opp : spec_of "secp256k1_opp" := Field.spec_of_UnOp un_opp.
120 Local Instance spec_of_secp256k1_square : spec_of "secp256k1_square" := Field.spec_of_UnOp un_square.
121 Local Instance spec_of_secp256k1_mul : spec_of "secp256k1_mul" := Field.spec_of_BinOp bin_mul.
122 Local Instance spec_of_secp256k1_add : spec_of "secp256k1_add" := Field.spec_of_BinOp bin_add.
123 Local Instance spec_of_secp256k1_sub : spec_of "secp256k1_sub" := Field.spec_of_BinOp bin_sub.
124 Local Instance spec_of_secp256k1_inv : spec_of "secp256k1_inv" := Addchain.spec_of_inv.
125 Local Instance spec_of_secp256k1_cswap : spec_of "felem_cswap" := CSwap.spec_of_cswap.
126 Local Instance spec_of_secp256k1_tplu : spec_of "secp256k1_tplu" := (@spec_of_tplu field a b).
127 Local Instance spec_of_secp256k1_make_co_z : spec_of "secp256k1_make_co_z" := (@spec_of_make_co_z field a b).
128 Local Instance spec_of_secp256k1_zaddu : spec_of "secp256k1_zaddu" := (@spec_of_zaddu field a b).
129 Local Instance spec_of_secp256k1_zdau : spec_of "secp256k1_zdau" := (@spec_of_zdau field a b).
130
131 Local Arguments word.rep : simpl never.
132 Local Arguments word.wrap : simpl never.
133 Local Arguments word.unsigned : simpl never.
134 Local Arguments word.of_Z : simpl never.
135
136 Local Ltac solve_mem :=
137 repeat match goal with
138 | |- exists _ : _ -> Prop, _%sep _ => eexists
139 | |- _%sep _ => ecancel_assumption_impl
140 end.
141
142 Local Ltac cbv_bounds H :=
143 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;
144 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].
145
146 Local Ltac solve_bounds :=
147 match goal with
148 | H: bounded_by _ ?x |- bounded_by _ ?x => apply H
149 end.
150
151 Local Ltac solve_stack :=
152 (* Rewrites the `stack$@a` term in H to use a Bignum instead *)
153 match goal with
154 | H: _%sep ?m |- (Bignum.Bignum felem_size_in_words ?a _ * _)%sep ?m =>
155 seprewrite_in (@Bignum.Bignum_of_bytes _ _ _ _ _ _ 4 a) H
156 end;
157 [> transitivity 32%nat; trivial | ];
158 (* proves the memory matches up *)
159 use_sep_assumption; cancel; cancel_seps_at_indices 0%nat 0%nat; cbn; [> trivial | eapply RelationClasses.reflexivity].
160
161 Local Ltac single_step :=
162 repeat straightline; straightline_call; ssplit; try solve_mem; try solve_bounds; try solve_stack.
163
164 Local Instance spec_of_laddermul : spec_of "secp256k1_laddermul" :=
165 fnspec! "secp256k1_laddermul"
166 (OXptr OYptr kptr Xptr Yptr : word) /
167 (OX OY X Y : felem) (P : Wpoint) (HPnz : P <> Wzero) kbytes (k : Z) (R : _ -> Prop),
168 { requires t m :=
169 proj1_sig P = inl ((feval X), (feval Y)) /\
170 bounded_by loose_bounds X /\
171 bounded_by loose_bounds Y /\
172 LittleEndianList.le_combine kbytes = k /\
173 2 <= scalarbitsz <= 8*Z.of_nat (List.length kbytes) /\
174 m =* (FElem OXptr OX) * (FElem OYptr OY) * kbytes$@kptr *
175 (FElem Xptr X) * (FElem Yptr Y) * R;
176 ensures t' m' :=
177 t = t' /\
178 match proj1_sig (ScalarMult.joye_ladder scalarbitsz (Z.testbit k) P HPnz) with
179 | inl (X', Y') =>
180 exists OX' OY',
181 X' = (feval OX') /\ Y' = (feval OY') /\
182 bounded_by tight_bounds OX' /\
183 bounded_by tight_bounds OY' /\
184 m' =* (FElem OXptr OX') * (FElem OYptr OY') * kbytes$@kptr *
185 (FElem Xptr X) * (FElem Yptr Y) * R
186 | _ => (* result is point at infinity *)
187 exists OX' OY',
188 0%F = (feval OX') /\ 0%F = (feval OY') /\
189 m' =* (FElem OXptr OX') * (FElem OYptr OY') * kbytes$@kptr *
190 (FElem Xptr X) * (FElem Yptr Y) * R
191 end
192 }.
193
194 Lemma spec_of_testbit functions tr mem loc post :
195 forall var kptr kbytes k wi i R,
196 map.get loc "k" = Some kptr ->
197 map.get loc "i" = Some wi ->
198 (kbytes$@kptr * R)%sep mem ->
199 LittleEndianList.le_combine kbytes = k ->
200 wi = word.of_Z i ->
201 (0 <= i < 2 ^ 64)%Z ->
202 (i < 8 * Z.of_nat (List.length kbytes)) ->
203
204 (forall tr' mem' loc',
205 (tr' = tr /\
206 mem' = mem /\
207 loc' = map.put loc var (Core.word.b2w (Z.testbit k i))) ->
208 post tr' mem' loc') ->
209
210 cmd functions
211 bedrock_func_body:(
212 $var = load1(coq:(expr.var "k") + coq:(expr.var "i") >> coq:(expr.literal 3)) >> (coq:(expr.var "i") & coq:(expr.literal 7)) & coq:(expr.literal 1))
213 tr mem loc post.
214 Proof.
215 repeat straightline.
216 repeat (eexists; split; repeat Tactics.straightline'; eauto); cbn [Semantics.interp_binop].
217
218 - subst wi.
219 eapply load_one_of_sep.
220 unshelve (
221 let Hrw := open_constr:(@bytearray_index_inbounds _ _ _ _ _ _ _ _ _ : Lift1Prop.iff1 _ _) in
222 seprewrite0_in Hrw H1; ecancel_assumption).
223 all: repeat rewrite ?Core.word.unsigned_of_Z_b2z, ?word.unsigned_of_Z, ?word.unsigned_and_nowrap, ?word.unsigned_sru_nowrap.
224 all: cbv [word.wrap]; rewrite ?Z.mod_small; try lia.
225 rewrite ?Z.shiftr_div_pow2 by lia; change (2^3) with 8. lia.
226 - eapply H6. ssplit; auto.
227 f_equal.
228 eapply word.unsigned_inj.
229 unfold Core.word.b2w, wi.
230 repeat rewrite ?Core.word.unsigned_of_Z_b2z, ?word.unsigned_of_Z, ?word.unsigned_and_nowrap, ?word.unsigned_sru_nowrap.
231 all: cbv [word.wrap]; repeat rewrite ?Z.mod_small; try lia.
232 2: match goal with | |- context [byte.unsigned ?n] => generalize (byte.unsigned_range n); lia end.
233 all: change 7 with (Z.ones 3); change 1 with (Z.ones 1); rewrite ?Z.land_ones by lia.
234 2: lia.
235 rewrite ?Z.shiftr_div_pow2 by lia; change (2^3) with 8; change (2^1) with 2.
236 rewrite <-?List.hd_skipn_nth_default.
237 rewrite <- Z.testbit_spec' by lia; f_equal.
238 rewrite <- (LittleEndianList.split_le_combine kbytes), H2.
239 rewrite LittleEndianList.nth_default_le_split, byte.unsigned_of_Z, Z2Nat.id by lia.
240 cbv [byte.wrap]; rewrite <-Z.land_ones, Z.land_spec, Z.ones_spec_low by lia.
241 rewrite Z.shiftr_spec, Bool.andb_true_r by lia; f_equal. lia.
242 Qed.
243
244 Lemma while_is_iter {A:Type} test body fuel s:
245 @Loops.while.while A test body fuel s = Nat.iter (S fuel) (fun s => if test s then body s else s) s.
246 Proof.
247 revert s; induction fuel; [reflexivity|]; intros.
248 rewrite Nat.iter_succ_r.
249 simpl Loops.while.while. rewrite IHfuel.
250 destruct (test s) eqn:Htest; [reflexivity|].
251 clear -Htest.
252 induction fuel; [simpl; rewrite Htest; reflexivity|].
253 rewrite Nat.iter_succ_r, Htest. apply IHfuel.
254 Qed.
255
256 Lemma car_cswap {A} swap x y :
257 Core.P2.car (@cswap A swap x y) = if swap then y else x.
258 Proof. destruct swap; reflexivity. Qed.
259
260 Lemma cdr_cswap {A} swap x y :
261 Core.P2.cdr (@cswap A swap x y) = if swap then x else y.
262 Proof. destruct swap; reflexivity. Qed.
263
264 Lemma co_z_conv (PQ: co_z_points) : Jacobian.co_z (fst (proj1_sig PQ)) (snd (proj1_sig PQ)).
265 Proof.
266 generalize (proj2_sig PQ). rewrite (surjective_pairing (proj1_sig PQ)) at 1.
267 auto.
268 Qed.
269
270 Lemma proj1_sig_zdau_co_z_points PQ :
271 proj1_sig (zdau_co_z_points PQ) = (fst (Jacobian.zdau (fst (proj1_sig PQ)) (snd (proj1_sig PQ)) (co_z_conv PQ)), snd (Jacobian.zdau (fst (proj1_sig PQ)) (snd (proj1_sig PQ)) (co_z_conv PQ))).
272 Proof.
273 destruct PQ as (PQ' & ?). destruct PQ' as (P & Q).
274 unfold zdau_co_z_points. cbv [proj1_sig].
275 rewrite (surjective_pairing (Jacobian.zdau P Q _)).
276 repeat f_equal; apply Eqdep_dec.UIP_dec; apply F.eq_dec.
277 Qed.
278
279 Lemma proj1_sig_cswap_co_z_points swap PQ :
280 proj1_sig (cswap_co_z_points swap PQ) = (if swap then snd (proj1_sig PQ) else fst (proj1_sig PQ), if swap then fst (proj1_sig PQ) else snd (proj1_sig PQ)).
281 Proof.
282 destruct PQ as (PQ' & ?). destruct PQ' as (P & Q).
283 unfold cswap_co_z_points. cbv [proj1_sig].
284 destruct swap; reflexivity.
285 Qed.
286
287 Lemma proj1_sig_zaddu_co_z_points PQ :
288 proj1_sig (zaddu_co_z_points PQ) = (fst (Jacobian.zaddu (fst (proj1_sig PQ)) (snd (proj1_sig PQ)) (co_z_conv PQ)), snd (Jacobian.zaddu (fst (proj1_sig PQ)) (snd (proj1_sig PQ)) (co_z_conv PQ))).
289 Proof.
290 destruct PQ as (PQ' & ?). destruct PQ' as (P & Q).
291 unfold zaddu_co_z_points. cbv [proj1_sig].
292 rewrite (surjective_pairing (Jacobian.zaddu P Q _)).
293 repeat f_equal; apply Eqdep_dec.UIP_dec; apply F.eq_dec.
294 Qed.
295
296 Lemma zdau_eq (P: point) P' Q Q' HPQ HPQ' :
297 P = P' ->
298 Q = Q' ->
299 Jacobian.zdau P Q HPQ = Jacobian.zdau P' Q' HPQ'.
300 Proof.
301 intros. subst P' Q'.
302 assert (HPQ = HPQ') as -> by (apply Eqdep_dec.UIP_dec; apply F.eq_dec).
303 reflexivity.
304 Qed.
305
306 Lemma zaddu_eq (P: point) P' Q Q' HPQ HPQ' :
307 P = P' ->
308 Q = Q' ->
309 Jacobian.zaddu P Q HPQ = Jacobian.zaddu P' Q' HPQ'.
310 Proof.
311 intros. subst P' Q'.
312 assert (HPQ = HPQ') as -> by (apply Eqdep_dec.UIP_dec; apply F.eq_dec).
313 reflexivity.
314 Qed.
315
316 Lemma spec_of_inner_loop functions tr mem loc post :
317 forall kptr kbytes k X0ptr X1ptr Y0ptr Y1ptr Zptr X0 X1 Y0 Y1 Z (R0 R1:point) R,
318 spec_of_secp256k1_cswap functions ->
319 spec_of_secp256k1_zdau functions ->
320 map.get loc "k" = Some kptr ->
321 map.get loc "i" = Some (word.of_Z 2) ->
322 map.get loc "swap" = Some (Core.word.b2w (Z.testbit k 1)) ->
323 map.get loc "X0" = Some X0ptr ->
324 map.get loc "Y0" = Some Y0ptr ->
325 map.get loc "X1" = Some X1ptr ->
326 map.get loc "Y1" = Some Y1ptr ->
327 map.get loc "Z" = Some Zptr ->
328 LittleEndianList.le_combine kbytes = k ->
329 2 <= scalarbitsz <= 8*Z.of_nat (List.length kbytes) ->
330 (kbytes$@kptr * FElem X0ptr X0 * FElem Y0ptr Y0 * FElem X1ptr X1 * FElem Y1ptr Y1 * FElem Zptr Z * R)%sep mem ->
331 proj1_sig R0 = (feval X0, feval Y0, feval Z) ->
332 proj1_sig R1 = (feval X1, feval Y1, feval Z) ->
333 bounded_by loose_bounds X0 ->
334 bounded_by loose_bounds Y0 ->
335 bounded_by loose_bounds X1 ->
336 bounded_by loose_bounds Y1 ->
337 bounded_by loose_bounds Z ->
338
339 (forall tr' mem' loc',
340 (tr' = tr) ->
341 (exists (R1R0_co_z : Jacobian.co_z R1 R0),
342 let R1R0 := exist (fun '(P, Q) => Jacobian.co_z P Q) (R1, R0) R1R0_co_z in
343 let '(R1R0', vswap, _) :=
344 Loops.while.while (fun '(_, _, i) => i <? scalarbitsz)
345 (fun '(R1R0, swap, i) => (zdau_co_z_points (cswap_co_z_points (xorb swap (Z.testbit k i)) R1R0), Z.testbit k i, Z.succ i)) (Z.to_nat (scalarbitsz - 2)) (R1R0, Z.testbit k 1, 2) in
346 exists l, (forall s, s <> "b" -> map.get l s = map.get loc s) /\
347 loc' = map.put (map.put l "swap" (Core.word.b2w vswap)) "i" (word.of_Z scalarbitsz) /\
348 exists X0' Y0' X1' Y1' Z',
349 (kbytes$@kptr * FElem X0ptr X0' * FElem Y0ptr Y0' * FElem X1ptr X1' * FElem Y1ptr Y1' * FElem Zptr Z' * R)%sep mem' /\
350 bounded_by loose_bounds X0' /\
351 bounded_by loose_bounds Y0' /\
352 bounded_by loose_bounds X1' /\
353 bounded_by loose_bounds Y1' /\
354 bounded_by loose_bounds Z' /\
355 let '(R1', R0') := proj1_sig R1R0' in
356 proj1_sig R0' = (feval X0', feval Y0', feval Z') /\
357 proj1_sig R1' = (feval X1', feval Y1', feval Z')) ->
358 post tr' mem' loc'
359 ) ->
360
361 cmd functions
362 bedrock_func_body:(
363 while coq:(expr.var "i") < coq:(expr.literal scalarbitsz) {
364 {$"b" = load1(coq:(expr.var "k") +
365 coq:(expr.var "i") >> coq:(expr.literal 3)) >>
366 (coq:(expr.var "i") & coq:(expr.literal 7)) & coq:(expr.literal 1)};
367 {$"swap" = coq:(expr.var "swap") ^ coq:(expr.var "b")};
368 {$"felem_cswap"(coq:(expr.var "swap"), coq:(expr.var "X0"), coq:(expr.var "X1"))};
369 {$"felem_cswap"(coq:(expr.var "swap"), coq:(expr.var "Y0"), coq:(expr.var "Y1"))};
370 {$"secp256k1_zdau"(coq:(expr.var "X1"), coq:(expr.var "Y1"),
371 coq:(expr.var "X0"), coq:(expr.var "Y0"),
372 coq:(expr.var "Z"))};
373 {$"swap" = coq:(expr.var "b")};
374 $"i" = coq:(expr.var "i") + coq:(expr.literal 1)
375 }) tr mem loc post.
376 Proof.
377 intros. repeat straightline.
378 assert (R1R0_z : Jacobian.co_z R1 R0) by (unfold Jacobian.co_z, Jacobian.z_of; rewrite H12, H13; reflexivity).
379 pose (R1R0 := exist (fun '(P, Q) => Jacobian.co_z P Q) (R1, R0) R1R0_z).
380 pose (test := (fun '(_, _, i) => (Z.ltb i scalarbitsz)):(co_z_points*bool*BinInt.Z)->bool).
381 pose (body := (fun '(R1R0, swap, i) =>
382 let b := Z.testbit k i in
383 let swap := xorb swap b in
384 let R1R0 := cswap_co_z_points swap R1R0 in
385 let R1R0 := zdau_co_z_points R1R0 in
386 let swap := b in
387 let i := Z.succ i in
388 (R1R0, swap, i)):(co_z_points*bool*BinInt.Z)->(co_z_points*bool*BinInt.Z)).
389 pose (inv := fun (v: nat) (t: trace) (m: @map.rep word byte _) (l: @map.rep string word locals) =>
390 t = tr /\
391 exists i (Hi: 2 <= i <= scalarbitsz),
392 v = Z.to_nat (scalarbitsz - i) /\
393 let '(R1R0', swap', i') := Nat.iter (Z.to_nat (i - 2)) (fun s => if test s then body s else s) (R1R0, Z.testbit k 1, 2%Z) in
394 i' = i /\
395 exists loc', (forall s, s <> "b" -> map.get loc' s = map.get loc s) /\
396 l = map.put (map.put loc' "swap" (Core.word.b2w swap')) "i" (word.of_Z i) /\
397 exists X0' Y0' X1' Y1' Z',
398 (kbytes$@kptr * FElem X0ptr X0' * FElem Y0ptr Y0' * FElem X1ptr X1' * FElem Y1ptr Y1' * FElem Zptr Z' * R)%sep m /\
399 bounded_by loose_bounds X0' /\
400 bounded_by loose_bounds Y0' /\
401 bounded_by loose_bounds X1' /\
402 bounded_by loose_bounds Y1' /\
403 bounded_by loose_bounds Z' /\
404 let '(R1', R0') := proj1_sig R1R0' in
405 proj1_sig R0' = (feval X0', feval Y0', feval Z') /\
406 proj1_sig R1' = (feval X1', feval Y1', feval Z')).
407 eapply wp_while. exists nat, lt, inv. ssplit; [eapply lt_wf|..].
408 (* Invariant holds at beginning *)
409 eexists. unfold inv; ssplit; [reflexivity|..].
410 exists 2. exists (ltac:(lia): 2 <= 2 <= scalarbitsz). ssplit; [reflexivity|..].
411 rewrite DecimalPos.Unsigned.nat_iter_0. ssplit; [reflexivity|..].
412 exists loc. ssplit; [reflexivity|..].
413 apply Core.map.ext_eq; intros s.
414 destruct (String.eqb_spec "i" s); [subst s; rewrite map.get_put_same; assumption|rewrite map.get_put_diff by auto].
415 destruct (String.eqb_spec "swap" s); [subst s; rewrite map.get_put_same; assumption|rewrite map.get_put_diff by auto].
416 reflexivity. do 5 eexists; ssplit; [ecancel_assumption|..]; try solve_bounds.
417 unfold R1R0. cbn [proj1_sig]. rewrite H12, H13. split; reflexivity.
418 (* Invariant preservation *)
419 intros fuel * Hinv. destruct Hinv as (-> & vi & Hvi & -> & Hiter).
420 pose (iter_res := Nat.iter (Z.to_nat (vi - 2)) (fun s => if test s then body s else s) (R1R0, Z.testbit k 1, 2%Z)).
421 fold iter_res in Hiter. rewrite (surjective_pairing iter_res) in Hiter.
422 rewrite (surjective_pairing (fst iter_res)) in Hiter.
423 destruct Hiter as (Hvi' & loc' & Hloc' & -> & Hmem).
424 assert (Hsmall: scalarbitsz < 2 ^ 64) by (rewrite <- scalarbitsz_small; apply Z.mod_pos_bound; lia).
425 eexists ?[b]; ssplit.
426 eexists; split; [apply map.get_put_same|].
427 eapply Core.WeakestPrecondition_dexpr_expr; [|apply ExprCompiler.expr_compile_Z_literal].
428 cbn. rewrite <- Core.word.morph_ltu by lia.
429 reflexivity.
430 all: pose proof Zlt_cases vi scalarbitsz;
431 intros Hnz; destruct (vi <? scalarbitsz);
432 try (rewrite ?word.unsigned_of_Z_0, ?word.unsigned_of_Z_1 in Hnz;
433 congruence); [].
434 destruct Hmem as (X0' & Y0' & X1' & Y1' & Z' & Hmem & HX0' & HY0' & HX1' & HY1' & HZ' & Hprojs).
435 rewrite (surjective_pairing (proj1_sig _)) in Hprojs.
436 pose (R0' := snd (proj1_sig (fst (fst iter_res)))).
437 pose (R1' := fst (proj1_sig (fst (fst iter_res)))).
438 fold R0' R1' in Hprojs.
439 destruct Hprojs as (Hproj0 & Hproj1).
440 do 3 straightline. eapply spec_of_testbit; eauto.
441 repeat (rewrite map.get_put_diff by congruence).
442 rewrite Hloc' by congruence. eassumption.
443 apply map.get_put_same. ecancel_assumption.
444 lia. lia.
445 intros ? ? ? (-> & -> & ->).
446 repeat straightline. eexists; split.
447 repeat straightline. eexists; split.
448 repeat (rewrite map.get_put_diff by congruence).
449 apply map.get_put_same. repeat straightline.
450 eexists; split. apply map.get_put_same.
451 unfold Core.word.b2w. rewrite <- Core.word.morph_xor.
452 rewrite Core.Z.lxor_xorb. reflexivity.
453 repeat straightline. eexists; split.
454 eexists. split. apply map.get_put_same.
455 repeat straightline. eexists; split.
456 unfold l. repeat (rewrite map.get_put_diff by congruence).
457 rewrite Hloc' by congruence. eassumption.
458 repeat straightline. eexists; split.
459 unfold l. repeat (rewrite map.get_put_diff by congruence).
460 rewrite Hloc' by congruence. eassumption.
461 repeat straightline.
462 straightline_call; ssplit.
463 destruct (xorb _ _); simpl; auto.
464 rewrite <- Bignum_as_array. unfold FElem in Hmem.
465 ecancel_assumption_impl. repeat straightline.
466 eexists. split. repeat straightline.
467 eexists; split. apply map.get_put_same.
468 repeat straightline. eexists; split.
469 unfold l. repeat (rewrite map.get_put_diff by congruence).
470 rewrite Hloc' by congruence. eassumption.
471 repeat straightline. eexists; split.
472 unfold l. repeat (rewrite map.get_put_diff by congruence).
473 rewrite Hloc' by congruence. eassumption.
474 repeat straightline.
475 cbv [dlet.dlet] in H26.
476 straightline_call; ssplit.
477 destruct (xorb _ _); auto.
478 rewrite <- Bignum_as_array. ecancel_assumption_impl.
479 repeat straightline.
480 cbv [dlet.dlet] in H27.
481 assert (Hlen: forall ptr v m R, (FElem ptr v * R)%sep m -> Datatypes.length v = felem_size_in_words).
482 { unfold FElem. rewrite Bignum_as_array.
483 intros * XX. apply Arrays.length_of_sizedlistarray_value_R in XX. exact XX. }
484 repeat rewrite cswap_low_combine_eq in H27 by (repeat erewrite Hlen by ecancel_assumption; reflexivity).
485 rewrite cswap_combine_eq in H27.
486 2: destruct (xorb _ _); cbv; auto.
487 2-3: symmetry; eapply Hlen; ecancel_assumption.
488 rewrite cswap_combine_eq in H27.
489 2: destruct (xorb _ _); cbv; auto.
490 2-3: symmetry; eapply Hlen; ecancel_assumption.
491 rewrite <- Bignum_as_array in H27.
492 repeat rewrite car_cswap, cdr_cswap in H27.
493 rewrite word.unsigned_eqb, Core.word.unsigned_of_Z_b2z, word.unsigned_of_Z_1 in H27.
494 assert (XY: forall bb, (Z.b2z bb =? 1) = bb) by (destruct bb; auto).
495 rewrite XY in H27; clear XY.
496 eexists; split.
497 repeat (repeat straightline; eexists; split; [unfold l; repeat rewrite map.get_put_diff by congruence; rewrite Hloc' by congruence; eassumption|]).
498 repeat straightline. straightline_call; ssplit.
499 8: unfold FElem; ecancel_assumption_impl.
500 instantiate (1 := if xorb (snd (fst iter_res)) (Z.testbit k vi) then R0' else R1').
501 destruct (xorb _ _); [exact Hproj0|exact Hproj1].
502 instantiate (1 := if xorb (snd (fst iter_res)) (Z.testbit k vi) then R1' else R0').
503 destruct (xorb _ _); [exact Hproj1|exact Hproj0].
504 1-4: destruct (xorb _ _); solve_bounds.
505 solve_bounds.
506 Unshelve. 3: unfold Jacobian.co_z, Jacobian.z_of; destruct (xorb _ _); rewrite Hproj0, Hproj1; reflexivity.
507 repeat straightline.
508 eexists. ssplit. repeat straightline.
509 eexists. split. unfold l. repeat (rewrite map.get_put_diff by congruence).
510 apply map.get_put_same. reflexivity.
511 repeat straightline.
512 eexists. ssplit. repeat straightline.
513 eexists. split. unfold l0, l. repeat (rewrite map.get_put_diff by congruence).
514 apply map.get_put_same. repeat straightline.
515 repeat straightline. eexists. split.
516 unfold inv; split; [reflexivity|].
517 exists (vi + 1). exists (ltac:(lia): 2 <= vi + 1 <= scalarbitsz).
518 ssplit; [reflexivity|].
519 replace (Z.to_nat (vi + 1 - 2)) with (S (Z.to_nat (vi - 2))) by lia.
520 rewrite Nat.iter_succ. fold iter_res.
521 unfold test. rewrite (surjective_pairing iter_res), Hvi'.
522 replace (vi <? scalarbitsz) with true by lia.
523 rewrite (surjective_pairing (fst iter_res)).
524 unfold body. ssplit; [reflexivity|].
525 exists (map.put loc' "b" (Core.word.b2w (Z.testbit k vi))); ssplit.
526 intros; rewrite map.get_put_diff by congruence. apply Hloc'; auto.
527 apply Core.map.ext_eq; intros s.
528 unfold l1. destruct (String.eqb_spec s "i"); [subst s; repeat rewrite map.get_put_same|repeat rewrite map.get_put_diff by congruence].
529 rewrite word.ring_morph_add; reflexivity.
530 unfold l0. destruct (String.eqb_spec s "swap"); [subst s; repeat rewrite map.get_put_same; reflexivity|repeat rewrite map.get_put_diff by congruence].
531 unfold l. repeat rewrite map.get_put_diff by congruence.
532 destruct (String.eqb_spec s "b"); [subst s; repeat rewrite map.get_put_same; reflexivity|repeat rewrite map.get_put_diff by congruence; reflexivity].
533 do 5 eexists; ssplit; [ecancel_assumption_impl|..].
534 1-5: solve_bounds.
535 rewrite (surjective_pairing (proj1_sig _)).
536 rewrite proj1_sig_zdau_co_z_points.
537 unfold frep256k1 in H28, H29.
538 unfold frep256k1. rewrite <- H28, <- H29.
539 rewrite <- (surjective_pairing (Jacobian.zdau _ _ _)).
540 split; repeat f_equal; apply zdau_eq; rewrite proj1_sig_cswap_co_z_points; reflexivity.
541 (* measure decreases *) lia.
542 (* post condition *)
543 assert (vi = scalarbitsz) as -> by lia.
544 assert (iter_res = Nat.iter (S (Z.to_nat (scalarbitsz - 2))) (fun s : co_z_points * bool * BinNums.Z => if test s then body s else s) (R1R0, Z.testbit k 1, 2)).
545 { rewrite Nat.iter_succ. fold iter_res.
546 rewrite (surjective_pairing iter_res) at 2.
547 rewrite Hvi'. simpl test.
548 replace (scalarbitsz <? scalarbitsz) with false by lia.
549 unfold iter_res. rewrite (surjective_pairing (fst _)).
550 reflexivity. }
551 rewrite <- while_is_iter in H22.
552 unfold test, body in H22.
553 apply H19; [reflexivity|].
554 exists R1R0_z. fold R1R0. rewrite <- H22.
555 rewrite (surjective_pairing (iter_res)).
556 rewrite (surjective_pairing (fst (iter_res))).
557 eexists; ssplit; eauto.
558 Qed.
559
560 Lemma spec_of_laddermul_ok : program_logic_goal_for_function! secp256k1_laddermul.
561 Proof.
562 Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub un_outbounds bin_outbounds].
563
564 repeat first [match goal with | |- cmd _ bedrock_func_body:($_ = load1(coq:(expr.var "k") + coq:(expr.var "i") >> coq:(expr.literal 3)) >> (coq:(expr.var "i") & coq:(expr.literal 7)) & coq:(expr.literal 1)) _ _ _ _ => idtac end |straightline].
565 eapply spec_of_testbit; try reflexivity; try ecancel_assumption_impl; try lia.
566
567 assert (HPaff: Jacobian.z_of (Jacobian.of_affine P) = F.one) by (apply (ScalarMult.ScalarMult.joye_ladder_obligation_1 P HPnz)).
568
569 repeat straightline.
570 single_step.
571 instantiate (3 := Jacobian.of_affine P).
572 unfold Jacobian.of_affine, Jacobian.of_affine_impl, WeierstrassCurve.W.coordinates.
573 cbn [proj1_sig]. rewrite H15. eexists. reflexivity.
574 1-2: solve_bounds.
575 repeat match goal with
576 | H: context [Array.array ptsto _ ?a _] |- context [FElem ?a _] =>
577 seprewrite_in (@Bignum.Bignum_of_bytes _ _ _ _ _ _ 4 a) H; [trivial|]
578 end.
579 multimatch goal with
580 | |- _ ?m1 =>
581 multimatch goal with
582 | H:_ ?m2
583 |- _ =>
584 syntactic_unify._syntactic_unify_deltavar m1 m2;
585 refine (Lift1Prop.subrelation_iff1_impl1 _ _ _ _ _ H); clear H
586 end
587 end.
588 cbv [FElem].
589 cancel. cancel_seps_at_indices 0%nat 4%nat; [reflexivity|].
590 cancel_seps_at_indices 0%nat 3%nat; [reflexivity|].
591 cancel_seps_at_indices 0%nat 2%nat; [reflexivity|].
592 cancel_seps_at_indices 0%nat 1%nat; [reflexivity|].
593 cancel_seps_at_indices 0%nat 0%nat; [reflexivity|].
594 ecancel.
595
596 repeat straightline.
597 instantiate (1:=HPaff) in H48.
598 eapply spec_of_inner_loop; try reflexivity; try ecancel_assumption_impl; try lia.
599 exact H49. exact H48.
600 1-5: solve_bounds.
601 repeat straightline.
602 rewrite H18 in H47.
603 case_eq (Loops.while.while (fun '(_, _, i) => i <? scalarbitsz)
604 (fun '(R1R0, swap, i) =>
605 (zdau_co_z_points (cswap_co_z_points (xorb swap (Z.testbit k i)) R1R0), Z.testbit k i, Z.succ i)) (Z.to_nat (scalarbitsz - 2))
606 (exist (fun '(P, Q) => Jacobian.co_z P Q) (fst (Jacobian.tplu (Jacobian.of_affine P) HPaff), snd (Jacobian.tplu (Jacobian.of_affine P) HPaff)) x4, Z.testbit k 1, 2)); intros [R1R0' vswap] ?i Hloopeq.
607 rewrite Hloopeq in H47. repeat straightline.
608
609 eexists; ssplit. repeat straightline.
610 eexists; ssplit; [unfold loc'0; repeat rewrite map.get_put_diff by congruence; apply map.get_put_same|repeat straightline].
611 eexists; ssplit; [unfold loc'0; repeat rewrite map.get_put_diff by congruence; rewrite H46|]; repeat straightline. congruence.
612 eexists; ssplit; [unfold loc'0; repeat rewrite map.get_put_diff by congruence; rewrite H46|]; repeat straightline. congruence.
613
614 single_step. rewrite Core.word.b2w_if; destruct vswap; auto.
615 rewrite <- Bignum_as_array. unfold FElem in H56. ecancel_assumption_impl.
616 repeat straightline. red in H64.
617 assert (Hlen: forall ptr v m R, (FElem ptr v * R)%sep m -> Datatypes.length v = felem_size_in_words).
618 { unfold FElem. rewrite Bignum_as_array.
619 intros * XX. apply Arrays.length_of_sizedlistarray_value_R in XX. exact XX. }
620 rewrite cswap_low_combine_eq in H64 by (repeat erewrite Hlen by ecancel_assumption; reflexivity).
621 rewrite cswap_combine_eq in H64.
622 2: destruct vswap; cbv; auto.
623 2-3: symmetry; eapply Hlen; ecancel_assumption.
624 repeat rewrite car_cswap, cdr_cswap in H64.
625 rewrite word.unsigned_eqb, Core.word.unsigned_b2w, word.unsigned_of_Z_1 in H64.
626 assert (XY: forall bb, (Z.b2z bb =? 1) = bb) by (destruct bb; auto).
627 rewrite XY in H64; clear XY.
628 rewrite <- Bignum_as_array in H64.
629 eexists; ssplit. repeat straightline.
630 eexists; ssplit; [unfold loc'0; repeat rewrite map.get_put_diff by congruence; apply map.get_put_same|repeat straightline].
631 eexists; ssplit; [unfold loc'0; repeat rewrite map.get_put_diff by congruence; rewrite H46|]; repeat straightline. congruence.
632 eexists; ssplit; [unfold loc'0; repeat rewrite map.get_put_diff by congruence; rewrite H46|]; repeat straightline. congruence.
633
634 single_step. rewrite Core.word.b2w_if; destruct vswap; auto.
635 rewrite <- Bignum_as_array. ecancel_assumption_impl.
636 repeat straightline. red in H65.
637 rewrite cswap_low_combine_eq in H65 by (repeat erewrite Hlen by ecancel_assumption; reflexivity).
638 rewrite cswap_combine_eq in H65.
639 2: destruct vswap; cbv; auto.
640 2-3: symmetry; eapply Hlen; ecancel_assumption.
641 repeat rewrite car_cswap, cdr_cswap in H65.
642 rewrite word.unsigned_eqb, Core.word.unsigned_b2w, word.unsigned_of_Z_1 in H65.
643 assert (XY: forall bb, (Z.b2z bb =? 1) = bb) by (destruct bb; auto).
644 rewrite XY in H65; clear XY.
645 rewrite <- Bignum_as_array in H65.
646 eexists; ssplit. repeat straightline.
647 eexists; ssplit. unfold l9. rewrite map.get_put_diff by congruence.
648 apply map.get_put_same. repeat straightline.
649 eexists; ssplit; [apply map.get_put_same|]. repeat straightline.
650 eexists; ssplit. unfold l9, l8, loc'0.
651 repeat (rewrite map.get_put_diff by congruence).
652 rewrite H46. repeat straightline. congruence.
653 repeat straightline. eexists; ssplit. unfold l9, l8, loc'0.
654 repeat (rewrite map.get_put_diff by congruence).
655 rewrite H46. repeat straightline. congruence.
656 repeat straightline. eexists; ssplit. unfold l9, l8, loc'0.
657 repeat (rewrite map.get_put_diff by congruence).
658 rewrite H46. repeat straightline. congruence.
659 repeat straightline.
660 destruct R1R0' as ((R1'& R0') & HR1R0') eqn:HeqR1R0'.
661 destruct H62.
662 single_step.
663 exists (if vswap then feval x8 else feval x6).
664 exists (if vswap then feval x9 else feval x7).
665 instantiate (2:=if vswap then R1' else R0').
666 destruct (vswap); [exact H68|exact H62].
667 exists 1%F. instantiate (3:=Jacobian.of_affine P).
668 unfold Jacobian.of_affine, WeierstrassCurve.W.coordinates.
669 cbv [proj1_sig].
670 assert (proj1_sig P = (let (xyi, _) := P in xyi)) as <- by (destruct P; reflexivity).
671 rewrite H15. reflexivity.
672 1-3:solve_bounds.
673 repeat match goal with
674 | H: context [Array.array ptsto _ ?a _] |- context [FElem ?a _] =>
675 seprewrite_in (@Bignum.Bignum_of_bytes _ _ _ _ _ _ 4 a) H; [trivial|]
676 end.
677 multimatch goal with
678 | |- _ ?m1 =>
679 multimatch goal with
680 | H:_ ?m2
681 |- _ =>
682 syntactic_unify._syntactic_unify_deltavar m1 m2;
683 refine (Lift1Prop.subrelation_iff1_impl1 _ _ _ _ _ H); clear H
684 end
685 end.
686 cancel. cancel_seps_at_indices 0%nat 1%nat; [reflexivity|].
687 cancel_seps_at_indices 0%nat 0%nat; [reflexivity|].
688 cancel_seps_at_indices 6%nat 0%nat; [reflexivity|].
689 cancel_seps_at_indices 6%nat 0%nat; [reflexivity|].
690 cancel_seps_at_indices 5%nat 0%nat; [reflexivity|].
691 ecancel.
692 instantiate (1:=HPaff) in H73.
693 repeat straightline.
694
695 eexists; ssplit. repeat straightline.
696 eexists; ssplit. apply map.get_put_same. repeat straightline.
697 eexists; ssplit; [apply map.get_put_same|]; repeat straightline.
698 single_step.
699 repeat straightline.
700 eexists; split. unfold l9, l8, loc'0.
701 repeat (repeat straightline; eexists; ssplit; [repeat (rewrite map.get_put_diff by congruence); rewrite H46; repeat straightline; congruence|]).
702 repeat straightline. eexists; ssplit.
703 repeat (rewrite map.get_put_diff by congruence); apply map.get_put_same.
704 repeat straightline. eexists; ssplit.
705 repeat (rewrite map.get_put_diff by congruence); apply map.get_put_same.
706 repeat straightline. eexists; ssplit.
707 repeat (rewrite map.get_put_diff by congruence); rewrite H46; repeat straightline; congruence.
708 repeat straightline.
709 cbv [un_model un_opp] in H79.
710
711 assert (Hzaddu_ob: Jacobian.co_z (if vswap then R1' else R0') (Jacobian.opp (snd (Jacobian.make_co_z (if vswap then R1' else R0') (Jacobian.of_affine P) HPaff)))).
712 { unfold Jacobian.co_z, Jacobian.z_of, Jacobian.opp.
713 cbv [proj1_sig] in *. rewrite H75.
714 destruct vswap; [rewrite H68|rewrite H62]; reflexivity. }
715 single_step.
716 instantiate (1:=x10).
717 instantiate (1:=if vswap then x9 else x7).
718 instantiate (1:=if vswap then x8 else x6).
719 instantiate (1:=if vswap then R1' else R0').
720 destruct vswap; [exact H68|exact H62].
721 instantiate (1:=x13). instantiate (1:=x11).
722 instantiate (1:=Jacobian.opp (snd (Jacobian.make_co_z (if vswap then R1' else R0') (Jacobian.of_affine P) HPaff))).
723 unfold Jacobian.opp.
724 cbv [proj1_sig]. cbv [proj1_sig] in H75. rewrite H75.
725 unfold frep256k1. unfold frep256k1 in H79.
726 rewrite H79. reflexivity.
727 1-2: destruct vswap; solve_bounds.
728 1-3: solve_bounds.
729 repeat match goal with
730 | H: context [Array.array ptsto _ ?a _] |- context [FElem ?a _] =>
731 seprewrite_in (@Bignum.Bignum_of_bytes _ _ _ _ _ _ 4 a) H; [trivial|]
732 end.
733 multimatch goal with
734 | |- _ ?m1 =>
735 multimatch goal with
736 | H:_ ?m2
737 |- _ =>
738 syntactic_unify._syntactic_unify_deltavar m1 m2;
739 refine (Lift1Prop.subrelation_iff1_impl1 _ _ _ _ _ H); clear H
740 end
741 end. fold FElem in *.
742 cancel. cancel_seps_at_indices 8%nat 0%nat; [reflexivity|].
743 cancel_seps_at_indices 8%nat 0%nat; [reflexivity|].
744 cancel_seps_at_indices 6%nat 0%nat; [reflexivity|].
745 cancel_seps_at_indices 4%nat 0%nat; [reflexivity|].
746 cancel_seps_at_indices 0%nat 0%nat; [reflexivity|].
747 cancel_seps_at_indices 3%nat 0%nat; [reflexivity|].
748 cancel_seps_at_indices 2%nat 0%nat; [reflexivity|].
749 ecancel.
750 instantiate (1:=Hzaddu_ob) in H73.
751 repeat first [match goal with | |- cmd _ bedrock_func_body:($_ = load1(coq:(expr.var "k") + coq:(expr.var "i") >> coq:(expr.literal 3)) >> (coq:(expr.var "i") & coq:(expr.literal 7)) & coq:(expr.literal 1)) _ _ _ _ => idtac end |straightline].
752 eapply spec_of_testbit; try reflexivity; try ecancel_assumption_impl; try lia.
753 unfold l10, l9, l8, loc'0. repeat (rewrite map.get_put_diff by congruence).
754 rewrite H46 by congruence. reflexivity.
755 apply map.get_put_same. lia. lia.
756 repeat straightline.
757
758 eexists; ssplit. repeat straightline.
759 eexists; ssplit; [apply map.get_put_same|].
760 repeat straightline. eexists; ssplit.
761 unfold loc'1, l10, l9, l8, loc'0. repeat (rewrite map.get_put_diff by congruence).
762 rewrite H46 by congruence. reflexivity.
763 repeat straightline. eexists; ssplit.
764 unfold loc'1, l10, l9, l8, loc'0. repeat (rewrite map.get_put_diff by congruence).
765 rewrite H46 by congruence. reflexivity.
766 repeat straightline.
767 rewrite H18.
768 single_step.
769 rewrite Core.word.b2w_if; destruct (Z.testbit k 0); auto.
770 rewrite <- Bignum_as_array.
771 unfold FElem in H89. ecancel_assumption_impl.
772 repeat straightline. red in H90.
773 rewrite <- Bignum_as_array in H90.
774 rewrite cswap_low_combine_eq in H90 by (repeat erewrite Hlen by ecancel_assumption_impl; reflexivity).
775 rewrite cswap_combine_eq in H90.
776 2: destruct (Z.testbit k 0); cbv; auto.
777 2-3: symmetry; eapply Hlen; ecancel_assumption_impl.
778 repeat rewrite car_cswap, cdr_cswap in H90.
779 rewrite word.unsigned_eqb, Core.word.unsigned_b2w, word.unsigned_of_Z_1 in H90.
780 assert (XY: forall bb, (Z.b2z bb =? 1) = bb) by (destruct bb; auto).
781 rewrite XY in H90; clear XY.
782 eexists; ssplit. repeat straightline.
783 eexists; ssplit; [apply map.get_put_same|].
784 repeat straightline. eexists; ssplit.
785 unfold loc'1, l10, l9, l8, loc'0. repeat (rewrite map.get_put_diff by congruence).
786 rewrite H46 by congruence. reflexivity.
787 repeat straightline. eexists; ssplit.
788 unfold loc'1, l10, l9, l8, loc'0. repeat (rewrite map.get_put_diff by congruence).
789 rewrite H46 by congruence. reflexivity.
790 repeat straightline.
791 rewrite H18.
792 single_step.
793 destruct (Z.testbit k 0); cbv; auto.
794 rewrite <- Bignum_as_array.
795 unfold FElem in H90. ecancel_assumption_impl.
796 repeat straightline. red in H91.
797 rewrite <- Bignum_as_array in H91.
798 rewrite cswap_low_combine_eq in H91 by (repeat erewrite Hlen by ecancel_assumption_impl; reflexivity).
799 rewrite cswap_combine_eq in H91.
800 2: destruct (Z.testbit k 0); cbv; auto.
801 2-3: symmetry; eapply Hlen; ecancel_assumption_impl.
802 repeat rewrite car_cswap, cdr_cswap in H91.
803 rewrite word.unsigned_eqb, Core.word.unsigned_b2w, word.unsigned_of_Z_1 in H91.
804 assert (XY: forall bb, (Z.b2z bb =? 1) = bb) by (destruct bb; auto).
805 rewrite XY in H91; clear XY.
806
807 eexists; ssplit. repeat straightline. eexists; ssplit.
808 unfold loc'1, l10, l9, l8, loc'0. repeat (rewrite map.get_put_diff by congruence).
809 rewrite H46 by congruence. reflexivity.
810 repeat straightline. eexists; ssplit.
811 unfold loc'1, l10, l9, l8, loc'0. repeat (rewrite map.get_put_diff by congruence).
812 rewrite H46 by congruence. reflexivity.
813 repeat straightline.
814 single_step.
815 3: { fold FElem in H91. ecancel_assumption_impl. }
816 reflexivity. solve_bounds.
817 repeat straightline.
818 eexists; ssplit. repeat straightline. eexists; ssplit.
819 unfold loc'1, l10, l9, l8, loc'0. repeat (rewrite map.get_put_diff by congruence).
820 rewrite H46 by congruence. reflexivity.
821 repeat straightline. eexists; ssplit.
822 unfold loc'1, l10, l9, l8, loc'0. repeat (rewrite map.get_put_diff by congruence).
823 rewrite H46 by congruence. reflexivity.
824 repeat straightline. eexists; ssplit.
825 unfold loc'1, l10, l9, l8, loc'0. repeat (rewrite map.get_put_diff by congruence).
826 rewrite H46 by congruence. reflexivity.
827 repeat straightline.
828 single_step.
829 destruct (Z.testbit k 0); solve_bounds.
830 repeat straightline.
831 eexists; ssplit. repeat straightline.
832 repeat (eexists; ssplit; [unfold loc'1, l10, l9, l8, loc'0; repeat (rewrite map.get_put_diff by congruence); rewrite H46 by congruence; reflexivity|]; repeat straightline).
833 cbv [bin_model bin_mul] in H95.
834
835 single_step.
836 repeat straightline.
837 eexists; ssplit. repeat straightline.
838 repeat (eexists; ssplit; [unfold loc'1, l10, l9, l8, loc'0; repeat (rewrite map.get_put_diff by congruence); rewrite H46 by congruence; reflexivity|]; repeat straightline).
839 cbv [bin_model bin_mul] in H98.
840
841 single_step.
842 destruct (Z.testbit k 0); solve_bounds.
843 repeat straightline.
844 eexists; ssplit. repeat straightline.
845 repeat (eexists; ssplit; [unfold loc'1, l10, l9, l8, loc'0; repeat (rewrite map.get_put_diff by congruence); rewrite H46 by congruence; reflexivity|]; repeat straightline).
846 cbv [bin_model bin_mul] in H101.
847
848 single_step.
849 repeat straightline.
850 cbv [bin_model bin_mul] in H104.
851
852 cbv [FElem Field.FElem] in *.
853 repeat match goal with
854 | |- context [anybytes ?a _ _] =>
855 match goal with
856 | H: _ ?a' |- context [map.split ?a' _ _] =>
857 seprewrite_in (@Bignum.Bignum_to_bytes _ _ _ _ _ _ felem_size_in_words a) H
858 end
859 end.
860 extract_ex1_and_emp_in H106.
861 repeat straightline.
862
863 unfold ScalarMult.joye_ladder.
864 unfold ScalarMult.joye_ladder_inner.
865 assert (ScalarMult.tplu_co_z_points (Jacobian.of_affine P) (ScalarMult.ScalarMult.joye_ladder_obligation_1 P HPnz) = exist (fun '(P, Q) => Jacobian.co_z P Q) (fst (Jacobian.tplu (Jacobian.of_affine P) HPaff), snd (Jacobian.tplu (Jacobian.of_affine P) HPaff)) x4) as ->.
866 { unfold ScalarMult.tplu_co_z_points.
867 apply eq_sig_hprop.
868 - intros. destruct x24.
869 apply Eqdep_dec.UIP_dec; apply F.eq_dec.
870 - cbv [proj1_sig].
871 rewrite <- (surjective_pairing (Jacobian.tplu (Jacobian.of_affine P) HPaff)).
872 f_equal. apply Eqdep_dec.UIP_dec; apply F.eq_dec. }
873 assert (Z.to_nat scalarbitsz - 2 = Z.to_nat (scalarbitsz - 2))%nat as -> by lia.
874 match goal with
875 | |- context [Loops.while.while ?test ?body ?fuel ?args] =>
876 match type of Hloopeq with
877 | context [Loops.while.while ?test1 ?body1 ?fuel1 ?args1] =>
878 replace (Loops.while.while test body fuel args) with (Loops.while.while test1 body1 fuel1 args1) by reflexivity
879 end
880 end.
881 rewrite Hloopeq.
882 rewrite (proj1_sig_cswap_co_z_points vswap).
883 assert (red_proj1_sig: forall (A : Type) (P : A -> Prop) (x : A) (Px : P x), proj1_sig (exist P x Px) = x) by reflexivity.
884 rewrite red_proj1_sig.
885 cbv [fst snd].
886 rewrite proj1_sig_cswap_co_z_points, proj1_sig_zaddu_co_z_points.
887 rewrite <- (surjective_pairing (Jacobian.zaddu _ _ _)).
888 assert (Jacobian.zaddu
889 (fst (proj1_sig (make_co_z_points (if vswap then R1' else R0') (Jacobian.opp (Jacobian.of_affine P)) (ScalarMult.opp_of_affine P HPnz))))
890 (snd (proj1_sig (make_co_z_points (if vswap then R1' else R0') (Jacobian.opp (Jacobian.of_affine P)) (ScalarMult.opp_of_affine P HPnz))))
891 (co_z_conv (make_co_z_points (if vswap then R1' else R0') (Jacobian.opp (Jacobian.of_affine P)) (ScalarMult.opp_of_affine P HPnz))) =
892 Jacobian.zaddu (if vswap then R1' else R0')
893 (Jacobian.opp (snd (Jacobian.make_co_z (if vswap then R1' else R0') (Jacobian.of_affine P) HPaff))) Hzaddu_ob) as ->.
894 { apply zaddu_eq.
895 - unfold make_co_z_points. rewrite red_proj1_sig.
896 unfold Jacobian.make_co_z. cbv [fst].
897 apply eq_sig_hprop; [|rewrite red_proj1_sig].
898 + intros. destruct x24 as ((?X & ?Y) & ?Z).
899 destruct (dec (Z = 0%F)). destruct p, q; reflexivity.
900 apply Eqdep_dec.UIP_dec; apply F.eq_dec.
901 + destruct (Jacobian.of_affine P) as (((?X & ?Y) & ?Z) & ?H).
902 destruct R1' as (((?X & ?Y) & ?Z) & ?H).
903 destruct R0' as (((?X & ?Y) & ?Z) & ?H).
904 destruct vswap; cbv [Jacobian.opp]; repeat rewrite red_proj1_sig; reflexivity.
905 - unfold make_co_z_points. rewrite red_proj1_sig.
906 unfold Jacobian.make_co_z. cbv [snd].
907 apply eq_sig_hprop; [|rewrite red_proj1_sig].
908 + intros. destruct x24 as ((?X & ?Y) & ?Z).
909 destruct (dec (Z = 0%F)). destruct p, q; reflexivity.
910 apply Eqdep_dec.UIP_dec; apply F.eq_dec.
911 + unfold Jacobian.opp; repeat rewrite red_proj1_sig.
912 destruct (Jacobian.of_affine P) as (((?X & ?Y) & ?Z) & ?H).
913 destruct R1' as (((?X & ?Y) & ?Z) & ?H).
914 destruct R0' as (((?X & ?Y) & ?Z) & ?H).
915 destruct vswap; repeat rewrite red_proj1_sig; f_equal; f_equal; ring. }
916 unfold Jacobian.to_affine, Jacobian.to_affine_impl. rewrite red_proj1_sig.
917 destruct (Z.testbit k 0); [rewrite H83|rewrite H82];
918 match goal with
919 | |- context [if ?test then _ else _] => destruct test
920 end; do 2 eexists; ssplit; try ecancel_assumption_impl; try solve_bounds;
921 repeat match goal with
922 | H : feval ?a = _ |- context [feval ?a] => rewrite H
923 end.
924 1,2,5,6: rewrite F.inv_0; ring.
925 Add Field Private_field : (Algebra.Field.field_theory_for_stdlib_tactic (T:=F M_pos)).
926 Import Field_tac.
927 1-4: field; exact n.
928 Qed.
929
930 End WithParameters.
931
932 Require Import bedrock2.ToCString.
933 Require Import coqutil.Macros.WithBaseName.
934 Definition funcs :=
935 let secp256k1_laddermul := (@secp256k1_laddermul 256%Z) in
936 List.app
937 [ secp256k1_opp;
938 secp256k1_mul;
939 secp256k1_add;
940 secp256k1_sub;
941 secp256k1_square;
942 secp256k1_to_bytes;
943 secp256k1_from_bytes;
944 secp256k1_from_mont;
945 secp256k1_to_mont;
946 secp256k1_select_znz;
947 secp256k1_felem_copy;
948 ("felem_cswap", secp256k1_felem_cswap)]
949 &[,secp256k1_make_co_z;
950 secp256k1_zaddu;
951 secp256k1_dblu;
952 secp256k1_tplu;
953 secp256k1_zdau;
954 secp256k1_inv;
955 secp256k1_laddermul
956 ].
957
958 Lemma link_secp256k1_felem_copy :
959 spec_of_secp256k1_felem_copy (map.of_list funcs).
960 Proof. apply secp256k1_felem_copy_correct. reflexivity. Qed.
961 (* ^-- Why is only felem_copy so slow ???!!! *)
962
963 (* Assume m = 2 ^ 256 - 2 ^ 32 - 977 is prime *)
964 Lemma link_secp256k1_laddermul (secp256k1_prime: Znumtheory.prime m) :
965 @spec_of_laddermul (F.field_modulo M_pos) (0%F) (F.of_Z _ 7%Z) (256%Z) (map.of_list funcs).
966 Proof.
967 eapply spec_of_laddermul_ok; repeat
968 match goal with
969 | |- map.get _ _ = _ => reflexivity
970 | |- spec_of_secp256k1_tplu _ => eapply secp256k1_tplu_ok
971 | |- spec_of_dblu _ => eapply secp256k1_dblu_ok
972 | |- spec_of_zaddu _ => eapply secp256k1_zaddu_ok
973 | |- spec_of_secp256k1_opp _ => eapply secp256k1_opp_correct
974 | |- spec_of_secp256k1_zaddu _ => eapply secp256k1_zaddu_ok
975 | |- spec_of_secp256k1_zdau _ => eapply secp256k1_zdau_ok
976 | |- spec_of_secp256k1_make_co_z _ => eapply secp256k1_make_co_z_ok
977 | |- spec_of_secp256k1_inv _ => eapply secp256k1_inv_ok
978 | |- JacobianCoZ.spec_of_secp256k1_add _ => eapply secp256k1_add_correct
979 | |- JacobianCoZ.spec_of_secp256k1_sub _ => eapply secp256k1_sub_correct
980 | |- spec_of_secp256k1_mul _ => eapply secp256k1_mul_correct
981 | |- JacobianCoZ.spec_of_secp256k1_mul _ => eapply secp256k1_mul_correct
982 | |- JacobianCoZ.spec_of_secp256k1_square _ => eapply secp256k1_square_correct
983 | |- Addchain.spec_of_secp256k1_mul _ => eapply secp256k1_mul_correct
984 | |- Addchain.spec_of_secp256k1_square _ => eapply secp256k1_square_correct
985 | |- spec_of_secp256k1_cswap _ => eapply cswap_body_correct
986 | |- spec_of_secp256k1_felem_copy _ => apply link_secp256k1_felem_copy
987 | |- Z.of_nat _ < 2 ^ 64 => unfold field_representation, Signature.field_representation, Representation.frep; cbn; cbv; trivial
988 | |- Core.__rupicola_program_marker _ => cbv [Core.__rupicola_program_marker]; tauto
989 | _ => idtac
990 end.
991 Unshelve.
992 all: cbn; reflexivity.
993 Qed.
994
995 (* Compute (ToCString.c_module funcs). *)