I'm trying to learn homomorphism so the problem may also be in my code (which I will include).
I want to compute $a - b$ where $a$ is bigger then $b$. In order to do this I tried to compute $\frac{a}{b}$. However when I decrypt this I get garbage. Or ciphertext $c$ is not in $\mathbb{Z}^{*}_{n^2}$.
Now because the problem may also be in my code here it is:
import java.math.BigInteger;
public class Main {
public static void main(String[] args) throws Exception{
Paillier pp = new Paillier (2048);
BigInteger a=pp.encrypt(BigInteger.valueOf(100000));
BigInteger b=pp.encrypt(BigInteger.valueOf(10000));
BigInteger c=a.divide(b);
c=c.remainder(pp.getNsquare());
BigInteger res=pp.decrypt(c);
System.out.println("res = "+ res);
}
}
And I used this library
I also tried to see what happens if you multiply $b$ with $-1$ but that just gave me garbage. Anyone know how to do this?
So so far I tried
- $c = \frac{a}{b}$ which tends to result in $c=0$ and unable to retrieve $c$
- $c+2^{2047}=\frac{a \times 2^{2047}}{b}$ which results in $c$ is a large number and $c$ is negative some larger number
- $c+2^{2047}=\frac{a \times 2^{2047}}{b}$ inverse mod $N^2$ which results in $c$ is a large number and $c$ is negative some larger number
- $c+2^{2047}=\frac{a \times 2^{2047}}{b}$ remainder $N^2$ which didn't decrypt.
Is this a problem in my code or in my understanding of crypto?