# Class BSV::Primitives::Polynomial <a id="class-BSV-Primitives-Polynomial"></a>

**Inherits:** `Object`

A polynomial defined by a set of points, evaluated using Lagrange
interpolation.

Used in Shamir's Secret Sharing Scheme to split and reconstruct a secret. All
arithmetic is performed in the finite field GF(P) where P is the secp256k1
field prime.

The secret is encoded as the y-value at x=0. Given `threshold` distinct points
the polynomial can be evaluated at any x by Lagrange interpolation.

**@example Construct shares from a private key**
```ruby
poly   = Polynomial.from_private_key(key, threshold: 2)
share1 = poly.value_at(OpenSSL::BN.new('1'))
share0 = poly.value_at(OpenSSL::BN.new('0'))  # recovers the secret
```

## Constants
### `P` <a id="constant-P"></a> <a id="P-constant"></a>
Not documented.

## Attributes
### `points` [R] <a id="attribute-i-points"></a> <a id="points-instance_method"></a>
- **@return** [Array<PointInFiniteField>] the defining points of the polynomial

### `threshold` [R] <a id="attribute-i-threshold"></a> <a id="threshold-instance_method"></a>
- **@return** [Integer] the minimum number of shares needed to reconstruct the secret

## Public Class Methods
### `from_private_key(key, threshold:)` <a id="method-c-from_private_key"></a> <a id="from_private_key-class_method"></a>
Build a polynomial whose y-intercept (secret) is the private key scalar.

The first point is (0, key_scalar). The remaining <code>threshold-1</code>
points have random coordinates in [0, P), providing the random coefficients of
the underlying polynomial.
- **@param** `key` [PrivateKey] the private key to split
- **@param** `threshold` [Integer] the reconstruction threshold (minimum 2)
- **@return** [Polynomial]

## Public Instance Methods
### `initialize(points, threshold = nil)` <a id="method-i-initialize"></a> <a id="initialize-instance_method"></a>
- **@param** `points` [Array<PointInFiniteField>] defining points
- **@param** `threshold` [Integer] reconstruction threshold (defaults to points.length)
- **@return** [Polynomial] a new instance of Polynomial

### `value_at(x)` <a id="method-i-value_at"></a> <a id="value_at-instance_method"></a>
Evaluate the polynomial at x using Lagrange interpolation mod P.
- **@param** `x` [OpenSSL::BN] the x value at which to evaluate
- **@return** [OpenSSL::BN] the y value, in [0, P)
