CSE 20 DISCRETE MATH Prof. Shachar Lovett http://cseweb.ucsd.edu/classes/wi15/cse20-a/ Clicker frequency: CA Todays topics • Modular arithmetic • Section 6.2 in Jenkyns, Stephenson Recall: equivalence relations • Saw two equivalent definitions • xRy is an equivalence relation if… • Symmetric: ∀π₯, π¦, π₯π π¦ ⇔ π¦π π₯ • Reflexive: ∀π₯, π₯π π₯ • Transitive: ∀π₯, π¦, π§, π₯π π¦ ∧ π¦π π§ → π₯π π§ • Equivalently: there is a partition of the universe to equivalence classes, and xRy iff x,y are in the same class • Can be phrased as: ∃πΆ, πΆπππ π : π → πΆ such that π₯π π¦ ⇔ πΆπππ π π₯ = πΆπππ π (π¦) Today: modular arithmetic • A very important (and very useful) example of an equivalence relation • Recall our example of {even,odd} classes: • π₯π π¦ ⇔ 2| π₯ − π¦ ⇔x,y are both even, or both odd • Modular arithmetic generalizes this from 2 to any divisor Modular arithmetic: definition • Fix π ≥ 2 • Define a relation: integers x,y are equivalent modulo m, if π₯ πππ π = π¦ πππ π • Equivalently, π|π₯ − π¦ • Defines a partition of Z to m equivalence classes π π = {π₯ ∈ π: π₯ πππ π = π} where π ∈ {0,1, … , π − 1} Modular arithmetic: examples • π π = {π₯ ∈ π: π₯ πππ π = π} • What is [0]2? A. All integers B. 0 C. Even numbers D. Odd numbers E. Other Modular arithmetic: examples • π π = {π₯ ∈ π: π₯ πππ π = π} • What is [1]2? A. All integers B. 1 C. Even numbers D. Odd numbers E. Other Modular arithmetic: examples • π π = {π₯ ∈ π: π₯ πππ π = π} • What is [0]3? A. All integers B. All integers divisible by 3 C. All integers whose value mod 3 is equal to 1 D. All integers whose value mod 3 is equal to 2 E. Other Modular arithmetic: examples • π π = {π₯ ∈ π: π₯ πππ π = π} • What is [1]3? A. All integers B. All integers divisible by 3 C. All integers whose value mod 3 is equal to 1 D. All integers whose value mod 3 is equal to 2 E. Other Modular arithmetic: examples • π π = {π₯ ∈ π: π₯ πππ π = π} • What is [4]3? A. All integers B. All integers divisible by 3 C. All integers whose value mod 3 is equal to 1 D. All integers whose value mod 3 is equal to 2 E. Other Modular arithmetic: notation • Notation: to say that π₯ ∈ π π , we typically write π₯ ≡ π πππ π • More generally, for any two integers x,y we can write π₯ ≡ π¦ πππ π which means that x,y belong to the same equivalence class modulo m, which means π|π₯ − π¦. Modular addition and multiplication • Why is modular arithmetic so important? Because we can operate directly on the equivalence classes, adding and multiplying them (for a fixed modulus m) • Definition: for any fixed π ≥ 2, π π + π π = π + π πππ π ∗ π π = π ∗ π πππ π π And π π • Theorem: this is well defined • What does this even mean??? π Modular addition • Theorem: π π + π π = π + π πππ π π • Interpretation: if we take any π₯ ∈ π π , and any π¦ ∈ π π , then it is always true that π₯ + π¦ ∈ π + π πππ π π • Equivalently: π₯ πππ π + π¦ πππ π πππ π = π₯ + π¦ πππ π Modular addition: proof • Theorem: π π + π π = π + π πππ π π • Proof: we need to show ∀π₯ ∈ π π, π¦ ∈ π π, π₯ + π¦ πππ π = π + π πππ π Let π₯ = ππ + π, π¦ = ππ + π and π + π = ππ + π, with π, π, π ∈ {0, … , π − 1}. Then: π₯+π¦ = π+π π+ π+π = π+π+π π+π So π₯ + π¦ ∈ π π, and also a + π ∈ π π. QED. Modular addition: examples • What is 0 2 + 1 2 ? A. B. C. D. E. 02 12 21 All integers Other Modular addition: examples • What is 4 7 + 5 7 ? A. B. C. D. E. 17 27 72 74 Other Modular addition: examples • What is 4 7 + 1 2 ? A. B. C. D. E. 57 5 14 72 1 14 Other Be careful! • We can only add π π + π π if π = π. Otherwise, this is not defined!!! • Also: in the special case of π = 2, addition modulo 2 corresponds to XOR • 0 2+ 0 2= 0 2 • 0 2+ 1 2= 1 2 • 1 2+ 0 2= 1 2 • 1 2+ 1 2= 0 2 Modular multiplication • Theorem: π π ∗ π π = π ∗ π πππ π π Modular multiplication: proof • Theorem: π π ∗ π π = π ∗ π πππ π π • Proof: we need to show ∀π₯ ∈ π π, π¦ ∈ π π, π₯ ∗ π¦ πππ π = π ∗ π πππ π Let π₯ = ππ + π, π¦ = ππ + π and π ∗ π = ππ + π, with π, π, π ∈ {0, … , π − 1}. Then: π₯ ∗ π¦ = ππ + π ∗ ππ + π = πππ2 + πππ + πππ + ππ = πππ2 + πππ + πππ + ππ + π = π πππ + ππ + ππ + π + π So π₯ ∗ π¦ ∈ π π, and also a ∗ π ∈ π π. QED. Applications of modular arithmetic • For a fixed π ≥ 2, we can add, multiply and take modulo m, and the value (modulo m) will stay invariant • This is very useful; many algorithmic applications • Example 1: verifying calculations • Prove that 12294793 ∗ 2329373 ≠ 92739273 • Hint: use values modulo 10 Applications of modular arithmetic • Example 1: prove that 12294793 ∗ 2329373 ≠ 92739273 • Solution: compute values modulo 10 • 12294793 ≡ 3 πππ 10 • 2329373 ≡ 3 πππ 10 • 92739273 ≡ 3 πππ 10 • But 3 ∗ 3 ≡ 9 πππ 10 , so the equation cannot be correct Applications of modular arithmetic • Example 2: number theory • Theorem: there are no π₯, π¦ ∈ π such that π₯ 2 + π¦ 2 = 1234567 • Hint: use values modulo 4 Applications of modular arithmetic • Theorem: there are no π₯, π¦ ∈ π such that π₯ 2 + π¦ 2 = 1234567 • Hint: use values modulo 4 • Solution: for any π₯ ∈ π, π₯ 2 ∈ 0 4 or π₯ 2 ∈ 1 4 . • There are 4 cases to check: • 0 4 ∗ 0 4 = 0 4; 1 4 ∗ 1 4 = 1 4; 2 4 ∗ 2 4 = 0 4; 3 4 ∗ 3 4 = 1 4; • So, π₯ 2 + π¦ 2 ππππ’ππ 4 can either be 0 4 , 1 4 or 2 4 • But 1234567 ≡ 3 (πππ 4), so they cannot be equal! Applications of modular arithmetic • Cryptography • Your online transactions are encrypted, based on modular arithmetic • RSA public key crpytosystem: • Very large m, product of two large primes m=p*q • Message: π₯ ∈ {0,1, … , π − 1} • Encryption: π₯ π πππ π • Security is based on the assumption that factorization is hard