I was wondering how RSA algorithm deals with such big numbers and tried one example in WolframAlpha. How can they deal with such crazy numbers?
EDIT: Just to make it more bizarre, one more example
When you need an exact answer, Wolfram Alpha is efficient, accurate, and reliable –- as long as the answer can be found in one of its databases and the question is asked correctly.
As a result, the five million lines of Mathematica code that make up Wolfram|Alpha are equivalent to many tens of millions of lines of code in a lower-level language like C, Java, or Python. Mathematica is a very tall starting point from which to begin building Wolfram|Alpha (or anything else, for that matter).
It's Wolfram|Alpha, named after its creator, Stephen Wolfram, a 49-year-old former particle physics prodigy who became bewitched by the potential of computers.
Exponential functions can be entered as ee x: Copy to clipboard.
There's a simple algorithm called exponentiation by squaring that can be used to compute ab mod c very efficiently. It's based on the observation that
a2k mod c = (ak)2 mod c
a2k + 1 mod c = a · (ak)2 mod c
Given this, you can compute ab mod c with this recursive approach:
function raiseModPower(a, b, c):
if b == 0 return 1
let d = raiseModPower(a, floor(b/2), c)
if b mod 2 = 1:
return d * d * a mod c
else
return d * d mod c
This does only O(log b) multiplications, each of which can't have any more digits in them than O(log c), so it's really fast. This is how RSA implementations raise things to powers as well. You can rewrite this to be iterative if you'd like, though I think the recursive presentation is really clean.
Once you have this algorithm, you can use standard techniques for multiplying arbitrary-precision numbers to do the computation. Since only O(log b) iterations of the multiplication are required (as opposed to, say, b iterations), it's crazy fast. You never actually end up computing ab and then modding it by c, which also keeps the number of digits low and makes it even faster.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With