
    ÿigW@                         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e          Z G d d	e          Z e            Z G d
 de          Z G 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                   2    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     W/var/www/html/afkarena/venv/lib/python3.11/site-packages/Cryptodome/PublicKey/_point.pyr   r      s;        DDDDDGEJHHHr%   r   c                       e Zd Zi Z ej                    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z   e	z   e
z   ez   ez   ez   ez   ez   Zd Zd Zd Zd Z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                     || j         v S N	all_names)selfitems     r&   __contains__z_Curves.__contains__1   s    t~%%r%   c                     | j         S rH   rI   rK   s    r&   __dir__z_Curves.__dir__4   s
    ~r%   c                    || j         v reddlm} |                                }t          j        |_        | j                            t          
                    | j         |                     n|| j        v reddlm} |                                }t          j        |_        | j                            t          
                    | j        |                     n|| j        v reddlm} |                                }t          j        |_        | j                            t          
                    | j        |                     n|| j        v reddlm} |                                }t          j        |_        | j                            t          
                    | j        |                     n5|| j        v reddlm} |                                }t          j        |_        | j                            t          
                    | j        |                     n|| j        v reddlm} |                                }	t          j        |	_        | j                            t          
                    | j        |	                     nY|| j        v rdddlm} |                                }
t          j        |
_        | j                            t          
                    | j        |
                     n|| j        v rdddlm} |                                 }t          j!        |_        | j                            t          
                    | j        |                     n|| j"        v rdddlm} |#                                }t          j$        |_        | j                            t          
                    | j"        |                     ntK          d|z            | j        |         S )Nr   )	_nist_ecc)_edwards)_montgomeryzUnsupported curve '%s')&
p192_names rR   
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_namesrS   ed25519_curver    ed448_namesed448_curver!   curve25519_namesrT   curve25519_curver"   curve448_namescurve448_curver#   
ValueError)rK   namerR   r)   r-   r1   r5   r9   rS   r=   r?   rT   rA   rD   s                 r&   loadz_Curves.load7   s   4?""######''))DlDGKt}}T_dCCDDDDT_$$######''))DlDGKt}}T_dCCDDDDT_$$######''))DlDGKt}}T_dCCDDDDT_$$######''))DlDGKt}}T_dCCDDDDT_$$######''))DlDGKt}}T_dCCDDDDT'''"""""",,..G GJKt}}T-?IIJJJJT%%%""""""((**E}EHKt}}T-=uEEFFFFT***%%%%%%$5577J#.JMKt}}T-BJOOPPPPT(((%%%%%%"1133H!*HKKt}}T-@(KKLLLL5<==={4  r%   c                    | j         5  | j                            |          }||                     |          }|| 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        p|j         |_        d d d            n# 1 swxY w Y   |S rH   )curves_lockrY   getro   ri   rk   	EccXPointGxGEccPointGyrX   r   r    r!   
is_edwardsr"   r#   is_montgomeryis_weierstrass)rK   rn   curves      r&   __getitem__z_Curves.__getitem__i   s<    	A 	AKOOD))E}		$4000DD<O4O4O'$77EGG&ux4@@EG#(8/O#O &+h73E3:3C3E 'E#,1,< -@,1,?(A$	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A s   CC//C36C3c                 X    | j         D ]
}| |         }| j                                        S rH   )rJ   rY   items)rK   rn   _s      r&   r~   z_Curves.itemsy   s2    N 	 	DT
AA{  """r%   N)r   r   r   rY   	threadingRLockrq   rU   r]   r_   ra   rc   re   rg   ri   rk   rJ   rM   rP   ro   r|   r~   r$   r%   r&   r(   r(      s.       F!)/##K  J  J  J  J  J	*MG$K===555NZ'*4zAJN#$&679GHI& & &  0! 0! 0!d   # # # # #r%   r(   c                       e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zed             Zed             Zed             Zd Zd Zd Zd Zd Zd Zd Zd ZdS )rv   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
    r1   c                    	 t           |         | _        n-# t          $ r  t          dt	          |          z            w xY w| j        j        | _        | j        j        t          j	        k    rt          d          | 
                                }t          ||          }t          ||          }t          |          |k    st          |          |k    rt          d          | j        j        j        }| j        j        j        }t!                      | _        	 | j        j                                        }	n# t(          $ r
 t*          }	Y nw xY w || j                                        t/          |          t/          |          t1          |          |	          }
|
r'|
dk    rt          d          t          d|
z            t3          | 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KeyErrorrm   str	canonicalr{   rX   r   r"   size_in_bytesr   lenrawlib	new_point
free_pointr   _pointcontextrr   AttributeErrorr   
address_ofr	   r   r   )rK   xyr{   modulus_bytesxbybr   	free_funcr   results              r&   __init__zEccPoint.__init__   s   	C!%.DKK 	C 	C 	C4s5zzABBB	C[*
;>W///HIII**,,1m,,1m,,r77m##s2ww-'?'?:;;;K&0	K&1	!mm	#k)--//GG 	# 	# 	#"GGG	#4;1133&r??&r??#M22"	$ $  	R|| !LMMMG&PQQQ #4;??#4#4i@@s    *?D: :EEc                 d   | j         j        j        }| j         j        j        }t	                      | _         || j                                        |j                                                  }|rt          d|z            t          | j                                        |          | _        | S Nz"Error %d while cloning an EC point
r   r   cloner   r   r   r   rr   rm   r   rK   pointr   r   r   s        r&   setzEccPoint.set   s    "(K&1	!mmt{--//|''))+ +  	LAFJKKK"4;??#4#4i@@r%   c                     t          |t                    sdS | j        j        j        }d || j                                        |j                                                  k    S NFr   )
isinstancerv   r   r   cmpr   rr   )rK   r   cmp_funcs      r&   __eq__zEccPoint.__eq__   sW    %** 	5;%)HHT[__..0@0@0B0BCCCCr%   c                     | |k     S rH   r$   )rK   r   s     r&   __ne__zEccPoint.__ne__   s    5=  r%   c                     | j         j        j        }|                                 } ||j                                                  }|rt          d|z            |S )Nz$Error %d while inverting an EC point)r   r   negcopyr   rr   rm   )rK   neg_funcnpr   s       r&   __neg__zEccPoint.__neg__   sW    ;%)YY[[")--//** 	NCfLMMM	r%   c                 F    | j         \  }}t          ||| j                  }|S zReturn a copy of this point.)xyrv   r{   )rK   r   r   r   s       r&   r   zEccPoint.copy   s%    w1aDJ''	r%   c                 F    | j         j        r| j        dk    S | j        dk    S ),``True`` if this is the *point-at-infinity*.r   )r   r   )r   rx   r   r   rO   s    r&   is_point_at_infinityzEccPoint.is_point_at_infinity   s*     ;! 	%6Q;7f$$r%   c                 r    | j         j        rt          dd| j                  S t          dd| j                  S )-Return the *point-at-infinity* for the curve.r   r   )r   rx   rv   r{   rO   s    r&   point_at_infinityzEccPoint.point_at_infinity   s9     ;! 	.Aq$*---Aq$*---r%   c                     | j         d         S )Nr   r   rO   s    r&   r   z
EccPoint.x       wqzr%   c                     | j         d         S )Nr   r   rO   s    r&   r   z
EccPoint.y   r   r%   c                    |                                  }t          |          }t          |          }| j        j        j        } |t          |          t          |          t          |          | j                                                  }|rt          d|z            t          t          |                    t          t          |                    fS )Nz#Error %d while encoding an EC point)r   	bytearrayr   r   get_xyr	   r   r   rr   rm   r   r   )rK   r   r   r   r   r   s         r&   r   zEccPoint.xy  s    **,,}%%}%%#*B#B //))+ +  	MBVKLLLb))**GM"4E4E,F,FGGr%   c                 6    |                                  dz   dz  S z"Size of each coordinate, in bytes.r   r   size_in_bitsrO   s    r&   r   zEccPoint.size_in_bytes      !!##a'A--r%   c                     | j         j        S z!Size of each coordinate, in bits.r   modulus_bitsrO   s    r&   r   zEccPoint.size_in_bits      {''r%   c                     | j         j        j        } || j                                                  }|rt          d|z            | 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   rr   rm   )rK   double_funcr   s      r&   r   zEccPoint.double  sN     k(/T[__..// 	MBVKLLLr%   c                     | j         j        j        } || j                                        |j                                                  }|r'|dk    rt          d          t          d|z            | 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   rr   rm   )rK   r   add_funcr   s       r&   __iadd__zEccPoint.__iadd__'  sv     ;%)$+//++U\-=-=-?-?@@ 	M|| !FGGGBVKLLLr%   c                 8    |                                  }||z  }|S )z8Return a new point, the addition of this one and anotherr   )rK   r   r   s      r&   __add__zEccPoint.__add__2  s     YY[[
e	r%   c           
      d   | j         j        j        }|dk     rt          d          t	          |          } || j                                        t          |          t          t          |                    t          t          d                              }|rt          d|z            | 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   scalarrm   r   r   rr   r	   r   r   r
   r   rK   r   scalar_funcsbr   s        r&   __imul__zEccPoint.__imul__9       k(/A::^___6""T[__..(__%c"gg..(R99; ;  	ODvMNNNr%   c                 8    |                                  }||z  }|S z2Return a new point, the scalar product of this oner   rK   r   r   s      r&   __mul__zEccPoint.__mul__H       YY[[
f	r%   c                 ,    |                      |          S rH   r   rK   	left_hands     r&   __rmul__zEccPoint.__rmul__O      ||I&&&r%   N)r1   )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&   rv   rv      so        .'A 'A 'A 'AR  D D D! ! !    % % %. . .   X   X H H XH. . .( ( (  	 	 	      ' ' ' ' 'r%   rv   c                   j    e Zd ZdZd Zd Zd Zd Zd Zd Z	e
d             Zd	 Zd
 Zd Zd Zd ZdS )rs   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                    	 t           |         | _        n-# t          $ r  t          dt	          |          z            w xY w| j        j        | _        | j        j        t          j	        t          j
        fvrt          d          | j        j        j        }| j        j        j        }t                      | _        	 | j        j                                        }n# t$          $ r
 t&          }Y nw xY w|                                 }|t&          }n?t+          t-          ||                    }t/          |          |k    rt          d          t                      | _         || j                                        |t3          |          |          }|dk    rt          d          |rt          d|z            t5          | j                                        |          | _        d S )Nr   z5EccXPoint can only be created for Curve25519/Curve448r   r   r   r   )r   r   r   rm   r   r   r{   rX   r   r"   r#   r   r   r   r   r   r   rr   r   r   r   r	   r   r   r   r   r   )	rK   r   r{   r   r   r   r   r   r   s	            r&   r   zEccXPoint.__init__c  s   
	C!%.DKK 	C 	C 	C4s5zzABBB	C[*
;>'"4g6F!GGGTUUUK&0	K&1	!mm	#k)--//GG 	# 	# 	#"GGG	# **,,9BB]1m<<==B2ww-'' !>???!mm4;1133#M22"$ $
 R<<HIII 	RG&PQQQ #4;??#4#4i@@s    *?<C C/.C/c                 d   | j         j        j        }| j         j        j        }t	                      | _         || j                                        |j                                                  }|rt          d|z            t          | j                                        |          | _        | S r   r   r   s        r&   r   zEccXPoint.set  s    "(K&1	!mmt{--//|''))+ + 	LAFJKKK"4;??#4#4i@@r%   c                     t          |t                    sdS | j        j        j        }| j                                        }|j                                        } |||          }d|k    S r   )r   rs   r   r   r   r   rr   )rK   r   r   p1p2ress         r&   r   zEccXPoint.__eq__  sd    %++ 	5;%)[__\hr2Cxr%   c                     	 | j         }n$# t          $ r |                                 cY S w xY wt          || j                  S r   )r   rm   r   rs   r{   )rK   r   s     r&   r   zEccXPoint.copy  sU    	,AA 	, 	, 	,))+++++	,DJ'''s   
 ++c                 :    	 | j         }n# t          $ r Y dS w xY wdS )r   TF)r   rm   )rK   r   s     r&   r   zEccXPoint.is_point_at_infinity  s7    	AA 	 	 	44	us   
 
c                 ,    t          d| j                  S )r   N)rs   r{   rO   s    r&   r   zEccXPoint.point_at_infinity  s     tz***r%   c                 p   |                                  }t          |          }| j        j        j        } |t          |          t          |          | j                                                  }|dk    rt          d          |rt          d|z            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   rr   rm   r   r   )rK   r   r   r   r   s        r&   r   zEccXPoint.x  s    **,,}%%"({2..{((* * R<<HIII 	QFOPPP}R(()))r%   c                 6    |                                  dz   dz  S r   r   rO   s    r&   r   zEccXPoint.size_in_bytes  r   r%   c                     | j         j        S r   r   rO   s    r&   r   zEccXPoint.size_in_bits  r   r%   c           
      d   | j         j        j        }|dk     rt          d          t	          |          } || j                                        t          |          t          t          |                    t          t          d                              }|rt          d|z            | S r   r   r   s        r&   r   zEccXPoint.__imul__  r   r%   c                 8    |                                  }||z  }|S r   r   r   s      r&   r   zEccXPoint.__mul__  r   r%   c                 ,    |                      |          S rH   r   r   s     r&   r   zEccXPoint.__rmul__  r   r%   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r%   r&   rs   rs   S  s         -A -A -A^    ( ( (  + + +
 * * X*. . .( ( (    ' ' ' ' 'r%   rs   )r   Cryptodome.Util.numberr   r   Cryptodome.Util._raw_apir   r   r   r   r	   r
   Cryptodome.Math.Numbersr   Cryptodome.Random.randomr   objectr   r(   r   rv   rs   r$   r%   r&   <module>r     s|       ? ? ? ? ? ? ? ?/ / / / / / / / / / / / / / / / , + + + + + 0 0 0 0 0 0	 	 	 	 	f 	 	 	c# c# c# c# c#f c# c# c#L '))M' M' M' M' M'v M' M' M'`Z' Z' Z' Z' Z' Z' Z' Z' Z' Z'r%   