Question

So, I have implemented the following simple class to parallelize the independent encryption processes:

public class SelectionEncryptor implements Callable<BigInteger> {

    private DamgardJurik dj;
    private byte c;
    private int s;

    public SelectionEncryptor(DamgardJurik dj, byte c, int s) {
        this.dj = dj;
        this.c = c;
        this.s = s;
    }

    @Override
    public BigInteger call() throws Exception {

        dj.setS(s);

        BigInteger r = dj.encryption(BigInteger.valueOf(c));
        System.out.println("dj s: " + dj.s + ", given s: " + s + ", c: " + c); 

        return r;
    }

}

s is just a parameter of the cryptosystem we use, but it is an important one determining the depth of the encryption.

I initialize and run my threads as following:

int s = s_max;

ExecutorService executor = Executors.newFixedThreadPool(selectionBits.length);
ArrayList<Future<BigInteger>> list = new ArrayList<Future<BigInteger>>();

// selectionBits is just a byte array holding 1's and 0's
for (int i = 0; i < selectionBits.length; i++) {

    dj.setS(s);

    SelectionEncryptor se = new SelectionEncryptor(dj, selectionBits[i], s);
    System.out.println("submitted: " + selectionBits[i] + " with s " + s + ", dj s: " + dj.s);

    Future<BigInteger> result = executor.submit(se);
    list.add(result);

    s--;
}

// collecting results

for (Future<BigInteger> future : list) {
    try {
        BigInteger f = future.get();
        out.println(f);
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}

executor.shutdown();

The problem is, even if I send the correct s values to the SelectionEncryptor, it just uses different values for it. This is an example output of my program:

submitted: 1 with s 6, dj s: 6
submitted: 0 with s 5, dj s: 6
submitted: 1 with s 4, dj s: 5
submitted: 0 with s 3, dj s: 3
submitted: 1 with s 2, dj s: 2
submitted: 0 with s 1, dj s: 1
dj s: 4, given s: 1, c: 0
dj s: 2, given s: 2, c: 1
dj s: 2, given s: 3, c: 0
dj s: 2, given s: 4, c: 1
dj s: 2, given s: 6, c: 1
dj s: 2, given s: 5, c: 0

I have tried setting s only in my main function, only in the callable class and now I'm setting it in both of them just to be safe but none of them have worked yet.

What can be the source of this problem? Do those callable instances share the DamgardJurick object? What I'm missing?

Sorry about the long question, I couldn't find a way to make it simpler.

Was it helpful?

Solution

All your threads share the same instance of DamgardJurik, and each thread sets its s to the value it got from the main thread. Use a separate instance of DamgardJurik for each thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top