Due Date: Feb 18, before midnight (11:59:59PM)

This assignment comprises both written questions and implementation-based lab.


Answer the following questions from the DPV book i) Q1.17, ii) Q1.19, iii) Q 1.23, and iv) given that $x^{86} \equiv 6 \mod 29$, find $x$.

Lab3: Primality Testing

You will compare the two randomized primality testing algorithms -- the basic one in Fig 1.8, and the Miller-Rabin algorithm. Both these methods require modular exponentiation, so you must also implement the modexp algorithm in Fig 1.4.

Implement the randomized primality algorithm in Fig 1.8 in the book.

Next, implement the Miller-Rabin primality test, which is informally described in the gray box just before sec 1.3.1 in the book, and whose pseudo-code is given below:

MillerRabin(N, K):
    Pick positive intgers a1, a2, ..., aK < N at random
    Find u, t, such that N-1 = u * 2^t
    for i = 1, 2, ..., K
        z = ai^u mod N
        if z = 1 mod N 
            mark that ai passes the test
            # repeated squaring, keep track of previous value of z,
            # find first occurrence of 1 if any
            for j = 1, 2, .., t
                compute z^2 mod N
                # ai passes the test iff squared value is 1 and prev value is N-1
                # else ai fails the test
            #ai also fails the test if no 1 is encountered above
    if all a1, a2, ..., aK pass the test
        return yes
        return no

Carmichael numbers

Test your code on the following Carmichael numbers:

Carmichael = [

Except for 561, these numbers are difficult for the basic primality testing code, but will be marked as non-prime by MillerRabin for reasonable values of $K$. On the contrary, some numbers like 999987515379253441 are still incorrectly marked as prime by the basic test, even for $K=1000$!

Your script should test all of the numbers above, except for the last one, which will cause "maximum recursion exceed" error for the recursive version of modexp. Your script should print whether the given Carmichael number is prime or not, and it must also print the fraction of choices of $a_i$ (out of the $K$) that pass Fermat's test for each method. Try different value of $K$, such as $K=10, 20, 50, 100, 1000$.

Bonus: To run the last number in the list above you'll have to implement a non-recursive version of modexp, which will earn bonus points.


Use submitty to submit exactly one PDF and one .py script. The .py script must be implemented in Python3. You can hand write your solutions to the HW, take pics, or you can type up your answers and convert to PDF.

The PDF file must contain your solutions to the HW, and the output from the python script. The script output should be whether each Carmichael number is prime or not, and the fraction of choices of $a_i$ that pass the test (even if the number is eventually identified as non-prime).

Policy on Academic Honesty

You are free to discuss how to tackle the assignment, but all coding must be your own. Please do not copy or modify code from anyone else, including code on the web. Any students caught violating the academic honesty principle will get an automatic F grade on the course and will be referred to the dean of students for disciplinary action.