     n 1 - :> n-1
     n-1 factor-2s :> s :> r
     0 :> a!
-    t :> prime?!
     trials [
+        drop
         n 1 - [1,b] random a!
         a s n ^mod 1 = [
+            f
+        ] [
             r iota [
                 2^ s * a swap n ^mod n - -1 =
-            ] any? not [ f prime?! trials + ] when
-        ] unless drop
-    ] each prime? ;
+            ] any? not 
+        ] if
+    ] any? not ;
 
 PRIVATE>
 

    1 + 6 divisor? ;

: next-safe-prime-candidate ( n -- candidate )
    1 - 2/
    next-prime dup safe-prime-candidate?
    [ next-safe-prime-candidate ] unless ;


    dup miller-rabin
    [ nip ] [ drop next-safe-prime ] if ;

: random-bits* ( numbits -- n )
    [ random-bits ] keep set-bit ;

: random-safe-prime ( numbits -- p )
    1- random-bits* next-safe-prime ;
