Modular-Exponentiation(a, b, n) c = 0 d = 1 let be binary encoding of b for i = k downto 0 c = 2c d = (d*d) mod n if bi = 1 then c = c + 1 d = (d*a) mod n return d
Modular-Exponentiation(a=5, b=501, n=6) b = 111110101 i = 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 c = 0 1 | 2 3 | 6 7 | 14 15 | 30 31 | 62 - | 124 125 | 250 - | 500 501 d = 1 5 | 1 5 | 1 5 | 1 5 | 1 5 | 1 - | 1 5 | 1 - | 1 5