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.

44 lines
970 B

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