package elgamal

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

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

func TestElGamal(t *testing.T) {
	x := bigFromBase16("013d5955a5e91b8fed1b56b6bdcd467939de9bfc")
	y := bigFromBase16("b7866990d044b1bccbbcf84c29f145ee17d4f4608c79a55e249e9e108b91e36381944fa3c0c3f51876f63bce7bb30ffde9ca02265e916dd3fb2e060b0dfeaaa67d5a359159b948c3df1141f0e0a22380a3633c1ffbcb1c228ffe4ef0bab52293bfdff4b64e3f362d63b11a4d2507f6e9e98de71aff09fdb64e3737c046044138")
	// M := bigFromBase16("4142434445464748494a31323334353637383930")
	// m := new(big.Int).Exp(P1024.g, M, P1024.p)
	m, _ := rand.Int(grand.Reader, P1024.p)
	sk := &PrivateKey{
		x: *x,
		PublicKey: PublicKey{
			y:      *y,
			Params: P1024,
		},
	}
	pk := &sk.PublicKey
	b, _ := hex.DecodeString("d8d0f2d6a3e2d745ab4410e2042a740e7a81a280")
	c1, err := pk.Encryption(m, b)
	assert.Nil(t, err)
	c2, err := pk.Encryption(m, grand.Reader)
	c, _ := new(Cipher).HomoMap(c1, c2)
	mm0, _ := sk.Decryption(c)
	mm1 := new(big.Int).Mul(m, m)
	mm1.Mod(mm1, pk.Params.p)

	assert.Zero(t, mm0.Cmp(mm1))
}
