new Polynomial()
Polynomial class for finite field arithmetic over secp256k1 curve order
Provides polynomial operations essential for cryptographic secret sharing:
- Random polynomial generation for secret distribution
- Polynomial evaluation at specific points (share generation)
- Lagrange interpolation for secret reconstruction
- Polynomial arithmetic (addition, multiplication)
All coefficients are BigNumbers reduced modulo the secp256k1 curve order, ensuring compatibility with elliptic curve cryptographic operations.
Example
// Create a random degree-2 polynomial for 3-of-5 threshold scheme
const poly = Polynomial.fromRandom(2);
// Evaluate at points 1,2,3,4,5 to generate shares
const shares = [1,2,3,4,5].map(x => [x, poly.evaluate(x)]);
// Reconstruct secret using any 3 shares
const secret = Polynomial.interpolate_evaluate(shares.slice(0,3), 0);
Members
(readonly) coefficients :Array.<BN>
Array of polynomial coefficients as BigNumbers
Type:
- Array.<BN>
(readonly) order :number
Polynomial degree (highest power of x)
Type:
- number
Methods
add(otheropt) → {Polynomial}
Adds two polynomials coefficient-wise
Performs polynomial addition: (f + g)(x) = f(x) + g(x) The resulting polynomial has degree max(deg(f), deg(g))
This operation is useful in cryptographic protocols that require linear combinations of shared secrets.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
other |
Polynomial |
<optional> |
{order: 1, coefficients: [1, 2, 3]} | Polynomial to add |
Returns:
New polynomial representing the sum
- Type
- Polynomial
Example
// Add two random polynomials
const poly1 = Polynomial.fromRandom(2); // f(x) = a₀ + a₁x + a₂x²
const poly2 = Polynomial.fromRandom(2); // g(x) = b₀ + b₁x + b₂x²
const sum = poly1.add(poly2); // h(x) = (a₀+b₀) + (a₁+b₁)x + (a₂+b₂)x²
// Verify addition property: h(5) = f(5) + g(5)
const x = 5;
const sumAtX = sum.evaluate(x);
const directSum = poly1.evaluate(x).add(poly2.evaluate(x)).umod(N);
console.log(sumAtX.eq(directSum)); // true
evaluate(xopt) → {BN}
Evaluates the polynomial at a given point using Horner's method
Efficiently computes f(x) = a₀ + a₁x + a₂x² + ... + aₙxⁿ using Horner's method: f(x) = a₀ + x(a₁ + x(a₂ + x(a₃ + ...)))
This method is used to generate shares in secret sharing schemes by evaluating the polynomial at participant indices.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
x |
number |
<optional> |
2 | Point at which to evaluate the polynomial |
Returns:
The polynomial value f(x) modulo curve order
- Type
- BN
Example
// Generate shares for a 3-of-5 threshold scheme
const secret = new BN("deadbeefcafe", 'hex');
const coeffs = [secret, new BN(randomBytes(32)), new BN(randomBytes(32))];
const poly = new Polynomial(coeffs);
// Generate 5 shares
const shares = [];
for (let i = 1; i <= 5; i++) {
shares.push([i, poly.evaluate(i)]);
}
// Any 3 shares can reconstruct the secret
const reconstructed = Polynomial.interpolate_evaluate(shares.slice(0, 3), 0);
console.log(reconstructed.eq(secret)); // true
multiply(otheropt) → {Polynomial}
Multiplies two polynomials using convolution
Performs polynomial multiplication: (f * g)(x) = f(x) * g(x) The resulting polynomial has degree deg(f) + deg(g)
Uses the standard convolution algorithm where each coefficient of the result is the sum of products of coefficients whose indices sum to that position.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
other |
Polynomial |
<optional> |
{order: 1, coefficients: [1, 2, 3]} | Polynomial to multiply |
Returns:
New polynomial representing the product
- Type
- Polynomial
Example
// Multiply two polynomials: (2 + 3x) * (1 + 4x) = 2 + 11x + 12x²
const poly1 = new Polynomial([new BN(2), new BN(3)]); // 2 + 3x
const poly2 = new Polynomial([new BN(1), new BN(4)]); // 1 + 4x
const product = poly1.multiply(poly2); // 2 + 11x + 12x²
// Verify: coefficients should be [2, 11, 12]
console.log(product.coefficients[0].toNumber()); // 2
console.log(product.coefficients[1].toNumber()); // 11
console.log(product.coefficients[2].toNumber()); // 12
(static) fromRandom(orderopt) → {Polynomial}
Generates a random polynomial of specified degree using cryptographically secure randomness
Each coefficient is generated using 32 bytes of secure random data, ensuring unpredictability suitable for cryptographic applications. The constant term (coefficients[0]) becomes the secret to be shared.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
order |
number |
<optional> |
2 | Degree of the polynomial to generate |
Returns:
New polynomial with random coefficients
- Type
- Polynomial
Example
// Generate random polynomial for 2-of-3 threshold (degree = threshold - 1)
const poly = Polynomial.fromRandom(2);
// Generate shares by evaluating at points 1, 2, 3
const share1 = poly.evaluate(1);
const share2 = poly.evaluate(2);
const share3 = poly.evaluate(3);
// Any 2 shares can reconstruct the secret (coefficients[0])
(static) interpolate_evaluate(pointsopt, xopt) → {BN}
Reconstructs a secret using Lagrange interpolation from coordinate points
Implements Lagrange interpolation to evaluate a polynomial at point x given sufficient coordinate pairs. This is the core operation for reconstructing secrets in Shamir's Secret Sharing.
The algorithm computes: f(x) = Σᵢ yᵢ * Lᵢ(x) where Lᵢ(x) = Πⱼ≠ᵢ (x - xⱼ) / (xᵢ - xⱼ)
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
points |
InterpolationPoints |
<optional> |
[[1, 2], [1,2]] | Array of [x, y] coordinate pairs |
x |
number |
<optional> |
2 | Point at which to evaluate the interpolated polynomial |
Returns:
The interpolated value f(x) modulo curve order
- Type
- BN
Example
// Reconstruct secret from threshold shares
const shares = [[1, new BN("123")], [2, new BN("456")], [3, new BN("789")]];
const secret = Polynomial.interpolate_evaluate(shares, 0); // Evaluate at x=0
// Verify polynomial evaluation at known point
const poly = Polynomial.fromRandom(2);
const testPoints = [[1, poly.evaluate(1)], [2, poly.evaluate(2)], [3, poly.evaluate(3)]];
const reconstructed = Polynomial.interpolate_evaluate(testPoints, 5);
const direct = poly.evaluate(5);
console.log(reconstructed.eq(direct)); // true