z_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package tools
  2. import (
  3. "context"
  4. "log"
  5. "math/big"
  6. "testing"
  7. "time"
  8. "git.ali33.ru/fcg-xvii/go-tools/json"
  9. )
  10. func TestSearchP(t *testing.T) {
  11. min := big.NewInt(1)
  12. var g *big.Int
  13. var err error
  14. mmin := big.NewInt(0)
  15. mg := big.NewInt(0)
  16. for {
  17. ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*50))
  18. if min, g, err = SearchP(min, ctx); err != nil {
  19. log.Println(mmin, mg)
  20. t.Fatal(err)
  21. }
  22. mmin, mg = min, g
  23. cancel()
  24. //log.Println(min, g)
  25. min = Add64(min, 1)
  26. }
  27. }
  28. func TestFormula(t *testing.T) {
  29. a, _ := big.NewInt(0).SetString("10", 10)
  30. b, _ := big.NewInt(0).SetString("15", 10)
  31. c, err := NewCurve(a, b)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. t.Log(c.FormulaString())
  36. }
  37. func TestSingular(t *testing.T) {
  38. a := big.NewInt(0)
  39. b := big.NewInt(0)
  40. c, err := NewCurve(a, b)
  41. if err == nil {
  42. t.Fatal(c.FormulaString(), ", не сингулярна")
  43. }
  44. t.Log(err)
  45. }
  46. func TestSearchPrime(t *testing.T) {
  47. a := big.NewInt(101010101010)
  48. b := SearchPrime(a)
  49. t.Log(a, b)
  50. }
  51. func TestBigRandom(t *testing.T) {
  52. for i := 0; i < 10; i++ {
  53. t.Log(Random(big.NewInt(15), big.NewInt(20)))
  54. }
  55. }
  56. func TestPointShow(t *testing.T) {
  57. p := &Point{
  58. y: big.NewInt(105465465463543),
  59. }
  60. t.Log(p.Show())
  61. t.Log(p.ShowHex())
  62. }
  63. func TestCurveG(t *testing.T) {
  64. curve, err := NewCurve(
  65. big.NewInt(2),
  66. big.NewInt(4),
  67. )
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. p := SearchPrime(big.NewInt(2000000))
  72. log.Println("P", p)
  73. if err = curve.SetP(p); err != nil {
  74. t.Fatal(err)
  75. }
  76. //log.Println(curve.a, curve.b, curve.p)
  77. //p1, _, err := curve.searhClosePoints(big.NewInt(1000000))
  78. //log.Println("==============================")
  79. //log.Println(p1.Show())
  80. //log.Println(p2.Show())
  81. //log.Println("==============================")
  82. //return
  83. //log.Println(p1.Show(), p2.Show(), err)
  84. if err = curve.SetGRandom(context.Background()); err != nil {
  85. t.Fatal(err)
  86. }
  87. t.Log("G", curve.g)
  88. }
  89. /*
  90. func TestGe(t *testing.T) {
  91. curve, err := New(
  92. big.NewInt(2),
  93. big.NewInt(4),
  94. )
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. p := SearchPrime(big.NewInt(int64(math.Pow(2, 10))))
  99. log.Println("P", p)
  100. if err = curve.SetP(p); err != nil {
  101. t.Fatal(err)
  102. }
  103. if err = curve.SetGRandom(); err != nil {
  104. t.Fatal(err)
  105. }
  106. t.Log("G", curve.g.Show())
  107. if err = curve.SetN(); err != nil {
  108. t.Fatal(err)
  109. }
  110. t.Log("N", curve.n)
  111. v1, vp1 := curve.GKeyPair()
  112. v2, vp2 := curve.GKeyPair()
  113. t.Log(v1, vp1)
  114. t.Log(v2, vp2)
  115. e1, e2 := curve.GEncode(5, vp2)
  116. t.Log(e1, e2)
  117. curve.GDecode(e1, e2, v2)
  118. }
  119. */
  120. /*
  121. func TestEncryptDecrypt(t *testing.T) {
  122. p := SearchPrime(big.NewInt(int64(math.Pow(2, 10))))
  123. g := Div64(Sub64(p, 1), 2)
  124. priv := &PrivateKey{
  125. PublicKey: PublicKey{
  126. G: g,
  127. P: p,
  128. },
  129. X: big.NewInt(20),
  130. }
  131. priv.Y = new(big.Int).Exp(priv.G, priv.X, priv.P)
  132. message := []byte{5}
  133. c1, c2, err := Encrypt(rand.Reader, &priv.PublicKey, message)
  134. if err != nil {
  135. t.Errorf("error encrypting: %s", err)
  136. }
  137. message2, err := Decrypt(priv, c1, c2)
  138. if err != nil {
  139. t.Errorf("error decrypting: %s", err)
  140. }
  141. if !bytes.Equal(message2, message) {
  142. t.Errorf("decryption failed, got: %x, want: %x", message2, message)
  143. }
  144. log.Println(message, message2)
  145. }
  146. */
  147. func TestJSON(t *testing.T) {
  148. p := big.NewInt(0).SetBytes([]byte{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10})
  149. t.Log(p)
  150. jm := json.Map{
  151. "p": p,
  152. }
  153. jm.LogPretty()
  154. pl := jm.Value("p", big.NewInt(0))
  155. log.Printf("%T %s", pl, pl)
  156. src := []byte(`{
  157. "p": "13"
  158. }`)
  159. var jjm json.Map
  160. json.Unmarshal(src, &jjm)
  161. //pll := jjm.Value("p", big.NewInt(0))
  162. pll, _ := big.NewInt(0).SetString(jjm.StringVal("p", ""), 10)
  163. log.Printf("%T %s", pll, pll)
  164. }
  165. /*
  166. func TestCrypt(t *testing.T) {
  167. jm := json.Map{}
  168. ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*1000))
  169. cr, err := NewCrypt(jm, ctx)
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. c1 := cr.KeysGenerate(context.Background())
  174. c2 := cr.KeysGenerate(context.Background())
  175. log.Println(c1.priv.key, c1.pub.key)
  176. log.Println(c2.priv.key, c2.pub.key)
  177. text := "Hello, World!!! ;)"
  178. log.Println(text)
  179. log.Println([]byte(text))
  180. mes := c2.pub.MessageEncode([]byte(text))
  181. //mes := c2.pub.MessageEncode([]byte{15, 10})
  182. log.Println(mes.a, mes.encrypted)
  183. dData, err := c2.priv.MessageDecode(mes)
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. t.Log(string(dData))
  188. }
  189. */
  190. /*
  191. func TestCryptBrutforceKey(t *testing.T) {
  192. ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*1000))
  193. defer cancel()
  194. cr, err := NewCrypt(json.Map{}, ctx)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. k1 := cr.KeysGenerate(context.Background())
  199. mes := k1.pub.MessageEncode([]byte("Hello, WORLD!!! ;)"))
  200. log.Println(k1.priv.key, k1.pub.key)
  201. cctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*10))
  202. pair, err := cr.BrutforceKey(k1.pub, 4, cctx)
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. log.Println(pair.priv.key, pair.pub.key)
  207. dec, err := pair.priv.MessageDecode(mes)
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. t.Log(string(dec))
  212. }
  213. */
  214. func TestSplit(t *testing.T) {
  215. x := big.NewInt(1000)
  216. parts := 2
  217. firstOffset, lastOffset := big.NewInt(1), big.NewInt(2)
  218. res := Split(x, parts, firstOffset, lastOffset)
  219. log.Println(res)
  220. }
  221. func TestInt(t *testing.T) {
  222. x := big.NewInt(100000000)
  223. b := Bytes32(x)
  224. dx := new(big.Int).SetBytes(b)
  225. t.Log(x, dx, b)
  226. }