1 | <?php |
||
17 | class PrivateKey extends Key implements PrivateKeyInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var \GMP |
||
21 | */ |
||
22 | private $secretMultiplier; |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | private $compressed; |
||
28 | |||
29 | /** |
||
30 | * @var PublicKey |
||
31 | */ |
||
32 | private $publicKey; |
||
33 | |||
34 | /** |
||
35 | * @var EcAdapter |
||
36 | */ |
||
37 | private $ecAdapter; |
||
38 | |||
39 | /** |
||
40 | * @param EcAdapter $ecAdapter |
||
41 | * @param \GMP $int |
||
42 | * @param bool $compressed |
||
43 | * @throws InvalidPrivateKey |
||
44 | */ |
||
45 | 125 | public function __construct(EcAdapter $ecAdapter, \GMP $int, $compressed = false) |
|
46 | { |
||
47 | 125 | if (false === $ecAdapter->validatePrivateKey(Buffer::int(gmp_strval($int, 10), 32, $ecAdapter->getMath()))) { |
|
48 | 2 | throw new InvalidPrivateKey('Invalid private key - must be less than curve order.'); |
|
49 | } |
||
50 | |||
51 | 123 | if (false === is_bool($compressed)) { |
|
52 | throw new \InvalidArgumentException('PrivateKey: Compressed argument must be a boolean'); |
||
53 | } |
||
54 | |||
55 | 123 | $this->ecAdapter = $ecAdapter; |
|
56 | 123 | $this->secretMultiplier = $int; |
|
57 | 123 | $this->compressed = $compressed; |
|
58 | 123 | } |
|
59 | |||
60 | /** |
||
61 | * @return \GMP |
||
62 | */ |
||
63 | 112 | public function getSecretMultiplier() |
|
67 | |||
68 | /** |
||
69 | * @param BufferInterface $msg32 |
||
70 | * @param RbgInterface|null $rbg |
||
71 | * @return \BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface |
||
72 | */ |
||
73 | public function sign(BufferInterface $msg32, RbgInterface $rbg = null) |
||
77 | |||
78 | /** |
||
79 | * @param \GMP $tweak |
||
80 | * @return PrivateKeyInterface |
||
81 | */ |
||
82 | 14 | public function tweakAdd(\GMP $tweak) |
|
83 | { |
||
84 | 14 | $adapter = $this->ecAdapter; |
|
85 | 14 | return $adapter->getPrivateKey( |
|
86 | $adapter |
||
87 | 14 | ->getMath() |
|
88 | 14 | ->getModularArithmetic( |
|
89 | $adapter |
||
90 | 14 | ->getGenerator() |
|
91 | 14 | ->getOrder() |
|
92 | 7 | ) |
|
93 | 14 | ->add( |
|
94 | 7 | $tweak, |
|
95 | 14 | $this->getSecretMultiplier() |
|
96 | 7 | ), |
|
97 | 14 | $this->compressed |
|
98 | 7 | ); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param \GMP $tweak |
||
103 | * @return PrivateKeyInterface |
||
104 | */ |
||
105 | 2 | public function tweakMul(\GMP $tweak) |
|
106 | { |
||
107 | 2 | $adapter = $this->ecAdapter; |
|
108 | 2 | return $adapter->getPrivateKey( |
|
109 | $adapter |
||
110 | 2 | ->getMath() |
|
111 | 2 | ->getModularArithmetic( |
|
112 | $adapter |
||
113 | 2 | ->getGenerator() |
|
114 | 2 | ->getOrder() |
|
115 | 1 | ) |
|
116 | 2 | ->mul( |
|
117 | 1 | $tweak, |
|
118 | 2 | $this->getSecretMultiplier() |
|
119 | 1 | ), |
|
120 | 2 | $this->compressed |
|
121 | 1 | ); |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * {@inheritDoc} |
||
126 | */ |
||
127 | 66 | public function isCompressed() |
|
131 | |||
132 | /** |
||
133 | * Return the public key |
||
134 | * |
||
135 | * @return PublicKey |
||
136 | */ |
||
137 | 129 | public function getPublicKey() |
|
151 | |||
152 | /** |
||
153 | * @param NetworkInterface $network |
||
154 | * @return string |
||
155 | */ |
||
156 | 6 | public function toWif(NetworkInterface $network = null) |
|
166 | |||
167 | /** |
||
168 | * @return \BitWasp\Buffertools\BufferInterface |
||
169 | */ |
||
170 | 66 | public function getBuffer() |
|
174 | } |
||
175 |