nim-lang / bigints

BigInts for Nim
MIT License
124 stars 32 forks source link

Add probabilistic and deterministic primality tests #78

Closed dlesnoff closed 2 years ago

dlesnoff commented 2 years ago

Miller-Rabin test is a polynomial probabilistic test to determine wheter an integer is prime. It is way faster than deterministic algorithm (even if they are polynomial) primality test.

Here are some implementations of what I would like : Miller-Rabin test in Nim, by @mratsim : https://github.com/mratsim/nim-project-euler/blob/master/src/lib/primes.nim GMP optimized version : https://gmplib.org/repo/gmp/file/tip/mpz/millerrabin.c

A primality test can then lead to algorithms for factoring integers, finding the next prime, improve prime sieving … Another question this issue raises, is what do we want from this library to do ? Should it also be an arithmetic library ? Since the limbs attribute of BigInt type is private, it might be complex to make another arithmetic library on top of this one (less optimizations on big integers due to less access to the internal representation).

mratsim commented 2 years ago

Since the limbs attribute of BigInt type is private, it might be complex to make another arithmetic library on top of this one (less optimizations on big integers due to less access to the internal representation).

I don't think there are low-level optimizations in a number-theory that require exposing the internal backend. All optimizations should be captured in:

For low-level optimizations, most of them are implemented in Constantine, though it deals with fixed size unsigned integers which are way simpler https://github.com/mratsim/constantine/tree/7d29cb9/constantine/math/arithmetic

konsumlamm commented 2 years ago

I'm inclined to say that primality tests are out of scope for this library. I think it should only offer the basic common bigint functions, as it is the official Nim bigints library.

dlesnoff commented 2 years ago

Feel free to close this issue. I agree with you, a gmp wrapper should be used for all the arithmetic purposes I had in mind.

konsumlamm commented 2 years ago

I'm not able to close issues, but you as the author of this issue can.