1 package civitas.crypto.keyshare;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.stereotype.Controller;
5
6 import civitas.crypto.Constants;
7 import civitas.crypto.CryptoException;
8 import civitas.crypto.parameters.ElGamalParameters;
9 import civitas.crypto.publickey.ElGamalPublicKey;
10 import civitas.util.CivitasBigInteger;
11
12 @Controller
13 public class CombineKeyShares implements Constants {
14 @Autowired
15 VerifyElGamalKeyShare verifyElGamalKeyShare;
16
17 public ElGamalPublicKey apply(final ElGamalKeyShare... shares) throws CryptoException {
18 if (shares == null || shares.length == 0) {
19 return null;
20 }
21 CivitasBigInteger product = ONE;
22 ElGamalParameters params = null;
23 for (ElGamalKeyShare share : shares) {
24 if (share == null) {
25 throw new CryptoException("Share is null");
26 }
27
28 if (params == null) {
29 params = share.pubKey().params;
30 }
31 if (!verifyElGamalKeyShare.apply(share)) {
32 throw new CryptoException("Invalid share");
33 }
34
35 product = product.multiply(share.pubKey().y);
36 }
37 return new ElGamalPublicKey(product, params);
38 }
39 }