package paillier

import (
	"crypto/rand"
	"math/big"
	"testing"

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

func TestNeg(t *testing.T) {
	sk, err := GenerateKeyFromPassword(2048, grand.GetRandom(32), grand.GetRandom(32), 1024)
	assert.Nil(t, err)
	pk := sk.Public()

	m, _ := rand.Int(grand.Reader, pk.N)
	mNeg := new(big.Int).Sub(pk.N, m) // n - m
	c, _ := Encrypt(m, pk, grand.Reader)

	c.Neg(pk) // Enc(n-m) = Enc(m)^-1 mod n^2

	mm, _ := Decrypt(c, sk)

	assert.Zero(t, mm.Cmp(mNeg))
}
