|
|
@ -0,0 +1,55 @@ |
|
|
|
package crypto; |
|
|
|
|
|
|
|
import java.math.BigInteger; |
|
|
|
|
|
|
|
public class WeierstrassPoint { |
|
|
|
public BigInteger x; |
|
|
|
public BigInteger y; |
|
|
|
public WeierstrassCurve curve; |
|
|
|
public boolean isIdentity; |
|
|
|
|
|
|
|
public WeierstrassPoint(WeierstrassCurve curve) |
|
|
|
{ |
|
|
|
this.x = null; |
|
|
|
this.y = null; |
|
|
|
this.curve = curve; |
|
|
|
this.isIdentity = true; |
|
|
|
} |
|
|
|
|
|
|
|
public WeierstrassPoint(WeierstrassCurve curve, BigInteger x, BigInteger y) |
|
|
|
{ |
|
|
|
if (!y.multiply(y).mod(curve.mod).equals(x.modPow(BigInteger.ONE.add(BigInteger.ONE).add(BigInteger.ONE), curve.mod).add(y.multiply(curve.a)).add(curve.b).mod(curve.mod))) |
|
|
|
{ |
|
|
|
throw new ArithmeticException("Invalid points on eliptic curve"); |
|
|
|
} |
|
|
|
this.curve = curve; |
|
|
|
this.x = x; |
|
|
|
this.y = y; |
|
|
|
this.isIdentity = false; |
|
|
|
} |
|
|
|
|
|
|
|
public WeierstrassPoint add(WeierstrassPoint other) |
|
|
|
{ |
|
|
|
BigInteger x, y, m; |
|
|
|
if (this.isIdentity) |
|
|
|
return other; |
|
|
|
if (other.isIdentity) |
|
|
|
return this; |
|
|
|
if (this.x.equals(other.x)) |
|
|
|
{ |
|
|
|
if (this.y.equals(other.y)) |
|
|
|
{ |
|
|
|
m = this.x.multiply(this.x).add(curve.a).multiply(this.y.add(this.y).modInverse(curve.mod)); |
|
|
|
} else |
|
|
|
{ |
|
|
|
return new WeierstrassPoint(this.curve); |
|
|
|
} |
|
|
|
} else |
|
|
|
{ |
|
|
|
m = other.y.add(this.y.negate()).multiply(other.x.add(this.x.negate()).modInverse(curve.mod)); |
|
|
|
} |
|
|
|
x = m.multiply(m).add(this.x.negate()).add(other.x.negate()); |
|
|
|
y = x.add(this.x.negate()).multiply(m).add(this.y); |
|
|
|
return new WeierstrassPoint(this.curve, x, y); |
|
|
|
} |
|
|
|
} |