Completed
Pull Request — master (#758)
by thomas
20:21
created

XOnlyPublicKey::doVerifySchnorr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php declare(strict_types=1);
2
3
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Key;
4
5
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\SchnorrSignature;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\XOnlyPublicKeyInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SchnorrSignatureInterface;
9
use BitWasp\Buffertools\Buffer;
10
use BitWasp\Buffertools\BufferInterface;
11
12
class XOnlyPublicKey implements XOnlyPublicKeyInterface
13
{
14
    /**
15
     * @var resource
16
     */
17
    private $context;
18
19
    /**
20
     * @var resource
21
     */
22
    private $xonlyKey;
23
24
    /**
25
     * @var bool
26
     */
27
    private $isPositive;
28
29
    /**
30
     * @param resource $context
31
     * @param resource $xonlyKey
32
     */
33
    public function __construct($context, $xonlyKey)
34
    {
35
        if (!is_resource($context) ||
36
            !get_resource_type($context) === SECP256K1_TYPE_CONTEXT) {
37
            throw new \InvalidArgumentException('Secp256k1\Key\XOnlyPublicKey expects ' . SECP256K1_TYPE_CONTEXT . ' resource');
38
        }
39
40
        if (!(is_resource($xonlyKey) && get_resource_type($xonlyKey) === SECP256K1_TYPE_XONLY_PUBKEY)) {
0 ignored issues
show
Bug introduced by
The constant BitWasp\Bitcoin\Crypto\E...256K1_TYPE_XONLY_PUBKEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
41
            throw new \InvalidArgumentException('Secp256k1\Key\XOnlyPublicKey expects ' . SECP256K1_TYPE_XONLY_PUBKEY . ' resource');
42
        }
43
44
        $this->context = $context;
45
        $this->xonlyKey = $xonlyKey;
46
    }
47
48
    public function isPositive(): bool
49
    {
50
        if (null === $this->isPositive) {
51
            $x = gmp_init(unpack("H*", $this->getBuffer()->getBinary())[1], 16);
52
            $p = gmp_init("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16);
53
            // todo: is this === 1 or >= 0
54
            // https://github.com/bitcoin-core/secp256k1/blob/1c131affd3c3402f269b56685bca63c631cfcf26/src/field_impl.h#L311
55
            // https://github.com/sipa/bitcoin/commit/348b0e0e00c0ebe57c180e49b08edcecde5f9158#diff-607598e1a39100b1883191275b789557R278
56
            $this->isPositive = gmp_jacobi($x, $p) === 1;
57
        }
58
        return $this->isPositive;
59
    }
60
61
    private function doVerifySchnorr(BufferInterface $msg32, SchnorrSignature $schnorrSig): bool
62
    {
63
        return (bool) secp256k1_schnorrsig_verify($this->context, $schnorrSig->getResource(), $msg32->getBinary(), $this->xonlyKey);
0 ignored issues
show
Bug introduced by
The function secp256k1_schnorrsig_verify was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        return (bool) /** @scrutinizer ignore-call */ secp256k1_schnorrsig_verify($this->context, $schnorrSig->getResource(), $msg32->getBinary(), $this->xonlyKey);
Loading history...
64
    }
65
66
    public function verifySchnorr(BufferInterface $msg32, SchnorrSignatureInterface $schnorrSignature): bool
67
    {
68
        /** @var SchnorrSignature $schnorrSignature */
69
        return $this->doVerifySchnorr($msg32, $schnorrSignature);
70
    }
71
    /**
72
     * @return resource
73
     * @throws \Exception
74
     */
75
    private function clonePubkey()
76
    {
77
        $context = $this->context;
78
        $serialized = '';
79
        if (1 !== secp256k1_xonly_pubkey_serialize($context, $serialized, $this->xonlyKey)) {
0 ignored issues
show
Bug introduced by
The function secp256k1_xonly_pubkey_serialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        if (1 !== /** @scrutinizer ignore-call */ secp256k1_xonly_pubkey_serialize($context, $serialized, $this->xonlyKey)) {
Loading history...
80
            throw new \Exception('failed to serialize xonly pubkey for clone');
81
        }
82
83
        /** @var resource $clone */
84
        $clone = null;
85
        if (1 !== secp256k1_xonly_pubkey_parse($context, $clone, $serialized)) {
0 ignored issues
show
Bug introduced by
The function secp256k1_xonly_pubkey_parse was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        if (1 !== /** @scrutinizer ignore-call */ secp256k1_xonly_pubkey_parse($context, $clone, $serialized)) {
Loading history...
86
            throw new \Exception('failed to parse xonly pubkey');
87
        }
88
89
        return $clone;
90
    }
91
92
    public function tweakAdd(BufferInterface $tweak32): XOnlyPublicKeyInterface
93
    {
94
        $pubkey = $this->clonePubkey();
95
        $tweaked = null;
96
        if (!secp256k1_xonly_pubkey_tweak_add($this->context, $tweaked, $pubkey, $tweak32->getBinary())) {
0 ignored issues
show
Bug introduced by
The function secp256k1_xonly_pubkey_tweak_add was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        if (!/** @scrutinizer ignore-call */ secp256k1_xonly_pubkey_tweak_add($this->context, $tweaked, $pubkey, $tweak32->getBinary())) {
Loading history...
97
            throw new \RuntimeException("failed to tweak pubkey");
98
        }
99
        return new XOnlyPublicKey($this->context, $pubkey);
100
    }
101
102
    public function getBuffer(): BufferInterface
103
    {
104
        $out = '';
105
        if (!secp256k1_xonly_pubkey_serialize($this->context, $out, $this->xonlyKey)) {
0 ignored issues
show
Bug introduced by
The function secp256k1_xonly_pubkey_serialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        if (!/** @scrutinizer ignore-call */ secp256k1_xonly_pubkey_serialize($this->context, $out, $this->xonlyKey)) {
Loading history...
106
            throw new \RuntimeException("failed to serialize xonly pubkey!");
107
        }
108
        return new Buffer($out);
109
    }
110
}