3 ms·
For those who would like to play around with Shamir secret sharing, here is a Python implementation I wrote to check out something I was curious about [1]. Note
by tzs 3y ago
For those who would like to play around with Shamir secret sharing, here is a Python implementation I wrote to check out something I was curious about [1]. Note: requires at least Python 3.8.
The function make_sss(secret, num_shares, p=0) makes a polynomial for sharing the given secret that requires num_shares shares to recover. P is the prime. If p is 0 then make_sss will pick a random prime large enough for the secret. Make_sss returns the prime and the polynomial. The polynomial is represented by an array of coefficients.
The function make_share(sss, i) takes a secret sharing scheme sss of the form [p, polynomial] and returns share i.
The function recover_secret(share_list, p) takes an array of num_shares shares and the prime, and return the polynomial. Each share in the share list is represented by an array that contains the share number and the share.
If you run this from the command line it will run a demo. The demo will make a random 128 bit secret, use make_sss to make a sharing system for that secret that requires 3 shares for recover, print the prime and the polynomial, then generate 3 shares (shares 1, 2, and 5), use those shares to recover the polynomial and print the recovered polynomial, and finally print the 3 shares.
This implementation is a bit different from most others I've seen. Most use Lagrange interpolating polynomials to recover the polynomial fro a set of shares. I was curious about whether it would work reasonably to instead just solve the system of linear equations for recovery the way I would have done it by hand in high school--good old fashioned straightforward Gaussian elimination.
[1] https://pastebin.com/zePiXVUj https://pastebin.com/zePiXVUj
- computerfriend 3y agoNice! When I first heard of SSS, but before I looked it up, this was my assumption for how it worked. Seems just as good as polynomial interpolation.