We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Add tests for ec.derive_private_key with negative secret value


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/cryptography/hazmat/primitives/asymmetric/ec.py`
Content:
```
1 # This file is dual licensed under the terms of the Apache License, Version
2 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
3 # for complete details.
4 
5 from __future__ import absolute_import, division, print_function
6 
7 import abc
8 
9 import six
10 
11 from cryptography import utils
12 
13 
14 @six.add_metaclass(abc.ABCMeta)
15 class EllipticCurve(object):
16     @abc.abstractproperty
17     def name(self):
18         """
19         The name of the curve. e.g. secp256r1.
20         """
21 
22     @abc.abstractproperty
23     def key_size(self):
24         """
25         The bit length of the base point of the curve.
26         """
27 
28 
29 @six.add_metaclass(abc.ABCMeta)
30 class EllipticCurveSignatureAlgorithm(object):
31     @abc.abstractproperty
32     def algorithm(self):
33         """
34         The digest algorithm used with this signature.
35         """
36 
37 
38 @six.add_metaclass(abc.ABCMeta)
39 class EllipticCurvePrivateKey(object):
40     @abc.abstractmethod
41     def signer(self, signature_algorithm):
42         """
43         Returns an AsymmetricSignatureContext used for signing data.
44         """
45 
46     @abc.abstractmethod
47     def exchange(self, algorithm, peer_public_key):
48         """
49         Performs a key exchange operation using the provided algorithm with the
50         provided peer's public key.
51         """
52 
53     @abc.abstractmethod
54     def public_key(self):
55         """
56         The EllipticCurvePublicKey for this private key.
57         """
58 
59     @abc.abstractproperty
60     def curve(self):
61         """
62         The EllipticCurve that this key is on.
63         """
64 
65     @abc.abstractproperty
66     def sign(self, data, signature_algorithm):
67         """
68         Signs the data
69         """
70 
71 
72 @six.add_metaclass(abc.ABCMeta)
73 class EllipticCurvePrivateKeyWithSerialization(EllipticCurvePrivateKey):
74     @abc.abstractmethod
75     def private_numbers(self):
76         """
77         Returns an EllipticCurvePrivateNumbers.
78         """
79 
80     @abc.abstractmethod
81     def private_bytes(self, encoding, format, encryption_algorithm):
82         """
83         Returns the key serialized as bytes.
84         """
85 
86 
87 @six.add_metaclass(abc.ABCMeta)
88 class EllipticCurvePublicKey(object):
89     @abc.abstractmethod
90     def verifier(self, signature, signature_algorithm):
91         """
92         Returns an AsymmetricVerificationContext used for signing data.
93         """
94 
95     @abc.abstractproperty
96     def curve(self):
97         """
98         The EllipticCurve that this key is on.
99         """
100 
101     @abc.abstractmethod
102     def public_numbers(self):
103         """
104         Returns an EllipticCurvePublicNumbers.
105         """
106 
107     @abc.abstractmethod
108     def public_bytes(self, encoding, format):
109         """
110         Returns the key serialized as bytes.
111         """
112 
113     @abc.abstractmethod
114     def verify(self, signature, data, signature_algorithm):
115         """
116         Verifies the signature of the data.
117         """
118 
119 
120 EllipticCurvePublicKeyWithSerialization = EllipticCurvePublicKey
121 
122 
123 @utils.register_interface(EllipticCurve)
124 class SECT571R1(object):
125     name = "sect571r1"
126     key_size = 571
127 
128 
129 @utils.register_interface(EllipticCurve)
130 class SECT409R1(object):
131     name = "sect409r1"
132     key_size = 409
133 
134 
135 @utils.register_interface(EllipticCurve)
136 class SECT283R1(object):
137     name = "sect283r1"
138     key_size = 283
139 
140 
141 @utils.register_interface(EllipticCurve)
142 class SECT233R1(object):
143     name = "sect233r1"
144     key_size = 233
145 
146 
147 @utils.register_interface(EllipticCurve)
148 class SECT163R2(object):
149     name = "sect163r2"
150     key_size = 163
151 
152 
153 @utils.register_interface(EllipticCurve)
154 class SECT571K1(object):
155     name = "sect571k1"
156     key_size = 571
157 
158 
159 @utils.register_interface(EllipticCurve)
160 class SECT409K1(object):
161     name = "sect409k1"
162     key_size = 409
163 
164 
165 @utils.register_interface(EllipticCurve)
166 class SECT283K1(object):
167     name = "sect283k1"
168     key_size = 283
169 
170 
171 @utils.register_interface(EllipticCurve)
172 class SECT233K1(object):
173     name = "sect233k1"
174     key_size = 233
175 
176 
177 @utils.register_interface(EllipticCurve)
178 class SECT163K1(object):
179     name = "sect163k1"
180     key_size = 163
181 
182 
183 @utils.register_interface(EllipticCurve)
184 class SECP521R1(object):
185     name = "secp521r1"
186     key_size = 521
187 
188 
189 @utils.register_interface(EllipticCurve)
190 class SECP384R1(object):
191     name = "secp384r1"
192     key_size = 384
193 
194 
195 @utils.register_interface(EllipticCurve)
196 class SECP256R1(object):
197     name = "secp256r1"
198     key_size = 256
199 
200 
201 @utils.register_interface(EllipticCurve)
202 class SECP256K1(object):
203     name = "secp256k1"
204     key_size = 256
205 
206 
207 @utils.register_interface(EllipticCurve)
208 class SECP224R1(object):
209     name = "secp224r1"
210     key_size = 224
211 
212 
213 @utils.register_interface(EllipticCurve)
214 class SECP192R1(object):
215     name = "secp192r1"
216     key_size = 192
217 
218 
219 _CURVE_TYPES = {
220     "prime192v1": SECP192R1,
221     "prime256v1": SECP256R1,
222 
223     "secp192r1": SECP192R1,
224     "secp224r1": SECP224R1,
225     "secp256r1": SECP256R1,
226     "secp384r1": SECP384R1,
227     "secp521r1": SECP521R1,
228     "secp256k1": SECP256K1,
229 
230     "sect163k1": SECT163K1,
231     "sect233k1": SECT233K1,
232     "sect283k1": SECT283K1,
233     "sect409k1": SECT409K1,
234     "sect571k1": SECT571K1,
235 
236     "sect163r2": SECT163R2,
237     "sect233r1": SECT233R1,
238     "sect283r1": SECT283R1,
239     "sect409r1": SECT409R1,
240     "sect571r1": SECT571R1,
241 }
242 
243 
244 @utils.register_interface(EllipticCurveSignatureAlgorithm)
245 class ECDSA(object):
246     def __init__(self, algorithm):
247         self._algorithm = algorithm
248 
249     algorithm = utils.read_only_property("_algorithm")
250 
251 
252 def generate_private_key(curve, backend):
253     return backend.generate_elliptic_curve_private_key(curve)
254 
255 
256 def derive_private_key(private_value, curve, backend):
257     if not isinstance(private_value, six.integer_types):
258         raise TypeError("private_value must be an integer type.")
259 
260     if not isinstance(curve, EllipticCurve):
261         raise TypeError("curve must provide the EllipticCurve interface.")
262 
263     return backend.derive_elliptic_curve_private_key(private_value, curve)
264 
265 
266 class EllipticCurvePublicNumbers(object):
267     def __init__(self, x, y, curve):
268         if (
269             not isinstance(x, six.integer_types) or
270             not isinstance(y, six.integer_types)
271         ):
272             raise TypeError("x and y must be integers.")
273 
274         if not isinstance(curve, EllipticCurve):
275             raise TypeError("curve must provide the EllipticCurve interface.")
276 
277         self._y = y
278         self._x = x
279         self._curve = curve
280 
281     def public_key(self, backend):
282         return backend.load_elliptic_curve_public_numbers(self)
283 
284     def encode_point(self):
285         # key_size is in bits. Convert to bytes and round up
286         byte_length = (self.curve.key_size + 7) // 8
287         return (
288             b'\x04' + utils.int_to_bytes(self.x, byte_length) +
289             utils.int_to_bytes(self.y, byte_length)
290         )
291 
292     @classmethod
293     def from_encoded_point(cls, curve, data):
294         if not isinstance(curve, EllipticCurve):
295             raise TypeError("curve must be an EllipticCurve instance")
296 
297         if data.startswith(b'\x04'):
298             # key_size is in bits. Convert to bytes and round up
299             byte_length = (curve.key_size + 7) // 8
300             if len(data) == 2 * byte_length + 1:
301                 x = utils.int_from_bytes(data[1:byte_length + 1], 'big')
302                 y = utils.int_from_bytes(data[byte_length + 1:], 'big')
303                 return cls(x, y, curve)
304             else:
305                 raise ValueError('Invalid elliptic curve point data length')
306         else:
307             raise ValueError('Unsupported elliptic curve point type')
308 
309     curve = utils.read_only_property("_curve")
310     x = utils.read_only_property("_x")
311     y = utils.read_only_property("_y")
312 
313     def __eq__(self, other):
314         if not isinstance(other, EllipticCurvePublicNumbers):
315             return NotImplemented
316 
317         return (
318             self.x == other.x and
319             self.y == other.y and
320             self.curve.name == other.curve.name and
321             self.curve.key_size == other.curve.key_size
322         )
323 
324     def __ne__(self, other):
325         return not self == other
326 
327     def __hash__(self):
328         return hash((self.x, self.y, self.curve.name, self.curve.key_size))
329 
330     def __repr__(self):
331         return (
332             "<EllipticCurvePublicNumbers(curve={0.curve.name}, x={0.x}, "
333             "y={0.y}>".format(self)
334         )
335 
336 
337 class EllipticCurvePrivateNumbers(object):
338     def __init__(self, private_value, public_numbers):
339         if not isinstance(private_value, six.integer_types):
340             raise TypeError("private_value must be an integer.")
341 
342         if not isinstance(public_numbers, EllipticCurvePublicNumbers):
343             raise TypeError(
344                 "public_numbers must be an EllipticCurvePublicNumbers "
345                 "instance."
346             )
347 
348         self._private_value = private_value
349         self._public_numbers = public_numbers
350 
351     def private_key(self, backend):
352         return backend.load_elliptic_curve_private_numbers(self)
353 
354     private_value = utils.read_only_property("_private_value")
355     public_numbers = utils.read_only_property("_public_numbers")
356 
357     def __eq__(self, other):
358         if not isinstance(other, EllipticCurvePrivateNumbers):
359             return NotImplemented
360 
361         return (
362             self.private_value == other.private_value and
363             self.public_numbers == other.public_numbers
364         )
365 
366     def __ne__(self, other):
367         return not self == other
368 
369     def __hash__(self):
370         return hash((self.private_value, self.public_numbers))
371 
372 
373 class ECDH(object):
374     pass
375
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/cryptography/hazmat/primitives/asymmetric/ec.py b/src/cryptography/hazmat/primitives/asymmetric/ec.py
--- a/src/cryptography/hazmat/primitives/asymmetric/ec.py
+++ b/src/cryptography/hazmat/primitives/asymmetric/ec.py
@@ -257,6 +257,9 @@
     if not isinstance(private_value, six.integer_types):
         raise TypeError("private_value must be an integer type.")
 
+    if private_value <= 0:
+        raise ValueError("private_value must be a positive integer.")
+
     if not isinstance(curve, EllipticCurve):
         raise TypeError("curve must provide the EllipticCurve interface.")

{"golden_diff": "diff --git a/src/cryptography/hazmat/primitives/asymmetric/ec.py b/src/cryptography/hazmat/primitives/asymmetric/ec.py\n--- a/src/cryptography/hazmat/primitives/asymmetric/ec.py\n+++ b/src/cryptography/hazmat/primitives/asymmetric/ec.py\n@@ -257,6 +257,9 @@\n     if not isinstance(private_value, six.integer_types):\n         raise TypeError(\"private_value must be an integer type.\")\n \n+    if private_value <= 0:\n+        raise ValueError(\"private_value must be a positive integer.\")\n+\n     if not isinstance(curve, EllipticCurve):\n         raise TypeError(\"curve must provide the EllipticCurve interface.\")\n", "issue": "Add tests for ec.derive_private_key with negative secret value\n\n", "before_files": [{"content": "# This file is dual licensed under the terms of the Apache License, Version\n# 2.0, and the BSD License. See the LICENSE file in the root of this repository\n# for complete details.\n\nfrom __future__ import absolute_import, division, print_function\n\nimport abc\n\nimport six\n\nfrom cryptography import utils\n\n\n@six.add_metaclass(abc.ABCMeta)\nclass EllipticCurve(object):\n    @abc.abstractproperty\n    def name(self):\n        \"\"\"\n        The name of the curve. e.g. secp256r1.\n        \"\"\"\n\n    @abc.abstractproperty\n    def key_size(self):\n        \"\"\"\n        The bit length of the base point of the curve.\n        \"\"\"\n\n\n@six.add_metaclass(abc.ABCMeta)\nclass EllipticCurveSignatureAlgorithm(object):\n    @abc.abstractproperty\n    def algorithm(self):\n        \"\"\"\n        The digest algorithm used with this signature.\n        \"\"\"\n\n\n@six.add_metaclass(abc.ABCMeta)\nclass EllipticCurvePrivateKey(object):\n    @abc.abstractmethod\n    def signer(self, signature_algorithm):\n        \"\"\"\n        Returns an AsymmetricSignatureContext used for signing data.\n        \"\"\"\n\n    @abc.abstractmethod\n    def exchange(self, algorithm, peer_public_key):\n        \"\"\"\n        Performs a key exchange operation using the provided algorithm with the\n        provided peer's public key.\n        \"\"\"\n\n    @abc.abstractmethod\n    def public_key(self):\n        \"\"\"\n        The EllipticCurvePublicKey for this private key.\n        \"\"\"\n\n    @abc.abstractproperty\n    def curve(self):\n        \"\"\"\n        The EllipticCurve that this key is on.\n        \"\"\"\n\n    @abc.abstractproperty\n    def sign(self, data, signature_algorithm):\n        \"\"\"\n        Signs the data\n        \"\"\"\n\n\n@six.add_metaclass(abc.ABCMeta)\nclass EllipticCurvePrivateKeyWithSerialization(EllipticCurvePrivateKey):\n    @abc.abstractmethod\n    def private_numbers(self):\n        \"\"\"\n        Returns an EllipticCurvePrivateNumbers.\n        \"\"\"\n\n    @abc.abstractmethod\n    def private_bytes(self, encoding, format, encryption_algorithm):\n        \"\"\"\n        Returns the key serialized as bytes.\n        \"\"\"\n\n\n@six.add_metaclass(abc.ABCMeta)\nclass EllipticCurvePublicKey(object):\n    @abc.abstractmethod\n    def verifier(self, signature, signature_algorithm):\n        \"\"\"\n        Returns an AsymmetricVerificationContext used for signing data.\n        \"\"\"\n\n    @abc.abstractproperty\n    def curve(self):\n        \"\"\"\n        The EllipticCurve that this key is on.\n        \"\"\"\n\n    @abc.abstractmethod\n    def public_numbers(self):\n        \"\"\"\n        Returns an EllipticCurvePublicNumbers.\n        \"\"\"\n\n    @abc.abstractmethod\n    def public_bytes(self, encoding, format):\n        \"\"\"\n        Returns the key serialized as bytes.\n        \"\"\"\n\n    @abc.abstractmethod\n    def verify(self, signature, data, signature_algorithm):\n        \"\"\"\n        Verifies the signature of the data.\n        \"\"\"\n\n\nEllipticCurvePublicKeyWithSerialization = EllipticCurvePublicKey\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT571R1(object):\n    name = \"sect571r1\"\n    key_size = 571\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT409R1(object):\n    name = \"sect409r1\"\n    key_size = 409\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT283R1(object):\n    name = \"sect283r1\"\n    key_size = 283\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT233R1(object):\n    name = \"sect233r1\"\n    key_size = 233\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT163R2(object):\n    name = \"sect163r2\"\n    key_size = 163\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT571K1(object):\n    name = \"sect571k1\"\n    key_size = 571\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT409K1(object):\n    name = \"sect409k1\"\n    key_size = 409\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT283K1(object):\n    name = \"sect283k1\"\n    key_size = 283\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT233K1(object):\n    name = \"sect233k1\"\n    key_size = 233\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT163K1(object):\n    name = \"sect163k1\"\n    key_size = 163\n\n\n@utils.register_interface(EllipticCurve)\nclass SECP521R1(object):\n    name = \"secp521r1\"\n    key_size = 521\n\n\n@utils.register_interface(EllipticCurve)\nclass SECP384R1(object):\n    name = \"secp384r1\"\n    key_size = 384\n\n\n@utils.register_interface(EllipticCurve)\nclass SECP256R1(object):\n    name = \"secp256r1\"\n    key_size = 256\n\n\n@utils.register_interface(EllipticCurve)\nclass SECP256K1(object):\n    name = \"secp256k1\"\n    key_size = 256\n\n\n@utils.register_interface(EllipticCurve)\nclass SECP224R1(object):\n    name = \"secp224r1\"\n    key_size = 224\n\n\n@utils.register_interface(EllipticCurve)\nclass SECP192R1(object):\n    name = \"secp192r1\"\n    key_size = 192\n\n\n_CURVE_TYPES = {\n    \"prime192v1\": SECP192R1,\n    \"prime256v1\": SECP256R1,\n\n    \"secp192r1\": SECP192R1,\n    \"secp224r1\": SECP224R1,\n    \"secp256r1\": SECP256R1,\n    \"secp384r1\": SECP384R1,\n    \"secp521r1\": SECP521R1,\n    \"secp256k1\": SECP256K1,\n\n    \"sect163k1\": SECT163K1,\n    \"sect233k1\": SECT233K1,\n    \"sect283k1\": SECT283K1,\n    \"sect409k1\": SECT409K1,\n    \"sect571k1\": SECT571K1,\n\n    \"sect163r2\": SECT163R2,\n    \"sect233r1\": SECT233R1,\n    \"sect283r1\": SECT283R1,\n    \"sect409r1\": SECT409R1,\n    \"sect571r1\": SECT571R1,\n}\n\n\n@utils.register_interface(EllipticCurveSignatureAlgorithm)\nclass ECDSA(object):\n    def __init__(self, algorithm):\n        self._algorithm = algorithm\n\n    algorithm = utils.read_only_property(\"_algorithm\")\n\n\ndef generate_private_key(curve, backend):\n    return backend.generate_elliptic_curve_private_key(curve)\n\n\ndef derive_private_key(private_value, curve, backend):\n    if not isinstance(private_value, six.integer_types):\n        raise TypeError(\"private_value must be an integer type.\")\n\n    if not isinstance(curve, EllipticCurve):\n        raise TypeError(\"curve must provide the EllipticCurve interface.\")\n\n    return backend.derive_elliptic_curve_private_key(private_value, curve)\n\n\nclass EllipticCurvePublicNumbers(object):\n    def __init__(self, x, y, curve):\n        if (\n            not isinstance(x, six.integer_types) or\n            not isinstance(y, six.integer_types)\n        ):\n            raise TypeError(\"x and y must be integers.\")\n\n        if not isinstance(curve, EllipticCurve):\n            raise TypeError(\"curve must provide the EllipticCurve interface.\")\n\n        self._y = y\n        self._x = x\n        self._curve = curve\n\n    def public_key(self, backend):\n        return backend.load_elliptic_curve_public_numbers(self)\n\n    def encode_point(self):\n        # key_size is in bits. Convert to bytes and round up\n        byte_length = (self.curve.key_size + 7) // 8\n        return (\n            b'\\x04' + utils.int_to_bytes(self.x, byte_length) +\n            utils.int_to_bytes(self.y, byte_length)\n        )\n\n    @classmethod\n    def from_encoded_point(cls, curve, data):\n        if not isinstance(curve, EllipticCurve):\n            raise TypeError(\"curve must be an EllipticCurve instance\")\n\n        if data.startswith(b'\\x04'):\n            # key_size is in bits. Convert to bytes and round up\n            byte_length = (curve.key_size + 7) // 8\n            if len(data) == 2 * byte_length + 1:\n                x = utils.int_from_bytes(data[1:byte_length + 1], 'big')\n                y = utils.int_from_bytes(data[byte_length + 1:], 'big')\n                return cls(x, y, curve)\n            else:\n                raise ValueError('Invalid elliptic curve point data length')\n        else:\n            raise ValueError('Unsupported elliptic curve point type')\n\n    curve = utils.read_only_property(\"_curve\")\n    x = utils.read_only_property(\"_x\")\n    y = utils.read_only_property(\"_y\")\n\n    def __eq__(self, other):\n        if not isinstance(other, EllipticCurvePublicNumbers):\n            return NotImplemented\n\n        return (\n            self.x == other.x and\n            self.y == other.y and\n            self.curve.name == other.curve.name and\n            self.curve.key_size == other.curve.key_size\n        )\n\n    def __ne__(self, other):\n        return not self == other\n\n    def __hash__(self):\n        return hash((self.x, self.y, self.curve.name, self.curve.key_size))\n\n    def __repr__(self):\n        return (\n            \"<EllipticCurvePublicNumbers(curve={0.curve.name}, x={0.x}, \"\n            \"y={0.y}>\".format(self)\n        )\n\n\nclass EllipticCurvePrivateNumbers(object):\n    def __init__(self, private_value, public_numbers):\n        if not isinstance(private_value, six.integer_types):\n            raise TypeError(\"private_value must be an integer.\")\n\n        if not isinstance(public_numbers, EllipticCurvePublicNumbers):\n            raise TypeError(\n                \"public_numbers must be an EllipticCurvePublicNumbers \"\n                \"instance.\"\n            )\n\n        self._private_value = private_value\n        self._public_numbers = public_numbers\n\n    def private_key(self, backend):\n        return backend.load_elliptic_curve_private_numbers(self)\n\n    private_value = utils.read_only_property(\"_private_value\")\n    public_numbers = utils.read_only_property(\"_public_numbers\")\n\n    def __eq__(self, other):\n        if not isinstance(other, EllipticCurvePrivateNumbers):\n            return NotImplemented\n\n        return (\n            self.private_value == other.private_value and\n            self.public_numbers == other.public_numbers\n        )\n\n    def __ne__(self, other):\n        return not self == other\n\n    def __hash__(self):\n        return hash((self.private_value, self.public_numbers))\n\n\nclass ECDH(object):\n    pass\n", "path": "src/cryptography/hazmat/primitives/asymmetric/ec.py"}], "after_files": [{"content": "# This file is dual licensed under the terms of the Apache License, Version\n# 2.0, and the BSD License. See the LICENSE file in the root of this repository\n# for complete details.\n\nfrom __future__ import absolute_import, division, print_function\n\nimport abc\n\nimport six\n\nfrom cryptography import utils\n\n\n@six.add_metaclass(abc.ABCMeta)\nclass EllipticCurve(object):\n    @abc.abstractproperty\n    def name(self):\n        \"\"\"\n        The name of the curve. e.g. secp256r1.\n        \"\"\"\n\n    @abc.abstractproperty\n    def key_size(self):\n        \"\"\"\n        The bit length of the base point of the curve.\n        \"\"\"\n\n\n@six.add_metaclass(abc.ABCMeta)\nclass EllipticCurveSignatureAlgorithm(object):\n    @abc.abstractproperty\n    def algorithm(self):\n        \"\"\"\n        The digest algorithm used with this signature.\n        \"\"\"\n\n\n@six.add_metaclass(abc.ABCMeta)\nclass EllipticCurvePrivateKey(object):\n    @abc.abstractmethod\n    def signer(self, signature_algorithm):\n        \"\"\"\n        Returns an AsymmetricSignatureContext used for signing data.\n        \"\"\"\n\n    @abc.abstractmethod\n    def exchange(self, algorithm, peer_public_key):\n        \"\"\"\n        Performs a key exchange operation using the provided algorithm with the\n        provided peer's public key.\n        \"\"\"\n\n    @abc.abstractmethod\n    def public_key(self):\n        \"\"\"\n        The EllipticCurvePublicKey for this private key.\n        \"\"\"\n\n    @abc.abstractproperty\n    def curve(self):\n        \"\"\"\n        The EllipticCurve that this key is on.\n        \"\"\"\n\n    @abc.abstractproperty\n    def sign(self, data, signature_algorithm):\n        \"\"\"\n        Signs the data\n        \"\"\"\n\n\n@six.add_metaclass(abc.ABCMeta)\nclass EllipticCurvePrivateKeyWithSerialization(EllipticCurvePrivateKey):\n    @abc.abstractmethod\n    def private_numbers(self):\n        \"\"\"\n        Returns an EllipticCurvePrivateNumbers.\n        \"\"\"\n\n    @abc.abstractmethod\n    def private_bytes(self, encoding, format, encryption_algorithm):\n        \"\"\"\n        Returns the key serialized as bytes.\n        \"\"\"\n\n\n@six.add_metaclass(abc.ABCMeta)\nclass EllipticCurvePublicKey(object):\n    @abc.abstractmethod\n    def verifier(self, signature, signature_algorithm):\n        \"\"\"\n        Returns an AsymmetricVerificationContext used for signing data.\n        \"\"\"\n\n    @abc.abstractproperty\n    def curve(self):\n        \"\"\"\n        The EllipticCurve that this key is on.\n        \"\"\"\n\n    @abc.abstractmethod\n    def public_numbers(self):\n        \"\"\"\n        Returns an EllipticCurvePublicNumbers.\n        \"\"\"\n\n    @abc.abstractmethod\n    def public_bytes(self, encoding, format):\n        \"\"\"\n        Returns the key serialized as bytes.\n        \"\"\"\n\n    @abc.abstractmethod\n    def verify(self, signature, data, signature_algorithm):\n        \"\"\"\n        Verifies the signature of the data.\n        \"\"\"\n\n\nEllipticCurvePublicKeyWithSerialization = EllipticCurvePublicKey\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT571R1(object):\n    name = \"sect571r1\"\n    key_size = 571\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT409R1(object):\n    name = \"sect409r1\"\n    key_size = 409\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT283R1(object):\n    name = \"sect283r1\"\n    key_size = 283\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT233R1(object):\n    name = \"sect233r1\"\n    key_size = 233\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT163R2(object):\n    name = \"sect163r2\"\n    key_size = 163\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT571K1(object):\n    name = \"sect571k1\"\n    key_size = 571\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT409K1(object):\n    name = \"sect409k1\"\n    key_size = 409\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT283K1(object):\n    name = \"sect283k1\"\n    key_size = 283\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT233K1(object):\n    name = \"sect233k1\"\n    key_size = 233\n\n\n@utils.register_interface(EllipticCurve)\nclass SECT163K1(object):\n    name = \"sect163k1\"\n    key_size = 163\n\n\n@utils.register_interface(EllipticCurve)\nclass SECP521R1(object):\n    name = \"secp521r1\"\n    key_size = 521\n\n\n@utils.register_interface(EllipticCurve)\nclass SECP384R1(object):\n    name = \"secp384r1\"\n    key_size = 384\n\n\n@utils.register_interface(EllipticCurve)\nclass SECP256R1(object):\n    name = \"secp256r1\"\n    key_size = 256\n\n\n@utils.register_interface(EllipticCurve)\nclass SECP256K1(object):\n    name = \"secp256k1\"\n    key_size = 256\n\n\n@utils.register_interface(EllipticCurve)\nclass SECP224R1(object):\n    name = \"secp224r1\"\n    key_size = 224\n\n\n@utils.register_interface(EllipticCurve)\nclass SECP192R1(object):\n    name = \"secp192r1\"\n    key_size = 192\n\n\n_CURVE_TYPES = {\n    \"prime192v1\": SECP192R1,\n    \"prime256v1\": SECP256R1,\n\n    \"secp192r1\": SECP192R1,\n    \"secp224r1\": SECP224R1,\n    \"secp256r1\": SECP256R1,\n    \"secp384r1\": SECP384R1,\n    \"secp521r1\": SECP521R1,\n    \"secp256k1\": SECP256K1,\n\n    \"sect163k1\": SECT163K1,\n    \"sect233k1\": SECT233K1,\n    \"sect283k1\": SECT283K1,\n    \"sect409k1\": SECT409K1,\n    \"sect571k1\": SECT571K1,\n\n    \"sect163r2\": SECT163R2,\n    \"sect233r1\": SECT233R1,\n    \"sect283r1\": SECT283R1,\n    \"sect409r1\": SECT409R1,\n    \"sect571r1\": SECT571R1,\n}\n\n\n@utils.register_interface(EllipticCurveSignatureAlgorithm)\nclass ECDSA(object):\n    def __init__(self, algorithm):\n        self._algorithm = algorithm\n\n    algorithm = utils.read_only_property(\"_algorithm\")\n\n\ndef generate_private_key(curve, backend):\n    return backend.generate_elliptic_curve_private_key(curve)\n\n\ndef derive_private_key(private_value, curve, backend):\n    if not isinstance(private_value, six.integer_types):\n        raise TypeError(\"private_value must be an integer type.\")\n\n    if private_value <= 0:\n        raise ValueError(\"private_value must be a positive integer.\")\n\n    if not isinstance(curve, EllipticCurve):\n        raise TypeError(\"curve must provide the EllipticCurve interface.\")\n\n    return backend.derive_elliptic_curve_private_key(private_value, curve)\n\n\nclass EllipticCurvePublicNumbers(object):\n    def __init__(self, x, y, curve):\n        if (\n            not isinstance(x, six.integer_types) or\n            not isinstance(y, six.integer_types)\n        ):\n            raise TypeError(\"x and y must be integers.\")\n\n        if not isinstance(curve, EllipticCurve):\n            raise TypeError(\"curve must provide the EllipticCurve interface.\")\n\n        self._y = y\n        self._x = x\n        self._curve = curve\n\n    def public_key(self, backend):\n        return backend.load_elliptic_curve_public_numbers(self)\n\n    def encode_point(self):\n        # key_size is in bits. Convert to bytes and round up\n        byte_length = (self.curve.key_size + 7) // 8\n        return (\n            b'\\x04' + utils.int_to_bytes(self.x, byte_length) +\n            utils.int_to_bytes(self.y, byte_length)\n        )\n\n    @classmethod\n    def from_encoded_point(cls, curve, data):\n        if not isinstance(curve, EllipticCurve):\n            raise TypeError(\"curve must be an EllipticCurve instance\")\n\n        if data.startswith(b'\\x04'):\n            # key_size is in bits. Convert to bytes and round up\n            byte_length = (curve.key_size + 7) // 8\n            if len(data) == 2 * byte_length + 1:\n                x = utils.int_from_bytes(data[1:byte_length + 1], 'big')\n                y = utils.int_from_bytes(data[byte_length + 1:], 'big')\n                return cls(x, y, curve)\n            else:\n                raise ValueError('Invalid elliptic curve point data length')\n        else:\n            raise ValueError('Unsupported elliptic curve point type')\n\n    curve = utils.read_only_property(\"_curve\")\n    x = utils.read_only_property(\"_x\")\n    y = utils.read_only_property(\"_y\")\n\n    def __eq__(self, other):\n        if not isinstance(other, EllipticCurvePublicNumbers):\n            return NotImplemented\n\n        return (\n            self.x == other.x and\n            self.y == other.y and\n            self.curve.name == other.curve.name and\n            self.curve.key_size == other.curve.key_size\n        )\n\n    def __ne__(self, other):\n        return not self == other\n\n    def __hash__(self):\n        return hash((self.x, self.y, self.curve.name, self.curve.key_size))\n\n    def __repr__(self):\n        return (\n            \"<EllipticCurvePublicNumbers(curve={0.curve.name}, x={0.x}, \"\n            \"y={0.y}>\".format(self)\n        )\n\n\nclass EllipticCurvePrivateNumbers(object):\n    def __init__(self, private_value, public_numbers):\n        if not isinstance(private_value, six.integer_types):\n            raise TypeError(\"private_value must be an integer.\")\n\n        if not isinstance(public_numbers, EllipticCurvePublicNumbers):\n            raise TypeError(\n                \"public_numbers must be an EllipticCurvePublicNumbers \"\n                \"instance.\"\n            )\n\n        self._private_value = private_value\n        self._public_numbers = public_numbers\n\n    def private_key(self, backend):\n        return backend.load_elliptic_curve_private_numbers(self)\n\n    private_value = utils.read_only_property(\"_private_value\")\n    public_numbers = utils.read_only_property(\"_public_numbers\")\n\n    def __eq__(self, other):\n        if not isinstance(other, EllipticCurvePrivateNumbers):\n            return NotImplemented\n\n        return (\n            self.private_value == other.private_value and\n            self.public_numbers == other.public_numbers\n        )\n\n    def __ne__(self, other):\n        return not self == other\n\n    def __hash__(self):\n        return hash((self.private_value, self.public_numbers))\n\n\nclass ECDH(object):\n    pass\n", "path": "src/cryptography/hazmat/primitives/asymmetric/ec.py"}]}