package implicitcert_test

import (
	"testing"

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

func TestKeyGen(t *testing.T) {
	id := []byte("alice")
	randReader := grand.Reader

	kgcPrivateKey, kgcPublicKey, _ := sm2.GenerateKeyPairs(randReader)
	t.Log("kgcPrivateKey:", kgcPrivateKey)
	t.Log("kgcPublicKey:", kgcPublicKey)
	userKey := implicitcert.NewUserKey()
	u, err := userKey.GeneratePartialKey(randReader)
	if err != nil {
		t.Log("userKey.GeneratePartialKey failed")
		t.Fail()
	}
	kgcGeneratedUserPartialPrivateKey, declarPublicKey, err := implicitcert.KGCComputeUserKey(id, u, kgcPrivateKey, kgcPublicKey, randReader)
	if err != nil {
		t.Log("implicitcert.KGCComputeUserKey failed")
		t.Fail()
	}

	userPrivateKey, err := userKey.GeneratePrivateKey(kgcGeneratedUserPartialPrivateKey)
	if err != nil {
		t.Log("implicitcert.KGCComputeUserKey failed")
		t.Fail()
	}

	t.Log("userPrivateKey: ", userPrivateKey)
	t.Log("declarPublicKey: ", declarPublicKey)

	t.Log("VerifyUserKeyPair: ", implicitcert.VerifyUserKeyPair(id, userPrivateKey, declarPublicKey, kgcPublicKey))
}
