Debian Codegraph @

src/Curves/TableMult/TableMult.v

raw
1 From Coq Require Import List.
2 From Coq Require Import ZArith.
3 Require Import Lia.
4 From Coq Require Import Permutation.
5 Import ListNotations.
6
7 Local Open Scope Z_scope.
8
9 Section FoldLemmas.
10
11 Lemma fold_right_val_closed: forall (T : Type) (V: Type)
12 (fT: T -> T -> T) (val: T -> Prop),
13 (forall x y : T, val x -> val y -> val (fT x y)) ->
14 forall (f: V -> T) t l,
15 val t -> List.Forall (fun v : V => val (f v)) l -> val (fold_right fT t (map f l)).
16 Proof.
17 intros.
18 induction l.
19 - auto.
20 - simpl. apply H; [ | apply IHl]; inversion H1; assumption.
21 Qed.
22
23 Lemma fold_right_map_cond: forall (T U V : Type)
24 (fT: T -> T -> T) (fU: U -> U -> U) (TtoU: T -> U) (val: T -> Prop),
25 (forall x y : T, val x -> val y -> fU (TtoU x) (TtoU y) = TtoU (fT x y)) ->
26 (forall x y : T, val x -> val y -> val (fT x y)) ->
27 forall (f : V -> T) l t u,
28 TtoU t = u ->
29 val t ->
30 List.Forall (fun v : V => val (f v)) l ->
31 fold_right fU u (map (fun x : V => TtoU (f x)) l) =
32 TtoU (fold_right fT t (map f l)).
33 Proof.
34 intros.
35 induction l.
36 - auto.
37 - simpl. rewrite IHl. rewrite H.
38 + trivial.
39 + inversion H3. assumption.
40 + simpl. apply fold_right_val_closed; try assumption.
41 inversion H3. assumption.
42 + inversion H3. assumption.
43 Qed.
44
45 Lemma fold_right_map: forall (T U V : Type)
46 (fT: T -> T -> T) (fU: U -> U -> U) (TtoU: T -> U),
47 (forall x y : T, fU (TtoU x) (TtoU y) = TtoU (fT x y)) ->
48 forall (f : V -> T) l t u,
49 TtoU t = u ->
50 fold_right fU u (map (fun x : V => TtoU (f x)) l) =
51 TtoU (fold_right fT t (map f l)).
52 Proof.
53 intros.
54 induction l.
55 - auto.
56 - simpl. rewrite IHl. rewrite H. trivial.
57 Qed.
58
59 Lemma app_cons: forall [A : Type] (l : list A) (a : A),
60 a :: l = [a] ++ l.
61 Proof.
62 simpl. reflexivity.
63 Qed.
64
65 Lemma fold_right_flat_map: forall (T U : Type) (f: T -> list U) (l : list T),
66 fold_right (@List.app _ ) [] (map f l) = flat_map f l.
67 Proof.
68 intros.
69 induction l.
70 - auto.
71 - rewrite app_cons.
72 rewrite map_app.
73 rewrite fold_right_app.
74 rewrite flat_map_app.
75 rewrite IHl.
76 simpl.
77 rewrite app_nil_r.
78 trivial.
79 Qed.
80
81 End FoldLemmas.
82
83 Section TableMult.
84
85 Context
86 (P : Type)
87 (addP : P -> P -> P)
88 (doubleP : P -> P)
89 (negP : P -> P)
90 (mulP : Z -> P -> P)
91 (O : P) (* zero element *)
92 (B : P) (* base point *)
93 (q : Z) (* order of B *)
94 (odd_q : Zodd q)
95 (addP_negP : forall Q R, addP (negP Q) (negP R) = negP (addP Q R))
96 (mulP_q : mulP q B = O)
97 (mulP_zero : forall Q, mulP 0 Q = O)
98 (mulP_one : forall Q, mulP 1 Q = Q)
99 (mulP_addP : forall m n Q, mulP (m + n) Q = addP (mulP m Q) (mulP n Q))
100 (mulP_doubleP : forall n Q, mulP (2 * n) Q = doubleP (mulP n Q))
101 (mulP_negP : forall n Q, mulP (- n) Q = negP (mulP n Q))
102 (mulP_mulP : forall m n Q, mulP (m * n) Q = mulP m (mulP n Q)).
103
104 Definition ZtoP (n : Z) : P := mulP n B.
105
106 (* Table-based multiplication, as outlined in https://eprint.iacr.org/2012/309.pdf, Section 3.3 *)
107
108 (* Here, n denotes # of blocks, s denotes spacing, t denotes # of teeth,
109 and D = s * n * t denotes the total number of digits *)
110 Context (s : Z) (t : Z) (n : Z) (nt : Z) (D : Z)
111 (Hs: 0 <= s) (Ht: 1 <= t) (Hn: 1 <= n)
112 (Hnt: nt = n * t) (HD: D = s * nt)
113 (Hq: 0 < q) (HDq: q < 2 ^ D).
114
115 Section Multicomb.
116
117 (* ======================================== *)
118 (* Definitions related to `eval` and `multicomb` *)
119
120 (* A `state` is a list of (location, bit #). A state is `valid` if all locations are nonnegative. *)
121 Definition state := list (Z * Z).
122 Definition valid (st: state) : Prop
123 := List.Forall (fun '(i, x) => i >= 0) st.
124
125 (* `shift` shifts the location of every bit in a state by k *)
126 Definition shift (k: Z) : state -> state := List.map (fun '(i, x) => (k+i, x)).
127
128 (* `add` adds two states together *)
129 Definition add : state -> state -> state := @List.app _ .
130
131 (* Signed bit for a given odd e *)
132 Definition sbit (e : Z) (x : Z) := 2 * Z.b2z (Z.testbit ((e + 2 ^ D - 1) / 2) x) - 1.
133
134 (* Unscaled signed bit for a given odd e*)
135 Definition sbit' (e : Z) (x : Z) := Z.b2z (Z.testbit ((e + 2 ^ D - 1) / 2) x).
136
137 Definition eval (l: state) (e: Z): Z
138 := List.fold_right Z.add 0 (List.map (fun '(i, x) => 2^i * (sbit e x)) l).
139
140 Definition Zseq (k: Z) (l: Z) := List.map Z.of_nat (List.seq (Z.to_nat k) (Z.to_nat l)).
141
142 Definition entry (bnum: Z) (offset: Z)
143 := List.map (fun x => (s * x, s * x + offset)) (Zseq (t * bnum) t).
144
145 Definition comb (offset: Z) : state
146 := List.map (fun x => (s * x, s * x + offset)) (Zseq 0 nt).
147
148 Definition multicomb : state
149 := List.fold_right (fun x y => add x (shift 1 y)) [] (List.map (fun x => comb x) (Zseq 0 s)).
150
151 Definition stateseq (k: Z) := List.map (fun x => (x, x)) (Zseq 0 k).
152
153 (* ==================== *)
154 (* Lemmas and theorems *)
155
156 (* -------------------- *)
157 (* Zseq *)
158
159 Lemma Zseq_succ_r': forall m b : Z,
160 0 <= m -> 0 <= b -> Zseq b (Z.succ m) = Zseq b m ++ [(b + m)].
161 Proof.
162 (* For some reason, Coq deduces the wrong dependencies when we don't
163 clear out all context variables *)
164 clear Hs Ht Hn Hnt HD HDq Hq odd_q mulP_q s t n nt q D.
165 intros.
166 unfold Zseq.
167 rewrite Z2Nat.inj_succ by trivial.
168 rewrite seq_S.
169 rewrite map_app.
170 simpl.
171 f_equal.
172 rewrite <- Z2Nat.inj_add by trivial.
173 rewrite Z2Nat.id by lia; trivial.
174 Qed.
175
176 Lemma Zseq_succ_r: forall m : Z,
177 0 <= m -> Zseq 0 (Z.succ m) = Zseq 0 m ++ [m].
178 Proof.
179 clear Hs Ht Hn Hnt HD HDq Hq odd_q mulP_q s t n nt q D.
180 intros; apply Zseq_succ_r' with (b := 0); lia.
181 Qed.
182
183 Lemma Zseq_bound': forall r : Z, 0 <= r
184 -> forall b : Z, 0 <= b
185 -> (forall a : Z, In a (Zseq b r) <-> b <= a < b + r).
186 Proof.
187 refine (natlike_ind _ _ _).
188 - intros. split; intro.
189 + inversion H0.
190 + lia.
191 - intros.
192 destruct (Z.eq_dec a (b + x)); split; intro.
193 + lia.
194 + rewrite Zseq_succ_r' by assumption.
195 rewrite in_app_iff.
196 right. subst. apply in_eq.
197 + rewrite Zseq_succ_r' in H2 by assumption.
198 apply in_app_or in H2.
199 simpl in H1.
200 destruct H2; split.
201 1-2: apply H0 with (a := a) in H1;
202 apply H1 in H2; lia.
203 all: inversion H2; try lia; inversion H3.
204 + rewrite Zseq_succ_r' by assumption.
205 rewrite in_app_iff.
206 left.
207 assert (b <= a < b + x) by lia.
208 apply H0; assumption.
209 Qed.
210
211 Lemma Zseq_bound: forall r : Z, 0 <= r
212 -> forall a : Z, In a (Zseq 0 r) <-> 0 <= a < r.
213 Proof.
214 intros; apply Zseq_bound'; lia.
215 Qed.
216
217 Lemma Zseq_app: forall b r1 r2 : Z,
218 0 <= b -> 0 <= r1 -> 0 <= r2
219 -> Zseq b (r1 + r2) = Zseq b r1 ++ Zseq (b + r1) r2.
220 Proof.
221 intros.
222 unfold Zseq.
223 rewrite <- map_app.
224 f_equal.
225 rewrite Z2Nat.inj_add by trivial.
226 rewrite seq_app.
227 rewrite Z2Nat.inj_add by trivial.
228 reflexivity.
229 Qed.
230
231 (* -------------------- *)
232 (* shift, add *)
233
234 Lemma shift_0: forall st : state,
235 (shift 0 st) = st.
236 Proof.
237 intros.
238 induction st; simpl.
239 - trivial.
240 - destruct a; rewrite IHst; trivial.
241 Qed.
242
243 Lemma valid_add: forall st1 st2 : state,
244 valid st1 -> valid st2 -> valid (add st1 st2).
245 Proof.
246 intros.
247 unfold valid in *.
248 unfold add.
249 apply Forall_app.
250 intuition.
251 Qed.
252
253 Lemma valid_shift: forall st : state, forall k : Z,
254 valid st -> 0 <= k -> valid (shift k st).
255 Proof.
256 intros.
257 unfold valid in *.
258 unfold shift.
259 apply Forall_map.
260 eapply Forall_impl; [ | exact H].
261 intros.
262 destruct a.
263 lia.
264 Qed.
265
266 Lemma eval_add: forall st1 st2 : state, forall e : Z,
267 eval (add st1 st2) e = eval st1 e + eval st2 e.
268 Proof.
269 intros st1 st2 e.
270 unfold eval.
271 induction st1.
272 - simpl. reflexivity.
273 - simpl. rewrite IHst1. rewrite Z.add_assoc. reflexivity.
274 Qed.
275
276 Lemma eval_shift: forall st : state, forall k e : Z,
277 valid st -> 0 <= k -> eval (shift k st) e = 2^k * eval st e.
278 Proof.
279 intros.
280 unfold eval.
281 induction st.
282 - simpl. ring.
283 - simpl.
284 rewrite IHst.
285 clear IHst.
286 destruct a.
287 ring_simplify.
288 f_equal.
289 rewrite Z.pow_add_r.
290 rewrite Z.mul_comm.
291 rewrite Z.mul_assoc.
292 trivial.
293 trivial.
294 + unfold valid in H.
295 inversion H.
296 lia.
297 + unfold valid in H.
298 inversion H.
299 unfold valid.
300 trivial.
301 Qed.
302
303 Lemma shift_add: forall st1 st2 k,
304 add (shift k st1) (shift k st2) = shift k (add st1 st2).
305 Proof.
306 intros. unfold shift, add. rewrite map_app. trivial.
307 Qed.
308
309 Lemma shift_shift: forall st k l,
310 shift k (shift l st) = shift (k + l) st.
311 Proof.
312 intros. unfold shift.
313 rewrite map_map.
314 apply map_ext_in.
315 intros.
316 destruct a.
317 rewrite Z.add_assoc.
318 trivial.
319 Qed.
320
321 Lemma fold_right_shift: forall t, 0 <= t -> forall (f : Z -> state) l,
322 fold_right (fun x y => add x (shift 1 y)) l (map f (Zseq 0 t)) =
323 fold_right add (shift t l) (map (fun n => (shift n (f n))) (Zseq 0 t)).
324 Proof.
325 refine (natlike_ind _ _ _).
326 - intros. simpl. rewrite shift_0. trivial.
327 - intros.
328 repeat rewrite Zseq_succ_r by assumption.
329 repeat rewrite map_app.
330 repeat rewrite fold_right_app.
331 rewrite H0.
332 f_equal.
333 simpl.
334 rewrite <- shift_add.
335 f_equal.
336 rewrite shift_shift.
337 f_equal.
338 Qed.
339
340 (* -------------------- *)
341 (* eval, stateseq *)
342
343 Lemma in_stateseq: forall n, 0 <= n -> forall x,
344 In x (stateseq n) -> exists m, 0 <= m < n /\ x = (m, m).
345 Proof.
346 refine (natlike_ind _ _ _).
347 - simpl. intuition.
348 - unfold stateseq.
349 intros.
350 rewrite Zseq_succ_r in H1 by assumption.
351 rewrite map_app in H1.
352 simpl in H1.
353 rewrite in_app_iff in H1.
354 intuition.
355 + apply H0 in H2.
356 destruct H2.
357 exists x1.
358 intuition; lia.
359 + exists x.
360 intuition; try lia.
361 inversion H2; try destruct H1; auto.
362 Qed.
363
364 Lemma mod_sub: forall a b c : Z, c <> 0 -> (a - b * c) mod c = a mod c.
365 Proof.
366 intros.
367 rewrite <- Z.add_opp_r.
368 rewrite <- Z.mul_opp_l.
369 apply Z.mod_add.
370 assumption.
371 Qed.
372
373 Theorem eval_stateseq_partial: forall n, 0 <= n < D
374 -> forall e : Z, - 2 ^ D < e < 2 ^ D
375 -> Zodd e
376 -> eval (stateseq n) e = (e mod 2 ^ (n + 1)) - 2 ^ n.
377 Proof.
378 intros m Hm.
379 destruct Hm.
380 generalize dependent m.
381 refine (natlike_ind _ _ _).
382 - intros. unfold stateseq, eval.
383 rewrite <- Zodd_bool_iff in H1.
384 pose proof Zmod_odd e.
385 rewrite H1 in H2.
386 replace (2 ^ (0 + 1)) with 2 by nia.
387 simpl.
388 lia.
389 - intro m. intros.
390 unfold stateseq in *.
391 rewrite Zseq_succ_r by trivial.
392 rewrite map_app.
393 rewrite eval_add.
394 erewrite H0;[ | lia | trivial | trivial].
395 unfold eval.
396 simpl.
397 rewrite <- Z.add_1_r.
398 rewrite -> Z.add_0_r.
399 unfold sbit.
400
401 remember ((e + 2 ^ D - 1) / 2) as f.
402 assert (e = 2 * f + 1 - 2 ^ D). {
403 rewrite Heqf.
404 erewrite <- Z_div_exact_2; try lia.
405 apply Zodd_ex in H3.
406 destruct H3.
407 rewrite H3 in *.
408 replace D with (D - 1 + 1) by lia.
409 remember (D - 1) as E.
410 assert (0 <= E) by lia.
411 rewrite Z.pow_add_r; try lia.
412 rewrite Z.pow_1_r.
413 replace (2 * x + 1 + 2 ^ E * 2 - 1) with ((x + 2 ^ E) * 2) by lia.
414 apply Z_mod_mult.
415 }
416 replace (n + 1 - 1) with n by lia.
417 rewrite H4.
418 rewrite Z.mul_sub_distr_l.
419 rewrite Z.add_sub_assoc.
420 rewrite Z.mul_1_r.
421 rewrite Z.testbit_spec' by lia.
422 replace D with (D - (m + 1) + (m + 1)) at 1 by lia.
423 replace D with (D - (m + 1 + 1) + (m + 1 + 1)) at 2 by lia.
424 rewrite Z.pow_add_r by lia.
425 rewrite Z.pow_add_r with (c := m + 1 + 1) by lia.
426 repeat rewrite mod_sub by lia.
427 assert (f / 2 ^ m = (2 * f + 1) / (2 ^ m * 2)). {
428 rewrite Z.mul_comm with (n := 2 ^ m).
429 rewrite <- Z.div_div by lia.
430 rewrite Z.mul_comm.
431 rewrite Z.div_add_l by lia.
432 replace (1 / 2) with 0 by auto.
433 rewrite Z.add_0_r.
434 trivial.
435 }
436 rewrite Z.mul_assoc.
437 repeat rewrite Z.pow_add_r by lia.
438 repeat rewrite Z.pow_1_r.
439 rewrite H5.
440 rewrite (Z.rem_mul_r (2 * f + 1) (2 ^ m * 2) 2) by lia.
441 lia.
442 Qed.
443
444 Theorem eval_stateseq: forall e : Z,
445 - 2 ^ D < e < 2 ^ D
446 -> Zodd e
447 -> eval (stateseq D) e = e.
448 Proof.
449 intros.
450 assert (HD1: 1 <= D). {
451 destruct D.
452 - rewrite Z.pow_0_r in H.
453 apply Zodd_ex in H0.
454 destruct H0; lia.
455 - lia.
456 - nia.
457 }
458 assert (stateseq D = stateseq (D - 1) ++ [(D - 1, D - 1)]).
459 {
460 unfold stateseq.
461 replace D with (Z.succ (D - 1)) at 1 by lia.
462 rewrite Zseq_succ_r by lia.
463 rewrite map_app.
464 simpl.
465 trivial.
466 }
467 rewrite H1.
468 rewrite eval_add.
469 erewrite eval_stateseq_partial with (n := (D - 1)) (e := e); try lia; try assumption.
470 unfold eval.
471 simpl.
472 case_eq e; intros.
473 - subst. inversion H0.
474 - assert (1 <= e) by lia.
475 rewrite <- H2.
476 assert (sbit e (D - 1) = 1).
477 {
478 clear H1.
479 unfold sbit.
480 rewrite Z.testbit_spec' by lia.
481 rewrite Z.div_div by lia.
482 rewrite <- Z.pow_succ_r by lia.
483 replace (Z.succ (D - 1)) with D by lia.
484 replace (e + 2 ^ D - 1) with (e - 1 + (1 * 2 ^ D)) by lia.
485 rewrite Z.div_add by lia.
486 rewrite Z.div_small by lia.
487 auto.
488 }
489 rewrite H4.
490 rewrite Z.sub_add.
491 rewrite Z.mod_small; lia.
492 - assert (e <= -1) by lia.
493 rewrite <- H2.
494 assert (sbit e (D - 1) = -1).
495 {
496 clear H1.
497 unfold sbit.
498 rewrite Z.testbit_spec' by lia.
499 rewrite Z.div_div by lia.
500 rewrite <- Z.pow_succ_r by lia.
501 replace (Z.succ (D - 1)) with D by lia.
502 rewrite Z.div_small by lia.
503 auto.
504 }
505 rewrite H4.
506 rewrite Z.sub_add.
507 replace e with (e + 2 ^ D - 1 * 2 ^ D) at 1 by lia.
508 rewrite mod_sub by nia.
509 rewrite Z.mod_small by lia.
510 replace D with (D - 1 + 1) at 1 by lia.
511 rewrite Z.pow_add_r by lia.
512 nia.
513 Qed.
514
515 Theorem eval_permutation: forall st1 st2 : state, forall e : Z,
516 Permutation st1 st2 -> eval st1 e = eval st2 e.
517 Proof.
518 induction 1; simpl; auto.
519 - rewrite app_cons.
520 rewrite (app_cons l' x).
521 rewrite eval_add.
522 rewrite eval_add.
523 rewrite IHPermutation.
524 trivial.
525 - rewrite app_cons.
526 rewrite (app_cons l x).
527 rewrite (app_cons (y :: l) x).
528 rewrite (app_cons l y).
529 repeat progress rewrite eval_add.
530 ring.
531 - rewrite IHPermutation1.
532 eapply IHPermutation2.
533 Qed.
534
535 Theorem permute_comb_correct: forall e : Z, forall st : state,
536 - 2 ^ D < e < 2 ^ D -> Zodd e -> Permutation (stateseq D) st -> eval st e = e.
537 Proof.
538 intros.
539 rewrite <- eval_permutation with (st1 := stateseq D).
540 apply eval_stateseq.
541 all: assumption.
542 Qed.
543
544 (* -------------------- *)
545 (* comb, entry *)
546
547 Lemma comb_entry: forall offset,
548 comb offset = List.flat_map (fun x => entry x offset) (Zseq 0 n).
549 Proof.
550 assert (0 <= n) by lia.
551 unfold comb.
552 rewrite Hnt.
553 clear Hn Hnt.
554 generalize dependent n.
555 refine (natlike_ind _ _ _).
556 - simpl. trivial.
557 - intros.
558 rewrite Zseq_succ_r by assumption.
559 rewrite flat_map_app.
560 simpl.
561 replace (Z.succ x * t) with (x * t + t) by nia.
562 rewrite app_nil_r.
563 rewrite Zseq_app by nia.
564 rewrite map_app.
565 unfold entry.
566 f_equal.
567 + apply H0.
568 + f_equal.
569 f_equal.
570 nia.
571 Qed.
572
573 (* -------------------- *)
574 (* multicomb *)
575
576 Lemma multicomb_length:
577 length multicomb = Z.to_nat D.
578 Proof.
579 unfold multicomb.
580 erewrite <- fold_right_map with (u := 0%nat) (fU := Nat.add).
581 - assert (map (fun x : Z => length (comb x)) (Zseq 0 s) = map (fun x : Z => Z.to_nat nt) (Zseq 0 s)).
582 { apply map_ext_in.
583 intros.
584 unfold comb, Zseq.
585 repeat rewrite map_length.
586 apply seq_length. }
587 rewrite H; clear H.
588 rewrite HD; clear HD.
589 generalize dependent s.
590 refine (natlike_ind _ _ _).
591 + auto.
592 + intros.
593 rewrite Zseq_succ_r by assumption.
594 rewrite map_app.
595 rewrite <- fold_symmetric by lia.
596 rewrite fold_left_app.
597 simpl.
598 rewrite -> fold_symmetric by lia.
599 rewrite H0.
600 lia.
601 - intros.
602 unfold add.
603 rewrite app_length.
604 f_equal.
605 unfold shift.
606 rewrite map_length.
607 trivial.
608 - auto.
609 Qed.
610
611 Lemma multicomb_simple:
612 multicomb = List.fold_right add [] (map (fun x =>
613 (map (fun o => (s * o + x, s * o + x)) (Zseq 0 nt)))
614 (Zseq 0 s)).
615 Proof.
616 unfold multicomb, comb.
617 rewrite fold_right_shift by assumption.
618 simpl.
619 f_equal.
620 apply map_ext_in; intros; unfold shift; rewrite map_map.
621 apply map_ext_in; intros; rewrite Z.add_comm; trivial.
622 Qed.
623
624 Lemma multicomb_elts: forall n,
625 0 <= n < D -> In (n, n) multicomb.
626 Proof.
627 intros.
628 rewrite multicomb_simple.
629 unfold add.
630 setoid_rewrite fold_right_flat_map with
631 (f := fun x : Z => map (fun o : Z => (s * o + x, s * o + x)) (Zseq 0 nt)) (l := (Zseq 0 s)).
632 rewrite in_flat_map.
633 exists (n0 mod s); split.
634 - apply Zseq_bound; try apply Z.mod_pos_bound; lia.
635 - apply in_map_iff.
636 exists (n0 / s); split.
637 + rewrite <- Z_div_mod_eq_full. trivial.
638 + apply Zseq_bound; try nia; split.
639 * apply Z_div_nonneg_nonneg; lia.
640 * apply Z.div_lt_upper_bound; lia.
641 Qed.
642
643 Theorem multicomb_stateseq:
644 Permutation (stateseq D) multicomb.
645 Proof.
646 eapply NoDup_Permutation_bis.
647 - unfold stateseq, Zseq.
648 apply NoDup_map_inv with (f := (fun '(i, x) => Z.to_nat i)).
649 repeat rewrite map_map.
650 assert (forall l, map (fun x : nat => Z.to_nat (Z.of_nat x)) l = l).
651 { intros.
652 induction l.
653 - auto.
654 - simpl. rewrite IHl. rewrite Nat2Z.id. trivial. }
655 rewrite H.
656 apply seq_NoDup.
657 - rewrite multicomb_length.
658 unfold stateseq, Zseq.
659 repeat rewrite map_length.
660 rewrite seq_length.
661 auto.
662 - rewrite incl_Forall_in_iff.
663 rewrite Forall_forall.
664 intros.
665 apply in_stateseq in H; [ | lia ].
666 destruct H. rename x0 into m.
667 destruct H.
668 rewrite H0.
669 apply multicomb_elts; assumption.
670 Qed.
671
672 Theorem multicomb_correct: forall e : Z,
673 - 2 ^ D < e < 2 ^ D -> Zodd e -> eval multicomb e = e.
674 Proof.
675 intros.
676 apply permute_comb_correct; try assumption.
677 exact multicomb_stateseq.
678 Qed.
679
680 End Multicomb.
681
682 Section Table.
683
684 (* ======================================== *)
685 (* Definitions related to tables *)
686
687 (*
688 1 0 0 1 = 9
689 Table: c = [+1; -1; -1; +1], offset |-> sum (c[i] * 2^(i*k))
690 should be equal to eval (comb s t offset) e * P,
691 where the c is the bits in the respective positions of e
692 *)
693
694 (* TODO *)
695 (*
696 - submit PR
697 - make it clear which part is used at runtime
698 - (potentially) split file into code and proofs
699 *)
700
701 (* To be replaced by looking up in an actual precomputed table *)
702 Definition table_entry (bnum: Z) (d: Z) : P
703 := List.fold_right addP O
704 (List.map
705 (fun x => ZtoP ((2 * (Z.b2z (Z.testbit d (x - t * bnum))) - 1) * 2 ^ (s * x)))
706 (Zseq (t * bnum) t)).
707
708 Definition table_entry' (bnum: Z) (d: Z) : P
709 := List.fold_right addP O
710 (List.map
711 (fun x => ZtoP ((2 * (Z.b2z (Z.testbit d (x - t * bnum))) - 1) * 2 ^ (s * x)))
712 (Zseq (t * bnum) t)).
713
714 Lemma table_entry_hyp: forall bnum d : Z,
715 0 <= d < 2 ^ (t - 1) ->
716 table_entry bnum d = table_entry' bnum d.
717 Proof. intros. reflexivity. Qed.
718
719 Opaque table_entry.
720
721 Definition table_lookup (bnum: Z) (d: Z) : P
722 := if Z.testbit d (t - 1) then negP (table_entry bnum (2 ^ t - 1 - d)) else table_entry bnum d.
723
724 (* Definition table := List.map table_entry (Zseq 0 (2 ^ t)). *)
725
726 Definition extract_bits (offset: Z) (e: Z) : Z
727 := List.fold_right Z.add 0 (
728 List.map (fun x => (sbit' e (x * s + offset)) * (2 ^ x))
729 (Zseq 0 t)).
730
731 Definition table_comb (offset: Z) (e: Z) : P
732 := List.fold_right addP O
733 (List.map (fun x => table_lookup x (extract_bits (offset + x * s * t) e)) (Zseq 0 n)).
734
735 Definition table_multicomb (e: Z) : P :=
736 List.fold_right (fun x y => addP x (doubleP y)) O (List.map (fun x => table_comb x e) (Zseq 0 s)).
737
738 (* ==================== *)
739 (* Lemmas and theorems *)
740
741 (* -------------------- *)
742 (* extract_bits *)
743
744 Lemma bitmap_bound: forall (t : Z), 0 <= t ->
745 forall (f: Z -> bool),
746 0 <= fold_right Z.add 0 (map (fun y : Z => Z.b2z (f y) * 2 ^ y) (Zseq 0 t)) < 2 ^ t.
747 Proof.
748 clear HD HDq D.
749 refine (natlike_ind _ _ _).
750 - intros. simpl. lia.
751 - intros. simpl.
752 rewrite Zseq_succ_r by lia.
753 rewrite map_app.
754 simpl.
755 rewrite <- fold_symmetric by lia.
756 rewrite fold_left_app.
757 simpl.
758 rewrite fold_symmetric by lia.
759 specialize H0 with f.
760 assert (0 <= Z.b2z (f x) * 2 ^ x <= 2 ^ x). {
761 case (f x).
762 - replace (Z.b2z true) with 1; [ lia | compute; trivial].
763 - replace (Z.b2z false) with 0; [ lia | compute; trivial].
764 }
765 replace (2 ^ Z.succ x) with (2 ^ x + 2 ^ x); [ lia | ].
766 rewrite <- Z.add_1_r.
767 rewrite Z.pow_add_r by lia.
768 nia.
769 Qed.
770
771 Lemma bitmap_testbit: forall (t : Z), 0 <= t ->
772 forall (x : Z), 0 <= x < t ->
773 forall (f: Z -> bool),
774 Z.testbit (fold_right Z.add 0
775 (map (fun y : Z => Z.b2z (f y) * 2 ^ y) (Zseq 0 t))) x
776 = f x.
777 Proof.
778 clear HD HDq D.
779 refine (natlike_ind _ _ _).
780 - intros. lia.
781 - intros.
782 rewrite Zseq_succ_r by lia.
783 rewrite map_app.
784 simpl.
785 rewrite <- fold_symmetric by lia.
786 rewrite fold_left_app.
787 simpl.
788 rewrite fold_symmetric by lia.
789 simpl.
790 destruct (Z.eq_dec x0 x).
791 + subst.
792 rewrite <- Z.div_pow2_bits with (n := x) (m := 0) by lia.
793 rewrite Z.div_add by nia.
794 pose proof bitmap_bound x H f.
795 rewrite Z.div_small by assumption.
796 rewrite Z.add_0_l.
797 apply Z.b2z_bit0.
798 + assert (x0 < x) by lia.
799 rewrite <- Z.mod_pow2_bits_low with (m := x0) (n := x) by lia.
800 rewrite Z.mod_add by nia.
801 rewrite Z.mod_small; [ apply H0 | apply bitmap_bound ]; lia.
802 Qed.
803
804 Lemma extract_bits_bound: forall offset e,
805 0 <= extract_bits offset e < 2 ^ t.
806 Proof.
807 intros; unfold extract_bits; apply bitmap_bound; lia.
808 Qed.
809
810 Lemma extract_bits_spec: forall offset e x : Z,
811 0 <= x -> 0 <= offset <= s * nt ->
812 (x < t -> Z.testbit (extract_bits offset e) x
813 = Z.testbit ((e + 2 ^ (s * nt) - 1) / 2) (s * x + offset))
814 /\ (t <= x -> Z.testbit (extract_bits offset e) x = false).
815 Proof.
816 intros.
817 unfold extract_bits, sbit'.
818 rewrite HD in *.
819 intuition.
820 - rewrite Z.mul_comm with (m := x).
821 apply bitmap_testbit with
822 (f := fun x : Z => Z.testbit ((e + 2 ^ (s * nt) - 1) / 2) (x * s + offset));
823 lia.
824 - rewrite <- Z.div_pow2_bits with (n := x) (m := 0) by lia.
825 assert (Ht' : 0 <= t) by lia.
826 pose proof bitmap_bound t Ht' (fun x : Z =>
827 Z.testbit ((e + 2 ^ (s * nt) - 1) / 2) (x * s + offset)).
828 assert (0 < 2) by lia.
829 pose proof Z.pow_le_mono_r 2 t x H4 H0.
830 rewrite Z.div_small; [ auto | lia ].
831 Qed.
832
833 (* -------------------- *)
834 (* fold_right with addP and shiftP *)
835
836 Lemma fold_right_ZtoP: forall fZ fP,
837 (forall x y, fP (ZtoP x) (ZtoP y) = ZtoP (fZ x y)) ->
838 forall f l,
839 fold_right fP O (map (fun x : Z => ZtoP (f x)) l ) =
840 ZtoP (fold_right fZ 0 (map f l)).
841 Proof.
842 intros. apply fold_right_map. exact H. unfold ZtoP. apply mulP_zero.
843 Qed.
844
845 Lemma fold_right_addP: forall f l,
846 fold_right addP O (map (fun x : Z => ZtoP (f x)) l) =
847 ZtoP (fold_right Z.add 0 (map f l)).
848 Proof.
849 apply fold_right_ZtoP. unfold ZtoP. intros. rewrite mulP_addP. trivial.
850 Qed.
851
852 Lemma fold_right_addP_shiftP: forall f l,
853 fold_right (fun x y : P => addP x (doubleP y)) O (map (fun x : Z => ZtoP (f x)) l) =
854 ZtoP (fold_right (fun x y : Z => Z.add x (Z.double y)) 0 (map f l)).
855 Proof.
856 apply fold_right_ZtoP. unfold ZtoP. intros.
857 rewrite mulP_addP. rewrite mulP_doubleP. trivial.
858 Qed.
859
860 (* -------------------- *)
861 (* table_lookup *)
862
863 Lemma table_entry'_spec: forall bnum offset e : Z,
864 0 <= bnum < n -> 0 <= offset < s
865 -> table_entry' bnum (extract_bits (offset + bnum * s * t) e) = ZtoP (eval (entry bnum offset) e).
866 Proof.
867 intros.
868 unfold eval, entry, table_entry'.
869 rewrite fold_right_addP.
870 rewrite map_map.
871 f_equal.
872 f_equal.
873 apply map_ext_in.
874 intros.
875 rewrite Z.mul_comm.
876 f_equal.
877 unfold sbit.
878 f_equal.
879 f_equal.
880 f_equal.
881 assert (Hb: 0 <= t * bnum) by nia.
882 assert (Ht': 0 <= t) by lia.
883 pose proof Zseq_bound' t Ht' (t * bnum) Hb.
884 apply H2 in H1.
885 replace a with (a - t * bnum + t * bnum) by lia.
886 remember (a - t * bnum) as a'.
887 rewrite Z.add_simpl_r.
888 rewrite HD.
889 replace (s * (a' + t * bnum) + offset) with (s * a' + (offset + bnum * s * t)) by nia.
890 eapply extract_bits_spec; assert (0 < s * t) by nia; nia.
891 Qed.
892
893
894 Lemma testbit_flip: forall d t' b : Z,
895 0 <= d < 2 ^ t' -> 0 <= b < t'
896 -> Z.testbit (2 ^ t' - 1 - d) b = negb (Z.testbit d b).
897 Proof.
898 intros.
899 rewrite <- Z.lnot_spec by lia.
900 rewrite <- Z.mod_pow2_bits_low with (n := t') by lia.
901 rewrite <- Z.mod_pow2_bits_low with (n := t') (a := Z.lnot d) by lia.
902 f_equal.
903 rewrite <- mod_sub with (b := 1) by nia.
904 f_equal.
905 pose proof Z.add_lnot_diag d.
906 lia.
907 Qed.
908
909 Lemma b2z_testbit_flip: forall d t' b : Z,
910 0 <= d < 2 ^ t' -> 0 <= b < t'
911 -> 2 * (Z.b2z (Z.testbit (2 ^ t' - 1 - d) b)) - 1
912 = - (2 * (Z.b2z (Z.testbit d b)) - 1).
913 Proof.
914 intros.
915 rewrite testbit_flip by assumption.
916 case (Z.testbit d b); reflexivity.
917 Qed.
918
919 Lemma testbit_top_bit: forall d t' : Z,
920 1 <= t' -> 0 <= d < 2 ^ t'
921 -> Z.testbit d (t' - 1) = (2 ^ (t' - 1) <=? d).
922 Proof.
923 intros.
924 assert (0 <= t' - 1) by lia.
925 case_eq (2 ^ (t' - 1) <=? d); intro.
926 - rewrite Z.leb_le in H2.
927 replace (t' - 1) with (0 + (t' - 1)) by lia.
928 rewrite <- Z.div_pow2_bits by lia.
929 replace (d / 2 ^ (t' - 1)) with 1; auto.
930 replace d with (d - 2 ^ (t' - 1) + 1 * 2 ^ (t' - 1)) by lia.
931 rewrite Z.div_add by nia.
932 replace 1 with (0 + 1) at 1 by lia.
933 apply Z.add_cancel_r.
934 symmetry.
935 apply Z.div_small.
936 replace (2 ^ t') with (2 ^ (t' - 1) + 2 ^ (t' - 1)) in H0.
937 lia.
938 replace t' with (t' - 1 + 1) at 3 by lia.
939 rewrite Z.pow_add_r by lia.
940 nia.
941 - rewrite Z.leb_gt in H2.
942 replace (t' - 1) with (0 + (t' - 1)) by lia.
943 rewrite <- Z.div_pow2_bits by lia.
944 replace (d / 2 ^ (t' - 1)) with 0; auto.
945 symmetry.
946 apply Z.div_small.
947 lia.
948 Qed.
949
950 Lemma table_entry'_flip: forall bnum d : Z,
951 0 <= d < 2 ^ t -> 0 <= bnum
952 -> table_entry' bnum (2 ^ t - 1 - d) = negP (table_entry' bnum d).
953 Proof.
954 intros.
955 unfold table_entry'.
956 assert (rewrite_map:
957 map (fun x : Z => ZtoP ((2 * Z.b2z (Z.testbit (2 ^ t - 1 - d) (x - t * bnum)) - 1) * 2 ^ (s * x)))
958 (Zseq (t * bnum) t) =
959 map (fun x : Z => negP (ZtoP ((2 * Z.b2z (Z.testbit d (x - t * bnum)) - 1) * 2 ^ (s * x))))
960 (Zseq (t * bnum) t)).
961 {
962 apply map_ext_in.
963 intros.
964 rewrite Zseq_bound' in H1 by lia.
965 rewrite b2z_testbit_flip by lia.
966 unfold ZtoP.
967 rewrite <- mulP_negP.
968 f_equal.
969 nia.
970 }
971 rewrite rewrite_map.
972 apply fold_right_map.
973 - exact addP_negP.
974 - rewrite <- mulP_zero with (Q := B).
975 rewrite <- mulP_negP.
976 f_equal; lia.
977 Qed.
978
979 Lemma table_lookup_spec: forall bnum offset e : Z,
980 0 <= bnum < n -> 0 <= offset < s
981 -> table_lookup bnum (extract_bits (offset + bnum * s * t) e) = ZtoP (eval (entry bnum offset) e).
982 Proof.
983 intros.
984 pose proof extract_bits_bound (offset + bnum * s * t) e.
985 unfold table_lookup.
986 pose proof testbit_top_bit (extract_bits (offset + bnum * s * t) e) t Ht H1.
987 case_eq (Z.testbit (extract_bits (offset + bnum * s * t) e) (t - 1)); intro; rewrite H3 in *.
988 - symmetry in H2.
989 rewrite Z.leb_le in H2.
990 replace (2 ^ t) with (2 ^ (t - 1) + 2 ^ (t - 1)) in *;
991 [ | replace t with (t - 1 + 1) at 3 by lia; rewrite Z.pow_add_r; nia ].
992 rewrite table_entry_hyp by lia.
993 rewrite <- table_entry'_flip.
994 + rewrite <- table_entry'_spec by lia.
995 f_equal.
996 replace (2 ^ t) with (2 ^ (t - 1) + 2 ^ (t - 1)) in *;
997 [ lia | replace t with (t - 1 + 1) at 3 by lia; rewrite Z.pow_add_r; nia ].
998 + replace (2 ^ t) with (2 ^ (t - 1) + 2 ^ (t - 1)) in *;
999 [ lia | replace t with (t - 1 + 1) at 3 by lia; rewrite Z.pow_add_r; nia ].
1000 + lia.
1001 - symmetry in H2.
1002 rewrite Z.leb_gt in H2.
1003 rewrite table_entry_hyp by lia.
1004 apply table_entry'_spec; assumption.
1005 Qed.
1006
1007 (* -------------------- *)
1008 (* table_comb *)
1009
1010 Lemma table_comb_spec: forall offset e : Z,
1011 0 <= offset < s -> table_comb offset e = ZtoP (eval (comb offset) e).
1012 Proof.
1013 intros.
1014 unfold table_comb.
1015 assert (rewrite_map: map (fun x : Z => table_lookup x (extract_bits (offset + x * s * t) e))
1016 (Zseq 0 n) = map (fun x : Z => ZtoP (eval (entry x offset) e)) (Zseq 0 n)). {
1017 apply map_ext_in.
1018 intros.
1019 apply table_lookup_spec; apply Zseq_bound in H0; lia.
1020 }
1021 rewrite rewrite_map.
1022 rewrite fold_right_addP.
1023 f_equal.
1024 f_equal.
1025 rewrite comb_entry.
1026 rewrite <- fold_right_flat_map.
1027 rewrite fold_right_map with (t := []) (fT := add) (TtoU := fun x => eval x e).
1028 - f_equal.
1029 - intros. rewrite eval_add. trivial.
1030 - trivial.
1031 Qed.
1032
1033 (* -------------------- *)
1034 (* table_multicomb *)
1035
1036 Theorem table_multicomb_spec: forall e : Z,
1037 table_multicomb e = ZtoP (eval multicomb e).
1038 Proof.
1039 intros.
1040 unfold table_multicomb.
1041 unfold ZtoP.
1042 unfold multicomb.
1043 assert (rewrite_map: fold_right (fun x y : P => addP x (doubleP y)) O
1044 (map (fun x : Z => table_comb x e) (Zseq 0 s)) =
1045 fold_right (fun x y : P => addP x (doubleP y)) O
1046 (map (fun x : Z => ZtoP (eval (comb x) e)) (Zseq 0 s))).
1047 { f_equal.
1048 apply map_ext_in.
1049 intros.
1050 pose proof Zseq_bound s Hs.
1051 apply H0 in H.
1052 apply table_comb_spec; assumption. }
1053 rewrite rewrite_map.
1054 rewrite fold_right_addP_shiftP.
1055 unfold ZtoP.
1056 f_equal.
1057 eapply fold_right_map_cond with (TtoU := fun st : state => eval st e) (val := valid).
1058 - intros.
1059 rewrite eval_add.
1060 rewrite eval_shift by (try assumption; lia).
1061 nia.
1062 - intros.
1063 apply valid_add; try apply valid_shift; try assumption; lia.
1064 - compute. trivial.
1065 - unfold valid. auto.
1066 - unfold comb.
1067 rewrite Forall_forall.
1068 intros.
1069 unfold valid.
1070 rewrite Forall_map.
1071 rewrite Forall_forall.
1072 intros.
1073 apply Zseq_bound in H0; nia.
1074 Qed.
1075
1076 Theorem table_multicomb_correct: forall e : Z,
1077 - 2 ^ D < e < 2 ^ D -> Zodd e -> table_multicomb e = ZtoP e.
1078 Proof.
1079 intros. rewrite table_multicomb_spec. f_equal.
1080 apply multicomb_correct; assumption.
1081 Qed.
1082
1083 End Table.
1084
1085 Section Oddify.
1086
1087 (* ======================================== *)
1088 (* Oddify: support any value of e by reducing mod q to an odd number in bounds. *)
1089
1090 Definition oddify (e: Z) : Z := if Z.odd e then e else (e - q).
1091 Definition positify (e: Z) : Z := (2 ^ D - 1 + oddify (e mod q)) / 2.
1092 (*
1093 This can always be computed without negative intermediate values:
1094 if oddify (e mod q) <= 0 then this amounts to flipping the last D bits from - (oddify (e mod q));
1095 else, we compute 2 ^ (D - 1) + (oddify (e mod q) >> 1).
1096 *)
1097
1098 (*
1099 Definition positify' (e: Z) : Z :=
1100 let e := e mod q in
1101 if Z.odd e then
1102 2 ^ (D - 1) + (e >> 1)
1103 else
1104 ((2 ^ D - 1) - (q - e)) >> 1.
1105
1106 Definition positify'' (e: Z) : Z :=
1107 let e := e mod q in
1108 let o := 2 ^ D - 1 + e in
1109 let i := if Z.odd e then o else o - q in
1110 Z.shiftr i 1.
1111 *)
1112
1113 Definition extract_bits_positify (offset: Z) (e: Z) : Z
1114 := List.fold_right Z.add 0 (
1115 List.map (fun x => Z.b2z (Z.testbit (positify e) (x * s + offset)) * (2 ^ x))
1116 (Zseq 0 t)).
1117
1118 Definition table_comb_positify (offset: Z) (e: Z) : P
1119 := List.fold_right addP O
1120 (List.map (fun x => table_lookup x (extract_bits_positify (offset + x * s * t) e)) (Zseq 0 n)).
1121
1122 (* This is the definition used at runtime! *)
1123 Definition table_multicomb_positify (e: Z) : P :=
1124 List.fold_right (fun x y => addP x (doubleP y)) O (List.map (fun x => table_comb_positify x e) (Zseq 0 s)).
1125
1126 (* Evenify has been replaced by positify *)
1127 (*
1128 Definition evenify (e: Z) : Z := if Z.odd e then e + q else e.
1129
1130 Definition sbit_evenify (e: Z) (x: Z) := 2 * Z.b2z (Z.testbit (evenify (e + 2 ^ D - 1) / 2) x) - 1.
1131 Definition sbit'_evenify (e: Z) (x: Z) := Z.b2z (Z.testbit (evenify (e + 2 ^ D - 1) / 2) x).
1132 *)
1133
1134 (* ==================== *)
1135 (* Lemmas and theorems *)
1136
1137 (* -------------------- *)
1138 (* oddify *)
1139
1140 Lemma oddify_odd: forall (e : Z), Zodd (oddify (e mod q)).
1141 Proof.
1142 intros.
1143 unfold oddify.
1144 case_eq (Z.odd (e mod q)); intros.
1145 - apply Zodd_bool_iff. assumption.
1146 - apply Zeven_plus_Zodd.
1147 apply Zeven_equiv.
1148 apply Z.even_spec.
1149 rewrite Zodd_even_bool in H.
1150 rewrite Bool.negb_false_iff in H.
1151 assumption.
1152 rewrite Z.opp_eq_mul_m1.
1153 apply Zodd_mult_Zodd; [ assumption | ].
1154 rewrite Zodd_ex_iff.
1155 exists (- 1).
1156 lia.
1157 Qed.
1158
1159 Lemma oddify_bounds: forall (e : Z), - 2 ^ D < oddify (e mod q) < 2 ^ D.
1160 Proof.
1161 intros.
1162 unfold oddify.
1163 pose proof (Z.mod_pos_bound e q Hq).
1164 case_eq (Z.odd (e mod q)); intros; lia.
1165 Qed.
1166
1167 Lemma oddify_mod_q: forall (e : Z), (oddify (e mod q)) mod q = e mod q.
1168 Proof.
1169 intros.
1170 unfold oddify.
1171 case_eq (Z.odd (e mod q)); intros.
1172 - rewrite Z.mod_mod; lia.
1173 - replace q with (1 * q) at 2 by lia.
1174 rewrite mod_sub by lia.
1175 rewrite Z.mod_mod; lia.
1176 Qed.
1177
1178 Lemma ZtoP_oddify: forall (e : Z), ZtoP (oddify (e mod q)) = ZtoP e.
1179 Proof.
1180 intros.
1181 assert ((oddify (e mod q) - e) mod q = 0). {
1182 rewrite Zminus_mod.
1183 rewrite <- oddify_mod_q with (e := e) at 2.
1184 rewrite Z.sub_diag.
1185 rewrite Z.mod_0_l; lia.
1186 }
1187 apply Z.div_exact in H; [ | lia].
1188 rewrite Z.sub_move_r in H.
1189 rewrite H.
1190 rewrite Z.add_comm.
1191 unfold ZtoP.
1192 rewrite mulP_addP.
1193 rewrite Z.mul_comm.
1194 rewrite mulP_mulP.
1195 rewrite mulP_q.
1196 rewrite <- mulP_zero with (Q := B).
1197 rewrite <- mulP_mulP.
1198 rewrite Z.mul_0_r.
1199 rewrite <- mulP_addP.
1200 f_equal.
1201 lia.
1202 Qed.
1203
1204 Theorem table_multicomb_oddify_correct: forall e : Z,
1205 table_multicomb (oddify (e mod q)) = ZtoP e.
1206 Proof.
1207 intros. rewrite table_multicomb_correct.
1208 - apply ZtoP_oddify.
1209 - apply oddify_bounds.
1210 - apply oddify_odd.
1211 Qed.
1212
1213 (* -------------------- *)
1214 (* evenify *)
1215
1216 (*
1217 Lemma oddify_evenify: forall e, 0 <= e < q ->
1218 exists f, f mod q = (oddify e) mod q /\
1219 (f + 2 ^ D - 1) / 2 = evenify (e + 2 ^ D - 1) / 2.
1220 Proof.
1221 intros.
1222 unfold oddify, evenify.
1223 f_equal.
1224 case_eq (Z.odd e); intros.
1225 - replace (Z.odd (e + 2 ^ D - 1)) with false.
1226 exists e.
1227 auto.
1228 admit.
1229 - replace (Z.odd (e + 2 ^ D - 1)) with true by admit.
1230 exists (e + q).
1231 split.
1232 admit.
1233 f_equal.
1234 lia.
1235 Admitted.
1236 *)
1237
1238 (* -------------------- *)
1239 (* positify *)
1240
1241 Lemma positify_spec: forall e, 0 <= positify e < 2 ^ D.
1242 Proof.
1243 intros.
1244 unfold positify.
1245 pose proof oddify_bounds e.
1246 intuition auto with core.
1247 - apply Z.div_pos; lia.
1248 - apply Z.div_lt_upper_bound; lia.
1249 Qed.
1250
1251 Lemma extract_bits_positify_spec: forall offset e,
1252 extract_bits_positify offset e = extract_bits offset (oddify (e mod q)).
1253 Proof.
1254 intros.
1255 unfold extract_bits, extract_bits_positify.
1256 f_equal.
1257 apply map_ext_in.
1258 intros.
1259 unfold sbit', positify.
1260 f_equal.
1261 f_equal.
1262 f_equal.
1263 f_equal.
1264 lia.
1265 Qed.
1266
1267 Lemma table_comb_positify_spec: forall offset e,
1268 table_comb_positify offset e = table_comb offset (oddify (e mod q)).
1269 Proof.
1270 intros.
1271 unfold table_comb, table_comb_positify.
1272 f_equal.
1273 apply map_ext_in.
1274 intros.
1275 f_equal.
1276 apply extract_bits_positify_spec.
1277 Qed.
1278
1279 Lemma table_multicomb_positify_spec: forall e,
1280 table_multicomb_positify e = table_multicomb (oddify (e mod q)).
1281 Proof.
1282 intros.
1283 unfold table_multicomb, table_multicomb_positify.
1284 f_equal.
1285 apply map_ext_in.
1286 intros.
1287 apply table_comb_positify_spec.
1288 Qed.
1289
1290 Lemma table_multicomb_positify_correct: forall e,
1291 table_multicomb_positify e = ZtoP e.
1292 Proof.
1293 intros.
1294 rewrite table_multicomb_positify_spec.
1295 apply table_multicomb_oddify_correct.
1296 Qed.
1297
1298 End Oddify.
1299
1300 End TableMult.
1301
1302 Print Transparent Dependencies table_multicomb_positify.
1303
1304 Section Instantiations.
1305
1306 Section AddMod.
1307
1308 Require Import Spec.ModularArithmetic.
1309
1310 Definition P := F ( 101 ).
1311 Definition O : P := F.zero.
1312 Definition B : P := F.of_Z _ 2.
1313 Definition addP : P -> P -> P := F.add.
1314 Definition doubleP : P -> P := F.mul (F.of_Z _ 2).
1315 Definition mulP (n : Z) : P -> P := F.mul (F.of_Z _ n).
1316 Definition negP : P -> P := F.opp.
1317
1318 Definition n := 1.
1319 Definition s := 2.
1320 Definition t := 3.
1321 Definition D := s * t.
1322
1323 Compute mulP 4 B.
1324
1325 Print table_entry.
1326 Compute table_entry P addP mulP O B s t 0 1.
1327 (* (-16 -4 +1) * 2 mod 101 = 63 *)
1328
1329 Print extract_bits.
1330 Compute extract_bits s t D 1 1.
1331
1332 Print table_comb.
1333 Compute table_comb P addP negP mulP O B s t n D 1 1.
1334
1335 Print table_multicomb.
1336 Compute table_multicomb P addP doubleP negP mulP O B s t n D (-7).
1337
1338 End AddMod.
1339
1340 (* TODO test on multiplication over F_p, using FLT to do negative exponents *)
1341 (* TODO instantiate this for Curve25519 *)
1342
1343 End Instantiations.