arnaucube 11b4c8ed9f | 5 years ago | |
---|---|---|
.. | ||
wasm | 5 years ago | |
LICENSE | 5 years ago | |
README.md | 5 years ago | |
go.mod | 5 years ago | |
go.sum | 5 years ago | |
shamirsecretsharing.go | 5 years ago | |
shamirsecretsharing_test.go | 5 years ago |
Shamir's Secret Sharing in Go lib + WASM lib
This directory contains a Go lang implementation of Shamir's Secret Sharing, and a compiled Web Assembly version from the Go code to be used from the browser.
Compile to wasm, inside the wasm
directory, execute:
GOARCH=wasm GOOS=js go build -o shamirsecretsharing.wasm shamirsecretsharing-wasm-wrapper.go
Add the file wasm_exec.js
in the directory:
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
Call the library from javascript:
// Create shares from a secret
// nNeededShares: number of secrets needed
// nShares: number of shares
// p: random point
// k: secret to share
createShares(nNeededShares, nShares, p, k);
// define secret to share
k, ok := new(big.Int).SetString("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", 10)
assert.True(t, ok)
// define random prime
p, err := rand.Prime(rand.Reader, bits/2)
assert.Nil(t, err)
// define how many shares want to generate
nShares := big.NewInt(int64(6))
// define how many shares are needed to recover the secret
nNeededShares := big.NewInt(int64(3))
// create the shares
shares, err := Create(
nNeededShares,
nShares,
p,
big.NewInt(int64(k)))
assert.Nil(t, err)
// select shares to use
var sharesToUse [][]*big.Int
sharesToUse = append(sharesToUse, shares[2])
sharesToUse = append(sharesToUse, shares[1])
sharesToUse = append(sharesToUse, shares[0])
// recover the secret using Lagrange Interpolation
secr := LagrangeInterpolation(sharesToUse, p)
// check that the restored secret matches the original secret
if !bytes.Equal(k.Bytes(), secr.Bytes()) {
fmt.Println("reconstructed secret not correspond to original secret")
}