# Shamir's Secret Sharing

This article may be too technical for most readers to understand. (March 2014) |

**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 the secret might be impractical, and therefore sometimes the *threshold scheme* is used where any of the parts are sufficient to reconstruct the original secret.

## Contents

## Mathematical definition

The goal is to divide secret (e.g., a safe combination) into pieces of data in such a way that:

- Knowledge of any or more pieces makes easily computable.
- Knowledge of any or fewer pieces leaves completely undetermined (in the sense that all its possible values are equally likely).

This scheme is called threshold scheme. If then all participants are required to reconstruct the secret.

## Shamir's secret-sharing scheme

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 points to define a polynomial of degree .

Suppose we want to use a threshold scheme to share our secret , without loss of generality assumed to be an element in a finite field of size where and is a prime number.

Choose at random positive integers with , and let . Build the polynomial . Let us construct any points out of it, for instance set to retrieve . Every participant is given a point (an integer input to the polynomial, and the corresponding integer output). Given any subset of of these pairs, we can find the coefficients of the polynomial using interpolation. The secret is the constant term .

## 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 .

We wish to divide the secret into 6 parts , where any subset of 3 parts is sufficient to reconstruct the secret. At random we obtain two () numbers: 166 and 94.

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

We construct 6 points from the polynomial:

We give each participant a different single point (both and ). Because we use instead of the points start from and not . This is necessary because if one would have he would also know the secret ()

#### Reconstruction

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

Let us consider .

We will compute Lagrange basis polynomials:

Therefore

Recall that the secret is the free coefficient, which means that , and we are done.

#### Computationally Efficient Approach

Considering that the goal of using polynomial interpolation is to find a constant in a source polynomial using Lagrange polynomials "as it is" is not efficient, since unused constant are calculated.

An optimized approach to use Lagrange polynomials to find is defined as following:

##### Problem

Although this method works fine^{[citation needed]}, there is a security problem: Eve wins a lot of information about with every that she finds.

Suppose that she finds the 2 points and , she still doesn't have points so in theory she shouldn't have won anymore info about . But she combines the info from the 2 points with the public info: and she :

- fills the -formula with and the value of
- fills (i) with the values of 's and
- fills (i) with the values of 's and
- does (iii)-(ii): and rewrites this as
- knows that so she starts replacing in (iv) with 0, 1, 2, 3, ... to find all possible values for :
- replaces by (iv) in (ii):
- replaces in (vi) by the values found in (v) so she gets which leads her to the information:

. She now only has 150 numbers to guess from instead of an infinite number of natural numbers.

#### Solution

This problem can be fixed by using finite field arithmetic in a field of size .

This is in practice only a small change, it just means that we should choose a prime that is bigger than the number of participants and every (including ) and we have to calculate the points as instead of .

Since everyone who receives a point also has to know the value of so it may be considered to be publicly known. Therefore, one should select a value for that is not too low.

Low values of are risky because Eve knows , so the lower one sets , the lower the number of possible values Eve has to guess from to get .

For this example we choose , so our polynomial becomes which gives the points:

This time Eve doesn't win any info when she finds a (until she has points).

Suppose again Eve again finds and , this time the public info is: so she:

- fills the -formula with and the value of and :
- fills (i) with the values of 's and
- fills (i) with the values of 's and
- does (iii)-(ii): and rewrites this as
- knows that so she starts replacing in (iv) with 0, 1, 2, 3, ... to find all possible values for :

This time she can't stop because could be any integer (even negative if ) so there are an infinite amount of possible values for . She knows that always decreases by 3 so if was divisible by she could conclude 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 prime > k >= 1 */
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 selection of 3 shared keys from sh */
alert(join(newshares));
```

## Properties

Some of the useful properties of Shamir's threshold scheme are:

**Secure**: Information theoretic security.**Minimal**: The size of each piece does not exceed the size of the original data.**Extensible**: When is kept fixed, pieces can be dynamically added or deleted without affecting the other pieces.**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.**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.

## See also

- Secret sharing
- Lagrange polynomial
- Homomorphic secret sharing - A simplistic decentralized voting protocol.
- Two-man rule
- Partial Password

## References

- Shamir, Adi (1979), "How to share a secret",
*Communications of the ACM*,**22**(11): 612–613, doi:10.1145/359168.359176.

- Liu, C. L. (1968),
*Introduction to Combinatorial Mathematics*, New York: McGraw-Hill. - Dawson, E.; Donovan, D. (1994), "The breadth of Shamir's secret-sharing scheme",
*Computers & Security*,**13**: 69–78, doi:10.1016/0167-4048(94)90097-3. - Knuth, D. E. (1997),
*The Art of Computer Programming*, II: Seminumerical Algorithms (3rd ed.), Addison-Wesley, p. 505.

## External links

- A proper Javascript implementation of Shamir's secret sharing scheme with open source (MIT) license
- ShamirSecretSharing: An open source (BSD-license) Python implementation of Shamir's algorithm
- ssss: An open source (GPL) implementation of Shamir's Scheme with online demo
- Secret Sharp: An open source (GPL) implementation of Shamir's Scheme for windows
- Christophe David's web based implementation of Shamir's scheme 'How to share a Secret'
- Shamir's Secret Sharing in Java : An open source (LGPL) implementation of Shamir's scheme in Java
- An open source implementation of the Shamir's Secret Sharing as open Web application, augmented by additional security features
- libgfshare: a secret sharing library in GF(2**8), opensource (MIT)
- Web implementation of Shamir's method
- Java library implementation of multiple secret sharing methods, opensource(LGPLv2)

- Pages with syntax highlighting errors
- Wikipedia articles that are too technical from March 2014
- All articles that are too technical
- Articles needing expert attention from March 2014
- All articles needing expert attention
- Pages with broken file links
- All articles with unsourced statements
- Articles with unsourced statements from April 2016
- Secret sharing
- Information-theoretically secure algorithms