package sm2a

import (
	"math/big"

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

// ServerDecrypt 服务端协同计算，返回[d_s]·C1给客户端
func ServerDecrypt(in []byte, ds *sm2.PrivateKey) ([]byte, error) {
	x := new(big.Int)
	y := new(big.Int)
	x.SetBytes(in[:sm2.ByteSize()])
	y.SetBytes(in[sm2.ByteSize() : 2*sm2.ByteSize()])

	x, y = sm2.Curve256.ScalarMult(x, y, ds.D.Bytes())
	out := make([]byte, 0, 2*sm2.ByteSize())
	out = append(out, gmath.BigIntToNByte(x, sm2.ByteSize())...)
	out = append(out, gmath.BigIntToNByte(y, sm2.ByteSize())...)
	// 中间变量内存置0
	gmath.ClearBigInt(x)
	gmath.ClearBigInt(y)
	return out, nil

}
