arnaucube c63b841899 | 3 years ago | |
---|---|---|
.. | ||
wasm | 5 years ago | |
LICENSE | 5 years ago | |
README.md | 3 years ago | |
go.mod | 5 years ago | |
go.sum | 5 years ago | |
shamirsecretsharing.go | 3 years ago | |
shamirsecretsharing_test.go | 3 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: size of finite field
// k: secret to share
createShares(nNeededShares, nShares, p, k);
// define secret to share
k, ok := new(big.Int).SetString("12345678901234567890123456789012345678", 10)
assert.True(t, ok)
// define the field
p, ok := new(big.Int).SetString("170141183460469231731687303715884105727", 10)
assert.True(t, ok)
// 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")
}