Recent

Author Topic: Help implementing Miller-rabin algorithm  (Read 4275 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 19128
  • Glad to be alive.
Re: Help implementing Miller-rabin algorithm
« Reply #15 on: August 07, 2025, 12:59:14 pm »
1. See https://en.wikipedia.org/w/index.php?title=Miller%E2%80%93Rabin_primality_test
That means that given 2,7,61 isprime  in the range ~4.8 billion scores no false positives.
2. The value of k is arbitrary, but a good estimate would be round(ln(range)).(over-values lower ranges a bit)'
This has a direct relation ship with
3. miller-rabin finds prime candidates faster than classic prime solvers. If it finds a prime candidate you can verify with classic prime like with this simple isprime:
Code: Pascal  [Select][+][-]
  1. function IsPrime(n:Uint32):Boolean;inline;
  2. var
  3.   p:Uint32;
  4. begin
  5.   if n = 2 then exit(true);
  6.   if (n < 2) or (n mod 2 = 0) then exit(false);
  7.   p := 3;
  8.   while p * p <= n do
  9.   begin
  10.     if n mod p = 0 then exit(false);
  11.     inc(p,2);
  12.   end;
  13.   result:= true;
  14. end;
Note that everything I wrote is limited to the range of native Uint32. For higher values you need a biginteger library, like the one by Rudy Velthuys.

With the chosen values, miller-rabin has been proven 100% correct for Cardinal (see wiki again);
« Last Edit: August 11, 2025, 08:29:11 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

ad1mt

  • Sr. Member
  • ****
  • Posts: 488
    • Mark Taylor's Home Page
Re: Help implementing Miller-rabin algorithm
« Reply #16 on: August 10, 2025, 07:13:26 pm »
I'm truly amazed at how quickly this algorithm works; I only wish I could understand how it works!

AlexTP

  • Hero Member
  • *****
  • Posts: 2708
    • UVviewsoft
Re: Help implementing Miller-rabin algorithm
« Reply #17 on: August 10, 2025, 07:23:53 pm »
I never seen this algo, but we can easily understand how example works. It tests all ODD (n mod 2=1) numbers (variable p) from 3 to square_root(n). On each step, if n is divided by p, n is NOT prime.

This is school level.
« Last Edit: August 10, 2025, 07:31:47 pm by AlexTP »

Thaddy

  • Hero Member
  • *****
  • Posts: 19128
  • Glad to be alive.
Re: Help implementing Miller-rabin algorithm
« Reply #18 on: August 11, 2025, 02:02:07 pm »
@AlextTP
My last entry is not miller-rabin, but a naive test to see if miller-rabin is correct.
You did not read the whole thread. Miller-rabin is about probabilities, either by determinstic values or by random probing.
The deterministic values come from verification runs and in the wiki entry you will find them.
It is much faster than simple sieves or other naive prime algorithms on big primes.
Note that the current implementation I gave  is hampered by the limits of native unsigned integer types.(Uint64, Uint32)
Ideally you would implement it using a biginteger library. (As I wrote before)
But the algorithm is correct and the example is useable for primes in a range < ~4.7 billion.
With the chosen values it covers the primes in that range 100%

That is "not quite" highschool math.... :D
« Last Edit: August 11, 2025, 03:18:40 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

abouchez

  • Full Member
  • ***
  • Posts: 137
    • Synopse
Re: Help implementing Miller-rabin algorithm
« Reply #19 on: August 11, 2025, 04:14:15 pm »
You have a very fast Miler-rabin algorithm implemented in mORMot RSA.
It has been validated with 2048-bit RSA keys on several platforms.
With a pre-defined list of small known primes, it has very good performance in practice.

Perhaps a bit overkill for your initial request, but worth considering as a working solution.

See https://github.com/synopse/mORMot2/blob/master/src/crypt/mormot.crypt.rsa.pas#L1400

Thaddy

  • Hero Member
  • *****
  • Posts: 19128
  • Glad to be alive.
Re: Help implementing Miller-rabin algorithm
« Reply #20 on: August 11, 2025, 04:45:24 pm »
@abouchez

More undiscovered beauty in mORMot. That is an implementation that does exactly what is needed and I suggested.
KUDOS to the mORMot team.

It also follows the same - correct miller-rabin - algorithm but on a production level for high primes.
I suggest to use my example to study HOW it works in somewhat simple code (it is not by any means "simple") and use the mORMot code in production.
for <~4,7 billion the results of both implementations are the same of course.
I suppose, but did not test, that the mORMot implementation can handle more than just ~4.7 billion.
E.g. it might do primes in the range of > 2 trillion with test a = 2, 3, 5, 7, and 11
« Last Edit: August 11, 2025, 05:07:20 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

ad1mt

  • Sr. Member
  • ****
  • Posts: 488
    • Mark Taylor's Home Page
Re: Help implementing Miller-rabin algorithm
« Reply #21 on: August 12, 2025, 06:51:17 pm »
Thanks for your help with this algorithm.
Found a candidate prime 801 digits long:  1000...0001537

Thaddy

  • Hero Member
  • *****
  • Posts: 19128
  • Glad to be alive.
Re: Help implementing Miller-rabin algorithm
« Reply #22 on: August 12, 2025, 07:35:31 pm »
But do you now know how it....works?  :-X
objects are fine constructs. You can even initialize them with constructors.

ad1mt

  • Sr. Member
  • ****
  • Posts: 488
    • Mark Taylor's Home Page
Re: Help implementing Miller-rabin algorithm
« Reply #23 on: August 14, 2025, 07:15:43 pm »
But do you now know how it....works?  :-X
No... I trusted your code!   :)

Thanks for your help with this... getting this code working with my big integer library found some more bugs, which had escaped my testing until now.

srvaldez

  • Full Member
  • ***
  • Posts: 201
Re: Help implementing Miller-rabin algorithm
« Reply #24 on: August 15, 2025, 12:27:21 am »
hello ad1mt
just in case you want to look at a different implementation of Miller-Rabin prime test, here's one in Basic https://raw.githubusercontent.com/stephanbrunker/big_integer/refs/heads/master/example_bigint_isprime.bas
I changed r = randomstring(128) to r = randomstring(416) and the for loop to 4000
it produced a pseudo prime of 1002 digits in less than a minute
please take no offense at this post, Basic is not Pascal but it's easy to understand and it's only about 270 lines of code
@Thaddy
I think it's basically the same as your code so please don't feel offended

ad1mt

  • Sr. Member
  • ****
  • Posts: 488
    • Mark Taylor's Home Page
Re: Help implementing Miller-rabin algorithm
« Reply #25 on: August 17, 2025, 11:07:16 pm »
@srvaldez... thanks for your suggestion.
please take no offense at this post, Basic is not Pascal
I don't have a problem with Basic. When I first started hobby programming at home in 1981, I used a Basic compiler on a Sinclair Spectrum to write a Draughts playing program!
That program has evolved over the decades and is now written in Free Pascal. I still sometimes work on trying to improve it, but I think I've reached the limits of ability on that project.

srvaldez

  • Full Member
  • ***
  • Posts: 201
Re: Help implementing Miller-rabin algorithm
« Reply #26 on: August 18, 2025, 12:24:22 am »
ad1mt
the reason that I posted that link was to present to you a different way of generating possible random primes
how are you progressing with your code?

 

TinyPortal © 2005-2018