From e5897d52b26cc6196e73e41f8c29eb155b14ebcb Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Sat, 29 Aug 2009 21:42:15 +0200 Subject: [PATCH] Ensure that random-prime result has the right size 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 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/basis/math/primes/primes.factor b/basis/math/primes/primes.factor index 7e877a03ce..27743a4a85 100644 --- a/basis/math/primes/primes.factor +++ b/basis/math/primes/primes.factor @@ -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