| 1 |
From Coq Require Import ZArith. |
| 2 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 3 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 4 |
Require Import Crypto.Util.LetIn. |
| 5 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 6 |
Require Import Crypto.Util.Tactics.Head. |
| 7 |
|
| 8 |
Local Open Scope Z_scope. |
| 9 |
|
| 10 |
Module Z. |
| 11 |
Definition eq_dec_cps {T} (x y : Z) (f : {x = y} + {x <> y} -> T) : T |
| 12 |
:= f (Z.eq_dec x y). |
| 13 |
Definition eq_dec_cps_correct {T} x y f : @eq_dec_cps T x y f = f (Z.eq_dec x y) |
| 14 |
:= eq_refl. |
| 15 |
#[global] |
| 16 |
Hint Rewrite @eq_dec_cps_correct : uncps. |
| 17 |
|
| 18 |
Definition eqb_cps {T} (x y : Z) (f : bool -> T) : T |
| 19 |
:= f (Z.eqb x y). |
| 20 |
Definition eqb_cps_correct {T} x y f : @eqb_cps T x y f = f (Z.eqb x y) |
| 21 |
:= eq_refl. |
| 22 |
#[global] |
| 23 |
Hint Rewrite @eqb_cps_correct : uncps. |
| 24 |
|
| 25 |
Local Ltac prove_cps_correct _ := |
| 26 |
try match goal with |
| 27 |
| [ |- ?lhs ?f = ?f ?rhs ] |
| 28 |
=> let l := head lhs in |
| 29 |
let r := head rhs in |
| 30 |
cbv [l r] in * |
| 31 |
end; |
| 32 |
repeat first [ reflexivity |
| 33 |
| progress cbv [Decidable.dec Decidable.dec_eq_Z] in * |
| 34 |
| progress Z.ltb_to_lt |
| 35 |
| congruence |
| 36 |
| progress autorewrite with uncps |
| 37 |
| break_innermost_match_step ]. |
| 38 |
|
| 39 |
Definition get_carry_cps {T} (bitwidth : Z) (v : Z) (f : Z * Z -> T) : T |
| 40 |
:= f (Z.get_carry bitwidth v). |
| 41 |
Definition get_carry_cps_correct {T} bitwidth v f |
| 42 |
: @get_carry_cps T bitwidth v f = f (Z.get_carry bitwidth v) |
| 43 |
:= eq_refl. |
| 44 |
#[global] |
| 45 |
Hint Rewrite @get_carry_cps_correct : uncps. |
| 46 |
Definition add_with_get_carry_cps {T} (bitwidth : Z) (c : Z) (x y : Z) (f : Z * Z -> T) : T |
| 47 |
:= f (Z.add_with_get_carry bitwidth c x y). |
| 48 |
Definition add_with_get_carry_cps_correct {T} bitwidth c x y f |
| 49 |
: @add_with_get_carry_cps T bitwidth c x y f = f (Z.add_with_get_carry bitwidth c x y) |
| 50 |
:= eq_refl. |
| 51 |
#[global] |
| 52 |
Hint Rewrite @add_with_get_carry_cps_correct : uncps. |
| 53 |
Definition add_get_carry_cps {T} (bitwidth : Z) (x y : Z) (f : Z * Z -> T) : T |
| 54 |
:= f (Z.add_get_carry bitwidth x y). |
| 55 |
Definition add_get_carry_cps_correct {T} bitwidth x y f |
| 56 |
: @add_get_carry_cps T bitwidth x y f = f (Z.add_get_carry bitwidth x y) |
| 57 |
:= eq_refl. |
| 58 |
#[global] |
| 59 |
Hint Rewrite @add_get_carry_cps_correct : uncps. |
| 60 |
|
| 61 |
Definition get_borrow_cps {T} (bitwidth : Z) (v : Z) (f : Z * Z -> T) |
| 62 |
:= f (Z.get_borrow bitwidth v). |
| 63 |
Definition get_borrow_cps_correct {T} bitwidth v f |
| 64 |
: @get_borrow_cps T bitwidth v f = f (Z.get_borrow bitwidth v) |
| 65 |
:= eq_refl. |
| 66 |
#[global] |
| 67 |
Hint Rewrite @get_borrow_cps_correct : uncps. |
| 68 |
Definition sub_with_get_borrow_cps {T} (bitwidth : Z) (c : Z) (x y : Z) (f : Z * Z -> T) : T |
| 69 |
:= f (Z.sub_with_get_borrow bitwidth c x y). |
| 70 |
Definition sub_with_get_borrow_cps_correct {T} (bitwidth : Z) (c : Z) (x y : Z) (f : Z * Z -> T) |
| 71 |
: @sub_with_get_borrow_cps T bitwidth c x y f = f (Z.sub_with_get_borrow bitwidth c x y) |
| 72 |
:= eq_refl. |
| 73 |
#[global] |
| 74 |
Hint Rewrite @sub_with_get_borrow_cps_correct : uncps. |
| 75 |
Definition sub_get_borrow_cps {T} (bitwidth : Z) (x y : Z) (f : Z * Z -> T) : T |
| 76 |
:= f (Z.sub_get_borrow bitwidth x y). |
| 77 |
Definition sub_get_borrow_cps_correct {T} (bitwidth : Z) (x y : Z) (f : Z * Z -> T) |
| 78 |
: @sub_get_borrow_cps T bitwidth x y f = f (Z.sub_get_borrow bitwidth x y) |
| 79 |
:= eq_refl. |
| 80 |
#[global] |
| 81 |
Hint Rewrite @sub_get_borrow_cps_correct : uncps. |
| 82 |
|
| 83 |
(* splits at [bound], not [2^bitwidth]; wrapper to make add_getcarry |
| 84 |
work if input is not known to be a power of 2 *) |
| 85 |
Definition add_get_carry_full_cps {T} (bound : Z) (x y : Z) (f : Z * Z -> T) : T |
| 86 |
:= eqb_cps |
| 87 |
(2 ^ (Z.log2 bound)) bound |
| 88 |
(fun eqb |
| 89 |
=> if eqb |
| 90 |
then add_get_carry_cps (Z.log2 bound) x y f |
| 91 |
else f ((x + y) mod bound, (x + y) / bound)). |
| 92 |
Lemma add_get_carry_full_cps_correct {T} (bound : Z) (x y : Z) (f : Z * Z -> T) |
| 93 |
: @add_get_carry_full_cps T bound x y f = f (Z.add_get_carry_full bound x y). |
| 94 |
Proof. prove_cps_correct (). Qed. |
| 95 |
#[global] |
| 96 |
Hint Rewrite @add_get_carry_full_cps_correct : uncps. |
| 97 |
Definition add_with_get_carry_full_cps {T} (bound : Z) (c x y : Z) (f : Z * Z -> T) : T |
| 98 |
:= eqb_cps |
| 99 |
(2 ^ (Z.log2 bound)) bound |
| 100 |
(fun eqb |
| 101 |
=> if eqb |
| 102 |
then add_with_get_carry_cps (Z.log2 bound) c x y f |
| 103 |
else f ((c + x + y) mod bound, (c + x + y) / bound)). |
| 104 |
Lemma add_with_get_carry_full_cps_correct {T} (bound : Z) (c x y : Z) (f : Z * Z -> T) |
| 105 |
: @add_with_get_carry_full_cps T bound c x y f = f (Z.add_with_get_carry_full bound c x y). |
| 106 |
Proof. prove_cps_correct (). Qed. |
| 107 |
#[global] |
| 108 |
Hint Rewrite @add_with_get_carry_full_cps_correct : uncps. |
| 109 |
Definition sub_get_borrow_full_cps {T} (bound : Z) (x y : Z) (f : Z * Z -> T) : T |
| 110 |
:= eqb_cps |
| 111 |
(2 ^ (Z.log2 bound)) bound |
| 112 |
(fun eqb |
| 113 |
=> if eqb |
| 114 |
then sub_get_borrow_cps (Z.log2 bound) x y f |
| 115 |
else f ((x - y) mod bound, -((x - y) / bound))). |
| 116 |
Lemma sub_get_borrow_full_cps_correct {T} (bound : Z) (x y : Z) (f : Z * Z -> T) |
| 117 |
: @sub_get_borrow_full_cps T bound x y f = f (Z.sub_get_borrow_full bound x y). |
| 118 |
Proof. prove_cps_correct (). Qed. |
| 119 |
#[global] |
| 120 |
Hint Rewrite @sub_get_borrow_full_cps_correct : uncps. |
| 121 |
Definition sub_with_get_borrow_full_cps {T} (bound : Z) (c x y : Z) (f : Z * Z -> T) : T |
| 122 |
:= eqb_cps |
| 123 |
(2 ^ (Z.log2 bound)) bound |
| 124 |
(fun eqb |
| 125 |
=> if eqb |
| 126 |
then sub_with_get_borrow_cps (Z.log2 bound) c x y f |
| 127 |
else f ((x - y - c) mod bound, -((x - y - c) / bound))). |
| 128 |
Lemma sub_with_get_borrow_full_cps_correct {T} (bound : Z) (c x y : Z) (f : Z * Z -> T) |
| 129 |
: @sub_with_get_borrow_full_cps T bound c x y f = f (Z.sub_with_get_borrow_full bound c x y). |
| 130 |
Proof. prove_cps_correct (). Qed. |
| 131 |
#[global] |
| 132 |
Hint Rewrite @sub_with_get_borrow_full_cps_correct : uncps. |
| 133 |
|
| 134 |
Definition mul_split_at_bitwidth_cps {T} (bitwidth : Z) (x y : Z) (f : Z * Z -> T) : T |
| 135 |
:= dlet xy := x * y in |
| 136 |
f (if Z.geb bitwidth 0 |
| 137 |
then Z.land xy (Z.ones bitwidth) |
| 138 |
else xy mod 2^bitwidth, |
| 139 |
if Z.geb bitwidth 0 |
| 140 |
then Z.shiftr xy bitwidth |
| 141 |
else xy / 2^bitwidth). |
| 142 |
Definition mul_split_at_bitwidth_cps_correct {T} (bitwidth : Z) (x y : Z) (f : Z * Z -> T) |
| 143 |
: @mul_split_at_bitwidth_cps T bitwidth x y f = f (Z.mul_split_at_bitwidth bitwidth x y) |
| 144 |
:= eq_refl. |
| 145 |
#[global] |
| 146 |
Hint Rewrite @mul_split_at_bitwidth_cps_correct : uncps. |
| 147 |
Definition mul_split_cps {T} (s x y : Z) (f : Z * Z -> T) : T |
| 148 |
:= eqb_cps |
| 149 |
s (2^Z.log2 s) |
| 150 |
(fun b |
| 151 |
=> if b |
| 152 |
then mul_split_at_bitwidth_cps (Z.log2 s) x y f |
| 153 |
else f ((x * y) mod s, (x * y) / s)). |
| 154 |
Lemma mul_split_cps_correct {T} (s x y : Z) (f : Z * Z -> T) |
| 155 |
: @mul_split_cps T s x y f = f (Z.mul_split s x y). |
| 156 |
Proof. prove_cps_correct (). Qed. |
| 157 |
#[global] |
| 158 |
Hint Rewrite @mul_split_cps_correct : uncps. |
| 159 |
|
| 160 |
Definition mul_split_cps' {T} (s x y : Z) (f : Z * Z -> T) : T |
| 161 |
:= eqb_cps |
| 162 |
s (2^Z.log2 s) |
| 163 |
(fun b |
| 164 |
=> if b |
| 165 |
then f (Z.mul_split_at_bitwidth (Z.log2 s) x y) |
| 166 |
else f ((x * y) mod s, (x * y) / s)). |
| 167 |
Lemma mul_split_cps'_correct {T} (s x y : Z) (f : Z * Z -> T) |
| 168 |
: @mul_split_cps' T s x y f = f (Z.mul_split s x y). |
| 169 |
Proof. prove_cps_correct (). Qed. |
| 170 |
#[global] |
| 171 |
Hint Rewrite @mul_split_cps'_correct : uncps. |
| 172 |
End Z. |