#14MediumCryptography· Modular Exponentiation
Diffie-Hellman Key Agreement
Input format
p: prime modulus, g: generator, a: Alice's private exponent, b: Bob's private exponent
Output format
Integer: the shared secret `g^(a*b) mod p`
Examples
Input: p = 23, g = 5, a = 6, b = 15
Output: 2
A = 5^6 mod 23 = 8 and B = 5^15 mod 23 = 19. Alice computes 19^6 mod 23 = 2 and Bob computes 8^15 mod 23 = 2.
Input: p = 101, g = 2, a = 50, b = 2
Output: 1
The secret is 2^100 mod 101. By Fermat's little theorem 2^100 = 1 (mod 101).
Constraints
- 3 <= p <= 2^31 - 1, and p is prime
- 2 <= g <= p - 1
- 1 <= a, b <= 10^9
Study first
- #cryptography
- #math
- #modular-exponentiation
- #number-theory
