File: ec.py

package info (click to toggle)
python-ledger-bitcoin 0.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 720 kB
  • sloc: python: 9,357; makefile: 2
file content (263 lines) | stat: -rw-r--r-- 8,233 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
from . import base58
from . import hashes
from .misc import secp256k1
from .networks import NETWORKS
from .base import EmbitBase, EmbitError, EmbitKey
from binascii import hexlify, unhexlify


class ECError(EmbitError):
    pass


class Signature(EmbitBase):
    def __init__(self, sig):
        self._sig = sig

    def write_to(self, stream) -> int:
        return stream.write(secp256k1.ecdsa_signature_serialize_der(self._sig))

    @classmethod
    def read_from(cls, stream):
        der = stream.read(2)
        der += stream.read(der[1])
        return cls(secp256k1.ecdsa_signature_parse_der(der))


class SchnorrSig(EmbitBase):
    def __init__(self, sig):
        assert len(sig) == 64
        self._sig = sig

    def write_to(self, stream) -> int:
        return stream.write(self._sig)

    @classmethod
    def read_from(cls, stream):
        return cls(stream.read(64))


class PublicKey(EmbitKey):
    def __init__(self, point: bytes, compressed: bool = True):
        self._point = point
        self.compressed = compressed

    @classmethod
    def read_from(cls, stream):
        b = stream.read(1)
        if b not in [b"\x02", b"\x03", b"\x04"]:
            raise ECError("Invalid public key")
        if b == b"\x04":
            b += stream.read(64)
        else:
            b += stream.read(32)
        try:
            point = secp256k1.ec_pubkey_parse(b)
        except Exception as e:
            raise ECError(str(e))
        compressed = b[0] != 0x04
        return cls(point, compressed)

    def sec(self) -> bytes:
        """Sec representation of the key"""
        flag = secp256k1.EC_COMPRESSED if self.compressed else secp256k1.EC_UNCOMPRESSED
        return secp256k1.ec_pubkey_serialize(self._point, flag)

    def xonly(self) -> bytes:
        return self.sec()[1:33]

    def taproot_tweak(self, h=b""):
        """Returns a tweaked public key"""
        x = self.xonly()
        tweak = hashes.tagged_hash("TapTweak", x + h)
        if not secp256k1.ec_seckey_verify(tweak):
            raise EmbitError("Tweak is too large")
        point = secp256k1.ec_pubkey_parse(b"\x02" + x)
        pub = secp256k1.ec_pubkey_add(point, tweak)
        sec = secp256k1.ec_pubkey_serialize(pub)
        return PublicKey.from_xonly(sec[1:33])

    def write_to(self, stream) -> int:
        return stream.write(self.sec())

    def serialize(self) -> bytes:
        return self.sec()

    def verify(self, sig, msg_hash) -> bool:
        return bool(secp256k1.ecdsa_verify(sig._sig, msg_hash, self._point))

    def _xonly(self):
        """Returns internal representation of the xonly-pubkey (64 bytes)"""
        pub, _ = secp256k1.xonly_pubkey_from_pubkey(self._point)
        return pub

    @classmethod
    def from_xonly(cls, data: bytes):
        assert len(data) == 32
        return cls.parse(b"\x02" + data)

    def schnorr_verify(self, sig, msg_hash) -> bool:
        return bool(secp256k1.schnorrsig_verify(sig._sig, msg_hash, self._xonly()))

    @classmethod
    def from_string(cls, s):
        return cls.parse(unhexlify(s))

    @property
    def is_private(self) -> bool:
        return False

    def to_string(self):
        return hexlify(self.sec()).decode()

    def __lt__(self, other):
        # for lexagraphic ordering
        return self.sec() < other.sec()

    def __gt__(self, other):
        # for lexagraphic ordering
        return self.sec() > other.sec()

    def __eq__(self, other):
        return self.sec() == other.sec()

    def __hash__(self):
        return hash(self._point)


class PrivateKey(EmbitKey):
    def __init__(self, secret, compressed: bool = True, network=NETWORKS["main"]):
        """Creates a private key from 32-byte array"""
        if len(secret) != 32:
            raise ECError("Secret should be 32-byte array")
        if not secp256k1.ec_seckey_verify(secret):
            raise ECError("Secret is not valid (larger then N?)")
        self.compressed = compressed
        self._secret = secret
        self.network = network

    def wif(self, network=None) -> str:
        """Export private key as Wallet Import Format string.
        Prefix 0x80 is used for mainnet, 0xEF for testnet.
        This class doesn't store this information though.
        """
        if network is None:
            network = self.network
        prefix = network["wif"]
        b = prefix + self._secret
        if self.compressed:
            b += bytes([0x01])
        return base58.encode_check(b)

    @property
    def secret(self):
        return self._secret

    def sec(self) -> bytes:
        """Sec representation of the corresponding public key"""
        return self.get_public_key().sec()

    def xonly(self) -> bytes:
        return self.sec()[1:]

    def taproot_tweak(self, h=b""):
        """Returns a tweaked private key"""
        sec = self.sec()
        negate = sec[0] != 0x02
        x = sec[1:33]
        tweak = hashes.tagged_hash("TapTweak", x + h)
        if not secp256k1.ec_seckey_verify(tweak):
            raise EmbitError("Tweak is too large")
        if negate:
            secret = secp256k1.ec_privkey_negate(self._secret)
        else:
            secret = self._secret
        res = secp256k1.ec_privkey_add(secret, tweak)
        pk = PrivateKey(res)
        if pk.sec()[0] == 0x03:
            pk = PrivateKey(secp256k1.ec_privkey_negate(res))
        return pk

    @classmethod
    def from_wif(cls, s):
        """Import private key from Wallet Import Format string."""
        b = base58.decode_check(s)
        prefix = b[:1]
        network = None
        for net in NETWORKS:
            if NETWORKS[net]["wif"] == prefix:
                network = NETWORKS[net]
        secret = b[1:33]
        compressed = False
        if len(b) not in [33, 34]:
            raise ECError("Wrong WIF length")
        if len(b) == 34:
            if b[-1] == 0x01:
                compressed = True
            else:
                raise ECError("Wrong WIF compressed flag")
        return cls(secret, compressed, network)

    # to unify API
    def to_base58(self, network=None) -> str:
        return self.wif(network)

    @classmethod
    def from_base58(cls, s):
        return cls.from_wif(s)

    def get_public_key(self) -> PublicKey:
        return PublicKey(secp256k1.ec_pubkey_create(self._secret), self.compressed)

    def to_public(self) -> PublicKey:
        """Alias to get_public_key for API consistency"""
        return self.get_public_key()

    def sign(self, msg_hash, grind=True) -> Signature:
        sig = Signature(secp256k1.ecdsa_sign(msg_hash, self._secret))
        if grind:
            counter = 1
            while len(sig.serialize()) > 70:
                sig = Signature(
                    secp256k1.ecdsa_sign(
                        msg_hash, self._secret, None, counter.to_bytes(32, "little")
                    )
                )
                counter += 1
                # just in case we get in infinite loop for some reason
                if counter > 200:
                    break
        return sig

    def schnorr_sign(self, msg_hash) -> SchnorrSig:
        return SchnorrSig(secp256k1.schnorrsig_sign(msg_hash, self._secret))

    def verify(self, sig, msg_hash) -> bool:
        return self.get_public_key().verify(sig, msg_hash)

    def schnorr_verify(self, sig, msg_hash) -> bool:
        return self.get_public_key().schnorr_verify(sig, msg_hash)

    def write_to(self, stream) -> int:
        # return a copy of the secret
        return stream.write(self._secret)

    def ecdh(self, public_key: PublicKey, hashfn=None, data=None) -> bytes:
        pubkey_point = secp256k1.ec_pubkey_parse(public_key.sec())
        return secp256k1.ecdh(pubkey_point, self._secret, hashfn, data)

    @classmethod
    def read_from(cls, stream):
        # just to unify the API
        return cls(stream.read(32))

    @property
    def is_private(self) -> bool:
        return True


# Nothing up my sleeve point for no-internal-key taproot
# see https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#constructing-and-spending-taproot-outputs
NUMS_PUBKEY = PublicKey.from_string(
    "0250929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"
)