ICS-252 Discrete Structure II

  ICS-252 Discrete Structure II Lecture 2

  ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

Outlines

  Modular Arithmetic (DMA-203-205) •

  • Theorem 3, 4, 5

  Example 5, 6 • Applications of Congruences • (DMA-205-208) DMA=Discrete Mathematics and its ICS Applications

  ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail.  2

Modular Arithmetic

  In  mathematics,  modular arithmetic (sometimes  called  •

  clock arithmetic )  is a system of  arithmetic for integers, where 

  numbers  "wrap around" after they reach a certain value—the 

  modulus m modulus m .  Modular arithmetic was introduced by Carl   Modular arithmetic was introduced by Carl 

  Friedrich  Gauss in his book Disquisitiones Arithmeticae,  published  in 1801. Time ‐keeping on a clock gives an example of modular  arithmetic. A A  familiar use of modular arithmetic is its use in the 12 hour   familiar use of modular arithmetic is its use in the 12‐hour  clock,  in which the day is divided into two 12 hour periods. If the  time  is 7:00 now, then 8 hours later it will be 3:00. Usual addition  would  suggest that the later time should be 7 + 8 = 15, but this is  not  the answer because clock time "wraps around" every 12  hours;  there is no "15 o'clock".  ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail.  3 Modular Arithmetic

  • Likewise, elapse,  then the time will be 9:00 the next day, rather than  33:00.

   if the clock starts at 12:00 (noon) and 21 hours 

   Since the hour number starts over when it reaches 12, 

  m=12 this  is arithmetic modulo 12 ( ). Time ‐keeping on a clock gives an  example ICS  of modular arithmetic. ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail.  4

Modular Arithmetic

  Modular arithmetic can be handled mathematically by introducing a congruence relation on the integers that is compatible with the operations of the ring of integers is compatible with the operations of the ring of integers a ring is an algebraic structure consisting of a set together with two binary operations (usually called addition and multiplication). For a positive integer m, two integers a and b are said to m congruent modulo be , Therefore we can write a mod m = b mod m ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail.  5 Modular Arithmetic

  Let a and b be integers, and let m be a positive

Theorem 3:

  integer, Then a if and only if a mod m = b mod

  ≡ b (mod m) m .

  If suppose a mod m = b mod m, then a and b have the

  Proof: same remainder when divided by m.

  1 m + r and b = q 2 m + r, where 0 Hence, a = q ≤ r < m.

  It follows that a – b = (q

  1 - q 2 m , So m | (a – b) .

  )

  It follows that a ≡ b (mod m) .

  Note: m | (a - b) we can read as- m evaluates for a –b

  ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail.  6

Modular Arithmetic

  Example 5: Determine whether 17 is congruent to 5 modulo 6

  and whether 24 and 14 are congruent modulo 6

  Solution: Because 6 divides 17 – 5 = 12, We see that 17

  ≡ 5 (mod 6)

  However, because 24 – 14 = 10 is not divisible by 6, We see that 24 is not congruent with 14 (mod 6).

  Theorem 4: Let m be a positive integer. The integers a and b

  are congruent modulo m if and only if there is an integer k such that a = b + km

  Proof : if a ( ) | ( )

  ≡ b (mod m), then m | (a - b). This means that there is an integer k such that a – b = km, so that a = b + km. Conversely, if there is an integer K such that a = b + km then km = a – b, Hence, m divides a – b , so that a ICS ≡ b (mod m). 7

  ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

Modular Arithmetic Theorem

   5: Let  m be a positive integer. If a 

  ≡ b (mod m) and c ≡ d (mod m), then a + c

  ≡ b + d (mod m) and ac ≡ bd (mod m) Because a

Proof:

  ≡ b (mod m) and c ≡ d (mod m), there are integers s and t with b = a + sm and d = c + tm, Hence, i t d t ith b d d t H b + d = (a + sm) + (c + tm) = (a + c) + (s + t)m And bd= (a + sm)(c + tm) = ac + atm + smc + smmt = ac + m( at + sc + smt) = ac + m(at + cs+ stm) Hence, a + c a + c

  ≡ b + d (mod m) and ac ≡ bd (mod m) ≡ b + d (mod m) and ac ≡ bd (mod m) Because 7

  Example: ≡ 2 (mod 5) and 11 ≡ 1 (mod 5), it

  follows from theorem 5 that 18 = 7 + 11 ≡ 2 + 1 = 3 (mod 5)

  And that 77 = 7. 11 ICS ≡ 2 . 1 = 2 (mod 5). 8

  ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

Applications of Congruences

  • Hashing Functions • Pseudorandom Numbers • Cryptology

  Hashing Functions: Hashing Functions: Question: The central computer at Riyad Bank maintains

  records (Bank account) for each of its customers. How can memory locations be assigned so that customer records can be retrieved quickly?

  Solution: The solution to this problem is to use a suitably

  chosen Hashing Function . Customer records are indentified using a Key , which will be called Hash Key , which uniquely identify each customer’s Bank account record. In this case Bank account number will be the key . ICS 9

  ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

Applications of Congruences

  In general term a hashing function h assigns memory location h(k) to a record that has k as its key. There are many hashing functions. One of the most common is the function- function , where m is the number of where m is the number of

  h(k) = k mod m h(k) = k mod m available memory locations.

  To find h(k) we only compute the remainder when k is divided by m.

  suppose m=111, the record of the customer

Example:

  with Bank Account number 064212848 is h(064212848) = 064212848 mod 111 = 14 h(037149212) = 037149212 mod 111 = ?

  h(037149212) = 037149212 mod 111 = 65 ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

  10 Applications of Congruences

  Randomly chosen numbers are often needed for computer simulations. Its also useful in cryptography. The numbers generated by systematic methods are not truly random they are called methods are not truly random, they are called pseudorandom numbers.

Pseudorandom Numbers:

  m

  , multiplier

  a

  , increment c and seed x with 2 ≤ a < m, 0 ≤ c < m, d 0

  ≤ < Th W t f and 0

  ≤ x < m. Then We can generate a sequence of pseudorandom numbers {x n }, with 0

  It is a most commonly used method to generate Pseudorandom Numbers. Let us Consider four integers: the modulus

Linear Congruential Method:

  n < m for all n, by

  successfully using the congruence x n+1 = ( ax n + c) mod m. 11 ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

  Applications of Congruences

  Consider m = 9, a = 7, c = 4, and x = 3, then we can generate the sequence of pseudorandom numbers as by using

Example:

  x n+1 = ( ax n + c) mod m.

  ≤ x

Solution: x

  3 = ( 7x

  6 + 4 ) mod 9 = ( 7. 0 + 4 ) mod 9 = 4 mod 9 = 4 x

  1 = ( 7x + 4 ) mod 9 = ( 7. 3 + 4 ) mod 9 = 25 mod 9 = 7 x

  2 = ( 7x

  1 + 4 ) mod 9 = ( 7. 7 + 4 ) mod 9 = 53 mod 9 = 8 x

  8 + 4 ) mod 9 = ( 7. 5 + 4 ) mod 9 = 39 mod 9 = 3

  9 = ( 7x

  7 + 4 ) mod 9 = ( 7. 4 + 4 ) mod 9 = 32 mod 9 = 5 x

  8 = ( 7x

  7 = ( 7x

  4 = ( 7x

  5 + 4 ) mod 9 = ( 7. 2 + 4 ) mod 9 = 18 mod 9 = 0 x

  6 = ( 7x

  12 x

  ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

  4 + 4 ) mod 9 = ( 7. 1 + 4 ) mod 9 = 11 mod 9 = 2

  5 = ( 7x

  3 + 4 ) mod 9 = ( 7. 6 + 4 ) mod 9 = 46 mod 9 = 1 x

  2 + 4 ) mod 9 = ( 7. 8 + 4 ) mod 9 = 60 mod 9 = 6 x

  • Most computers do use linear congruential generators to generate pseudorandom numbers.
  • Most computers do use linear congruential
  • A Linear congruential generator with increment c = 0 is used, such a generator is called a pure

  2

  ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

  letter  X is sent to A.   

  y Example: using  this scheme the letter B is sent to E and the 

  secret  by shifting each letter three letter forward in the  alphabet  (sending the last three letters of the alphabet to the  first).  

   C  (R  E )  H   d     by   Julius Caesar (Roman Emperor) .  He made messages 

  Computer  Science. One of the most important application of  congruences involves  cryptology. It is the study of secret  messages.  One of the earliest known uses of cryptology was  b   J li

  Applications of Congruences y Cryptology: Congruences have  many applications in 

  31 −

  2

  2 7 =

  Applications of Congruences In this example, x9 = x0 and because each term depend on the previous term. This sequence contains nine different numbers before repeating.

  16

  807 ,

  13 and multiplier is widely used. With these values, it can be shown that numbers are generated before repetition begins.

  2 7 ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

  16

  − 807

  31

  2

  1

  multiplicative generator . For example, the pure multiplicative generator with modulus d l i li i id l d Wi h

  14 Applications of Congruences y Encryption Scheme:

  A A B B C C D D E E F F G G H H

I I J J

  1

  2

  3

  4

  5

  6

  7

  8

  9 K L M N O P Q R S T 10 11 12 13 14 15 16 17 18 19 U

V W X Y Z 20 21 22 23 24 25

  ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

  15 Applications of Congruences y Therefore Caesar encryption method can be represented mathematically as mathematically as y f(p) = ( p + 3) mod 26 y Function f that assigns to the nonnegative integers p, p

  ≤ 25, the Integer f(p) in the set of { }.

  0,1,2,3,4,……25 y In the encrypted version of message, the letter represented by p In the encrypted version of message, the letter represented by p is replaced with the letter represented by (p + 3) mod 26

  ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

  16 Applications of Congruences y What is the secret message produced from the

Example 9:

  message using f(p) = ( p + 3)

  “MEET YOU IN THE PARK” mod 26 method of encryption. y First replace the letters in the message with

Solution:

  numbers. This produces

  12

  4

  4

  19

  24

  14

  20

  8

  y

  13 19 7 4

  15

  17

  10

  y Now replace each of these numbers p by f(p) = (p+3) mod 26

  15 15 7

  7

  7

  7

  22

  22

  1

  1

  17 17 23 11 16 22 23 11 16 22 10 7 18 3 20 13

  y Translating this back to letters produces the encrypted

  message-

  “PHHW BRX LQ WKH SDUN “ ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

  17 Applications of Congruences y To  recover the original message from the secret message use 

  the  function

  1 − ( ( ) ) ( (

  3 3 ) ) f f p p = = p p − Mod 26 Mod  26 y The  process of determining the original message from the  encrypted  message is called  decryption . y We  can write in general forms

  ( ( ) ) ( ( ) ) f f p p p p k k Mod Mod 26  26

  = + + y

  1 −

  Mod  26

  ( ) ( ) f p = pk y ICS

  18 ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail.  Applications of Congruences y It is obvious, that Caesar’s method does not provide a high

  l level of security. There are various ways to enhance this l f it Th i t h thi method. One approach that slightly enhances the security is to use a function of the form;

  y f(p) = ( ap + b ) mod 26 y y Where a and b are integers, chosen such that f is a Where a and b are integers chosen such that f is a bijection (one-to-one mapping) . Such mapping is called .

Affine Transformation

  ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

  19 Applications of Congruences y Example 10: What letter replaces the letter k when the

  function f(p) function f(p) = ( 7p + 3) mod 26 is used for encryption? ( 7p + 3) mod 26 is used for encryption? First note that 10 represent K. Then, using

  y Solution: y f(p) = ( 7p + 3) mod 26 y f(10) = ( 7.10 + 3) mod 26 = 73 mod 26 = 21 y Because 21 represents V, therefore K is replaced by V

  in the encrypted message. ICS

  20 ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

Home  Work

  y Question 1: Decide  where each of these integers is congruent to  

   mod 17 .

   

  a)

  80       b)  141    c)  39     d) 103      e)  47    f) 125      g) 225       h) 345 a)

  80       b)  141    c)  39     d) 103      e)  47    f) 125      g) 225       h) 345 y Question 2: Which  memory locations are assigned by the hashing  function  h(k) = k mod 101 to records of the following customer  having  ID a)

  104578690       b) 432222187     c) 372201919    d)  501338753 y

  Question 3:  A  parking lot has 31 visitors spaces, numbered from 0 to  30.  Visitors are assigned parking spaces using the hashing function  y h(k)

     k  d       h  k i  th   b  f d  f  th  fi t th   y h(k)

   = k mod 31  ,  where k is the number formed from the first three  digits  on the visitor’s license  plate. Which spaces are assigned by the  hashing  function to cars that have these first three digits on their  plates? y 317,

   918, 007, 100, 111, and 310 y ICS

  ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

  21 Home   work y

  Question 4:  What  sequence of pseuorandom numbers generated  using  the linear congruential generator    x n+1 = ( 4 x n

  • +1 ) mod 7  with  

   sequence of pseuorandom numbers generated  Q

  5 q p g using  the multiplicative  generator   x n+1 = ( 3 x n ) mod 11  with  seed  x

  = 2  ? y Question 6: Encrypt

   the message “WELCOME TO HAIL”  by  translating  the letters into numbers, applying the encryption functions  given,

   and then translating the numbers back into letters.

  a) f(p)  = ( p + 3 ) mod 26              b)  f(p) = ( p + 13 ) mod 26  y c)  f(p) = ( 3p + 7 ) mod 26 f

  ) ) 3 ( ( 1 − = − p p f

  ) (p) ( 3p 7 ) y

  Question 7:  Decrypt  these messages   using y

  a)  EOXH   MHDQV      b)  WHVW    WRGDB     c)  HDW  GLP  VXP 

  ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail. 

  22 ) ) 3 ( (

  

− = p p f

  seed   x = 3  ? y Question 5:   What

  Thank you for your Attention.

  23 ICS ‐252:  Dr. Salah Omer, Assistant Professor, CSSE, University  of  Hail.