CSE 311 Lecture 11: Modular Arithmetic. Emina Torlak and Kevin Zatloukal

Similar documents
CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic

cse 311: foundations of computing Fall 2015 Lecture 11: Modular arithmetic and applications

CSE 311 Lecture 13: Primes and GCD. Emina Torlak and Kevin Zatloukal

cse 311: foundations of computing Fall 2015 Lecture 12: Primes, GCD, applications

Number theory (Chapter 4)

Math.3336: Discrete Mathematics. Primes and Greatest Common Divisors

CS March 17, 2009

CSE 311: Foundations of Computing. Lecture 12: Two s Complement, Primes, GCD

10 Modular Arithmetic and Cryptography

Encryption: The RSA Public Key Cipher

Public Key Cryptography

MATH 433 Applied Algebra Lecture 4: Modular arithmetic (continued). Linear congruences.

cse 311: foundations of computing Spring 2015 Lecture 12: Primes, GCD, applications

Carmen s Core Concepts (Math 135)

Mathematics of Cryptography

Discrete Mathematics GCD, LCM, RSA Algorithm

Course 2BA1: Trinity 2006 Section 9: Introduction to Number Theory and Cryptography

Course MA2C02, Hilary Term 2013 Section 9: Introduction to Number Theory and Cryptography

Review. CS311H: Discrete Mathematics. Number Theory. Computing GCDs. Insight Behind Euclid s Algorithm. Using this Theorem. Euclidian Algorithm

Integers and Division

1 Recommended Reading 1. 2 Public Key/Private Key Cryptography Overview RSA Algorithm... 2

CS483 Design and Analysis of Algorithms

Cryptography and Number Theory

Final Exam Math 105: Topics in Mathematics Cryptology, the Science of Secret Writing Rhodes College Tuesday, 30 April :30 11:00 a.m.

Mathematical Foundations of Public-Key Cryptography

Lecture 5: Arithmetic Modulo m, Primes and Greatest Common Divisors Lecturer: Lale Özkahya

CPSC 467b: Cryptography and Computer Security

One can use elliptic curves to factor integers, although probably not RSA moduli.

ECE596C: Handout #11

Classical Cryptography

Cosc 412: Cryptography and complexity Lecture 7 (22/8/2018) Knapsacks and attacks

2. Cryptography 2.5. ElGamal cryptosystems and Discrete logarithms

MODULAR ARITHMETIC KEITH CONRAD

Ma/CS 6a Class 2: Congruences

ECE 646 Lecture 5. Mathematical Background: Modular Arithmetic

3 The fundamentals: Algorithms, the integers, and matrices

Introduction to Cryptography k. Lecture 5. Benny Pinkas k. Requirements. Data Integrity, Message Authentication

Public Key Cryptography

Public Key Cryptography. All secret key algorithms & hash algorithms do the same thing but public key algorithms look very different from each other.

CPSC 467b: Cryptography and Computer Security

For your quiz in recitation this week, refer to these exercise generators:

Lecture Notes, Week 6

MEETING 6 - MODULAR ARITHMETIC AND INTRODUCTORY CRYPTOGRAPHY

CSE 20 DISCRETE MATH. Winter

COT 3100 Applications of Discrete Structures Dr. Michael P. Frank

MATH 158 FINAL EXAM 20 DECEMBER 2016

Ma/CS 6a Class 2: Congruences

Part V. Chapter 19. Congruence of integers

CSE 311 Lecture 28: Undecidability of the Halting Problem. Emina Torlak and Kevin Zatloukal

Lecture 19: Public-key Cryptography (Diffie-Hellman Key Exchange & ElGamal Encryption) Public-key Cryptography

Public-key Cryptography and elliptic curves

Notes. Number Theory: Applications. Notes. Number Theory: Applications. Notes. Hash Functions I

The RSA public encryption scheme: How I learned to stop worrying and love buying stuff online

CIS 551 / TCOM 401 Computer and Network Security

COMP Intro to Logic for Computer Scientists. Lecture 15

4 Number Theory and Cryptography

CPSC 467b: Cryptography and Computer Security

U.C. Berkeley CS276: Cryptography Luca Trevisan February 5, Notes for Lecture 6

Solutions to the Midterm Test (March 5, 2011)

Lecture 1: Perfect Secrecy and Statistical Authentication. 2 Introduction - Historical vs Modern Cryptography

Simple Math: Cryptography

CPSC 467: Cryptography and Computer Security

Cryptography. P. Danziger. Transmit...Bob...

Lecture 7: ElGamal and Discrete Logarithms

Linear Congruences. The equation ax = b for a, b R is uniquely solvable if a 0: x = b/a. Want to extend to the linear congruence:

Topics in Cryptography. Lecture 5: Basic Number Theory

Ma/CS 6a Class 1. Course Details

Math.3336: Discrete Mathematics. Mathematical Induction

The set of integers will be denoted by Z = {, -3, -2, -1, 0, 1, 2, 3, 4, }

Introduction to Modern Cryptography. Benny Chor

Intro to Public Key Cryptography Diffie & Hellman Key Exchange

CPSC 467b: Cryptography and Computer Security

Introduction to Number Theory. The study of the integers

PERFECT SECRECY AND ADVERSARIAL INDISTINGUISHABILITY

Slides by Kent Seamons and Tim van der Horst Last Updated: Oct 1, 2013

Adam Blank Spring 2017 CSE 311. Foundations of Computing I. * All slides are a combined effort between previous instructors of the course

Exam Security January 19, :30 11:30

Pseudo-random Number Generation. Qiuliang Tang

Week 7 An Application to Cryptography

Elliptic curves: Theory and Applications. Day 4: The discrete logarithm problem.

Wilson s Theorem and Fermat s Little Theorem

Lecture Notes. Advanced Discrete Structures COT S

CPSC 467: Cryptography and Computer Security

Lecture 1: Introduction to Public key cryptography

Tutorial on Quantum Computing. Vwani P. Roychowdhury. Lecture 1: Introduction

CPSC 467b: Cryptography and Computer Security

Candidates must show on each answer book the type of calculator used. Only calculators permitted under UEA Regulations may be used.

Number Theory: Applications. Number Theory Applications. Hash Functions II. Hash Functions III. Pseudorandom Numbers

Elliptic Curve Cryptography

basics of security/cryptography

CSCI3390-Lecture 16: Probabilistic Algorithms: Number Theory and Cryptography

Introduction to Cryptography. Lecture 6

Notes 10: Public-key cryptography

Introduction to Modern Cryptography. Benny Chor

then the hard copy will not be correct whenever your instructor modifies the assignments.

Lecture 3.1: Public Key Cryptography I

17.1 Binary Codes Normal numbers we use are in base 10, which are called decimal numbers. Each digit can be 10 possible numbers: 0, 1, 2, 9.

Transcription:

CSE 311 Lecture 11: Modular Arithmetic Emina Torlak and Kevin Zatloukal 1

Topics Sets and set operations A quick wrap-up of Lecture 10. Modular arithmetic basics Arithmetic over a finite domain (a.k.a computer arithmetic). Modular arithmetic properties Congruence, addition, multiplication, proofs. Modular arithmetic and integer representations Unsigned, sign-magnitude, and two s complement representation. Applications of modular arithmetic Hashing, pseudo-random numbers, ciphers. 2

Sets and set operations A quick wrap-up of Lecture 10.

3

Sets and set operations A set is a collection of elements. Write a B (or a B) to say that a is (or isn t) an element in the set B. The order of elements doesn t matter, and duplicates don t matter. A B A = B x. x A x B A B A B A B x. x A x B Sets and are equal if they have the same elements. is a subset of if every element of is also in. Sets can be built from predicates or using set operations. A B = {x : (x A) (x B)} A B = {x : (x A) (x B)} A B = {x : (x A) (x B)} A B = {x : (x A) (x B)} A = {x : x A} = {x : (x A)} 4

This is Boolean algebra again A B = {x : (x A) (x B)} A B = {x : (x A) (x B)} A = {x : x A} = {x : (x A)} Union is defined using. Intersection is defined using. Complement works like. This means that all equivalences from Boolean algebra translate directly into set theory, and you can use them in your proofs! 5

More sets Power set, Cartesian product, and Russell s paradox. 6

Power set A (A) = {B : B A} Power set of a set is the set of all subsets of. Examples Let Days = {M, W, F}. (Days) = ( ) = A 7

Power set A (A) = {B : B A} Power set of a set is the set of all subsets of. Examples Let Days = {M, W, F}. (Days) = {, {M}, {W}, {F}, {M, W}, {M, F}, {W, F}, {M, W, F}} ( ) = A 7

Power set A (A) = {B : B A} Power set of a set is the set of all subsets of. Examples Let Days = {M, W, F}. (Days) = {, {M}, {W}, {F}, {M, W}, {M, F}, {W, F}, {M, W, F}} ( ) = { } A 7

Cartesian product The Cartesian product of two sets is the set of all of their ordered pairs. A B = {(a, b) : a A b B} Examples R R Z Z is the real plane. is the set of all pairs of integers. 8

Cartesian product The Cartesian product of two sets is the set of all of their ordered pairs. A B = {(a, b) : a A b B} Examples R R is the real plane. Z Z is the set of all pairs of integers. If A = {1, 2}, B = {a, b, c}, then A B = 8

Cartesian product The Cartesian product of two sets is the set of all of their ordered pairs. A B = {(a, b) : a A b B} Examples R R is the real plane. Z Z is the set of all pairs of integers. If A = {1, 2}, B = {a, b, c}, then. A B = {(1, a), (1, b), (1, c), (2, a), (2, b), (2, c)} 8

Cartesian product The Cartesian product of two sets is the set of all of their ordered pairs. A B = {(a, b) : a A b B} Examples R R is the real plane. Z Z is the set of all pairs of integers. If A = {1, 2}, B = {a, b, c}, then. A B = {(1, a), (1, b), (1, c), (2, a), (2, b), (2, c)} A = 8

Cartesian product The Cartesian product of two sets is the set of all of their ordered pairs. A B = {(a, b) : a A b B} Examples R R is the real plane. Z Z is the set of all pairs of integers. If A = {1, 2}, B = {a, b, c}, then. A B = {(1, a), (1, b), (1, c), (2, a), (2, b), (2, c)} A = {(a, b) : a A b } = {(a, b) : a A F} =. 8

Russell s paradox Let S S = {x : x x} be the set of all sets that don t contain themselves. 9

Russell s paradox Let S S = {x : x x} S be the set of all sets that don t contain themselves. The definition of is contradictory, hence the paradox. 9

Russell s paradox Let S S = {x : x x} S be the set of all sets that don t contain themselves. The definition of is contradictory, hence the paradox. Suppose that S S. Then, by definition of S, S S, which is a contradiction. 9

Russell s paradox Let S S = {x : x x} S be the set of all sets that don t contain themselves. The definition of is contradictory, hence the paradox. Suppose that S S. Then, by definition of S, S S, which is a contradiction. Suppose that S S. Then, by definition of S, S S, which is a contradiction too. 9

Russell s paradox Let S S = {x : x x} S be the set of all sets that don t contain themselves. The definition of is contradictory, hence the paradox. Suppose that S S. Then, by definition of S, S S, which is a contradiction. Suppose that S S. Then, by definition of S, S S, which is a contradiction too. To avoid the paradox Define with respect to a universe of discourse. S S = {x U : x x} S S S U With this definition, and there is no contradiction because. 9

Working with sets Representing sets as bitvectors and applications of bitvectors. 10

Representing sets as bitvectors Suppose that universe is. We can represent every set U {1, 2,, n} B U b 1 b 2 b n where b i = 1 if i B = 0 if i B b i as a vector of bits: This is called the characteristic vector of set. B 11

Representing sets as bitvectors Suppose that universe is. We can represent every set U {1, 2,, n} B U b 1 b 2 b n where b i = 1 if i B = 0 if i B b i as a vector of bits: This is called the characteristic vector of set. Given characteristic vectors for and, what is the vector for A B = A B = A B B 11

Representing sets as bitvectors Suppose that universe is. We can represent every set U {1, 2,, n} B U b 1 b 2 b n where b i = 1 if i B = 0 if i B b i as a vector of bits: This is called the characteristic vector of set. A A B = ( a 1 + b 1 ) ( a n + b n ) A B = Given characteristic vectors for and, what is the vector for B B 11

Representing sets as bitvectors Suppose that universe is. We can represent every set U {1, 2,, n} B U b 1 b 2 b n where b i = 1 if i B = 0 if i B b i as a vector of bits: This is called the characteristic vector of set. A A B = ( a 1 + b 1 ) ( a n + b n ) A B = ( a 1 b 1 ) ( a n b n ) Given characteristic vectors for and, what is the vector for B B 11

Unix/Linux file permissions $ ls -l drwxr-xr-x... Documents/ -rw-r--r--... file1 Permissions maintained as bitvectors. Letter means the bit is 1. - means the bit is zero. 12

Bitwise operations 01101101 00110111 01111111 00101010 00001111 00001010 01101101 00110111 01011010 z = x y z = x & y z = x ^ y (x y) y = x Note that. 13

Private key cryptography Alice wants to communicate a message secretly to Bob, so that eavesdropper Eve who sees their conversation can t understand. Alice and Bob can get together ahead of time and privately share a secret key. K How can they communicate securely in this setting? m m Sender (Alice) plaintext message encrypt key cyphertext decrypt key plaintext message Receiver (Bob) Eavesdropper (Eve) 14

One-time pad Alice and Bob privately share a random -bitvector. Eve doesn t know. n K Later, Alice has -bit message to send to Bob. Alice computes C = m K. Alice sends C to Bob. Bob computes, which is. Eve can t figure out from unless she can guess. And that s very unlikely for large m m = C K (m K) K = m m C K n n K 15

Modular arithmetic basics Arithmetic over a finite domain (a.k.a computer arithmetic). 16

Modular arithmetic in action What does this Java program output? public class Seconds { final static int SEC_IN_YEAR = 364*24*60*60; public static void main(string args[]) { System.out.println("Number of seconds in 10100 years: " + (SEC_IN_YEAR*10100)); } } 17

Modular arithmetic in action What does this Java program output? public class Seconds { final static int SEC_IN_YEAR = 364*24*60*60; public static void main(string args[]) { System.out.println("Number of seconds in 10100 years: " + (SEC_IN_YEAR*10100)); } } $javac Test.java $java -Xmx128M -Xms16M Test Number of seconds in 10100 years: -186619904 You ll recognize this as integer overflow. It happens because computers use modular arithmetic to operate on finite integer data types, such as int. 17

It all starts with divisibility Divisibility is the core concept behind modular arithmetic. a b a b a Z, b Z a b k Z. b = ka Definition: divides, written as. For,. 18

It all starts with divisibility Divisibility is the core concept behind modular arithmetic. a b a b a Z, b Z a b k Z. b = ka Definition: divides, written as. For,. Examples: which of the following are true and which are false? 5 1 1 5 25 5 5 25 5 0 0 5 2 3 3 2 18

It all starts with divisibility Divisibility is the core concept behind modular arithmetic. a b a b a Z, b Z a b k Z. b = ka Definition: divides, written as. For,. Examples: which of the following are true and which are false? 5 1 F 1 5 T 25 5 5 25 5 0 0 5 2 3 3 2 18

It all starts with divisibility Divisibility is the core concept behind modular arithmetic. a b a b a Z, b Z a b k Z. b = ka Definition: divides, written as. For,. Examples: which of the following are true and which are false? 5 1 F 1 5 T 25 5 F 5 25 T 5 0 0 5 2 3 3 2 18

It all starts with divisibility Divisibility is the core concept behind modular arithmetic. a b a b a Z, b Z a b k Z. b = ka Definition: divides, written as. For,. Examples: which of the following are true and which are false? 5 1 F 1 5 T 25 5 F 5 25 T 5 0 T 0 5 F 2 3 3 2 18

It all starts with divisibility Divisibility is the core concept behind modular arithmetic. a b a b a Z, b Z a b k Z. b = ka Definition: divides, written as. For,. Examples: which of the following are true and which are false? 5 1 F 1 5 T 25 5 F 5 25 T 5 0 T 0 5 F 2 3 F 3 2 F 18

Division theorem Division theorem For a Z, d Z with d > 0, there exist unique integers with such that. a = dq + r q, r 0 r < d a d q = a div d r = a mod d If we divide by, we get a unique quotient and nonnegative remainder. 19

Division theorem Division theorem For a Z, d Z with d > 0, there exist unique integers with such that. a = dq + r q, r 0 r < d a d q = a div d r = a mod d If we divide by, we get a unique quotient and nonnegative remainder. r 0 a < 0 mod is not % Note that even if, so. public class NotMod { public static void main(string args[]) { System.out.println("-5 mod 2 = 1."); System.out.println("-5 % 2 = " + (-5 % 2)) } } $javac NotMod.java $java -Xmx128M -Xms16M NotMod -5 mod 2 = 1. -5 % 2 = -1 19

Example: arithmetic mod 7 + a + 7 b = (a + b) mod 7 0 1 2 3 4 5 6 0 0 1 2 3 4 5 6 1 1 2 3 4 5 6 0 2 2 3 4 5 6 0 1 3 3 4 5 6 0 1 2 4 4 5 6 0 1 2 3 5 5 6 0 1 2 3 4 6 6 0 1 2 3 4 5 a 7 b = (a b) mod 7 0 1 2 3 4 5 6 0 0 0 0 0 0 0 0 1 0 1 2 3 4 5 6 2 0 2 4 6 1 3 5 3 0 3 6 2 5 1 4 4 0 4 1 5 2 6 3 5 0 5 3 1 6 4 2 6 0 6 5 4 3 2 1 20

Modular arithmetic properties Congruence, addition, multiplication, proofs. 21

Congruence modulo a positive integer a b m a b (mod m) a, b, m Z m > 0 a b (mod m) m (a b) Definition: is congruent to modulo, written as For with, 22

Congruence modulo a positive integer a b m a b (mod m) a, b, m Z m > 0 a b (mod m) m (a b) Definition: is congruent to modulo, written as For with, Examples: what do these mean and when are they true? x 0 (mod 2) 1 19 (mod 5) y 2 (mod 7) 22

Congruence modulo a positive integer a b m a b (mod m) a, b, m Z m > 0 a b (mod m) m (a b) Definition: is congruent to modulo, written as For with, Examples: what do these mean and when are they true? x 0 (mod 2) True for every x 1 19 (mod 5) y 2 (mod 7) that is divisible by 2, i.e., even. 22

Congruence modulo a positive integer a b m a b (mod m) a, b, m Z m > 0 a b (mod m) m (a b) Definition: is congruent to modulo, written as For with, Examples: what do these mean and when are they true? x 0 (mod 2) True for every x 1 19 (mod 5) 1 19 = 20 y 2 (mod 7) that is divisible by 2, i.e., even. True because is divisible by 5. 22

Congruence modulo a positive integer a b m a b (mod m) a, b, m Z m > 0 a b (mod m) m (a b) Definition: is congruent to modulo, written as For with, Examples: what do these mean and when are they true? x 0 (mod 2) True for every x that is divisible by 2, i.e., even. 1 19 (mod 5) True because 1 19 = 20 is divisible by 5. y 2 (mod 7) y y = 2 + 7k k Z True for every of the form where. 22

Congruence and equality Congruence property Let a, b, m Z with m > 0. Then, if and only if. a b (mod m) a mod m = b mod m 23

Congruence and equality Congruence property Let a, b, m Z with m > 0. Then, if and only if. a b (mod m) Proof: Suppose that. a b (mod m) a mod m = b mod m a mod m = b mod m Suppose that. 23

Congruence and equality Congruence property Let a, b, m Z with m > 0. Then, if and only if. a b (mod m) Proof: Suppose that. a b (mod m) Then by definition of congruence. So for some by definition of divides. Therefore,. Taking both sides modulo, we get Suppose that. a mod m = b mod m m a b a b = km k Z a = b + km m a mod m = (b + km) mod m = b mod m. a mod m = b mod m 23

Congruence and equality Congruence property Let a, b, m Z with m > 0. Then, if and only if. a b (mod m) Proof: Suppose that. a b (mod m) Then by definition of congruence. So for some by definition of divides. Therefore,. Taking both sides modulo, we get Suppose that. a mod m = b mod m m a b a b = km k Z a = b + km m a mod m = (b + km) mod m = b mod m. a mod m = b mod m a = mq + (a mod m) b = ms + (b mod m) By the division theorem, and for some q, s Z. Then, a b = (mq + (a mod m)) (ms + (b mod m)) = m(q s) + (a mod m b mod m) = m(q s), since a mod m = b mod m. Therefore, and so. m (a b) a b (mod m) 23

mod m (mod m) The function vs the predicate mod m a Z a mod m {0, 1,, m 1} The function takes any and maps it to a remainder. In other words, remainder modulo mod m m places all integers that have the same into the same group (a.k.a. congruence class ). (mod m) a, b Z a b mod m The predicate compares and returns true if and only if and are in the same group according to the function. 24

Modular addition property Modular addition property Let m be a positive integer ( m Z with m > 0). If and, then. a b (mod m) c d (mod m) a + c b + d (mod m) 25

Modular addition property Modular addition property Let m be a positive integer ( m Z with m > 0). If and, then. a b (mod m) c d (mod m) a + c b + d (mod m) Proof: Suppose that a b (mod m) and c d (mod m). By the definition of congruence, there are k and j such that a b = km and c d = jm. Adding these equations together, we get (a + c) (b + d) = m(j + k). Reapplying the definition of congruence, we get that. (a + c) (b + d) (mod m) 25

Modular multiplication property Modular multiplication property Let m be a positive integer ( m Z with m > 0). If and, then. a b (mod m) c d (mod m) ac bd (mod m) 26

Modular multiplication property Modular multiplication property Let m be a positive integer ( m Z with m > 0). If and, then. a b (mod m) c d (mod m) ac bd (mod m) Proof: Suppose that a b (mod m) and c d (mod m). By the definition of congruence, there are k and j such that a b = km and c d = jm. So, a = km + b and c = jm + b. Multiplying these equations together, we get ac = (km + b)(jm + d) = kj m 2 + kmd + bjm + bd. Rearranging gives us ac bd = m(kjm + kd + bj). Reapplying the definition of congruence, we get that. ac bd (mod m) 26

Example: a proof using modular arithmetic n Z n 2 0 (mod 4) n 2 1 (mod 4) Let, and prove that or. 27

Example: a proof using modular arithmetic n Z n 2 0 (mod 4) n 2 1 (mod 4) Let, and prove that or. Let s look at a few examples: 0 2 = 0 0 (mod 4) 1 2 = 1 1 (mod 4) 2 2 = 4 0 (mod 4) 3 2 = 9 1 (mod 4) 4 2 = 16 0 (mod 4) 27

Example: a proof using modular arithmetic n Z n 2 0 (mod 4) n 2 1 (mod 4) Let, and prove that or. Let s look at a few examples: 0 2 = 0 0 (mod 4) 1 2 = 1 1 (mod 4) 2 2 = 4 0 (mod 4) 3 2 = 9 1 (mod 4) 4 2 = 16 0 (mod 4) It looks like n 0 (mod 2) n 2 0 (mod 4) n 1 (mod 2) n 2 1 (mod 4) 27

Example: a proof using modular arithmetic n Z n 2 0 (mod 4) n 2 1 (mod 4) Let, and prove that or. Let s look at a few examples: 0 2 = 0 0 (mod 4) 1 2 = 1 1 (mod 4) 2 2 = 4 0 (mod 4) 3 2 = 9 1 (mod 4) 4 2 = 16 0 (mod 4) It looks like n 0 (mod 2) n 2 0 (mod 4) n 1 (mod 2) n 2 1 (mod 4) Proof by cases: Case 1 ( is even): n n Case 2 ( is odd): 27

Example: a proof using modular arithmetic n Z n 2 0 (mod 4) n 2 1 (mod 4) Let, and prove that or. Let s look at a few examples: 0 2 = 0 0 (mod 4) 1 2 = 1 1 (mod 4) 2 2 = 4 0 (mod 4) 3 2 = 9 1 (mod 4) 4 2 = 16 0 (mod 4) It looks like n 0 (mod 2) n 2 0 (mod 4) n 1 (mod 2) n 2 1 (mod 4) Proof by cases: Case 1 ( is even): n n 0 (mod 2) Suppose. Then for some integer k. So n 2 = (2k) 2 = 4k 2. Therefore, by definition of congruence,. n Case 2 ( is odd): n 2 n = 2k 0 (mod 4) 27

Example: a proof using modular arithmetic n Z n 2 0 (mod 4) n 2 1 (mod 4) Let, and prove that or. Let s look at a few examples: 0 2 = 0 0 (mod 4) 1 2 = 1 1 (mod 4) 2 2 = 4 0 (mod 4) 3 2 = 9 1 (mod 4) 4 2 = 16 0 (mod 4) It looks like n 0 (mod 2) n 2 0 (mod 4) n 1 (mod 2) n 2 1 (mod 4) Proof by cases: Case 1 ( is even): n n 0 (mod 2) Suppose. Then for some integer k. So n 2 = (2k) 2 = 4k 2. Therefore, by definition of congruence,. n Case 2 ( is odd): n 2 n = 2k 0 (mod 4) n 1 (mod 2) n = 2k + 1 Suppose. Then for some integer k. So n 2 = (2k + 1 ) 2 = 4 k 2 + 4k + 1 = 4( + k) + 1. Therefore, by definition of congruence,. n 2 k 2 1 (mod 4) 27

Modular arithmetic and integer representations Unsigned, sign-magnitude, and two s complement representation. 28

Unsigned integer representation x n x = n 1 i=0 b i2 i b i {0, 1} bn 1 b 2 b 1 b 0 Represent integer as a sum of powers of 2: If where each, then the representation is. 29

Unsigned integer representation x n x = n 1 i=0 b i2 i b i {0, 1} bn 1 b 2 b 1 b 0 Represent integer as a sum of powers of 2: If where each, then the representation is. Examples: 99 = 64 + 32 + 2 + 1 18 = 16 + 2 n = 8 99 = 0110 0011 18 = 0001 0010 So for : 29

Unsigned integer representation x n x = n 1 i=0 b i2 i b i {0, 1} bn 1 b 2 b 1 b 0 Represent integer as a sum of powers of 2: If where each, then the representation is. Examples: 99 = 64 + 32 + 2 + 1 18 = 16 + 2 n = 8 99 = 0110 0011 18 = 0001 0010 So for : This works for unsigned integers. How do we represented signed integers? 29

Sign-magnitude integer representation 2 n 1 < x < 2 n 1 x n If, represent with bits as follows: Use the first bit as the sign (0 for positive and 1 for negative), and the remaining bits as the (unsigned) value. n 1 Examples: 99 = 64 + 32 + 2 + 1 18 = 16 + 2 n = 8 99 = 0110 0011 18 = 1001 0010 So for : 30

Sign-magnitude integer representation 2 n 1 < x < 2 n 1 x n If, represent with bits as follows: Use the first bit as the sign (0 for positive and 1 for negative), and the remaining bits as the (unsigned) value. n 1 Examples: 99 = 64 + 32 + 2 + 1 18 = 16 + 2 n = 8 99 = 0110 0011 18 = 1001 0010 81 = 0101 0001 So for : The problem with this representation is that our standard arithmetic algorithms no longer work, e.g., adding the representation of -18 and 99 doesn t give the representation of 81. 30

Two s complement integer representation x n Represent with bits as follows: If 0 x < 2 n 1, use the n-bit unsigned representation of x. If, use the -bit unsigned representation of. 2 n 1 x < 0 n x 2 n 31

Two s complement integer representation x n Represent with bits as follows: If 0 x < 2 n 1, use the n-bit unsigned representation of x. If, use the -bit unsigned representation of. 2 n 1 x < 0 n x 2 n Key property: Two s complement representation of any number so arithmetic works. y mod 2 n mod 2 n y is equivalent to 31

Two s complement integer representation x n Represent with bits as follows: If 0 x < 2 n 1, use the n-bit unsigned representation of x. If, use the -bit unsigned representation of. 2 n 1 x < 0 n x 2 n Key property: Two s complement representation of any number so arithmetic works. y mod 2 n mod 2 n y is equivalent to Examples: So for : 99 = 64 + 32 + 2 + 1 18 = 16 + 2 2 8 18 = 256 18 = 238 = 128 + 64 + 32 + 8 + 4 + 2 81 = 64 + 16 + 1 n = 8 99 = 0110 0011 18 = 1110 1110 81 = 0101 0001 31

Computing the two s complement representation 2 n 1 x < 0 x n 2 n x For, is represented using the -bit unsigned representation of. Here is an easy way to compute this value: n n Compute the -bit unsigned representation of. Flip the bits of x to get the representation of 2 1 x. This works because the string of all 1 s represents 2 n 1. Add 1 to get. 2 n x x 32

Applications of modular arithmetic Hashing, pseudo-random numbers, ciphers. 33

Hashing Problem: We want to map a small number of data values from a large domain {0, 1,, M 1} into a small set of locations {0, 1,, n 1} to be able to quickly check if a value is present. Solution: Compute hash(x) = x mod p for a prime p close to n. Or, compute for a prime close to. hash(x) = ax + b mod p p n This approach depends on all of the bits of data the data. Helps avoid collisions due to similar values. But need to manage them if they occur. 34

Pseudo-random number generation Linear Congruential method x n+1 = (a x n + c) mod m, a, c, m x 0 Choose random and produce a long sequences of s. x n 35

Simple ciphers Ceasar or shi! cipher: Treat letters as numbers: A = 0, B = 1, f (p) = (p + k) mod 26 f 1 (p) = (p k) mod 26 More general version: f (p) = (ap + b) mod 26 f 1 (p) = ( a 1 (p b)) mod 26 36

Summary Sets can be represented efficiently using bitvectors. This representation is used heavily in the real world. With this representation, set operations reduce to fast bitwise operations. Modular arithmetic is arithmetic over a finite domain. Key notions are divisibility and congruency. mod m Modular arithmetic is the basis of computing. Used with two s complement representation to implement computer arithmetic. Also used in hashing, pseudo-random number generation, and cryptography. 37