
    df68                        d dl mZ d dlmZ ddlmZmZmZmZ ddl	m
Z
mZmZ ddlmZmZ g dZdZd	Z G d
 de      Z G d d      Z edej,                  ej.                  dd      Z edej2                  ej4                  dd      Z edej8                  ej:                  dd      Z edej>                  ej@                  dd      Z! edejD                  ejF                  dd      Z$ edejJ                  ejL                  dd      Z' ed ejP                  ejR                  d!d"      Z* ed#ejV                  ejX                  d$d%      Z- ed&ej\                  ej^                  d'd(      Z0 ed)ejb                  ejd                  d*d+      Z3 ed,ejh                  ejj                  d-d.      Z6 ed/ejn                  ejp                  d0d1      Z9 ed2ejt                  ejv                  d3d4      Z< ed5ejz                  ej|                  d6d7      Z? ed8ej                  ej                  d9d:      ZB ed;ej                  ej                  d<d=      ZE ed>ej                  ej                  d?d@      ZH edAej                  ej                  dB      ZK edCej                  ej                  dD      ZNe$e'e*e-e0e3e6e9e<e?eBeEeHeeee!eKeNgZOdE ZPdF ZQyG)H    )division)PY2   )derecdsaellipticcurveeddsa)orderlennumber_to_stringstring_to_number)normalise_bytes
bit_length)UnknownCurveErrorr
   Curve	SECP112r1	SECP112r2	SECP128r1	SECP160r1NIST192pNIST224pNIST256pNIST384pNIST521pcurves
find_curvecurve_by_name	SECP256k1BRAINPOOLP160r1BRAINPOOLP192r1BRAINPOOLP224r1BRAINPOOLP256r1BRAINPOOLP320r1BRAINPOOLP384r1BRAINPOOLP512r1PRIME_FIELD_OIDCHARACTERISTIC_TWO_FIELD_OIDEd25519Ed448)r      H  ='  r   r   )r   r)   r*   r+   r   r)   c                       e Zd Zy)r   N)__name__
__module____qualname__     E/home/api-vastappli/venv/lib/python3.12/site-packages/ecdsa/curves.pyr   r   .   s    r1   r   c                   Z    e Zd Zd
dZd Zd Zd ZddZddZe	d
d       Z
ed
d	       Zy)r   Nc                    || _         || _        || _        || _        |j	                         | _        t        |t        j                        r9t        |j                               dz   dz   dz  | _
        | j                  | _        n;t        | j                        | _
        dt        |j                               z  | _        d| j                  z  | _        || _        |rt        j                   | | _        y y )Nr         r)   )nameopenssl_namecurve	generatororder
isinstancer   	CurveEdTwr   pbaselenverifying_key_lengthr
   signature_lengthoidr   
encode_oidencoded_oid)selfr7   r9   r:   rB   r8   s         r2   __init__zCurve.__init__3   s    	(
"__&
e]445
 'uwwy1A59a?DL(,D%#DJJ/DL()HUWWY,?(?D% !DLL 0"~~s3D r1   c                     t        |t              r4| j                  |j                  k(  xr | j                  |j                  k(  S t        S N)r<   r   r9   r:   NotImplementedrE   others     r2   __eq__zCurve.__eq__H   s;    eU#

ekk)Odnn.O r1   c                     | |k(   S rH   r0   rJ   s     r2   __ne__zCurve.__ne__O   s    5=  r1   c                     | j                   S rH   )r7   )rE   s    r2   __repr__zCurve.__repr__R   s    yyr1   c           	         || j                   rd}nd}|dvrt        d      |dk(  r3| j                   st        d      t        j                  | j                    S t        | j                  t        j                        r|dk(  sJ t        d      | j                  j                         }t        j                  d      }t        j                  t        j                  t         t        j                  |            }t        j                  t        j                  t        | j                  j                         |z  |            t        j                  t        | j                  j!                         |z  |                  }t        j                  | j"                  j%                  |            }t        j                  | j"                  j'                               }|||||g}	| j                  j)                         r>t        j                  | j                  j)                               }
|	j+                  |
       t        j                  |	 S )a  Serialise the curve parameters to binary string.

        :param str encoding: the format to save the curve parameters in.
            Default is ``named_curve``, with fallback being the ``explicit``
            if the OID is not set for the curve.
        :param str point_encoding: the point encoding of the generator when
            explicit curve encoding is used. Ignored for ``named_curve``
            format.

        :return: DER encoded ECParameters structure
        :rtype: bytes
        named_curveexplicitrR   rS   z5Only 'named_curve' and 'explicit' encodings supportedzJCan't encode curve using named_curve encoding without associated curve OIDz6Twisted Edwards curves don't support explicit encodingr   )rB   
ValueErrorr   r   rC   r<   r9   r   r=   r>   encode_integerencode_sequencer%   encode_octet_stringr   abr:   to_bytesr;   cofactorappend)rE   encodingpoint_encodingcurve_pversionfield_idr9   baser;   seq_elementsr\   s              r2   to_derzCurve.to_derU   s    xx(%66G  }$88'+  >>488,,

M$;$;<z))#H 
 **,,.$$Q'&&NNO,c.@.@.I
 #### '!97C ## '!97C	
 &&t~~'>'>~'NO""4>>#7#7#9:5$>:: ))$***=*=*?@H)""L11r1   c                 N    t        j                  | j                  ||      d      S )a  
        Serialise the curve parameters to the :term:`PEM` format.

        :param str encoding: the format to save the curve parameters in.
            Default is ``named_curve``, with fallback being the ``explicit``
            if the OID is not set for the curve.
        :param str point_encoding: the point encoding of the generator when
            explicit curve encoding is used. Ignored for ``named_curve``
            format.

        :return: PEM encoded ECParameters structure
        :rtype: str
        zEC PARAMETERS)r   topemre   )rE   r^   r_   s      r2   to_pemzCurve.to_pem   s%     yyKK.1?
 	
r1   c                 p   |st        d      }t        d |D              st        d      t        |       } t	        j
                  |       sSd|vrt	        j                  d      t	        j                  |       \  }}|rt	        j                  d      t        |      S d|vrt	        j                  d      t	        j                  |       \  }}|rt	        j                  d	      t	        j                  |      \  }}|d
k7  rt	        j                  d      t	        j                  |      \  }}t	        j                  |      \  }}t	        j                  |      \  }	}t	        j                  |      \  }
}d}|rt	        j                  |      \  }}t	        j                  |      \  }}|t        k(  rt        d      |t        k7  rt        dj                  |            t	        j                  |      \  }}|rt	        j                  d      t	        j                  |      \  }}t	        j                  |      \  }}t!        |      }t!        |      }t#        j$                  ||||      }t"        j&                  j)                  ||	d|
d      }t+        d||d      }t,        D ]  }||k(  s	|c S  |S )a  Decode the curve parameters from DER file.

        :param data: the binary string to decode the parameters from
        :type data: :term:`bytes-like object`
        :param valid_encodings: set of names of allowed encodings, by default
            all (set by passing ``None``), supported ones are ``named_curve``
            and ``explicit``
        :type valid_encodings: :term:`set-like object`
        rT   c              3   $   K   | ]  }|d v  
 yw)rT   Nr0   ).0is     r2   	<genexpr>z!Curve.from_der.<locals>.<genexpr>   s     M133Ms   z1Only named_curve and explicit encodings supportedrR   z(named_curve curve parameters not allowedzUnexpected data after OIDrS   z%explicit curve parameters not allowedz,Unexpected data after ECParameters structurer   z!Unknown parameter encoding formatNz#Characteristic 2 curves unsupportedzUnknown field type: {0}z:Unexpected data after ECParameters.fieldID.Prime-p element)uncompressed
compressedhybridT)valid_encodingsr;   r:   unknown)setallrU   r   r   is_sequenceUnexpectedDERremove_objectr   remove_sequenceremove_integerremove_octet_stringr&   r   r%   formatr   r   CurveFpPointJacobi
from_bytesr   r   )datarq   rB   emptyseqra   restrb   r9   
base_bytesr;   r\   _
field_typeprimecurve_a_bytescurve_b_bytescurve_acurve_bcurve_fprc   	tmp_curverl   s                          r2   from_derzCurve.from_der   s    !"=>OM_MMC  t$t$O3''>  **40JC''(CDDc?"_,##$KLL((.
U##>  **3/a<##$GHH,,T2$))$/t2248
D((.t ,,T2KHa ,,X6
D55#$IJJ(#)00<  ))$/u##L 
 "55e<t!55d;t #=1"=1 (((K ((33D 4 
 )XtT:	  	AA~	 r1   c                     t         s t        |t              r|j                         }|j	                  d      }|dk(  rt        j                  d      | j                  t        j                  ||d       |      S )am  Decode the curve parameters from PEM file.

        :param str string: the text string to decode the parameters from
        :param valid_encodings: set of names of allowed encodings, by default
            all (set by passing ``None``), supported ones are ``named_curve``
            and ``explicit``
        :type valid_encodings: :term:`set-like object`
        s   -----BEGIN EC PARAMETERS-----z"EC PARAMETERS PEM header not foundN)	r   r<   strencodefindr   rv   r   unpem)clsstringrq   ec_param_indexs       r2   from_pemzCurve.from_pem   sj     z&#.]]_F%EFR##$HII||IIf^_-.
 	
r1   rH   )Nrn   )r-   r.   r/   rF   rL   rN   rP   re   rh   staticmethodr   classmethodr   r0   r1   r2   r   r   2   sL    4*!:2x
$ Y Yv 
 
r1   r   r   )r         r      	secp112r1r   )r   r   r   r   r5   	secp112r2r   )r   r   r   r      	secp128r1r   )r   r   r   r   r6   	secp160r1r   )r   r)   r*   r+   r   r   r   
prime192v1r   )r   r   r   r   !   	secp224r1r   )r   r)   r*   r+   r   r   r5   
prime256v1r   )r   r   r   r   "   	secp384r1r   )r   r   r   r   #   	secp521r1r   )r   r   r   r   
   	secp256k1r   )
r   r   $   r   r   r)   r6   r   r   r   brainpoolP160r1r   )
r   r   r   r   r   r)   r6   r   r   r   brainpoolP192r1r    )
r   r   r   r   r   r)   r6   r   r      brainpoolP224r1r!   )
r   r   r   r   r   r)   r6   r   r   r5   brainpoolP256r1r"   )
r   r   r   r   r   r)   r6   r   r   	   brainpoolP320r1r#   )
r   r   r   r   r   r)   r6   r   r      brainpoolP384r1r$   )
r   r   r   r   r   r)   r6   r   r      brainpoolP512r1r'   )r   r   e   p   r(   )r   r   r   q   c           	          t         D ]  }|j                  | k(  s|c S  t        d| dt         D cg c]  }|j                   c}      c c}w )a9  Select a curve based on its OID

    :param tuple[int,...] oid_curve: ASN.1 Object Identifier of the
        curve to return, like ``(1, 2, 840, 10045, 3, 1, 7)`` for ``NIST256p``.

    :raises UnknownCurveError: When the oid doesn't match any of the supported
        curves

    :rtype: ~ecdsa.curves.Curve
    z&I don't know about the curve with oid z.I only know about these: )r   rB   r   r7   )	oid_curvecs     r2   r   r     sP      55IH )2V4LQVV4L	N 4Ls   Ac           	          t         D ]1  }| |j                  k(  s|j                  s| |j                  k(  s/|c S  t        dj	                  | t         D cg c]  }|j                   c}            c c}w )a{  Select a curve based on its name.

    Returns a :py:class:`~ecdsa.curves.Curve` object with a ``name`` name.
    Note that ``name`` is case-sensitve.

    :param str name: Name of the curve to return, like ``NIST256p`` or
        ``prime256v1``

    :raises UnknownCurveError: When the name doesn't match any of the supported
        curves

    :rtype: ~ecdsa.curves.Curve
    z9Curve with name {0!r} unknown, only curves supported: {1})r   r7   r8   r   r{   )r7   r   s     r2   r   r     sh      166>ann1GH CJJ6*a166*	
 *s   A2N)R
__future__r   sixr    r   r   r   r	   utilr
   r   r   _compatr   r   __all__r%   r&   	Exceptionr   r   curve_112r1generator_112r1r   curve_112r2generator_112r2r   curve_128r1generator_128r1r   curve_160r1generator_160r1r   	curve_192generator_192r   	curve_224generator_224r   	curve_256generator_256r   	curve_384generator_384r   	curve_521generator_521r   curve_secp256k1generator_secp256k1r   curve_brainpoolp160r1generator_brainpoolp160r1r   curve_brainpoolp192r1generator_brainpoolp192r1r   curve_brainpoolp224r1generator_brainpoolp224r1r    curve_brainpoolp256r1generator_brainpoolp256r1r!   curve_brainpoolp320r1generator_brainpoolp320r1r"   curve_brainpoolp384r1generator_brainpoolp384r1r#   curve_brainpoolp512r1generator_brainpoolp512r1r$   curve_ed25519generator_ed25519r'   curve_ed448generator_ed448r(   r   r   r   r0   r1   r2   <module>r      sh     . . > > 0
> +7 		 	`
 `
H 			 			 			 			 	OO	 	OO	 	OO	 	OO	 	OO	 			 		### 		### 		### 		### 		### 		##$ 		##$ 			 					 	'
.(r1   