Modular Arithmetic and the Caesar...
Transcript of Modular Arithmetic and the Caesar...
![Page 1: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/1.jpg)
ModularArithmeticandtheCaesarCipher
YanHuang
![Page 2: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/2.jpg)
Objectives
• Divisibility• PrimeandCompositeNumbers• FundamentalTheoremofArithmetic• ceiling, floor, /, mod• Caesarcipher
![Page 3: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/3.jpg)
Divisibility
• Thesetofingtegers ℤ = … , −2, −1, 0, 1, 2, … .
• ) divides* if)+ = * forsome+ ∈ ℤ.
• Wewrite)|* todenote) divides*.Wesay) isadivisorof* and* isamultiple of).
![Page 4: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/4.jpg)
Divisibility
Forall),*,. ∈ ℤ
) ), 1 ), )| 0.
![Page 5: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/5.jpg)
Divisibility
Forall),*,. ∈ ℤ
0|) ifandonlyif) = 0.
![Page 6: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/6.jpg)
Divisibility
Forall),*,. ∈ ℤ
) * ⇔ −) * ⇔ )| − *.
![Page 7: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/7.jpg)
Divisibility
Forall),*,. ∈ ℤ
) * and ) . ⇒ )|(* + .).
![Page 8: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/8.jpg)
Divisibility
Forall),*,. ∈ ℤ
) * and * . ⇒ )|..
![Page 9: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/9.jpg)
Divisibility
Forall),* ∈ ℤ
) * and * ) ⇔ ) = ±*.
![Page 10: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/10.jpg)
Primality
: isaprimeif: > 1 andhasnootherpositivedivisorbesides1 and:.
: isacompositeif: > 1 andisnotaprime.
![Page 11: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/11.jpg)
TheListofPrimes
![Page 12: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/12.jpg)
Fundamentaltheoremofarithmetic
Everynon-zerointeger: canbewrittenas
: = ±<=>? … <@
>A.
where<= < <C < ⋯ < <@ aredistinctprimesandE=, … , E@ arenon-negativeintegers.Moreover,theexpressionisunique.
![Page 13: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/13.jpg)
DivisionwithRemainder
Let), * ∈ ℤ with * > 0.ThenthereexistuniqueF, G ∈ ℤ suchthat
) = F* + G and0 ≤ G < *.
![Page 14: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/14.jpg)
Floors
Thefloor function,denotedby⌊⋅⌋,isafunctionfromrealnumbersℝ toℤ.Forevery M ∈ ℝ,⌊M⌋ isthegreatestintegerN ≤ M.
⌊M⌋ isuniquelydefinedforeveryM.
![Page 15: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/15.jpg)
Ceilings
Theceilingfunction,denotedby⌈⋅⌉,isafunctionfromrealnumbersℝ toℤ.Forevery M ∈ ℝ,⌈M⌉ isthesmallestintegerN ≥ M.
⌈M⌉ isuniquelydefinedforeveryM.
![Page 16: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/16.jpg)
Themod operator
Let), * ∈ ℤ with * > 0,) = F* + G and0 ≤ G < *.Wedefine
) mod * ≔ G
![Page 17: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/17.jpg)
Themod operator(GeneralizedDefinition)
Let), * ∈ ℤ,wedefine
) mod * ≔ ) − *⌊)/*⌋
![Page 18: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/18.jpg)
DayinaWeek
September1,2016isThursday.WhatdayisOct1,2016?
![Page 19: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/19.jpg)
MessagesEncoding&Decoding
• Percharacter:
encodeC :: Char -> IntencodeC =
decodeC :: Int -> ChardecodeC =
![Page 20: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/20.jpg)
MessagesEncoding&Decoding
• Percharacter:
encodeC :: Char -> IntencodeC ‘A’ = 0encodeC ‘B’ = 1...encodeC ‘Z’ = 25
decodeC :: Int -> ChardecodeC 0 = ‘A’decodeC 1 = ‘B’...decodeC 25 = ‘Z’
Verytediousandunscalable.Doyouhavebetterideas?
![Page 21: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/21.jpg)
MessagesEncoding&Decoding
• Dealingwithmulti-charactermessages
encode :: [Char] -> [Int]encode m = map encodeC m
encode :: [Char] -> [Int]encode = map encodeC
Point-freeform
![Page 22: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/22.jpg)
CaesarCipher(ShiftCipher)
Encryption:V = W + X mod 26
Decryption:W = V − X mod 26
TheencryptionkeyInputcharacter
![Page 23: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/23.jpg)
ImplementingCaesarCipher
caesarC :: Int -> Int -> IntcaesarC k c = (c + k) mod 26
caesar :: Int -> [Int] -> [Int]caesar = map . caesarC
![Page 24: Modular Arithmetic and the Caesar Cipherhomes.sice.indiana.edu/yh33/Teaching/I231-2016/lectures/...Caesar Cipher (Shift Cipher) Encryption: V = W +X mod 26 Decryption: W = V −X mod](https://reader036.fdocuments.net/reader036/viewer/2022071416/611313215017ee252a726835/html5/thumbnails/24.jpg)
ImplementingCaesarCipher
caesarDC :: Int -> Int -> IntcaesarDC k c = (c - k) mod 26
caesar :: Int -> [Int] -> [Int]caesar = map . caesarDC