pyecsca.ec.mod.base module

gcd(a, b)[source]

Euclid’s greatest common denominator algorithm.

Return type:

int

extgcd(a, b)[source]

Compute the extended Euclid’s greatest common denominator algorithm.

Return type:

Tuple[int, int, int]

jacobi(x, n)[source]

Jacobi symbol.

Return type:

int

miller_rabin(n, rounds=50)[source]

Miller-Rabin probabilistic primality test.

Return type:

bool

class RandomModAction(order)[source]

Bases: ResultAction

A random sampling from Z_n.

order: int[source]
exit(result)[source]
property result: Any[source]

The result of the action.

inside: bool[source]
class Mod(x, n)[source]

Bases: object

An element x of ℤₙ.

Has all the usual special methods that upcast integers automatically:

>>> a = mod(3, 5)
>>> b = mod(2, 5)
>>> a + b
0
>>> a * 2
1
>>> a == 3
True
>>> a == -2
True
>>> -a
2

Plus some additional useful things:

>>> a.inverse()
2
>>> a.is_residue()
False
>>> (a**2).is_residue()
True
>>> (a**2).sqrt() in (a, -a)
True
x: Any[source]
n: Any[source]
bit_length()[source]

Compute the bit length of this element (in its positive integer representation).

Returns:

The bit-length.

inverse()[source]

Invert the element.

Return type:

Mod

Returns:

The inverse.

Raises:

NonInvertibleError if the element is not invertible.

is_residue()[source]

Whether this element is a quadratic residue (only implemented for prime modulus).

Return type:

bool

sqrt()[source]

Compute the modular square root of this element (only implemented for prime modulus).

Uses the Tonelli-Shanks algorithm.

Return type:

Mod

classmethod random(n)[source]

Generate a random Mod in ℤₙ.

Parameters:

n (int) – The order.

Return type:

Mod

Returns:

The random Mod.

class Undefined[source]

Bases: Mod

A special undefined element.

x: Any[source]
n: Any[source]
bit_length()[source]

Compute the bit length of this element (in its positive integer representation).

Returns:

The bit-length.

inverse()[source]

Invert the element.

Returns:

The inverse.

Raises:

NonInvertibleError if the element is not invertible.

sqrt()[source]

Compute the modular square root of this element (only implemented for prime modulus).

Uses the Tonelli-Shanks algorithm.

is_residue()[source]

Whether this element is a quadratic residue (only implemented for prime modulus).

classmethod random(n)[source]

Generate a random Mod in ℤₙ.

Parameters:

n (int) – The order.

Return type:

Mod

Returns:

The random Mod.

mod(x, n)[source]

Construct a Mod.

Note

This function dispatches to one of RawMod, GMPMod or FlintMod based on what packages are installed and what implementation is configured (see pyecsca.misc.cfg).

Parameters:
  • x (int) – The value.

  • n (int) – The modulus.

Return type:

Mod

Returns:

A selected Mod implementation object.

Raises:

ValueError in case a working Mod implementation cannot be found.