ecdsa.ellipticcurve module

class ecdsa.ellipticcurve.AbstractPoint[source]

Bases: object

Class for common methods of elliptic curve points.

classmethod from_bytes(curve, data, validate_encoding=True, valid_encodings=None)[source]

Initialise the object from byte encoding of a point.

The method does accept and automatically detect the type of point encoding used. It supports the raw encoding, uncompressed, compressed, and hybrid encodings.

Note: generally you will want to call the from_bytes() method of either a child class, PointJacobi or Point.

Parameters:
  • data (bytes-like object) – single point encoding of the public key

  • curve (CurveFp) – the curve on which the public key is expected to lay

  • validate_encoding (bool) – whether to verify that the encoding of the point is self-consistent, defaults to True, has effect only on hybrid encoding

  • valid_encodings (set-like object) – list of acceptable point encoding formats, supported ones are: uncompressed, compressed, hybrid, and raw encoding (specified with raw name). All formats by default (specified with None).

Raises:

MalformedPointError – if the public point does not lay on the curve or the encoding is invalid

Returns:

x and y coordinates of the encoded point

Return type:

tuple(int, int)

to_bytes(encoding='raw')[source]

Convert the point to a byte string.

The method by default uses the raw encoding (specified by encoding=”raw”. It can also output points in uncompressed, compressed, and hybrid formats.

For points on Edwards curves encoding is ignored and only the encoding defined in RFC 8032 is supported.

Returns:

raw encoding of a public on the curve

Return type:

bytes

class ecdsa.ellipticcurve.CurveEdTw(p, a, d, h=None, hash_func=None)[source]

Bases: object

Parameters for a Twisted Edwards Elliptic Curve

a()[source]
cofactor()[source]
contains_point(x, y)[source]

Is the point (x, y) on this curve?

d()[source]
hash_func(data)[source]
p()[source]
class ecdsa.ellipticcurve.CurveFp(p, a, b, h=None)[source]

Bases: object

Short Weierstrass Elliptic Curve over a prime field.

a()[source]
b()[source]
cofactor()[source]
contains_point(x, y)[source]

Is the point (x,y) on this curve?

p()[source]
class ecdsa.ellipticcurve.Point(curve, x, y, order=None)[source]

Bases: AbstractPoint

A point on a short Weierstrass elliptic curve. Altering x and y is forbidden, but they can be read by the x() and y() methods.

curve()[source]
double()[source]

Return a new point that is twice the old.

classmethod from_bytes(curve, data, validate_encoding=True, valid_encodings=None, order=None)[source]

Initialise the object from byte encoding of a point.

The method does accept and automatically detect the type of point encoding used. It supports the raw encoding, uncompressed, compressed, and hybrid encodings.

Parameters:
  • data (bytes-like object) – single point encoding of the public key

  • curve (CurveFp) – the curve on which the public key is expected to lay

  • validate_encoding (bool) – whether to verify that the encoding of the point is self-consistent, defaults to True, has effect only on hybrid encoding

  • valid_encodings (set-like object) – list of acceptable point encoding formats, supported ones are: uncompressed, compressed, hybrid, and raw encoding (specified with raw name). All formats by default (specified with None).

  • order (int) – the point order, must be non zero when using generator=True

Raises:

MalformedPointError – if the public point does not lay on the curve or the encoding is invalid

Returns:

Point on curve

Return type:

Point

order()[source]
to_bytes(encoding='raw')

Convert the point to a byte string.

The method by default uses the raw encoding (specified by encoding=”raw”. It can also output points in uncompressed, compressed, and hybrid formats.

For points on Edwards curves encoding is ignored and only the encoding defined in RFC 8032 is supported.

Returns:

raw encoding of a public on the curve

Return type:

bytes

x()[source]
y()[source]
class ecdsa.ellipticcurve.PointEdwards(curve, x, y, z, t, order=None, generator=False)[source]

Bases: AbstractPoint

Point on Twisted Edwards curve.

Internally represents the coordinates on the curve using four parameters, X, Y, Z, T. They correspond to affine parameters ‘x’ and ‘y’ like so:

x = X / Z y = Y / Z x*y = T / Z

curve()[source]

Return the curve of the point.

double()[source]

Return point added to itself.

classmethod from_bytes(curve, data, validate_encoding=None, valid_encodings=None, order=None, generator=False)[source]

Initialise the object from byte encoding of a point.

validate_encoding and valid_encodings are provided for compatibility with Weierstrass curves, they are ignored for Edwards points.

Parameters:
  • data (bytes-like object) – single point encoding of the public key

  • curve (ecdsa.ellipticcurve.CurveEdTw) – the curve on which the public key is expected to lay

  • validate_encoding (None) – Ignored, encoding is always validated

  • valid_encodings (None) – Ignored, there is just one encoding supported

  • order (int) – the point order, must be non zero when using generator=True

  • generator (bool) – Flag to mark the point as a curve generator, this will cause the library to pre-compute some values to make repeated usages of the point much faster

Raises:

MalformedPointError – if the public point does not lay on the curve or the encoding is invalid

Returns:

Initialised point on an Edwards curve

Return type:

PointEdwards

order()[source]
scale()[source]

Return point scaled so that z == 1.

Modifies point in place, returns self.

to_bytes(encoding='raw')

Convert the point to a byte string.

The method by default uses the raw encoding (specified by encoding=”raw”. It can also output points in uncompressed, compressed, and hybrid formats.

For points on Edwards curves encoding is ignored and only the encoding defined in RFC 8032 is supported.

Returns:

raw encoding of a public on the curve

Return type:

bytes

x()[source]

Return affine x coordinate.

y()[source]

Return affine y coordinate.

class ecdsa.ellipticcurve.PointJacobi(curve, x, y, z, order=None, generator=False)[source]

Bases: AbstractPoint

Point on a short Weierstrass elliptic curve. Uses Jacobi coordinates.

In Jacobian coordinates, there are three parameters, X, Y and Z. They correspond to affine parameters ‘x’ and ‘y’ like so:

x = X / Z² y = Y / Z³

curve()[source]

Return curve over which the point is defined.

double()[source]

Add a point to itself.

static from_affine(point, generator=False)[source]

Create from an affine point.

Parameters:

generator (bool) – set to True to make the point to precalculate multiplication table - useful for public point when verifying many signatures (around 100 or so) or for generator points of a curve.

classmethod from_bytes(curve, data, validate_encoding=True, valid_encodings=None, order=None, generator=False)[source]

Initialise the object from byte encoding of a point.

The method does accept and automatically detect the type of point encoding used. It supports the raw encoding, uncompressed, compressed, and hybrid encodings.

Parameters:
  • data (bytes-like object) – single point encoding of the public key

  • curve (CurveFp) – the curve on which the public key is expected to lay

  • validate_encoding (bool) – whether to verify that the encoding of the point is self-consistent, defaults to True, has effect only on hybrid encoding

  • valid_encodings (set-like object) – list of acceptable point encoding formats, supported ones are: uncompressed, compressed, hybrid, and raw encoding (specified with raw name). All formats by default (specified with None).

  • order (int) – the point order, must be non zero when using generator=True

  • generator (bool) – the point provided is a curve generator, as such, it will be commonly used with scalar multiplication. This will cause to precompute multiplication table generation for it

Raises:

MalformedPointError – if the public point does not lay on the curve or the encoding is invalid

Returns:

Point on curve

Return type:

PointJacobi

mul_add(self_mul, other, other_mul)[source]

Do two multiplications at the same time, add results.

calculates self*self_mul + other*other_mul

order()[source]

Return the order of the point.

None if it is undefined.

scale()[source]

Return point scaled so that z == 1.

Modifies point in place, returns self.

to_affine()[source]

Return point in affine form.

to_bytes(encoding='raw')

Convert the point to a byte string.

The method by default uses the raw encoding (specified by encoding=”raw”. It can also output points in uncompressed, compressed, and hybrid formats.

For points on Edwards curves encoding is ignored and only the encoding defined in RFC 8032 is supported.

Returns:

raw encoding of a public on the curve

Return type:

bytes

x()[source]

Return affine x coordinate.

This method should be used only when the ‘y’ coordinate is not needed. It’s computationally more efficient to use to_affine() and then call x() and y() on the returned instance. Or call scale() and then x() and y() on the returned instance.

y()[source]

Return affine y coordinate.

This method should be used only when the ‘x’ coordinate is not needed. It’s computationally more efficient to use to_affine() and then call x() and y() on the returned instance. Or call scale() and then x() and y() on the returned instance.

ecdsa.ellipticcurve.bytes_to_int(bytes, byteorder='big', *, signed=False)

Return the integer represented by the given array of bytes.

bytes

Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.

byteorder

The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value. Default is to use ‘big’.

signed

Indicates whether two’s complement is used to represent the integer.