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.

48 lines
1.2 KiB

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