 So we know about divisors, or divisors of a number, sometimes the other one we call it is the factors. We can take a number and find its factors. Greatest common divisor. New concept we introduced and we'll see used, a bit more relatively prime, two numbers. So given two numbers, I ask you, is 7, are 7 and 12 relatively prime? Well, so a pair of numbers, 7 and 12. If the greatest common divisor of those two numbers is one, then we say those two numbers are relatively prime. We'll use that concept as we go through. We know about prime numbers. I think you know about prime numbers. And note that any integer can be factored into primes. That is, can be made up as multiplying primes together. So given some integer, often we can try and find the prime factors. That is, given an integer, find the primes that when we multiply them together we get that integer. So find the prime factors of a number, and that will become important as we go through some primes. And then we started to introduce not normal arithmetic, but modular arithmetic, everything mod n. Where the answers are always within our set of 0 up to n minus 1 when we mod n, called the set zn. And we went through addition. And addition in modular arithmetic conceptually is easy. It's just the same as normal addition, but you've mod the answer by n. We didn't come to that. Then we can talk about an additive inverse of a number. Add two numbers together, and if you get 0 in mod n, then we say one is the additive inverse of the other. 3 plus 7 mod 10 equals 0. Therefore 7 is the additive inverse of 3, and 3 is the additive inverse of 7. And subtraction simply becomes addition by adding the additive inverse of the number we're trying to subtract. We'll come back to some examples in a moment, just summarize the concepts. Multiplication in modular arithmetic is easy conceptually. We just multiply the numbers and mod by n. And we also have the concept of a multiplicative inverse. If you multiply two numbers together and you get 1, and then we say that those two numbers are the multiplicative inverse of each other. a times b, if it's equal to 1 in mod n, then b is a multiplicative inverse of a and vice versa. Every number has an additive inverse. We can always find some number and add another number such that we'll get 0 in mod n. So every number has an additive inverse, but not every number has a multiplicative inverse. There are some numbers in our set that we cannot multiply by any other number and mod by n and get 1 as the answer. So some numbers don't have a multiplicative inverse, and it turns out that some number a will have a multiplicative inverse in mod n if a is relatively prime to n. So if the modulus is n, some integer a, if a and n are relatively prime, meaning the greatest common divisor of a and n is 1, then a will have a multiplicative inverse, and we can find that value. Otherwise it will not. And then we'll see that division is simply multiplication of the multiplicative inverse. Traction is the addition of the additive inverse. Division is the multiplication of the multiplicative inverse. And then we'll move on to a few more concepts. So just continue with the examples to demonstrate all these just to make sure everyone's clear. Let's go through maybe some different ones from yesterday. In mod 8, as an example, everything mod 8. So I will not write the mod 8 just to be brief. What numbers have a multiplicative inverse in mod 8? So the numbers, let's list them. What's the multiplicative inverse of 1 in mod 8? We multiply 1 by some number if the answer is 1 when we mod by 8, then that is its inverse. So we'd say if that's the integer a, then the multiplicative inverse of a for 1 is 1. 1 times 1 mod 8 is 1. What's the multiplicative inverse of 2? 2 times some number mod 8 equals 1. There is no such number. How do we know? How can we check? 2 and 8 are not relatively prime. The greatest common divisor of 2 and 8 is 2. There needs to be 1 for it to be relatively prime and it needs to be 1 for them to have a multiplicative inverse. So there is no inverse of 2. So I'll just note this is a cross. 3, does 3 have a multiplicative inverse? They are 3 and 8 relatively prime. The greatest common divisor of 3 and 8 is 1. Yes, they are relatively prime. Therefore, yes, 3 does have a multiplicative inverse because the greatest common divisor of 3 and 8 is 1. The divisors of 3 are 1 and 3 of 8, 1, 2, 4, 8. The greatest common value is 1. Therefore we can say that 3 and 8 are relatively prime. 3 and 8 are relatively prime. Not prime, relatively prime. And that means that 3 does have a multiplicative inverse. What is its value? 3 times something equals 1. 3 times 3 equals 1 in mod 8. 3 times 3 mod 8 is 1. So the multiplicative inverse of 3 is 3. And then do so for the remaining numbers, 4, 5, 6, 7. Find their multiplicative inverse. Or if they don't have one, note that. So check. Does the integer have an inverse? If so, find the value. 4 and 8 relatively prime. 4 and 8 have a greatest common divisor of 4. Therefore 4 and 8 are not relatively prime and therefore 4 does not have an inverse in mod 8. So there's no inverse of 4. Are 5 and 8 relatively prime? Yes, because the greatest common divisor of 5 and 8 is 1. Divisors of 5, 5 is a prime number. And 8 is not a model of 5. So yes, it does have a multiplicative inverse. What is its value? 5 times something equals 1 mod 8. Turns out it's actually itself. 5 times 5 is 25. Mod 8 is 1, because 3 times 8 is 24. 6, no inverse. 7 is 7 relatively prime with 8. Yes, greatest common divisor of 7 and 8 is 1. So what is the inverse of 7? Yes, it's in fact 7. 7 times 7 is 49. Mod 8 is 1. 6 times 8 is 48. So in this case, when we have mod 8, the numbers 1, 3, 5 and 7 have an inverse. And in this case, but not all cases, just in this example, they're actually inverses of themselves. We'll see some other examples later that they don't have to be the inverse of themselves. So it doesn't have to be 3 and 3, just in mod 8 it is. So if we know the inverse, we can do division. All in mod 8, what is 2 divided by 3? And remember, when we're using modular arithmetic, the answers are always in the set 0 up to n minus 1. So what's 2 divided by 3? 6, check. Yes, correct. Everyone see if they can work out why at 6. 2 divided by 3 is 6. Division, in our normal arithmetic, what is division? When we have 2 divided by 3, it's the same as 2 times 1 over 3. 2 times the inverse of 3 in normal arithmetic. The same concept here. Division is multiplication of the multiplicative inverse. It was 2 times, to note at MI, the multiplicative inverse of 3. And we just worked that out. In mod 8, the multiplicative inverse of 3 is in fact 3. Still in mod 8, 2 divided by 4. 2 divided by 4 is... There is no answer. Again, for division, we multiply by the multiplicative inverse. So multiply by the multiplicative inverse of 4, but there is no such value. We just worked out before that we cannot multiply 4 by a number and get 1. 4 does not have an inverse. So we cannot divide by 4 in mod 8. So we cannot do that. It doesn't run across. We can't solve that. So with modular arithmetic, we can only divide by numbers that have an inverse. Which are the numbers which are relatively prime with the modulus. So we've gone through the four basic operations in arithmetic. We'll see two more in a moment. Two extensions. Similar, if we go back to one slide, similar in normal arithmetic, there is a number of properties or rules that apply that can simplify calculations. So a number of laws. And they are listed here. You don't have to remember them. But they are effectively the same as in normal arithmetic. W plus x mod n is the same as x plus w mod n. W plus x plus y is the same as w plus x plus y or mod n. So this is normal arithmetic laws. W times x plus y mod n is the same as w times x plus w times y mod n. So this is nothing new compared to our normal arithmetic. And these ones at the top that I've listed are useful as well. And we'll see that they become very useful in some of the operations we perform in cryptography. Or at least the ones we will see. Effectively, we can expand or contract. So it depends on which way we look at it. A mod n plus B mod n all by all mod n. So the answer of this mod n is the same as simply A plus B mod n. Similar with subtraction and multiplication. And we often use that if, for example, we have multiplication A times B mod n, we can break it out into smaller values to mod by n. Let's see that. What is 160 mod 8? Without a calculator, well, you can go through and work out. Let's do it the long way and solve it in the long approach. We can use these properties. This one, A times B mod n is the same as A mod n times B mod n all mod n. So we have 160 mod 8. Let's find two factors of 160. A and B. And then mod those values to simplify the numbers we're dealing with. What are two factors of 160? There are multiple factors, but two numbers multiply together to get 160. Easy ones. 10. Yours is not easy. 10 and 16. 10 times 16, 160, which is, and I'll just move over here, is the same as using our laws, 10 mod 8 times 16 mod 8 all mod 8. Looks complex, but in fact, what do we end up with? 10 mod 8, easy. The remainder is 2 times 16 mod 8. What do we get? 0 mod 8 is 0, 2 times 0, 0 mod 8. So although there's no need to do it in this case, you can solve directly. The idea is that once we have big numbers, not 160, but a very large number, then to solve it, we can break that large number into its factors and solve the modulus of those factors faster. And in fact, we can have computer algorithms that will do that for us. If we know the factors of the number, we can find the modulus of the factor, multiply them together, and mod by 8 at the end, mod by n. We'll see that come in play in a moment once we look at exponentials. So four operations so far. Addition, subtraction, multiplication, division. Next, we've got two more operators. Let's go to them. We'll come back to these theorems after these operators. The next two operators, exponentiation and logarithms, which are really just extensions of multiplication and division. Exponentiation is just multiplying multiple times. So A to the power of B is just A times B, where we have B instances of A, A times A times A, B times. And logarithms, the inverse of exponentiation. So first, exponentiation is quite easy. It's the same concept as we use in normal arithmetic. Then we just use our normal arithmetic. So 2 to the power of 3 is 8, mod 7 is 1. Easy, so exponentiation is easy. Solve this one. No calculators. 11 to the power of 7, mod 13. Bonus, one mark for the quiz if someone can do it quickly. No calculators. 12. Come and show me. You ready to show me? How do you solve it? 11 to the power of 7, mod 13. 9 maybe? We want to know the process to solve it. First approach is the normal approach. 11 times 11 is 121 times 11 again. We get whatever it is. My brain's not turned on today. It won't calculate that. So times 11, 7 times, you'll get some big number. Do that in your head, not so easy. With a calculator, easy. Let's do it in our head. So one way is to use the property that we just saw is that remember, exponentiation is just multiplication, multiple times. Let's break this into... There's different ways to do it. Break this into... We'll see why in a minute. 11 to the power of 7 is the same as 11 to the power of 4 times 11 to the power of 2 times 11. That is the exponents 4 plus 2 plus 1 here. I'll even write it, 7. That's a normal property of exponentiation. How does it help us? Well, using... Just going back, we're going to try and use this property here, the one that we used in the example before, that multiplying two numbers together, mod n, we can expand that to be a mod n times b mod n or mod n. Now we have three numbers multiplied together. We can expand them using this property or rule. So it becomes... Actually, let's... What's 11 to the power of 4? So why did I do this? My brain can only... I can think of 11 to the power of 1 is 11. 11 to the power of 2 is 121. 11 to the power of 3, I don't know. All right, I could calculate it, but I don't know off the top of my head. So let's try and deal and keep the numbers small to values that I can use in my head. So let's try and break this into numbers lower than 11 to the power of 2. 11 to the power of 4, I don't know what it is. 11 to the power of 2, I know, is 121. But 11 to the power of 4 is, in fact, 11 squared squared. Again, that's nothing new. That's just the property of exponentiation. 11 squared, I know. It's 121. All mod 13. Nothing special yet. Then we use our property that... We had a property a times b mod 13 is the same as a mod 13 times b mod 13, all mod 13. And we can extend that when we have effectively a times b times c mod 13. So we get 121 squared mod 13. 121 mod 13 times 11 mod 13. All mod 13. And now I can start to solve some in my head at least. What's 11 mod 13? That one's 11. What's 121 mod 13? Anyone solve it in the head? 4. Okay, two people set up. Let's trust them. 130 times 10... 13 times 10 is 130. Minus 9 is 11. So 13 minus 9 is 4. And 121 squared mod 13. 4 squared mod 13. Okay? 121 mod 13 is 4. So 121 squared mod 13 would be... If we apply the same logic here of expanding would be 4 squared mod 13. So we've solved 121 mod 13. This becomes 4 squared mod 13. Because 121 mod 13 is 4. All mod 13. And 4 squared mod 13 is 3. Again, how did we do that step? Okay, from this step we've got I think A times B times C mod 13. Our rules say that we can split that up to be A mod 13 times B mod 13 times C mod 13 or mod 13. Then, okay, 11 mod 13, 11. 121 mod 13. We use our brain. 4. 121 squared mod 13. In fact, we've applied this same rule again. 121 squared mod 13. 121 mod 13 times 121 mod 13 or mod 13 is 4 squared mod 13. And what do we get? 3 times 4 times 11. 77. Sorry, yeah, not add. 12 times 11. 132 mod 13. Which is an easy one. Alright. So we've started with what? 11 to the power of 7 mod 13. And we can manually go through the steps and use this, especially this property of expanding and get the answer of 2. Of course, we don't have to do this all the time. We have calculators to do this. But even with a calculator when you have large numbers not 11 to the power of 7 a 10-digit number to the power of a 5-digit number some numbers that don't are not handled on your calculator or even a very, very large number which take a long time to calculate on a computer we can have an algorithm that would do this type of steps for us. It speeds up the computation. So software can be used to implement these steps to speed up the computation when we're doing exponentiation in modular arithmetic. You can check. I calculated before 11 to the power of 7 7. So in general with exponentiation we can apply these rules and be faster than calculating direct exponentiation. That's the point. So we're getting 11 to the power of 7 first and getting this and then mod by 13 when we have large numbers it's faster to instead of calculating 11 to the power of 7 first then break it into smaller numbers and mod by a number our modules. My calculator will do it direct. I hope. Small numbers with large numbers using these rules can speed up the calculation. Large numbers we'll see we're starting to deal with hundreds of digits. Not 10 digits not 20 numbers digits long but hundreds of digits in length. What's next? That's exponentiation raising a number to the exponent or to a power. Rather simple in modular arithmetic but we've got rules to speed up the calculation. We'll return a little bit later to logarithms. Logarithms are just the inverse of exponentiation but it gets a bit more complex. Before we go through logarithms there are some theorems that people have developed that combine some of these concepts together that apply when we use modular arithmetic and in fact we'll use these later when we do encryption. So we're going to use a lot of this theory later in the next topic. Fermat's theorem actually can be written in two ways and we're not going to try and prove the theorems we're just going to accept that they're true someone's done that and come up with them but we'll use them when necessary. Fermat's theorem the first form so it's the same theorem but just written in two different forms if p is prime if we have prime number p and some positive integer a which is not divisible by that prime p then it holds that a to the power of p minus one is equivalent to one when we mod by p so that's Fermat's theorem it can be modified or written in a different form depending on what you want to do with it to this second form here if p is prime and a is any positive integer then it holds that a to the power of p is equivalent to a in mod p so we can use that if we have some statement a to the power of p a to the power of p mod p assuming p is prime then we immediately know the answer is a that's what Fermat's theorem tells us and let's accept that but just show a quick example to demonstrate that it does indeed hold a what is three to the power of five in mod five what is three to the power of five in mod five too slow the answer if you use Fermat's theorem you should see is three so let's check so we don't need to manually calculate three to the power of five we could and we will in a moment but no Fermat's theorem says that if p is prime the exponent is prime and it's the same as the modulus then take any positive integer a raised to p mod p is a so in our example we have three to the power of five a is three p is five five is the prime number so in our case the general form is that's assuming p is prime well we have p equal to five that's a prime number a is three so it fits the form of Fermat's theorem which means a to the power of p is the same as a when we mod by p three to the power of five when we mod by five is the same as three note that we just this is different ways to write the modulus this writing in brackets here means both sides are mod by p we're using mod p as the arithmetic so a to the power of p mod p equals a that's what Fermat's theorem tells us if p is prime and that's true in our case so we can use that again as a shortcut to find the solution when we've gotten integers raised to a prime power that prime that's just a simple demonstration of Fermat's theorem you can check with other prime numbers and see that it does hold what is three to the power of five three to the power of five three times three is nine times three is twenty seven times three is eighty one times three is two hundred and forty three three to the power of five equals two hundred and forty three two hundred and forty three two hundred and forty three two hundred and forty three mod five equals three mod five two hundred and forty mod five is zero so we have a remainder of three so it's true in this case and if you try all values that p is prime then you'll find that that's true we often use, we'll see that we'll use the second form of this theorem in cryptography and we'll see now another theorem Euler's theorem but before that we need to introduce Euler's totient function phi of n is the notation the totient function of some number n returns the count positive integers that are less than n and relatively prime with n or this totient function so the totient of n we look at the integers from one up to n minus one we determine which ones of them are relatively prime with n and count those ones count the number that are relatively prime and that's the answer, the count of them we'll see some properties of that in a moment some examples of that what is Euler's totient of eight well let's go the full way to determine it we look at the numbers from one up until seven check if they're relatively prime to eight and then count how many are so let's list them is one relatively prime with eight, yes one and eight, greatest common divisor is one so yes two and eight, relatively prime no three and eight yes four and eight are not because they have a divisor of four five and eight yes six and eight are not because they have a divisor of two and seven and eight yes so the answer is four so the totient Euler's totient of eight is four it's the number of numbers less than eight which are relatively prime with eight the count of numbers let's quickly determine the totient of nine Euler's totient of nine maybe, sounds good try and find yourself the totient of nine so the long way to solve is to find the numbers from one up until eight one check whether they're relatively prime with nine and then count how many are one up until eight are they relatively prime with nine one is relatively prime with every number because the greatest common divisor of one and that number is always one so yes two and nine, relatively prime yes or no hands up for no alright good well done three and nine have a divisor greater than one so no four and nine greatest common divisor of one five and nine greatest common divisor of one six and nine greatest common divisor of three seven and nine one eight and nine we have six numbers less than nine which are relatively prime with nine of nine is six easy keep going totient of 23 well I don't want you to go and try all numbers one up to 22 of course you can, it won't take long but we'll start to identify some patterns in this case let's come back to 23, let's do an easier one, what's the totient of five we can do that one one two three four one and five relatively prime two and five, relatively prime three and five greatest common divisor of one so it's four and five it's four, what's the shortcut five is a prime number by definition all numbers less than five will be relatively prime with five because five being a prime number its factors are one and five so any number less than five has a greatest common divisor with five of one only so the Euler's totient of a prime number is that prime number minus one there are four numbers less than five relatively prime with five so if you can identify the number as a prime number you immediately know that the answer 23 is a prime number the totient will be 22 okay so there's the first shortcut that's useful the totient of a prime number is that number minus one the totient of one is one for prime p the totient of p is p minus one and you can see a small extension of that and it will become useful later for primes p and q where n equals p times q the totient of n e so n equals p times q the totient of n equals the totient of p times q equals the totient of p times the totient of q equals p minus one times q minus one so that's the case where p and q at prime numbers multiply together there are others but this last one will make heavy use of later when we see in cryptography what's the totient of 77 again 16 no 6-0 go and write the numbers one up to 76 check which are relatively prime with 77 yeah you'll find the answer but too slow what do we know 77 it's factors or it's prime factors that is the primes that we multiply together to get 77 7 and 11 and our property is that it turns out that it equals the totient of 7 times the totient of 11 and 7 and 11 are prime numbers so the totient of the prime number is that number minus one, the totient of 7 is 6 the totient of 11 is 10 so the answer is 60 so that's using the this property that the totient of a prime is p-1 and also the multiplication of those two primes we can expand to get p-1 times q-1 now that step required me to factor 77 into two primes so that was the first jump I had to make to see 77 is actually 7 times 11 and 7 and 11 are both prime numbers so factoring into the primes was needed there if we can do that we can quickly solve the totient if we can't do that then it takes longer to solve the totient if we go through the manual steps of 1 up to 76 and it also applies as we go to longer numbers that's Euler's totient function and related to that, Euler's theorem again written in two forms let's just focus on the second form because we'll see that in being used when we look at cryptography for positive integers a and n a to the power of the totient of n plus 1 mod n equals a so Euler's theorem tells us that so if we can set some value in this form then we can quickly find the mod modulus the mod n, let's see an example what's the answer try and solve this one 4362 to the power of 61 all mod 77 no calculator allowed a common quiz or exam question maybe a quiz question well no calculator allowed cannot solve it by hand the idea is to think do one of our theorems help in this case so far we've introduced two theorems Fermat's theorem and Euler's theorem does this statement or question match the format of one of those theorems and Euler's theorem here says for two integers mod n if a to the power of the totient of n plus 1 mod n equals a well let's check does it match that format we'll write that so Euler's theorem tells us that if we have a to the totient of n plus 1 mod n well we just solved does this fit this form well if n is 77 we found before the totient of n just above was 60 so the totient of n plus 1 is 61 so in fact it does match this form some number to the power of the totient of 77 that is 61 mod 77 the answer is that original number 4362 to the power of 61 is 4362 to the power of the totient of 77 plus 1 mod 77 running out of space because the totient of 77 we calculated to be 60 which matches Euler's theorem's form and which tells us the answer to that is a or 4362 in our case so we can only use this theorem if we have a statement or a question that matches the correct form we can rearrange it to suit that form and in fact we'll see when we look at return to cryptography that we'll see some of our encryption algorithms take advantage of this fact the point is that 4362 to the power of 61 is a large number then mod 77 takes some time to solve but we've solved it immediately since it's in the form of this theorem we've immediately got the answer of 4362 now do it with a large number that is tens of digits hundreds of digits actually tens of digits here hundreds of digits number here and too large to calculate manually but using this theorem if it matches the form we can immediately get the answer so it's a way to speed up the calculation make it practical let's look at logarithms I know a couple of minutes left we'll introduce logarithms remember logarithms are the opposite operation of exponential in normal arithmetic and same in log in modular arithmetic so in our normal arithmetic as an example 2 to the power of 6 is 64 so the log in base 2 of 64 equals 6 that's our normal arithmetic we use the same concept in modular arithmetic I'll give you an example and then we'll calculate it later I've calculated this one before 2 to the power of 13 mod 19 we can do it in the calculator 2 to the power of 13 mod 19 2 to the power of 13 sorry mod 19 is 3 calculator there then the inverse operation is the logarithm we have the modulus we call it a discrete logarithm in modular arithmetic often written as D log the discrete logarithm the base is 2 there's a subscript here 2 but we have another subscript which is the modulus we write it as the discrete logarithm in base 2 with mod 19 of equals 13 so using the same concept of our normal arithmetic the logarithm is the inverse operation to exponentiation it's the same in modular arithmetic but we call it a discrete log the discrete log in base 2 with mod 19 of 3 equals the exponent 13 that's the way to read that and in the same way that exponentiation is just multiplying multiple times discrete logarithm we can think of as we're using division so it's related in the same way that division relies on a multiplicative inverse and not all numbers have a multiplicative inverse it follows that not all numbers can we find the discrete log of so we cannot always divide by we cannot divide by any number in modular arithmetic some numbers we cannot find the discrete logarithm of so it doesn't all it's not always solvable division is not always solvable in modular arithmetic next week we'll look at some cases where it's not solvable and the conditions when it becomes solvable and that will finish this topic on number theory and then we'll move on to public key cryptography and we'll look at an algorithm that uses a lot of these concepts exponentiation the different theorems that we introduce Euler's Totion and eventually an algorithm that uses the discrete logarithm to encrypt data so it all start to make sense when we see in a practical cryptography example for now we'll stop our lecture you'll have a homework task I'll release it either today or tomorrow on the website it will involve using some software I'll give you some instructions and it will be quite easy once you learn how to use it I'll give you detailed instructions basically you will need to decrypt some ciphertext that I give you using this software the software runs on linux or unix so if you have a mac or a computer or you have access to a linux computer you can use this software it doesn't run natively on windows alternatives you have and I'll give some details in the instructions but some alternatives will be to use the ICT server which you have accounts on or the lab computers the mac computers on the third floor all have this software available so you'll see an email with some instructions for the homework you'll have a week or a bit longer than a week to do it but just a warning that will come out before the weekend