Elliptic Curve in Python

Recall that an elliptic curve over a finite field has 3 distinct properties — aa, bb, and the field parameters. Let's define them below:

@dataclass
class EllipticCurve:
    a: int
    b: int

    field: PrimeGaloisField
    
    def __contains__(self, point: "Point") -> bool:
        x, y = point.x, point.y
        return y ** 2 == x ** 3 + self.a * x + self.b

    def __post_init__(self):
        # Encapsulate int parameters in FieldElement
        self.a = FieldElement(self.a, self.field)
        self.b = FieldElement(self.b, self.field)
    
        # Check for membership of curve parameters in the field.
        if self.a not in self.field or self.b not in self.field:
            raise ValueError

Defining secp256k1

secp256k1 refers to the parameters of the elliptic curve used in Bitcoin's public-key cryptography. The name represents the specific parameters of curve:

  • sec: stands for Standards for Efficient Cryptography.

  • p: indicates that what follows are the parameters of the curve.

  • 256: length in bits of the field size.

  • k: Kolbitz curve, as opposed to random. The non-random construction allows for efficient construction.

  • 1: sequence number

The general equation of the secp256k1 curve is y2=x3+7y^2 = x^3 + 7. Let's represent this curve using the classes we have already defined.

Last updated

Was this helpful?