package sm2m

import (
	"fmt"
	"math/big"
	"testing"

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

func TestDec(t *testing.T) {
	rnd := make([]byte, 32)
	if _, err := grand.GenerateRandom(rnd); err != nil {
		t.Fatal(err)
	}

	clientKey, clientTempKey, _ := ClientGenSignKey_one(rnd)
	if _, err := grand.GenerateRandom(rnd); err != nil {
		t.Fatal(err)
	}
	serverKey, serverTempKey, pk, _ := ServerGenSignKey(clientTempKey, rnd)
	_ = ClientGenSignKey_two(clientKey, serverTempKey, pk)

	d := new(big.Int).Mul(serverKey.D, clientKey.D)
	d.ModInverse(d, sm2.OrderN())
	d.Sub(d, gmath.BigInt1)

	msg := []byte("1234567890123456")
	c, _ := sm2.Encrypt(pk, msg, grand.GetRandom(32))
	sk := sm2.NewPrivateKey()
	sk.D = d
	msg2, _ := sm2.Decrypt(sk, c)
	fmt.Println(string(msg2))
	clientCTX := NewClientDecContext()
	out, _ := clientCTX.Initial(c)
	out, _ = ServerImportKey(serverKey, out)
	plain, _ := clientCTX.Final(clientKey, out)
	t.Log(string(plain))
}
