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.

45 lines
953 B

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package shamirsecretsharing
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "fmt"
  6. "math/big"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestCreate(t *testing.T) {
  11. k := big.NewInt(int64(123456789))
  12. p, err := rand.Prime(rand.Reader, bits/2)
  13. assert.Nil(t, err)
  14. nShares := big.NewInt(int64(6))
  15. nNeededShares := big.NewInt(int64(3))
  16. shares, err := Create(
  17. nNeededShares,
  18. nShares,
  19. p,
  20. k)
  21. assert.Nil(t, err)
  22. //generate sharesToUse
  23. var sharesToUse [][]*big.Int
  24. sharesToUse = append(sharesToUse, shares[2])
  25. sharesToUse = append(sharesToUse, shares[1])
  26. sharesToUse = append(sharesToUse, shares[0])
  27. secr := LagrangeInterpolation(sharesToUse, p)
  28. fmt.Print("original secret: ")
  29. fmt.Println(k)
  30. fmt.Print("p: ")
  31. fmt.Println(p)
  32. fmt.Print("shares: ")
  33. fmt.Println(shares)
  34. fmt.Print("secret result: ")
  35. fmt.Println(secr)
  36. if !bytes.Equal(k.Bytes(), secr.Bytes()) {
  37. t.Errorf("reconstructed secret not correspond to original secret")
  38. }
  39. }