 Hey all, Mr. Gibson here with another lesson in cryptography. Today we're going to be taking a look at modular arithmetic, and this idea of modular arithmetic really all comes down to congruency. So in the last two lessons on substitution ciphers, we've kind of introduced this idea of wrapping around the letters. So with the keyword cipher, when our remaining alphabet after the keyword reached Z, we've wrapped the letters back around to A. With the Caesar cipher, when we added the key to our numerical value, if the number got to be bigger than 25, we had to find a way to wrap it around and find an equivalent number between zero and 25. Likewise when we were deciphering using the Caesar, we might have had a value come up that was less than zero, and we had to add 26 back on to find an equivalent. So in general, this idea of wrapping around numerically comes down to modular arithmetic. So there's some notation we're going to introduce here. So when we said in the previous video that 27 was the same thing as 1 when we do a wrap around assuming our alphabet had 26 letters, we could use this 3 bar equal sign to say that 27 is equivalent to the number 1 in a modulus of 26. So this is kind of shorthand for the word modulus, where the actual modulus we're talking about is 27. And we're not always going to be working in the volume of 26. We happen to work in that one a lot because we have 26 characters, but we'll see later in the course, we might work on a mod of 64, if we have 64 characters, that would include uppercase, lowercase, all 10 digits, and two punctuation characters. That might be a better alphabet for us to work in. So we kind of want to figure out how can we generalize this wrapping around for modulus beyond 26. So we'll give a few examples here. So another example here could be that 5 is equivalent to 2 when we're working in mod 3. Again, if you think about if we're in mod 3, that means we've only got really three numbers to work with, 0, 1, and 2. So if I were to start counting along that number line, I would start out with 0, 1, 2, 3, 4, 5. So we can see that 2 and 5 are equivalent, if I keep counting, 6, 7, and 8. 8 is equivalent to 5, which is also equivalent to 2, 9, 10, 11. So 11 is equivalent to 8, which is equivalent to 5, which is equivalent to 2. We can call that 2 is kind of the primitive or the root of all of these equivalent numbers in mod 3. A nice trick to check if two numbers are equivalent in the same mod is if you take their difference. So if I take the difference between 5 and 2 to get 3, if this number is divisible by your modulus, that means those two numbers are equivalent. So I'll give another example here. We can check to see if 11 is congruent to 2 in mod 3. 11 minus 2 is 9, and 9 is divisible by 3. So yes, 2 and 11 are equivalent, or sometimes we'll call that congruent. And we could have flipped the order around. But it'd be just as easy to say that 2 is congruent to 11 in mod 3. We also have this idea of modulo, as opposed to modulus, whereas modulo is kind of an operation. You can think about the word modulo as a verb. So if I wanted to reduce a number, so I want to reduce the number 15 to its lowest value, that it's equivalent to in mod 7, we would call that we're going to apply the modulo operation to 15, and we're going to reduce it by the modulus of 7. So how could we do that? It's a common question, trying to find an equivalent reduction. So we could do some trial and error here. I could think about what number could I subtract off of 15 to make it evenly divisible by 7? That's a lot of guesswork. So we could do that. We might be able to come up with maybe 8. 8 is certainly true, but 8 is actually not in the modulus of 7. We saw to be in a modulus, that number needs to be between 0 and 1 less than the modulus itself. So I'm looking for a number between 0 and 6 inclusive. And it turns out in this case, the other equivalent value is 1. So that's some trial and error, and we eventually got there. But another way to go about doing that, maybe this is easier, is if you'll notice that the pattern between the number we started with 15 and our ultimate reduction value of 1, is that they're all just differences of 7. So if you start with a large number, we're going to go even larger here, of 131, and I want to know what is that equivalent to mod 10. Again, I can sit here and do some trial and error, or I can just sit here and repeatedly subtract 10 off until ultimately I get down to a value that's between 0 and 9, and boom, there it is. And by luck, we happen to figure out that 131 is equivalent to 1 mod 10. Luck that we had 1 again, not luck that we got the correct answer. So we've got two ways to think about it. We've got this trial and error situation, and we have this repeated subtraction. I'll give you one more way that we can find a congruent number in order to be given modulus. So let's say we've got 59, and I want to know what's congruent to 59 in the modulus of 8. So again, we could do some trial and error here. We could repeat and subtract off 8, but here's another way to think about it. Let's actually divide 59 by 8. When we divide 59 by 8, it doesn't go in evenly, but it does go in seven times, and that'll give us a product of 56, meaning we have a remainder of 3. So to reiterate, one way to write the number of 59 is 7 times 8 plus our remainder of 3. What this does is it's similar to our subtraction method. It tells us that we would have had to subtract off 8 seven times to get to a number between 0 and 7, and that number we would have had left over once we subtracted off 8 seven times was 3. So it's kind of a nice shortcut for us to figure out that our answer is 3. Thinking ahead to how we might do this in Python. Python has a nice operator, floor division. That's a quick, easy way for you to figure out how many times 8 goes into 59. So that double division symbol, it also has a modulo operator of the percent symbol. So when you do 59 modulo 8, it'll give you the remainder, which is the reduction or the equivalent number of 59 in that given modulus. So these two operations are going to be pretty helpful for us when we start programming these substitution ciphers, where we're going to need to find equivalent numbers in given moduluses or reducing, another way to put that is reducing a number to its equivalent in a given modulus. These two operations, especially that percent sign modulo operator, are going to be really helpful and essential to our success to these operations. That's a good grounding in some basic modular arithmetic. There'll be a few other videos that might go into a little bit more depth, but this should be sufficient for us to figure out how we're going to operate with the remaining substitution ciphers of multiplicative ciphers and the affine cipher. That'll be it for now. Thanks for watching, and we'll catch you on the next one.