Esempio n. 1
0
 /**
  * @param MathAdapterInterface $math
  * @param NamedCurveFp $c
  * @return Sequence
  */
 private function getCurveAsn(MathAdapterInterface $math, NamedCurveFp $c)
 {
     $a = new OctetString($math->decHex($math->mod($c->getA(), $c->getPrime())));
     $b = new OctetString($math->decHex($math->mod($c->getB(), $c->getPrime())));
     try {
         $seed = CurveRandomSeed::getSeed($c);
         return new Sequence($a, $b, new BitString($seed));
     } catch (\Exception $e) {
         return new Sequence($a, $b);
     }
 }
Esempio n. 2
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));
 }
Esempio n. 3
0
 /**
  * {@inheritDoc}
  * @see \Mdanter\Ecc\EcMathInterface::mod()
  */
 public function mod($n)
 {
     if ($this->identify($n) !== 'int') {
         throw new \LogicException('Parameter for mod() must be an integer');
     }
     if ($this->dataType == 'int') {
         $this->data = $this->math->mod($this->data, $n);
     }
     return $this;
 }
Esempio n. 4
0
 /**
  * {@inheritDoc}
  * @see \Mdanter\Ecc\PointInterface::mul()
  */
 public function mul($n)
 {
     if ($this->isInfinity()) {
         return $this->curve->getInfinity();
     }
     if ($this->adapter->cmp($this->order, '0') > 0) {
         $n = $this->adapter->mod($n, $this->order);
     }
     if ($this->adapter->cmp($n, '0') == 0) {
         return $this->curve->getInfinity();
     }
     $r = [$this->curve->getInfinity(), clone $this];
     $n = $this->adapter->baseConvert($n, 10, 2);
     $k = strlen($n);
     for ($i = 0; $i < $k; $i++) {
         $j = $n[$i];
         $this->cswap($r[0], $r[1], $j ^ 1);
         $r[0] = $r[0]->add($r[1]);
         $r[1] = $r[1]->getDouble();
         $this->cswap($r[0], $r[1], $j ^ 1);
     }
     $r[0]->validate();
     return $r[0];
 }
Esempio n. 5
0
 /**
  *
  * @dataProvider getMultAdapters
  */
 public function testMultiply2(MathAdapterInterface $math, $p, $a, $b, $x, $y, $m, $ex, $ey)
 {
     $c = new CurveFp($p, $a, $b, $math);
     $p1 = $c->getPoint($x, $y);
     $p3 = $p1->mul($m);
     $this->assertFalse($p3->isInfinity());
     $this->assertEquals($ex, $math->mod($p3->getX(), $p));
     $this->assertEquals($ey, $math->mod($p3->getY(), $p));
 }
Esempio n. 6
0
 /**
  * @dataProvider getAdapters
  */
 public function testModFunction(MathAdapterInterface $math)
 {
     // $o->compressed, $o->decompressed public key.
     // Check that we can compress a key properly (tests $math->mod())
     foreach ($this->compression_data as $o) {
         $prefix = substr($o->decompressed, 0, 2);
         // will be 04.
         $this->assertEquals('04', $prefix);
         // hex encoded (X,Y) coordinate of ECDSA public key.
         $x = substr($o->decompressed, 2, 64);
         $y = substr($o->decompressed, 66, 64);
         // y % 2 == 0       - true: y is even(02) / false: y is odd(03)
         $mod = $math->mod($math->hexDec($y), 2);
         $compressed = '0' . ($mod == 0 ? '2' : '3') . $x;
         // Check that the mod function reported the parity for the y value.
         $this->assertTrue($compressed === $o->compressed);
     }
 }