next up previous
Next: Up: Previous:

Pseudocode

Modular-Exponentiation(a, b, n)
$\;\;\;\;\;$c = 0
$\;\;\;\;\;$d = 1
$\;\;\;\;\;$let \(b \;=\; <b_k b_{k-1}, \ldots, b_1, b_0>\) 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


next up previous
Next: Up: Previous: