Math

Orders and logs

Count and list the points of a curve and find the order of a point and a discrete log by baby step giant step and Pohlig-Hellman

How many points?

ts
import { countPoints, defineCurve, listPoints } from "@agntn/curves";

const curve = defineCurve({ a: 2n, b: 2n, p: 17n });
countPoints(curve); // 19n
listPoints(curve, { limit: 3 }); // [{ x: 0n, y: 6n }, { x: 0n, y: 11n }, { x: 3n, y: 1n }]

countPoints counts infinity too, so it gives the group order. listPoints leaves infinity out and goes by x, then y. It walks every x once, which is why p has a ceiling.

Want only the points of one order? Pass order:

ts
const f23 = defineCurve({ a: 1n, b: 1n, p: 23n });
listPoints(f23, { order: 7n }); // six points, (5, 4) to (17, 20)

An order that doesn't divide the group order gives an empty list without multiplying a single point.

The order of a point

ts
import { pointOrder } from "@agntn/curves";

pointOrder(curve, { x: 5n, y: 1n }); // 19n

That's the smallest n with n times the point at infinity. Baby step giant step finds it inside the Hasse interval. The prime factors of that number trim it down. So it works far past what a walk could count:

ts
const wide = defineCurve({ a: 2n, b: 3n, p: 1099511627563n });
pointOrder(wide, { x: 1n, y: 727918651225n }); // 1099513053442n

A 40 bit field, a few milliseconds.

Discrete logs

ts
import { discreteLog } from "@agntn/curves";

discreteLog(curve, { x: 5n, y: 1n }, { x: 16n, y: 4n }); // 13n

You get the smallest k below the order of the base with k times base equal to the target. No such k? You get undefined, never a guess. In f23, the point (4, 0) has order 2, and no multiple of a point of order 7 ever lands on it.

So how big can the order get? Wrong question. What counts is its biggest prime. discreteLog splits the order into prime powers, finds k mod each one with its own small search, then glues the pieces back with the Chinese remainder theorem. That's Pohlig-Hellman. A smooth order is the first thing a CTF curve begs you to try it on:

ts
discreteLog(wide, { x: 1n, y: 727918651225n }, { x: 746206281184n, y: 443106390269n }); // 987654321987n

The order of that base is 2 · 103 · 2917 · 1829771. Four tiny searches instead of one huge one, about 20 ms.

Over MCP, log also names the order it searched in, so a model knows what "smallest" meant:

text
{"operation":"log","order":"19","scalar":"13"}

Discrete log when you know roughly where k is

ts
import { discreteLogInInterval } from "@agntn/curves";

const pair = { base: { x: 5n, y: 1n }, target: { x: 16n, y: 4n } };
discreteLogInInterval(curve, { ...pair, lo: 14n, hi: 40n }); // 32n

The result is the smallest k in the inclusive bounds where k times the base equals the target. The base has order 19. discreteLog gives 13, which falls below lo. Add 19 and you're back in range.

Nothing fits? You get undefined, as with lo: 14n and hi: 31n. Negative bounds work too.

Know a remainder of k? Pass residue and modulus together.

ts
discreteLogInInterval(curve, { ...pair, lo: -30n, hi: 200n, residue: -1n, modulus: 3n }); // -25n

Baby step giant step counts candidates, not field size. It never computes the base order. You get up to 2^40 candidates after filtering.

The table holds their square root, up to MAX_INTERVAL_TABLE points, which is 2^20. tableLimit lowers that for one call. Too many candidates? Nothing gets built.

text
The interval holds 1099511627777 scalars to try, and a table of 1048576 points covers 1099511627776; narrow it or split it

Splitting the stretch is your job.

What about secp256k1?

Define it with a = 0, b = 7 and its p. The same function found 2^200 + 123456789 in a window of 2^40 scalars starting at 2^200.

A local run took about three seconds. With a residue and modulus: 1021n, the same window took about a tenth of a second.

A window of 2^40 is tiny beside a group of about 2^256. Finding an unknown scalar on a real curve remains impossible. You still need to know roughly where k lives.

Over MCP and the CLI, log accepts lo, hi, residue and modulus. It reports the bounds instead of the order. That tells the model where it searched, so null means nothing matched there, not that no k exists. One call tries up to 2^36 scalars.

text
{"operation":"log","lo":"14","hi":"40","scalar":"32"}

Limits

Each of these stops at a limit instead of running for ever. The numbers are exported, so you can check before you call.

FunctionTakesConstant
countPoints, listPointsp up to 2^20MAX_COUNTED_PRIME
listPoints with orderp up to 2^16MAX_FILTERED_PRIME
pointOrderp up to 2^48MAX_ORDER_PRIME
discreteLoga base whose order has no prime above 2^36MAX_LOG_ORDER
discreteLogInIntervalup to 2^40 scalars after the residue filterMAX_INTERVAL_TABLE

Past one, the error says which:

text
Counting and listing points takes p up to 1048576
The base has order 1099512267719 with the prime factor 1099512267719, above the 68719476736 a search takes

The slow log is a prime order near 2^36, with nothing to split. About 0.6 seconds. A whole 256 bit group? Nobody's search takes that, which is the whole point of elliptic curve cryptography. A window of 2^40 is a peek, not the whole group.