]> gitweb.factorcode.org Git - factor.git/commitdiff
Ensure that random-prime result has the right size
authorSamuel Tardieu <sam@rfc1149.net>
Sat, 29 Aug 2009 19:42:15 +0000 (21:42 +0200)
committerSamuel Tardieu <sam@rfc1149.net>
Sat, 29 Aug 2009 19:42:15 +0000 (21:42 +0200)
As noted by Slava, choosing the next prime following a random number
with a specified number of bits may give a number one more bit long.

basis/math/primes/primes.factor

index 7e877a03ce3f9dfcd91fca9734c73ef0adb78260..27743a4a85780f45c2ee6006ab8da325d83c15b9 100644 (file)
@@ -56,7 +56,8 @@ PRIVATE>
 : coprime? ( a b -- ? ) gcd nip 1 = ; foldable
 
 : random-prime ( numbits -- p )
-    random-bits* next-prime ;
+    [ ] [ 2^ ] [ random-bits* next-prime ] tri
+    2dup < [ 2drop random-prime ] [ 2nip ] if ;
 
 : estimated-primes ( m -- n )
     dup log / ; foldable