Browse Source

Fixed a few bugs

crypto
Thomas Johnson 3 years ago
parent
commit
9a8f987a46
  1. 14
      crypto/WeierstrassCurve.java
  2. 44
      crypto/WeierstrassPoint.java

14
crypto/WeierstrassCurve.java

@ -14,4 +14,18 @@ public class WeierstrassCurve
this.b = b;
this.mod = mod;
}
@Override
public boolean equals(Object otherobj)
{
if (!(otherobj instanceof WeierstrassCurve))
{
return false;
}
WeierstrassCurve other = (WeierstrassCurve) otherobj;
boolean b = this.a.equals(other.a) && this.b.equals(other.b) && this.mod.equals(other.mod);
if (b) return true;
else return false;
}
}

44
crypto/WeierstrassPoint.java

@ -18,10 +18,15 @@ public class WeierstrassPoint {
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");
}
// if (!y.multiply(y).mod(curve.mod).equals(x.modPow(BigInteger.ONE.add(BigInteger.ONE).add(BigInteger.ONE), curve.mod).add(x.multiply(curve.a)).add(curve.b).mod(curve.mod)))
// {
// System.out.println("x = " + x.toString());
// System.out.println("y = " + y.toString());
// System.out.println("m = " + curve.mod.toString());
// System.out.println("y^2 = " + y.multiply(y).mod(curve.mod).toString());
// System.out.println("x^3 + ax + b = " + x.modPow(BigInteger.ONE.add(BigInteger.ONE).add(BigInteger.ONE), curve.mod).add(x.multiply(curve.a)).add(curve.b).mod(curve.mod).toString());
// throw new ArithmeticException("Invalid points on elliptic curve");
// }
this.curve = curve;
this.x = x;
this.y = y;
@ -35,11 +40,11 @@ public class WeierstrassPoint {
return other;
if (other.isIdentity)
return this;
if (this.x.equals(other.x))
if (this.x.mod(curve.mod).equals(other.x.mod(curve.mod)))
{
if (this.y.equals(other.y))
if (this.y.mod(curve.mod).equals(other.y.mod(curve.mod)))
{
m = this.x.multiply(this.x).add(curve.a).multiply(this.y.add(this.y).modInverse(curve.mod));
m = this.x.multiply(this.x).multiply(new BigInteger(new byte[]{3})).add(curve.a).multiply(this.y.add(this.y).modInverse(curve.mod));
} else
{
return new WeierstrassPoint(this.curve);
@ -50,7 +55,7 @@ public class WeierstrassPoint {
}
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);
return new WeierstrassPoint(this.curve, x.mod(curve.mod), y.negate().mod(curve.mod));
}
public WeierstrassPoint mult(BigInteger n)
@ -68,4 +73,27 @@ public class WeierstrassPoint {
return half.add(half);
}
}
public WeierstrassPoint inv()
{
return new WeierstrassPoint(this.curve, this.x, this.y.negate());
}
@Override
public boolean equals(Object otherobj)
{
if (!(otherobj instanceof WeierstrassPoint))
{
return false;
}
WeierstrassPoint other = (WeierstrassPoint) otherobj;
if (!other.isIdentity && !this.isIdentity)
return true;
if (this.curve.equals(other.curve) && this.isIdentity == other.isIdentity && this.x.equals(other.x) && this.y.equals(other.y))
return true;
else return false;
}
}
Loading…
Cancel
Save