# Shamir's Secret Sharing

{{ safesubst:#invoke:Unsubst||$N=Technical |date=__DATE__ |$B= {{#invoke:Message box|ambox}} }}

Shamir's Secret Sharing is an algorithm in cryptography created by Adi Shamir. It is a form of secret sharing, where a secret is divided into parts, giving each participant its own unique part, where some of the parts or all of them are needed in order to reconstruct the secret.

Counting on all participants to combine together the secret might be impractical, and therefore sometimes the threshold scheme is used where any ${\displaystyle k}$ of the parts are sufficient to reconstruct the original secret.

## Mathematical definition

The goal is to divide secret ${\displaystyle S}$ (e.g., a safe combination) into ${\displaystyle n}$ pieces of data ${\displaystyle S_{1},\ldots ,S_{n}}$ in such a way that:

1. Knowledge of any ${\displaystyle k}$ or more ${\displaystyle S_{i}}$ pieces makes ${\displaystyle S}$ easily computable.
2. Knowledge of any ${\displaystyle k-1}$ or fewer ${\displaystyle S_{i}}$ pieces leaves ${\displaystyle S}$ completely undetermined (in the sense that all its possible values are equally likely).

This scheme is called ${\displaystyle \left(k,n\right)}$ threshold scheme. If ${\displaystyle k=n}$ then all participants are required to reconstruct the secret.

## Shamir's secret-sharing scheme

One can draw an infinite number of polynomials of degree 2 through 2 points. 3 points are required to define a unique polynomial of degree 2. This image is for illustration purposes only — Shamir's scheme uses polynomials over a finite field, not representable on a 2-dimensional plane.

The essential idea of Adi Shamir's threshold scheme is that 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. That is, it takes ${\displaystyle k\,\!}$ points to define a polynomial of degree ${\displaystyle k-1\,\!}$.

Suppose we want to use a ${\displaystyle \left(k,n\right)\,\!}$ threshold scheme to share our secret ${\displaystyle S\,\!}$, without loss of generality assumed to be an element in a finite field ${\displaystyle F}$ of size ${\displaystyle P}$ where ${\displaystyle 0 and ${\displaystyle P}$ is a prime number.

Choose at random ${\displaystyle k-1\,\!}$ positive integers ${\displaystyle a_{1},\cdots ,a_{k-1}\,\!}$ with ${\displaystyle a_{i}, and let ${\displaystyle a_{0}=S\,\!}$. Build the polynomial ${\displaystyle f\left(x\right)=a_{0}+a_{1}x+a_{2}x^{2}+a_{3}x^{3}+\cdots +a_{k-1}x^{k-1}\,\!}$. Let us construct any ${\displaystyle n\,\!}$ points out of it, for instance set ${\displaystyle i=1,\cdots ,n\,\!}$ to retrieve ${\displaystyle \left(i,f\left(i\right)\right)\,\!}$. Every participant is given a point (an integer input to the polynomial, and the corresponding integer output). Given any subset of ${\displaystyle k\,\!}$ of these pairs, we can find the coefficients of the polynomial using interpolation. The secret is the constant term ${\displaystyle a_{0}\,\!}$.

## Usage

### Example

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. Therefore the example below does not provide perfect secrecy and is not a true example of Shamir's scheme. So we'll explain this problem and show the right way to implement it (using finite field arithmetic).

#### Preparation

Suppose that our secret is 1234 ${\displaystyle (S=1234)\,\!}$.

We wish to divide the secret into 6 parts ${\displaystyle (n=6)\,\!}$, where any subset of 3 parts ${\displaystyle (k=3)\,\!}$ is sufficient to reconstruct the secret. At random we obtain two (${\displaystyle k-1}$) numbers: 166 and 94.

Our polynomial to produce secret shares (points) is therefore:

We construct 6 points ${\displaystyle D_{x-1}=(x,f(x))}$ from the polynomial:

We give each participant a different single point (both ${\displaystyle x\,\!}$ and ${\displaystyle f\left(x\right)\,\!}$). Because we use ${\displaystyle D_{x-1}}$ instead of ${\displaystyle D_{x}}$ the points start from ${\displaystyle (1,f(1))}$ and not ${\displaystyle (0,f(0))}$. This is necessary because if one would have ${\displaystyle (0,f(0))}$ he would also know the secret (${\displaystyle S=f(0)}$)

#### Reconstruction

In order to reconstruct the secret any 3 points will be enough.

We will compute Lagrange basis polynomials:

Therefore

Recall that the secret is the free coefficient, which means that ${\displaystyle S=1234\,\!}$, and we are done.

##### Problem

Although this method works fine, there is a security problem: Eve wins a lot of information about ${\displaystyle S}$ with every ${\displaystyle D_{i}}$ that she finds.

Suppose that she finds the 2 points ${\displaystyle D_{0}=(1,1494)}$ and ${\displaystyle D_{1}=(2,1942)}$, she still doesn't have ${\displaystyle k=3}$ points so in theory she shouldn't have won anymore info about ${\displaystyle S}$. But she combines the info from the 2 points with the public info: ${\displaystyle n=6,k=3,f(x)=a_{0}+a_{1}x+\dots +a_{k-1}x^{k-1},a_{0}=S,a_{i}\in \mathbb {N} }$ and she : Template:Ordered list ${\displaystyle S\in [1046,1048,\dots ,1342,1344]}$. She now only has 150 numbers to guess from instead of a infinite number of natural numbers.

#### Solution

This problem can be fixed by using finite field arithmetic in a field of size ${\displaystyle p\in \mathbb {P} :p>S,p>n}$.

This is in practice only a small change, it just means that we should choose a prime ${\displaystyle p}$ that is bigger than the number of participants and every ${\displaystyle a_{i}}$ (including ${\displaystyle a_{0}=S}$) and we have to calculate the points as ${\displaystyle (x,f(x){\pmod {p}})}$ instead of ${\displaystyle (x,f(x))}$.

Everyone that receives a point also has to know the value of ${\displaystyle p}$ so it's publicly known so you should choose a value for ${\displaystyle p}$ that is not too low because Eve knows ${\displaystyle p>S\Rightarrow {}S\in {[0,1,\dots ,p-2,p-1]}}$, so the lower you choose ${\displaystyle p}$, the lower the number of possible values Eve has to guess from to get ${\displaystyle S}$.

You should also not choose it too high because Eve knows that the chance for ${\displaystyle f(x){\pmod {p}}=f(x)}$ increases with a higher ${\displaystyle p}$ and she can use the procedure from the original problem to guess ${\displaystyle S}$ (although now, instead of being sure of the 150 possible values, they just have a increased chance of being valid compared to the other natural numbers)

This time Eve doesn't win any info when she finds a ${\displaystyle D_{x}}$ (until she has ${\displaystyle k}$ points).

Suppose again Eve again finds ${\displaystyle D_{0}=\left(1,1494\right)}$ and ${\displaystyle D_{1}=\left(2,329\right)}$, this time the public info is: ${\displaystyle n=6,k=3,p=1613,f(x)=a_{0}+a_{1}x+\dots +a_{k-1}x^{k-1}\mod {p},a_{0}=S,a_{i}\in \mathbb {N} }$ so she: Template:Ordered list This time she can't stop because ${\displaystyle (m_{2}-m_{1})}$ could be any integer (even negative if ${\displaystyle m_{2}>m_{1}}$) so there are a infinite amount of possible values for ${\displaystyle a_{1}}$. She knows that ${\displaystyle [448,445,442,...]}$ always decreases by 3 so if ${\displaystyle 1613}$ was divisible by ${\displaystyle 3}$ she could conclude ${\displaystyle a_{1}\in [1,4,7,\dots ]}$ but because it's prime she can't even conclude that and so she didn't win any information.

#### Javascript example

var prime = 257;

/* Split number into the shares */
function split(number, available, needed) {
var coef = [number, 166, 94], x, exp, c, accum, shares = [];
/* Normally, we use the line:
* for(c = 1, coef[0] = number; c < needed; c++) coef[c] = Math.floor(Math.random() * (prime  - 1));
* where (prime - 1) is the maximum allowable value.
* However, to follow this example, we hardcode the values:
* coef = [number, 166, 94];
* For production, replace the hardcoded value with the random loop
* For each share that is requested to be available, run through the formula plugging the corresponding coefficient
* The result is f(x), where x is the byte we are sharing (in the example, 1234)
*/
for(x = 1; x <= available; x++) {
/* coef = [1234, 166, 94] which is 1234x^0 + 166x^1 + 94x^2 */
for(exp = 1, accum = coef[0]; exp < needed; exp++) accum = (accum + (coef[exp] * (Math.pow(x, exp) % prime) % prime)) % prime;
/* Store values as (1, 132), (2, 66), (3, 188), (4, 241), (5, 225) (6, 140) */
shares[x - 1] = [x, accum];
}
return shares;
}

/* Gives the decomposition of the gcd of a and b.  Returns [x,y,z] such that x = gcd(a,b) and y*a + z*b = x */
function gcdD(a,b) {
if (b == 0) return [a, 1, 0];
else {
var n = Math.floor(a/b), c = a % b, r = gcdD(b,c);
return [r[0], r[2], r[1]-r[2]*n];
}
}

/* Gives the multiplicative inverse of k mod prime.  In other words (k * modInverse(k)) % prime = 1 for all 1 <= k < prime */
function modInverse(k) {
k = k % prime;
var r = (k < 0) ? -gcdD(prime,-k)[2] : gcdD(prime,k)[2];
return (prime + r) % prime;
}

/* Join the shares into a number */
function join(shares) {
var accum, count, formula, startposition, nextposition, value, numerator, denominator;
for(formula = accum = 0; formula < shares.length; formula++) {
/* Multiply the numerator across the top and denominators across the bottom to do Lagrange's interpolation
* Result is x0(2), x1(4), x2(5) -> -4*-5 and (2-4=-2)(2-5=-3), etc for l0, l1, l2...
*/
for(count = 0, numerator = denominator = 1; count < shares.length; count++) {
if(formula == count) continue; // If not the same value
startposition = shares[formula][0];
nextposition = shares[count][0];
numerator = (numerator * -nextposition) % prime;
denominator = (denominator * (startposition - nextposition)) % prime;
}
value = shares[formula][1];
accum = (prime + accum + (value * numerator * modInverse(denominator))) % prime;
}
return accum;
}

var sh = split(129, 6, 3) /* split the secret value 129 into 6 components - at least 3 of which will be needed to figure out the secret value */
var newshares = [sh[1], sh[3], sh[4]]; /* pick any any selection of 3 shared keys from sh */

## Properties

Some of the useful properties of Shamir's ${\displaystyle \left(k,n\right)\,\!}$ threshold scheme are:

1. Secure: Information theoretic security.
2. Minimal: The size of each piece does not exceed the size of the original data.
3. Extensible: When ${\displaystyle k\,\!}$ is kept fixed, ${\displaystyle D_{i}\,\!}$ pieces can be dynamically added or deleted without affecting the other pieces.
4. Dynamic: Security can be easily enhanced without changing the secret, but by changing the polynomial occasionally (keeping the same free term) and constructing new shares to the participants.
5. Flexible: In organizations where hierarchy is important, we can supply each participant different number of pieces according to their importance inside the organization. For instance, the president can unlock the safe alone, whereas 3 secretaries are required together to unlock it.

## References

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.