github.com/emmansun/gmsm@v0.29.1/sm9/bn256/gt_test.go (about)

     1  package bn256
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"encoding/hex"
     7  	"testing"
     8  )
     9  
    10  func TestGTMarshal(t *testing.T) {
    11  	expected, _ := hex.DecodeString("256943fbdb2bf87ab91ae7fbeaff14e146cf7e2279b9d155d13461e09b22f5230167b0280051495c6af1ec23ba2cd2ff1cdcdeca461a5ab0b5449e90913083105e7addaddf7fbfe16291b4e89af50b8217ddc47ba3cba833c6e77c3fb027685e79d0c8337072c93fef482bb055f44d6247ccac8e8e12525854b3566236337ebe082cde173022da8cd09b28a2d80a8cee53894436a52007f978dc37f36116d39b3fa7ed741eaed99a58f53e3df82df7ccd3407bcc7b1d44a9441920ced5fb824f7fc6eb2aa771d99c9234fddd31752edfd60723e05a4ebfdeb5c33fbd47e0cf066fa6b6fa6dd6b6d3b19a959a110e748154eef796dc0fc2dd766ea414de7869688ffe1c0e9de45fd0fed790ac26be91f6b3f0a49c084fe29a3fb6ed288ad7994d1664a1366beb3196f0443e15f5f9042a947354a5678430d45ba031cff06db9277f7c6d52b475e6aaa827fdc5b4175ac6929320f782d998f86b6b57cda42a042636a699de7c136f78eee2dbac4ca9727bff0cee02ee920f5822e65ea170aa9669")
    12  	x := &GT{gfP12Gen}
    13  	ret := x.Marshal()
    14  	if !bytes.Equal(expected, ret) {
    15  		t.Errorf("expected %x, got %x\n", expected, ret)
    16  	}
    17  }
    18  
    19  func TestGT(t *testing.T) {
    20  	k, Ga, err := RandomGT(rand.Reader)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	ma := Ga.Marshal()
    25  
    26  	Gb := new(GT)
    27  	_, err = Gb.Unmarshal((&GT{gfP12Gen}).Marshal())
    28  	if err != nil {
    29  		t.Fatal("unmarshal not ok")
    30  	}
    31  	Gb.ScalarMult(Gb, k)
    32  	mb := Gb.Marshal()
    33  
    34  	if !bytes.Equal(ma, mb) {
    35  		t.Fatal("bytes are different")
    36  	}
    37  
    38  	_, err = Gb.Unmarshal((&GT{gfP12Gen}).Marshal())
    39  	if err != nil {
    40  		t.Fatal("unmarshal not ok")
    41  	}
    42  	Gc, err := ScalarMultGT(Gb, k.Bytes())
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	mc := Gc.Marshal()
    47  	if !bytes.Equal(ma, mc) {
    48  		t.Fatal("bytes are different")
    49  	}
    50  }
    51  
    52  func BenchmarkGTMarshal(b *testing.B) {
    53  	x := &GT{gfP12Gen}
    54  	b.ReportAllocs()
    55  	b.ResetTimer()
    56  	for i := 0; i < b.N; i++ {
    57  		x.Marshal()
    58  	}
    59  }
    60  
    61  func BenchmarkGT(b *testing.B) {
    62  	x, _ := rand.Int(rand.Reader, Order)
    63  	b.ReportAllocs()
    64  	b.ResetTimer()
    65  
    66  	for i := 0; i < b.N; i++ {
    67  		new(GT).ScalarBaseMult(x)
    68  	}
    69  }
    70  
    71  func BenchmarkPairing(b *testing.B) {
    72  	b.ReportAllocs()
    73  	for i := 0; i < b.N; i++ {
    74  		Pair(&G1{curveGen}, &G2{twistGen})
    75  	}
    76  }