pyecsca.ec.mod.base module¶
- extgcd(a, b)[source]¶
Compute the extended Euclid’s greatest common denominator algorithm.
- Return type:
Tuple
[int
,int
,int
]
- class RandomModAction(order)[source]¶
Bases:
ResultAction
A random sampling from Z_n.
- 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
- 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:
- 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:
- class Undefined[source]¶
Bases:
Mod
A special undefined element.
- 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.
- mod(x, n)[source]¶
Construct a
Mod
.Note
This function dispatches to one of
RawMod
,GMPMod
orFlintMod
based on what packages are installed and what implementation is configured (seepyecsca.misc.cfg
).