z_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 TestIsCurveSingular(t *testing.T) {
  11. t.Log(IsCurveSingular(intZero, intZero))
  12. t.Log(IsCurveSingular(big.NewInt(2), big.NewInt(4)))
  13. }
  14. func TestCurveRandom(t *testing.T) {
  15. a, b, p := CurveRandomParams(big.NewInt(10000000))
  16. t.Log(a, b, p)
  17. cur, _ := NewCurve(a, b)
  18. cur.SetP(p)
  19. log.Println(cur.SetGRandom(context.Background()))
  20. log.Println(cur.g)
  21. }
  22. func TestSearchP(t *testing.T) {
  23. min := big.NewInt(1)
  24. var g *big.Int
  25. var err error
  26. mmin := big.NewInt(0)
  27. mg := big.NewInt(0)
  28. for {
  29. ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*50))
  30. if min, g, err = SearchP(min, ctx); err != nil {
  31. log.Println(mmin, mg)
  32. t.Fatal(err)
  33. }
  34. mmin, mg = min, g
  35. cancel()
  36. //log.Println(min, g)
  37. min = Add64(min, 1)
  38. }
  39. }
  40. func TestFormula(t *testing.T) {
  41. a, _ := big.NewInt(0).SetString("10", 10)
  42. b, _ := big.NewInt(0).SetString("15", 10)
  43. c, err := NewCurve(a, b)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. t.Log(c.FormulaString())
  48. }
  49. func TestSingular(t *testing.T) {
  50. a := big.NewInt(0)
  51. b := big.NewInt(0)
  52. c, err := NewCurve(a, b)
  53. if err == nil {
  54. t.Fatal(c.FormulaString(), ", не сингулярна")
  55. }
  56. t.Log(err)
  57. }
  58. func TestSearchPrime(t *testing.T) {
  59. a := big.NewInt(101010101010)
  60. b := SearchPrime(a)
  61. t.Log(a, b)
  62. }
  63. func TestBigRandom(t *testing.T) {
  64. for i := 0; i < 10; i++ {
  65. t.Log(Random(big.NewInt(15), big.NewInt(20)))
  66. }
  67. }
  68. func TestPointShow(t *testing.T) {
  69. p := &Point{
  70. y: big.NewInt(105465465463543),
  71. }
  72. t.Log(p.Show())
  73. t.Log(p.ShowHex())
  74. }
  75. func TestCurveG(t *testing.T) {
  76. curve, err := NewCurve(
  77. big.NewInt(2),
  78. big.NewInt(4),
  79. )
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. p := SearchPrime(big.NewInt(2000000))
  84. log.Println("P", p)
  85. if err = curve.SetP(p); err != nil {
  86. t.Fatal(err)
  87. }
  88. if err = curve.SetGRandom(context.Background()); err != nil {
  89. t.Fatal(err)
  90. }
  91. t.Log("G", curve.g)
  92. }
  93. func TestJSON(t *testing.T) {
  94. p := big.NewInt(0).SetBytes([]byte{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10})
  95. t.Log(p)
  96. jm := json.Map{
  97. "p": p,
  98. }
  99. jm.LogPretty()
  100. pl := jm.Value("p", big.NewInt(0))
  101. log.Printf("%T %s", pl, pl)
  102. src := []byte(`{
  103. "p": "13"
  104. }`)
  105. var jjm json.Map
  106. json.Unmarshal(src, &jjm)
  107. pll, _ := big.NewInt(0).SetString(jjm.StringVal("p", ""), 10)
  108. log.Printf("%T %s", pll, pll)
  109. }
  110. func TestSplit(t *testing.T) {
  111. x := big.NewInt(1000)
  112. parts := 2
  113. firstOffset, lastOffset := big.NewInt(1), big.NewInt(2)
  114. res := Split(x, parts, firstOffset, lastOffset)
  115. log.Println(res)
  116. }
  117. func TestInt(t *testing.T) {
  118. x := big.NewInt(100000000)
  119. b := Bytes32(x)
  120. dx := new(big.Int).SetBytes(b)
  121. t.Log(x, dx, b)
  122. }