Modular exponentiation is exponentiation performed over a modulus. It is useful in computer science, especially in the field of public-key cryptography, where it is used in both Diffie-Hellman Key Exchange and RSA public/private keys. Modular exponentiation is the remainder when an integer (the base) is raised to the power (the exponent), and divided by a positive integer (the modulus); that is, . From the definition of division, it follows that . For example, given , and , dividing by leaves a remainder of . Modular exponentiation can be performed with a negative exponent by finding the modular multiplicative inverse of modulo using the extended Euclidean algorithm. That is: , where and . Modular exponentiation is efficient to compute, even for very large integers. On the other hand, computing the modular discrete logarithm – that is, finding the exponent when given , , and – is believed to be difficult. This one-way function behavior makes modular exponentiation a candidate for use in cryptographic algorithms. Direct method The most direct method of calculating a modular exponent is to calculate directly, then to take this number modulo . Consider trying to compute , given , , and : One could use a calculator to compute 413; this comes out to 67,108,864. Taking this value modulo 497, the answer is determined to be 445. Note that is only one digit in length and that is only two digits in length, but the value is 8 digits in length. In strong cryptography, is often at least 1024 bits. Consider and , both of which are perfectly reasonable values. In this example, is 77 digits in length and is 2 digits in length, but the value is 1,304 decimal digits in length. Such calculations are possible on modern computers, but the sheer magnitude of such numbers causes the speed of calculations to slow considerably. As and increase even further to provide better security, the value becomes unwieldy. The time required to perform the exponentiation depends on the operating environment and the processor. The method described above requires multiplications to complete. Memory-efficient method Keeping the numbers smaller requires additional modular reduction operations, but the reduced size makes each operation faster, saving time (as well as memory) overall. This algorithm makes use of the identity The modified algorithm is: Set , . Increase by 1. Set . If , go to step 2. Else, contains the correct solution