Completed
Pull Request — master (#221)
by thomas
51:04 queued 47:55
created

PublicKey::tweakAdd()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.6493
Metric Value
dl 0
loc 13
ccs 5
cts 11
cp 0.4545
rs 9.4286
cc 2
eloc 8
nc 2
nop 1
crap 2.6493
1
<?php
2
3
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Key;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Serializer\Key\PublicKeySerializer;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\Key;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface;
10
use BitWasp\Buffertools\Buffer;
11
12
class PublicKey extends Key implements PublicKeyInterface
13
{
14
    /**
15
     * @var EcAdapter
16
     */
17
    private $ecAdapter;
18
19
    /**
20
     * @var bool|false
21
     */
22
    private $compressed;
23
24
    /**
25
     * @var resource
26
     */
27
    private $pubkey_t;
28
29
    /**
30
     * @param EcAdapter $ecAdapter
31
     * @param resource $secp256k1_pubkey_t
32
     * @param bool|false $compressed
33
     */
34 195
    public function __construct(EcAdapter $ecAdapter, $secp256k1_pubkey_t, $compressed = false)
35
    {
36 195
        if (!is_resource($secp256k1_pubkey_t) ||
37 195
            !get_resource_type($secp256k1_pubkey_t) === SECP256K1_TYPE_PUBKEY) {
38
            throw new \InvalidArgumentException('Secp256k1\Key\PublicKey expects ' . SECP256K1_TYPE_PUBKEY . ' resource');
39
        }
40
41 195
        if (false === is_bool($compressed)) {
42
            throw new \InvalidArgumentException('PublicKey: Compressed must be a boolean');
43
        }
44
45 195
        $this->ecAdapter = $ecAdapter;
46 195
        $this->pubkey_t = $secp256k1_pubkey_t;
47 195
        $this->compressed = $compressed;
48 195
    }
49
50
    /**
51
     * @param Buffer $msg32
52
     * @param SignatureInterface $signature
53
     * @return bool
54
     */
55
    public function verify(Buffer $msg32, SignatureInterface $signature)
56
    {
57
        return $this->ecAdapter->verify($msg32, $this, $signature);
58
    }
59
60
    /**
61
     * @return bool|false
62
     */
63 183
    public function isCompressed()
64
    {
65 183
        return $this->compressed;
66
    }
67
68
    /**
69
     * @return resource
70
     */
71 189
    public function getResource()
72
    {
73 189
        return $this->pubkey_t;
74
    }
75
76
    /**
77
     * @return resource
78
     * @throws \Exception
79
     */
80 9
    private function clonePubkey()
81
    {
82 9
        $context = $this->ecAdapter->getContext();
83
        /** @var resource $serialized */
84 9
        $serialized = '';
85 9
        if (1 !== secp256k1_ec_pubkey_serialize($context, $this->pubkey_t, $this->compressed, $serialized)) {
0 ignored issues
show
Documentation introduced by
$serialized is of type resource, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
            throw new \Exception('Secp256k1: pubkey serialize');
87
        }
88
89
        /** @var resource $clone */
90 9
        $clone = '';
91 9
        if (1 !== secp256k1_ec_pubkey_parse($context, $serialized, $clone)) {
0 ignored issues
show
Documentation introduced by
$serialized is of type resource, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92 9
            throw new \Exception('Secp256k1 pubkey parse');
93
        }
94
95
        return $clone;
96
    }
97
98
    /**
99
     * @param int $tweak
100
     * @return PublicKey
101
     * @throws \Exception
102
     */
103 6
    public function tweakAdd($tweak)
104
    {
105 6
        $context = $this->ecAdapter->getContext();
106 6
        $math = $this->ecAdapter->getMath();
107 6
        $bin = pack('H*', str_pad($math->decHex($tweak), 64, '0', STR_PAD_LEFT));
108
109 6
        $clone = $this->clonePubkey();
110
        if (1 !== secp256k1_ec_pubkey_tweak_add($context, $clone, $bin)) {
111
            throw new \RuntimeException('Secp256k1: tweak add failed.');
112
        }
113
114
        return new PublicKey($this->ecAdapter, $clone, $this->compressed);
115
    }
116
117
    /**
118
     * @param int $tweak
119
     * @return PublicKey
120
     * @throws \Exception
121
     */
122 3
    public function tweakMul($tweak)
123
    {
124 3
        $context = $this->ecAdapter->getContext();
125 3
        $math = $this->ecAdapter->getMath();
126 3
        $bin = pack('H*', str_pad($math->decHex($tweak), 64, '0', STR_PAD_LEFT));
127
128 3
        $clone = $this->clonePubkey();
129
        if (1 !== secp256k1_ec_pubkey_tweak_mul($context, $clone, $bin)) {
130
            throw new \RuntimeException('Secp256k1: tweak mul failed.');
131
        }
132
133
        return new PublicKey($this->ecAdapter, $clone, $this->compressed);
134
    }
135
136
    /**
137
     * @return Buffer
138
     */
139 180
    public function getBuffer()
140
    {
141 180
        return (new PublicKeySerializer($this->ecAdapter))->serialize($this);
142
    }
143
}
144