Blame


1 665c255d 2023-08-04 jrmu ;; Exercise 1.28. One variant of the Fermat test that cannot be fooled is called the Miller-Rabin test (Miller 1976; Rabin 1980). This starts from an alternate form of Fermat's Little Theorem, which states that if n is a prime number and a is any positive integer less than n, then a raised to the (n - 1)st power is congruent to 1 modulo n. To test the primality of a number n by the Miller-Rabin test, we pick a random number a<n and raise a to the (n - 1)st power modulo n using the expmod procedure. However, whenever we perform the squaring step in expmod, we check to see if we have discovered a ``nontrivial square root of 1 modulo n,'' that is, a number not equal to 1 or n - 1 whose square is equal to 1 modulo n. It is possible to prove that if such a nontrivial square root of 1 exists, then n is not prime. It is also possible to prove that if n is an odd number that is not prime, then, for at least half the numbers a<n, computing a^(n-1) in this way will reveal a nontrivial square root of 1 modulo n. (This is why the Miller-Rabin test cannot be fooled.) Modify the expmod procedure to signal if it discovers a nontrivial square root of 1, and use this to implement the Miller-Rabin test with a procedure analogous to fermat-test. Check your procedure by testing various known primes and non-primes. Hint: One convenient way to make expmod signal is to have it return 0.
2 665c255d 2023-08-04 jrmu
3 665c255d 2023-08-04 jrmu ;; calculate base^exp modulo m but return 0 if nontrivial square root of 1 modulo n is discovered
4 665c255d 2023-08-04 jrmu (define (expmod base exp m)
5 665c255d 2023-08-04 jrmu (cond ((= exp 0) 1)
6 665c255d 2023-08-04 jrmu ((even? exp)
7 665c255d 2023-08-04 jrmu (let ((modulo-sqrt (expmod base (/ exp 2) m)))
8 665c255d 2023-08-04 jrmu (let ((modulo-sqr (remainder (square modulo-sqrt) m)))
9 665c255d 2023-08-04 jrmu (if (and (not (or (= modulo-sqrt 1) (= modulo-sqrt (- m 1))))
10 665c255d 2023-08-04 jrmu (= modulo-sqr 1))
11 665c255d 2023-08-04 jrmu 0
12 665c255d 2023-08-04 jrmu modulo-sqr))))
13 665c255d 2023-08-04 jrmu (else (remainder (* base (expmod base (- exp 1) m)) m))))
14 665c255d 2023-08-04 jrmu
15 665c255d 2023-08-04 jrmu (define (miller-rabin-test n)
16 665c255d 2023-08-04 jrmu (define (try-it a)
17 665c255d 2023-08-04 jrmu (= (expmod a (- n 1) n) 1))
18 665c255d 2023-08-04 jrmu (try-it (+ 1 (random (- n 1)))))
19 665c255d 2023-08-04 jrmu
20 665c255d 2023-08-04 jrmu (define (miller-rabin-prime? n times)
21 665c255d 2023-08-04 jrmu (cond ((= times 0) #t)
22 665c255d 2023-08-04 jrmu ((miller-rabin-test n) (miller-rabin-prime? n (- times 1)))
23 665c255d 2023-08-04 jrmu (else #f)))
24 665c255d 2023-08-04 jrmu
25 665c255d 2023-08-04 jrmu (define (test-case actual expected)
26 665c255d 2023-08-04 jrmu (load-option 'format)
27 665c255d 2023-08-04 jrmu (newline)
28 665c255d 2023-08-04 jrmu (format #t "Actual: ~A Expected: ~A" actual expected))
29 665c255d 2023-08-04 jrmu
30 665c255d 2023-08-04 jrmu (define (quick-mr-prime? n)
31 665c255d 2023-08-04 jrmu (miller-rabin-prime? n 2500))
32 665c255d 2023-08-04 jrmu
33 665c255d 2023-08-04 jrmu ;;(test-case (quick-mr-prime? 2) #t)
34 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 3) #t)
35 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 4) #f)
36 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 5) #t)
37 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 6) #f)
38 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 7) #t)
39 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 8) #f)
40 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 9) #f)
41 665c255d 2023-08-04 jrmu
42 665c255d 2023-08-04 jrmu ;; (list-primes 10000)
43 665c255d 2023-08-04 jrmu
44 665c255d 2023-08-04 jrmu ;; Carmichael Numbers
45 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 561) #f)
46 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 1105) #f)
47 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 1729) #f)
48 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 2465) #f)
49 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 2821) #f)
50 665c255d 2023-08-04 jrmu (test-case (quick-mr-prime? 6601) #f)