Lesson 14 of 15

Shamir's Secret Sharing

Shamir's Secret Sharing

Shamir's Secret Sharing (Adi Shamir, 1979) splits a secret into nn shares such that any tt shares can reconstruct the secret, but any t1t-1 shares reveal absolutely nothing. This is called a (t,n)(t, n)-threshold scheme.

Construction

  1. Represent the secret as an integer ss (the constant term of a polynomial)
  2. Choose a random polynomial of degree t1t-1: f(x)=s+a1x+a2x2++at1xt1(modp)f(x) = s + a_1 x + a_2 x^2 + \cdots + a_{t-1} x^{t-1} \pmod{p} where pp is a prime larger than both ss and nn
  3. Give party ii the share (i,f(i))(i, f(i)) for i=1,2,,ni = 1, 2, \ldots, n

Reconstruction — Lagrange Interpolation

Given tt shares (x1,y1),,(xt,yt)(x_1, y_1), \ldots, (x_t, y_t), reconstruct f(0)=sf(0) = s using Lagrange interpolation modulo pp:

s=f(0)=i=1tyijixjxixj(modp)s = f(0) = \sum_{i=1}^{t} y_i \prod_{j \neq i} \frac{-x_j}{x_i - x_j} \pmod{p}

The modular division uses Fermat's little theorem: a1ap2(modp)a^{-1} \equiv a^{p-2} \pmod{p}.

Security

With fewer than tt shares, the secret is information-theoretically hidden — any value of ss is equally likely. This is provably secure, unlike computational security assumptions.

Applications

  • Distributed key management — split private keys across multiple servers
  • Disaster recoverymm-of-nn backup schemes
  • Secure multi-party computation — building block for advanced protocols

Our Deterministic Version

For testability, we use deterministic coefficients: ai=(s×i×1337)modpa_i = (s \times i \times 1337) \bmod p (instead of random).

Your Task

Implement:

  • shamir_share(secret, n, threshold, prime=2**31-1) — generate nn shares using a degree-(t1)(t-1) polynomial
  • shamir_reconstruct(shares, prime=2**31-1) — Lagrange interpolation to recover the secret
Python runtime loading...
Loading...
Click "Run" to execute your code.