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)) { |
|
|
|
|
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); |
|
|
|
|
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)) { |
|
|
|
|
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)) { |
|
|
|
|
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())) { |
|
|
|
|
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)) { |
|
|
|
|
106
|
|
|
throw new \RuntimeException("failed to serialize xonly pubkey!"); |
107
|
|
|
} |
108
|
|
|
return new Buffer($out); |
109
|
|
|
} |
110
|
|
|
} |