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
947 B

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