pyecsca.ec.curve module

Provides an elliptic curve class.

class EllipticCurve(model, coordinate_model, prime, neutral, parameters)[source]

Bases: object

An elliptic curve.

>>> from pyecsca.ec.params import get_params
>>> params = get_params("secg", "secp256r1", "projective")
>>> curve = params.curve
>>> curve.prime
115792089210356248762697446949407573530086143415290314195533631308867097853951
>>> curve.parameters  
{'a': 115792089210356248762697446949407573530086143415290314195533631308867097853948,
 'b': 41058363725152142129326129780047268409114441015993725554835256314039467401291}
>>> curve.neutral
InfinityPoint(shortw/projective)

You can also use the curve object to operate on affine points.

>>> from pyecsca.ec.coordinates import AffineCoordinateModel
>>> affine = AffineCoordinateModel(curve.model)
>>> points_P = sorted(curve.affine_lift_x(mod(5, curve.prime)), key=lambda p: int(p.y))
>>> points_P  
[Point([x=5, y=31468013646237722594854082025316614106172411895747863909393730389177298123724] in shortw/affine),
 Point([x=5, y=84324075564118526167843364924090959423913731519542450286139900919689799730227] in shortw/affine)]
>>> P = points_P[0]
>>> Q = Point(affine, x=mod(106156966968002564385990772707119429362097710917623193504777452220576981858057, curve.prime), y=mod(89283496902772247016522581906930535517715184283144143693965440110672128480043, curve.prime))
>>> curve.affine_add(P, Q)
Point([x=110884201872336783252492544257507655322265785208411447156687491781308462893723, y=17851997459724035659875545393642578516937407971293368958749928013979790074156] in shortw/affine)
>>> curve.affine_multiply(P, 10)
Point([x=102258728610797412855984739741975475478412665729440354248608608794190482472287, y=6863906685124263315402674958985193889511160759072519051123564041627571792194] in shortw/affine)
>>> curve.affine_random()  
Point([x=..., y=...] in shortw/affine)
>>> curve.is_on_curve(P)
True
>>> curve.is_neutral(P)
False
model: CurveModel[source]

The model of the curve.

coordinate_model: CoordinateModel[source]

The coordinate system of the curve.

prime: int[source]

The prime specifying the base prime field of the curve.

parameters: MutableMapping[str, Mod][source]

The values of the parameters defining the curve, these cover the curve model and coordinate system parameters.

neutral: Point[source]

The neutral point on the curve.

affine_add(one, other)[source]

Add two affine points using the affine addition formula.

Handles the case of point at infinity gracefully (short-circuits).

Parameters:
Return type:

Point

Returns:

The addition of the two points.

affine_double(one)[source]

Double an affine point using the affine doubling formula.

Handles the case of point at infinity gracefully (short-circuits).

Parameters:

one (Point) – A point.

Return type:

Point

Returns:

The doubling of the point.

affine_negate(one)[source]

Negate an affine point using the affine negation formula.

Handles the case of point at infinity gracefully (short-circuits).

Parameters:

one (Point) – A point.

Return type:

Point

Returns:

The negation of the point.

affine_multiply(point, scalar)[source]

Multiply an affine point by a scalar using the affine doubling and addition formulas.

Handles the case of point at infinity gracefully (short-circuits).

Parameters:
  • point (Point) – The point to multiply.

  • scalar (int) – The scalar to use.

Return type:

Point

Returns:

The scalar multiplication of point.

property affine_neutral: Point | None[source]

Get the neutral point in affine form, if it has one, otherwise None.

Returns:

The affine neutral point or None.

property neutral_is_affine[source]

Whether the neutral point is an affine point.

is_neutral(point)[source]

Check whether the point is the neutral point.

Parameters:

point (Point) – The point to test.

Return type:

bool

Returns:

Whether it is the neutral point.

is_on_curve(point)[source]

Check whether the point is on the curve.

Parameters:

point (Point) – The point to test.

Return type:

bool

Returns:

Whether it is on the curve.

to_coords(coordinate_model)[source]

Convert this curve into a different coordinate model, only possible if it is currently affine.

Parameters:

coordinate_model (CoordinateModel) – The target coordinate model.

Return type:

EllipticCurve

Returns:

The transformed elliptic curve.

to_affine()[source]

Convert this curve into the affine coordinate model, if possible.

Return type:

EllipticCurve

Returns:

The transformed elliptic curve.

decode_point(encoded)[source]

Decode a point encoded as a sequence of bytes (ANSI X9.62).

This decoding is the same as ANSI X9.63 for the affine coordinate system and for others it only implements the uncompressed variant.

Warning

The point is not validated to be on the curve (if the uncompressed encoding is used).

Parameters:

encoded (bytes) – The encoded representation of a point.

Return type:

Point

Returns:

The decoded point.

affine_lift_x(x)[source]

Lift an x-coordinate to the curve.

Parameters:

x (Mod) – The x-coordinate.

Return type:

Set[Point]

Returns:

Lifted (affine) points, if any.

affine_random()[source]

Generate a random affine point on the curve.

Return type:

Point