| 1 |
(** * C Operation Specifications *) |
| 2 |
(** The specifications for the various operations to be synthesized. *) |
| 3 |
From Coq Require Import ZArith Lia. |
| 4 |
From Coq Require Import List. |
| 5 |
Require Import Crypto.Arithmetic.Core. |
| 6 |
Require Import Crypto.Arithmetic.ModOps. |
| 7 |
Require Import Crypto.Arithmetic.BaseConversion. |
| 8 |
Require Import Crypto.Arithmetic.Partition. |
| 9 |
Require Import Crypto.Arithmetic.WordByWordMontgomery. |
| 10 |
Require Import Crypto.Arithmetic.BYInv. |
| 11 |
Require Import Crypto.Util.ZRange. |
| 12 |
Require Import Crypto.Util.ZRange.BasicLemmas. |
| 13 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 14 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 15 |
Require Import Crypto.Util.Bool. |
| 16 |
Require Import Crypto.Util.ListUtil. |
| 17 |
Require Import Crypto.Util.ListUtil.FoldBool. |
| 18 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 19 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 20 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 21 |
Local Open Scope Z_scope. Local Open Scope bool_scope. |
| 22 |
|
| 23 |
(** These Imports are only needed for the ring proof *) |
| 24 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 25 |
Require Import Crypto.Algebra.Ring. |
| 26 |
Require Import Crypto.Algebra.SubsetoidRing. |
| 27 |
|
| 28 |
Local Notation is_bounded_by0 r v |
| 29 |
:= ((lower r%zrange <=? v) && (v <=? upper r%zrange)). |
| 30 |
Local Notation is_bounded_by2 r v |
| 31 |
:= (let '(v1, v2) := v in is_bounded_by0 (fst r) v1 && is_bounded_by0 (snd r) v2). |
| 32 |
Local Notation is_bounded_by0o r |
| 33 |
:= (match r with Some r' => fun v' => is_bounded_by0 r' v' | None => fun _ => true end). |
| 34 |
Local Notation is_bounded_by bounds ls |
| 35 |
:= (fold_andb_map (fun r v'' => is_bounded_by0o r v'') bounds ls). |
| 36 |
|
| 37 |
Local Notation is_tighter_than0 x y |
| 38 |
:= ((lower y <=? lower x) && (upper x <=? upper y)). |
| 39 |
Local Notation is_tighter_than2 r v |
| 40 |
:= (let '(v1, v2) := v in is_tighter_than0 (fst r) v1 && is_tighter_than0 (snd r) v2). |
| 41 |
Local Notation is_tighter_than0oo r1 r2 |
| 42 |
:= (match r1, r2 with _, None => true | None, Some _ => false | Some r1', Some r2' => is_tighter_than0 r1' r2' end). |
| 43 |
Local Notation is_tighter_than ls1 ls2 |
| 44 |
:= (fold_andb_map (fun x y => is_tighter_than0oo x y) ls1 ls2). |
| 45 |
|
| 46 |
Section list_Z_bounded. |
| 47 |
Definition list_Z_bounded_by |
| 48 |
(bounds : list (option zrange)) |
| 49 |
(v : list Z) |
| 50 |
:= is_bounded_by bounds v = true. |
| 51 |
|
| 52 |
Definition list_Z_tighter_than |
| 53 |
(bounds1 bounds2 : list (option zrange)) |
| 54 |
:= is_tighter_than bounds1 bounds2 = true. |
| 55 |
|
| 56 |
Lemma length_list_Z_bounded_by bounds ls |
| 57 |
: list_Z_bounded_by bounds ls -> length ls = length bounds. |
| 58 |
Proof using Type. |
| 59 |
intro H. |
| 60 |
apply fold_andb_map_length in H; congruence. |
| 61 |
Qed. |
| 62 |
|
| 63 |
Lemma relax_list_Z_bounded_by |
| 64 |
: forall (bounds1 bounds2 : list (option zrange)) |
| 65 |
(H : list_Z_tighter_than bounds1 bounds2) |
| 66 |
(v : list Z), |
| 67 |
list_Z_bounded_by bounds1 v -> list_Z_bounded_by bounds2 v. |
| 68 |
Proof using Type. |
| 69 |
cbv [list_Z_bounded_by list_Z_tighter_than]. |
| 70 |
induction bounds1, bounds2, v; cbn in *; try congruence. |
| 71 |
rewrite ?Bool.andb_true_iff in *; intros; destruct_head'_and. |
| 72 |
break_innermost_match_hyps; repeat apply conj; try congruence; eauto. |
| 73 |
eapply ZRange.is_bounded_by_of_is_tighter_than; eassumption. |
| 74 |
Qed. |
| 75 |
|
| 76 |
Lemma eval_list_Z_bounded_by wt n' bounds bounds' f |
| 77 |
(H : list_Z_bounded_by bounds f) |
| 78 |
(Hb : bounds = List.map (@Some _) bounds') |
| 79 |
(Hblen : length bounds' = n') |
| 80 |
(Hwt : forall i, List.In i (seq 0 n') -> 0 <= wt i) |
| 81 |
: Positional.eval wt n' (List.map lower bounds') <= Positional.eval wt n' f <= Positional.eval wt n' (List.map upper bounds'). |
| 82 |
Proof using Type. |
| 83 |
setoid_rewrite in_seq in Hwt. |
| 84 |
subst bounds. |
| 85 |
pose proof H as H'; apply fold_andb_map_length in H'. |
| 86 |
revert dependent bounds'; intro bounds'. |
| 87 |
revert dependent f; intro f. |
| 88 |
rewrite <- (List.rev_involutive bounds'), <- (List.rev_involutive f); |
| 89 |
generalize (List.rev bounds') (List.rev f); clear bounds' f; intros bounds f; revert bounds f. |
| 90 |
induction n' as [|n IHn], bounds as [|b bounds], f as [|f fs]; intros; |
| 91 |
cbn [length rev map] in *; distr_length. |
| 92 |
{ rewrite !map_app in *; cbn [map] in *. |
| 93 |
erewrite !Positional.eval_snoc by (distr_length; eauto). |
| 94 |
cbv [list_Z_bounded_by] in *. |
| 95 |
specialize_by (intros; auto with lia). |
| 96 |
specialize (Hwt n); specialize_by lia. |
| 97 |
repeat first [ progress Bool.split_andb |
| 98 |
| rewrite Nat.add_1_r in * |
| 99 |
| rewrite fold_andb_map_snoc in * |
| 100 |
| rewrite Nat.succ_inj_wd in * |
| 101 |
| progress Z.ltb_to_lt |
| 102 |
| progress cbn [In seq] in * |
| 103 |
| match goal with |
| 104 |
| [ H : length _ = ?v |- _ ] => rewrite H in * |
| 105 |
| [ H : ?v = length _ |- _ ] => rewrite <- H in * |
| 106 |
end ]. |
| 107 |
split; apply Z.add_le_mono; try apply IHn; auto; distr_length; nia. } |
| 108 |
Qed. |
| 109 |
End list_Z_bounded. |
| 110 |
|
| 111 |
Ltac pose_proof_length_list_Z_bounded_by := |
| 112 |
repeat match goal with |
| 113 |
| [ H : list_Z_bounded_by _ _ |- _ ] => unique pose proof (length_list_Z_bounded_by _ _ H) |
| 114 |
end. |
| 115 |
|
| 116 |
Module Primitives. |
| 117 |
Definition mulx_correct s |
| 118 |
(mulx : Z -> Z -> Z * Z) |
| 119 |
:= forall x y, |
| 120 |
is_bounded_by0 r[0~>2^s-1] x = true |
| 121 |
-> is_bounded_by0 r[0~>2^s-1] y = true |
| 122 |
-> mulx x y = ((x * y) mod 2^s, (x * y) / 2^s) |
| 123 |
/\ is_bounded_by2 (r[0~>2^s-1], r[0~>2^s-1]) (mulx x y) = true. |
| 124 |
|
| 125 |
Local Notation return_carry_range relax_adc_sbb_return_carry_to_bitwidth s |
| 126 |
:= (if List.existsb (Z.eqb s) relax_adc_sbb_return_carry_to_bitwidth then r[0~>2^s%Z-1] else r[0~>1])%zrange. |
| 127 |
|
| 128 |
Definition addcarryx_correct (relax_adc_sbb_return_carry_to_bitwidth : list Z) s |
| 129 |
(addcarryx : Z -> Z -> Z -> Z * Z) |
| 130 |
:= forall c x y, |
| 131 |
is_bounded_by0 r[0~>1] c = true |
| 132 |
-> is_bounded_by0 r[0~>2^s-1] x = true |
| 133 |
-> is_bounded_by0 r[0~>2^s-1] y = true |
| 134 |
-> addcarryx c x y = ((c + x + y) mod 2^s, (c + x + y) / 2^s) |
| 135 |
/\ is_bounded_by2 (r[0~>2^s-1], return_carry_range relax_adc_sbb_return_carry_to_bitwidth s) |
| 136 |
(addcarryx c x y) = true. |
| 137 |
|
| 138 |
Definition subborrowx_correct (relax_adc_sbb_return_carry_to_bitwidth : list Z) s |
| 139 |
(subborrowx : Z -> Z -> Z -> Z * Z) |
| 140 |
:= forall b x y, |
| 141 |
is_bounded_by0 r[0~>1] b = true |
| 142 |
-> is_bounded_by0 r[0~>2^s-1] x = true |
| 143 |
-> is_bounded_by0 r[0~>2^s-1] y = true |
| 144 |
-> subborrowx b x y = ((-b + x + -y) mod 2^s, -((-b + x + -y) / 2^s)) |
| 145 |
/\ is_bounded_by2 (r[0~>2^s-1], return_carry_range relax_adc_sbb_return_carry_to_bitwidth s) |
| 146 |
(subborrowx b x y) = true. |
| 147 |
|
| 148 |
Definition value_barrier_correct (is_signed : bool) s |
| 149 |
(value_barrier : Z -> Z) |
| 150 |
:= match (if is_signed |
| 151 |
then r[-2^(s-1) ~> 2^(s-1) - 1] |
| 152 |
else r[0 ~> 2^s - 1])%zrange with |
| 153 |
| rs |
| 154 |
=> |
| 155 |
forall z, |
| 156 |
is_bounded_by0 rs z = true |
| 157 |
-> value_barrier z = z |
| 158 |
end. |
| 159 |
|
| 160 |
Definition cmovznz_correct (is_signed : bool) s |
| 161 |
(cmovznz : Z -> Z -> Z -> Z) |
| 162 |
:= match (if is_signed |
| 163 |
then r[-2^(s-1) ~> 2^(s-1) - 1] |
| 164 |
else r[0 ~> 2^s - 1])%zrange with |
| 165 |
| rs |
| 166 |
=> |
| 167 |
forall cond z nz, |
| 168 |
is_bounded_by0 r[0~>1] cond = true |
| 169 |
-> is_bounded_by0 rs z = true |
| 170 |
-> is_bounded_by0 rs nz = true |
| 171 |
-> cmovznz cond z nz = (if Decidable.dec (cond = 0) then z else nz) |
| 172 |
/\ is_bounded_by0 rs (cmovznz cond z nz) = true |
| 173 |
end. |
| 174 |
|
| 175 |
Section copy. |
| 176 |
Context (n : nat) |
| 177 |
(saturated_bounds : list (option zrange)) |
| 178 |
(length_saturated_bounds : length saturated_bounds = n). |
| 179 |
|
| 180 |
Definition copy_correct |
| 181 |
(copy : list Z -> list Z) |
| 182 |
:= forall x, |
| 183 |
list_Z_bounded_by saturated_bounds x |
| 184 |
-> copy x = x |
| 185 |
/\ list_Z_bounded_by saturated_bounds (copy x). |
| 186 |
End copy. |
| 187 |
End Primitives. |
| 188 |
|
| 189 |
Module selectznz. |
| 190 |
Section __. |
| 191 |
Context (wt : nat -> Z) |
| 192 |
(n : nat) |
| 193 |
(saturated_bounds : list (option zrange)) |
| 194 |
(length_saturated_bounds : length saturated_bounds = n). |
| 195 |
Local Notation eval := (Positional.eval wt n). |
| 196 |
|
| 197 |
Definition selectznz_correct |
| 198 |
(selectznz : Z -> list Z -> list Z -> list Z) |
| 199 |
:= forall cond x y, |
| 200 |
is_bounded_by0 r[0~>1] cond = true |
| 201 |
-> list_Z_bounded_by saturated_bounds x |
| 202 |
-> list_Z_bounded_by saturated_bounds y |
| 203 |
-> (selectznz cond x y) = (if Decidable.dec (cond = 0) then x else y) |
| 204 |
/\ list_Z_bounded_by saturated_bounds (selectznz cond x y). |
| 205 |
|
| 206 |
End __. |
| 207 |
|
| 208 |
|
| 209 |
End selectznz. |
| 210 |
|
| 211 |
Module BaseConversion. |
| 212 |
Section __. |
| 213 |
Context (src_wt dst_wt : nat -> Z) |
| 214 |
(s : Z) (c : list (Z * Z)) |
| 215 |
(src_n dst_n : nat) |
| 216 |
(inbounds : list (option zrange)) |
| 217 |
(outbounds : list (option zrange)) |
| 218 |
(length_inbounds : length inbounds = src_n) |
| 219 |
(length_outbounds : length outbounds = dst_n). |
| 220 |
Local Notation src_eval := (Positional.eval src_wt src_n). |
| 221 |
Local Notation dst_eval := (Positional.eval dst_wt dst_n). |
| 222 |
|
| 223 |
Definition convert_bases_correct |
| 224 |
(convert_bases : list Z -> list Z) |
| 225 |
:= forall x, |
| 226 |
list_Z_bounded_by inbounds x |
| 227 |
-> convert_bases x = Partition.partition dst_wt dst_n (src_eval x). |
| 228 |
End __. |
| 229 |
End BaseConversion. |
| 230 |
|
| 231 |
Module Solinas. |
| 232 |
(** re-export [selectznz_correct] and the primitives. We |
| 233 |
semi-arbitrarily choose to allow [Solinas.selectznz_correct] to |
| 234 |
exist, but have the full name of the primitive operations start |
| 235 |
with [Primitives.] *) |
| 236 |
Export Primitives. |
| 237 |
Include selectznz. |
| 238 |
|
| 239 |
Section __. |
| 240 |
Context (bitwidth : Z) (* only for encode_word *) |
| 241 |
(wt : nat -> Z) |
| 242 |
(n : nat) |
| 243 |
(n_bytes : nat) |
| 244 |
(m : Z) |
| 245 |
(s : Z) (* only for prime_bytes *) |
| 246 |
(tight_bounds : list (option zrange)) |
| 247 |
(length_tight_bounds : length tight_bounds = n) |
| 248 |
(loose_bounds : list (option zrange)) |
| 249 |
(length_loose_bounds : length loose_bounds = n) |
| 250 |
(saturated_bounds : list (option zrange)) |
| 251 |
(length_saturated_bounds : length saturated_bounds = n) |
| 252 |
(m_pos : 0 < m). |
| 253 |
Local Notation eval := (Positional.eval wt n). |
| 254 |
Local Notation bytes_eval := (Positional.eval (weight 8 1) n_bytes). |
| 255 |
|
| 256 |
Let prime_bytes_upperbound_list : list Z |
| 257 |
:= Partition.partition (weight 8 1) n_bytes (s-1). |
| 258 |
Let prime_bytes_bounds : list (option zrange) |
| 259 |
:= List.map (fun v => Some r[0 ~> v]%zrange) prime_bytes_upperbound_list. |
| 260 |
Local Notation word_bound |
| 261 |
:= r[0~>(2^bitwidth - 1)]%zrange. |
| 262 |
Let prime_bound : zrange |
| 263 |
:= r[0~>(m - 1)]%zrange. |
| 264 |
|
| 265 |
Definition from_bytes_correct |
| 266 |
(from_bytes : list Z -> list Z) |
| 267 |
:= forall x, |
| 268 |
list_Z_bounded_by prime_bytes_bounds x |
| 269 |
-> eval (from_bytes x) mod m = bytes_eval x mod m |
| 270 |
/\ list_Z_bounded_by tight_bounds (from_bytes x). |
| 271 |
|
| 272 |
Definition to_bytes_correct |
| 273 |
(to_bytes : list Z -> list Z) |
| 274 |
:= forall x, |
| 275 |
list_Z_bounded_by tight_bounds x |
| 276 |
-> to_bytes x = Partition.partition (weight 8 1) n_bytes (eval x mod m). |
| 277 |
|
| 278 |
Definition carry_mul_correct |
| 279 |
(carry_mul : list Z -> list Z -> list Z) |
| 280 |
:= forall x y, |
| 281 |
list_Z_bounded_by loose_bounds x |
| 282 |
-> list_Z_bounded_by loose_bounds y |
| 283 |
-> eval (carry_mul x y) mod m = (Z.mul (eval x) (eval y)) mod m |
| 284 |
/\ list_Z_bounded_by tight_bounds (carry_mul x y). |
| 285 |
|
| 286 |
Definition carry_square_correct |
| 287 |
(carry_square : list Z -> list Z) |
| 288 |
:= forall x, |
| 289 |
list_Z_bounded_by loose_bounds x |
| 290 |
-> eval (carry_square x) mod m = (eval x * eval x) mod m |
| 291 |
/\ list_Z_bounded_by tight_bounds (carry_square x). |
| 292 |
|
| 293 |
Definition carry_scmul_const_correct |
| 294 |
(a : Z) |
| 295 |
(carry_scmul_const : list Z -> list Z) |
| 296 |
:= forall x, |
| 297 |
list_Z_bounded_by loose_bounds x |
| 298 |
-> eval (carry_scmul_const x) mod m = (a * eval x) mod m |
| 299 |
/\ list_Z_bounded_by tight_bounds (carry_scmul_const x). |
| 300 |
|
| 301 |
Definition add_correct |
| 302 |
(add : list Z -> list Z -> list Z) |
| 303 |
:= forall x y, |
| 304 |
list_Z_bounded_by tight_bounds x |
| 305 |
-> list_Z_bounded_by tight_bounds y |
| 306 |
-> eval (add x y) mod m = (Z.add (eval x) (eval y)) mod m |
| 307 |
/\ list_Z_bounded_by loose_bounds (add x y). |
| 308 |
|
| 309 |
Definition sub_correct |
| 310 |
(sub : list Z -> list Z -> list Z) |
| 311 |
:= forall x y, |
| 312 |
list_Z_bounded_by tight_bounds x |
| 313 |
-> list_Z_bounded_by tight_bounds y |
| 314 |
-> eval (sub x y) mod m = (Z.sub (eval x) (eval y)) mod m |
| 315 |
/\ list_Z_bounded_by loose_bounds (sub x y). |
| 316 |
|
| 317 |
Definition opp_correct |
| 318 |
(opp : list Z -> list Z) |
| 319 |
:= forall x, |
| 320 |
list_Z_bounded_by tight_bounds x |
| 321 |
-> eval (opp x) mod m = (Z.opp (eval x)) mod m |
| 322 |
/\ list_Z_bounded_by loose_bounds (opp x). |
| 323 |
|
| 324 |
Definition carry_add_correct |
| 325 |
(carry_add : list Z -> list Z -> list Z) |
| 326 |
:= forall x y, |
| 327 |
list_Z_bounded_by tight_bounds x |
| 328 |
-> list_Z_bounded_by tight_bounds y |
| 329 |
-> eval (carry_add x y) mod m = (Z.add (eval x) (eval y)) mod m |
| 330 |
/\ list_Z_bounded_by tight_bounds (carry_add x y). |
| 331 |
|
| 332 |
Definition carry_sub_correct |
| 333 |
(carry_sub : list Z -> list Z -> list Z) |
| 334 |
:= forall x y, |
| 335 |
list_Z_bounded_by tight_bounds x |
| 336 |
-> list_Z_bounded_by tight_bounds y |
| 337 |
-> eval (carry_sub x y) mod m = (Z.sub (eval x) (eval y)) mod m |
| 338 |
/\ list_Z_bounded_by tight_bounds (carry_sub x y). |
| 339 |
|
| 340 |
Definition carry_opp_correct |
| 341 |
(carry_opp : list Z -> list Z) |
| 342 |
:= forall x, |
| 343 |
list_Z_bounded_by tight_bounds x |
| 344 |
-> eval (carry_opp x) mod m = (Z.opp (eval x)) mod m |
| 345 |
/\ list_Z_bounded_by tight_bounds (carry_opp x). |
| 346 |
|
| 347 |
Definition relax_correct |
| 348 |
(relax : list Z -> list Z) |
| 349 |
:= forall x, |
| 350 |
list_Z_bounded_by tight_bounds x |
| 351 |
-> relax x = x |
| 352 |
/\ list_Z_bounded_by loose_bounds (relax x). |
| 353 |
|
| 354 |
Definition carry_correct |
| 355 |
(carry : list Z -> list Z) |
| 356 |
:= forall x, |
| 357 |
list_Z_bounded_by loose_bounds x |
| 358 |
-> eval (carry x) mod m = (eval x) mod m |
| 359 |
/\ list_Z_bounded_by tight_bounds (carry x). |
| 360 |
|
| 361 |
Definition zero_correct |
| 362 |
(zero : list Z) |
| 363 |
:= eval zero mod m = 0 |
| 364 |
/\ list_Z_bounded_by tight_bounds zero. |
| 365 |
|
| 366 |
Definition one_correct |
| 367 |
(one : list Z) |
| 368 |
:= eval one mod m = 1 mod m |
| 369 |
/\ list_Z_bounded_by tight_bounds one. |
| 370 |
|
| 371 |
Definition encode_correct |
| 372 |
(encode : Z -> list Z) |
| 373 |
:= forall x, |
| 374 |
is_bounded_by0 prime_bound x = true |
| 375 |
-> eval (encode x) mod m = x mod m |
| 376 |
/\ list_Z_bounded_by tight_bounds (encode x). |
| 377 |
|
| 378 |
Definition encode_word_correct |
| 379 |
(encode_word : Z -> list Z) |
| 380 |
:= forall x, |
| 381 |
is_bounded_by0 word_bound x = true |
| 382 |
-> eval (encode_word x) mod m = x mod m |
| 383 |
/\ list_Z_bounded_by tight_bounds (encode_word x). |
| 384 |
|
| 385 |
Section ring. |
| 386 |
Context carry_mul (Hcarry_mul : carry_mul_correct carry_mul) |
| 387 |
add (Hadd : add_correct add) |
| 388 |
sub (Hsub : sub_correct sub) |
| 389 |
opp (Hopp : opp_correct opp) |
| 390 |
carry (Hcarry : carry_correct carry) |
| 391 |
encode (Hencode : encode_correct encode) |
| 392 |
zero (Hzero : zero_correct zero) |
| 393 |
one (Hone : one_correct one) |
| 394 |
(Hrelax : forall x, list_Z_bounded_by tight_bounds x -> list_Z_bounded_by loose_bounds x). |
| 395 |
|
| 396 |
Let m' := Z.to_pos m. |
| 397 |
|
| 398 |
Local Notation T := (list Z) (only parsing). |
| 399 |
Local Notation encoded_ok ls |
| 400 |
:= (is_bounded_by tight_bounds ls = true) (only parsing). |
| 401 |
Local Notation encoded_okf := (fun ls => encoded_ok ls) (only parsing). |
| 402 |
|
| 403 |
Definition Fdecode (v : T) : F m' |
| 404 |
:= F.of_Z m' (eval v). |
| 405 |
Definition T_eq (x y : T) |
| 406 |
:= Fdecode x = Fdecode y. |
| 407 |
|
| 408 |
Definition encodedT := sig encoded_okf. |
| 409 |
|
| 410 |
Definition ring_mul (x y : T) : T := carry_mul x y. |
| 411 |
Definition ring_add (x y : T) : T := carry (add x y). |
| 412 |
Definition ring_sub (x y : T) : T := carry (sub x y). |
| 413 |
Definition ring_opp (x : T) : T := carry (opp x). |
| 414 |
Definition ring_encode (x : F m') : T := encode (F.to_Z x). |
| 415 |
|
| 416 |
Definition GoodT : Prop |
| 417 |
:= @subsetoid_ring |
| 418 |
(list Z) encoded_okf T_eq |
| 419 |
zero one ring_opp ring_add ring_sub ring_mul |
| 420 |
/\ @is_subsetoid_homomorphism |
| 421 |
(F m') (fun _ => True) eq 1%F F.add F.mul |
| 422 |
(list Z) encoded_okf T_eq one ring_add ring_mul ring_encode |
| 423 |
/\ @is_subsetoid_homomorphism |
| 424 |
(list Z) encoded_okf T_eq one ring_add ring_mul |
| 425 |
(F m') (fun _ => True) eq 1%F F.add F.mul |
| 426 |
Fdecode. |
| 427 |
|
| 428 |
Hint Rewrite ->@F.to_Z_add : push_FtoZ. |
| 429 |
Hint Rewrite ->@F.to_Z_mul : push_FtoZ. |
| 430 |
Hint Rewrite ->@F.to_Z_opp : push_FtoZ. |
| 431 |
Hint Rewrite ->@F.to_Z_of_Z : push_FtoZ. |
| 432 |
|
| 433 |
Lemma Fm_bounded_alt (x : F m') |
| 434 |
: is_bounded_by0 prime_bound (F.to_Z x) = true. |
| 435 |
Proof using m_pos. |
| 436 |
clear -m_pos. |
| 437 |
destruct x as [x H]; cbn [F.to_Z proj1_sig]. |
| 438 |
pose proof (Z.mod_pos_bound x (Z.pos m')). |
| 439 |
subst m'; rewrite Z2Pos.id in * by lia. |
| 440 |
cbv [prime_bound lower upper]. |
| 441 |
rewrite Bool.andb_true_iff; split; Z.ltb_to_lt; lia. |
| 442 |
Qed. |
| 443 |
|
| 444 |
Lemma Good : GoodT. |
| 445 |
Proof using Hadd Hcarry Hcarry_mul Hencode Hone Hopp Hrelax Hsub Hzero m_pos. |
| 446 |
split_and; simpl in *. |
| 447 |
repeat match goal with |
| 448 |
| [ H : context[andb _ true] |- _ ] => setoid_rewrite andb_true_r in H |
| 449 |
end. |
| 450 |
all: cbv [carry_mul_correct add_correct sub_correct opp_correct carry_correct encode_correct zero_correct one_correct] in *; split_and. |
| 451 |
eapply subsetoid_ring_by_ring_isomorphism; |
| 452 |
cbv [ring_opp ring_add ring_sub ring_mul ring_encode F.sub list_Z_bounded_by Fdecode m' F.one] in *; auto. |
| 453 |
all: repeat first [ progress intros |
| 454 |
| reflexivity |
| 455 |
| progress autorewrite with push_FtoZ |
| 456 |
| rewrite Z2Pos.id |
| 457 |
| apply Fm_bounded_alt |
| 458 |
| match goal with |
| 459 |
| [ |- _ = _ :> F _ ] => apply F.eq_to_Z_iff |
| 460 |
| [ |- _ mod _ = F.to_Z ?x ] |
| 461 |
=> etransitivity; [ | apply (F.mod_to_Z x) ] |
| 462 |
| [ H : _ |- _ ] => apply H; clear H |
| 463 |
| [ H : context[eval (?f _) mod ?m = _] |- context[eval (?f _) mod ?m] ] |
| 464 |
=> rewrite H |
| 465 |
| [ H : context[eval (?f _ _) mod ?m = _] |- context[eval (?f _ _) mod ?m] ] |
| 466 |
=> rewrite H |
| 467 |
end |
| 468 |
| progress (push_Zmod; pull_Zmod); try (f_equal; lia) ]. |
| 469 |
Qed. |
| 470 |
End ring. |
| 471 |
End __. |
| 472 |
End Solinas. |
| 473 |
|
| 474 |
Module SaturatedSolinas. |
| 475 |
Section __. |
| 476 |
Context (wt : nat -> Z) |
| 477 |
(n : nat) |
| 478 |
(m : Z) |
| 479 |
(saturated_bounds : list (option zrange)) |
| 480 |
(length_saturated_bounds : length saturated_bounds = n). |
| 481 |
Local Notation eval := (Positional.eval wt n). |
| 482 |
|
| 483 |
Definition mul_correct |
| 484 |
(mul : list Z -> list Z -> list Z * Z) |
| 485 |
:= forall x y, |
| 486 |
list_Z_bounded_by saturated_bounds x |
| 487 |
-> list_Z_bounded_by saturated_bounds y |
| 488 |
-> (eval (fst (mul x y)) + wt n * snd (mul x y)) mod m |
| 489 |
= (eval x * eval y) mod m |
| 490 |
/\ ((let '(v, c) := mul x y in |
| 491 |
(is_bounded_by saturated_bounds v) |
| 492 |
&& true(*Should be: is_bounded_by0 r[0~>0] c, but bounds analysis is not good enough*)) |
| 493 |
= true). |
| 494 |
End __. |
| 495 |
End SaturatedSolinas. |
| 496 |
|
| 497 |
Module DettmanMultiplication. |
| 498 |
Section __. |
| 499 |
Context (wt : nat -> Z) |
| 500 |
(n : nat) |
| 501 |
(m : Z) |
| 502 |
(input_bounds : list (option zrange)) |
| 503 |
(output_bounds : list (option zrange)) |
| 504 |
(length_input_bounds : length input_bounds = n) |
| 505 |
(length_output_bounds : length output_bounds = n). |
| 506 |
Local Notation eval := (Positional.eval wt n). |
| 507 |
|
| 508 |
Definition mul_correct |
| 509 |
(mul : list Z -> list Z -> list Z) |
| 510 |
:= forall x y, |
| 511 |
list_Z_bounded_by input_bounds x |
| 512 |
-> list_Z_bounded_by input_bounds y |
| 513 |
-> eval (mul x y) mod m = ((eval x) * (eval y)) mod m |
| 514 |
/\ list_Z_bounded_by output_bounds (mul x y). |
| 515 |
|
| 516 |
Definition square_correct |
| 517 |
(square : list Z -> list Z) |
| 518 |
:= forall x, |
| 519 |
list_Z_bounded_by input_bounds x |
| 520 |
-> eval (square x) mod m = ((eval x) * (eval x)) mod m |
| 521 |
/\ list_Z_bounded_by output_bounds (square x). |
| 522 |
End __. |
| 523 |
End DettmanMultiplication. |
| 524 |
|
| 525 |
Module SolinasReduction. |
| 526 |
Section __. |
| 527 |
Context (wt : nat -> Z) |
| 528 |
(n : nat) |
| 529 |
(m : Z) |
| 530 |
(saturated_bounds : list (option zrange)) |
| 531 |
(length_saturated_bounds : length saturated_bounds = n). |
| 532 |
Local Notation eval := (Positional.eval wt n). |
| 533 |
|
| 534 |
Definition mul_correct |
| 535 |
(mul : list Z -> list Z -> list Z) := |
| 536 |
forall x y, |
| 537 |
list_Z_bounded_by saturated_bounds x -> |
| 538 |
list_Z_bounded_by saturated_bounds y -> |
| 539 |
((eval (mul x y)) mod m = (eval x * eval y) mod m) /\ |
| 540 |
(list_Z_bounded_by saturated_bounds (mul x y)). |
| 541 |
|
| 542 |
Definition sqr_correct |
| 543 |
(sqr : list Z -> list Z) := |
| 544 |
forall x, |
| 545 |
list_Z_bounded_by saturated_bounds x -> |
| 546 |
((eval (sqr x)) mod m = (eval x * eval x) mod m) /\ |
| 547 |
(list_Z_bounded_by saturated_bounds (sqr x)). |
| 548 |
|
| 549 |
End __. |
| 550 |
End SolinasReduction. |
| 551 |
|
| 552 |
Module WordByWordMontgomery. |
| 553 |
Import Arithmetic.WordByWordMontgomery. |
| 554 |
Local Coercion Z.of_nat : nat >-> Z. |
| 555 |
Section __. |
| 556 |
Context (bitwidth : Z) |
| 557 |
(n : nat) |
| 558 |
(n_bytes : nat) |
| 559 |
(m r' : Z) |
| 560 |
(s : Z) (* only for prime_bytes *) |
| 561 |
(bounds : list (option zrange)) |
| 562 |
(length_bounds : length bounds = n) |
| 563 |
(valid : list Z -> Prop) |
| 564 |
(bytes_valid : list Z -> Prop) |
| 565 |
(m_pos : 0 < m) |
| 566 |
(from_montgomery : list Z -> list Z) |
| 567 |
(to_montgomery : list Z -> list Z) |
| 568 |
(* saturated_bounds is only for selectznz *) |
| 569 |
(saturated_bounds : list (option zrange)) |
| 570 |
(length_saturated_bounds : length saturated_bounds = n). |
| 571 |
Local Notation eval := (@WordByWordMontgomery.eval bitwidth n). |
| 572 |
Local Notation bytes_eval := (Positional.eval (weight 8 1) n_bytes). |
| 573 |
Local Notation twos_complement_eval f := (eval_twos_complement bitwidth n f). |
| 574 |
|
| 575 |
Let prime_bound : zrange |
| 576 |
:= r[0~>(m - 1)]%zrange. |
| 577 |
Local Notation prime_word_bound (* fits in a single word, and smaller than the prime *) |
| 578 |
:= r[0~>(Z.min m (2^bitwidth) - 1)]%zrange. |
| 579 |
|
| 580 |
Definition from_montgomery_correct |
| 581 |
:= forall v, |
| 582 |
valid v |
| 583 |
-> eval (from_montgomery v) mod m = (eval v * r'^n) mod m |
| 584 |
/\ valid (from_montgomery v). |
| 585 |
|
| 586 |
Definition to_montgomery_correct |
| 587 |
:= forall v, |
| 588 |
valid v |
| 589 |
-> eval (from_montgomery (to_montgomery v)) mod m = (eval v) mod m |
| 590 |
/\ valid (to_montgomery v). |
| 591 |
|
| 592 |
Definition mul_correct |
| 593 |
(mul : list Z -> list Z -> list Z) |
| 594 |
:= forall a b, |
| 595 |
valid a |
| 596 |
-> valid b |
| 597 |
-> eval (from_montgomery (mul a b)) mod m = (Z.mul (eval (from_montgomery a)) (eval (from_montgomery b))) mod m |
| 598 |
/\ valid (mul a b). |
| 599 |
|
| 600 |
Definition add_correct |
| 601 |
(add : list Z -> list Z -> list Z) |
| 602 |
:= forall a b, |
| 603 |
valid a |
| 604 |
-> valid b |
| 605 |
-> eval (from_montgomery (add a b)) mod m = (Z.add (eval (from_montgomery a)) (eval (from_montgomery b))) mod m |
| 606 |
/\ valid (add a b). |
| 607 |
|
| 608 |
Definition sub_correct |
| 609 |
(sub : list Z -> list Z -> list Z) |
| 610 |
:= forall a b, |
| 611 |
valid a |
| 612 |
-> valid b |
| 613 |
-> eval (from_montgomery (sub a b)) mod m = (Z.sub (eval (from_montgomery a)) (eval (from_montgomery b))) mod m |
| 614 |
/\ valid (sub a b). |
| 615 |
|
| 616 |
Definition opp_correct |
| 617 |
(opp : list Z -> list Z) |
| 618 |
:= forall a, |
| 619 |
valid a |
| 620 |
-> eval (from_montgomery (opp a)) mod m = (Z.opp (eval (from_montgomery a))) mod m |
| 621 |
/\ valid (opp a). |
| 622 |
|
| 623 |
Definition square_correct |
| 624 |
(square : list Z -> list Z) |
| 625 |
:= forall a, |
| 626 |
valid a |
| 627 |
-> eval (from_montgomery (square a)) mod m = (eval (from_montgomery a) * eval (from_montgomery a)) mod m |
| 628 |
/\ valid (square a). |
| 629 |
|
| 630 |
Definition zero_correct |
| 631 |
(zero : list Z) |
| 632 |
:= eval (from_montgomery zero) mod m = 0 |
| 633 |
/\ valid zero. |
| 634 |
|
| 635 |
Definition one_correct |
| 636 |
(one : list Z) |
| 637 |
:= eval (from_montgomery one) mod m = 1 mod m |
| 638 |
/\ valid one. |
| 639 |
|
| 640 |
Definition encode_correct |
| 641 |
(encode : Z -> list Z) |
| 642 |
:= forall x, |
| 643 |
is_bounded_by0 prime_bound x = true |
| 644 |
-> eval (from_montgomery (encode x)) mod m = x mod m |
| 645 |
/\ valid (encode x). |
| 646 |
|
| 647 |
Definition encode_word_correct |
| 648 |
(encode_word : Z -> list Z) |
| 649 |
:= forall x, |
| 650 |
is_bounded_by0 prime_word_bound x = true |
| 651 |
-> eval (from_montgomery (encode_word x)) mod m = x mod m |
| 652 |
/\ valid (encode_word x). |
| 653 |
|
| 654 |
Definition nonzero_correct |
| 655 |
(nonzero : list Z -> Z) |
| 656 |
:= forall x, |
| 657 |
valid x |
| 658 |
-> (nonzero x = 0) <-> (eval (from_montgomery x) mod m = 0). |
| 659 |
|
| 660 |
Definition to_bytes_correct |
| 661 |
(to_bytes : list Z -> list Z) |
| 662 |
:= forall x, |
| 663 |
valid x |
| 664 |
-> to_bytes x = Partition.partition (weight 8 1) n_bytes (eval x mod m). |
| 665 |
|
| 666 |
Definition from_bytes_correct |
| 667 |
(from_bytes : list Z -> list Z) |
| 668 |
:= forall x, |
| 669 |
bytes_valid x |
| 670 |
-> eval (from_bytes x) mod m = bytes_eval x mod m |
| 671 |
/\ valid (from_bytes x). |
| 672 |
|
| 673 |
Definition selectznz_correct |
| 674 |
(selectznz : Z -> list Z -> list Z -> list Z) |
| 675 |
: Prop |
| 676 |
:= selectznz.selectznz_correct |
| 677 |
saturated_bounds |
| 678 |
selectznz. |
| 679 |
|
| 680 |
(* Bernstein-Yang inversion *) |
| 681 |
Definition msat_correct |
| 682 |
(msat : list Z) := |
| 683 |
twos_complement_eval msat = m /\ |
| 684 |
valid msat. |
| 685 |
|
| 686 |
Definition divstep_precomp_correct |
| 687 |
(divstep_precomp : list Z) := |
| 688 |
let mbits := (Z.log2 m) + 1 in |
| 689 |
(eval (from_montgomery divstep_precomp) = ((m - 1) / 2) ^ (if Decidable.dec (mbits < 46) |
| 690 |
then (49 * mbits + 80) / 17 |
| 691 |
else (49 * mbits + 57)/ 17)) |
| 692 |
/\ valid divstep_precomp. |
| 693 |
|
| 694 |
Definition divstep_correct |
| 695 |
(divstep : |
| 696 |
Z -> list Z -> list Z -> list Z -> list Z -> |
| 697 |
Z * list Z * list Z * list Z * list Z) : Prop |
| 698 |
:= forall (d : Z) f g v r, |
| 699 |
valid v -> valid r -> |
| 700 |
let '(d1,f1,g1,v1,r1) := divstep d f g v r in |
| 701 |
(((d1, |
| 702 |
twos_complement_eval f1, |
| 703 |
twos_complement_eval g1, |
| 704 |
eval (from_montgomery v1) mod m, |
| 705 |
eval (from_montgomery r1) mod m) = |
| 706 |
(if (0 <? d) && Z.odd (twos_complement_eval g) |
| 707 |
then (1 - d, |
| 708 |
(twos_complement_eval g), |
| 709 |
((twos_complement_eval g) - (twos_complement_eval f)) / 2, |
| 710 |
(2 * (eval (from_montgomery r))) mod m, |
| 711 |
((eval (from_montgomery v)) - (eval (from_montgomery v))) mod m) |
| 712 |
else (1 + d, |
| 713 |
(twos_complement_eval f), |
| 714 |
((twos_complement_eval g) + (twos_complement_eval g mod 2) * (twos_complement_eval f)) / 2, |
| 715 |
(2 * (eval (from_montgomery v))) mod m, |
| 716 |
((eval (from_montgomery r)) + (twos_complement_eval g mod 2) * (eval (from_montgomery v))) mod m))) |
| 717 |
/\ valid r1 /\ valid r1 /\ valid f1 /\ valid g1). |
| 718 |
|
| 719 |
Section ring. |
| 720 |
Context mul (Hmul : mul_correct mul) |
| 721 |
add (Hadd : add_correct add) |
| 722 |
sub (Hsub : sub_correct sub) |
| 723 |
opp (Hopp : opp_correct opp) |
| 724 |
encode (Hencode : encode_correct encode) |
| 725 |
zero (Hzero : zero_correct zero) |
| 726 |
one (Hone : one_correct one). |
| 727 |
|
| 728 |
Let m' := Z.to_pos m. |
| 729 |
|
| 730 |
Local Notation T := (list Z) (only parsing). |
| 731 |
Local Notation encoded_ok ls |
| 732 |
:= (valid ls) (only parsing). |
| 733 |
Local Notation encoded_okf := (fun ls => encoded_ok ls) (only parsing). |
| 734 |
|
| 735 |
Definition Fdecode (v : T) : F m' |
| 736 |
:= F.of_Z m' (eval (from_montgomery v)). |
| 737 |
Definition T_eq (x y : T) |
| 738 |
:= Fdecode x = Fdecode y. |
| 739 |
|
| 740 |
Definition encodedT := sig encoded_okf. |
| 741 |
|
| 742 |
Definition ring_mul (x y : T) : T := mul x y. |
| 743 |
Definition ring_add (x y : T) : T := add x y. |
| 744 |
Definition ring_sub (x y : T) : T := sub x y. |
| 745 |
Definition ring_opp (x : T) : T := opp x. |
| 746 |
Definition ring_encode (x : F m') : T := encode (F.to_Z x). |
| 747 |
|
| 748 |
Definition GoodT : Prop |
| 749 |
:= @subsetoid_ring |
| 750 |
(list Z) encoded_okf T_eq |
| 751 |
zero one ring_opp ring_add ring_sub ring_mul |
| 752 |
/\ @is_subsetoid_homomorphism |
| 753 |
(F m') (fun _ => True) eq 1%F F.add F.mul |
| 754 |
(list Z) encoded_okf T_eq one ring_add ring_mul ring_encode |
| 755 |
/\ @is_subsetoid_homomorphism |
| 756 |
(list Z) encoded_okf T_eq one ring_add ring_mul |
| 757 |
(F m') (fun _ => True) eq 1%F F.add F.mul |
| 758 |
Fdecode. |
| 759 |
|
| 760 |
Hint Rewrite ->@F.to_Z_add : push_FtoZ. |
| 761 |
Hint Rewrite ->@F.to_Z_mul : push_FtoZ. |
| 762 |
Hint Rewrite ->@F.to_Z_opp : push_FtoZ. |
| 763 |
Hint Rewrite ->@F.to_Z_of_Z : push_FtoZ. |
| 764 |
|
| 765 |
Lemma Fm_bounded_alt (x : F m') |
| 766 |
: is_bounded_by0 prime_bound (F.to_Z x) = true. |
| 767 |
Proof using m_pos. |
| 768 |
clear -m_pos. |
| 769 |
destruct x as [x H]; cbn [F.to_Z proj1_sig]. |
| 770 |
pose proof (Z.mod_pos_bound x (Z.pos m')). |
| 771 |
subst m'; rewrite Z2Pos.id in * by lia. |
| 772 |
cbv [prime_bound lower upper]. |
| 773 |
rewrite Bool.andb_true_iff; split; Z.ltb_to_lt; lia. |
| 774 |
Qed. |
| 775 |
|
| 776 |
Lemma Good : GoodT. |
| 777 |
Proof using Hadd Hencode Hmul Hone Hopp Hsub Hzero m_pos. |
| 778 |
split_and; simpl in *. |
| 779 |
repeat match goal with |
| 780 |
| [ H : context[andb _ true] |- _ ] => setoid_rewrite andb_true_r in H |
| 781 |
end. |
| 782 |
all: cbv [mul_correct add_correct sub_correct opp_correct encode_correct zero_correct one_correct] in *; split_and. |
| 783 |
eapply subsetoid_ring_by_ring_isomorphism; |
| 784 |
cbv [ring_opp ring_add ring_sub ring_mul ring_encode F.sub list_Z_bounded_by Fdecode m' F.one] in *; auto. |
| 785 |
all: repeat first [ progress intros |
| 786 |
| reflexivity |
| 787 |
| progress autorewrite with push_FtoZ |
| 788 |
| rewrite Z2Pos.id |
| 789 |
| apply Fm_bounded_alt |
| 790 |
| match goal with |
| 791 |
| [ |- _ = _ :> F _ ] => apply F.eq_to_Z_iff |
| 792 |
| [ |- _ mod _ = F.to_Z ?x ] |
| 793 |
=> etransitivity; [ | apply (F.mod_to_Z x) ] |
| 794 |
| [ H : _ |- _ ] => apply H; clear H |
| 795 |
| [ H : context[eval (?f _) mod ?m = _] |- context[eval (?f _) mod ?m] ] |
| 796 |
=> rewrite H |
| 797 |
| [ H : context[eval (?f _ _) mod ?m = _] |- context[eval (?f _ _) mod ?m] ] |
| 798 |
=> rewrite H |
| 799 |
end |
| 800 |
| progress (push_Zmod; pull_Zmod); try (f_equal; lia) ]. |
| 801 |
Qed. |
| 802 |
End ring. |
| 803 |
End __. |
| 804 |
End WordByWordMontgomery. |
| 805 |
|
| 806 |
Module BarrettReduction. |
| 807 |
Section __. |
| 808 |
Context (k M : Z). |
| 809 |
|
| 810 |
Definition barrett_red_correct |
| 811 |
(barrett_red : Z -> Z -> Z) |
| 812 |
:= forall xLow xHigh, |
| 813 |
0 <= xLow < 2 ^ k |
| 814 |
-> 0 <= xHigh < M |
| 815 |
-> barrett_red xLow xHigh = (xLow + 2 ^ k * xHigh) mod M. |
| 816 |
End __. |
| 817 |
End BarrettReduction. |
| 818 |
|
| 819 |
Module MontgomeryReduction. |
| 820 |
Section __. |
| 821 |
Context (N R R' : Z). |
| 822 |
|
| 823 |
Definition montred_correct |
| 824 |
(mont_red : Z -> Z -> Z) |
| 825 |
:= forall lo hi, |
| 826 |
0 <= lo < R |
| 827 |
-> 0 <= hi < R |
| 828 |
-> 0 <= lo + R * hi < R * N |
| 829 |
-> mont_red lo hi = ((lo + R * hi) * R') mod N. |
| 830 |
End __. |
| 831 |
End MontgomeryReduction. |