Example #1
0
 /**
  * Calculates the greatest common divisor and Bezout's identity.
  *
  * Say you have 693 and 609.  The GCD is 21.  Bezout's identity states that there exist integers x and y such that
  * 693*x + 609*y == 21.  In point of fact, there are actually an infinite number of x and y combinations and which
  * combination is returned is dependant upon which mode is in use.  See
  * {@link http://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity Bezout's identity - Wikipedia} for more information.
  *
  * Here's an example:
  * <code>
  * <?php
  *    $a = new \Topxia\Service\Util\Phpsec\Math\BigInteger(693);
  *    $b = new \Topxia\Service\Util\Phpsec\Math\BigInteger(609);
  *
  *    extract($a->extendedGCD($b));
  *
  *    echo $gcd->toString() . "\r\n"; // outputs 21
  *    echo $a->toString() * $x->toString() + $b->toString() * $y->toString(); // outputs 21
  * ?>
  * </code>
  *
  * @access public
  * @internal Calculates the GCD using the binary xGCD algorithim described in
  *    {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=19 HAC 14.61}.  As the text above 14.61 notes,
  *    the more traditional algorithim requires "relatively costly multiple-precision divisions".
  * @param  \Topxia\Service\Util\Phpsec\Math\BigInteger    $n
  * @return \Topxia\Service\Util\Phpsec\Math\BigInteger
  */
 public function extendedGCD($n)
 {
     switch (MATH_BIGINTEGER_MODE) {
         case self::MODE_GMP:
             extract(gmp_gcdext($this->value, $n->value));
             return array('gcd' => $this->_normalize(new static($g)), 'x' => $this->_normalize(new static($s)), 'y' => $this->_normalize(new static($t)));
         case self::MODE_BCMATH:
             // it might be faster to use the binary xGCD algorithim here, as well, but (1) that algorithim works
             // best when the base is a power of 2 and (2) i don't think it'd make much difference, anyway.  as is,
             // the basic extended euclidean algorithim is what we're using.
             $u = $this->value;
             $v = $n->value;
             $a = '1';
             $b = '0';
             $c = '0';
             $d = '1';
             while (bccomp($v, '0', 0) != 0) {
                 $q = bcdiv($u, $v, 0);
                 $temp = $u;
                 $u = $v;
                 $v = bcsub($temp, bcmul($v, $q, 0), 0);
                 $temp = $a;
                 $a = $c;
                 $c = bcsub($temp, bcmul($a, $q, 0), 0);
                 $temp = $b;
                 $b = $d;
                 $d = bcsub($temp, bcmul($b, $q, 0), 0);
             }
             return array('gcd' => $this->_normalize(new static($u)), 'x' => $this->_normalize(new static($a)), 'y' => $this->_normalize(new static($b)));
     }
     $y = $n->copy();
     $x = $this->copy();
     $g = new static();
     $g->value = array(1);
     while (!($x->value[0] & 1 || $y->value[0] & 1)) {
         $x->_rshift(1);
         $y->_rshift(1);
         $g->_lshift(1);
     }
     $u = $x->copy();
     $v = $y->copy();
     $a = new static();
     $b = new static();
     $c = new static();
     $d = new static();
     $a->value = $d->value = $g->value = array(1);
     $b->value = $c->value = array();
     while (!empty($u->value)) {
         while (!($u->value[0] & 1)) {
             $u->_rshift(1);
             if (!empty($a->value) && $a->value[0] & 1 || !empty($b->value) && $b->value[0] & 1) {
                 $a = $a->add($y);
                 $b = $b->subtract($x);
             }
             $a->_rshift(1);
             $b->_rshift(1);
         }
         while (!($v->value[0] & 1)) {
             $v->_rshift(1);
             if (!empty($d->value) && $d->value[0] & 1 || !empty($c->value) && $c->value[0] & 1) {
                 $c = $c->add($y);
                 $d = $d->subtract($x);
             }
             $c->_rshift(1);
             $d->_rshift(1);
         }
         if ($u->compare($v) >= 0) {
             $u = $u->subtract($v);
             $a = $a->subtract($c);
             $b = $b->subtract($d);
         } else {
             $v = $v->subtract($u);
             $c = $c->subtract($a);
             $d = $d->subtract($b);
         }
     }
     return array('gcd' => $this->_normalize($g->multiply($v)), 'x' => $this->_normalize($c), 'y' => $this->_normalize($d));
 }