BigInteger pow function not being calculated in Paillier Encryption

94 views Asked by At

Here is my Paillier class:

public class Paillier {
    private static BigInteger Nv;
    private static BigInteger Nc;
    private static BigInteger b;
    private static BigInteger p;
    private static BigInteger q;
    private static BigInteger n;
    private static BigInteger nsquared;
    private static BigInteger lambda;
    private static BigInteger g;
    private static BigInteger mu;

    public Paillier(BigInteger cNv, BigInteger cNc, BigInteger cb, BigInteger cp, BigInteger cq) {
        this.Nv = cNv;
        this.Nc = cNc;
        this.b = cb;
        this.p = cp;
        this.q = cq;

        keyGeneration();
    }

    public static BigInteger getn() {
        return n;
    }

public static void keyGeneration() {
    n = p.multiply(q);
    nsquared = n.multiply(n);

    lambda = carmichaelFunction();

    g = new BigInteger("1578485334874745");
    BigInteger gModPow = LFunction(g.modPow(lambda, nsquared));

    mu = gModPow.modInverse(n);

    /**System.out.print("Encryption key: (" + n + ", " + g + ") \n");
     System.out.print("Decryption key: (" + lambda + ", " + mu + ") \n");**/
}

public static BigInteger carmichaelFunction() {
    BigInteger product = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
    BigInteger lamb = product.divide(p.subtract(BigInteger.ONE).gcd(q.subtract(BigInteger.ONE)));

    return lamb;
}

public static BigInteger LFunction(BigInteger u) {
    BigInteger L = u.subtract(BigInteger.ONE).divide(n);

    return L;
}

public BigInteger encryption(int[] votes) {
    BigInteger m = BigInteger.ZERO;

    for (BigInteger i = BigInteger.ZERO; i.compareTo(Nc) == -1; i = i.add(BigInteger.ONE)) {
        if (votes[i.intValue()] == 1)
            m = m.add(b.pow(i.intValue()));
    }

    Log.d("MESSAGE","Message to be encrypted "+ m);
    Log.d("n ", n.toString());
    Log.d("nsquared ", nsquared.toString());

    BigInteger r = randomZStarN();
    Log.d("RandomZStarN ", r.toString());

    BigInteger gmi = g.pow(m.intValue());
    Log.d("gmi ", gmi.toString());
    BigInteger rin = r.pow(n.intValue());
    Log.d("rin ", rin.toString());
    BigInteger gMr = gmi.multiply(rin);
    Log.d("gMr ", gMr.toString());
    BigInteger c = gMr.mod(nsquared);
    Log.d("Ciphertext ", c.toString());

    return c;
}

public static BigInteger randomZStarN() {
    BigInteger r;

    do {
        r = new BigInteger(n.bitLength(), new Random());
    } while (r.compareTo(n) >= 0 || r.gcd(n).intValue() != 1);

    return r;
}
}

In the encryption method, the variable rin is not being calculated and logged and so are the variables after it. The Android application then stops responding. I am assuming it's because c is returning a null value.

The output is as follows and the log d doesn't display the value of rin:

01-15 11:03:22.593 3468-3468/com.example.oct30th D/MESSAGE: Message to be encrypted 1
01-15 11:03:22.593 3468-3468/com.example.oct30th D/n: 1291588321
01-15 11:03:22.593 3468-3468/com.example.oct30th D/nsquared: 1668200390943599041
01-15 11:03:22.594 3468-3468/com.example.oct30th D/RandomZStarN: 75325222
01-15 11:03:22.594 3468-3468/com.example.oct30th D/gmi: 1578485334874745

Can someone please help?

0

There are 0 answers