1 | <?php |
||
20 | class EcAdapter implements EcAdapterInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var Math |
||
24 | */ |
||
25 | private $math; |
||
26 | |||
27 | /** |
||
28 | * @var GeneratorPoint |
||
29 | */ |
||
30 | private $generator; |
||
31 | |||
32 | /** |
||
33 | * @var resource |
||
34 | */ |
||
35 | private $context; |
||
36 | |||
37 | /** |
||
38 | * @param Math $math |
||
39 | * @param GeneratorPoint $generator |
||
40 | * @param resource $secp256k1_context_t |
||
41 | */ |
||
42 | 5 | public function __construct(Math $math, GeneratorPoint $generator, $secp256k1_context_t) |
|
51 | |||
52 | /** |
||
53 | * @return Math |
||
54 | */ |
||
55 | 315 | public function getMath() |
|
59 | |||
60 | /** |
||
61 | * @return GeneratorPoint |
||
62 | */ |
||
63 | 36 | public function getGenerator() |
|
67 | |||
68 | /** |
||
69 | * @param BufferInterface $privateKey |
||
70 | * @return bool |
||
71 | */ |
||
72 | 61 | public function validatePrivateKey(BufferInterface $privateKey) |
|
76 | |||
77 | /** |
||
78 | * @param \GMP $element |
||
79 | * @param bool $half |
||
80 | * @return bool |
||
81 | */ |
||
82 | 3 | public function validateSignatureElement(\GMP $element, $half = false) |
|
83 | { |
||
84 | 3 | $math = $this->getMath(); |
|
85 | 3 | $against = $this->getGenerator()->getOrder(); |
|
86 | 3 | if ($half) { |
|
87 | 3 | $against = $math->rightShift($against, 1); |
|
88 | } |
||
89 | |||
90 | 3 | return $math->cmp($element, $against) < 0 && $math->cmp($element, gmp_init(0)) !== 0; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param \GMP $int |
||
95 | * @param bool|false $compressed |
||
96 | * @return PrivateKey |
||
97 | */ |
||
98 | 59 | public function getPrivateKey(\GMP $int, $compressed = false) |
|
102 | |||
103 | /** |
||
104 | * @return resource |
||
105 | */ |
||
106 | 234 | public function getContext() |
|
110 | |||
111 | /** |
||
112 | * @param BufferInterface $msg32 |
||
113 | * @param PrivateKey $privateKey |
||
114 | * @return Signature |
||
115 | */ |
||
116 | 29 | private function doSign(BufferInterface $msg32, PrivateKey $privateKey) |
|
117 | { |
||
118 | /** @var resource $sig_t */ |
||
119 | 29 | $sig_t = ''; |
|
120 | 29 | if (1 !== secp256k1_ecdsa_sign($this->context, $sig_t, $msg32->getBinary(), $privateKey->getBinary())) { |
|
121 | throw new \RuntimeException('Secp256k1: failed to sign'); |
||
122 | } |
||
123 | |||
124 | 29 | $derSig = ''; |
|
125 | 29 | secp256k1_ecdsa_signature_serialize_der($this->context, $derSig, $sig_t); |
|
126 | |||
127 | 29 | $rL = ord($derSig[3]); |
|
128 | 29 | $r = (new Buffer(substr($derSig, 4, $rL), $rL, $this->math))->getGmp(); |
|
129 | |||
130 | 29 | $sL = ord($derSig[4+$rL + 1]); |
|
131 | 29 | $s = (new Buffer(substr($derSig, 4 + $rL + 2, $sL), $sL, $this->math))->getGmp(); |
|
132 | |||
133 | 29 | return new Signature($this, $r, $s, $sig_t); |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param BufferInterface $msg32 |
||
138 | * @param PrivateKeyInterface $privateKey |
||
139 | * @param RbgInterface|null $rbg |
||
140 | * @return Signature |
||
141 | */ |
||
142 | 29 | public function sign(BufferInterface $msg32, PrivateKeyInterface $privateKey, RbgInterface $rbg = null) |
|
147 | |||
148 | /** |
||
149 | * @param BufferInterface $msg32 |
||
150 | * @param PublicKey $publicKey |
||
151 | * @param Signature $signature |
||
152 | * @return bool |
||
153 | */ |
||
154 | 90 | private function doVerify(BufferInterface $msg32, PublicKey $publicKey, Signature $signature) |
|
158 | |||
159 | /** |
||
160 | * @param BufferInterface $msg32 |
||
161 | * @param PublicKeyInterface $publicKey |
||
162 | * @param SignatureInterface $signature |
||
163 | * @return bool |
||
164 | */ |
||
165 | 90 | public function verify(BufferInterface $msg32, PublicKeyInterface $publicKey, SignatureInterface $signature) |
|
171 | |||
172 | /** |
||
173 | * @param BufferInterface $msg32 |
||
174 | * @param CompactSignature $compactSig |
||
175 | * @return PublicKey |
||
176 | */ |
||
177 | 6 | private function doRecover(BufferInterface $msg32, CompactSignature $compactSig) |
|
189 | |||
190 | /** |
||
191 | * @param BufferInterface $msg32 |
||
192 | * @param CompactSignatureInterface $compactSig |
||
193 | * @return PublicKey |
||
194 | */ |
||
195 | 6 | public function recover(BufferInterface $msg32, CompactSignatureInterface $compactSig) |
|
200 | |||
201 | /** |
||
202 | * @param BufferInterface $msg32 |
||
203 | * @param PrivateKey $privateKey |
||
204 | * @return CompactSignature |
||
205 | */ |
||
206 | 5 | private function doSignCompact(BufferInterface $msg32, PrivateKey $privateKey) |
|
207 | { |
||
208 | 5 | $sig_t = ''; |
|
209 | /** @var resource $sig_t */ |
||
210 | 5 | if (1 !== secp256k1_ecdsa_sign_recoverable($this->context, $sig_t, $msg32->getBinary(), $privateKey->getBinary())) { |
|
211 | throw new \RuntimeException('Secp256k1: failed to sign'); |
||
212 | } |
||
213 | |||
214 | 5 | $recid = ''; |
|
215 | 5 | $ser = ''; |
|
216 | 5 | if (!secp256k1_ecdsa_recoverable_signature_serialize_compact($this->context, $sig_t, $ser, $recid)) { |
|
217 | throw new \RuntimeException('Failed to obtain recid'); |
||
218 | } |
||
219 | |||
220 | 5 | unset($ser); |
|
221 | 5 | return new CompactSignature( |
|
222 | 5 | $this, |
|
223 | 5 | $sig_t, |
|
224 | 5 | $recid, |
|
225 | 5 | $privateKey->isCompressed() |
|
226 | ); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @param BufferInterface $msg32 |
||
231 | * @param PrivateKeyInterface $privateKey |
||
232 | * @param RbgInterface|null $rbg |
||
233 | * @return CompactSignatureInterface |
||
234 | */ |
||
235 | 5 | public function signCompact(BufferInterface $msg32, PrivateKeyInterface $privateKey, RbgInterface $rbg = null) |
|
240 | } |
||
241 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.