| 1 |
(** * Push-Button Synthesis of Unsaturated Solinas: Reification Cache *) |
| 2 |
From Coq Require Import ZArith. |
| 3 |
From Coq Require Import Derive. |
| 4 |
Require Import Crypto.Arithmetic.Core. |
| 5 |
Require Import Crypto.Arithmetic.Freeze. |
| 6 |
Require Import Crypto.Arithmetic.ModOps. |
| 7 |
Require Import Crypto.PushButtonSynthesis.ReificationCache. |
| 8 |
Local Open Scope Z_scope. |
| 9 |
|
| 10 |
Import Associational Positional. |
| 11 |
|
| 12 |
Local Set Keyed Unification. (* needed for making [autorewrite] fast, c.f. COQBUG(https://github.com/coq/coq/issues/9283) *) |
| 13 |
|
| 14 |
Module Export UnsaturatedSolinas. |
| 15 |
Definition zeromod limbwidth_num limbwidth_den s c n := encodemod limbwidth_num limbwidth_den s c n 0. |
| 16 |
Definition onemod limbwidth_num limbwidth_den s c n := encodemod limbwidth_num limbwidth_den s c n 1. |
| 17 |
Definition primemod limbwidth_num limbwidth_den s c n := encodemod limbwidth_num limbwidth_den s c n (s - Associational.eval c). |
| 18 |
Definition evalmod limbwidth_num limbwidth_den n := Positional.eval (weight limbwidth_num limbwidth_den) n. |
| 19 |
Definition bytes_evalmod s := Positional.eval (weight 8 1) (bytes_n s). |
| 20 |
|
| 21 |
(** |
| 22 |
<< |
| 23 |
#!/usr/bin/env python |
| 24 |
|
| 25 |
indent = ' ' |
| 26 |
|
| 27 |
print((indent + '(' + r'''** |
| 28 |
<< |
| 29 |
%s |
| 30 |
>> |
| 31 |
*''' + ')\n') % open(__file__, 'r').read()) |
| 32 |
|
| 33 |
same_ops = ('carry_mul', 'carry_square', 'carry_scmul', 'carry', 'encode', 'add', 'sub', 'opp', 'carry_add', 'carry_sub', 'carry_opp', 'zero', 'one', 'prime', 'eval', 'bytes_eval') |
| 34 |
ops = [(op, f'{op}mod') for op in same_ops] + [('to_bytes', 'freeze_to_bytesmod'), ('from_bytes', 'from_bytesmod')] |
| 35 |
for op, opmod in ops: |
| 36 |
print((fr'''{indent}Derive reified_{op}_gen |
| 37 |
SuchThat (is_reification_of reified_{op}_gen {opmod}) |
| 38 |
As reified_{op}_gen_correct. |
| 39 |
Proof. Time cache_reify (). Time Qed. |
| 40 |
Local Definition reified_{op}_gen_correct_proj1 := proj1 reified_{op}_gen_correct. |
| 41 |
Local Definition reified_{op}_gen_correct_proj2 := proj2 reified_{op}_gen_correct. |
| 42 |
#[global] |
| 43 |
Hint Extern 1 (_ = _) => apply_cached_reification {opmod} reified_{op}_gen_correct_proj1 : reify_cache_gen. |
| 44 |
#[global] |
| 45 |
Hint Immediate reified_{op}_gen_correct_proj2 : wf_gen_cache. |
| 46 |
#[global] |
| 47 |
Hint Rewrite reified_{op}_gen_correct_proj1 : interp_gen_cache. |
| 48 |
Local Opaque reified_{op}_gen. (* needed for making [autorewrite] not take a very long time *)''').replace('\n', f'\n{indent}') + '\n') |
| 49 |
|
| 50 |
>> |
| 51 |
*) |
| 52 |
|
| 53 |
Derive reified_carry_mul_gen |
| 54 |
SuchThat (is_reification_of reified_carry_mul_gen carry_mulmod) |
| 55 |
As reified_carry_mul_gen_correct. |
| 56 |
Proof. Time cache_reify (). Time Qed. |
| 57 |
Local Definition reified_carry_mul_gen_correct_proj1 := proj1 reified_carry_mul_gen_correct. |
| 58 |
Local Definition reified_carry_mul_gen_correct_proj2 := proj2 reified_carry_mul_gen_correct. |
| 59 |
#[global] |
| 60 |
Hint Extern 1 (_ = _) => apply_cached_reification carry_mulmod reified_carry_mul_gen_correct_proj1 : reify_cache_gen. |
| 61 |
#[global] |
| 62 |
Hint Immediate reified_carry_mul_gen_correct_proj2 : wf_gen_cache. |
| 63 |
#[global] |
| 64 |
Hint Rewrite reified_carry_mul_gen_correct_proj1 : interp_gen_cache. |
| 65 |
Local Opaque reified_carry_mul_gen. (* needed for making [autorewrite] not take a very long time *) |
| 66 |
|
| 67 |
Derive reified_carry_square_gen |
| 68 |
SuchThat (is_reification_of reified_carry_square_gen carry_squaremod) |
| 69 |
As reified_carry_square_gen_correct. |
| 70 |
Proof. Time cache_reify (). Time Qed. |
| 71 |
Local Definition reified_carry_square_gen_correct_proj1 := proj1 reified_carry_square_gen_correct. |
| 72 |
Local Definition reified_carry_square_gen_correct_proj2 := proj2 reified_carry_square_gen_correct. |
| 73 |
#[global] |
| 74 |
Hint Extern 1 (_ = _) => apply_cached_reification carry_squaremod reified_carry_square_gen_correct_proj1 : reify_cache_gen. |
| 75 |
#[global] |
| 76 |
Hint Immediate reified_carry_square_gen_correct_proj2 : wf_gen_cache. |
| 77 |
#[global] |
| 78 |
Hint Rewrite reified_carry_square_gen_correct_proj1 : interp_gen_cache. |
| 79 |
Local Opaque reified_carry_square_gen. (* needed for making [autorewrite] not take a very long time *) |
| 80 |
|
| 81 |
Derive reified_carry_scmul_gen |
| 82 |
SuchThat (is_reification_of reified_carry_scmul_gen carry_scmulmod) |
| 83 |
As reified_carry_scmul_gen_correct. |
| 84 |
Proof. Time cache_reify (). Time Qed. |
| 85 |
Local Definition reified_carry_scmul_gen_correct_proj1 := proj1 reified_carry_scmul_gen_correct. |
| 86 |
Local Definition reified_carry_scmul_gen_correct_proj2 := proj2 reified_carry_scmul_gen_correct. |
| 87 |
#[global] |
| 88 |
Hint Extern 1 (_ = _) => apply_cached_reification carry_scmulmod reified_carry_scmul_gen_correct_proj1 : reify_cache_gen. |
| 89 |
#[global] |
| 90 |
Hint Immediate reified_carry_scmul_gen_correct_proj2 : wf_gen_cache. |
| 91 |
#[global] |
| 92 |
Hint Rewrite reified_carry_scmul_gen_correct_proj1 : interp_gen_cache. |
| 93 |
Local Opaque reified_carry_scmul_gen. (* needed for making [autorewrite] not take a very long time *) |
| 94 |
|
| 95 |
Derive reified_carry_gen |
| 96 |
SuchThat (is_reification_of reified_carry_gen carrymod) |
| 97 |
As reified_carry_gen_correct. |
| 98 |
Proof. Time cache_reify (). Time Qed. |
| 99 |
Local Definition reified_carry_gen_correct_proj1 := proj1 reified_carry_gen_correct. |
| 100 |
Local Definition reified_carry_gen_correct_proj2 := proj2 reified_carry_gen_correct. |
| 101 |
#[global] |
| 102 |
Hint Extern 1 (_ = _) => apply_cached_reification carrymod reified_carry_gen_correct_proj1 : reify_cache_gen. |
| 103 |
#[global] |
| 104 |
Hint Immediate reified_carry_gen_correct_proj2 : wf_gen_cache. |
| 105 |
#[global] |
| 106 |
Hint Rewrite reified_carry_gen_correct_proj1 : interp_gen_cache. |
| 107 |
Local Opaque reified_carry_gen. (* needed for making [autorewrite] not take a very long time *) |
| 108 |
|
| 109 |
Derive reified_encode_gen |
| 110 |
SuchThat (is_reification_of reified_encode_gen encodemod) |
| 111 |
As reified_encode_gen_correct. |
| 112 |
Proof. Time cache_reify (). Time Qed. |
| 113 |
Local Definition reified_encode_gen_correct_proj1 := proj1 reified_encode_gen_correct. |
| 114 |
Local Definition reified_encode_gen_correct_proj2 := proj2 reified_encode_gen_correct. |
| 115 |
#[global] |
| 116 |
Hint Extern 1 (_ = _) => apply_cached_reification encodemod reified_encode_gen_correct_proj1 : reify_cache_gen. |
| 117 |
#[global] |
| 118 |
Hint Immediate reified_encode_gen_correct_proj2 : wf_gen_cache. |
| 119 |
#[global] |
| 120 |
Hint Rewrite reified_encode_gen_correct_proj1 : interp_gen_cache. |
| 121 |
Local Opaque reified_encode_gen. (* needed for making [autorewrite] not take a very long time *) |
| 122 |
|
| 123 |
Derive reified_add_gen |
| 124 |
SuchThat (is_reification_of reified_add_gen addmod) |
| 125 |
As reified_add_gen_correct. |
| 126 |
Proof. Time cache_reify (). Time Qed. |
| 127 |
Local Definition reified_add_gen_correct_proj1 := proj1 reified_add_gen_correct. |
| 128 |
Local Definition reified_add_gen_correct_proj2 := proj2 reified_add_gen_correct. |
| 129 |
#[global] |
| 130 |
Hint Extern 1 (_ = _) => apply_cached_reification addmod reified_add_gen_correct_proj1 : reify_cache_gen. |
| 131 |
#[global] |
| 132 |
Hint Immediate reified_add_gen_correct_proj2 : wf_gen_cache. |
| 133 |
#[global] |
| 134 |
Hint Rewrite reified_add_gen_correct_proj1 : interp_gen_cache. |
| 135 |
Local Opaque reified_add_gen. (* needed for making [autorewrite] not take a very long time *) |
| 136 |
|
| 137 |
Derive reified_sub_gen |
| 138 |
SuchThat (is_reification_of reified_sub_gen submod) |
| 139 |
As reified_sub_gen_correct. |
| 140 |
Proof. Time cache_reify (). Time Qed. |
| 141 |
Local Definition reified_sub_gen_correct_proj1 := proj1 reified_sub_gen_correct. |
| 142 |
Local Definition reified_sub_gen_correct_proj2 := proj2 reified_sub_gen_correct. |
| 143 |
#[global] |
| 144 |
Hint Extern 1 (_ = _) => apply_cached_reification submod reified_sub_gen_correct_proj1 : reify_cache_gen. |
| 145 |
#[global] |
| 146 |
Hint Immediate reified_sub_gen_correct_proj2 : wf_gen_cache. |
| 147 |
#[global] |
| 148 |
Hint Rewrite reified_sub_gen_correct_proj1 : interp_gen_cache. |
| 149 |
Local Opaque reified_sub_gen. (* needed for making [autorewrite] not take a very long time *) |
| 150 |
|
| 151 |
Derive reified_opp_gen |
| 152 |
SuchThat (is_reification_of reified_opp_gen oppmod) |
| 153 |
As reified_opp_gen_correct. |
| 154 |
Proof. Time cache_reify (). Time Qed. |
| 155 |
Local Definition reified_opp_gen_correct_proj1 := proj1 reified_opp_gen_correct. |
| 156 |
Local Definition reified_opp_gen_correct_proj2 := proj2 reified_opp_gen_correct. |
| 157 |
#[global] |
| 158 |
Hint Extern 1 (_ = _) => apply_cached_reification oppmod reified_opp_gen_correct_proj1 : reify_cache_gen. |
| 159 |
#[global] |
| 160 |
Hint Immediate reified_opp_gen_correct_proj2 : wf_gen_cache. |
| 161 |
#[global] |
| 162 |
Hint Rewrite reified_opp_gen_correct_proj1 : interp_gen_cache. |
| 163 |
Local Opaque reified_opp_gen. (* needed for making [autorewrite] not take a very long time *) |
| 164 |
|
| 165 |
Derive reified_carry_add_gen |
| 166 |
SuchThat (is_reification_of reified_carry_add_gen carry_addmod) |
| 167 |
As reified_carry_add_gen_correct. |
| 168 |
Proof. Time cache_reify (). Time Qed. |
| 169 |
Local Definition reified_carry_add_gen_correct_proj1 := proj1 reified_carry_add_gen_correct. |
| 170 |
Local Definition reified_carry_add_gen_correct_proj2 := proj2 reified_carry_add_gen_correct. |
| 171 |
#[global] |
| 172 |
Hint Extern 1 (_ = _) => apply_cached_reification carry_addmod reified_carry_add_gen_correct_proj1 : reify_cache_gen. |
| 173 |
#[global] |
| 174 |
Hint Immediate reified_carry_add_gen_correct_proj2 : wf_gen_cache. |
| 175 |
#[global] |
| 176 |
Hint Rewrite reified_carry_add_gen_correct_proj1 : interp_gen_cache. |
| 177 |
Local Opaque reified_carry_add_gen. (* needed for making [autorewrite] not take a very long time *) |
| 178 |
|
| 179 |
Derive reified_carry_sub_gen |
| 180 |
SuchThat (is_reification_of reified_carry_sub_gen carry_submod) |
| 181 |
As reified_carry_sub_gen_correct. |
| 182 |
Proof. Time cache_reify (). Time Qed. |
| 183 |
Local Definition reified_carry_sub_gen_correct_proj1 := proj1 reified_carry_sub_gen_correct. |
| 184 |
Local Definition reified_carry_sub_gen_correct_proj2 := proj2 reified_carry_sub_gen_correct. |
| 185 |
#[global] |
| 186 |
Hint Extern 1 (_ = _) => apply_cached_reification carry_submod reified_carry_sub_gen_correct_proj1 : reify_cache_gen. |
| 187 |
#[global] |
| 188 |
Hint Immediate reified_carry_sub_gen_correct_proj2 : wf_gen_cache. |
| 189 |
#[global] |
| 190 |
Hint Rewrite reified_carry_sub_gen_correct_proj1 : interp_gen_cache. |
| 191 |
Local Opaque reified_carry_sub_gen. (* needed for making [autorewrite] not take a very long time *) |
| 192 |
|
| 193 |
Derive reified_carry_opp_gen |
| 194 |
SuchThat (is_reification_of reified_carry_opp_gen carry_oppmod) |
| 195 |
As reified_carry_opp_gen_correct. |
| 196 |
Proof. Time cache_reify (). Time Qed. |
| 197 |
Local Definition reified_carry_opp_gen_correct_proj1 := proj1 reified_carry_opp_gen_correct. |
| 198 |
Local Definition reified_carry_opp_gen_correct_proj2 := proj2 reified_carry_opp_gen_correct. |
| 199 |
#[global] |
| 200 |
Hint Extern 1 (_ = _) => apply_cached_reification carry_oppmod reified_carry_opp_gen_correct_proj1 : reify_cache_gen. |
| 201 |
#[global] |
| 202 |
Hint Immediate reified_carry_opp_gen_correct_proj2 : wf_gen_cache. |
| 203 |
#[global] |
| 204 |
Hint Rewrite reified_carry_opp_gen_correct_proj1 : interp_gen_cache. |
| 205 |
Local Opaque reified_carry_opp_gen. (* needed for making [autorewrite] not take a very long time *) |
| 206 |
|
| 207 |
Derive reified_zero_gen |
| 208 |
SuchThat (is_reification_of reified_zero_gen zeromod) |
| 209 |
As reified_zero_gen_correct. |
| 210 |
Proof. Time cache_reify (). Time Qed. |
| 211 |
Local Definition reified_zero_gen_correct_proj1 := proj1 reified_zero_gen_correct. |
| 212 |
Local Definition reified_zero_gen_correct_proj2 := proj2 reified_zero_gen_correct. |
| 213 |
#[global] |
| 214 |
Hint Extern 1 (_ = _) => apply_cached_reification zeromod reified_zero_gen_correct_proj1 : reify_cache_gen. |
| 215 |
#[global] |
| 216 |
Hint Immediate reified_zero_gen_correct_proj2 : wf_gen_cache. |
| 217 |
#[global] |
| 218 |
Hint Rewrite reified_zero_gen_correct_proj1 : interp_gen_cache. |
| 219 |
Local Opaque reified_zero_gen. (* needed for making [autorewrite] not take a very long time *) |
| 220 |
|
| 221 |
Derive reified_one_gen |
| 222 |
SuchThat (is_reification_of reified_one_gen onemod) |
| 223 |
As reified_one_gen_correct. |
| 224 |
Proof. Time cache_reify (). Time Qed. |
| 225 |
Local Definition reified_one_gen_correct_proj1 := proj1 reified_one_gen_correct. |
| 226 |
Local Definition reified_one_gen_correct_proj2 := proj2 reified_one_gen_correct. |
| 227 |
#[global] |
| 228 |
Hint Extern 1 (_ = _) => apply_cached_reification onemod reified_one_gen_correct_proj1 : reify_cache_gen. |
| 229 |
#[global] |
| 230 |
Hint Immediate reified_one_gen_correct_proj2 : wf_gen_cache. |
| 231 |
#[global] |
| 232 |
Hint Rewrite reified_one_gen_correct_proj1 : interp_gen_cache. |
| 233 |
Local Opaque reified_one_gen. (* needed for making [autorewrite] not take a very long time *) |
| 234 |
|
| 235 |
Derive reified_prime_gen |
| 236 |
SuchThat (is_reification_of reified_prime_gen primemod) |
| 237 |
As reified_prime_gen_correct. |
| 238 |
Proof. Time cache_reify (). Time Qed. |
| 239 |
Local Definition reified_prime_gen_correct_proj1 := proj1 reified_prime_gen_correct. |
| 240 |
Local Definition reified_prime_gen_correct_proj2 := proj2 reified_prime_gen_correct. |
| 241 |
#[global] |
| 242 |
Hint Extern 1 (_ = _) => apply_cached_reification primemod reified_prime_gen_correct_proj1 : reify_cache_gen. |
| 243 |
#[global] |
| 244 |
Hint Immediate reified_prime_gen_correct_proj2 : wf_gen_cache. |
| 245 |
#[global] |
| 246 |
Hint Rewrite reified_prime_gen_correct_proj1 : interp_gen_cache. |
| 247 |
Local Opaque reified_prime_gen. (* needed for making [autorewrite] not take a very long time *) |
| 248 |
|
| 249 |
Derive reified_eval_gen |
| 250 |
SuchThat (is_reification_of reified_eval_gen evalmod) |
| 251 |
As reified_eval_gen_correct. |
| 252 |
Proof. Time cache_reify (). Time Qed. |
| 253 |
Local Definition reified_eval_gen_correct_proj1 := proj1 reified_eval_gen_correct. |
| 254 |
Local Definition reified_eval_gen_correct_proj2 := proj2 reified_eval_gen_correct. |
| 255 |
#[global] |
| 256 |
Hint Extern 1 (_ = _) => apply_cached_reification evalmod reified_eval_gen_correct_proj1 : reify_cache_gen. |
| 257 |
#[global] |
| 258 |
Hint Immediate reified_eval_gen_correct_proj2 : wf_gen_cache. |
| 259 |
#[global] |
| 260 |
Hint Rewrite reified_eval_gen_correct_proj1 : interp_gen_cache. |
| 261 |
Local Opaque reified_eval_gen. (* needed for making [autorewrite] not take a very long time *) |
| 262 |
|
| 263 |
Derive reified_bytes_eval_gen |
| 264 |
SuchThat (is_reification_of reified_bytes_eval_gen bytes_evalmod) |
| 265 |
As reified_bytes_eval_gen_correct. |
| 266 |
Proof. Time cache_reify (). Time Qed. |
| 267 |
Local Definition reified_bytes_eval_gen_correct_proj1 := proj1 reified_bytes_eval_gen_correct. |
| 268 |
Local Definition reified_bytes_eval_gen_correct_proj2 := proj2 reified_bytes_eval_gen_correct. |
| 269 |
#[global] |
| 270 |
Hint Extern 1 (_ = _) => apply_cached_reification bytes_evalmod reified_bytes_eval_gen_correct_proj1 : reify_cache_gen. |
| 271 |
#[global] |
| 272 |
Hint Immediate reified_bytes_eval_gen_correct_proj2 : wf_gen_cache. |
| 273 |
#[global] |
| 274 |
Hint Rewrite reified_bytes_eval_gen_correct_proj1 : interp_gen_cache. |
| 275 |
Local Opaque reified_bytes_eval_gen. (* needed for making [autorewrite] not take a very long time *) |
| 276 |
|
| 277 |
Derive reified_to_bytes_gen |
| 278 |
SuchThat (is_reification_of reified_to_bytes_gen freeze_to_bytesmod) |
| 279 |
As reified_to_bytes_gen_correct. |
| 280 |
Proof. Time cache_reify (). Time Qed. |
| 281 |
Local Definition reified_to_bytes_gen_correct_proj1 := proj1 reified_to_bytes_gen_correct. |
| 282 |
Local Definition reified_to_bytes_gen_correct_proj2 := proj2 reified_to_bytes_gen_correct. |
| 283 |
#[global] |
| 284 |
Hint Extern 1 (_ = _) => apply_cached_reification freeze_to_bytesmod reified_to_bytes_gen_correct_proj1 : reify_cache_gen. |
| 285 |
#[global] |
| 286 |
Hint Immediate reified_to_bytes_gen_correct_proj2 : wf_gen_cache. |
| 287 |
#[global] |
| 288 |
Hint Rewrite reified_to_bytes_gen_correct_proj1 : interp_gen_cache. |
| 289 |
Local Opaque reified_to_bytes_gen. (* needed for making [autorewrite] not take a very long time *) |
| 290 |
|
| 291 |
Derive reified_from_bytes_gen |
| 292 |
SuchThat (is_reification_of reified_from_bytes_gen from_bytesmod) |
| 293 |
As reified_from_bytes_gen_correct. |
| 294 |
Proof. Time cache_reify (). Time Qed. |
| 295 |
Local Definition reified_from_bytes_gen_correct_proj1 := proj1 reified_from_bytes_gen_correct. |
| 296 |
Local Definition reified_from_bytes_gen_correct_proj2 := proj2 reified_from_bytes_gen_correct. |
| 297 |
#[global] |
| 298 |
Hint Extern 1 (_ = _) => apply_cached_reification from_bytesmod reified_from_bytes_gen_correct_proj1 : reify_cache_gen. |
| 299 |
#[global] |
| 300 |
Hint Immediate reified_from_bytes_gen_correct_proj2 : wf_gen_cache. |
| 301 |
#[global] |
| 302 |
Hint Rewrite reified_from_bytes_gen_correct_proj1 : interp_gen_cache. |
| 303 |
Local Opaque reified_from_bytes_gen. (* needed for making [autorewrite] not take a very long time *) |
| 304 |
End UnsaturatedSolinas. |