pyecsca.ec.mod.base module¶
- extgcd(a, b)[source]¶
 Compute the extended Euclid’s greatest common denominator algorithm.
- Return type:
 Tuple[int,int,int]
- square_roots(x)[source]¶
 Compute all square roots of x.
- Parameters:
 x¶ (
TypeVar(M, bound= Mod))- Return type:
 Set[TypeVar(M, bound= Mod)]- Returns:
 
- cube_roots(x)[source]¶
 Compute all cube roots of x.
- Parameters:
 x¶ (
TypeVar(M, bound= Mod))- Return type:
 Set[TypeVar(M, bound= Mod)]- Returns:
 
- class RandomModAction(order)[source]¶
 Bases:
ResultActionA random sampling from Z_n.
- class Mod(x, n)[source]¶
 Bases:
Generic[M]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).
- Return type:
 int- Returns:
 The bit-length.
- inverse()[source]¶
 Invert the element.
- Return type:
 TypeVar(M, bound= Mod)- Returns:
 The inverse.
- Raises:
 NonInvertibleErrorif 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:
 TypeVar(M, bound= Mod)
- is_cubic_residue()[source]¶
 Whether this element is a cubic residue (only implemented for prime modulus).
- Return type:
 bool
- class Undefined[source]¶
 - 
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:
 NonInvertibleErrorif 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).
- cube_root()[source]¶
 Compute the cube root of this element (only implemented for prime modulus).
Uses the Adleman-Manders-Miller algorithm (which is adapted Tonelli-Shanks).
 
- mod(x, n)[source]¶
 Construct a
Mod.Note
This function dispatches to one of
RawMod,GMPModorFlintModbased on what packages are installed and what implementation is configured (seepyecsca.misc.cfg).