mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-23 02:36:20 +08:00
86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
package sm9
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"testing"
|
|
)
|
|
|
|
func TestG2(t *testing.T) {
|
|
k, Ga, err := RandomG2(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ma := Ga.Marshal()
|
|
|
|
Gb := new(G2).ScalarBaseMult(k)
|
|
mb := Gb.Marshal()
|
|
|
|
if !bytes.Equal(ma, mb) {
|
|
t.Errorf("bytes are different, expected %v, got %v", hex.EncodeToString(ma), hex.EncodeToString(mb))
|
|
}
|
|
}
|
|
|
|
func TestG2Marshal(t *testing.T) {
|
|
_, Ga, err := RandomG2(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ma := Ga.Marshal()
|
|
|
|
Gb := new(G2)
|
|
_, err = Gb.Unmarshal(ma)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mb := Gb.Marshal()
|
|
|
|
if !bytes.Equal(ma, mb) {
|
|
t.Errorf("bytes are different, expected %v, got %v", hex.EncodeToString(ma), hex.EncodeToString(mb))
|
|
}
|
|
}
|
|
|
|
func Test_G2MarshalCompressed(t *testing.T) {
|
|
e, e2 := &G2{}, &G2{}
|
|
ret := e.MarshalCompressed()
|
|
_, err := e2.UnmarshalCompressed(ret)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !e2.p.IsInfinity() {
|
|
t.Errorf("not same")
|
|
}
|
|
e.p.Set(twistGen)
|
|
ret = e.MarshalCompressed()
|
|
_, err = e2.UnmarshalCompressed(ret)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e2.p.x != e.p.x || e2.p.y != e.p.y || e2.p.z != e.p.z {
|
|
t.Errorf("not same")
|
|
}
|
|
e.p.Neg(e.p)
|
|
ret = e.MarshalCompressed()
|
|
_, err = e2.UnmarshalCompressed(ret)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e2.p.x != e.p.x || e2.p.y != e.p.y || e2.p.z != e.p.z {
|
|
t.Errorf("not same")
|
|
}
|
|
if e2.p.x == twistGen.x && e2.p.y == twistGen.y && e2.p.z == twistGen.z {
|
|
t.Errorf("not expected")
|
|
}
|
|
}
|
|
|
|
func BenchmarkG2(b *testing.B) {
|
|
x, _ := rand.Int(rand.Reader, Order)
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
new(G2).ScalarBaseMult(x)
|
|
}
|
|
}
|