is_bfsw_pseudoprime.sf 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/ruby
  2. # Author: Daniel "Trizen" Șuteu
  3. # Date: 31 October 2023
  4. # https://github.com/trizen
  5. # A new primality test, using only the Lucas V sequence.
  6. # This test is a simplification of the strengthen BPSW test:
  7. # https://arxiv.org/abs/2006.14425
  8. define USE_METHOD_A_STAR = false # true to use the A* method in finding (P,Q)
  9. func check_lucasV(P,Q,n,m) {
  10. var d = n+1
  11. var s = valuation(d, 2)
  12. var(V1=2, V2=P, Q1=1, Q2=1)
  13. for bit in ((d >> (s+1)) -> bits) {
  14. Q1 = mulmod(Q1, Q2, m)
  15. if (bit) {
  16. Q2 = mulmod(Q1, Q, m)
  17. V1 = mulsubmulmod(V2, V1, P, Q1, m)
  18. V2 = mulsubmulmod(V2, V2, 2, Q2, m)
  19. }
  20. else {
  21. Q2 = Q1
  22. V2 = mulsubmulmod(V2, V1, P, Q1, m)
  23. V1 = mulsubmulmod(V1, V1, 2, Q2, m)
  24. }
  25. }
  26. Q1 = mulmod(Q1, Q2, m)
  27. Q2 = mulmod(Q1, Q, m)
  28. V1 = mulsubmulmod(V1, V2, P, Q1, m)
  29. Q2 = mulmod(Q2, Q1, m)
  30. s.times {
  31. V1 = mulsubmulmod(V1, V1, 2, Q2, m)
  32. Q2 = mulmod(Q2, Q2, m)
  33. }
  34. V1.is_congruent(2*Q, m) || return false
  35. Q2.is_congruent(Q*Q, m) || return false
  36. return true
  37. }
  38. func findQ(N) {
  39. for k in (2 .. Inf) {
  40. var D = ((-1)**k * (2*k + 1))
  41. var K = kronecker(D, N)
  42. if (K.is_zero && gcd(D, N).is_between(2, N-1)) {
  43. return nil
  44. }
  45. elsif ((k == 20) && N.is_square) {
  46. return nil
  47. }
  48. return ((1-D)/4) if (K == -1)
  49. }
  50. }
  51. func findP (N, Q) {
  52. for P in (2..Inf) {
  53. var D = (P*P - 4*Q)
  54. var K = kronecker(D, N)
  55. if (K == -1) {
  56. return P
  57. }
  58. elsif (K.is_zero && gcd(D, N).is_between(2, N-1)) {
  59. return nil
  60. }
  61. elsif ((P == 20) && N.is_square) {
  62. return nil
  63. }
  64. }
  65. }
  66. func is_bfsw_psp(n) {
  67. n <= 1 && return false
  68. n == 2 && return true
  69. n.is_even && return false
  70. var(P,Q)
  71. if (USE_METHOD_A_STAR) {
  72. P = 1
  73. Q = findQ(n) \\ return false
  74. if (Q == -1) {
  75. P = 5
  76. Q = 5
  77. }
  78. }
  79. else { # this is faster
  80. Q = -2
  81. P = findP(n, Q) \\ return false
  82. }
  83. check_lucasV(P,Q,n,n)
  84. }
  85. say 25.by(is_bfsw_psp)
  86. assert([913, 150267335403, 430558874533, 14760229232131, 936916995253453].none(is_bfsw_psp))
  87. for n in (1..1e3) {
  88. if (is_bfsw_psp(n)) {
  89. if (!n.is_prime) {
  90. say "Counter-example: #{n}"
  91. }
  92. }
  93. elsif (n.is_prime) {
  94. say "Missed-prime: #{n}"
  95. }
  96. }
  97. __END__
  98. Inspired by the paper "Strengthening the Baillie-PSW primality test", I propose a simplified test based on Lucas V-pseudoprimes, that requires computing only the Lucas V sequence, making it faster than the full BPSW test, while being about as strong.
  99. The first observation was that none of the 5 vpsp terms < 10^15 satisfy:
  100. Q^(n+1) == Q^2 (mod n)
  101. This gives us a simple test:
  102. V_{n+1}(P,Q) == 2*Q (mod n)
  103. Q^(n+1) == Q^2 (mod n)
  104. where (P,Q) are selected using Method A*.