- Timestamp:
- 05/17/2019 10:57:19 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/sodium_compat/src/Core32/Int64.php
r44953 r45344 206 206 public function mulInt($int = 0, $size = 0) 207 207 { 208 if (ParagonIE_Sodium_Compat::$fastMult) { 209 return $this->mulIntFast($int); 210 } 208 211 ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); 209 212 ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); … … 269 272 270 273 $int >>= 1; 271 $return->limbs[0] = $ret0;272 $return->limbs[1] = $ret1;273 $return->limbs[2] = $ret2;274 $return->limbs[3] = $ret3;275 }274 } 275 $return->limbs[0] = $ret0; 276 $return->limbs[1] = $ret1; 277 $return->limbs[2] = $ret2; 278 $return->limbs[3] = $ret3; 276 279 return $return; 277 280 } … … 319 322 320 323 /** 324 * @param array<int, int> $a 325 * @param array<int, int> $b 326 * @param int $baseLog2 327 * @return array<int, int> 328 */ 329 public function multiplyLong(array $a, array $b, $baseLog2 = 16) 330 { 331 $a_l = count($a); 332 $b_l = count($b); 333 /** @var array<int, int> $r */ 334 $r = array_fill(0, $a_l + $b_l + 1, 0); 335 $base = 1 << $baseLog2; 336 for ($i = 0; $i < $a_l; ++$i) { 337 $a_i = $a[$i]; 338 for ($j = 0; $j < $a_l; ++$j) { 339 $b_j = $b[$j]; 340 $product = ($a_i * $b_j) + $r[$i + $j]; 341 $carry = ($product >> $baseLog2 & 0xffff); 342 $r[$i + $j] = ($product - (int) ($carry * $base)) & 0xffff; 343 $r[$i + $j + 1] += $carry; 344 } 345 } 346 return array_slice($r, 0, 5); 347 } 348 349 /** 350 * @param int $int 351 * @return ParagonIE_Sodium_Core32_Int64 352 */ 353 public function mulIntFast($int) 354 { 355 // Handle negative numbers 356 $aNeg = ($this->limbs[0] >> 15) & 1; 357 $bNeg = ($int >> 31) & 1; 358 $a = array_reverse($this->limbs); 359 $b = array( 360 $int & 0xffff, 361 ($int >> 16) & 0xffff, 362 -$bNeg & 0xffff, 363 -$bNeg & 0xffff 364 ); 365 if ($aNeg) { 366 for ($i = 0; $i < 4; ++$i) { 367 $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; 368 } 369 ++$a[0]; 370 } 371 if ($bNeg) { 372 for ($i = 0; $i < 4; ++$i) { 373 $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; 374 } 375 ++$b[0]; 376 } 377 // Multiply 378 $res = $this->multiplyLong($a, $b); 379 380 // Re-apply negation to results 381 if ($aNeg !== $bNeg) { 382 for ($i = 0; $i < 4; ++$i) { 383 $res[$i] = (0xffff ^ $res[$i]) & 0xffff; 384 } 385 // Handle integer overflow 386 $c = 1; 387 for ($i = 0; $i < 4; ++$i) { 388 $res[$i] += $c; 389 $c = $res[$i] >> 16; 390 $res[$i] &= 0xffff; 391 } 392 } 393 394 // Return our values 395 $return = new ParagonIE_Sodium_Core32_Int64(); 396 $return->limbs = array( 397 $res[3] & 0xffff, 398 $res[2] & 0xffff, 399 $res[1] & 0xffff, 400 $res[0] & 0xffff 401 ); 402 if (count($res) > 4) { 403 $return->overflow = $res[4] & 0xffff; 404 } 405 $return->unsignedInt = $this->unsignedInt; 406 return $return; 407 } 408 409 /** 410 * @param ParagonIE_Sodium_Core32_Int64 $right 411 * @return ParagonIE_Sodium_Core32_Int64 412 */ 413 public function mulInt64Fast(ParagonIE_Sodium_Core32_Int64 $right) 414 { 415 $aNeg = ($this->limbs[0] >> 15) & 1; 416 $bNeg = ($right->limbs[0] >> 15) & 1; 417 418 $a = array_reverse($this->limbs); 419 $b = array_reverse($right->limbs); 420 if ($aNeg) { 421 for ($i = 0; $i < 4; ++$i) { 422 $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; 423 } 424 ++$a[0]; 425 } 426 if ($bNeg) { 427 for ($i = 0; $i < 4; ++$i) { 428 $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; 429 } 430 ++$b[0]; 431 } 432 $res = $this->multiplyLong($a, $b); 433 if ($aNeg !== $bNeg) { 434 if ($aNeg !== $bNeg) { 435 for ($i = 0; $i < 4; ++$i) { 436 $res[$i] = ($res[$i] ^ 0xffff) & 0xffff; 437 } 438 $c = 1; 439 for ($i = 0; $i < 4; ++$i) { 440 $res[$i] += $c; 441 $c = $res[$i] >> 16; 442 $res[$i] &= 0xffff; 443 } 444 } 445 } 446 $return = new ParagonIE_Sodium_Core32_Int64(); 447 $return->limbs = array( 448 $res[3] & 0xffff, 449 $res[2] & 0xffff, 450 $res[1] & 0xffff, 451 $res[0] & 0xffff 452 ); 453 if (count($res) > 4) { 454 $return->overflow = $res[4]; 455 } 456 return $return; 457 } 458 459 /** 321 460 * @param ParagonIE_Sodium_Core32_Int64 $int 322 461 * @param int $size … … 328 467 public function mulInt64(ParagonIE_Sodium_Core32_Int64 $int, $size = 0) 329 468 { 469 if (ParagonIE_Sodium_Compat::$fastMult) { 470 return $this->mulInt64Fast($int); 471 } 330 472 ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); 331 473 if (!$size) { … … 567 709 return $this->shiftRight(-$c); 568 710 } else { 569 if ( is_null($c)) {711 if (!is_int($c)) { 570 712 throw new TypeError(); 571 713 } … … 592 734 { 593 735 ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); 736 $c = (int) $c; 737 /** @var int $c */ 594 738 $return = new ParagonIE_Sodium_Core32_Int64(); 595 739 $return->unsignedInt = $this->unsignedInt; 596 740 $c &= 63; 597 /** @var int $c */598 741 599 742 $negative = -(($this->limbs[0] >> 15) & 1);
Note: See TracChangeset
for help on using the changeset viewer.