---
title: "Curves and points"
description: "Define a short Weierstrass curve over a prime and add and double and negate and multiply its points or lift an x"
canonical_url: "https://curves.agntn.dev/math/curves"
---
# Curves and points

> Define a short Weierstrass curve over a prime and add and double and negate and multiply its points or lift an x

## Define the curve

```ts
import { defineCurve } from "@agntn/curves";

defineCurve({ a: -3n, b: 20n, p: 17n }); // { a: 14n, b: 3n, p: 17n }
```

a and b take any integer and come back reduced mod p. So a = -3 works and turns into 14. p has to be a prime above 3, checked with Baillie-PSW. And the curve can't be singular, so 4a³ + 27b² mod p can't be 0.

```text
defineCurve({ a: 0n, b: 0n, p: 17n })   // The curve is singular: 4a^3 + 27b^2 is 0 mod p
defineCurve({ a: 1n, b: 1n, p: 21n })   // p must be a prime above 3
```

The result is frozen and remembered. Every other function takes it without checking it again. Plain `{ a, b, p }` works too, it just gets checked on every call.

## Arithmetic

```ts
import { addPoints, doublePoint, multiplyPoint, negatePoint } from "@agntn/curves";

const curve = defineCurve({ a: 2n, b: 2n, p: 17n });
const G = { x: 5n, y: 1n };

doublePoint(curve, G); // { x: 6n, y: 3n }
addPoints(curve, G, { x: 6n, y: 3n }); // { x: 10n, y: 6n }
negatePoint(curve, G); // { x: 5n, y: 16n }
multiplyPoint(curve, G, 19n); // null
multiplyPoint(curve, G, -2n); // { x: 6n, y: 14n }
```

`null` is the point at infinity, and it goes in too. A negative scalar multiplies the negation. Zero gives infinity.

## Checking a point

`isOnCurve` answers true or false. Every other function refuses a point off the curve before it touches it:

```text
isOnCurve(curve, { x: 5n, y: 2n })     // false
doublePoint(curve, { x: 5n, y: 2n })   // RangeError: The point is not on the curve; coordinates run from 0 to p minus 1
```

Why so strict? An invalid curve attack starts exactly there. Feed a point from another curve and you get answers on that curve, quietly. Here you get an error.

## Lifting an x

A compressed point is an x and one bit. So which y did it mean? `liftX` hands you both:

```ts
import { liftX } from "@agntn/curves";

liftX(curve, 5n); // [{ x: 5n, y: 1n }, { x: 5n, y: 16n }]
liftX(curve, 1n); // []
```

Smaller y first. p is odd, so the two y always differ in parity, and the bit picks one. Here odd means 1, even means 16. An x with no point gives an empty list, not an error. Where y is 0 there's only one point.

No size limit here, unlike counting. secp256k1 gets its root from one power, since its p is 3 mod 4. A curve you define owes you nothing like that. So the root goes through Tonelli-Shanks. NIST P-224 is 1 mod 4, so no shortcut, and its p - 1 hides 2⁹⁶, which makes Tonelli-Shanks sweat. Its generator still comes back from x alone, in under a millisecond.

## Over MCP

`curves_compute` takes the same thing as strings. Points are `{ "x": "5", "y": "1" }`, integers decimal or `0x` hex:

```json
{ "operation": "multiply", "a": "2", "b": "2", "p": "17", "point": { "x": "5", "y": "1" }, "scalar": "2" }
```

```text
{"operation":"multiply","point":{"x":"6","y":"3"}}
```

`lift` takes x where the others take a point:

```json
{ "operation": "lift", "a": "2", "b": "2", "p": "17", "x": "5" }
```

```text
{"operation":"lift","points":[{"x":"5","y":"1"},{"x":"5","y":"16"}]}
```


## Sitemap

See the full [sitemap](https://curves.agntn.dev/sitemap.md) for all pages.
