/** * @param PublicKeyInterface $key * @param SignatureInterface $signature * @param $hash * @return bool */ public function verify(PublicKeyInterface $key, SignatureInterface $signature, $hash) { $generator = $key->getGenerator(); $n = $generator->getOrder(); $point = $key->getPoint(); $r = $signature->getR(); $s = $signature->getS(); $math = $this->adapter; if ($math->cmp($r, 1) < 1 || $math->cmp($r, $math->sub($n, 1)) > 0) { return false; } if ($math->cmp($s, 1) < 1 || $math->cmp($s, $math->sub($n, 1)) > 0) { return false; } $modMath = $math->getModularArithmetic($n); $c = $math->inverseMod($s, $n); $u1 = $modMath->mul($hash, $c); $u2 = $modMath->mul($r, $c); $xy = $generator->mul($u1)->add($point->mul($u2)); $v = $math->mod($xy->getX(), $n); return BinaryString::constantTimeCompare($v, $r); }
/** * @param EncryptedPrivateKey $that * @return bool */ public function equals(EncryptedPrivateKey $that) { return $this->getMethod() === $that->getMethod() && $this->getIv() === $that->getIv() && BinaryString::constantTimeCompare(gmp_strval($this->getKey()->getSecret(), 10), gmp_strval($that->getKey()->getSecret(), 10)); }