prime.odin 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /*
  2. Copyright 2021 Jeroen van Rijn <[email protected]>.
  3. Made available under Odin's BSD-3 license.
  4. An arbitrary precision mathematics implementation in Odin.
  5. For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
  6. The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.
  7. This file contains prime finding operations.
  8. */
  9. package math_big
  10. import rnd "core:math/rand"
  11. /*
  12. Determines if an Integer is divisible by one of the _PRIME_TABLE primes.
  13. Returns true if it is, false if not.
  14. */
  15. internal_int_prime_is_divisible :: proc(a: ^Int, allocator := context.allocator) -> (res: bool, err: Error) {
  16. assert_if_nil(a)
  17. context.allocator = allocator
  18. internal_clear_if_uninitialized(a) or_return
  19. for prime in _private_prime_table {
  20. rem := #force_inline int_mod_digit(a, prime) or_return
  21. if rem == 0 {
  22. return true, nil
  23. }
  24. }
  25. /*
  26. Default to not divisible.
  27. */
  28. return false, nil
  29. }
  30. /*
  31. This is a shell function that calls either the normal or Montgomery exptmod functions.
  32. Originally the call to the Montgomery code was embedded in the normal function but that
  33. wasted alot of stack space for nothing (since 99% of the time the Montgomery code would be called).
  34. Computes res == G**X mod P.
  35. Assumes `res`, `G`, `X` and `P` to not be `nil` and for `G`, `X` and `P` to have been initialized.
  36. */
  37. internal_int_power_modulo :: proc(res, G, X, P: ^Int, allocator := context.allocator) -> (err: Error) {
  38. context.allocator = allocator
  39. dr: int
  40. /*
  41. Modulus P must be positive.
  42. */
  43. if internal_is_negative(P) { return .Invalid_Argument }
  44. /*
  45. If exponent X is negative we have to recurse.
  46. */
  47. if internal_is_negative(X) {
  48. tmpG, tmpX := &Int{}, &Int{}
  49. defer internal_destroy(tmpG, tmpX)
  50. internal_init_multi(tmpG, tmpX) or_return
  51. /*
  52. First compute 1/G mod P.
  53. */
  54. internal_invmod(tmpG, G, P) or_return
  55. /*
  56. now get |X|.
  57. */
  58. internal_abs(tmpX, X) or_return
  59. /*
  60. And now compute (1/G)**|X| instead of G**X [X < 0].
  61. */
  62. return internal_int_exponent_mod(res, tmpG, tmpX, P)
  63. }
  64. /*
  65. Modified diminished radix reduction.
  66. */
  67. can_reduce_2k_l := _private_int_reduce_is_2k_l(P) or_return
  68. if can_reduce_2k_l {
  69. return _private_int_exponent_mod(res, G, X, P, 1)
  70. }
  71. /*
  72. Is it a DR modulus? default to no.
  73. */
  74. dr = 1 if _private_dr_is_modulus(P) else 0
  75. /*
  76. If not, is it a unrestricted DR modulus?
  77. */
  78. if dr == 0 {
  79. reduce_is_2k := _private_int_reduce_is_2k(P) or_return
  80. dr = 2 if reduce_is_2k else 0
  81. }
  82. /*
  83. If the modulus is odd or dr != 0 use the montgomery method.
  84. */
  85. if internal_int_is_odd(P) || dr != 0 {
  86. return _private_int_exponent_mod(res, G, X, P, dr)
  87. }
  88. /*
  89. Otherwise use the generic Barrett reduction technique.
  90. */
  91. return _private_int_exponent_mod(res, G, X, P, 0)
  92. }
  93. internal_int_exponent_mod :: internal_int_power_modulo
  94. internal_int_powmod :: internal_int_power_modulo
  95. internal_powmod :: proc { internal_int_power_modulo, }
  96. /*
  97. Kronecker/Legendre symbol (a|p)
  98. Straightforward implementation of algorithm 1.4.10 in
  99. Henri Cohen: "A Course in Computational Algebraic Number Theory"
  100. @book{cohen2013course,
  101. title={A course in computational algebraic number theory},
  102. author={Cohen, Henri},
  103. volume={138},
  104. year={2013},
  105. publisher={Springer Science \& Business Media}
  106. }
  107. Assumes `a` and `p` to not be `nil` and to have been initialized.
  108. */
  109. internal_int_kronecker :: proc(a, p: ^Int, allocator := context.allocator) -> (kronecker: int, err: Error) {
  110. context.allocator = allocator
  111. a1, p1, r := &Int{}, &Int{}, &Int{}
  112. defer internal_destroy(a1, p1, r)
  113. table := []int{0, 1, 0, -1, 0, -1, 0, 1}
  114. if internal_int_is_zero(p) {
  115. if a.used == 1 && a.digit[0] == 1 {
  116. return 1, nil
  117. } else {
  118. return 0, nil
  119. }
  120. }
  121. if internal_is_even(a) && internal_is_even(p) {
  122. return 0, nil
  123. }
  124. internal_copy(a1, a) or_return
  125. internal_copy(p1, p) or_return
  126. v := internal_count_lsb(p1) or_return
  127. internal_shr(p1, p1, v) or_return
  128. k := 1 if v & 1 == 0 else table[a.digit[0] & 7]
  129. if internal_is_negative(p1) {
  130. p1.sign = .Zero_or_Positive
  131. if internal_is_negative(a1) {
  132. k = -k
  133. }
  134. }
  135. internal_zero(r) or_return
  136. for {
  137. if internal_is_zero(a1) {
  138. if internal_eq(p1, 1) {
  139. return k, nil
  140. } else {
  141. return 0, nil
  142. }
  143. }
  144. v = internal_count_lsb(a1) or_return
  145. internal_shr(a1, a1, v) or_return
  146. if v & 1 == 1 {
  147. k = k * table[p1.digit[0] & 7]
  148. }
  149. if internal_is_negative(a1) {
  150. /*
  151. Compute k = (-1)^((a1)*(p1-1)/4) * k.
  152. a1.digit[0] + 1 cannot overflow because the MSB
  153. of the DIGIT type is not set by definition.
  154. */
  155. if ((a1.digit[0] + 1) & p1.digit[0] & 2) != 0 {
  156. k = -k
  157. }
  158. } else {
  159. /*
  160. Compute k = (-1)^((a1-1)*(p1-1)/4) * k.
  161. */
  162. if (a1.digit[0] & p1.digit[0] & 2) != 0 {
  163. k = -k
  164. }
  165. }
  166. internal_copy(r, a1) or_return
  167. r.sign = .Zero_or_Positive
  168. internal_mod(a1, p1, r) or_return
  169. internal_copy(p1, r) or_return
  170. }
  171. return
  172. }
  173. internal_int_legendre :: internal_int_kronecker
  174. /*
  175. Miller-Rabin test of "a" to the base of "b" as described in HAC pp. 139 Algorithm 4.24.
  176. Sets result to `false` if definitely composite or `true` if probably prime.
  177. Randomly the chance of error is no more than 1/4 and often very much lower.
  178. Assumes `a` and `b` not to be `nil` and to have been initialized.
  179. */
  180. internal_int_prime_miller_rabin :: proc(a, b: ^Int, allocator := context.allocator) -> (probably_prime: bool, err: Error) {
  181. context.allocator = allocator
  182. n1, y, r := &Int{}, &Int{}, &Int{}
  183. defer internal_destroy(n1, y, r)
  184. /*
  185. Ensure `b` > 1.
  186. */
  187. if internal_lte(b, 1) { return false, nil }
  188. /*
  189. Get `n1` = `a` - 1.
  190. */
  191. internal_copy(n1, a) or_return
  192. internal_sub(n1, n1, 1) or_return
  193. /*
  194. Set `2`**`s` * `r` = `n1`
  195. */
  196. internal_copy(r, n1) or_return
  197. /*
  198. Count the number of least significant bits which are zero.
  199. */
  200. s := internal_count_lsb(r) or_return
  201. /*
  202. Now divide `n` - 1 by `2`**`s`.
  203. */
  204. internal_shr(r, r, s) or_return
  205. /*
  206. Compute `y` = `b`**`r` mod `a`.
  207. */
  208. internal_int_exponent_mod(y, b, r, a) or_return
  209. /*
  210. If `y` != 1 and `y` != `n1` do.
  211. */
  212. if !internal_eq(y, 1) && !internal_eq(y, n1) {
  213. j := 1
  214. /*
  215. While `j` <= `s` - 1 and `y` != `n1`.
  216. */
  217. for j <= (s - 1) && !internal_eq(y, n1) {
  218. internal_sqrmod(y, y, a) or_return
  219. /*
  220. If `y` == 1 then composite.
  221. */
  222. if internal_eq(y, 1) {
  223. return false, nil
  224. }
  225. j += 1
  226. }
  227. /*
  228. If `y` != `n1` then composite.
  229. */
  230. if !internal_eq(y, n1) {
  231. return false, nil
  232. }
  233. }
  234. /*
  235. Probably prime now.
  236. */
  237. return true, nil
  238. }
  239. /*
  240. `a` is the big Int to test for primality.
  241. `miller_rabin_trials` can be one of the following:
  242. `< 0`: For `a` up to 3_317_044_064_679_887_385_961_981, set `miller_rabin_trials` to negative to run a predetermined
  243. number of trials for a deterministic answer.
  244. `= 0`: Run Miller-Rabin with bases 2, 3 and one random base < `a`. Non-deterministic.
  245. `> 0`: Run Miller-Rabin with bases 2, 3 and `miller_rabin_trials` number of random bases. Non-deterministic.
  246. `miller_rabin_only`:
  247. `false` Also use either Frobenius-Underwood or Lucas-Selfridge, depending on the compile-time `MATH_BIG_USE_FROBENIUS_TEST` choice.
  248. `true` Run Rabin-Miller trials but skip Frobenius-Underwood / Lucas-Selfridge.
  249. `r` takes a pointer to an instance of `core:math/rand`'s `Rand` and may be `nil` to use the global one.
  250. Returns `is_prime` (bool), where:
  251. `false` Definitively composite.
  252. `true` Probably prime if `miller_rabin_trials` >= 0, with increasing certainty with more trials.
  253. Deterministically prime if `miller_rabin_trials` = 0 for `a` up to 3_317_044_064_679_887_385_961_981.
  254. Assumes `a` not to be `nil` and to have been initialized.
  255. */
  256. internal_int_is_prime :: proc(a: ^Int, miller_rabin_trials := int(-1), miller_rabin_only := USE_MILLER_RABIN_ONLY, r: ^rnd.Rand = nil, allocator := context.allocator) -> (is_prime: bool, err: Error) {
  257. context.allocator = allocator
  258. miller_rabin_trials := miller_rabin_trials
  259. // Default to `no`.
  260. is_prime = false
  261. b, res := &Int{}, &Int{}
  262. defer internal_destroy(b, res)
  263. // Some shortcuts
  264. // `N` > 3
  265. if a.used == 1 {
  266. if a.digit[0] == 0 || a.digit[0] == 1 {
  267. return
  268. }
  269. if a.digit[0] == 2 {
  270. return true, nil
  271. }
  272. }
  273. // `N` must be odd.
  274. if internal_is_even(a) {
  275. return
  276. }
  277. // `N` is not a perfect square: floor(sqrt(`N`))^2 != `N`
  278. if internal_int_is_square(a) or_return { return }
  279. // Is the input equal to one of the primes in the table?
  280. for p in _private_prime_table {
  281. if internal_eq(a, p) {
  282. return true, nil
  283. }
  284. }
  285. // First perform trial division
  286. if internal_int_prime_is_divisible(a) or_return { return }
  287. // Run the Miller-Rabin test with base 2 for the BPSW test.
  288. internal_set(b, 2) or_return
  289. if !(internal_int_prime_miller_rabin(a, b) or_return) { return }
  290. // Rumours have it that Mathematica does a second M-R test with base 3.
  291. // Other rumours have it that their strong L-S test is slightly different.
  292. // It does not hurt, though, beside a bit of extra runtime.
  293. b.digit[0] += 1
  294. if !(internal_int_prime_miller_rabin(a, b) or_return) { return }
  295. // Both, the Frobenius-Underwood test and the the Lucas-Selfridge test are quite
  296. // slow so if speed is an issue, set `USE_MILLER_RABIN_ONLY` to use M-R tests with
  297. // bases 2, 3 and t random bases.
  298. if !miller_rabin_only {
  299. if miller_rabin_trials >= 0 {
  300. when MATH_BIG_USE_FROBENIUS_TEST {
  301. if !(internal_int_prime_frobenius_underwood(a) or_return) { return }
  302. } else {
  303. if !(internal_int_prime_strong_lucas_selfridge(a) or_return) { return }
  304. }
  305. }
  306. }
  307. // Run at least one Miller-Rabin test with a random base.
  308. // Don't replace this with `min`, because we try known deterministic bases
  309. // for certain sized inputs when `miller_rabin_trials` is negative.
  310. if miller_rabin_trials == 0 {
  311. miller_rabin_trials = 1
  312. }
  313. // Only recommended if the input range is known to be < 3_317_044_064_679_887_385_961_981
  314. // It uses the bases necessary for a deterministic M-R test if the input is smaller than 3_317_044_064_679_887_385_961_981
  315. // The caller has to check the size.
  316. // TODO: can be made a bit finer grained but comparing is not free.
  317. if miller_rabin_trials < 0 {
  318. p_max := 0
  319. // Sorenson, Jonathan; Webster, Jonathan (2015), "Strong Pseudoprimes to Twelve Prime Bases".
  320. // 0x437ae92817f9fc85b7e5 = 318_665_857_834_031_151_167_461
  321. atoi(b, "437ae92817f9fc85b7e5", 16) or_return
  322. if internal_lt(a, b) {
  323. p_max = 12
  324. } else {
  325. /* 0x2be6951adc5b22410a5fd = 3_317_044_064_679_887_385_961_981 */
  326. atoi(b, "2be6951adc5b22410a5fd", 16) or_return
  327. if internal_lt(a, b) {
  328. p_max = 13
  329. } else {
  330. return false, .Invalid_Argument
  331. }
  332. }
  333. // We did bases 2 and 3 already, skip them
  334. for ix := 2; ix < p_max; ix += 1 {
  335. internal_set(b, _private_prime_table[ix])
  336. if !(internal_int_prime_miller_rabin(a, b) or_return) { return }
  337. }
  338. } else if miller_rabin_trials > 0 {
  339. // Perform `miller_rabin_trials` M-R tests with random bases between 3 and "a".
  340. // See Fips 186.4 p. 126ff
  341. // The DIGITs have a defined bit-size but the size of a.digit is a simple 'int',
  342. // the size of which can depend on the platform.
  343. size_a := internal_count_bits(a)
  344. mask := (1 << uint(ilog2(size_a))) - 1
  345. /*
  346. Assuming the General Rieman hypothesis (never thought to write that in a
  347. comment) the upper bound can be lowered to 2*(log a)^2.
  348. E. Bach, "Explicit bounds for primality testing and related problems,"
  349. Math. Comp. 55 (1990), 355-380.
  350. size_a = (size_a/10) * 7;
  351. len = 2 * (size_a * size_a);
  352. E.g.: a number of size 2^2048 would be reduced to the upper limit
  353. floor(2048/10)*7 = 1428
  354. 2 * 1428^2 = 4078368
  355. (would have been ~4030331.9962 with floats and natural log instead)
  356. That number is smaller than 2^28, the default bit-size of DIGIT on 32-bit platforms.
  357. */
  358. /*
  359. How many tests, you might ask? Dana Jacobsen of Math::Prime::Util fame
  360. does exactly 1. In words: one. Look at the end of _GMP_is_prime() in
  361. Math-Prime-Util-GMP-0.50/primality.c if you do not believe it.
  362. The function rand() goes to some length to use a cryptographically
  363. good PRNG. That also means that the chance to always get the same base
  364. in the loop is non-zero, although very low.
  365. -- NOTE(Jeroen): This is not yet true in Odin, but I have some ideas.
  366. If the BPSW test and/or the additional Frobenious test have been
  367. performed instead of just the Miller-Rabin test with the bases 2 and 3,
  368. a single extra test should suffice, so such a very unlikely event will not do much harm.
  369. To preemptivly answer the dangling question: no, a witness does not need to be prime.
  370. */
  371. for ix := 0; ix < miller_rabin_trials; ix += 1 {
  372. // rand() guarantees the first digit to be non-zero
  373. internal_random(b, _DIGIT_TYPE_BITS, r) or_return
  374. // Reduce digit before casting because DIGIT might be bigger than
  375. // an unsigned int and "mask" on the other side is most probably not.
  376. l: int
  377. fips_rand := (uint)(b.digit[0] & DIGIT(mask))
  378. if fips_rand > (uint)(max(int) - _DIGIT_BITS) {
  379. l = max(int) / _DIGIT_BITS
  380. } else {
  381. l = (int(fips_rand) + _DIGIT_BITS) / _DIGIT_BITS
  382. }
  383. // Unlikely.
  384. if (l < 0) {
  385. ix -= 1
  386. continue
  387. }
  388. internal_random(b, l) or_return
  389. // That number might got too big and the witness has to be smaller than "a"
  390. l = internal_count_bits(b)
  391. if l >= size_a {
  392. l = (l - size_a) + 1
  393. internal_shr(b, b, l) or_return
  394. }
  395. // Although the chance for b <= 3 is miniscule, try again.
  396. if internal_lte(b, 3) {
  397. ix -= 1
  398. continue
  399. }
  400. if !(internal_int_prime_miller_rabin(a, b) or_return) { return }
  401. }
  402. }
  403. // Passed the test.
  404. return true, nil
  405. }
  406. /*
  407. * floor of positive solution of (2^16) - 1 = (a + 4) * (2 * a + 5)
  408. * TODO: Both values are smaller than N^(1/4), would have to use a bigint
  409. * for `a` instead, but any `a` bigger than about 120 are already so rare that
  410. * it is possible to ignore them and still get enough pseudoprimes.
  411. * But it is still a restriction of the set of available pseudoprimes
  412. * which makes this implementation less secure if used stand-alone.
  413. */
  414. _FROBENIUS_UNDERWOOD_A :: 32764
  415. internal_int_prime_frobenius_underwood :: proc(N: ^Int, allocator := context.allocator) -> (result: bool, err: Error) {
  416. context.allocator = allocator
  417. T1z, T2z, Np1z, sz, tz := &Int{}, &Int{}, &Int{}, &Int{}, &Int{}
  418. defer internal_destroy(T1z, T2z, Np1z, sz, tz)
  419. internal_init_multi(T1z, T2z, Np1z, sz, tz) or_return
  420. a, ap2: int
  421. frob: for a = 0; a < _FROBENIUS_UNDERWOOD_A; a += 1 {
  422. switch a {
  423. case 2, 4, 7, 8, 10, 14, 18, 23, 26, 28:
  424. continue frob
  425. }
  426. internal_set(T1z, i32((a * a) - 4))
  427. j := internal_int_kronecker(T1z, N) or_return
  428. switch j {
  429. case -1: break frob
  430. case 0: return false, nil
  431. }
  432. }
  433. // Tell it a composite and set return value accordingly.
  434. if a >= _FROBENIUS_UNDERWOOD_A { return false, .Max_Iterations_Reached }
  435. // Composite if N and (a+4)*(2*a+5) are not coprime.
  436. internal_set(T1z, u32((a + 4) * ((2 * a) + 5)))
  437. internal_int_gcd(T1z, T1z, N) or_return
  438. if !(T1z.used == 1 && T1z.digit[0] == 1) {
  439. // Composite.
  440. return false, nil
  441. }
  442. ap2 = a + 2
  443. internal_add(Np1z, N, 1) or_return
  444. internal_set(sz, 1) or_return
  445. internal_set(tz, 2) or_return
  446. for i := internal_count_bits(Np1z) - 2; i >= 0; i -= 1 {
  447. // temp = (sz * (a * sz + 2 * tz)) % N;
  448. // tz = ((tz - sz) * (tz + sz)) % N;
  449. // sz = temp;
  450. internal_int_shl1(T2z, tz) or_return
  451. // a = 0 at about 50% of the cases (non-square and odd input)
  452. if a != 0 {
  453. internal_mul(T1z, sz, DIGIT(a)) or_return
  454. internal_add(T2z, T2z, T1z) or_return
  455. }
  456. internal_mul(T1z, T2z, sz) or_return
  457. internal_sub(T2z, tz, sz) or_return
  458. internal_add(sz, sz, tz) or_return
  459. internal_mul(tz, sz, T2z) or_return
  460. internal_mod(tz, tz, N) or_return
  461. internal_mod(sz, T1z, N) or_return
  462. if bit, _ := internal_int_bitfield_extract_bool(Np1z, i); bit {
  463. // temp = (a+2) * sz + tz
  464. // tz = 2 * tz - sz
  465. // sz = temp
  466. if a == 0 {
  467. internal_int_shl1(T1z, sz) or_return
  468. } else {
  469. internal_mul(T1z, sz, DIGIT(ap2)) or_return
  470. }
  471. internal_add(T1z, T1z, tz) or_return
  472. internal_int_shl1(T2z, tz) or_return
  473. internal_sub(tz, T2z, sz)
  474. internal_swap(sz, T1z)
  475. }
  476. }
  477. internal_set(T1z, u32((2 * a) + 5)) or_return
  478. internal_mod(T1z, T1z, N) or_return
  479. result = internal_is_zero(sz) && internal_eq(tz, T1z)
  480. return
  481. }
  482. /*
  483. Strong Lucas-Selfridge test.
  484. returns true if it is a strong L-S prime, false if it is composite
  485. Code ported from Thomas Ray Nicely's implementation of the BPSW test at http://www.trnicely.net/misc/bpsw.html
  486. Freeware copyright (C) 2016 Thomas R. Nicely <http://www.trnicely.net>.
  487. Released into the public domain by the author, who disclaims any legal liability arising from its use.
  488. The multi-line comments are made by Thomas R. Nicely and are copied verbatim.
  489. (If that name sounds familiar, he is the guy who found the fdiv bug in the Pentium CPU.)
  490. */
  491. internal_int_prime_strong_lucas_selfridge :: proc(a: ^Int, allocator := context.allocator) -> (lucas_selfridge: bool, err: Error) {
  492. // TODO: choose better variable names!
  493. Dz, gcd, Np1, Uz, Vz, U2mz, V2mz, Qmz, Q2mz, Qkdz, T1z, T2z, T3z, T4z, Q2kdz := &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}
  494. defer internal_destroy(Dz, gcd, Np1, Uz, Vz, U2mz, V2mz, Qmz, Q2mz, Qkdz, T1z, T2z, T3z, T4z, Q2kdz)
  495. /*
  496. Find the first element D in the sequence {5, -7, 9, -11, 13, ...}
  497. such that Jacobi(D,N) = -1 (Selfridge's algorithm). Theory
  498. indicates that, if N is not a perfect square, D will "nearly
  499. always" be "small." Just in case, an overflow trap for D is included.
  500. */
  501. internal_init_multi(Dz, gcd, Np1, Uz, Vz, U2mz, V2mz, Qmz, Q2mz, Qkdz, T1z, T2z, T3z, T4z, Q2kdz) or_return
  502. D := 5
  503. sign := 1
  504. Ds : int
  505. for {
  506. Ds = sign * D
  507. sign = -sign
  508. internal_set(Dz, D) or_return
  509. internal_int_gcd(gcd, a, Dz) or_return
  510. /*
  511. If 1 < GCD < `N` then `N` is composite with factor "D", and
  512. Jacobi(D, N) is technically undefined (but often returned as zero).
  513. */
  514. if internal_gt(gcd, 1) && internal_lt(gcd, a) { return }
  515. if Ds < 0 { Dz.sign = .Negative }
  516. j := internal_int_kronecker(Dz, a) or_return
  517. if j == -1 { break }
  518. D += 2
  519. if D > max(int) - 2 { return false, .Invalid_Argument }
  520. }
  521. Q := (1 - Ds) / 4 /* Required so D = P*P - 4*Q */
  522. /*
  523. NOTE: The conditions (a) N does not divide Q, and
  524. (b) D is square-free or not a perfect square, are included by
  525. some authors; e.g., "Prime numbers and computer methods for
  526. factorization," Hans Riesel (2nd ed., 1994, Birkhauser, Boston),
  527. p. 130. For this particular application of Lucas sequences,
  528. these conditions were found to be immaterial.
  529. */
  530. /*
  531. Now calculate N - Jacobi(D,N) = N + 1 (even), and calculate the
  532. odd positive integer d and positive integer s for which
  533. N + 1 = 2^s*d (similar to the step for N - 1 in Miller's test).
  534. The strong Lucas-Selfridge test then returns N as a strong
  535. Lucas probable prime (slprp) if any of the following
  536. conditions is met: U_d=0, V_d=0, V_2d=0, V_4d=0, V_8d=0,
  537. V_16d=0, ..., etc., ending with V_{2^(s-1)*d}=V_{(N+1)/2}=0
  538. (all equalities mod N). Thus d is the highest index of U that
  539. must be computed (since V_2m is independent of U), compared
  540. to U_{N+1} for the standard Lucas-Selfridge test; and no
  541. index of V beyond (N+1)/2 is required, just as in the
  542. standard Lucas-Selfridge test. However, the quantity Q^d must
  543. be computed for use (if necessary) in the latter stages of
  544. the test. The result is that the strong Lucas-Selfridge test
  545. has a running time only slightly greater (order of 10 %) than
  546. that of the standard Lucas-Selfridge test, while producing
  547. only (roughly) 30 % as many pseudoprimes (and every strong
  548. Lucas pseudoprime is also a standard Lucas pseudoprime). Thus
  549. the evidence indicates that the strong Lucas-Selfridge test is
  550. more effective than the standard Lucas-Selfridge test, and a
  551. Baillie-PSW test based on the strong Lucas-Selfridge test
  552. should be more reliable.
  553. */
  554. internal_add(Np1, a, 1) or_return
  555. s := internal_count_lsb(Np1) or_return
  556. /*
  557. This should round towards zero because Thomas R. Nicely used GMP's mpz_tdiv_q_2exp()
  558. and mp_div_2d() is equivalent. Additionally: dividing an even number by two does not produce
  559. any leftovers.
  560. */
  561. internal_int_shr(Dz, Np1, s) or_return
  562. /*
  563. We must now compute U_d and V_d. Since d is odd, the accumulated
  564. values U and V are initialized to U_1 and V_1 (if the target
  565. index were even, U and V would be initialized instead to U_0=0
  566. and V_0=2). The values of U_2m and V_2m are also initialized to
  567. U_1 and V_1; the FOR loop calculates in succession U_2 and V_2,
  568. U_4 and V_4, U_8 and V_8, etc. If the corresponding bits
  569. (1, 2, 3, ...) of t are on (the zero bit having been accounted
  570. for in the initialization of U and V), these values are then
  571. combined with the previous totals for U and V, using the
  572. composition formulas for addition of indices.
  573. */
  574. internal_set(Uz, 1) or_return
  575. internal_set(Vz, 1) or_return // P := 1; /* Selfridge's choice */
  576. internal_set(U2mz, 1) or_return
  577. internal_set(V2mz, 1) or_return // P := 1; /* Selfridge's choice */
  578. internal_set(Qmz, Q) or_return
  579. internal_int_shl1(Q2mz, Qmz) or_return
  580. /*
  581. Initializes calculation of Q^d.
  582. */
  583. internal_set(Qkdz, Q) or_return
  584. Nbits := internal_count_bits(Dz)
  585. for u := 1; u < Nbits; u += 1 { /* zero bit off, already accounted for */
  586. /*
  587. Formulas for doubling of indices (carried out mod N). Note that
  588. the indices denoted as "2m" are actually powers of 2, specifically
  589. 2^(ul-1) beginning each loop and 2^ul ending each loop.
  590. U_2m = U_m*V_m
  591. V_2m = V_m*V_m - 2*Q^m
  592. */
  593. internal_mul(U2mz, U2mz, V2mz) or_return
  594. internal_mod(U2mz, U2mz, a) or_return
  595. internal_sqr(V2mz, V2mz) or_return
  596. internal_sub(V2mz, V2mz, Q2mz) or_return
  597. internal_mod(V2mz, V2mz, a) or_return
  598. /*
  599. Must calculate powers of Q for use in V_2m, also for Q^d later.
  600. */
  601. internal_sqr(Qmz, Qmz) or_return
  602. /* Prevents overflow. Still necessary without a fixed prealloc'd mem.? */
  603. internal_mod(Qmz, Qmz, a) or_return
  604. internal_int_shl1(Q2mz, Qmz) or_return
  605. if internal_int_bitfield_extract_bool(Dz, u) or_return {
  606. /*
  607. Formulas for addition of indices (carried out mod N);
  608. U_(m+n) = (U_m*V_n + U_n*V_m)/2
  609. V_(m+n) = (V_m*V_n + D*U_m*U_n)/2
  610. Be careful with division by 2 (mod N)!
  611. */
  612. internal_mul(T1z, U2mz, Vz) or_return
  613. internal_mul(T2z, Uz, V2mz) or_return
  614. internal_mul(T3z, V2mz, Vz) or_return
  615. internal_mul(T4z, U2mz, Uz) or_return
  616. internal_mul(T4z, T4z, Ds) or_return
  617. internal_add(Uz, T1z, T2z) or_return
  618. if internal_is_odd(Uz) {
  619. internal_add(Uz, Uz, a) or_return
  620. }
  621. /*
  622. This should round towards negative infinity because Thomas R. Nicely used GMP's mpz_fdiv_q_2exp().
  623. But `internal_shr1` does not do so, it is truncating instead.
  624. */
  625. oddness := internal_is_odd(Uz)
  626. internal_int_shr1(Uz, Uz) or_return
  627. if internal_is_negative(Uz) && oddness {
  628. internal_sub(Uz, Uz, 1) or_return
  629. }
  630. internal_add(Vz, T3z, T4z) or_return
  631. if internal_is_odd(Vz) {
  632. internal_add(Vz, Vz, a) or_return
  633. }
  634. oddness = internal_is_odd(Vz)
  635. internal_int_shr1(Vz, Vz) or_return
  636. if internal_is_negative(Vz) && oddness {
  637. internal_sub(Vz, Vz, 1) or_return
  638. }
  639. internal_mod(Uz, Uz, a) or_return
  640. internal_mod(Vz, Vz, a) or_return
  641. /* Calculating Q^d for later use */
  642. internal_mul(Qkdz, Qkdz, Qmz) or_return
  643. internal_mod(Qkdz, Qkdz, a) or_return
  644. }
  645. }
  646. /*
  647. If U_d or V_d is congruent to 0 mod N, then N is a prime or a strong Lucas pseudoprime. */
  648. if internal_is_zero(Uz) || internal_is_zero(Vz) {
  649. return true, nil
  650. }
  651. /*
  652. NOTE: Ribenboim ("The new book of prime number records," 3rd ed.,
  653. 1995/6) omits the condition V0 on p.142, but includes it on
  654. p. 130. The condition is NECESSARY; otherwise the test will
  655. return false negatives---e.g., the primes 29 and 2000029 will be
  656. returned as composite.
  657. */
  658. /*
  659. Otherwise, we must compute V_2d, V_4d, V_8d, ..., V_{2^(s-1)*d}
  660. by repeated use of the formula V_2m = V_m*V_m - 2*Q^m. If any of
  661. these are congruent to 0 mod N, then N is a prime or a strong
  662. Lucas pseudoprime.
  663. */
  664. /* Initialize 2*Q^(d*2^r) for V_2m */
  665. internal_int_shr1(Q2kdz, Qkdz) or_return
  666. for r := 1; r < s; r += 1 {
  667. internal_sqr(Vz, Vz) or_return
  668. internal_sub(Vz, Vz, Q2kdz) or_return
  669. internal_mod(Vz, Vz, a) or_return
  670. if internal_is_zero(Vz) {
  671. return true, nil
  672. }
  673. /* Calculate Q^{d*2^r} for next r (final iteration irrelevant). */
  674. if r < (s - 1) {
  675. internal_sqr(Qkdz, Qkdz) or_return
  676. internal_mod(Qkdz, Qkdz, a) or_return
  677. internal_int_shl1(Q2kdz, Qkdz) or_return
  678. }
  679. }
  680. return false, nil
  681. }
  682. /*
  683. Performs one Fermat test.
  684. If "a" were prime then b**a == b (mod a) since the order of
  685. the multiplicative sub-group would be phi(a) = a-1. That means
  686. it would be the same as b**(a mod (a-1)) == b**1 == b (mod a).
  687. Returns `true` if the congruence holds, or `false` otherwise.
  688. Assumes `a` and `b` not to be `nil` and to have been initialized.
  689. */
  690. internal_prime_fermat :: proc(a, b: ^Int, allocator := context.allocator) -> (fermat: bool, err: Error) {
  691. t := &Int{}
  692. defer internal_destroy(t)
  693. /*
  694. Ensure `b` > 1.
  695. */
  696. if !internal_gt(b, 1) { return false, .Invalid_Argument }
  697. /*
  698. Compute `t` = `b`**`a` mod `a`
  699. */
  700. internal_int_exponent_mod(t, b, a, a) or_return
  701. /*
  702. Is it equal to b?
  703. */
  704. fermat = internal_eq(t, b)
  705. return
  706. }
  707. /*
  708. Tonelli-Shanks algorithm
  709. https://en.wikipedia.org/wiki/Tonelli%E2%80%93Shanks_algorithm
  710. https://gmplib.org/list-archives/gmp-discuss/2013-April/005300.html
  711. */
  712. internal_int_sqrtmod_prime :: proc(res, n, prime: ^Int, allocator := context.allocator) -> (err: Error) {
  713. context.allocator = allocator
  714. /*
  715. The type is "int" because of the types in the mp_int struct.
  716. Don't forget to change them here when you change them there!
  717. */
  718. S, M, i: int
  719. t1, C, Q, Z, T, R, two := &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}
  720. defer internal_destroy(t1, C, Q, Z, T, R, two)
  721. /*
  722. First handle the simple cases.
  723. */
  724. if internal_is_zero(n) { return internal_zero(res) }
  725. /*
  726. "prime" must be odd and > 2
  727. */
  728. if internal_is_even(prime) || internal_lt(prime, 3) { return .Invalid_Argument }
  729. legendre := internal_int_kronecker(n, prime) or_return
  730. /*
  731. n \not\cong 0 (mod p) and n \cong r^2 (mod p) for some r \in N^+
  732. */
  733. if legendre != 1 { return .Invalid_Argument }
  734. internal_init_multi(t1, C, Q, Z, T, R, two) or_return
  735. /*
  736. SPECIAL CASE: if prime mod 4 == 3
  737. compute directly: err = n^(prime+1)/4 mod prime
  738. Handbook of Applied Cryptography algorithm 3.36
  739. x%4 == x&3 for x in N and x>0
  740. */
  741. if prime.digit[0] & 3 == 3 {
  742. internal_add(t1, prime, 1) or_return
  743. internal_shr(t1, t1, 2) or_return
  744. internal_int_exponent_mod(res, n, t1, prime) or_return
  745. return
  746. }
  747. /*
  748. NOW: Tonelli-Shanks algorithm
  749. Factor out powers of 2 from prime-1, defining Q and S as: prime-1 = Q*2^S
  750. Q = prime - 1
  751. */
  752. internal_copy(Q, prime) or_return
  753. internal_sub(Q, Q, 1) or_return
  754. /*
  755. S = 0
  756. */
  757. S = 0
  758. for internal_is_even(Q) {
  759. /*
  760. Q = Q / 2
  761. */
  762. internal_int_shr1(Q, Q) or_return
  763. /*
  764. S = S + 1
  765. */
  766. S += 1
  767. }
  768. /*
  769. Find a `Z` such that the Legendre symbol (Z|prime) == -1.
  770. Z = 2.
  771. */
  772. internal_set(Z, 2) or_return
  773. for {
  774. legendre = internal_int_kronecker(Z, prime) or_return
  775. /*
  776. If "prime" (p) is an odd prime Jacobi(k|p) = 0 for k \cong 0 (mod p)
  777. but there is at least one non-quadratic residue before k>=p if p is an odd prime.
  778. */
  779. if legendre == 0 { return .Invalid_Argument }
  780. if legendre == -1 { break }
  781. /*
  782. Z = Z + 1
  783. */
  784. internal_add(Z, Z, 1) or_return
  785. }
  786. /*
  787. C = Z ^ Q mod prime
  788. */
  789. internal_int_exponent_mod(C, Z, Q, prime) or_return
  790. /*
  791. t1 = (Q + 1) / 2
  792. */
  793. internal_add(t1, Q, 1) or_return
  794. internal_int_shr1(t1, t1) or_return
  795. /*
  796. R = n ^ ((Q + 1) / 2) mod prime
  797. */
  798. internal_int_exponent_mod(R, n, t1, prime) or_return
  799. /*
  800. T = n ^ Q mod prime
  801. */
  802. internal_int_exponent_mod(T, n, Q, prime) or_return
  803. /*
  804. M = S
  805. */
  806. M = S
  807. internal_set(two, 2)
  808. for {
  809. internal_copy(t1, T) or_return
  810. i = 0
  811. for {
  812. if internal_eq(T, 1) { break }
  813. /*
  814. No exponent in the range 0 < i < M found.
  815. (M is at least 1 in the first round because "prime" > 2)
  816. */
  817. if M == i { return .Invalid_Argument }
  818. internal_int_exponent_mod(t1, t1, two, prime) or_return
  819. i += 1
  820. }
  821. if i == 0 {
  822. internal_copy(res, R) or_return
  823. }
  824. /*
  825. t1 = 2 ^ (M - i - 1)
  826. */
  827. internal_set(t1, M - i - 1) or_return
  828. internal_int_exponent_mod(t1, two, t1, prime) or_return
  829. /*
  830. t1 = C ^ (2 ^ (M - i - 1)) mod prime
  831. */
  832. internal_int_exponent_mod(t1, C, t1, prime) or_return
  833. /*
  834. C = (t1 * t1) mod prime
  835. */
  836. internal_sqrmod(C, t1, prime) or_return
  837. /*
  838. R = (R * t1) mod prime
  839. */
  840. internal_mulmod(R, R, t1, prime) or_return
  841. /*
  842. T = (T * C) mod prime
  843. */
  844. mulmod(T, T, C, prime) or_return
  845. /*
  846. M = i
  847. */
  848. M = i
  849. }
  850. return
  851. }
  852. /*
  853. Finds the next prime after the number `a` using `t` trials of Miller-Rabin,
  854. in place: It sets `a` to the prime found.
  855. `bbs_style` = true means the prime must be congruent to 3 mod 4
  856. */
  857. internal_int_prime_next_prime :: proc(a: ^Int, trials: int, bbs_style: bool, allocator := context.allocator) -> (err: Error) {
  858. context.allocator = allocator
  859. res_tab := [_PRIME_TAB_SIZE]DIGIT{}
  860. /*
  861. Force positive.
  862. */
  863. a.sign = .Zero_or_Positive
  864. /*
  865. Simple algo if `a` is less than the largest prime in the table.
  866. */
  867. if internal_lt(a, _private_prime_table[_PRIME_TAB_SIZE - 1]) {
  868. /*
  869. Find which prime it is bigger than `a`
  870. */
  871. for p in _private_prime_table {
  872. cmp := internal_cmp(a, p)
  873. if cmp == 0 { continue }
  874. if cmp != 1 {
  875. if bbs_style && (p & 3 != 3) {
  876. /*
  877. Try again until we get a prime congruent to 3 mod 4.
  878. */
  879. continue
  880. } else {
  881. return internal_set(a, p)
  882. }
  883. }
  884. }
  885. /*
  886. Fall through to the sieve.
  887. */
  888. }
  889. /*
  890. Generate a prime congruent to 3 mod 4 or 1/3 mod 4?
  891. */
  892. kstep: DIGIT = 4 if bbs_style else 2
  893. /*
  894. At this point we will use a combination of a sieve and Miller-Rabin.
  895. */
  896. if bbs_style {
  897. /*
  898. If `a` mod 4 != 3 subtract the correct value to make it so.
  899. */
  900. if a.digit[0] & 3 != 3 {
  901. internal_sub(a, a, (a.digit[0] & 3) + 1) or_return
  902. }
  903. } else {
  904. if internal_is_even(a) {
  905. /*
  906. Force odd.
  907. */
  908. internal_sub(a, a, 1) or_return
  909. }
  910. }
  911. /*
  912. Generate the restable.
  913. */
  914. for x := 1; x < _PRIME_TAB_SIZE; x += 1 {
  915. res_tab = cast(type_of(res_tab))(internal_mod(a, _private_prime_table[x]) or_return)
  916. }
  917. for {
  918. step := DIGIT(0)
  919. y: bool
  920. /*
  921. Skip to the next non-trivially divisible candidate.
  922. */
  923. for {
  924. /*
  925. y == true if any residue was zero [e.g. cannot be prime]
  926. */
  927. y = false
  928. /*
  929. Increase step to next candidate.
  930. */
  931. step += kstep
  932. /*
  933. Compute the new residue without using division.
  934. */
  935. for x := 1; x < _PRIME_TAB_SIZE; x += 1 {
  936. /*
  937. Add the step to each residue.
  938. */
  939. res_tab[x] += kstep
  940. /*
  941. Subtract the modulus [instead of using division].
  942. */
  943. if res_tab[x] >= _private_prime_table[x] {
  944. res_tab[x] -= _private_prime_table[x]
  945. }
  946. /*
  947. Set flag if zero.
  948. */
  949. if res_tab[x] == 0 {
  950. y = true
  951. }
  952. }
  953. if !(y && (step < (((1 << _DIGIT_BITS) - kstep)))) { break }
  954. }
  955. /*
  956. Add the step.
  957. */
  958. internal_add(a, a, step) or_return
  959. /*
  960. If we didn't pass the sieve and step == MP_MAX then skip test */
  961. if y && (step >= ((1 << _DIGIT_BITS) - kstep)) { continue }
  962. if internal_int_is_prime(a, trials) or_return { break }
  963. }
  964. return
  965. }
  966. /*
  967. Makes a truly random prime of a given size (bits),
  968. Flags are as follows:
  969. Blum_Blum_Shub - Make prime congruent to 3 mod 4
  970. Safe - Make sure (p-1)/2 is prime as well (implies .Blum_Blum_Shub)
  971. Second_MSB_On - Make the 2nd highest bit one
  972. This is possibly the mother of all prime generation functions, muahahahahaha!
  973. */
  974. internal_random_prime :: proc(a: ^Int, size_in_bits: int, trials: int, flags := Primality_Flags{}, r: ^rnd.Rand = nil, allocator := context.allocator) -> (err: Error) {
  975. context.allocator = allocator
  976. flags := flags
  977. trials := trials
  978. t := &Int{}
  979. defer internal_destroy(t)
  980. /*
  981. Sanity check the input.
  982. */
  983. if size_in_bits <= 1 || trials < -1 { return .Invalid_Argument }
  984. /*
  985. `.Safe` implies `.Blum_Blum_Shub`.
  986. */
  987. if .Safe in flags {
  988. if size_in_bits < 3 {
  989. /*
  990. The smallest safe prime is 5, which takes 3 bits.
  991. We early out now, else we'd be locked in an infinite loop trying to generate a 2-bit Safe Prime.
  992. */
  993. return .Invalid_Argument
  994. }
  995. flags += { .Blum_Blum_Shub, }
  996. }
  997. /*
  998. Automatically choose the number of Rabin-Miller trials?
  999. */
  1000. if trials == -1 {
  1001. trials = number_of_rabin_miller_trials(size_in_bits)
  1002. }
  1003. RANDOM_PRIME_ITERATIONS_USED = 0
  1004. for {
  1005. if MAX_ITERATIONS_RANDOM_PRIME > 0 {
  1006. RANDOM_PRIME_ITERATIONS_USED += 1
  1007. if RANDOM_PRIME_ITERATIONS_USED > MAX_ITERATIONS_RANDOM_PRIME {
  1008. return .Max_Iterations_Reached
  1009. }
  1010. }
  1011. internal_int_random(a, size_in_bits) or_return
  1012. /*
  1013. Make sure it's odd.
  1014. */
  1015. if size_in_bits > 2 {
  1016. a.digit[0] |= 1
  1017. } else {
  1018. /*
  1019. A 2-bit prime can be either 2 (0b10) or 3 (0b11).
  1020. So, let's force the top bit to 1 and return early.
  1021. */
  1022. a.digit[0] |= 2
  1023. return nil
  1024. }
  1025. if .Blum_Blum_Shub in flags {
  1026. a.digit[0] |= 3
  1027. }
  1028. if .Second_MSB_On in flags {
  1029. internal_int_bitfield_set_single(a, size_in_bits - 2) or_return
  1030. }
  1031. /*
  1032. Is it prime?
  1033. */
  1034. res := internal_int_is_prime(a, trials) or_return
  1035. if !res {
  1036. continue
  1037. }
  1038. if .Safe in flags {
  1039. /*
  1040. See if (a-1)/2 is prime.
  1041. */
  1042. internal_sub(a, a, 1) or_return
  1043. internal_int_shr1(a, a) or_return
  1044. /*
  1045. Is it prime?
  1046. */
  1047. res = internal_int_is_prime(a, trials) or_return
  1048. }
  1049. if res {
  1050. break
  1051. }
  1052. }
  1053. if .Safe in flags {
  1054. /*
  1055. Restore a to the original value.
  1056. */
  1057. internal_int_shl1(a, a) or_return
  1058. internal_add(a, a, 1) or_return
  1059. }
  1060. return
  1061. }
  1062. /*
  1063. Extended Euclidean algorithm of (a, b) produces `a * u1` + `b * u2` = `u3`.
  1064. */
  1065. internal_int_extended_euclidean :: proc(a, b, U1, U2, U3: ^Int, allocator := context.allocator) -> (err: Error) {
  1066. context.allocator = allocator
  1067. u1, u2, u3, v1, v2, v3, t1, t2, t3, q, tmp := &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{}
  1068. defer internal_destroy(u1, u2, u3, v1, v2, v3, t1, t2, t3, q, tmp)
  1069. internal_init_multi(u1, u2, u3, v1, v2, v3, t1, t2, t3, q, tmp) or_return
  1070. /*
  1071. Initialize, (u1, u2, u3) = (1, 0, a).
  1072. */
  1073. internal_set(u1, 1) or_return
  1074. internal_set(u3, a) or_return
  1075. /*
  1076. Initialize, (v1, v2, v3) = (0, 1, b).
  1077. */
  1078. internal_set(v2, 1) or_return
  1079. internal_set(v3, b) or_return
  1080. /*
  1081. Loop while v3 != 0
  1082. */
  1083. for !internal_is_zero(v3) {
  1084. /*
  1085. q = u3 / v3
  1086. */
  1087. internal_div(q, u3, v3) or_return
  1088. /*
  1089. (t1, t2, t3) = (u1, u2, u3) - (v1, v2, v3)q
  1090. */
  1091. internal_mul(tmp, v1, q) or_return
  1092. internal_sub( t1, u1, tmp) or_return
  1093. internal_mul(tmp, v2, q) or_return
  1094. internal_sub( t2, u2, tmp) or_return
  1095. internal_mul(tmp, v3, q) or_return
  1096. internal_sub( t3, u3, tmp) or_return
  1097. /*
  1098. (u1, u2, u3) = (v1, v2, v3)
  1099. */
  1100. internal_set(u1, v1) or_return
  1101. internal_set(u2, v2) or_return
  1102. internal_set(u3, v3) or_return
  1103. /*
  1104. (v1, v2, v3) = (t1, t2, t3)
  1105. */
  1106. internal_set(v1, t1) or_return
  1107. internal_set(v2, t2) or_return
  1108. internal_set(v3, t3) or_return
  1109. }
  1110. /*
  1111. Make sure U3 >= 0.
  1112. */
  1113. if internal_is_negative(u3) {
  1114. internal_neg(u1, u1) or_return
  1115. internal_neg(u2, u2) or_return
  1116. internal_neg(u3, u3) or_return
  1117. }
  1118. /*
  1119. Copy result out.
  1120. */
  1121. if U1 != nil {
  1122. internal_swap(u1, U1)
  1123. }
  1124. if U2 != nil {
  1125. internal_swap(u2, U2)
  1126. }
  1127. if U3 != nil {
  1128. internal_swap(u3, U3)
  1129. }
  1130. return
  1131. }
  1132. /*
  1133. Returns the number of Rabin-Miller trials needed for a given bit size.
  1134. */
  1135. number_of_rabin_miller_trials :: proc(bit_size: int) -> (number_of_trials: int) {
  1136. switch {
  1137. case bit_size <= 80:
  1138. return -1 /* Use deterministic algorithm for size <= 80 bits */
  1139. case bit_size >= 81 && bit_size < 96:
  1140. return 37 /* max. error = 2^(-96) */
  1141. case bit_size >= 96 && bit_size < 128:
  1142. return 32 /* max. error = 2^(-96) */
  1143. case bit_size >= 128 && bit_size < 160:
  1144. return 40 /* max. error = 2^(-112) */
  1145. case bit_size >= 160 && bit_size < 256:
  1146. return 35 /* max. error = 2^(-112) */
  1147. case bit_size >= 256 && bit_size < 384:
  1148. return 27 /* max. error = 2^(-128) */
  1149. case bit_size >= 384 && bit_size < 512:
  1150. return 16 /* max. error = 2^(-128) */
  1151. case bit_size >= 512 && bit_size < 768:
  1152. return 18 /* max. error = 2^(-160) */
  1153. case bit_size >= 768 && bit_size < 896:
  1154. return 11 /* max. error = 2^(-160) */
  1155. case bit_size >= 896 && bit_size < 1_024:
  1156. return 10 /* max. error = 2^(-160) */
  1157. case bit_size >= 1_024 && bit_size < 1_536:
  1158. return 12 /* max. error = 2^(-192) */
  1159. case bit_size >= 1_536 && bit_size < 2_048:
  1160. return 8 /* max. error = 2^(-192) */
  1161. case bit_size >= 2_048 && bit_size < 3_072:
  1162. return 6 /* max. error = 2^(-192) */
  1163. case bit_size >= 3_072 && bit_size < 4_096:
  1164. return 4 /* max. error = 2^(-192) */
  1165. case bit_size >= 4_096 && bit_size < 5_120:
  1166. return 5 /* max. error = 2^(-256) */
  1167. case bit_size >= 5_120 && bit_size < 6_144:
  1168. return 4 /* max. error = 2^(-256) */
  1169. case bit_size >= 6_144 && bit_size < 8_192:
  1170. return 4 /* max. error = 2^(-256) */
  1171. case bit_size >= 8_192 && bit_size < 9_216:
  1172. return 3 /* max. error = 2^(-256) */
  1173. case bit_size >= 9_216 && bit_size < 10_240:
  1174. return 3 /* max. error = 2^(-256) */
  1175. case:
  1176. return 2 /* For keysizes bigger than 10_240 use always at least 2 Rounds */
  1177. }
  1178. }