package sm2_test

import (
	"fmt"
	"testing"

	"xdx.jelly/xgcl/grand"
	"xdx.jelly/xgcl/sm/sm2"
)

func TestIssue8(t *testing.T) {
	for i := 0; i < 100000000; i++ {
		//err := gcl.SDF_GenerateKeyPair_ECC(nil, sdf.SGD_SM2, 256, pubKey, priKey)
		//if err != nil {
		//	fmt.Println("gcl.SDF_GenerateKeyPair_ECC " + err.Error())
		//	return
		//}

		////生成32字节随机数
		//rnd := grand.GetRandom(32)
		//
		////使用随机数生成私钥
		//pri, err := sm2.GenPrivateKey(grand.Reader)
		//if err != nil {
		//	fmt.Println("sm2.GenPrivateKey " + err.Error())
		//	return
		//}
		//
		////使用私钥生成公钥
		//pub := sm2.GenPublicKey(pri)
		pri, pub, err := sm2.GenerateKeyPairs(grand.Reader)
		if err != nil {
			fmt.Println("sm2.GenerateKeyPairs " + err.Error())
			return
		}

		//publicKey, _ := convert.ECCrefPublicKeytoPublicKey(pubKey)
		//privateKey, _ := convert.ECCrefPrivateKeytoPrivateKey(priKey)

		data := "12345678901234561234567890123456"
		r, s, err := sm2.SignWithReader(grand.Reader, pri, []byte(data))
		if err != nil {
			fmt.Println("sm2.SignWithReader " + err.Error())
			return
		}
		//signValue, err := sm2.Sign([]byte(data), grand.GetRandom(32), pri)
		//if err != nil {
		//	fmt.Println("sm2.Sign " + err.Error())
		//	return
		//}

		signValue := &sm2.Signature{
			R: r,
			S: s,
		}

		if !sm2.Verify([]byte(data), pub, signValue) {
			fmt.Println("sm2.Verify error")
			fmt.Printf("pub = %s\n", pub.String() /* sdf.PublicKeyStructToByte(pubKey)*/)
			fmt.Printf("pri = %s\n", pri.String() /* sdf.PrivateKeyStructToByte(priKey)*/)
			return
		}
		if i%100000 == 0 {
			fmt.Printf("i = %d\n", i)
		}
	}
}
