z_test.go 6.1 KB

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