Add two points on the elliptic curve.
(ECPoint other, BigInteger p, BigInteger a)
| 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. |
no test coverage detected