package sm2m

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"xdx.jelly/xgcl/grand"
	"xdx.jelly/xgcl/he/paillier"
	"xdx.jelly/xgcl/sm/sm2"
)

func TestBlindSign(t *testing.T) {
	// 生成协同密钥
	rnd := make([]byte, 32)
	clientKey, clientTempKey, err := ClientGenSignKey_one(nil)
	assert.Nil(t, err)

	_, _ = grand.GenerateRandom(rnd)
	serverKey, serverTempKey, publicKey, err := ServerGenSignKey(clientTempKey, rnd)
	assert.Nil(t, err)

	err = ClientGenSignKey_two(clientKey, serverTempKey, publicKey)
	assert.Nil(t, err)

	// 生成paillier密钥
	psk, ppk, err := paillier.GenerateKey(2048)
	assert.Nil(t, err)

	// 盲协同签名
	e := make([]byte, 32)

	c := NewClientBlindSignContext(publicKey, psk, grand.Reader)
	ecipher, p, err := c.Initial(e)
	assert.Nil(t, err)

	rcipher, s1, s2cipher, err := ServerBlindSign(serverKey, ecipher, p, ppk, grand.Reader)
	assert.Nil(t, err)

	sig, err := c.Final(clientKey, rcipher, s1, s2cipher)
	assert.Nil(t, err)

	assert.True(t, sm2.Verify(e, publicKey, sig))
}
