# Discrete logarithm calculator

The discrete logarithm problem is to find the exponent in the expression *Base ^{Exponent} = Power (mod Modulus)*.

This applet works for both prime and composite moduli. The only restriction is that the base and the modulus, and the power and the modulus must be relatively prime.

In this version of the discrete logarithm calculator only the Pohlig-Hellman algorithm is implemented, so the execution time is proportional to the square root of the largest prime factor of the modulus minus 1.
The applet works in a reasonable amount of time if this factor is less than 10^{17}.

I will add the index-calculus algorithm soon. This algorithm has subexponential running time.

## Expressions

You can also enter expressions that use the following operators and parentheses:

**+**for addition**-**for subtraction*****for multiplication**/**for division**%**for remainder**^**or******for exponentiation-
**<**,**==**,**>**;**<=**,**>=**, != for comparisons. The operators return zero for false and -1 for true. -
**AND**,**OR**,**XOR**,**NOT**for binary logic. The operations are done in binary (base 2). Positive (negative) numbers are prepended with an infinite number of bits set to zero (one). -
**SHL**: When`b`≥ 0,`a`SHL`b`shifts`a`left the number of bits specified by`b`. This is equivalent to`a`× 2^{b}. Otherwise,`a`SHL`b`shifts`a`right the number of bits specified by −`b`. This is equivalent to floor(`a`/ 2^{−b}). Example: 5 SHL 3 = 40. -
**SHR**: When`b`≥ 0,`a`SHR`b`shifts`a`right the number of bits specified by`b`. This is equivalent to floor(`a`/ 2^{b}). Otherwise,`a`SHR`b`shifts`a`left the number of bits specified by −`b`. This is equivalent to`a`× 2^{−b}. Example: -19 SHR 2 = -5. -
**n!**: factorial (`n`must be greater than or equal to zero). Example: 6! = 6 × 5 × 4 × 3 × 2 = 720. -
**p#**: primorial (product of all primes less or equal than`p`). Example: 12# = 11 × 7 × 5 × 3 × 2 = 2310. -
**B(n)**: Previous probable prime before*n*. Example: B(24) = 23. -
**F(n)**: Fibonacci number F_{n}from the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, etc. where each element equals the sum of the previous two members of the sequence. Example: F(7) = 13. -
**L(n)**: Lucas number L_{n}= F_{n-1}+ F_{n+1} -
**N(n)**: Next probable prime after*n*. Example: N(24) = 29. -
**P(n)**: Unrestricted Partition Number (number of decompositions of`n`into sums of integers without regard to order). Example: P(4) = 5 because the number 4 can be partitioned in 5 different ways: 4 = 3+1 = 2+2 = 2+1+1 = 1+1+1+1. -
**Gcd(m,n)**: Greatest common divisor of these two integers. Example: GCD(12, 16) = 4. -
**Modinv(m,n)**: inverse of`m`modulo`n`, only valid when`m`and`n`are coprime, menaning that they do not have common factors. Example: Modinv(3,7) = 5 because 3 × 5 ≡ 1 (mod 7) -
**Modpow(m,n,r)**: finds`m`^{n}modulo`r`. Example: Modpow(3, 4, 7) = 4, because 3^{4}≡ 4 (mod 7). -
**IsPrime(n)**: returns zero if`n`is not probable prime, -1 if it is. Example: IsPrime(5) = -1. -
**NumDigits(n,r)**: Number of digits of`n`in base`r`. Example: NumDigits(13, 2) = 4 because 13 in binary (base 2) is expressed as 1101. -
**SumDigits(n,r)**: Sum of digits of`n`in base`r`. Example: SumDigits(213, 10) = 6 because the sum of the digits expressed in decimal is 2+1+3 = 6. -
**RevDigits(n,r)**: finds the value obtained by writing backwards the digits of`n`in base`r`. Example: RevDigits(213, 10) = 312.

You can use the prefix *0x* for hexadecimal numbers, for example 0x38 is equal to 56.

The exponentiation symbol is not present in some mobile devices, so two asterisks ** can by typed as the exponentiation operator.

Example: Find the number `n` such that 7^{n} ≡ 23 (mod 43241).

Type 7 in the Base input box, 23 in the Power input box and 43241 in the Mod input box. Then press the button named "Discrete logarithm".

The result is 3360 + 3930 k. As a check you can compute 7^{3360} ≡ 23 (mod 43241) and 7^{3930} ≡ 1 (mod 43241).

## Source code

You can download the source of the current program and the old sum polynomial factorization applet from GitHub. Notice that the source code is in C language and you need the Emscripten environment in order to generate Javascript.

Written by Dario Alpern. Last updated 13 May 2020.