nmod.h – integers mod n (word-size n)¶
Modular reduction and arithmetic¶
-
void nmod_init(nmod_t *mod, ulong n)¶
Initialises the given
nmod_t
structure 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)
. Themod
parameter must be a validnmod_t
structure. It is assumed thata_hi
is already reduced modulomod.n
.
-
NMOD_RED(r, a, mod)¶
Macro to set \(r\) to \(a\) reduced modulo
mod.n
. Themod
parameter must be a validnmod_t
structure.
-
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)
. Themod
parameter must be a validnmod_t
structure. 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)
. Themod
parameter must be a validnmod_t
structure. It is assumed thata_hi
is already reduced modulomod.n
.
-
NMOD_MUL_PRENORM(res, a, b, mod)¶
Macro to set \(r\) to \(ab\) modulo
mod.n
. Themod
parameter must be a validnmod_t
structure. It is assumed that \(a\), \(b\) are already reduced modulomod.n
and 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
. Themod
parameter must be a validnmod_t
structure. It is assumed that \(a\), \(b\) are already reduced modulomod.n
and thatmod.n
is exactlyFLINT_BITS
bits large.
-
NMOD_ADDMUL(r, a, b, mod)¶
Macro to set \(r\) to \(r + ab\) reduced modulo
mod.n
. Themod
parameter must be a validnmod_t
structure. 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 thatmod
is no more thanFLINT_BITS - 1
bits. 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 thatmod
is no more thanFLINT_BITS - 1
bits. 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.n
is exactlyFLINT_BITS
large. 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 initializationL
is 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
L
for discrete logarithms modulop
to an internally chosen base. It is assumed thatp
is prime. The return is an estimate on the number of multiplications needed for one run.