|
1 | | -from fastecdsa import ( |
2 | | - keys, |
3 | | - point, |
4 | | -) |
5 | | -from fastecdsa import curve as curve_types |
6 | | -from fastecdsa.encoding.sec1 import ( |
7 | | - SEC1Encoder, |
8 | | -) |
| 1 | +import sys |
9 | 2 |
|
10 | 3 | from libp2p.crypto.keys import ( |
11 | 4 | KeyPair, |
|
14 | 7 | PublicKey, |
15 | 8 | ) |
16 | 9 |
|
17 | | - |
18 | | -def infer_local_type(curve: str) -> curve_types.Curve: |
| 10 | +if sys.platform != "win32": |
| 11 | + from fastecdsa import ( |
| 12 | + keys, |
| 13 | + point, |
| 14 | + ) |
| 15 | + from fastecdsa import curve as curve_types |
| 16 | + from fastecdsa.encoding.sec1 import ( |
| 17 | + SEC1Encoder, |
| 18 | + ) |
| 19 | +else: |
| 20 | + from coincurve import PrivateKey as CPrivateKey |
| 21 | + from coincurve import PublicKey as CPublicKey |
| 22 | + |
| 23 | + |
| 24 | +def infer_local_type(curve: str) -> object: |
19 | 25 | """ |
20 | | - Convert a ``str`` representation of some elliptic curve to a |
| 26 | + Convert a str representation of some elliptic curve to a |
21 | 27 | representation understood by the backend of this module. |
22 | 28 | """ |
23 | | - if curve == "P-256": |
| 29 | + if curve != "P-256": |
| 30 | + raise NotImplementedError("Only P-256 curve is supported") |
| 31 | + |
| 32 | + if sys.platform != "win32": |
24 | 33 | return curve_types.P256 |
25 | | - else: |
26 | | - raise NotImplementedError() |
| 34 | + return "P-256" # coincurve only supports P-256 |
| 35 | + |
| 36 | + |
| 37 | +if sys.platform != "win32": |
| 38 | + |
| 39 | + class ECCPublicKey(PublicKey): |
| 40 | + def __init__(self, impl: point.Point, curve: curve_types.Curve) -> None: |
| 41 | + self.impl = impl |
| 42 | + self.curve = curve |
| 43 | + |
| 44 | + def to_bytes(self) -> bytes: |
| 45 | + return SEC1Encoder.encode_public_key(self.impl, compressed=False) |
| 46 | + |
| 47 | + @classmethod |
| 48 | + def from_bytes(cls, data: bytes, curve: str) -> "ECCPublicKey": |
| 49 | + curve_type = infer_local_type(curve) |
| 50 | + public_key_impl = SEC1Encoder.decode_public_key(data, curve_type) |
| 51 | + return cls(public_key_impl, curve_type) |
| 52 | + |
| 53 | + def get_type(self) -> KeyType: |
| 54 | + return KeyType.ECC_P256 |
| 55 | + |
| 56 | + def verify(self, data: bytes, signature: bytes) -> bool: |
| 57 | + raise NotImplementedError() |
| 58 | + |
| 59 | + class ECCPrivateKey(PrivateKey): |
| 60 | + def __init__(self, impl: int, curve: curve_types.Curve) -> None: |
| 61 | + self.impl = impl |
| 62 | + self.curve = curve |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def new(cls, curve: str) -> "ECCPrivateKey": |
| 66 | + curve_type = infer_local_type(curve) |
| 67 | + private_key_impl = keys.gen_private_key(curve_type) |
| 68 | + return cls(private_key_impl, curve_type) |
| 69 | + |
| 70 | + def to_bytes(self) -> bytes: |
| 71 | + return keys.export_key(self.impl, self.curve) |
| 72 | + |
| 73 | + def get_type(self) -> KeyType: |
| 74 | + return KeyType.ECC_P256 |
| 75 | + |
| 76 | + def sign(self, data: bytes) -> bytes: |
| 77 | + raise NotImplementedError() |
27 | 78 |
|
| 79 | + def get_public_key(self) -> PublicKey: |
| 80 | + public_key_impl = keys.get_public_key(self.impl, self.curve) |
| 81 | + return ECCPublicKey(public_key_impl, self.curve) |
28 | 82 |
|
29 | | -class ECCPublicKey(PublicKey): |
30 | | - def __init__(self, impl: point.Point, curve: curve_types.Curve) -> None: |
31 | | - self.impl = impl |
32 | | - self.curve = curve |
| 83 | +else: |
33 | 84 |
|
34 | | - def to_bytes(self) -> bytes: |
35 | | - return SEC1Encoder.encode_public_key(self.impl, compressed=False) |
| 85 | + class ECCPublicKey(PublicKey): |
| 86 | + def __init__(self, impl: CPublicKey, curve: str) -> None: |
| 87 | + self.impl = impl |
| 88 | + self.curve = curve |
36 | 89 |
|
37 | | - @classmethod |
38 | | - def from_bytes(cls, data: bytes, curve: str) -> "ECCPublicKey": |
39 | | - curve_type = infer_local_type(curve) |
40 | | - public_key_impl = SEC1Encoder.decode_public_key(data, curve_type) |
41 | | - return cls(public_key_impl, curve_type) |
| 90 | + def to_bytes(self) -> bytes: |
| 91 | + return self.impl.format(compressed=False) |
42 | 92 |
|
43 | | - def get_type(self) -> KeyType: |
44 | | - return KeyType.ECC_P256 |
| 93 | + @classmethod |
| 94 | + def from_bytes(cls, data: bytes, curve: str) -> "ECCPublicKey": |
| 95 | + curve_type = infer_local_type(curve) |
| 96 | + return cls(CPublicKey(data), curve_type) # type: ignore[arg-type] |
45 | 97 |
|
46 | | - def verify(self, data: bytes, signature: bytes) -> bool: |
47 | | - raise NotImplementedError() |
| 98 | + def get_type(self) -> KeyType: |
| 99 | + return KeyType.ECC_P256 |
48 | 100 |
|
| 101 | + def verify(self, data: bytes, signature: bytes) -> bool: |
| 102 | + raise NotImplementedError() |
49 | 103 |
|
50 | | -class ECCPrivateKey(PrivateKey): |
51 | | - def __init__(self, impl: int, curve: curve_types.Curve) -> None: |
52 | | - self.impl = impl |
53 | | - self.curve = curve |
| 104 | + class ECCPrivateKey(PrivateKey): |
| 105 | + def __init__(self, impl: CPrivateKey, curve: str) -> None: |
| 106 | + self.impl = impl |
| 107 | + self.curve = curve |
54 | 108 |
|
55 | | - @classmethod |
56 | | - def new(cls, curve: str) -> "ECCPrivateKey": |
57 | | - curve_type = infer_local_type(curve) |
58 | | - private_key_impl = keys.gen_private_key(curve_type) |
59 | | - return cls(private_key_impl, curve_type) |
| 109 | + @classmethod |
| 110 | + def new(cls, curve: str) -> "ECCPrivateKey": |
| 111 | + curve_type = infer_local_type(curve) |
| 112 | + return cls(CPrivateKey(), curve_type) # type: ignore[arg-type] |
60 | 113 |
|
61 | | - def to_bytes(self) -> bytes: |
62 | | - return keys.export_key(self.impl, self.curve) |
| 114 | + def to_bytes(self) -> bytes: |
| 115 | + return self.impl.secret |
63 | 116 |
|
64 | | - def get_type(self) -> KeyType: |
65 | | - return KeyType.ECC_P256 |
| 117 | + def get_type(self) -> KeyType: |
| 118 | + return KeyType.ECC_P256 |
66 | 119 |
|
67 | | - def sign(self, data: bytes) -> bytes: |
68 | | - raise NotImplementedError() |
| 120 | + def sign(self, data: bytes) -> bytes: |
| 121 | + raise NotImplementedError() |
69 | 122 |
|
70 | | - def get_public_key(self) -> PublicKey: |
71 | | - public_key_impl = keys.get_public_key(self.impl, self.curve) |
72 | | - return ECCPublicKey(public_key_impl, self.curve) |
| 123 | + def get_public_key(self) -> PublicKey: |
| 124 | + return ECCPublicKey(self.impl.public_key, self.curve) |
73 | 125 |
|
74 | 126 |
|
75 | 127 | def create_new_key_pair(curve: str) -> KeyPair: |
76 | 128 | """ |
77 | | - Return a new ECC keypair with the requested ``curve`` type, e.g. |
| 129 | + Return a new ECC keypair with the requested curve type, e.g. |
78 | 130 | "P-256". |
79 | 131 | """ |
80 | 132 | private_key = ECCPrivateKey.new(curve) |
|
0 commit comments