Bitpay\Util\Util::pointAdd PHP Метод

pointAdd() публичный статический Метод

Point addition method P + Q = R where: s = (yP - yQ)/(xP - xQ) mod p xR = s2 - xP - xQ mod p yR = -yP + s(xP - xR) mod p
public static pointAdd ( Bitpay\PointInterface $P, Bitpay\PointInterface $Q ) : Bitpay\PointInterface
$P Bitpay\PointInterface
$Q Bitpay\PointInterface
Результат Bitpay\PointInterface
    public static function pointAdd(PointInterface $P, PointInterface $Q)
    {
        if ($P->isInfinity()) {
            return $Q;
        }
        if ($Q->isInfinity()) {
            return $P;
        }
        if ($P->getX() == $Q->getX() && $P->getY() == $Q->getY()) {
            return self::pointDouble(new Point($P->getX(), $P->getY()));
        }
        $p = '0x' . Secp256k1::P;
        $a = '0x' . Secp256k1::A;
        $s = 0;
        $R = array('x' => 0, 'y' => 0, 's' => 0);
        // Critical math section
        try {
            $m = Math::sub($P->getY(), $Q->getY());
            $n = Math::sub($P->getX(), $Q->getX());
            $o = Math::invertm($n, $p);
            $st = Math::mul($m, $o);
            $s = Math::mod($st, $p);
            $R['x'] = Math::mod(Math::sub(Math::sub(Math::mul($s, $s), $P->getX()), $Q->getX()), $p);
            $R['y'] = Math::mod(Math::add(Math::sub(0, $P->getY()), Math::mul($s, Math::sub($P->getX(), $R['x']))), $p);
            $R['s'] = $s;
        } catch (Exception $e) {
            throw new \Exception('Error in Util::pointAdd(): ' . $e->getMessage());
        }
        return new Point($R['x'], $R['y']);
    }

Usage Example

Пример #1
0
 public function testPointAdd()
 {
     $point = Util::pointAdd(new Point(1, 1), new Point(1, 1));
     $expectedpoint = new Point("28948022309329048855892746252171976963317496166410141009864396001977208667916", "14474011154664524427946373126085988481658748083205070504932198000988604333958");
     $this->assertEquals($expectedpoint, $point);
     $point = Util::pointAdd(new Point(0, 0), new Point(1, 1));
     $expectedpoint = new Point("0", "0");
     $this->assertEquals($expectedpoint, $point);
     $point = Util::pointAdd(new Point(1, 0), new Point(0, 1));
     $expectedpoint = new Point("0", "115792089237316195423570985008687907853269984665640564039457584007908834671662");
     $this->assertEquals($expectedpoint, $point);
 }