# 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
)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://onyb.gitbook.io/roll-your-own-crypto/finite-field-in-python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
