fmpz_mod – integers mod n

class flint.fmpz_mod_ctx(mod)

Context object for creating fmpz_mod initialised with a modulus \(N\).

>>> fmpz_mod_ctx(2**127 - 1)
fmpz_mod_ctx(170141183460469231731687303715884105727)
is_prime(self)

Return whether the modulus is prime

>>> fmpz_mod_ctx(2**127).is_prime()
False
>>> fmpz_mod_ctx(2**127 - 1).is_prime()
True
modulus(self)

Return the modulus from the context as an fmpz type

>>> mod_ctx = fmpz_mod_ctx(2**127 - 1)
>>> mod_ctx.modulus()
170141183460469231731687303715884105727
one(self)

Return the one element

>>> F = fmpz_mod_ctx(163)
>>> F.one()
fmpz_mod(1, 163)
random_element(self)

Return a random element in \(\mathbb{Z}/N\mathbb{Z}\)

zero(self)

Return the zero element

>>> F = fmpz_mod_ctx(163)
>>> F.zero()
fmpz_mod(0, 163)
class flint.fmpz_mod(val, ctx)

The fmpz_mod type represents integer modulo an arbitrary-size modulus. For wordsize modulus, see nmod.

An fmpz_mod element is constructed from an fmpz_mod_ctx either by passing it as an argument to the type, or by directly calling the context

>>> fmpz_mod(-1, fmpz_mod_ctx(2**127 - 1))
fmpz_mod(170141183460469231731687303715884105726, 170141183460469231731687303715884105727)
>>> ZmodN = fmpz_mod_ctx(2**127 - 1)
>>> ZmodN(-2)
fmpz_mod(170141183460469231731687303715884105725, 170141183460469231731687303715884105727)
discrete_log(self, a)

Solve the discrete logarithm problem, using \(self = g\) as a base. Assumes a solution, \(a = g^x \pmod p\) exists.

NOTE: Requires that the context modulus is prime.

>>> F = fmpz_mod_ctx(163)
>>> g = F(2)
>>> x = 123
>>> a = g**123
>>> g.discrete_log(a)
123
inverse(self, check=True)

Computes \(a^{-1} \pmod N\)

When check=False, the solutions is assumed to exist and Flint will abort on failure.

>>> mod_ctx = fmpz_mod_ctx(163)
>>> mod_ctx(2).inverse()
fmpz_mod(82, 163)
>>> mod_ctx(2).inverse(check=False)
fmpz_mod(82, 163)
is_one(self)

Return whether an element is equal to one

>>> mod_ctx = fmpz_mod_ctx(163)
>>> mod_ctx(0).is_one()
False
>>> mod_ctx(1).is_one()
True
is_unit(self)

Returns whether the element is invertible modulo \(N\)

>>> from flint import *
>>> F = fmpz_mod_ctx(10)
>>> F(3).is_unit()
True
>>> F(2).is_unit()
False
is_zero(self)

Return whether an element is equal to zero

>>> mod_ctx = fmpz_mod_ctx(163)
>>> mod_ctx(0).is_zero()
True
>>> mod_ctx(1).is_zero()
False
repr(self)
sqrt(self)

Return the square root of this fmpz_mod or raise an exception.

>>> ctx = fmpz_mod_ctx(13)
>>> s = ctx(10).sqrt()
>>> s
fmpz_mod(6, 13)
>>> s * s
fmpz_mod(10, 13)
>>> ctx(11).sqrt()
Traceback (most recent call last):
    ...
flint.utils.flint_exceptions.DomainError: no square root exists for 11 mod 13

The modulus must be prime.

str(self)