Short answer: Shamir secret sharing splits a secret into points on a polynomial over a prime field. A polynomial of degree 2 is fixed by any 3 points. The fragment uses the field modulo 101 and the polynomial 42 + 7x + 3x^2. The shares at x = 1, x = 2, and x = 4 reconstruct 42 by Lagrange interpolation at x = 0. The coefficients are written in the fragment so the arithmetic can be checked. A deployment draws the higher coefficients at random and does not publish them.
What the problem actually asks
The problem is to hand out shares so that any 3 of them rebuild the secret and, when the higher coefficients are random and secret, any 2 of them leave it undetermined. Interpolation at zero is the rebuild step. Publishing the coefficients, as this worked example does, lets a reader recompute every share. That publication is the opposite of a deployment. With secret random coefficients, fewer shares than the degree plus one are consistent with many secrets.
What the algorithm guarantees
For this published polynomial, the three shares interpolate to 42. The arithmetic is exact in the field modulo 101, including the division by the Lagrange denominators, which is multiplication by a modular inverse. The same interpolation works for any three distinct x-coordinates in the field. The example does not claim that two of these published shares hide 42, because the coefficients are on the page.
The working steps
- Choose a prime and a polynomial whose constant term is the secret.
- Evaluate the polynomial at distinct nonzero points and hand out those points.
- To rebuild, form the Lagrange basis at
x = 0for the collected shares. - Sum
share valuetimesbasis weightover the field.
Approaches that look simpler and fail
| Approach | Why it fails when the coefficients are published or the points repeat |
|---|---|
| Publish the polynomial and also expect two shares to hide the secret | The published coefficients determine the polynomial. A deployment keeps them secret. |
| Issue two shares at the same x-coordinate | Interpolation needs distinct x-coordinates. A repeated x-coordinate is not a new point. |
| Interpolate with ordinary integer division | The weights are field elements. Division is multiplication by the modular inverse. |
| Add the shares and call the sum the secret | The secret is the constant term, which is the interpolation at zero, not the sum of the y-values. |
The arithmetic is Lagrange interpolation. The secrecy is the unpublished random coefficients, which this checked example deliberately prints.
A working example
This Python fragment evaluates a degree-2 polynomial over the field modulo 101 and interpolates the secret. It prints 42.
def shamir_share(secret: int, prime: int, xs: list[int]) -> list[tuple[int, int]]:
# f(x) = secret + 7x + 3x^2 over the field. Degree 2 needs any 3 shares.
def f(x: int) -> int:
return (secret + 7 * x + 3 * x * x) % prime
return [(x, f(x)) for x in xs]
def shamir_reconstruct(shares: list[tuple[int, int]], prime: int) -> int:
total = 0
for i, (x, y) in enumerate(shares):
num = den = 1
for j, (xo, _) in enumerate(shares):
if i == j:
continue
num = num * (-xo) % prime
den = den * (x - xo) % prime
total = (total + y * num * pow(den, -1, prime)) % prime
return total
shares = shamir_share(42, 101, [1, 2, 4])
assert shamir_reconstruct(shares, 101) == 42
print(shamir_reconstruct(shares, 101))
Where it is used
HashiCorp Vault unseals with Shamir sharing: the master key is split, and a threshold of key holders reconstructs it. Backup schemes that split a key among people use the same interpolation. The shares in the worked example publish their coefficients; a deployment does not.
When you can drop this algorithm
Drop a sharing whose coefficients are written down beside the shares. Keep Shamir sharing where the higher coefficients are random, the prime is large, and the reconstruction threshold is the number of shares the application can require.
Why this stays published
We publish this so a search for Shamir secret sharing returns the polynomial, the threshold, and the interpolation at zero. Shamir sharing evaluates a polynomial whose constant term is the secret, and any enough shares rebuild that constant by interpolation at zero.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
