Math

Linear algebra

Solve linear systems over a prime field with BigInt. Get rank, a nullspace, a proof when nothing solves it and exact integer determinants

BigInt linear algebra over a prime field

Need rank, a nullspace, or every solution over a prime field? matrixRank, nullspace, and solveLinearSystem give you those. For a system, you get one solution plus a kernel basis, or a certificate proving there's no solution. The modulus must be prime.

You can also compute an integer determinant. Import all four from @agntn/curves. Give them arrays of bigint rows, all the same length. For field operations, negative and oversized entries aren't a problem. They're reduced for you.

ts
import { determinant, matrixRank, nullspace, solveLinearSystem } from "@agntn/curves";

solveLinearSystem(
  [
    [1n, 2n, 3n],
    [2n, 4n, 6n],
  ],
  [1n, 2n],
  { modulus: 7n },
);
// { consistent: true, rank: 1, solution: [1n, 0n, 0n], kernel: [[5n, 1n, 0n], [4n, 0n, 1n]] }

These functions aren't constant time. Don't use them with secrets.

Solve A x = b mod a prime

A consistent result gives you one solution and a kernel basis. Add any combination of kernel vectors to get another solution. With n unknowns, there are p^(n - rank) solutions. Above, that's 7^2 = 49.

Results come back reduced into 0 to p - 1. Your arrays aren't modified.

How many solutions are there?

There are p^(unknowns - rank) solutions. You get a basis, not a list. Want to print them all? On the secp256k1 prime, one free variable means about 2^256 answers. That's a long print job.

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

const p = 2n ** 256n - 2n ** 32n - 977n;

solveLinearSystem(
  [
    [2n, 3n],
    [5n, 7n],
  ],
  [1n, 1n],
  { modulus: p },
);
// { consistent: true, rank: 2, solution: [p - 4n, 3n], kernel: [] }

The system has full rank and an empty kernel. It has exactly one solution. The comment writes p - 4n for the 78 digits that come back.

A proof when no solution exists

An inconsistent result gives you a rank and a certificate. Here, over F_2, x + y can't be both 0 and 1. Add the rows and you get 0 = 1. That's a proof in two bigints.

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

solveLinearSystem(
  [
    [1n, 1n],
    [1n, 1n],
  ],
  [0n, 1n],
  { modulus: 2n },
);
// { consistent: false, rank: 1, certificate: [1n, 1n] }

How do you check a certificate?

Multiply its weights by the rows. You get the zero row modulo p. Multiply those weights by the values. You get something nonzero modulo p. There's one weight per input row, in your original order.

Rank and nullspace over F_p

Does rank depend on the prime? Yes. This matrix has rank 2 modulo 11 and rank 1 modulo 3. matrixRank returns a number.

ts
import { matrixRank, nullspace } from "@agntn/curves";

const matrix = [
  [1n, 2n, 3n],
  [4n, 5n, 6n],
  [7n, 8n, 9n],
];

matrixRank(matrix, { modulus: 11n }); // 2
nullspace(matrix, { modulus: 11n }); // [[1n, 9n, 1n]]
matrixRank(matrix, { modulus: 3n }); // 1
nullspace(matrix, { modulus: 3n }); // [[1n, 1n, 0n], [0n, 0n, 1n]]

The nullspace basis has columns minus rank vectors. Each vector has 1n in its own free column. If only the zero vector solves the system, the basis is empty.

Why the modulus has to be prime

Z/15Z isn't a field. Rank doesn't have a single meaning there. A composite modulus is refused, even when elimination would get lucky. That's better than reporting a field rank where no field exists.

How is the prime checked? isPrime uses Baillie-PSW. No composite is known to pass it. Nobody has proved that none can. It's a test, not a primality proof.

Exact integer determinants

determinant works over the integers, not modulo p. It uses Bareiss elimination, where every division is exact. You won't need fractions or floats along the way. Your entries can be any size. The second one below is far past 2^53, and still right to the last digit.

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

determinant([
  [2n, 0n, 1n],
  [0n, 1n, 1n],
  [1n, 1n, 2n],
]); // 1n
determinant([
  [2n ** 70n, 1n],
  [1n, 2n ** 70n],
]); // 1393796574908163946345982392040522594123775n, which is 2^140 - 1

Empty matrices

What can you do with a matrix that has nothing in it? [] has no rows, so it has no columns either. Its rank is 0, and its nullspace is empty. Solve it with [] and you'll get an empty solution. Its determinant is 1n, the empty product.

Rows without columns, like [[], []], give you equations of the form 0 = b. They're consistent only when every b is zero modulo p. Otherwise, you'll get a certificate picking a row where zero claims to equal something else.

ts
import { determinant, matrixRank, solveLinearSystem } from "@agntn/curves";

matrixRank([], { modulus: 7n }); // 0
solveLinearSystem([], [], { modulus: 7n }); // { consistent: true, rank: 0, solution: [], kernel: [] }
solveLinearSystem([[], []], [0n, 1n], { modulus: 7n }); // { consistent: false, rank: 0, certificate: [0n, 1n] }
determinant([]); // 1n

Linear algebra errors

You'll get a RangeError when the dimensions or modulus don't fit the operation. A Number entry or a modulus passed bare throws TypeError.

text
matrixRank([[1n]], { modulus: 15n })                    // The modulus must be a prime
matrixRank([[1n, 2n], [3n]], { modulus: 7n })           // Row 1 has length 1, row 0 has length 2
solveLinearSystem([[1n], [2n]], [1n], { modulus: 7n })  // 2 rows need 2 values, not 1
determinant([[1n, 2n]])                                 // A determinant needs a square matrix, not 1 by 2
matrixRank([[1, 2]], { modulus: 7n })                   // Every matrix entry must be a bigint
matrixRank([[1n]], 7n)                                  // matrixRank takes the modulus as { modulus }

The first four throw RangeError. The last two throw TypeError.