pyecsca.ec.mod module¶
Provides several implementations of an element of ℤₙ.
The base class Mod
dynamically
dispatches to the implementation chosen by the runtime configuration of the library
(see pyecsca.misc.cfg.Config
). A Python integer based implementation is available under
RawMod
. A symbolic implementation based on sympy is available under SymbolicMod
. If
gmpy2 is installed, a GMP based implementation is available under GMPMod
.
- class RandomModAction(order)[source]¶
Bases:
ResultAction
A random sampling from Z_n.
-
order:
int
¶
- exit(result)¶
- property result: Any¶
-
inside:
bool
¶
-
order:
- class Mod(*args, **kwargs)[source]¶
Bases:
object
An element x of ℤₙ.
-
x:
Any
¶
-
n:
Any
¶
- inverse()[source]¶
Invert the element.
- Return type:
- 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:
-
x:
- class RawMod(x, n)[source]¶
Bases:
Mod
An element x of ℤₙ (implemented using Python integers).
-
x:
int
¶
-
n:
int
¶
- inverse()[source]¶
Invert the element.
- Return type:
- 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).
- sqrt()[source]¶
Compute the modular square root of this element (only implemented for prime modulus).
Uses the Tonelli-Shanks algorithm.
- Return type:
-
x:
- class Undefined(*args, **kwargs)[source]¶
Bases:
Mod
A special undefined element.
-
x:
Any
¶
-
n:
Any
¶
- 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.
-
x:
- class SymbolicMod(x, n)[source]¶
Bases:
Mod
A symbolic element x of ℤₙ (implemented using sympy).
-
x:
Expr
¶
-
n:
int
¶
- inverse()[source]¶
Invert the element.
- Return type:
- 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.
- Return type:
-
x:
- class GMPMod(x, n, ensure=True)[source]¶
Bases:
Mod
An element x of ℤₙ. Implemented by GMP.
-
n:
mpz
¶
-
x:
mpz
¶
- inverse()[source]¶
Invert the element.
- Return type:
- 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:
-
n: