You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
arnaucube c63b841899 Add blog link, small pending updates 3 years ago
..
wasm add Rust implementation, reorganize directories 5 years ago
LICENSE add Rust implementation, reorganize directories 5 years ago
README.md Add blog link, small pending updates 3 years ago
go.mod add Rust implementation, reorganize directories 5 years ago
go.sum add Rust implementation, reorganize directories 5 years ago
shamirsecretsharing.go Add blog link, small pending updates 3 years ago
shamirsecretsharing_test.go Add blog link, small pending updates 3 years ago

README.md

go-shamirsecretsharing Go Report Card GoDoc

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.

Wasm usage

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);

Usage from Go

// 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")
}