Skip to content

Commit 9c42a5f

Browse files
committed
Code update
1 parent 9381464 commit 9c42a5f

File tree

2 files changed

+40
-44
lines changed

2 files changed

+40
-44
lines changed

src/main/java/com/thealgorithms/ciphers/ECC.java

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@
1717
*/
1818
public class ECC {
1919

20-
private BigInteger privateKey; // Private key used for decryption
21-
private ECPoint publicKey; // Public key used for encryption
22-
private EllipticCurve curve; // Elliptic curve used in cryptography
23-
private ECPoint basePoint; // Base point G on the elliptic curve
20+
private BigInteger privateKey; // Private key used for decryption
21+
private ECPoint publicKey; // Public key used for encryption
22+
private EllipticCurve curve; // Elliptic curve used in cryptography
23+
private ECPoint basePoint; // Base point G on the elliptic curve
24+
25+
public ECC(int bits) {
26+
generateKeys(bits); // Generates public-private key pair
27+
}
2428

2529
public EllipticCurve getCurve() {
2630
return curve; // Returns the elliptic curve
2731
}
2832

29-
public ECC(int bits) {
30-
generateKeys(bits); // Generates public-private key pair
33+
public void setCurve(EllipticCurve curve) {
34+
this.curve = curve;
3135
}
3236

3337
// Getter and Setter for private key
@@ -39,29 +43,25 @@ public void setPrivateKey(BigInteger privateKey) {
3943
this.privateKey = privateKey;
4044
}
4145

42-
public void setCurve(EllipticCurve curve) {
43-
this.curve = curve;
44-
}
45-
4646
/**
4747
* Encrypts the message using the public key.
4848
* The message is transformed into an ECPoint and encrypted with elliptic curve operations.
4949
*
5050
* @param message The plain message to be encrypted
5151
* @return The encrypted message as an array of ECPoints (R, S)
5252
*/
53-
public synchronized ECPoint[] encrypt(String message) {
53+
public ECPoint[] encrypt(String message) {
5454
BigInteger m = new BigInteger(message.getBytes()); // Convert message to BigInteger
5555
SecureRandom r = new SecureRandom(); // Generate random value for k
5656
BigInteger k = new BigInteger(curve.getFieldSize(), r); // Generate random scalar k
5757

58-
// Calculate point R = k * G, where G is the base point
59-
ECPoint R = basePoint.multiply(k, curve.getP(), curve.getA());
58+
// Calculate point r = k * G, where G is the base point
59+
ECPoint rPoint = basePoint.multiply(k, curve.getP(), curve.getA());
6060

61-
// Calculate point S = k * publicKey + encodedMessage
62-
ECPoint S = publicKey.multiply(k, curve.getP(), curve.getA()).add(curve.encodeMessage(m), curve.getP(), curve.getA());
61+
// Calculate point s = k * publicKey + encodedMessage
62+
ECPoint sPoint = publicKey.multiply(k, curve.getP(), curve.getA()).add(curve.encodeMessage(m), curve.getP(), curve.getA());
6363

64-
return new ECPoint[] { R, S }; // Return encrypted message as two ECPoints
64+
return new ECPoint[] {rPoint, sPoint}; // Return encrypted message as two ECPoints
6565
}
6666

6767
/**
@@ -71,12 +71,12 @@ public synchronized ECPoint[] encrypt(String message) {
7171
* @param encryptedMessage The encrypted message as an array of ECPoints (R, S)
7272
* @return The decrypted plain message as a String
7373
*/
74-
public synchronized String decrypt(ECPoint[] encryptedMessage) {
75-
ECPoint R = encryptedMessage[0]; // First part of ciphertext
76-
ECPoint S = encryptedMessage[1]; // Second part of ciphertext
74+
public String decrypt(ECPoint[] encryptedMessage) {
75+
ECPoint rPoint = encryptedMessage[0]; // First part of ciphertext
76+
ECPoint sPoint = encryptedMessage[1]; // Second part of ciphertext
7777

78-
// Perform decryption: S - R * privateKey
79-
ECPoint decodedMessage = S.subtract(R.multiply(privateKey, curve.getP(), curve.getA()), curve.getP(), curve.getA());
78+
// Perform decryption: s - r * privateKey
79+
ECPoint decodedMessage = sPoint.subtract(rPoint.multiply(privateKey, curve.getP(), curve.getA()), curve.getP(), curve.getA());
8080

8181
BigInteger m = curve.decodeMessage(decodedMessage); // Decode the message from ECPoint
8282

@@ -88,7 +88,7 @@ public synchronized String decrypt(ECPoint[] encryptedMessage) {
8888
*
8989
* @param bits The size (in bits) of the keys to generate
9090
*/
91-
public final synchronized void generateKeys(int bits) {
91+
public final void generateKeys(int bits) {
9292
SecureRandom r = new SecureRandom();
9393
curve = new EllipticCurve(bits); // Initialize a new elliptic curve
9494
basePoint = curve.getBasePoint(); // Set the base point G
@@ -104,10 +104,10 @@ public final synchronized void generateKeys(int bits) {
104104
* Class representing an elliptic curve with the form y^2 = x^3 + ax + b.
105105
*/
106106
public static class EllipticCurve {
107-
private final BigInteger a; // Coefficient a in the curve equation
108-
private final BigInteger b; // Coefficient b in the curve equation
109-
private final BigInteger p; // Prime number p, defining the finite field
110-
private final ECPoint basePoint; // Base point G on the curve
107+
private final BigInteger a; // Coefficient a in the curve equation
108+
private final BigInteger b; // Coefficient b in the curve equation
109+
private final BigInteger p; // Prime number p, defining the finite field
110+
private final ECPoint basePoint; // Base point G on the curve
111111

112112
// Constructor with explicit parameters for a, b, p, and base point
113113
public EllipticCurve(BigInteger a, BigInteger b, BigInteger p, ECPoint basePoint) {
@@ -121,8 +121,8 @@ public EllipticCurve(BigInteger a, BigInteger b, BigInteger p, ECPoint basePoint
121121
public EllipticCurve(int bits) {
122122
SecureRandom r = new SecureRandom();
123123
this.p = BigInteger.probablePrime(bits, r); // Random prime p
124-
this.a = new BigInteger(bits, r); // Random coefficient a
125-
this.b = new BigInteger(bits, r); // Random coefficient b
124+
this.a = new BigInteger(bits, r); // Random coefficient a
125+
this.b = new BigInteger(bits, r); // Random coefficient b
126126
this.basePoint = new ECPoint(BigInteger.valueOf(4), BigInteger.valueOf(8)); // Fixed base point G
127127
}
128128

@@ -160,8 +160,8 @@ public BigInteger decodeMessage(ECPoint point) {
160160
* Class representing a point on the elliptic curve.
161161
*/
162162
public static class ECPoint {
163-
private final BigInteger x; // X-coordinate of the point
164-
private final BigInteger y; // Y-coordinate of the point
163+
private final BigInteger x; // X-coordinate of the point
164+
private final BigInteger y; // Y-coordinate of the point
165165

166166
public ECPoint(BigInteger x, BigInteger y) {
167167
this.x = x;
@@ -195,12 +195,10 @@ public ECPoint add(ECPoint other, BigInteger p, BigInteger a) {
195195
BigInteger lambda;
196196
if (this.equals(other)) {
197197
// Special case: point doubling
198-
lambda = (this.x.pow(2).multiply(BigInteger.valueOf(3)).add(a))
199-
.multiply(this.y.multiply(BigInteger.valueOf(2)).modInverse(p)).mod(p);
198+
lambda = (this.x.pow(2).multiply(BigInteger.valueOf(3)).add(a)).multiply(this.y.multiply(BigInteger.valueOf(2)).modInverse(p)).mod(p);
200199
} else {
201200
// General case: adding two different points
202-
lambda = (other.y.subtract(this.y))
203-
.multiply(other.x.subtract(this.x).modInverse(p)).mod(p);
201+
lambda = (other.y.subtract(this.y)).multiply(other.x.subtract(this.x).modInverse(p)).mod(p);
204202
}
205203

206204
BigInteger xr = lambda.pow(2).subtract(this.x).subtract(other.x).mod(p);
@@ -226,13 +224,13 @@ public ECPoint multiply(BigInteger k, BigInteger p, BigInteger a) {
226224

227225
while (k.signum() > 0) {
228226
if (k.testBit(0)) {
229-
result = result.add(addend, p, a); // Add when k's bit is 1
227+
result = result.add(addend, p, a); // Add the current point
230228
}
231229
addend = addend.add(addend, p, a); // Double the point
232-
k = k.shiftRight(1); // Shift k to the right by 1 bit
230+
k = k.shiftRight(1); // Divide k by 2
233231
}
234232

235-
return result; // Return the resulting point
233+
return result;
236234
}
237235
}
238236
}

src/test/java/com/thealgorithms/ciphers/ECCTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertNotEquals;
55

6-
import org.junit.jupiter.api.Test;
76
import java.math.BigInteger;
7+
import org.junit.jupiter.api.Test;
88

99
/**
1010
* ECCTest - Unit tests for the ECC (Elliptic Curve Cryptography) implementation.
@@ -64,15 +64,13 @@ void testDecryptWithKnownValues() {
6464
ECC.EllipticCurve curve = new ECC.EllipticCurve(a, b, p, basePoint);
6565

6666
// 4. Define the known ciphertext containing two ECPoints (R, S)
67-
ECC.ECPoint R = new ECC.ECPoint(new BigInteger("103077584019003058745849614420912636617007257617156724481937620119667345237687"),
68-
new BigInteger("68193862907937248121971710522760893811582068323088661566426323952783362061817"));
69-
ECC.ECPoint S = new ECC.ECPoint(new BigInteger("31932232426664380635434632300383525435115368414929679432313910646436992147798"),
70-
new BigInteger("77299754382292904069123203569944908076819220797512755280123348910207308129766"));
71-
ECC.ECPoint[] cipherText = new ECC.ECPoint[] { R, S };
67+
ECC.ECPoint R = new ECC.ECPoint(new BigInteger("103077584019003058745849614420912636617007257617156724481937620119667345237687"), new BigInteger("68193862907937248121971710522760893811582068323088661566426323952783362061817"));
68+
ECC.ECPoint S = new ECC.ECPoint(new BigInteger("31932232426664380635434632300383525435115368414929679432313910646436992147798"), new BigInteger("77299754382292904069123203569944908076819220797512755280123348910207308129766"));
69+
ECC.ECPoint[] cipherText = new ECC.ECPoint[] {R, S};
7270

7371
// 5. Create an ECC instance and set the private key and curve parameters
7472
ecc.setPrivateKey(knownPrivateKey); // Use setter method to set the private key
75-
ecc.setCurve(curve); // Use setter method to set the elliptic curve
73+
ecc.setCurve(curve);// Use setter method to set the elliptic curve
7674

7775
// 6. Decrypt the known ciphertext
7876
String decryptedMessage = ecc.decrypt(cipherText);

0 commit comments

Comments
 (0)