 Most modern cryptographic systems require us to find a very high power of a very large number And so we might be multiplying or might need to multiply a number by itself trillions and trillions and trillions of times and This is going to take a huge amount of time to try and implement and so one way we can get around that is by using something called the fast powering algorithm and In general the idea is that I can evaluate the expression of the form a to power m mod n In the following way what I can do first of all is I can evaluate a squared a to the fourth a to the eight and in general a to power 2k mod n for all 2k that are less than the exponent that we're interested in and Then what I can do is I can construct a to the m as a product of factors of this form And then I'll multiply these and reduce mod n now One of the reasons that this is useful is that these successive powers of a are formed by taking one of these and Squaring and taking one of these and squaring and every time we square a number We double the exponents or exponents are all going to be powers of two and this innovative algorithm was invented around 200 BC in a slightly different context in India and that's a story for another day But let's go ahead and take an example here We want to find 2 to 3 to power 35 mod 37 So what I'm going to do is I'm going to evaluate 3 to power 2 to the k for any value of 2k that's less than 35 So let's start out. We'll find 3 is congruent to 3. Well, that's unexciting 3 to the second power 3 squared is congruent to 9 3 to the fourth is the preceding value squared So that's 3 squared squared. That's 9 squared. That's 81 and here's the important point If I don't reduce this this is more than the modulus if I don't reduce this I'm losing all of the good things I have with modulo arithmetic So I really want to reduce this so I have to the ability to work with smaller numbers So instead of working with 81. I'll knock that down mod 37 down to 7 and note that in my next step I'll need to square this it's much easier to square 7 than it would have been to square 81 So next step 3 to the 8th. That's the previous one squared. That's 7 squared. That's 49 Knockdown mod 37 to 12 and again 3 to the 16 12 squared 144 knockdown mod 37 down to 33 3 to the 32nd 3 to the 16 squared 33 squared 1089 knockdown to 16 and at this point if I go any further I will get 3 to the 64th, but I only need 3 to the 35 So I'm done with the as it were data collection step now What I want to do is I want to find 3 to power 35 So I'm going to select the factors whose product will give me 3 to power 35 And this is just if the term means anything to you finding the binary representation of 35 And I can do that by selecting let's see I need this one so we'll take the largest value that we need we need 3 to the 32nd We don't need the 3 to the 16 that gets too much 3 to the 8 Don't need that one if I multiply these to you together. I get too much 3 to the 40th So I'll get rid of that one 3 to the 4th still too much That would give us 3 to the 36 get rid of that. I do need this one That's 3 to the 34th and one more is 3 to the 35 So I'll multiply those three terms together That's 3 by 9 by 16 and after all the dust settles 432 knock it down mod 37 And collect the exponents 3 to power 35 congruent to 25 Well, let's take a look at another example. We want to find 4 to power 50 mod 19 Now remember that one of the nice things about working mod anything is that you never have to work with a number Larger than or even equal to the modulus you can always work with smaller numbers if we don't mind working with negative numbers We can cut that down still farther and the largest number We'll have to work with is half the size of the modulus So for example here for congruent to 4 mod 19 for squared congruent to 16 And I could work with 16, but I also note that this is actually 3 less than the modulus So instead I could work with negative 3 much smaller nicer number to work with 4 to the 4th 4 squared squared. That's negative 3 squared. That's 9 That's 81 and again. I can work with a knock that down by the modulus 4 to the 16th 5 squared 25 down to 6 4 to the 32nd 4 to the 16th squared 6 squared 36 and again this time I'll knock that down to negative 2 rather than the corresponding value 17 and again this way I can work with even smaller numbers if I should want to Now as human beings, this is important as computers It might not be quite as important to do so, but I'll pick the values that give me 4 to the 50th That's 4 to the 32nd 4 to the 16th 4 squared multiply them together. There's 4 to the 32nd 4 to the 16th 4 squared over the left-hand side I have power 50 over the right-hand side I have product negative 2 6 and negative 3 gives me 36 Knock it down mod 19 down to 17 and there's my final answer