Elliptic Curve Digital Signature Algorithm: Difference between revisions

Content added Content deleted
m (Improved coding.)
m (Minor improvements to coding.)
Line 951: Line 951:


for ( Parameter parameter : parameters ) {
for ( Parameter parameter : parameters ) {
EllipticCurve ellipticCurve = new EllipticCurve(parameter);
EllipticCurve ellipticCurve = new EllipticCurve(parameter);
ecdsa(ellipticCurve, f, d);
ecdsa(ellipticCurve, f, d);
}
}
Line 959: Line 959:
private static void ecdsa(EllipticCurve aCurve, long aF, int aD) {
private static void ecdsa(EllipticCurve aCurve, long aF, int aD) {
Point point = aCurve.multiply(aCurve.g, aCurve.r);
Point point = aCurve.multiply(aCurve.g, aCurve.r);
if ( aCurve.discriminant() == 0 || aCurve.g.isZero() || ! point.isZero() || ! aCurve.contains(aCurve.g) ) {
if ( aCurve.discriminant() == 0 || aCurve.g.isZero() || ! point.isZero() || ! aCurve.contains(aCurve.g) ) {
throw new AssertionError("Invalid parameter in method ecdsa");
throw new AssertionError("Invalid parameter in method ecdsa");
Line 973: Line 974:
long i = 1;
long i = 1;
while ( i < 64 ) {
while ( i < 64 ) {
t = t | t >> i;
t |= t >> i;
i <<= 1;
i <<= 1;
}
}
Line 988: Line 989:
if ( d > 0 ) {
if ( d > 0 ) {
while ( d > t ) {
while ( d > t ) {
d = d >> 1;
d >>= 1;
}
}
f = f ^ d;
f ^= d;
System.out.println(System.lineSeparator() + "corrupted hash " + String.format("%08x", f));
System.out.println(System.lineSeparator() + "corrupted hash " + String.format("%08x", f));
}
}
Line 1,057: Line 1,058:
}
}


long r = 0;
long result = 0;
long s = 1;
long s = 1;
while ( aV != 0 ) {
while ( aV != 0 ) {
long q = aU / aV;
final long quotient = Math.floorDiv(aU, aV);
aU = Math.floorMod(aU, aV);
aU = Math.floorMod(aU, aV);
long temp = aU; aU = aV; aV = temp;
long temp = aU; aU = aV; aV = temp;
r -= q * s;
result -= quotient * s;
temp = r; r = s; s = temp;
temp = result; result = s; s = temp;
}
}


Line 1,070: Line 1,071:
throw new AssertionError("Cannot inverse modulo N, gcd = " + aU);
throw new AssertionError("Cannot inverse modulo N, gcd = " + aU);
}
}
return r;
return result;
}
}
Line 1,110: Line 1,111:
long la;
long la;
if ( aP.x != aQ.x ) {
if ( aP.x != aQ.x ) {
la = Math.floorMod(( aP.y - aQ.y ) * extendedGCD(aP.x - aQ.x, n), n);
la = Math.floorMod(( aP.y - aQ.y ) * extendedGCD(aP.x - aQ.x, n), n);
} else if ( aP.y == aQ.y && aP.y != 0 ) {
} else if ( aP.y == aQ.y && aP.y != 0 ) {
la = Math.floorMod(Math.floorMod(Math.floorMod(
la = Math.floorMod(Math.floorMod(Math.floorMod(
Line 1,123: Line 1,124:
}
}
public Point multiply(Point aPoint, long aK) {
public Point multiply(Point aPoint, long aK) {
Point result = Point.ZERO;
Point result = Point.ZERO;


while ( aK != 0 ) {
while ( aK != 0 ) {
if ( ( aK & 1 ) != 0 ) {
if ( ( aK & 1 ) == 1 ) {
result = add(result, aPoint);
result = add(result, aPoint);
}
}
Line 1,141: Line 1,142:
}
}
long r = Math.floorMod((Math.floorMod(a + aPoint.x * aPoint.x, n) * aPoint.x + b), n);
final long r = Math.floorMod(Math.floorMod(a + aPoint.x * aPoint.x, n) * aPoint.x + b, n);
long s = Math.floorMod(aPoint.y * aPoint.y, n);
final long s = Math.floorMod(aPoint.y * aPoint.y, n);
return r == s;
return r == s;
}
}
Line 1,163: Line 1,164:
}
}
private long a, b, n, r;
private final long a, b, n, r;
private Point g;
private final Point g;
}
}