Secret Sharing by Example
cryptography
1. The Scenario
You are haphazardly appointed as the group in charge of authorizing the launch of your country’s nuclear weapons. No idea how you got appointed, maybe you were just unlucky 🤷♂️.
Knowing that you are tasked with managing the launch code, you’ve decided that the only way to ensure no one abuses this code is to share the code in such a way that you and your friends must come together to reveal the secret code.
2. A Failed Attempt
Let's suppose our hypothetical launch code is \( 857392 \) for simplicity's sake.
You might want to give each of your five friends each a concatenated digit and number.
- You get \( 81 \)
- Albert gets \( 52 \)
- Bernard gets \( 73 \)
- Cheryl gets \( 34 \)
- Denise gets \( 95 \)
- Edward gets \( 26 \)
To recover the secret, you’ll need to spell out the launch code in order of increasing numbers. Simple enough.
There's a catch, Bernard isn't too careful and ends up having his secret share (\( 73 \)) stolen. The thief is now in the fortunate position of having to guess fewer numbers in order to brute force the secret code. If it takes \( 10^6 \) tries to brute force a six-digit number, it'll take \( \frac{1}{10} \) the number of tries, hence \( 10^5 \), to brute force a five-digit number. This is not ideal.
Interactive Example
3. A Somewhat Successful Attempt
Let’s do something different to mitigate the risk of having an exposed secret share.
You’ll give each of your friends a random six-digit number.
- Albert gets \( 397467 \)
- Bernard gets \( 645723 \)
- Cheryl gets \( 593239 \)
- Denise gets \( 305724 \)
- Edward gets \( 789942 \)
By adding each number to the original secret code, and using modulus 10 in case of a “carry over”, we can generate a random secret.
\[ \begin{array}{c c c c} &8&5&7&3&9&2&\text{Original Secret}\\ &3&9&7&4&6&7&\text{Albert's Secret}\\ &6&4&5&7&2&3&\text{Bernard's Secret}\\ &5&9&3&2&3&9&\text{Cheryl's Secret}\\ &3&0&5&7&2&4&\text{Denise's Secret}\\ +&7&8&9&9&4&2&\text{Edward's Secret}\\ \hline &2&5&6&2&6&7&\text{Random Secret}\\ \end{array} \]
You'll then keep the random secret \( 256267 \) to yourself.
To recover the secret you’ll need to perform the “reverse” of what you did to generate the random secret.
Note that we can subtract your friend’s secrets from the random secret in any order due to addition’s commutative properties.
\[ \begin{array}{c c c c} &2&5&6&2&6&7&\text{Random Secret}\\ -&3&9&7&4&6&7&\text{Albert's Secret}\\ -&6&4&5&7&2&3&\text{Bernard's Secret}\\ -&5&9&3&2&3&9&\text{Cheryl's Secret}\\ -&3&0&5&7&2&4&\text{Denise's Secret}\\ -&7&8&9&9&4&2&\text{Edward's Secret}\\ \hline &8&5&7&3&9&2&\text{Original Secret}\\ \end{array} \]
When we compare this method (known as a one-time pad or OTP) of generating secret shares to our first attempt. We can see that in the case that a secret share is exposed, an attacker is still required to brute force all \( 10^6 \) combinations since a random secret share doesn't narrow down the brute force search area in any meaningful way.
However, there’s another catch. Edward is also not very careful about his secret share and ends up losing it! Without Edward’s secret share, recovering the secret code is just as hard as brute-forcing it. Somehow, this doesn’t seem ideal.
Interactive Example
4. A Clever Geometric Example
Now let’s address the problems with our previous methods. Rather than representing shares as single numbers, let’s represent shares as points on a graph.
For simplicity, let's assume that the secret is \( 4 \).
The 2 Shares Case
Let's find an arbitrary line that results in a y-intercept of \( 4 \).
\[ y = -x+4 \]
Taking any two points where \( x \neq 0 \), gives us the ability to recover the line hence finding the y-intercept.
We could for example create shares in the following configuration:
- You get \( (1, 3) \)
- Albert gets \( (2, 2) \)
- Bernard gets \( (3, 1) \)
- Cheryl gets \( (4, 0) \)
- Denise gets \( (5, -1) \)
- Edward gets \( (6, -2) \)
You will note that this type of secret sharing scheme has two significant advantages over our previous methods.
- Knowing a share doesn’t improve your chances of knowing the original secret (the y-intercept can still be any arbitrary number).
- Shares can be lost and the original secret can still be recovered.
However, you'll note that allowing two shares to recover the original secret might not be considered secure. Remember, Bernard's share was leaked to the public and Edward's share was lost. If someone happened to find Edward's share, they could recover the original secret. We'll need to think of a way to secure the original secret through \( +3 \) shares.
The 3 Shares Case
Instead of using a line, what if we use a quadratic?
Let’s consider the following quadratic function:
\[ y = x^2-4x+4 \]
We could for example create shares in the following configuration:
- You get \( (1, 1) \)
- Albert gets \( (2, 0) \)
- Bernard gets \( (3, 1) \)
- Cheryl gets \( (4, 4) \)
- Denise gets \( (5, 9) \)
- Edward gets \( (6, 16) \)
Using any three shares, the y-intercept of the quadratic can be recovered.
Question! (I promise it's the only question I'll ask you)
Can picking any two points recover the y-intercept in the 3 shares case?
What if we picked the points \( (2, 0) \) and \( (4, 4) \)? Can you find a quadratic function that fits these two points that is not \( x^2-4x+4 \)?
Reveal the Answer
Let's solve it using a system of equations. \[ 2^2a+2b+c=0 \] \[ 4^2a+4b+c=4 \] Simplifying, \[ 4a+2b+c=0 \] \[ 16a+4b+c=4 \] Solve the system of equations, \[ b=-6a+2 \] \[ c=8a-4 \] Assuming \( a = 2 \), \[ b=-10 \] \[ c=12 \] We can verify that it is indeed a solution, \[ 2^2(2)+2(-10)+12=0 \] \[ 4^2(2)+4(-10)+12=4 \] Here is an interactive visualization of some possible solutions,5. Introducing Shamir’s Secret Sharing
The beauty of this secret sharing scheme is that you can extend the cases by introducing higher and higher order polynomials.
The 2 shares case is known as a \( (2, n) \) scheme and the 3 shares case is known as a \( (3, n) \) scheme.
Interactive Example (using the code below)
6. A Full Shamir’s Secret Sharing Implementation in JavaScript
We’ve talked about how previous secret sharing schemes have specific disadvantages, and we’ve also talked about the basis of how Shamir’s Secret Sharing works. But what does an actual implementation of Shamir’s Secret Sharing look like?
Note that actual implementation of Shamir’s Secret Sharing use polynomials over a finite field and are not representable on a 2D plane. For the mathematical formulation of scheme, here is a good overview.
The following code is ported from Wikipedia’s Shamir’s Secret Sharing Python Implementation.
Note that this implementation is not to be used in production! I am not responsible for any liabilities caused by following code.
|
|
7. Now What?
Whew! We are done! 🎉
I hope this gave you a somewhat brief introduction to the world of secret sharing schemes.
We haven’t covered all the secret sharing schemes out there! However, Shamir’s is widely considered one of the best secret sharing schemes. Here are a few alternative secret sharing schemes:
- Blakley’s Secret Sharing (arxiv.org)
- Secret sharing using the Chinese remainder theorem (wikipedia.org)
With the rise of the Internet, secret sharing schemes are becoming increasingly prevalent and are used in all sorts of applications such as blockchain and cloud computing.
If you find any issues with this article, please let me know at [email protected].
8. Credits
This article is made possible by the following great resources and JS libraries:
- Art of the Problem’s Secret Sharing Explained Visually
- Wikipedia’s Shamir’s Secret Sharing article
- MathJax for math notation
- Mauricio Poppe’s Function Plot for graph plots