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

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

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

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

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

CSE 20 DISCRETE MATH. Winter

Fall 2015 Lecture 14: Modular congruences. cse 311: foundations of computing

Applied Cryptography and Computer Security CSE 664 Spring 2017

Discrete Mathematics GCD, LCM, RSA Algorithm

Mat Week 8. Week 8. gcd() Mat Bases. Integers & Computers. Linear Combos. Week 8. Induction Proofs. Fall 2013

Student Responsibilities Week 8. Mat Section 3.6 Integers and Algorithms. Algorithm to Find gcd()

COMP239: Mathematics for Computer Science II. Prof. Chadi Assi EV7.635

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

Intermediate Math Circles February 26, 2014 Diophantine Equations I

4 Number Theory and Cryptography

Ch 4.2 Divisibility Properties

CPSC 467b: Cryptography and Computer Security

Public Key Encryption

8 Primes and Modular Arithmetic

3 The fundamentals: Algorithms, the integers, and matrices

ECE 646 Lecture 5. Mathematical Background: Modular Arithmetic

2.5 정수와알고리즘 (Integers and Algorithms)

CPSC 467: Cryptography and Computer Security

Section Summary. Division Division Algorithm Modular Arithmetic

Base-b representations of integers. (b 진법표현 ) Algorithms for computer arithmetic: Euclidean algorithm for finding GCD s.

Senior Math Circles Cryptography and Number Theory Week 2

Number Theory and Cryptography

This is a recursive algorithm. The procedure is guaranteed to terminate, since the second argument decreases each time.

CS483 Design and Analysis of Algorithms

Introduction to Cryptography CS 355 Lecture 3

Numbers. Çetin Kaya Koç Winter / 18

CS March 17, 2009

The Euclidean Algorithm

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

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

CS250: Discrete Math for Computer Science

1. Algebra 1.7. Prime numbers

Homework 3, solutions

With Question/Answer Animations. Chapter 4

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

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

The Euclidean Algorithm and Multiplicative Inverses

Number Theory. CSS322: Security and Cryptography. Sirindhorn International Institute of Technology Thammasat University CSS322. Number Theory.

Addition. Ch1 - Algorithms with numbers. Multiplication. al-khwārizmī. al-khwārizmī. Division 53+35=88. Cost? (n number of bits) 13x11=143. Cost?

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

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

ECE 646 Lecture 5. Motivation: Mathematical Background: Modular Arithmetic. Public-key ciphers. RSA keys. RSA as a trap-door one-way function

Integers and Division

Proofs. Methods of Proof Divisibility Floor and Ceiling Contradiction & Contrapositive Euclidean Algorithm. Reading (Epp s textbook)

Number Theory Basics Z = {..., 2, 1, 0, 1, 2,...} For, b Z, we say that divides b if z = b for some. Notation: b Fact: for all, b, c Z:

INTEGERS. In this section we aim to show the following: Goal. Every natural number can be written uniquely as a product of primes.

a the relation arb is defined if and only if = 2 k, k

MATH 215 Final. M4. For all a, b in Z, a b = b a.

Chapter 4 Finite Fields

Algorithms (II) Yu Yu. Shanghai Jiaotong University

NOTES ON INTEGERS. 1. Integers

Basic elements of number theory

Basic elements of number theory

NOTES ON SIMPLE NUMBER THEORY

CS 5319 Advanced Discrete Structure. Lecture 9: Introduction to Number Theory II

Number theory (Chapter 4)

1. (16 points) Circle T if the corresponding statement is True or F if it is False.

Mathematical Foundations of Cryptography

Remainders. We learned how to multiply and divide in elementary

MATH 145 Algebra, Solutions to Assignment 4

Number Theory and Group Theoryfor Public-Key Cryptography

Introduction to Cryptography. Lecture 6

NUMBER THEORY AND CODES. Álvaro Pelayo WUSTL

Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices. Integers & Algorithms (2.5)

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

MATH 501 Discrete Mathematics. Lecture 6: Number theory. German University Cairo, Department of Media Engineering and Technology.

Introduction to Number Theory. The study of the integers

Number Theory Proof Portfolio

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

Intermediate Math Circles February 29, 2012 Linear Diophantine Equations I

CMPUT 403: Number Theory

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:

CSE20: Discrete Mathematics

Homework #2 solutions Due: June 15, 2012

2 Arithmetic. 2.1 Greatest common divisors. This chapter is about properties of the integers Z = {..., 2, 1, 0, 1, 2,...}.

Exercises Exercises. 2. Determine whether each of these integers is prime. a) 21. b) 29. c) 71. d) 97. e) 111. f) 143. a) 19. b) 27. c) 93.

4. Number Theory (Part 2)

Number Theory Notes Spring 2011

Elementary Number Theory MARUCO. Summer, 2018

2 Elementary number theory

ICS141: Discrete Mathematics for Computer Science I

2. THE EUCLIDEAN ALGORITHM More ring essentials

4 Powers of an Element; Cyclic Groups

Chapter 1. Greatest common divisor. 1.1 The division theorem. In the beginning, there are the natural numbers 0, 1, 2, 3, 4,...,

1 Overview and revision

Mathematics for Computer Science Exercises for Week 10

Introduction to Public-Key Cryptosystems:

Elementary Algebra Chinese Remainder Theorem Euclidean Algorithm

Finite Fields. Mike Reiter

Modular Arithmetic and Elementary Algebra

Math Circle Beginners Group February 28, 2016 Euclid and Prime Numbers Solutions

An Algorithm for Prime Factorization

Lecture Notes. Advanced Discrete Structures COT S

NUMBER THEORY. Anwitaman DATTA SCSE, NTU Singapore CX4024. CRYPTOGRAPHY & NETWORK SECURITY 2018, Anwitaman DATTA

Notes for Recitation 6

ALG 4.0 Number Theory Algorithms:

Transcription:

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

n-bit Unsigned Integer Representation Represent integer as sum of powers of 2: If 2 where each {0,1} then representation is b n-1...b 2 b 1 b 0 99 = 64 + 32 + 2 + 1 18 = 16 + 2 For n = 8: 99: 0110 0011 18: 0001 0010

Sign-Magnitude Integer Representation -bit signed integers Suppose that 2 < < 2 First bit as the sign, 1 bits for the value 99 = 64 + 32 + 2 + 1 18 = 16 + 2 For n = 8: 99: 0110 0011-18: 1001 0010 Any problems with this representation?

Two s Complement Representation bit signed integers, first bit will still be the sign bit Suppose that 0 < 2, is represented by the binary representation of Suppose that 0 < 2, is represented by the binary representation of 2 Key property: Twos complement representation of any number is equivalent to so arithmetic works 99 = 64 + 32 + 2 + 1 18 = 16 + 2 For n = 8: 99: 0110 0011-18: 1110 1110

Sign-Magnitude vs. Two s Complement -7-6 -5-4 -3-2 -1 0 1 2 3 4 5 6 7 1111 1110 1101 1100 1011 1010 1001 0000 0001 0010 0011 0100 0101 0110 0111 Sign-bit -8-7 -6-5 -4-3 -2-1 0 1 2 3 4 5 6 7 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111 Two s complement

Two s Complement Representation For 0 < 2, is represented by the binary representation of 2 That is, the two s complement representation of any number has the same value as modulo 2. To compute this: Flip the bits of then add 1: All 1 s string is 2 1, so Flip the bits of replace by 2 1 Then add 1 to get 2

Basic Applications of mod Hashing Pseudo random number generation Simple cipher

Hashing Scenario: Map a small number of data values from a large domain 0,1,, 1......into a small set of locations 0,1,, 1 so one can quickly check if some value is present hash = mod ' for 'a prime close to or hash = ()+) mod ' Depends on all of the bits of the data helps avoid collisions due to similar values need to manage them if they occur

Pseudo-Random Number Generation Linear Congruential method, = ) +- mod. Choose random,), -,.and produce a long sequence of s

Simple Ciphers Caesar cipher, A = 1, B = 2,... HELLO WORLD Shift cipher f(p) = (p + k) mod 26 f -1 (p) = (p k) mod 26 More general f(p) = (ap + b) mod 26

Primality An integer p greater than 1 is called prime if the only positive factors of p are 1 and p. A positive integer that is greater than 1 and is not prime is called composite.

Fundamental Theorem of Arithmetic Every positive integer greater than 1 has a unique prime factorization 48 = 2 2 2 2 3 591 = 3 197 45,523 = 45,523 321,950 = 2 5 5 47 137 1,234,567,890 = 2 3 3 5 3,607 3,803

Euclid s Theorem There are an infinite number of primes. Proof by contradiction: Suppose that there are only a finite number of primes and call the full list ',' /,,'.

Euclid s Theorem There are an infinite number of primes. Proof by contradiction: Suppose that there are only a finite number of primes and call the full list ',' /,,'. Define the number 0 =' ' / ' 2 ' and let 4 =0+1.

Euclid s Theorem There are an infinite number of primes. Proof by contradiction: Suppose that there are only a finite number of primes and call the full list ',' /,,'. Define the number 0 =' ' / ' 2 ' and let 4 =0+1. Case 1: 4 is prime: Then 4is a prime different from all of ',' /,,' since it is bigger than all of them. Case 2: 4 > 1is not prime: Then 4has some prime factor '(which must be in the list). Therefore ' 0 and ' 4so ' 4 0 which means that ' 1. Both cases are contradictions so the assumption is false.

Famous Algorithmic Problems Primality Testing Given an integer, determine if is prime Factoring Given an integer, determine the prime factorization of

Factoring Factor the following 232 digit number [RSA768]: 123018668453011775513049495838496272077 285356959533479219732245215172640050726 365751874520219978646938995647494277406 384592519255732630345373154826850791702 612214291346167042921431160222124047927 4737794080665351419597459856902143413

12301866845301177551304949583849627207728535695953347 92197322452151726400507263657518745202199786469389956 47494277406384592519255732630345373154826850791702612 21429134616704292143116022212404792747377940806653514 19597459856902143413 334780716989568987860441698482126908177047949837 137685689124313889828837938780022876147116525317 43087737814467999489 367460436667995904282446337996279526322791581643 430876426760322838157396665112792333734171433968 10270092798736308917

Greatest Common Divisor GCD(a, b): Largest integer 8such that 8 )and 8 GCD(100, 125) = GCD(17, 49) = GCD(11, 66) = GCD(13, 0) = GCD(180, 252) =

GCD and Factoring a = 2 3 3 5 2 7 11 = 46,200 b = 2 3 2 5 3 7 13 = 204,750 GCD(a, b) = 2 min(3,1) 3 min(1,2) 5 min(2,3) 7 min(1,1) 11 min(1,0) 13 min(0,1) Factoring is expensive! Can we compute GCD(a,b) without factoring?

Useful GCD Fact If a and b are positive integers, then gcd(a,b)=gcd(b, a modb)

Useful GCD Fact If a and b are positive integers, then gcd(a,b) = gcd(b, a modb) Proof: By definition of mod, ) = :+() mod ) for some integer : = ) div. Let 8 = gcd (),). Then 8 ) and 8 so ) =?8 and = @8 for some integers? and @. Therefore () mod ) = ) : =?8 :@8 = (? :@)8. So, 8 () mod ) and since 8 we must have 8 gcd (,) mod ). Now, let A = gcd (,) mod ). Then A and A () mod ) so =.A and () mod )=A for some integers. and. Therefore ) = :+().B8 ) = :.A+ A = (:.+)A. So, A ) and since A we must have A gcd (),). It follows that gcd (),)=gcd (,) mod ).

Another simple GCD fact If a is a positive integer, gcd(a,0)=a.

Euclid s Algorithm gcd(a, b) = gcd(b, a mod b) int gcd(int a, int b){ /* a >= b, b > 0 */ if (b == 0) { return a; } else { return gcd(b, a % b); } Example: GCD(660, 126)

Euclid s Algorithm Repeatedly use gcd ), = gcd,) mod to reduce numbers until you get gcd (C,0) =C. gcd(660,126) =

Euclid s Algorithm Repeatedly use gcd ), = gcd,) mod to reduce numbers until you get gcd (C,0) =C. gcd(660,126) = gcd(126, 660 mod 126) = gcd(126, 30) = gcd(30, 126 mod 30) = gcd(30, 6) = gcd(6, 30 mod 6) = gcd(6, 0) = 6

Bézout s theorem If a and b are positive integers, then there exist integers s and t such that gcd(a,b) = sa + tb.

Extended Euclidean algorithm Can use Euclid s Algorithm to find D,Esuch that gcd ), =D)+E

Extended Euclidean algorithm Can use Euclid s Algorithm to find D,Esuch that gcd ), =D)+E Step 1 (Compute GCD & Keep Intermediary Information): a b b a mod b = r b r a = q * b + r gcd(35, 27) = gcd(27, 35 mod 27) = gcd(27, 8) (35 = 1 * 27 + 8)

Extended Euclidean algorithm Can use Euclid s Algorithm to find D,Esuch that gcd ), =D)+E Step 1 (Compute GCD & Keep Intermediary Information): a b b a mod b = r b r a = q * b + r gcd(35, 27) = gcd(27, 35 mod 27) = gcd(27, 8) (35 = 1 * 27 + 8) = gcd(8, 27 mod 8) = gcd(8, 3) (27 = 3 * 8 + 3) = gcd(3, 8 mod 3) = gcd(3, 2) (8 = 2 * 3 + 2) = gcd(2, 3 mod 2) = gcd(2, 1) (3 = 1 * 2 + 1) = gcd(1, 2 mod 1) = gcd(1, 0)

Extended Euclidean algorithm Can use Euclid s Algorithm to find D,Esuch that gcd ), =D)+E Step 2 (Solve the equations for r): a = q * b + r 35 = 1 * 27 + 8 27 = 3 * 8 + 3 8 = 2 * 3 + 2 3 = 1 * 2 + 1 r = a -- q * b 8 = 35 1 * 27

Extended Euclidean algorithm Can use Euclid s Algorithm to find D,Esuch that gcd ), =D)+E Step 2 (Solve the equations for r): a = q * b + r 35 = 1 * 27 + 8 27 = 3 * 8 + 3 8 = 2 * 3 + 2 3 = 1 * 2 + 1 2 = 2 * 1 + 0 r = a -- q * b 8 = 35 1 * 27 3 = 27 3 * 8 2 = 8 2 * 3 1 = 3 1 * 2

Extended Euclidean algorithm Can use Euclid s Algorithm to find D,Esuch that gcd ), =D)+E Step 3 (Backward Substitute Equations): 8 = 35 1 * 27 3 = 27 3 * 8 2 = 8 2 * 3 1 = 3 1 * 2 1 = 3 1*(8 2* 3) = 3 8 + 2 * 3 = ( 1)* 8 + 3 * 3 Plug in the defof 2 Re-arrange into 3 s and 8 s

Extended Euclidean algorithm Can use Euclid s Algorithm to find D,Esuch that gcd ), =D)+E Step 3 (Backward Substitute Equations): 8 = 35 1 * 27 3 = 27 3 * 8 2 = 8 2 * 3 1 = 3 1 * 2 Re-arrange into 27 s and 35 s 1 = 3 1*(8 2* 3) = 3 8 + 2 * 3 = ( 1)* 8 + 3 * 3 Plug in the defof 2 Re-arrange into 3 s and 8 s Plug in the defof 3 = ( 1)* 8 + 3 *(27 3* 8) = ( 1)* 8 + 3 * 27 + ( 9)* 8 = 3 * 27 + ( 10)* 8 Re-arrange into 8 s and 27 s = 3 * 27 + ( 10) * (35 1 * 27) = 3 * 27 + ( 10) * 35 + 10 * 27 = 13 * 27 + ( 10) * 35

multiplicative inverse mod. Suppose GCD ),. = 1 By Bézout s Theorem, there exist integers D and E such that D)+E. = 1. D mod.is the multiplicative inverse of ): 1 = D)+E. mod. = D) mod.

Example Solve: 7 1 (mod 26)

Example Solve: 7 1 (mod 26) gcd (26,7) = gcd (7,5) = gcd (5,2) = gcd (2,1) = 1 26 = 7 3 + 5 5 = 26 7 3 7 = 5 1 + 2 2 = 7 5 1 5 = 2 2 + 1 1 = 5 2 2 1 = 5 2 (7 5 1) = ( 7) 2 + 3 5 = 7 2 + 3 (26 7 3) = 11 7 + 3 26 Multiplicative inverse of 7 mod 26 Now ( 11) mod 26 =15. So, =15+26? for? Z.

Example of a more general equation Now solve: 7 3 (mod 26) We already computed that 15is the multiplicative inverse of 7modulo 26: That is, 7 15 1 (mod 26) By the multiplicative property of mod we have 7 15 3 3 (mod 26) So any 15 3 mod 26 is a solution. That is, = 19+26?for any integer?is a solution.

Math mod a prime is especially nice gcd (),.)=1if.is prime and 0 < ) <. so can always solve these equations mod a prime. + 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 X 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 mod 7