Debian Codegraph @

src/Arithmetic/Primitives.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import Lia.
3 Require Import Crypto.Arithmetic.MontgomeryReduction.Proofs. (* For MontgomeryReduction *)
4 Require Import Crypto.Util.Tactics.UniquePose.
5 Require Import Crypto.Util.Tuple Crypto.Util.Prod Crypto.Util.LetIn.
6 Require Import Crypto.Util.ListUtil.
7 Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
8 Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall.
9 Require Import Crypto.Util.ZUtil.Log2.
10 Require Import Crypto.Util.ZUtil.AddGetCarry Crypto.Util.ZUtil.MulSplit.
11 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
12 Require Import Crypto.Util.Tactics.SplitInContext.
13 Require Import Crypto.Util.ZUtil.Definitions.
14 Import Coq.Lists.List ListNotations. Local Open Scope Z_scope.
15
16 Section primitives.
17 Definition mulx (bitwidth : Z) := Eval cbv [Z.mul_split_at_bitwidth] in Z.mul_split_at_bitwidth bitwidth.
18 Definition addcarryx (bitwidth : Z) := Eval cbv [Z.add_with_get_carry Z.add_with_carry Z.get_carry] in Z.add_with_get_carry bitwidth.
19 Definition subborrowx (bitwidth : Z) := Eval cbv [Z.sub_with_get_borrow Z.sub_with_borrow Z.get_borrow Z.get_carry Z.add_with_carry] in Z.sub_with_get_borrow bitwidth.
20 Definition cmovznz_common (bitwidth : Z) (t : Z) (z nz : Z)
21 := Z.lor (Z.land (Z.value_barrier t) nz) (Z.land (Z.value_barrier (Z.lnot_modulo t (2^bitwidth))) z).
22 Definition cmovznz (bitwidth : Z) (cond : Z) (z nz : Z)
23 := dlet t := (0 - Z.bneg (Z.bneg cond)) mod 2^bitwidth in cmovznz_common bitwidth t z nz.
24 Definition cmovznz_by_mul (bitwidth : Z) (cond : Z) (z nz : Z)
25 := dlet t := cond * (2^bitwidth - 1) in cmovznz_common bitwidth t z nz.
26
27 Lemma mulx_correct (bitwidth : Z)
28 (x y : Z)
29 : mulx bitwidth x y = ((x * y) mod 2^bitwidth, (x * y) / 2^bitwidth).
30 Proof using Type.
31 change mulx with Z.mul_split_at_bitwidth.
32 rewrite <- Z.mul_split_at_bitwidth_div, <- Z.mul_split_at_bitwidth_mod; eta_expand.
33 eta_expand; reflexivity.
34 Qed.
35
36 Lemma addcarryx_correct (bitwidth : Z)
37 (c x y : Z)
38 : addcarryx bitwidth c x y = ((c + x + y) mod 2^bitwidth, (c + x + y) / 2^bitwidth).
39 Proof using Type.
40 cbv [addcarryx Let_In]; reflexivity.
41 Qed.
42
43 Lemma subborrowx_correct (bitwidth : Z)
44 (b x y : Z)
45 : subborrowx bitwidth b x y = ((-b + x + -y) mod 2^bitwidth, -((-b + x + -y) / 2^bitwidth)).
46 Proof using Type.
47 cbv [subborrowx Let_In]; reflexivity.
48 Qed.
49
50 Lemma cmovznz_correct bitwidth cond z nz
51 : 0 <= z < 2^bitwidth
52 -> 0 <= nz < 2^bitwidth
53 -> cmovznz bitwidth cond z nz = Z.zselect cond z nz.
54 Proof using Type.
55 intros.
56 assert (0 < 2^bitwidth) by lia.
57 assert (0 <= bitwidth) by auto with zarith.
58 assert (0 < bitwidth -> 1 < 2^bitwidth) by auto with zarith.
59 pose proof Z.log2_lt_pow2_alt.
60 assert (bitwidth = 0 \/ 0 < bitwidth) by lia.
61 repeat first [ progress cbv [cmovznz cmovznz_common Z.zselect Z.bneg Let_In Z.lnot_modulo]
62 | progress split_iff
63 | progress subst
64 | progress Z.ltb_to_lt
65 | progress destruct_head'_or
66 | congruence
67 | lia
68 | progress break_innermost_match_step
69 | progress break_innermost_match_hyps_step
70 | progress autorewrite with zsimplify_const in *
71 | progress pull_Zmod
72 | progress intros
73 | rewrite !Z.sub_1_r, <- Z.ones_equiv, <- ?Z.sub_1_r
74 | rewrite Z_mod_nz_opp_full by (Z.rewrite_mod_small; lia)
75 | rewrite (Z.land_comm (Z.ones _))
76 | rewrite Z.land_ones_low by auto with lia
77 | progress Z.rewrite_mod_small ].
78 Qed.
79
80 Lemma cmovznz_by_mul_correct bitwidth cond z nz
81 : 0 <= cond < 2^1
82 -> 0 <= z < 2^bitwidth
83 -> 0 <= nz < 2^bitwidth
84 -> cmovznz_by_mul bitwidth cond z nz = Z.zselect cond z nz.
85 Proof using Type.
86 intros.
87 assert (0 < 2^bitwidth) by lia.
88 assert (0 <= bitwidth) by auto with zarith.
89 assert (0 < bitwidth -> 1 < 2^bitwidth) by auto with zarith.
90 pose proof Z.log2_lt_pow2_alt.
91 assert (bitwidth = 0 \/ 0 < bitwidth) by lia.
92 assert (cond = 0 \/ cond = 1) by lia.
93 repeat first [ progress cbv [cmovznz_by_mul cmovznz_common Z.zselect Let_In Z.lnot_modulo Z.lnot Z.pred]
94 | progress split_iff
95 | progress subst
96 | progress Z.ltb_to_lt
97 | progress destruct_head'_or
98 | congruence
99 | lia
100 | progress break_innermost_match_step
101 | progress break_innermost_match_hyps_step
102 | progress autorewrite with zsimplify_const in *
103 | progress (push_Zmod; pull_Zmod)
104 | progress intros
105 | rewrite !Z.sub_1_r, <- Z.ones_equiv, <- ?Z.sub_1_r
106 | rewrite Z_mod_nz_opp_full by (Z.rewrite_mod_small; lia)
107 | rewrite (Z.land_comm (Z.ones _))
108 | rewrite Z.land_ones_low by auto with lia
109 | progress Z.rewrite_mod_small
110 | replace (-Z.ones bitwidth + -1) with (-2^bitwidth) by (rewrite Z.ones_equiv, <- Z.sub_1_r; lia) ].
111 Qed.
112 End primitives.