nmod.h – integers mod n (word-size n)¶
Modular reduction and arithmetic¶
-
void nmod_init(nmod_t *mod, ulong n)¶
Initialises the given
nmod_tstructure for reduction modulo \(n\) with a precomputed inverse.
-
NMOD_BITS(mod)¶
Macro giving the number of bits in
mod.n.
-
NMOD_CAN_USE_SHOUP(mod)¶
Macro returning whether Shoup’s algorithm can be used for preconditioned multiplication mod
mod.n.
-
NMOD_RED2(r, a_hi, a_lo, mod)¶
Macro to set \(r\) to \(a\) reduced modulo
mod.n, where \(a\) consists of two limbs(a_hi, a_lo). Themodparameter must be a validnmod_tstructure. It is assumed thata_hiis already reduced modulomod.n.
-
NMOD_RED(r, a, mod)¶
Macro to set \(r\) to \(a\) reduced modulo
mod.n. Themodparameter must be a validnmod_tstructure.
-
NMOD2_RED2(r, a_hi, a_lo, mod)¶
Macro to set \(r\) to \(a\) reduced modulo
mod.n, where \(a\) consists of two limbs(a_hi, a_lo). Themodparameter must be a validnmod_tstructure. No assumptions are made abouta_hi.
-
NMOD_RED3(r, a_hi, a_me, a_lo, mod)¶
Macro to set \(r\) to \(a\) reduced modulo
mod.n, where \(a\) consists of three limbs(a_hi, a_me, a_lo). Themodparameter must be a validnmod_tstructure. It is assumed thata_hiis already reduced modulomod.n.
-
NMOD_MUL_PRENORM(res, a, b, mod)¶
Macro to set \(r\) to \(ab\) modulo
mod.n. Themodparameter must be a validnmod_tstructure. It is assumed that \(a\), \(b\) are already reduced modulomod.nand that either \(a\) or \(b\) is prenormalised by left-shifting bymod.norm.
-
NMOD_MUL_FULLWORD(res, a, b, mod)¶
Macro to set \(r\) to \(ab\) modulo
mod.n. Themodparameter must be a validnmod_tstructure. It is assumed that \(a\), \(b\) are already reduced modulomod.nand thatmod.nis exactlyFLINT_BITSbits large.
-
NMOD_ADDMUL(r, a, b, mod)¶
Macro to set \(r\) to \(r + ab\) reduced modulo
mod.n. Themodparameter must be a validnmod_tstructure. It is assumed that \(r\), \(a\), \(b\) are already reduced modulomod.n.
-
ulong _nmod_add(ulong a, ulong b, nmod_t mod)¶
Returns \(a + b\) modulo
mod.n. It is assumed thatmodis no more thanFLINT_BITS - 1bits. It is assumed that \(a\) and \(b\) are already reduced modulomod.n.
-
ulong nmod_add(ulong a, ulong b, nmod_t mod)¶
Returns \(a + b\) modulo
mod.n. No assumptions are made aboutmod.n. It is assumed that \(a\) and \(b\) are already reduced modulomod.n.
-
ulong _nmod_sub(ulong a, ulong b, nmod_t mod)¶
Returns \(a - b\) modulo
mod.n. It is assumed thatmodis no more thanFLINT_BITS - 1bits. It is assumed that \(a\) and \(b\) are already reduced modulomod.n.
-
ulong nmod_sub(ulong a, ulong b, nmod_t mod)¶
Returns \(a - b\) modulo
mod.n. No assumptions are made aboutmod.n. It is assumed that \(a\) and \(b\) are already reduced modulomod.n.
-
ulong nmod_neg(ulong a, nmod_t mod)¶
Returns \(-a\) modulo
mod.n. It is assumed that \(a\) is already reduced modulomod.n, but no assumptions are made about the latter.
-
ulong nmod_mul(ulong a, ulong b, nmod_t mod)¶
Returns \(ab\) modulo
mod.n. No assumptions are made aboutmod.n. It is assumed that \(a\) and \(b\) are already reduced modulomod.n.
-
ulong _nmod_mul_fullword(ulong a, ulong b, nmod_t mod)¶
Returns \(ab\) modulo
mod.n. Requires thatmod.nis exactlyFLINT_BITSlarge. It is assumed that \(a\) and \(b\) are already reduced modulomod.n.
-
ulong nmod_inv(ulong a, nmod_t mod)¶
Returns \(a^{-1}\) modulo
mod.n. The inverse is assumed to exist.
-
ulong nmod_div(ulong a, ulong b, nmod_t mod)¶
Returns \(ab^{-1}\) modulo
mod.n. The inverse of \(b\) is assumed to exist. It is assumed that \(a\) is already reduced modulomod.n.
-
int nmod_divides(ulong *a, ulong b, ulong c, nmod_t mod)¶
If \(a\cdot c = b \mod n\) has a solution for \(a\) return \(1\) and set \(a\) to such a solution. Otherwise return \(0\) and leave \(a\) undefined.
Discrete Logarithms via Pohlig-Hellman¶
-
void nmod_discrete_log_pohlig_hellman_init(nmod_discrete_log_pohlig_hellman_t L)¶
Initialize
L. Upon initializationLis not ready for computation.
-
void nmod_discrete_log_pohlig_hellman_clear(nmod_discrete_log_pohlig_hellman_t L)¶
Free any space used by
L.
-
double nmod_discrete_log_pohlig_hellman_precompute_prime(nmod_discrete_log_pohlig_hellman_t L, ulong p)¶
Configure
Lfor discrete logarithms modulopto an internally chosen base. It is assumed thatpis prime. The return is an estimate on the number of multiplications needed for one run.