Skip to content

Instantly share code, notes, and snippets.

@tsusanka
Created June 14, 2016 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsusanka/e449f396ccba8867c73578fd0c478010 to your computer and use it in GitHub Desktop.
Save tsusanka/e449f396ccba8867c73578fd0c478010 to your computer and use it in GitHub Desktop.
einf.java
package com.tsusanka.dsa;
import java.math.BigInteger;
class DSA
{
private BigInteger alpha;
private BigInteger beta;
private BigInteger p;
private BigInteger q;
DSA(BigInteger alpha, BigInteger beta, BigInteger p, BigInteger q)
{
this.alpha = alpha;
this.beta = beta;
this.p = p;
this.q = q;
}
Signature sign(BigInteger key, BigInteger ephemeral, BigInteger hash)
{
BigInteger r, s;
r = alpha.modPow(ephemeral, p).mod(q);
s = (hash.add(key.multiply(r))).mod(q).multiply(ephemeral.modInverse(q)).mod(q);
return new Signature(r, s);
}
boolean verify(Signature signature, BigInteger hash)
{
BigInteger w, u1, u2, v;
w = signature.s.modInverse(q);
u1 = w.multiply(hash).mod(q);
u2 = w.multiply(signature.r).mod(q);
v = (alpha.modPow(u1, p).multiply(beta.modPow(u2, p))).mod(p).mod(q);
return v.equals(signature.r);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment