MCPcopy Create free account
hub / github.com/TheAlgorithms/Java / add

Method add

src/main/java/com/thealgorithms/ciphers/ECC.java:187–208  ·  view source on GitHub ↗

Add two points on the elliptic curve.

(ECPoint other, BigInteger p, BigInteger a)

Source from the content-addressed store, hash-verified

185 * Add two points on the elliptic curve.
186 */
187 public ECPoint add(ECPoint other, BigInteger p, BigInteger a) {
188 if (this.x.equals(BigInteger.ZERO) && this.y.equals(BigInteger.ZERO)) {
189 return other; // If this point is the identity, return the other point
190 }
191 if (other.x.equals(BigInteger.ZERO) && other.y.equals(BigInteger.ZERO)) {
192 return this; // If the other point is the identity, return this point
193 }
194
195 BigInteger lambda;
196 if (this.equals(other)) {
197 // Special case: point doubling
198 lambda = this.x.pow(2).multiply(BigInteger.valueOf(3)).add(a).multiply(this.y.multiply(BigInteger.valueOf(2)).modInverse(p)).mod(p);
199 } else {
200 // General case: adding two different points
201 lambda = other.y.subtract(this.y).multiply(other.x.subtract(this.x).modInverse(p)).mod(p);
202 }
203
204 BigInteger xr = lambda.pow(2).subtract(this.x).subtract(other.x).mod(p);
205 BigInteger yr = lambda.multiply(this.x.subtract(xr)).subtract(this.y).mod(p);
206
207 return new ECPoint(xr, yr);
208 }
209
210 /**
211 * Subtract two points on the elliptic curve.

Callers 15

subtractMethod · 0.95
multiplyMethod · 0.95
extractUnitsMethod · 0.45
validForBaseMethod · 0.45
convertToAnyBaseMethod · 0.45
decodeMethod · 0.45
preprocessWordsMethod · 0.45
handleHundredMethod · 0.45
handlePowerOfTenMethod · 0.45
handleNumberMethod · 0.45
handlePointMethod · 0.45

Calls 6

modMethod · 0.80
modInverseMethod · 0.80
equalsMethod · 0.45
multiplyMethod · 0.45
powMethod · 0.45
subtractMethod · 0.45

Tested by

no test coverage detected