actuallyachraf/gomorph

Subtraction?

protosam opened this issue · 3 comments

Is subtraction just not possible due to the use of exponents in the Paillier system? When I was playing around with gaillier.go, for the life of me I couldn't figure out a way to plug subtraction in.

I noticed this when I ran this:

	k := new(big.Int).SetInt64(-10)
	c := new(big.Int).SetInt64(32)

	pub, priv, err := gaillier.GenerateKeyPair(rand.Reader, 256)
	if err != nil {
		log.Fatal(err)
	}

	//encrypt
	encC, err := gaillier.Encrypt(pub, c.Bytes())
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(encC)

	// 32 + -10
	res := gaillier.AddConstant(pub, encC, k.Bytes())

	// Decrypt
	decRes, err := gaillier.Decrypt(priv, res)
	if err != nil {
		log.Fatal(err)
	}

	// Get the result
	result := new(big.Int).SetBytes(decRes)

	fmt.Println(result)

I'm at a bit of a loss as to how to accomplish that because of this:
https://github.com/actuallyachraf/gomorph/blob/master/gaillier/gaillier.go#L161-L163

Specifically new(big.Int).Exp(pubkey.G, k, pubkey.Nsq) is causing any number to be positive, even though I'm making k negative.

I'm still trying to wrap my head around around the purpose of bit.Int.Exp(x, y, m int) and what it is actually doing.

The line you pointed to specifically the go Exp(x,y,m) does modular exponentiation.
As to your request, I am sorry I missed it but Paillier does modular arithmetic over n thus messages can't be negative (0 <= m <= n) is a required condition.