Debian Codegraph @

src/Bedrock/P256/Specs.v

raw
1 Require Import coqutil.Datatypes.List Coq.Lists.List.
2 From bedrock2 Require Import Syntax NotationsCustomEntry.
3
4 From coqutil Require Import LittleEndianList.
5
6 Import BinInt.
7 Local Open Scope Z_scope.
8
9 Module Import List.
10 Import Lists.List ListNotations PeanoNat micromega.Lia.
11 Local Open Scope nat_scope.
12
13 Lemma cons_hd_tl [A] (a : A) l : l <> nil -> hd a l :: tl l = l.
14 Proof. case l; cbn; congruence. Qed.
15
16 Lemma tl_chunk [A : Type] (k : nat) (Hk : k <> 0%nat) (bs : list A) :
17 tl (List.chunk k bs) = List.chunk k (List.skipn k bs).
18 Proof.
19 rewrite <-(Nat.mul_1_l k) at 3. rewrite <-List.skipn_chunk by trivial.
20 case List.chunk; trivial.
21 Qed.
22
23 Lemma firstn_le_split n m z : List.firstn n (le_split m z) = le_split (Nat.min n m) z.
24 Proof.
25 apply nth_error_ext; intros i.
26 case (Nat.ltb_spec i (Nat.min n m)) as [].
27 { rewrite nth_error_le_split, nth_error_firstn by trivial.
28 case Nat.ltb_spec; intros; rewrite ?nth_error_le_split; trivial; lia. }
29 rewrite 2(proj2 (nth_error_None _ _)); trivial;
30 rewrite ?firstn_length, ?length_le_split; trivial.
31 Qed.
32
33 (* TODO: move *)
34 Lemma le_split_0_l z : le_split 0 z = nil.
35 Proof. trivial. Qed.
36
37 (* TODO: move *)
38 Lemma le_split_0_r n : le_split n 0 = List.repeat Byte.x00 n.
39 Proof.
40 induction n; trivial.
41 cbn [List.repeat]; unfold le_split; eapply f_equal2; auto.
42 Qed.
43 End List.
44
45 Module Z.
46 Notation to_bytes := le_split.
47 Notation of_bytes := le_combine.
48 Definition of_bigendian bs := le_combine (List.rev bs).
49 Definition to_bigendian n z := List.rev (le_split n z).
50
51 Lemma testbit_high a n : 0 <= a < 2^n -> Z.testbit a n = false.
52 Proof.
53 case n as [|p|p]; intros.
54 { assert (a = 0) as ->; trivial; Lia.lia. }
55 { erewrite <-(Z.mod_small a), Z.mod_pow2_bits_high; try eassumption; eauto using Zorder.Zle_0_pos, Z.le_refl. }
56 { trivial. }
57 Qed.
58 End Z.
59
60 Module Import NotationsCustomEntry.
61 Infix ".+" := (expr.op bopname.add) (in custom bedrock_expr at level 6, left associativity, only parsing).
62 End NotationsCustomEntry.
63
64 From Coq Require Import BinInt BinNat Init.Byte.
65 From Crypto Require Import PrimeFieldTheorems ModInv.
66
67 Lemma le_combine_app bs1 bs2 :
68 le_combine (bs1 ++ bs2) = le_combine bs1 + 2^(Z.of_nat (Datatypes.length bs1)*8) * le_combine bs2.
69 Proof.
70 rewrite le_combine_app, Z.shiftl_mul_pow2, Z.mul_comm by Lia.lia.
71 rewrite <-Zbitwise.Z.add_lor_land; enough (Z.land _ _ = 0) as -> by Lia.lia.
72 rewrite <-(List.firstn_all bs1), le_combine_firstn, List.firstn_length, PeanoNat.Nat.min_id.
73 rewrite Z.mul_comm, <-(Z.mul_comm (le_combine _)).
74 apply Z.bits_inj'; intros i H; rewrite Z.land_spec, Z.mul_pow2_bits, Z.testbit_mod_pow2, Z.testbit_0_l
75 by auto with zarith.
76 case Z.ltb_spec; intros; trivial.
77 case (Z.ltb_spec i (Z.of_nat (length bs1) * 8)); intros.
78 { rewrite (Z.testbit_neg_r _ (*workaround:*)(_ - _)) by Lia.lia. apply Bool.andb_false_r. }
79 { rewrite ?Z.testbit_neg_r by Lia.lia; trivial. }
80 Qed.
81
82 Module F.
83 Import micromega.Lia.
84 Definition eqb {m} (x y : F m) := if F.eq_dec x y then true else false.
85 Lemma eqb_eq {m} (x y : F m) : eqb x y = true <-> x = y.
86 Proof. cbv [eqb]; case F.eq_dec; intuition congruence. Qed.
87
88 Lemma zero_iff_to_Z : forall {m : positive} (x : F m), x = 0%F :> F m <-> F.to_Z x = 0%Z.
89 Proof.
90 split; intros; subst; try apply F.to_Z_0; [].
91 apply F.eq_to_Z_iff. rewrite F.to_Z_0. assumption.
92 Qed.
93
94 Lemma pow_0_iff (p : positive) (Hp : Znumtheory.prime p) (x : F p) n (Hn : n <> 0%N) : F.pow x n = 0%F <-> x = 0%F.
95 Proof.
96 revert Hn ;induction n using N.peano_ind; try contradiction.
97 intros _; split; cycle 1; intros; subst; rewrite ?F.pow_0_l; trivial; try lia.
98 rewrite F.pow_succ_r in H; eapply Hierarchy.zero_product_zero_factor in H; destruct H; trivial.
99 case (N.eqb_spec n 0) as [->|]; rewrite ?F.pow_0_r in *.
100 { apply eq_sym in H; apply Hierarchy.zero_neq_one in H; case H. }
101 eapply IHn; eauto.
102 Qed.
103
104 Lemma to_Z_sub {m} (x y : F m) : F.to_Z (x - y) = Z.modulo (F.to_Z x - F.to_Z y) m.
105 Proof.
106 cbv [F.sub].
107 rewrite F.to_Z_add, F.to_Z_opp, Zdiv.Zplus_mod_idemp_r; trivial.
108 Qed.
109 End F.
110
111 Module Byte.
112 Import Byte.
113 Local Coercion byte.unsigned : byte >-> Z.
114 Definition xor (a b : byte) := byte.of_Z (Z.lor a b).
115
116 Lemma xor_0_l x : xor x00 x = x.
117 Proof.
118 cbv [xor].
119 apply byte.unsigned_inj.
120 rewrite byte.unsigned_of_Z, Z.lor_0_l, byte.wrap_unsigned; trivial.
121 Qed.
122
123 Import Lists.List micromega.Lia.
124 Lemma map_xor_0_l bs l (H : (length bs <= l)%nat) : map (uncurry Byte.xor) (combine (repeat (Byte.x00) l) bs) = bs.
125 Proof.
126 revert dependent l.
127 induction bs, l; cbn in *; try lia; trivial; [intros Hl].
128 rewrite xor_0_l, IHbs; trivial; lia.
129 Qed.
130 End Byte.
131
132 Local Open Scope Z_scope.
133
134 Definition p256 : positive := 2^256 - 2^224 + 2^192 + 2^96 - 1.
135
136 #[export] Instance prime_p256 : Znumtheory.prime p256. Admitted.
137
138 Local Coercion F.to_Z : F >-> Z.
139
140 Definition coord := F p256.
141 Module Import coord. (* bytes in montgomery form *)
142 Definition R : F p256 := F.of_Z _ (2^256).
143 Coercion to_bytes (x : coord) : list byte := Z.to_bytes 32 (x * R)%F.
144 Lemma length_coord (x : coord) : length x = 32%nat.
145 Proof. apply length_le_split. Qed.
146
147 Lemma zero_iff (x : coord) : (x = 0 <-> x*R = 0)%F.
148 Proof.
149 split; intuition (subst; trivial).
150 eapply Field.is_mul_nonzero_nonzero in H; case H as [?|H]; trivial.
151 apply (f_equal F.to_Z) in H. cbv in H. inversion H.
152 Qed.
153 End coord.
154
155 #[export] Instance coord_char_ge_3 : @Ring.char_ge coord eq 0%F 1%F F.opp F.add F.sub F.mul 3.
156 Proof. intros n Hn. apply (@F.char_gt p256). cbv [p256]. Lia.lia. Qed.
157 #[export] Instance coord_char_ge_12 : @Ring.char_ge coord eq 0%F 1%F F.opp F.add F.sub F.mul 12.
158 Proof. intros n Hn. apply (@F.char_gt p256). cbv [p256]. Lia.lia. Qed.
159
160 From Crypto.Curves Require Import Jacobian.
161 Import Coq.Lists.List.
162
163 Definition a : F p256 := F.opp (1+1+1).
164 Definition b : F p256 := F.of_Z _ 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b.
165 Definition point := @Jacobian.point coord eq F.zero F.add F.mul a b _.
166 Definition add : point -> point -> point :=
167 @Jacobian.add coord eq F.zero F.one F.opp F.add F.sub F.mul F.inv F.div a b _ _ _.
168
169 Module Import point.
170 Coercion to_bytes (p : point) :=
171 let p := proj1_sig p in
172 to_bytes (fst (fst p)) ++ to_bytes (snd (fst p)) ++ to_bytes (snd p).
173 Lemma length_point (x : point) : length x = 96%nat.
174 Proof. cbv [to_bytes]. rewrite ?app_length, ?length_coord; reflexivity. Qed.
175
176 Definition iszero (P : point) :=
177 if Decidable.dec (Jacobian.iszero P) then true else false.
178 End point.
179
180 From Coq Require Import String List. Local Open Scope string_scope. Local Open Scope list_scope.
181 From bedrock2 Require Import ProgramLogic WeakestPrecondition.
182 Import ProgramLogic.Coercions.
183 From coqutil Require Import Word.Interface OfListWord Separation SeparationLogic.
184 From coqutil Require Import letexists.
185
186
187 From bedrock2 Require Import ListIndexNotations.
188 Local Open Scope list_index_scope.
189
190 From bedrock2 Require Import SepAutoArray.
191 From coqutil Require Import symmetry.
192 Import PeanoNat Lia.
193 Import Tactics.
194 Require Import UniquePose.
195
196 Require Curves.Weierstrass.AffineProofs.
197 From bedrock2 Require ToCString.
198 From coqutil Require Macros.WithBaseName.
199
200 Local Notation "xs $@ a" := (map.of_list_word_at a xs)
201 (at level 10, format "xs $@ a").
202 Local Notation "$ n" := (match word.of_Z n return word with w => w end) (only parsing, at level 9, format "$ n").
203 Local Notation "p .+ n" := (word.add p (word.of_Z n)) (only parsing, at level 50, format "p .+ n", left associativity).
204 Local Unset Printing Coercions.
205
206 Local Open Scope Z_scope.
207 Local Open Scope bool_scope.
208 Import micromega.Lia Word.Properties.
209
210 Section WithSemantics.
211 Context {width} {BW : Bitwidth.Bitwidth width} {word : word.word width}.
212 Context {locals : Interface.map.map string word}.
213 Context {mem : Interface.map.map word byte}.
214 Context {ext_spec : Semantics.ExtSpec}.
215
216
217 #[export] Instance spec_of_br_declassify : spec_of "br_declassify" :=
218 fnspec! "br_declassify" a ~> a',
219 { requires t m := True;
220 ensures t' m' := t' = t /\ m' = m /\ a' = a }.
221
222 #[export] Instance spec_of_value_barrier : spec_of "br_value_barrier" :=
223 fnspec! "br_value_barrier" a ~> a',
224 { requires t m := True;
225 ensures t' m' := t' = t /\ m' = m /\ a' = a }.
226
227 #[export] Instance spec_of_br_broadcast_odd : spec_of "br_broadcast_odd" :=
228 fnspec! "br_broadcast_odd" x ~> y,
229 { requires t m := True;
230 ensures t' m' := t' = t /\ m' = m /\ y = word.broadcast (Z.odd x) }.
231
232 #[export] Instance spec_of_br_broadcast_negative : spec_of "br_broadcast_negative" :=
233 fnspec! "br_broadcast_negative" x ~> y,
234 { requires t m := True;
235 ensures t' m' := t' = t /\ m' = m /\ y = word.broadcast (word.lts x (word.of_Z 0)) }.
236
237 #[export] Instance spec_of_br_broadcast_nonzero : spec_of "br_broadcast_nonzero" :=
238 fnspec! "br_broadcast_nonzero" x ~> y,
239 { requires t m := True;
240 ensures t' m' := t' = t /\ m' = m /\ y = word.broadcast (negb (Z.eqb x 0)) }.
241
242 #[export] Instance spec_of_p256_coord_nonzero : spec_of "p256_coord_nonzero" :=
243 fnspec! "p256_coord_nonzero" p_x / (x : coord) ~> nz,
244 { requires t m := m =*> x$@p_x;
245 ensures t' m' := t' = t /\ m' = m /\ nz = word.broadcast (negb (F.eqb x 0)) }.
246
247 #[export] Instance spec_of_fiat_p256_point_iszero : spec_of "p256_point_iszero" :=
248 fnspec! "p256_point_iszero" p_P / (P : point) ~> nz,
249 { requires t m := m =*> P$@p_P;
250 ensures t' m' := t' = t /\ m' = m /\ nz = word.broadcast (point.iszero P) }.
251
252 #[export] Instance spec_of_p256_coord_add : spec_of "p256_coord_add" :=
253 fnspec! "p256_coord_add" p_out p_x p_y / out (x y : coord) R,
254 { requires t m := m =*> x$@p_x /\ m =*> y$@p_y /\ m =* out$@p_out * R /\ length out = length x;
255 ensures t' m := let r : coord := F.add x y in t' = t /\ m =* r$@p_out * R }.
256
257 #[export] Instance spec_of_p256_coord_sub : spec_of "p256_coord_sub" :=
258 fnspec! "p256_coord_sub" p_out p_x p_y / out (x y : coord) R,
259 { requires t m := m =*> x$@p_x /\ m =*> y$@p_y /\ m =* out$@p_out * R /\ length out = length x;
260 ensures t' m := let r : coord := F.sub x y in t' = t /\ m =* r$@p_out * R }.
261
262 #[export] Instance spec_of_p256_coord_halve : spec_of "p256_coord_halve" :=
263 fnspec! "p256_coord_halve" p_out p_x / out (x : coord) R,
264 { requires t m := m =*> x$@p_x /\ m =* out$@p_out * R /\ length out = length x;
265 ensures t' m := let r : coord := F.div x (F.add F.one F.one) in t' = t /\ m =* r$@p_out * R }.
266
267 #[export] Instance spec_of_p256_coord_mul : spec_of "p256_coord_mul" :=
268 fnspec! "p256_coord_mul" p_out p_x p_y / out (x y : coord) R,
269 { requires t m := m =*> x$@p_x /\ m =*> y$@p_y /\ m =* out$@p_out * R /\ length out = length x;
270 ensures t' m := let r : coord := F.mul x y in t' = t /\ m =* r$@p_out * R }.
271
272 #[export] Instance spec_of_p256_coord_sqr : spec_of "p256_coord_sqr" :=
273 fnspec! "p256_coord_sqr" p_out p_x / out (x : coord) R,
274 { requires t m := m =*> x$@p_x /\ m =* out$@p_out * R /\ length out = length x;
275 ensures t' m := let r : coord := F.pow x 2 in t' = t /\ m =* r$@p_out * R }.
276
277 #[export] Instance spec_of_p256_point_add_nz_nz_neq : spec_of "p256_point_add_nz_nz_neq" :=
278 fnspec! "p256_point_add_nz_nz_neq" p_out p_P p_Q / out (P Q : point) R ~> ok,
279 { requires t m := m =* out$@p_out * P$@p_P * Q$@p_Q * R /\ length out = length P;
280 ensures t' m := t' = t /\ exists out,
281 m =* out$@p_out * P$@p_P * Q$@p_Q * R /\ length out = length P /\ (
282 ~ Jacobian.iszero P -> ~ Jacobian.iszero Q ->
283 (ok <> word.of_Z 0 /\ exists pfPneqQ, out = (Jacobian.add_inequal_nz_nz P Q pfPneqQ : point)) \/
284 (ok = word.of_Z 0) /\ Jacobian.eq P Q)
285 }%sep.
286
287 #[export] Instance spec_of_br_memset : spec_of "br_memset" :=
288 fnspec! "br_memset" (p_d v n : word) / d R,
289 { requires t m := m =* d$@p_d * R /\ length d = n :> Z;
290 ensures t' m := t' = t /\ let out := repeat (Byte.byte.of_Z v) (Z.to_nat n) in m =* out$@p_d * R }.
291
292 #[export] Instance spec_of_br_memcxor : spec_of "br_memcxor" :=
293 fnspec! "br_memcxor" (p_d p_s n mask : word) / (d s : list byte) R,
294 { requires t m := m =* d$@p_d * s$@p_s * R /\ length d = n :> Z /\ length s = n :> Z;
295 ensures t' m := t' = t /\
296 exists out, length out = n :> Z /\
297 m =* out$@p_d * s$@p_s * R /\
298 (mask = word.of_Z 0 -> out = d) /\
299 (mask = word.of_Z (-1) -> out = map (uncurry Byte.xor) (combine d s))
300 }.
301
302 #[export] Instance spec_of_p256_point_double : spec_of "p256_point_double" :=
303 fnspec! "p256_point_double" p_out p_P / out (P : point) R,
304 { requires t m := m =* out$@p_out * P$@p_P * R /\ length out = length P;
305 ensures t' m := t' = t /\
306 let out : point := Jacobian.double_minus_3 eq_refl P in
307 m =* out$@p_out * P$@p_P * R
308 }%sep.
309
310 #[export] Instance spec_of_p256_point_add_vartime_if_doubling : spec_of "p256_point_add_vartime_if_doubling" :=
311 fnspec! "p256_point_add_vartime_if_doubling" p_out p_P p_Q / out (P Q : point),
312 { requires t m := m =* out$@p_out * P$@p_P * Q$@p_Q /\ length out = length P;
313 ensures t' m := t' = t /\ exists out : point,
314 m =* out$@p_out * P$@p_P * Q$@p_Q /\ Jacobian.eq out (Jacobian.add P Q)
315 }%sep.
316
317 #[export] Instance spec_of_p256_point_add_affine_nz_nz_neq : spec_of "p256_point_add_affine_nz_nz_neq" :=
318 fnspec! "p256_point_add_affine_nz_nz_neq" p_x / (x : coord) ~> nz,
319 { requires t m := m =*> x$@p_x;
320 ensures t' m' := t' = t /\ m' = m /\ nz = word.of_Z 0 <-> x = F.zero }.
321
322 #[export] Instance spec_of_br_cmov : spec_of "br_cmov" :=
323 fnspec! "br_cmov" (c vnz vz : word) ~> r,
324 { requires t m := c < 2;
325 ensures t' m' := t' = t /\ m' = m /\ r = if Z.eqb c 0 then vz else vnz }.
326
327 (* Internal intermediate functions for field arithmetic: *)
328
329 #[export] Instance spec_of_u256_shr : spec_of "u256_shr" :=
330 fnspec! "u256_shr" p_out p_x n / out (x : Z) R,
331 { requires t m := m =*> (le_split 32 x)$@p_x /\ m =* out$@p_out * R /\ length out = 32%nat /\
332 0 <= x < 2^256 /\ word.unsigned n < width;
333 ensures t' m := let r : Z := x/2^n in
334 t' = t /\ m =* (le_split 32 r)$@p_out * R }.
335
336 Definition spec_of_p256_coord_sub_nonmont : spec_of "p256_coord_sub" :=
337 fnspec! "p256_coord_sub" p_out p_x p_y / out (x y : F p256) R,
338 { requires t m := m =*> (le_split 32 x)$@p_x /\ m =*> (le_split 32 y)$@p_y /\ m =* out$@p_out * R /\ length out = 32%nat;
339 ensures t' m := t' = t /\ m =* (le_split 32 (x-y)%F)$@p_out * R }.
340
341 #[export] Instance spec_of_p256_coord_set_minushalf_conditional : spec_of "u256_set_p256_minushalf_conditional" :=
342 fnspec! "u256_set_p256_minushalf_conditional" p_out mask / b out R,
343 { requires t m := m =* out$@p_out * R /\ length out = 32%nat /\ mask = word.broadcast b;
344 ensures t' m := exists r : F p256,
345 t' = t /\ m =* (le_split 32 r)$@p_out * R /\ (r = if b then F.opp (1/(1+1)) else F.zero)
346 }.
347
348 End WithSemantics.
349
350 From bedrock2 Require Import BasicC64Semantics.
351 From bedrock2Examples Require shrd full_sub full_add full_mul memmove memcpy.
352 #[export] Existing Instance shrd.spec_of_shrd.
353 #[export] Instance spec_of_memmove : spec_of "memmove". apply memmove.spec_of_memmove. Defined.
354 #[export] Hint Mode Interface.map.map - - : typeclass_instances.
355 #[export] Hint Mode word.word - : typeclass_instances.
356
357 Goal spec_of "p256_coord_nonzero". exact _. all : fail. Abort.
358
359 Module word.
360 Lemma signed_opp_nowrap (x : word) : word.signed x <> -2^63 -> word.signed (word.opp x) = - word.signed x.
361 Proof.
362 pose proof word.signed_range x.
363 rewrite word.signed_opp.
364 rewrite word.swrap_as_div_mod.
365 PreOmega.Z.div_mod_to_equations; lia.
366 Qed.
367
368 Lemma nz_signed (x : word) : word.signed x <> 0 <-> word.unsigned x <> 0.
369 Proof.
370 rewrite word.signed_eq_swrap_unsigned.
371 rewrite word.swrap_as_div_mod.
372 intuition ZnWords.ZnWords.
373 Qed.
374
375 Lemma and_m1_l (x : word) : word.and (word.opp (word.of_Z (1))) x = x.
376 Proof.
377 apply word.unsigned_inj.
378 rewrite word.unsigned_and_nowrap, word.unsigned_opp_nowrap, Z.sub_1_r, <-Z.ones_equiv.
379 2: { rewrite word.unsigned_of_Z_1; inversion 1. }
380 rewrite Z.land_comm, Z.land_ones, word.wrap_unsigned; trivial; blia.
381 Qed.
382 End word.
383
384 Add Field Private_field : (Algebra.Field.field_theory_for_stdlib_tactic (T:=F p256)).