o
    5hG@                     @   s   d dl Z d dlmZmZ d dlmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZ G dd deZG dd	 d	eZe ZG d
d deZG dd deZdS )    N)bytes_to_longlong_to_bytes)VoidPointernull_pointerSmartPointerc_size_tc_uint8_ptrc_ulonglong)Integer)getrandbitsc                   @   s0   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
S )CurveID                        	   N)__name__
__module____qualname__P192P224P256P384P521ED25519ED448
CURVE25519CURVE448 r"   r"   k/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/Crypto/PublicKey/_point.pyr      s    r   c                   @   s   e Zd Zi Ze Zg dZg dZg dZ	g dZ
g dZddgZdd	gZg d
Zg dZee e	 e
 e e e e e Zdd Zdd Zdd Zdd Zdd ZdS )_Curves)p192z
NIST P-192zP-192
prime192v1	secp192r1nistp192)p224z
NIST P-224zP-224
prime224v1	secp224r1nistp224)p256z
NIST P-256zP-256
prime256v1	secp256r1nistp256)p384z
NIST P-384zP-384
prime384v1	secp384r1nistp384)p521z
NIST P-521zP-521
prime521v1	secp521r1nistp521ed25519Ed25519ed448Ed448)
curve25519
Curve25519X25519)curve448Curve448X448c                 C   s
   || j v S N	all_names)selfitemr"   r"   r#   __contains__1      
z_Curves.__contains__c                 C   s   | j S rC   rD   rF   r"   r"   r#   __dir__4   s   z_Curves.__dir__c                 C   sF  || j v rddlm} | }tj|_| jt	
| j | n|| jv r>ddlm} | }tj|_| jt	
| j| n|| jv r]ddlm} | }tj|_| jt	
| j| n|| jv r|ddlm} | }tj|_| jt	
| j| n|| jv rddlm} | }tj|_| jt	
| j| n|| jv rddlm} | }	tj|	_| jt	
| j|	 nd|| jv rddlm} | }
tj|
_| jt	
| j|
 nE|| jv rddlm} |  }tj!|_| jt	
| j| n&|| j"v rddlm} |# }tj$|_| jt	
| j"| nt%d| | j| S )Nr   )	_nist_ecc)_edwards)_montgomeryzUnsupported curve '%s')&
p192_names rL   
p192_curver   r   idcurvesupdatedictfromkeys
p224_names
p224_curver   
p256_names
p256_curver   
p384_names
p384_curver   
p521_names
p521_curver   ed25519_namesrM   ed25519_curver   ed448_namesed448_curver   curve25519_namesrN   curve25519_curver    curve448_namescurve448_curver!   
ValueError)rF   namerL   r%   r)   r-   r1   r5   rM   r9   r;   rN   r=   r@   r"   r"   r#   load7   s^   








z_Curves.loadc                 C   s   | j W | j|}|d u rR| |}|| jv s|| jv r%t|j||_n	t	|j|j
||_|jtjtjfv |_|jtjtjfv |_|jpG|j |_W d    |S W d    |S 1 s]w   Y  |S rC   )curves_lockrS   getri   rc   re   	EccXPointGxGEccPointGyrR   r   r   r   
is_edwardsr    r!   is_montgomeryis_weierstrass)rF   rh   curver"   r"   r#   __getitem__i   s*   


z_Curves.__getitem__c                 C   s   | j D ]}| | }q| j S rC   )rE   rS   items)rF   rh   _r"   r"   r#   rv   y   s   


z_Curves.itemsN)r   r   r   rS   	threadingRLockrj   rO   rW   rY   r[   r]   r_   ra   rc   re   rE   rH   rK   ri   ru   rv   r"   r"   r"   r#   r$      s4    2r$   c                   @   s   e Zd ZdZd*ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zedd Zedd Zedd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)S )+ro   a  A class to model a point on an Elliptic Curve.

    The class supports operators for:

    * Adding two points: ``R = S + T``
    * In-place addition: ``S += T``
    * Negating a point: ``R = -T``
    * Comparing two points: ``if S == T: ...`` or ``if S != T: ...``
    * Multiplying a point by a scalar: ``R = S*k``
    * In-place multiplication by a scalar: ``T *= k``

    :ivar curve: The **canonical** name of the curve as defined in the `ECC table`_.
    :vartype curve: string

    :ivar x: The affine X-coordinate of the ECC point
    :vartype x: integer

    :ivar y: The affine Y-coordinate of the ECC point
    :vartype y: integer

    :ivar xy: The tuple with affine X- and Y- coordinates
    r-   c                 C   s&  zt | | _W n ty   tdt| w | jj| _| jjtj	kr'td| 
 }t||}t||}t||ksAt||krEtd| jjj}| jjj}t | _z| jj }	W n tyf   t}	Y nw || j t|t|t||	}
|
r|
dkrtdtd|
 t| j || _d S )NUnknown curve name %sz)EccPoint cannot be created for Curve25519Incorrect coordinate length   )The EC point does not belong to the curve(Error %d while instantiating an EC point)_curves_curveKeyErrorrg   str	canonicalrt   rR   r   r    size_in_bytesr   lenrawlib	new_point
free_pointr   _pointcontextrk   AttributeErrorr   
address_ofr   r   r   )rF   xyrt   modulus_bytesxbybr   	free_funcr   resultr"   r"   r#   __init__   s@   





zEccPoint.__init__c                 C   X   | j jj}| j jj}t | _|| j |j }|r!td| t	| j || _| S Nz"Error %d while cloning an EC point
r   r   cloner   r   r   r   rk   rg   r   rF   pointr   r   r   r"   r"   r#   set   s   


zEccPoint.setc                 C   s2   t |tsdS | jjj}d|| j |j kS NFr   )
isinstancero   r   r   cmpr   rk   )rF   r   cmp_funcr"   r"   r#   __eq__   s   

zEccPoint.__eq__c                 C   s
   | |k S rC   r"   )rF   r   r"   r"   r#   __ne__   rI   zEccPoint.__ne__c                 C   s4   | j jj}|  }||j }|rtd| |S )Nz$Error %d while inverting an EC point)r   r   negcopyr   rk   rg   )rF   neg_funcnpr   r"   r"   r#   __neg__   s   
zEccPoint.__neg__c                 C   s   | j \}}t||| j}|S zReturn a copy of this point.)xyro   rt   )rF   r   r   r   r"   r"   r#   r      s   
zEccPoint.copyc                 C   s   | j jr	| jdkS | jdkS ),``True`` if this is the *point-at-infinity*.r   )r   r   )r   rq   r   r   rJ   r"   r"   r#   is_point_at_infinity   s   

zEccPoint.is_point_at_infinityc                 C   s$   | j jrtdd| jS tdd| jS )-Return the *point-at-infinity* for the curve.r   r   )r   rq   ro   rt   rJ   r"   r"   r#   point_at_infinity   s   zEccPoint.point_at_infinityc                 C   
   | j d S )Nr   r   rJ   r"   r"   r#   r         
z
EccPoint.xc                 C   r   )Nr   r   rJ   r"   r"   r#   r      r   z
EccPoint.yc                 C   sj   |   }t|}t|}| jjj}|t|t|t|| j }|r)t	d| t
t|t
t|fS )Nz#Error %d while encoding an EC point)r   	bytearrayr   r   get_xyr   r   r   rk   rg   r
   r   )rF   r   r   r   r   r   r"   r"   r#   r     s   
zEccPoint.xyc                 C      |   d d S z"Size of each coordinate, in bytes.r   r   size_in_bitsrJ   r"   r"   r#   r        zEccPoint.size_in_bytesc                 C      | j jS z!Size of each coordinate, in bits.r   modulus_bitsrJ   r"   r"   r#   r        zEccPoint.size_in_bitsc                 C   s,   | j jj}|| j }|rtd| | S )zuDouble this point (in-place operation).

        Returns:
            This same object (to enable chaining).
        z#Error %d while doubling an EC point)r   r   doubler   rk   rg   )rF   double_funcr   r"   r"   r#   r     s
   
zEccPoint.doublec                 C   sD   | j jj}|| j |j }|r |dkrtdtd| | S )zAdd a second point to this one   z#EC points are not on the same curvez#Error %d while adding two EC points)r   r   addr   rk   rg   )rF   r   add_funcr   r"   r"   r#   __iadd__'  s   
zEccPoint.__iadd__c                 C   s   |   }||7 }|S )z8Return a new point, the addition of this one and anotherr   )rF   r   r   r"   r"   r#   __add__2     zEccPoint.__add__c                 C   ^   | j jj}|dk rtdt|}|| j t|tt	|t
td}|r-td| | S zMultiply this point by a scalarr   z?Scalar multiplication is only defined for non-negative integers@   z%Error %d during scalar multiplicationr   r   scalarrg   r   r   rk   r   r   r   r	   r   rF   r   scalar_funcsbr   r"   r"   r#   __imul__9     



zEccPoint.__imul__c                 C      |   }||9 }|S z2Return a new point, the scalar product of this oner   rF   r   r   r"   r"   r#   __mul__H  r   zEccPoint.__mul__c                 C   
   |  |S rC   r   rF   	left_handr"   r"   r#   __rmul__O  rI   zEccPoint.__rmul__N)r-   )r   r   r   __doc__r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   ro      s0    
)


ro   c                   @   st   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
dd Zdd Zdd Zdd Zdd Zdd ZdS )rl   a  A class to model a point on an Elliptic Curve,
    where only the X-coordinate is exposed.

    The class supports operators for:

    * Multiplying a point by a scalar: ``R = S*k``
    * In-place multiplication by a scalar: ``T *= k``

    :ivar curve: The **canonical** name of the curve as defined in the `ECC table`_.
    :vartype curve: string

    :ivar x: The affine X-coordinate of the ECC point
    :vartype x: integer
    c           	      C   s&  zt | | _W n ty   tdt| w | jj| _| jjtj	tj
fvr*td| jjj}| jjj}t | _z| jj }W n tyK   t}Y nw |  }|d u rWt}ntt||}t||krhtdt | _|| j |t||}|dkrtd|rtd| t| j || _d S )Nrz   z5EccXPoint can only be created for Curve25519/Curve448r{   r|   r}   r~   )r   r   r   rg   r   r   rt   rR   r   r    r!   r   r   r   r   r   r   rk   r   r   r   r   r   r   r   r   r   )	rF   r   rt   r   r   r   r   r   r   r"   r"   r#   r   c  sB   



zEccXPoint.__init__c                 C   r   r   r   r   r"   r"   r#   r     s   


zEccXPoint.setc                 C   s>   t |tsdS | jjj}| j }|j }|||}d|kS r   )r   rl   r   r   r   r   rk   )rF   r   r   p1p2resr"   r"   r#   r     s   




zEccXPoint.__eq__c                 C   s2   z| j }W n ty   |   Y S w t|| jS r   )r   rg   r   rl   rt   )rF   r   r"   r"   r#   r     s   
zEccXPoint.copyc                 C   s"   z| j }W dS  ty   Y dS w )r   TF)r   rg   )rF   rw   r"   r"   r#   r     s   zEccXPoint.is_point_at_infinityc                 C   s   t d| jS )r   N)rl   rt   rJ   r"   r"   r#   r     s   zEccXPoint.point_at_infinityc                 C   s`   |   }t|}| jjj}|t|t|| j }|dkr"t	d|r*t	d| t
t|S )N   z)No X coordinate for the point at infinityz'Error %d while getting X of an EC point)r   r   r   r   get_xr   r   r   rk   rg   r
   r   )rF   r   r   r   r   r"   r"   r#   r     s   
zEccXPoint.xc                 C   r   r   r   rJ   r"   r"   r#   r     r   zEccXPoint.size_in_bytesc                 C   r   r   r   rJ   r"   r"   r#   r     r   zEccXPoint.size_in_bitsc                 C   r   r   r   r   r"   r"   r#   r     r   zEccXPoint.__imul__c                 C   r   r   r   r   r"   r"   r#   r     r   zEccXPoint.__mul__c                 C   r   rC   r   r   r"   r"   r#   r     rI   zEccXPoint.__rmul__N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   rl   S  s    /
		
rl   )rx   Crypto.Util.numberr   r   Crypto.Util._raw_apir   r   r   r   r   r	   Crypto.Math.Numbersr
   Crypto.Random.randomr   objectr   r$   r   ro   rl   r"   r"   r"   r#   <module>   s    f Q