Ejemplo n.º 1
0
 /**
  * @dataProvider getAdapters
  */
 public function testStrictIntegerReturnValues(MathAdapterInterface $math)
 {
     $x = 10;
     $y = 4;
     $mod = $math->mod($x, $y);
     $this->assertTrue(is_string($mod) && !is_resource($mod));
     $add = $math->add($x, $y);
     $this->assertTrue(is_string($add) && !is_resource($add));
     $sub = $math->sub($add, $y);
     $this->assertTrue(is_string($sub) && !is_resource($sub));
     $mul = $math->mul($x, $y);
     $this->assertTrue(is_string($mul) && !is_resource($mul));
     $div = $math->div($mul, $y);
     $this->assertTrue(is_string($div) && !is_resource($div));
     $pow = $math->pow($x, $y);
     $this->assertTrue(is_string($pow) && !is_resource($div));
     $powmod = $math->powmod($x, $y, $y);
     $this->assertTrue(is_string($powmod) && !is_resource($powmod));
     $bitwiseand = $math->bitwiseAnd($x, $y);
     $this->assertTrue(is_string($bitwiseand) && !is_resource($bitwiseand));
     $hexdec = $math->decHex($x);
     $this->assertTrue(is_string($hexdec) && !is_resource($hexdec));
     $dechex = $math->hexDec($hexdec);
     $this->assertTrue(is_string($dechex) && !is_resource($dechex));
 }
Ejemplo n.º 2
0
 public function _doCompressionConsistence(MathAdapterInterface $adapter, NumberTheory $theory)
 {
     foreach ($this->compression_data as $o) {
         // Try and regenerate the y coordinate from the parity byte
         // '04' . $x_coordinate . determined y coordinate should equal $o->decompressed
         // Tests squareRootModP which touches most functions in NumberTheory
         $y_byte = substr($o->compressed, 0, 2);
         $x_coordinate = substr($o->compressed, 2);
         $x = $adapter->hexDec($x_coordinate);
         // x^3
         $x3 = $adapter->powmod($x, 3, $this->generator->getCurve()->getPrime());
         // y^2
         $y2 = $adapter->add($x3, $this->generator->getCurve()->getB());
         // y0 = sqrt(y^2)
         $y0 = $theory->squareRootModP($y2, $this->generator->getCurve()->getPrime());
         if ($y_byte == '02') {
             $y_coordinate = $adapter->mod($y0, 2) == '0' ? gmp_strval(gmp_init($y0, 10), 16) : gmp_strval(gmp_sub($this->generator->getCurve()->getPrime(), $y0), 16);
         } else {
             $y_coordinate = $adapter->mod($y0, 2) == '0' ? gmp_strval(gmp_sub($this->generator->getCurve()->getPrime(), $y0), 16) : gmp_strval(gmp_init($y0, 10), 16);
         }
         $y_coordinate = str_pad($y_coordinate, 64, '0', STR_PAD_LEFT);
         // Successfully regenerated uncompressed ECDSA key from the x coordinate and the parity byte.
         $this->assertTrue('04' . $x_coordinate . $y_coordinate == $o->decompressed);
     }
 }