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).
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 (
)
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.
Although this method works fine, 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
:






After
she stops because she reasons that if she continues she would get negative values for
(which is impossible because
), she can now conclude ![{\displaystyle a_{2}\in [0,1,\dots ,148,149]}](/media/api/rest_v1/media/math/render/svg/4f12fd12f02c84cc013677da439605632f004991)
- 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 a infinitive number of natural numbers.
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 both the secret and the number of participants and we have to calculate the points as
instead of
.
Everyone that receives a point also has to know the value of
so it's publicly known so you should choose a value for
that is not too low because Eve knows
, so the lower you choose
, the lower the number of possible values Eve has to guess from to get
.
You should also not choose it too high because Eve knows that the chance for
increases with a higher
and she can use the procedure from the original problem to guess
(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)
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 a 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.
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; // Modular math
/*
* Store values as (1, 1494), (2, 1942), (3, 2578), (4, 3402), (5, 4414) (6, 5614)
*/
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 */
alert(join(newshares));