Complex classes like Point often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Point, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | trait Point |
||
| 37 | { |
||
| 38 | use Math, Secp256k1; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * EC Point addition method P + Q = R where: |
||
| 42 | * s = (yP - yQ) / (xP - xQ) mod p |
||
| 43 | * xR = s2 - xP - xQ mod p |
||
| 44 | * yR = -yP + s(xP - xR) mod p |
||
| 45 | * |
||
| 46 | * @param array|string $P The first point to add. |
||
| 47 | * @param array|string $Q The second point to add. |
||
| 48 | * @return array $R The result of the point addition. |
||
| 49 | * @throws \Exception |
||
| 50 | */ |
||
| 51 | public function pointAddW($P, $Q) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Point multiplication method 2P = R where |
||
| 90 | * s = (3xP2 + a) / (2yP) mod p |
||
| 91 | * xR = s2 - 2xP mod p |
||
| 92 | * yR = -yP + s(xP - xR) mod p |
||
| 93 | * |
||
| 94 | * @param array|string $P The point to multiply. |
||
| 95 | * @return array|string $R The multiplied point. |
||
| 96 | * @throws \Exception |
||
| 97 | */ |
||
| 98 | public function pointDoubleW($P) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Performs a test of an EC point by substituting the new |
||
| 141 | * values into the equation for the Weierstrass form of the curve. |
||
| 142 | * |
||
| 143 | * @param array|string $P The generated point to test. |
||
| 144 | * @return bool Whether or not the point is valid. |
||
| 145 | * @throws \Exception |
||
| 146 | */ |
||
| 147 | public function pointTestW($P) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Pure PHP implementation of the Double-And-Add algorithm, for more info see: |
||
| 195 | * http://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Double-and-add |
||
| 196 | * |
||
| 197 | * @param array $P Base EC curve point. |
||
| 198 | * @param string $x Scalar value. |
||
| 199 | * @return array|string $S Either 'infinity' or the new coordinates. |
||
| 200 | */ |
||
| 201 | public function doubleAndAdd($P, $x = '1') |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Pure PHP implementation of the Montgomery Ladder algorithm which helps protect |
||
| 219 | * us against side-channel attacks. This performs the same number of operations |
||
| 220 | * regardless of the scalar value being used as the multiplier. It's slower than |
||
| 221 | * the traditional double-and-add algorithm because of that fact but safer to use. |
||
| 222 | * |
||
| 223 | * @param array $P Base EC curve point. |
||
| 224 | * @param string $x Scalar value. |
||
| 225 | * @return array|string $S Either 'infinity' or the new coordinates. |
||
| 226 | */ |
||
| 227 | public function mLadder($P, $x = '1') |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Creates a new point on the elliptic curve. |
||
| 254 | * |
||
| 255 | * @param boolean $ladder Whether or not to use the mladder method. |
||
| 256 | * @return array The new EC point. |
||
| 257 | * @throws \Exception |
||
| 258 | */ |
||
| 259 | public function GenerateNewPoint($ladder = true) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Recalculates the y-coordinate from a compressed public key. |
||
| 289 | * |
||
| 290 | * @param string $x_coord The x-coordinate. |
||
| 291 | * @param string $compressed_bit The hex compression value (03 or 02). |
||
| 292 | * @return string $y The calculated y-coordinate. |
||
| 293 | * @throws \Exception $e |
||
| 294 | */ |
||
| 295 | public function calcYfromX($x_coord, $compressed_bit) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Basic range check. Throws exception if coordinate value is out of range. |
||
| 311 | * |
||
| 312 | * @param string $value The coordinate to check. |
||
| 313 | * @return boolean The result of the check. |
||
| 314 | */ |
||
| 315 | public function RangeCheck($value) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Checks the basic type of the point value. |
||
| 324 | * |
||
| 325 | * @param mixed $value The point to check. |
||
| 326 | * @return string The result of the check. |
||
| 327 | * @codeCoverageIgnore |
||
| 328 | */ |
||
| 329 | private function pointType($value) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Checks the range of a pair of coordinates. |
||
| 344 | * |
||
| 345 | * @param string $x The key to check. |
||
| 346 | * @param string $y The key to check. |
||
| 347 | * @codeCoverageIgnore |
||
| 348 | */ |
||
| 349 | private function coordsRangeCheck($x, $y) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Basic coordinate check: verifies $hex is valid |
||
| 358 | * |
||
| 359 | * @param string $hex The coordinate to check. |
||
| 360 | * @return string $hex The checked coordinate. |
||
| 361 | * @codeCoverageIgnore |
||
| 362 | */ |
||
| 363 | private function CoordinateCheck($hex) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Checks if a Point is infinity or equal to another point. |
||
| 375 | * |
||
| 376 | * @param array|string $pointOne The first point to check. |
||
| 377 | * @param array|string $pointTwo The second point to check. |
||
| 378 | * @return mixed The result value to return or null. |
||
| 379 | */ |
||
| 380 | private function infPointCheck($pointOne, $pointTwo) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Checks if a number is within a certain range: |
||
| 399 | * 0x01 < number < n |
||
| 400 | * |
||
| 401 | * @param string $value The number to check. |
||
| 402 | * @return boolean The result of the comparison. |
||
| 403 | * @codeCoverageIgnore |
||
| 404 | */ |
||
| 405 | private function randCompare($value) |
||
| 409 | } |
||
| 410 |