> For the complete documentation index, see [llms.txt](https://onyb.gitbook.io/roll-your-own-crypto/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://onyb.gitbook.io/roll-your-own-crypto/finite-field-in-python.md).

# Elliptic Curve in Python

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

```python
@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 **S**tandards for **E**fficient **C**ryptography.
* **p:** indicates that what follows are the **p**arameters of the curve.
* **256:** length in bits of the field size.
* **k:** **K**olbitz curve, as opposed to random. The non-random construction allows for efficient construction.
* **1:** sequence number

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

```python
# Ref: https://en.bitcoin.it/wiki/Secp256k1
# secp256k1 elliptic curve equation: y² = x³ + 7

# Prime of the finite field
P: int = (
    0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
)
field = PrimeGaloisField(prime=P)

# Elliptic curve parameters A and B of the curve : y² = x³ Ax + B
A: int = 0
B: int = 7

secp256k1 = EllipticCurve(
    a=A,
    b=B,
    field=field
)
```
