//go:build ignore
// +build ignore


package paillier

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"xdx.jelly/xgcl/grand"
	"xdx.jelly/xgcl/sm/sm4"
)

func TestEnv(t *testing.T) {
	sk, pk, err := GenerateKey(2048)
	assert.Nil(t, err)
	for i := 0; i < 1000; i++ {
		kek := grand.GetRandom(16) //[]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

		mykey := grand.GetRandom(16)
		mykeyPaillierCipher, err := EncryptKey(mykey, pk)
		assert.Nil(t, err)

		iv, mykeyEnvelopeCipher, err := GenEnvelope(mykeyPaillierCipher, kek, pk)
		assert.Nil(t, err)

		mykeyEnvelope, err := DecryptEnvelope(mykeyEnvelopeCipher, sk) // mykeyEnvelope = SM4CTREnc(kek, mykey) = sm4.Enc(kek, iv) ^ key
		assert.Nil(t, err)

		// recover mykey from mykeyEnvelope

		mykey2 := DecryptKey(iv []byte, keyEnvelope []byte, pk *PublicKey)

		// check
		key2, err := sm4.EncryptECB(nil, kek, iv)
		assert.Nil(t, err)

		for i := range key2 {
			key2[i] ^= ctrEncKey[i]
		}
		assert.Equal(t, key, key2)
	}
}

func TestEncKey(t *testing.T) {
	key := grand.GetRandom(128)

	sk, pk, err := GenerateKey(2048)
	assert.Nil(t, err)

	c, err := EncryptKey(key, pk)
	assert.Nil(t, err)

	key2, err := DecryptKey(c, 128, sk)
	assert.Nil(t, err)

	assert.Equal(t, key, key2)

}
