arnaucube
arnaucube/cryptofunPublic Self-hosted repository
cryptofun/shamirsecretsharing/README.md
shamirsecretsharing/README.mdRaw
 1## Shamir Secret Sharing
 2- https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing
 3
 4- [x] create secret sharing from number of secrets needed, number of shares, random point p, secret to share
 5- [x] Lagrange Interpolation to restore the secret from the shares
 6
 7#### Usage
 8```go
 9// define secret to share
10k := 123456789
11
12// define random prime
13p, err := rand.Prime(rand.Reader, bits/2)
14if err!=nil {
15	fmt.Println(err)
16}
17
18// define how many shares want to generate
19nShares := big.NewInt(int64(6))
20
21// define how many shares are needed to recover the secret
22nNeededShares := big.NewInt(int64(3))
23
24// create the shares
25shares, err := Create(
26	nNeededShares,
27	nShares,
28	p,
29	big.NewInt(int64(k)))
30assert.Nil(t, err)
31if err!=nil {
32	fmt.Println(err)
33}
34
35// select shares to use
36var sharesToUse [][]*big.Int
37sharesToUse = append(sharesToUse, shares[2])
38sharesToUse = append(sharesToUse, shares[1])
39sharesToUse = append(sharesToUse, shares[0])
40
41// recover the secret using Lagrange Interpolation
42secr := LagrangeInterpolation(sharesToUse, p)
43
44// check that the restored secret matches the original secret
45if !bytes.Equal(k.Bytes(), secr.Bytes()) {
46	fmt.Println("reconstructed secret not correspond to original secret")
47}
48```