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.

47 lines
1.1 KiB

  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, ok := new(big.Int).SetString("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", 10)
  11. assert.True(t, ok)
  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(p, sharesToUse)
  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("recovered 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. assert.Equal(t, k, secr)
  40. }