01.01.2015 Views

Modular Arithmetic and Primality

Modular Arithmetic and Primality

Modular Arithmetic and Primality

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

function modexp (x, y, N) //Iterative version<br />

Input: Two n-bit integers x <strong>and</strong> N, an integer exponent y (arbitrarily large)<br />

Output: x y mod N<br />

if y = 0: return 1<br />

i = y; r = 1; z = x mod N<br />

while i > 0<br />

if i is odd: r = r z mod N<br />

z = z 2 mod N<br />

i = floor(i/2)<br />

return r<br />

– Same big-O complexity<br />

– Iteration can save overhead of calling stack (optimizing compilers)<br />

– Some feel that recursion is more elegant<br />

– Either way is reasonable<br />

CS 312 - Complexity Examples - <strong>Arithmetic</strong> <strong>and</strong> RSA 27

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!