| 1 |
From Coq Require Import String. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import ZArith. |
| 4 |
Require Import Crypto.Util.Option. |
| 5 |
Require Import Crypto.UnsaturatedSolinasHeuristics. |
| 6 |
Require Import Crypto.Util.Strings.Show. |
| 7 |
Require Import Crypto.Util.Strings.ParseArithmeticToTaps. |
| 8 |
Require Import Crypto.Util.OptionList. |
| 9 |
Import ListNotations. |
| 10 |
Import Coq.Lists.List. |
| 11 |
Local Open Scope option_scope. |
| 12 |
Local Open Scope string_scope. |
| 13 |
Local Open Scope list_scope. |
| 14 |
Local Open Scope nat_scope. |
| 15 |
Local Open Scope Z_scope. |
| 16 |
Local Open Scope core_scope. |
| 17 |
|
| 18 |
Definition single_tap_primes : list string := |
| 19 |
[ |
| 20 |
(* single-tap: *) |
| 21 |
|
| 22 |
"2^127 - 1"; (* "kummer strikes back" *) |
| 23 |
"2^129 - 25"; |
| 24 |
"2^130 - 5"; (* poly1305 *) |
| 25 |
"2^137 - 13"; |
| 26 |
"2^140 - 27"; |
| 27 |
"2^141 - 9"; |
| 28 |
"2^150 - 5"; |
| 29 |
"2^150 - 3"; |
| 30 |
"2^152 - 17"; |
| 31 |
"2^158 - 15"; |
| 32 |
"2^165 - 25"; |
| 33 |
"2^166 - 5"; |
| 34 |
"2^171 - 19"; |
| 35 |
"2^174 - 17"; |
| 36 |
"2^174 - 3"; |
| 37 |
"2^189 - 25"; |
| 38 |
"2^190 - 11"; |
| 39 |
"2^191 - 19"; |
| 40 |
"2^194 - 33"; |
| 41 |
"2^196 - 15"; |
| 42 |
"2^198 - 17"; |
| 43 |
"2^206 - 5"; |
| 44 |
"2^212 - 29"; |
| 45 |
"2^213 - 3"; |
| 46 |
"2^221 - 3"; |
| 47 |
"2^222 - 117"; |
| 48 |
"2^226 - 5"; |
| 49 |
"2^230 - 27"; |
| 50 |
"2^235 - 15"; |
| 51 |
"2^243 - 9"; |
| 52 |
"2^251 - 9"; |
| 53 |
"2^255 - 765"; |
| 54 |
"2^255 - 19"; (* curve25519 *) |
| 55 |
"2^256 - 189"; |
| 56 |
"2^266 - 3"; |
| 57 |
"2^285 - 9"; |
| 58 |
"2^291 - 19"; |
| 59 |
"2^321 - 9"; |
| 60 |
"2^336 - 17"; |
| 61 |
"2^336 - 3"; |
| 62 |
"2^338 - 15"; |
| 63 |
"2^369 - 25"; |
| 64 |
"2^379 - 19"; |
| 65 |
"2^382 - 105"; |
| 66 |
"2^383 - 421"; |
| 67 |
"2^383 - 187"; |
| 68 |
"2^383 - 31"; |
| 69 |
"2^384 - 317"; |
| 70 |
"2^389 - 21"; |
| 71 |
"2^401 - 31"; |
| 72 |
"2^413 - 21"; |
| 73 |
"2^414 - 17"; |
| 74 |
"2^444 - 17"; |
| 75 |
"2^452 - 3"; |
| 76 |
"2^468 - 17"; |
| 77 |
"2^488 - 17"; |
| 78 |
"2^489 - 21"; |
| 79 |
"2^495 - 31"; |
| 80 |
"2^511 - 481"; |
| 81 |
"2^511 - 187"; |
| 82 |
"2^512 - 569"; |
| 83 |
"2^521 - 1" (* p521 *) |
| 84 |
]. |
| 85 |
|
| 86 |
Definition two_tap_golden_ratio_primes : list string := |
| 87 |
[ |
| 88 |
|
| 89 |
(* two taps, golden ratio: *) |
| 90 |
|
| 91 |
"2^192 - 2^64 - 1"; |
| 92 |
"2^216 - 2^108 - 1"; |
| 93 |
"2^322 - 2^161 - 1"; |
| 94 |
"2^416 - 2^208 - 1"; |
| 95 |
"2^448 - 2^224 - 1"; (* goldilocks *) |
| 96 |
"2^450 - 2^225 - 1"; |
| 97 |
"2^480 - 2^240 - 1" (* ridinghood *) |
| 98 |
]. |
| 99 |
|
| 100 |
Definition two_or_more_tap_primes : list string := |
| 101 |
[ |
| 102 |
(* two or more taps *) |
| 103 |
|
| 104 |
"2^205 - 45*2^198 - 1"; |
| 105 |
"2^224 - 2^96 + 1"; (* p224 *) |
| 106 |
"2^256 - 2^224 + 2^192 + 2^96 - 1"; (* p256 *) |
| 107 |
"2^256 - 2^32 - 977"; (* bitcoin *) |
| 108 |
"2^256 - 4294968273"; (* bitcoin, for 64-bit impl *) |
| 109 |
"2^384 - 2^128 - 2^96 + 2^32 - 1" (* p384 *) |
| 110 |
]. |
| 111 |
|
| 112 |
Definition montgomery_friendly_primes : list string := |
| 113 |
[ |
| 114 |
(* Montgomery-Friendly *) |
| 115 |
|
| 116 |
"2^256 - 88*2^240 - 1"; |
| 117 |
"2^254 - 127*2^240 - 1"; |
| 118 |
"2^384 - 79*2^376 - 1"; |
| 119 |
"2^384 - 5*2^368 - 1"; |
| 120 |
"2^512 - 491*2^496 - 1"; |
| 121 |
"2^510 - 290*2^496 - 1" |
| 122 |
]. |
| 123 |
|
| 124 |
Definition multitap_primes : list string := |
| 125 |
two_tap_golden_ratio_primes |
| 126 |
++ two_or_more_tap_primes |
| 127 |
++ montgomery_friendly_primes. |
| 128 |
|
| 129 |
Definition primes : list string := |
| 130 |
single_tap_primes ++ multitap_primes. |
| 131 |
|
| 132 |
Local Instance : tight_upperbound_fraction_opt := default_tight_upperbound_fraction. |
| 133 |
|
| 134 |
(* 15.5 s for primes up to 2^301 *) |
| 135 |
(* 25.674 s for primes up to 2^351 *) |
| 136 |
(* 76.298 s for primes 2^300 up to 2^401 *) |
| 137 |
Local Open Scope nat_scope. |
| 138 |
Local Open Scope core_scope. |
| 139 |
Local Set NativeCompute Profiling. |
| 140 |
Local Set NativeCompute Timing. |
| 141 |
Time Definition possible_limbs := |
| 142 |
Eval native_compute in |
| 143 |
Option.List.lift |
| 144 |
(Option.List.map |
| 145 |
(fun p => match parseZ_arith_to_taps p with |
| 146 |
| Some (s, c) |
| 147 |
=> if (s <=? 2^350)%Z |
| 148 |
then Some (Some (p, [(64, get_possible_limbs s c 64); (32, get_possible_limbs s c 32)])) |
| 149 |
else None |
| 150 |
| None => Some None |
| 151 |
end) |
| 152 |
primes). |
| 153 |
|
| 154 |
Import ListUtil. |
| 155 |
Time Definition balances |
| 156 |
:= Eval native_compute in |
| 157 |
option_map |
| 158 |
(fun ps |
| 159 |
=> Option.List.lift |
| 160 |
(List.map |
| 161 |
(fun '(p, ls) |
| 162 |
=> option_map |
| 163 |
(fun '(s, c) |
| 164 |
=> (p, |
| 165 |
List.map |
| 166 |
(fun '(bw, ns) |
| 167 |
=> List.map |
| 168 |
(fun n |
| 169 |
=> let bal := @balance default_tight_upperbound_fraction n s c in |
| 170 |
let tub := @tight_upperbounds default_tight_upperbound_fraction n s c in |
| 171 |
let dif := List.map2 Z.sub bal tub in |
| 172 |
((bw, n), |
| 173 |
("balance: ", |
| 174 |
(let show_lvl_Z := PowersOfTwo.show_lvl_Z in show bal), |
| 175 |
(let show_lvl_Z := Hex.show_lvl_Z in show bal)), |
| 176 |
("tight_upperbounds: ", |
| 177 |
(let show_lvl_Z := PowersOfTwo.show_lvl_Z in show tub), |
| 178 |
(let show_lvl_Z := Hex.show_lvl_Z in show tub)), |
| 179 |
("balance - tight_upperbounds: ", |
| 180 |
(let show_lvl_Z := PowersOfTwo.show_lvl_Z in show dif), |
| 181 |
(let show_lvl_Z := Hex.show_lvl_Z in show dif)))) |
| 182 |
ns) |
| 183 |
ls)) |
| 184 |
(parseZ_arith_to_taps p)) |
| 185 |
ps)) |
| 186 |
possible_limbs. |
| 187 |
Local Notation "[ ]" := nil (format "[ ]") : core_scope. |
| 188 |
Local Notation "[ x ]" := (cons x nil) : core_scope. |
| 189 |
Local Notation "[ x ; y ; .. ; z ]" := (cons x (cons y .. (cons z nil) ..)) (format "'[hv ' [ x ; '/' y ; '/' .. ; '/' z ] ']'") : core_scope. |
| 190 |
Local Close Scope list_scope. |
| 191 |
Local Open Scope core_scope. |
| 192 |
Set Printing Width 250. |
| 193 |
Redirect "Crypto.UnsaturatedSolinasHeuristics.Tests.get_possible_limbs" |
| 194 |
Print possible_limbs. |
| 195 |
Redirect "Crypto.UnsaturatedSolinasHeuristics.Tests.get_balances" |
| 196 |
Print balances. |