z_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package dhellman
  2. import (
  3. "context"
  4. "log"
  5. "math/big"
  6. "testing"
  7. "git.ali33.ru/fcg-xvii/curve/v2/tools"
  8. )
  9. func TestMessage(t *testing.T) {
  10. c, err := tools.NewCurve(
  11. big.NewInt(2),
  12. big.NewInt(4),
  13. )
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. p := tools.SearchPrime(big.NewInt(10000))
  18. if err = c.SetP(p); err != nil {
  19. t.Fatal(err)
  20. }
  21. if err = c.SetGRandom(context.Background()); err != nil {
  22. t.Fatal(err)
  23. }
  24. p1, _ := randomCurveKeyPair(c)
  25. p2, _ := randomCurveKeyPair(c)
  26. log.Println(p1.priv.x, p1.pub.p)
  27. log.Println(p2.priv.x, p2.pub.p)
  28. log.Println(p1.priv.secret(p2.pub))
  29. log.Println(p2.priv.secret(p1.pub))
  30. message := []byte("Hello, WORLD!!!")
  31. encoded, err := p1.messageEncode(message, p2.pub)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. log.Println(encoded)
  36. data, err := p2.messageDecode(encoded, p1.pub)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. log.Println(string(data))
  41. aPair, err := p2.pub.attack(context.Background())
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. p2.Map().LogPretty()
  46. aPair.Map().LogPretty()
  47. log.Println(p1.priv.secret(p2.pub))
  48. log.Println(p2.priv.secret(p1.pub))
  49. log.Println(aPair.priv.secret(p1.pub))
  50. data, err = p2.messageDecode(encoded, p1.pub)
  51. log.Println(string(data), err)
  52. data, err = aPair.messageDecode(encoded, p1.pub)
  53. log.Println(string(data), err)
  54. }