Shamir's secret sharing

Shamir's secret sharing (SSS) is an efficient secret sharing algorithm for distributing private information (the "secret") among a group. The secret cannot be revealed unless a quorum of the group acts together to pool their knowledge. To achieve this, the secret is mathematically divided into parts (the "shares") from which the secret can be reassembled only when a sufficient number of shares are combined. SSS has the property of information-theoretic security, meaning that even if an attacker steals some shares, it is impossible for the attacker to reconstruct the secret unless they have stolen the quorum number of shares.

Shamir's secret sharing is used in some applications to share the access keys to a master secret.

High-level explanation
SSS is used to secure a secret in a distributed form, most often to secure encryption keys. The secret is split into multiple shares, which individually do not give any information about the secret.

To reconstruct a secret secured by SSS, a number of shares is needed, called the threshold. No information about the secret can be gained from any number of shares below the threshold (a property called perfect secrecy). In this sense, SSS is a generalisation of the one-time pad (which can be viewed as SSS with a two-share threshold and two shares in total).

Application example
A company needs to secure their vault. If a single person knows the code to the vault, the code might be lost or unavailable when the vault needs to be opened. If there are several people who know the code, they may not trust each other to always act honestly. SSS can be used in this situation to generate shares of the vault's code which are distributed to authorized individuals in the company. The minimum threshold and number of shares given to each individual can be selected such that the vault is accessible only by (groups of) authorized individuals. If fewer shares than the threshold are presented, the vault cannot be opened. By accident, coercion or as an act of opposition, some individuals might present incorrect information for their shares. If the total of correct shares fails to meet the minimum threshold, the vault remains locked.

Use cases
Shamir's secret sharing can be used to
 * share a key for decrypting the root key of a password manager,
 * recover a user key for encrypted email access and
 * share the passphrase used to recreate a master secret, which is in turn used to access a cryptocurrency wallet.

Properties and weaknesses
SSS has useful properties, but also weaknesses that means that it is unsuited to some uses.

Useful properties include:
 * 1) Secure: The scheme has information-theoretic security.
 * 2) Minimal: The size of each piece does not exceed the size of the original data.
 * 3) Extensible: For any given threshold, shares can be dynamically added or deleted without affecting existing shares
 * 4) Dynamic: Security can be enhanced without changing the secret, but by changing the polynomial occasionally (keeping the same free term) and constructing a new share for each of the participants.
 * 5) Flexible: In organizations where hierarchy is important, each participant can be issued different numbers of shares according to their importance inside the organization. For instance, with a threshold of 3, the president could unlock the safe alone if given three shares, while three secretaries with one share each must combine their shares to unlock the safe.

Weaknesses include:
 * 1) No verifiable secret sharing: During the share reassembly process, SSS does not provide a way to verify the correctness of each share being used. Verifiable secret sharing aims to verify that shareholders are honest and not submitting fake shares.
 * 2) Single point of failure: The secret must exist in one place when it is split into shares, and again in one place when it is reassembled. These are attack points, and other schemes including multisignature eliminate at least one of these single points of failure.

History
Adi Shamir, an Israeli scientist, first formulated the scheme in 1979.

Mathematical principle
The scheme exploits the Lagrange interpolation theorem, specifically that $$k$$ points on the polynomial uniquely determines a polynomial of degree less than or equal to $$k-1$$. For instance, 2 points are sufficient to define a line, 3 points are sufficient to define a parabola, 4 points to define a cubic curve and so forth.

Mathematical formulation
Shamir's secret sharing is an ideal and perfect $$\left(k,n\right)$$-threshold scheme based on polynomial interpolation over finite fields. In such a scheme, the aim is to divide a secret $$S$$ (for example, the combination to a safe) into $$n$$ pieces of data $$S_1,\ldots,S_n$$ (known as shares) in such a way that:
 * 1) Knowledge of any $$k$$ or more shares $$S_i$$ makes $$S$$ computable. That is, the entire secret $$S$$ can be reconstructed from any combination of $$k$$ shares.
 * 2) Knowledge of any $$k-1$$ or fewer shares $$S_i$$ leaves $$S$$ completely undetermined, in the sense that the possible values for $$S$$ remain as likely with knowledge of up to $$k-1$$ shares as with knowledge of $$0$$ shares. The secret $$S$$ cannot be reconstructed with fewer than $$k$$ shares.

If $$n=k$$, then all of the shares are needed to reconstruct the secret $$S$$.

Assume that the secret $$S$$ can be represented as an element $$a_0$$ of a finite field $$\mathrm{GF}(q)$$ (where $$q$$ is greater than the number $$n$$ of shares being generated). Randomly choose $$k-1$$ elements, $$a_1,\cdots,a_{k-1}$$, from $$\mathrm{GF}(q)$$ and construct the polynomial $$f\left(x\right)=a_0+a_1x+a_2x^2+a_3x^3+\cdots+a_{k-1}x^{k-1}$$. Compute any $$n$$ points out on the curve, for instance set $$i=1,\ldots,n$$ to find points $$\left(i,f\left(i\right)\right)$$. Every participant is given a point (a non-zero input to the polynomial, and the corresponding output). Given any subset of $$k$$ of these pairs, $$a_0$$ can be obtained using interpolation, with one possible formula for doing so being $$a_0 = f(0) = \sum_{j=0}^{k-1} y_j \prod_{\begin{smallmatrix} m\,=\,0 \\ m\,\ne\,j \end{smallmatrix}}^{k-1} \frac{x_m}{x_m - x_j} $$, where the list of points on the polynomial is given as $$k$$ pairs of the form $$(x_i, y_i)$$. Note that $$f(0)$$ is equal to the first coefficient of polynomial $$f(x)$$.

Example calculation
The following example illustrates the basic idea. Note, however, that calculations in the example are done using integer arithmetic rather than using finite field arithmetic to make the idea easier to understand. Therefore, the example below does not provide perfect secrecy and is not a proper example of Shamir's scheme. The next example will explain the problem.

Preparation
Suppose that the secret to be shared is 1234 $$(S=1234)$$.

In this example, the secret will be split into 6 shares $$(n=6)$$, where any subset of 3 shares $$(k=3)$$ is sufficient to reconstruct the secret. $$k-1=2$$ numbers are taken at random. Let them be 166 and 94.
 * This yields coefficients $$(a_0=1234;a_1=166;a_2=94),$$ where $$a_0$$ is the secret

The polynomial to produce secret shares (points) is therefore:
 * $$f(x)=1234+166x+94x^2$$

Six points $$D_{x-1}=(x, f(x))$$ from the polynomial are constructed as:
 * $$D_0=(1, 1494);D_1=(2, 1942);D_2=(3, 2578);D_3=(4, 3402); D_4= (5, 4414);D_5=(6, 5614)$$

Each participant in the scheme receives a different point (a pair of $$x$$ and $$f(x)$$). Because $$D_{x-1}$$ is used instead of $$D_x$$ the points start from $$(1, f(1))$$ and not $$(0, f(0))$$. This is necessary because $$f(0)$$ is the secret.

Reconstruction
In order to reconstruct the secret, any 3 points are sufficient

Consider using the 3 points$$\left(x_0,y_0\right)=\left(2,1942\right);\left(x_1,y_1\right)=\left(4,3402\right);\left(x_2,y_2\right)=\left(5,4414\right)$$.

Computing the Lagrange basis polynomials:


 * $$\ell_0(x)=\frac{x-x_1}{x_0-x_1}\cdot\frac{x-x_2}{x_0-x_2}=\frac{x-4}{2-4}\cdot\frac{x-5}{2-5}=\frac{1}{6}x^2-\frac{3}{2}x+\frac{10}{3}$$


 * $$\ell_1(x)=\frac{x-x_0}{x_1-x_0}\cdot\frac{x-x_2}{x_1-x_2}=\frac{x-2}{4-2}\cdot\frac{x-5}{4-5}=-\frac{1}{2}x^2+\frac{7}{2}x-5$$


 * $$\ell_2(x)=\frac{x-x_0}{x_2-x_0}\cdot\frac{x-x_1}{x_2-x_1}=\frac{x-2}{5-2}\cdot\frac{x-4}{5-4}=\frac{1}{3}x^2-2x+\frac{8}{3}$$

Using the formula for polynomial interpolation, $$f(x)$$ is:



\begin{align} f(x) & =\sum_{j=0}^2 y_j\cdot\ell_j(x) \\[6pt] & =y_0\ell_0(x)+y_1\ell_1(x)+y_2\ell_2(x) \\[6pt] & =1942\left(\frac{1}{6}x^2-\frac{3}{2}x+\frac{10}{3}\right) + 3402\left(-\frac{1}{2}x^2+\frac{7}{2}x-5\right) + 4414\left(\frac{1}{3}x^2-2x+\frac{8}{3}\right) \\[6pt] & =1234+166x+94x^2 \end{align} $$

Recalling that the secret is the free coefficient, which means that $$S=1234$$, and the secret has been recovered.

Computationally efficient approach
Using polynomial interpolation to find a coefficient in a source polynomial $$S=f(0)$$ using Lagrange polynomials is not efficient, since unused constants are calculated.

Considering this, an optimized formula to use Lagrange polynomials to find $$f(0)$$ is defined as follows:


 * $$f(0) = \sum_{j=0}^{k-1} y_j \prod_{\begin{smallmatrix} m\,=\,0 \\ m\,\ne\,j \end{smallmatrix}}^{k-1} \frac{x_m}{x_m - x_j} $$

Problem of using integer arithmetic
Although the simplified version of the method demonstrated above, which uses integer arithmetic rather than finite field arithmetic, works, there is a security problem: Eve gains information about $$S$$ with every $$D_i$$ that she finds.

Suppose that she finds the 2 points $$D_0=(1,1494)$$ and $$D_1=(2,1942)$$. She still does not have $$k=3$$ points, so in theory she should not have gained any more information about $$S$$. But she could combine the information from the 2 points with the public information: $$n=6, k=3, f(x)=a_0+a_1x+\cdots+a_{k-1}x^{k-1}, a_0=S, a_i\in\mathbb{Z}$$. Doing so, Eve could perform the following algebra:


 * 1) Fill the formula for $$f(x)$$ with $$S$$ and the value of $$k: f(x)=S+a_1x+\cdots+a_{k-1}x^{k-1}\Rightarrow{}f(x)=S+a_1x+a_2x^2$$
 * 2) Fill (1) with the values of $$D_0$$'s $$x$$ and $$f(x): 1494=S+a_{1}1+a_{2}1^2\Rightarrow{}1494=S+a_1+a_2$$
 * 3) Fill  (1) with the values of $$D_1$$'s $$x$$ and $$f(x): 1942=S+a_{1}2+a_{2}2^2\Rightarrow{}1942=S+2a_1+4a_2$$
 * 4) Subtract (3)-(2): $$(1942-1494)=(S-S)+(2a_1-a_1)+(4a_2-a_2)\Rightarrow{}448=a_1+3a_2$$ and rewrite this as $$a_1=448-3a_2$$.
 * 5) Now, Eve can replace the result from (4) into (2): $$1494=S+(448-3a_2)+a_2\Rightarrow{}S=1046+2a_2$$ which leads her to the information that S is even.

Solution using finite field arithmetic


The above attack exploits constraints on the values that the polynomial may take by virtue of how it was constructed: the polynomial must have coefficients that are integers, and the polynomial must take an integer as value when evaluated at each of the coordinates used in the scheme. This reduces its possible values at unknown points, including the resultant secret, given fewer than $$k$$ shares.

This problem can be remedied by using finite field arithmetic. A finite field always has size $$q = p^r$$, where $$p$$ is a prime and $$r$$ is a positive integer. The size $$q$$ of the field must satisfy $$q>n$$, and that $$q$$ is greater than the number of possible values for the secret, though the latter condition may be circumvented by splitting the secret into smaller secret values, and applying the scheme to each of these. In our example below, we use a prime field (i.e. r = 1). The figure shows a polynomial curve over a finite field.

In practice this is only a small change. The order q of the field (i.e. the number of values that it has) must be chosen to be greater than the number of participants and the number of values that the secret $$a_0=S$$ may take. All calculations involving the polynomial must also be calculated over the field (mod p in our example, in which $$p = q$$ is taken to be a prime) instead of over the integers. Both the choice of the field and the mapping of the secret to a value in this field are considered to be publicly known.

For this example, choose $$p=1613$$, so the polynomial becomes $$f(x)=1234+166x+94x^2\bmod{1613}$$ which gives the points: $$(1,1494);(2,329);(3,965);(4,176);(5,1188);(6,775)$$

This time Eve doesn't gain any information when she finds a $$D_x$$ (until she has $$k$$ points).

Suppose again that Eve finds $$D_0=\left(1,1494\right)$$ and $$D_1=\left(2,329\right)$$, and the public information is: $$n=6, k=3, p=1613, f(x)=a_0+a_1x+\dots+a_{k-1}x^{k-1}\mod{p}, a_0=S, a_i\in\mathbb{N}$$. Attempting the previous attack, Eve can: There are $$p$$ possible values for $$a_1$$. She knows that $$[448,445,442,\ldots]$$ always decreases by 3, so if $$p$$ were divisible by $$3$$ she could conclude $$a_1\in[1, 4, 7, \ldots]$$. However, $$p$$ is prime, so she can not conclude this. Thus, using a finite field avoids this possible attack.
 * 1) Fill the $$f(x)$$-formula with $$S$$ and the value of $$k$$ and $$p$$: $$f(x)=S+a_1x+\dots+a_{3-1}x^{3-1}\mod1613$$
 * 2) Fill (1) with the values of $$D_0$$'s $$x$$ and $$f(x): 1494\equiv S+a_1 1+a_2 1^2 \pmod{1613}\Rightarrow {} 1494\equiv S+a_1+a_2 \pmod{1613}$$
 * 3) Fill (1) with the values of $$D_1$$'s $$x$$ and $$f(x): 1942\equiv S+a_{1}2+a_{2}2^2 \pmod{1613}\Rightarrow{}1942\equiv S+2a_1+4a_2 \pmod{1613}$$
 * 4) Subtract (3)-(2): $$(1942-1494)\equiv (S-S)+(2a_1-a_1)+(4a_2-a_2) \pmod{1613}\Rightarrow{}448 \equiv a_1+3a_2 \pmod{1613}$$ and rewrite this as $$a_1\equiv 448-3a_2 \pmod{1613}$$

Also, even though Eve can conclude that $$S\equiv 1046+2a_2 \pmod{1613}$$, it does not provide any additional information, since the "wrapping around" behavior of modular arithmetic prevents the leakage of "S is even", unlike the example with integer arithmetic above.

Python code
For purposes of keeping the code clearer, a prime field is used here. In practice, for convenience a scheme constructed using a smaller binary field may be separately applied to small substrings of bits of the secret (e.g. GF(256) for byte-wise application), without loss of security. The strict condition that the size of the field must be larger than the number of shares must still be respected (e.g., if the number of shares could exceed 255, the field GF(256) might be replaced by say GF(65536)).