Discrete logarithm tables
- Cost
- After a precomputation of , each of logarithms in a group of prime order costs multiplications; for composite order the cost is the sum over the prime factors.
The dlog module computes discrete logarithms in a cyclic group of order , for word-size . Its use case, Dirichlet characters (Dirichlet characters), asks for many logarithms to the same base, typically with a prime power and a primitive root. The module therefore separates a precomputation, sized for an expected number of queries, from the queries themselves. For the general theory see [CraPom2005, §5.2–5.3]; the single-logarithm algorithm of nmod is described in Discrete logarithms modulo a prime (Pohlig–Hellman).
Baby-step giant-step with a balanced table#
Shanks' method [Sha1971] stores the baby steps for , sorted by value, and for a query computes the giant steps , , until one of them is found in the table as ; then .
Proposition 1. A query needs at most giant steps, each one multiplication and one binary search. For queries the total cost, to build the table plus for the queries, is minimised by , giving multiplications in total, or per query after the precomputation.
Proof. Write with ; then and is in the table. The optimum of is at .
dlog_bsgs_size(n, k) returns when , and (a complete table, so that every query is one search) otherwise. For groups of order at most 3 no table is needed, and for moduli below 50 a table indexed by the residue replaces the sorted array.
Pohlig–Hellman decomposition#
If , the logarithm modulo of is the logarithm of to the base , of order , and the Chinese remainder theorem combines them [PohHel1978]. Within a group of order , write ; then
so each digit is a logarithm in the subgroup of order , generated by . A logarithm in a group of order thus costs multiplications, where is the cost of a logarithm in a group of order ; the structure is the same as for nmod_discrete_log_pohlig_hellman. In FLINT, dlog_crt_t holds one precomputation per prime power, and dlog_power_t one precomputation of the subgroup of order , built for queries.
Prime power moduli and -adic logarithms#
For with odd and a primitive root modulo , the group has order , and its -Sylow part is the subgroup , cyclic of order and generated by . Logarithms there are cheap:
Lemma 2. For odd and , . Consequently, if with , then .
Proof. By induction on with the binomial theorem: if , then , the other terms being divisible by or, for the term , by since is odd.
So is found one base- digit at a time (dlog_1modpe_1modp), with modular powers. The logarithm to the base is then obtained from two smaller problems.
Proposition 3. Let modulo , modulo , and modulo . Then , , and
Proof. The first congruence is reduction modulo , the second follows from with of order . Since , the right-hand side is congruent to modulo and to modulo , and it lies in .
The logarithm is divided by modulo , which is precomputed (dlog_1modpe_init). For the subgroup of elements is generated by 5, and is found one bit at a time in the same way (dlog_mod2e_1mod4). The cost of a logarithm modulo is therefore a logarithm modulo , in a group of order , plus modular powers.
Choosing the method#
dlog_precomp_modpe_init, dlog_precomp_n_init, dlog_precomp_pe_init and dlog_precomp_p_init choose the method from what is known about the group, recursively:
| Situation | Method |
|---|---|
| modulus | complete table |
| modulus , | logarithm modulo and -adic logarithm |
| order | direct comparison |
| order an odd prime , or other | complete table (residue-indexed if the modulus is , else sorted) |
| order an odd prime | baby-step giant-step with dlog_bsgs_size(n, k) baby steps |
| order composite, | baby-step giant-step |
| order composite | Pohlig–Hellman over the prime powers |
| order , , | digits in the subgroup of order |
Each method returns an estimate of the cost of one logarithm in multiplications (cost), which the sieve below uses to decide whether to look for relations.
Pollard's rho method [Pol1978], which needs time and no memory, is available as dlog_rho but is not used by these precomputations.
The logarithms of all small integers#
To evaluate a Dirichlet character at , one needs for all . Two methods are used.
Enumeration (dlog_vec_loop). Running through and recording the exponent of every power below costs multiplications. It is the method of choice when is comparable with (FLINT uses it when ).
Sieving (dlog_vec_sieve). Logarithms are additive, , so it suffices to compute for the primes and to fill in the other entries in increasing order, . A prime costs one precomputed logarithm. For larger primes, a relation with smaller numbers may be cheaper: with for a fixed , compute for a partial extended Euclidean algorithm on , stopped when the remainder and the cofactor are both about , so that
If and factor into primes below , whose logarithms are already known, then . The probability that two numbers of size are -smooth is about with , which FLINT estimates crudely by with ; it searches for relations only when this predicts fewer multiplications than a precomputed logarithm. This is a small instance of the index calculus method [CraPom2005, §6.4], without the linear algebra.
Use in FLINT#
dlog_precomp_tand its initialisation functions implement the strategy table above withdlog_table_t,dlog_bsgs_t,dlog_crt_t,dlog_power_t,dlog_modpe_tanddlog_order23_t;dlog_onceenumerates for orders below 50 and otherwise uses a precomputation for one query.dlog_vecanddlog_vec_addchoose betweendlog_vec_loopanddlog_vec_sieve;dlog_vec_add_precompreuses an existing precomputation.dlog_vec_eratoscomputes the logarithm of every prime directly.dirichlet_group_dlog_precomputebuildsdlog_precomp_modpe_inittables for each prime power component;dirichlet_char_loganddirichlet_chi_vecuse them, ordlog_onceanddlog_vec_addwhen no tables were requested.
void dlog_precomp_n_init(dlog_precomp_t pre, ulong a, ulong mod, ulong n, ulong num)void dlog_precomp_modpe_init(dlog_precomp_t pre, ulong a, ulong p, ulong e, ulong pe, ulong num)ulong dlog_precomp(const dlog_precomp_t pre, ulong b)ulong dlog_bsgs_init(dlog_bsgs_t t, ulong a, ulong mod, ulong n, ulong m)Referenced from#
References#
- [CraPom2005] R. Crandall and C. Pomerance, Prime Numbers: A Computational Perspective, second edition, Springer (2005).
- [Sha1971] D. Shanks, "Class number, a theory of factorization, and genera", Proc. Symp. Pure Math. 20, American Mathematical Society (1971), 415–440
- [PohHel1978] S. C. Pohlig and M. E. Hellman, "An improved algorithm for computing logarithms over GF(p) and its cryptographic significance", IEEE Transactions on Information Theory 24 (1978), 106–110, https://doi.org/10.1109/TIT.1978.1055817
- [Pol1978] J. M. Pollard, "Monte Carlo methods for index computation (mod p)", Mathematics of Computation 32 (1978), 918–924. https://doi.org/10.1090/S0025-5718-1978-0491431-9
The FLINT team, “Discrete logarithm tables,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/number-theory/discrete-log-tables.html (generated 2026-11-02).
@misc{flint-doc-algorithms-number-theory-discrete-log-tables,
author = {{The FLINT team}},
title = {{Discrete logarithm tables}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/number-theory/discrete-log-tables.html},
note = {Generated 2026-11-02}
}