123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- package curve
- import (
- "context"
- "log"
- "math"
- "math/big"
- "testing"
- "time"
- "git.ali33.ru/fcg-xvii/go-tools/json"
- )
- func TestSearchP(t *testing.T) {
- min := big.NewInt(1)
- var g *big.Int
- var err error
- mmin := big.NewInt(0)
- mg := big.NewInt(0)
- for {
- ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*50))
- if min, g, err = SearchP(min, ctx); err != nil {
- log.Println(mmin, mg)
- t.Fatal(err)
- }
- mmin, mg = min, g
- cancel()
- //log.Println(min, g)
- min = Add64(min, 1)
- }
- }
- func TestFormula(t *testing.T) {
- a, _ := big.NewInt(0).SetString("10", 10)
- b, _ := big.NewInt(0).SetString("15", 10)
- c, err := New(a, b)
- if err != nil {
- t.Fatal(err)
- }
- t.Log(c.FormulaString())
- }
- func TestSingular(t *testing.T) {
- a := big.NewInt(0)
- b := big.NewInt(0)
- c, err := New(a, b)
- if err == nil {
- t.Fatal(c.FormulaString(), ", не сингулярна")
- }
- t.Log(err)
- }
- func TestSearchPrime(t *testing.T) {
- a := big.NewInt(101010101010)
- b := SearchPrime(a)
- t.Log(a, b)
- }
- func TestBigRandom(t *testing.T) {
- for i := 0; i < 10; i++ {
- t.Log(Random(big.NewInt(15), big.NewInt(20)))
- }
- }
- func TestPointShow(t *testing.T) {
- p := &Point{
- y: big.NewInt(105465465463543),
- }
- t.Log(p.Show())
- t.Log(p.ShowHex())
- }
- func TestCurveG(t *testing.T) {
- curve, err := New(
- big.NewInt(2),
- big.NewInt(4),
- )
- if err != nil {
- t.Fatal(err)
- }
- p := SearchPrime(big.NewInt(2000000))
- log.Println("P", p)
- if err = curve.SetP(p); err != nil {
- t.Fatal(err)
- }
- //log.Println(curve.a, curve.b, curve.p)
- //p1, _, err := curve.searhClosePoints(big.NewInt(1000000))
- //log.Println("==============================")
- //log.Println(p1.Show())
- //log.Println(p2.Show())
- //log.Println("==============================")
- //return
- //log.Println(p1.Show(), p2.Show(), err)
- if err = curve.SetGRandom(); err != nil {
- t.Fatal(err)
- }
- t.Log("G", curve.g.Show())
- if err = curve.SetN(); err != nil {
- t.Fatal(err)
- }
- t.Log(curve.n)
- }
- func TestKeyPairs(t *testing.T) {
- curve, err := New(
- big.NewInt(2),
- big.NewInt(4),
- )
- if err != nil {
- t.Fatal(err)
- }
- p := SearchPrime(big.NewInt(int64(math.Pow(2, 10))))
- log.Println("P", p)
- if err = curve.SetP(p); err != nil {
- t.Fatal(err)
- }
- if err = curve.SetGRandom(); err != nil {
- t.Fatal(err)
- }
- t.Log("G", curve.g.Show())
- if err = curve.SetN(); err != nil {
- t.Fatal(err)
- }
- t.Log("N", curve.n)
- priv1, pub1, err := curve.RandomKeyPair()
- if err != nil {
- t.Fatal(err)
- }
- t.Log(priv1, pub1)
- priv2, pub2, err := curve.RandomKeyPair()
- if err != nil {
- t.Fatal(err)
- }
- t.Log(priv2, pub2)
- log.Println(curve.PointSecret(priv1, pub2))
- log.Println(curve.PointSecret(priv2, pub1))
- p1, err := curve.ELKeyPair()
- if err != nil {
- t.Fatal(err)
- }
- t.Log(p1.priv.d)
- t.Log(p1.pub.e1, p1.pub.e2)
- mes, err := p1.EncodeMessage([]byte{5})
- if err != nil {
- t.Fatal(err)
- }
- log.Println(mes.c1)
- log.Println(mes.cd)
- p1.DecodeMessage(mes)
- }
- /*
- func TestGe(t *testing.T) {
- curve, err := New(
- big.NewInt(2),
- big.NewInt(4),
- )
- if err != nil {
- t.Fatal(err)
- }
- p := SearchPrime(big.NewInt(int64(math.Pow(2, 10))))
- log.Println("P", p)
- if err = curve.SetP(p); err != nil {
- t.Fatal(err)
- }
- if err = curve.SetGRandom(); err != nil {
- t.Fatal(err)
- }
- t.Log("G", curve.g.Show())
- if err = curve.SetN(); err != nil {
- t.Fatal(err)
- }
- t.Log("N", curve.n)
- v1, vp1 := curve.GKeyPair()
- v2, vp2 := curve.GKeyPair()
- t.Log(v1, vp1)
- t.Log(v2, vp2)
- e1, e2 := curve.GEncode(5, vp2)
- t.Log(e1, e2)
- curve.GDecode(e1, e2, v2)
- }
- */
- /*
- func TestEncryptDecrypt(t *testing.T) {
- p := SearchPrime(big.NewInt(int64(math.Pow(2, 10))))
- g := Div64(Sub64(p, 1), 2)
- priv := &PrivateKey{
- PublicKey: PublicKey{
- G: g,
- P: p,
- },
- X: big.NewInt(20),
- }
- priv.Y = new(big.Int).Exp(priv.G, priv.X, priv.P)
- message := []byte{5}
- c1, c2, err := Encrypt(rand.Reader, &priv.PublicKey, message)
- if err != nil {
- t.Errorf("error encrypting: %s", err)
- }
- message2, err := Decrypt(priv, c1, c2)
- if err != nil {
- t.Errorf("error decrypting: %s", err)
- }
- if !bytes.Equal(message2, message) {
- t.Errorf("decryption failed, got: %x, want: %x", message2, message)
- }
- log.Println(message, message2)
- }
- */
- func TestJSON(t *testing.T) {
- p := big.NewInt(0).SetBytes([]byte{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10})
- t.Log(p)
- jm := json.Map{
- "p": p,
- }
- jm.LogPretty()
- pl := jm.Value("p", big.NewInt(0))
- log.Printf("%T %s", pl, pl)
- src := []byte(`{
- "p": "13"
- }`)
- var jjm json.Map
- json.Unmarshal(src, &jjm)
- //pll := jjm.Value("p", big.NewInt(0))
- pll, _ := big.NewInt(0).SetString(jjm.StringVal("p", ""), 10)
- log.Printf("%T %s", pll, pll)
- }
- func TestCrypt(t *testing.T) {
- jm := json.Map{}
- ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*1000))
- cr, err := NewCrypt(jm, ctx)
- if err != nil {
- t.Fatal(err)
- }
- c1 := cr.KeysGenerate(context.Background())
- c2 := cr.KeysGenerate(context.Background())
- log.Println(c1.priv.key, c1.pub.key)
- log.Println(c2.priv.key, c2.pub.key)
- text := "Hello, World!!! ;)"
- log.Println(text)
- log.Println([]byte(text))
- mes := c2.pub.MessageEncode([]byte(text))
- //mes := c2.pub.MessageEncode([]byte{15, 10})
- log.Println(mes.a, mes.encrypted)
- dData, err := c2.priv.MessageDecode(mes)
- if err != nil {
- t.Fatal(err)
- }
- t.Log(string(dData))
- }
- func TestCryptBrutforceKey(t *testing.T) {
- ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*1000))
- defer cancel()
- cr, err := NewCrypt(json.Map{}, ctx)
- if err != nil {
- t.Fatal(err)
- }
- k1 := cr.KeysGenerate(context.Background())
- mes := k1.pub.MessageEncode([]byte("Hello, WORLD!!! ;)"))
- log.Println(k1.priv.key, k1.pub.key)
- cctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*10))
- pair, err := cr.BrutforceKey(k1.pub, 4, cctx)
- if err != nil {
- t.Fatal(err)
- }
- log.Println(pair.priv.key, pair.pub.key)
- dec, err := pair.priv.MessageDecode(mes)
- if err != nil {
- t.Fatal(err)
- }
- t.Log(string(dec))
- }
- func TestSplit(t *testing.T) {
- x := big.NewInt(1000)
- parts := 2
- firstOffset, lastOffset := big.NewInt(1), big.NewInt(2)
- res := Split(x, parts, firstOffset, lastOffset)
- log.Println(res)
- }
|