prime.odin 39 KB

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