View Javadoc
1   package civitas.crypto.ciphertext;
2   
3   import civitas.crypto.ciphertextlist.CiphertextList;
4   import civitas.crypto.parameters.ElGamalParameters;
5   import civitas.util.CivitasBigInteger;
6   
7   public class MultiplyCiphertexts {
8   
9   	public CiphertextList apply(final ElGamalCiphertextish[][] ciphertexts, final ElGamalParameters parameters) {
10  		if (ciphertexts == null || ciphertexts[0] == null) {
11  			return new CiphertextList();
12  		}
13  
14  		CivitasBigInteger[] aAccum = new CivitasBigInteger[ciphertexts[0].length];
15  		CivitasBigInteger[] bAccum = new CivitasBigInteger[ciphertexts[0].length];
16  		for (ElGamalCiphertextish[] ciphertext : ciphertexts) {
17  			for (int index = 0; index < ciphertext.length; index++) {
18  				ElGamalCiphertextish s = ciphertext[index];
19  				if (aAccum[index] == null) {
20  					aAccum[index] = s.getA();
21  					bAccum[index] = s.getB();
22  				} else {
23  					aAccum[index] = aAccum[index].modMultiply(s.getA(), parameters.p);
24  					bAccum[index] = bAccum[index].modMultiply(s.getB(), parameters.p);
25  				}
26  			}
27  		}
28  		CiphertextList result = new CiphertextList();
29  		for (int index = 0; index < aAccum.length; index++) {
30  			if (aAccum[index] == null || bAccum[index] == null) {
31  				return new CiphertextList();
32  			}
33  			result.add(new ElGamalCiphertext(aAccum[index], bAccum[index]));
34  		}
35  		return result;
36  	}
37  }