View Javadoc
1   package civitas.crypto.proofknowndisclog;
2   
3   import org.springframework.beans.factory.annotation.Autowired;
4   import org.springframework.stereotype.Controller;
5   
6   import civitas.crypto.CryptoBase;
7   import civitas.crypto.messagedigest.CryptoHash;
8   import civitas.crypto.parameters.ElGamalParameters;
9   import civitas.crypto.privatekey.ElGamalPrivateKey;
10  import civitas.util.CivitasBigInteger;
11  
12  @Controller
13  public class ConstructProofKnowDiscLog {
14  	@Autowired
15  	CryptoBase cryptoBase;
16  
17  	@Autowired
18  	CryptoHash cryptoHash;
19  
20  	public ElGamalProofKnowDiscLog apply(final ElGamalParameters prms, final ElGamalPrivateKey k) {
21  		if (!(k instanceof ElGamalPrivateKey) || prms == null || !(prms instanceof ElGamalParameters)) {
22  			return null;
23  		}
24  		CivitasBigInteger v = prms.g.modPow(k.x(), prms.p);
25  		CivitasBigInteger z = cryptoBase.generateRandomElement(prms.q);
26  		CivitasBigInteger a = prms.g.modPow(z, prms.p);
27  		CivitasBigInteger c = cryptoHash.apply(v, a, null, null).mod(prms.q);
28  		CivitasBigInteger r = z.modAdd(c.modMultiply(k.x(), prms.q), prms.q);
29  		return new ElGamalProofKnowDiscLog(a, c, r, v);
30  	}
31  }