o
    `qiG@                     @   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"   ^/home/app/PaddleOCR-VL/.venv_paddleocr/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-192Z
prime192v1Z	secp192r1Znistp192)p224z
NIST P-224zP-224Z
prime224v1Z	secp224r1Znistp224)p256z
NIST P-256zP-256Z
prime256v1Z	secp256r1Znistp256)p384z
NIST P-384zP-384Z
prime384v1Z	secp384r1Znistp384)p521z
NIST P-521zP-521Z
prime521v1Z	secp521r1Znistp521ed25519ZEd25519ed448ZEd448)
curve25519Z
Curve25519ZX25519)curve448ZCurve448ZX448c                 C   s
   || j v S N	all_names)selfitemr"   r"   r#   __contains__1      
z_Curves.__contains__c                 C   s   | j S r.   r/   r1   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 r7   Z
p192_curver   r   idcurvesupdatedictfromkeys
p224_namesZ
p224_curver   
p256_namesZ
p256_curver   
p384_namesZ
p384_curver   
p521_namesZ
p521_curver   ed25519_namesr8   Zed25519_curver   ed448_namesZed448_curver   curve25519_namesr9   Zcurve25519_curver    curve448_namesZcurve448_curver!   
ValueError)r1   namer7   r%   r&   r'   r(   r)   r8   r*   r+   r9   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 r.   )curves_lockr=   getrK   rG   rH   	EccXPointZGxGEccPointZGyr<   r   r   r   
is_edwardsr    r!   Zis_montgomeryZis_weierstrass)r1   rJ   curver"   r"   r#   __getitem__i   s*   


z_Curves.__getitem__c                 C   s   | j D ]}| | }q| j S r.   )r0   r=   items)r1   rJ   _r"   r"   r#   rT   y   s   


z_Curves.itemsN)r   r   r   r=   	threadingRLockrL   r:   rA   rB   rC   rD   rE   rF   rG   rH   r0   r3   r6   rK   rS   rT   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 )+rP   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KeyErrorrI   str	canonicalrR   r<   r   r    size_in_bytesr   lenrawlib	new_point
free_pointr   _pointcontextrM   AttributeErrorr   
address_ofr   r   r   )r1   xyrR   modulus_bytesxbybre   	free_funcrh   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^   rd   clonerf   r   rg   rj   rM   rI   r   r1   pointrv   rp   rq   r"   r"   r#   set   s   


zEccPoint.setc                 C   s2   t |tsdS | jjj}d|| j |j kS NFr   )
isinstancerP   r^   rd   cmprg   rM   )r1   rx   cmp_funcr"   r"   r#   __eq__   s   

zEccPoint.__eq__c                 C   s
   | |k S r.   r"   )r1   rx   r"   r"   r#   __ne__   r4   zEccPoint.__ne__c                 C   s4   | j jj}|  }||j }|rtd| |S )Nz$Error %d while inverting an EC point)r^   rd   negcopyrg   rM   rI   )r1   Zneg_funcnprq   r"   r"   r#   __neg__   s   
zEccPoint.__neg__c                 C   s   | j \}}t||| j}|S zReturn a copy of this point.)xyrP   rR   )r1   rk   rl   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   rk   r   r5   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   rP   rR   r5   r"   r"   r#   point_at_infinity   s   zEccPoint.point_at_infinityc                 C   
   | j d S )Nr   r   r5   r"   r"   r#   rk         
z
EccPoint.xc                 C   r   )Nr   r   r5   r"   r"   r#   rl      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)rb   	bytearrayr^   rd   get_xyr   r   rg   rM   rI   r
   r   )r1   rm   rn   ro   r   rq   r"   r"   r#   r     s   
zEccPoint.xyc                 C      |   d d S z"Size of each coordinate, in bytes.r   r   size_in_bitsr5   r"   r"   r#   rb        zEccPoint.size_in_bytesc                 C      | j jS z!Size of each coordinate, in bits.r^   Zmodulus_bitsr5   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^   rd   doublerg   rM   rI   )r1   Zdouble_funcrq   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^   rd   addrg   rM   rI   )r1   rx   Zadd_funcrq   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   )r1   rx   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^   rd   scalarrI   r   rg   rM   r   r   rc   r	   r   r1   r   Zscalar_funcsbrq   r"   r"   r#   __imul__9     



zEccPoint.__imul__c                 C      |   }||9 }|S z2Return a new point, the scalar product of this oner   r1   r   r   r"   r"   r#   __mul__H  r   zEccPoint.__mul__c                 C   
   |  |S r.   r   r1   Z	left_handr"   r"   r#   __rmul__O  r4   zEccPoint.__rmul__N)r'   )r   r   r   __doc__rr   ry   r~   r   r   r   r   r   propertyrk   rl   r   rb   r   r   r   r   r   r   r   r"   r"   r"   r#   rP      s0    
)


rP   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 )rN   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 )NrX   z5EccXPoint can only be created for Curve25519/Curve448rY   rZ   r[   r\   )r]   r^   r_   rI   r`   ra   rR   r<   r   r    r!   rd   re   rf   r   rg   rh   rM   ri   r   rb   r   r   rc   rj   r   r   )	r1   rk   rR   re   rp   rh   rm   rn   rq   r"   r"   r#   rr   c  sB   



zEccXPoint.__init__c                 C   rs   rt   ru   rw   r"   r"   r#   ry     s   


zEccXPoint.setc                 C   s>   t |tsdS | jjj}| j }|j }|||}d|kS rz   )r{   rN   r^   rd   r|   rg   rM   )r1   rx   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   )rk   rI   r   rN   rR   )r1   rk   r"   r"   r#   r     s   
zEccXPoint.copyc                 C   s"   z| j }W dS  ty   Y dS w )r   TF)rk   rI   )r1   rU   r"   r"   r#   r     s   zEccXPoint.is_point_at_infinityc                 C   s   t d| jS )r   N)rN   rR   r5   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)rb   r   r^   rd   get_xr   r   rg   rM   rI   r
   r   )r1   rm   rn   r   rq   r"   r"   r#   rk     s   
zEccXPoint.xc                 C   r   r   r   r5   r"   r"   r#   rb     r   zEccXPoint.size_in_bytesc                 C   r   r   r   r5   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   r.   r   r   r"   r"   r#   r     r4   zEccXPoint.__rmul__N)r   r   r   r   rr   ry   r~   r   r   r   r   rk   rb   r   r   r   r   r"   r"   r"   r#   rN   S  s    /
		
rN   )rV   ZCrypto.Util.numberr   r   ZCrypto.Util._raw_apir   r   r   r   r   r	   ZCrypto.Math.Numbersr
   ZCrypto.Random.randomr   objectr   r$   r]   rP   rN   r"   r"   r"   r#   <module>   s    f Q