As an exercise for myself, I’m implementing the Miller-Rabin test. (Working through SICP). I understand Fermat’s little theorem and was able to successfully implement that. The part that I’m getting tripped up on in the Miller-Rabin test is this “1 mod n” business. Isn’t 1 mod n (n being some random integer) always 1? So I’m confused at what a “nontrivial square root of 1 modulo n” could be since in my mind “1 mod n” is always 1 when dealing with integer values. What am I missing?
As an exercise for myself, I’m implementing the Miller-Rabin test. (Working through SICP). I
Share
1 is congruent to 9 mod 8 so 3 is a non trivial square root of 1 mod 8.
what you are working with is not individual numbers, but equivalence sets.
[m]nis the set of all numbersxsuch thatxis congruent tommodn. Any thing that sqaures to any element of this set is a square root ofmmodulon.given any
n, we have the set of integers modulo n which we can write asZn. this is the set (of sets)[1]n,[2]n, … ,[n]n. Every integer lies in one and only one of those sets. we can define addition and multiplication on this set by[a]n + [b]n = [a + b]nand likewise for multiplication. So a square root of[1]nis a(n element of)[b]nsuch that[b*b]n = [1]n.In practice though, we can conflate
mwith[m]nand normally choose the unique element,m'of[m]nsuch that0 <= m' < nas our ‘representative’ element: this is what we usually think of as them mod n. but it’s important to keep in mind that we are ‘abusing notation’ as the mathematicians say.here’s some (non-idiomatic) python code as I don’t have a scheme interpreter ATM:
So, in particular (looking at the last example), 17 is a root of unity modulo 9. indeed, 17^2 = 289 and 289 % 9 = 1. returning to our previous notation
[8]9 = [17]9and([17]9)^2 = [1]9