A simple implementation of Shamir's Secret Sharing configured to use a finite field in GF(2^8) with 128 bit padding.
Much of what you see in this module has been ported from or directly influenced by secrets.js, c-sss, and libgfshare
$ npm install shamirs-secret-sharing
import sss from 'shamirs-secret-sharing'
const secret = Buffer.from('secret key')
const shares = sss.split(secret, { shares: 10, threshold: 4 })
const recovered = sss.combine(shares.slice(3, 7))
console.log(recovered.toString()) // 'secret key'
Example Usage (Socket Runtime)
Configure the [build.copy-map]
section in socket.ini
:
[build.copy-map]
node_modules/shamirs-secret-sharing = shares-secret-sharing
import Buffer form 'socket:buffer'
import combine from 'npm:shares-secret-sharing/combine.js'
import split from 'npm:shares-secret-sharing/split.js'
const secret = Buffer.from('secret key')
const shares = sss.split(secret, { shares: 10, threshold: 4 })
const recovered = sss.combine(shares.slice(3, 7))
console.log(recovered.toString()) // 'secret key'
Generate a set of unique and distinct shares for a secret with a configured threshold.
secret
(required) - ABuffer
instance orstring
that represents a secret for which shares are created foropts
(required) - An object of options for configuring how shares are created for a secretopts.shares
(required) - The number ofn
shares that should be created for this secretopts.threshold
(required) - The number oft
ofn
distinct share that are required to reconstruct this secretopts.random
(optional) - An optional Pseudorandom number generator (PRNG) function that should generate a random value buffer based on some input. e.gopts.random = (size) => randomBytes(size)
Reconstruct a secret from a distinct set of shares. This function will
not throw an error for incorrect shares or if p(0)
is not the correct
secret for the given shares.
shares
(required) - An array of shares, that is an array of equally sized and distinctBuffer
instances, or strings
- https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing
- https://en.wikipedia.org/wiki/Secret_sharing
- https://en.wikipedia.org/wiki/Lagrange_polynomial
- https://en.wikipedia.org/wiki/Horner%27s_method
- https://en.wikipedia.org/wiki/Pseudorandom_number_generator
- https://codesandbox.io/s/shamirs-secret-sharing-pcsbk
MIT