
    xhy                     ,    d dl mZ  G d d          ZdS )   )Pointc                       e Zd Zed             Zed             Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zd
S )Mathc                 0    t          ||dz   dz  |          S )Nr      )pow)clsvalueprimes      E/var/www/html/what/lib/python3.11/site-packages/ellipticcurve/math.pymodularSquareRootzMath.modularSquareRoot   s    5519*E222    c           	          |                      |                     |                     |          ||||          |          S )a  
        Fast way to multily point and scalar in elliptic curves

        :param p: First Point to mutiply
        :param n: Scalar to mutiply
        :param N: Order of the elliptic curve
        :param P: Prime number in the module of the equation Y^2 = X^3 + A*X + B (mod p)
        :param A: Coefficient of the first-order term of the equation Y^2 = X^3 + A*X + B (mod p)
        :return: Point that represents the sum of First and Second Point
        )_fromJacobian_jacobianMultiply_toJacobianr	   pnNAPs         r   multiplyzMath.multiply
   sC       !!#//!"4"4aAqAA1
 
 	
r   c                     |                      |                     |                     |          |                     |          ||          |          S )a  
        Fast way to add two points in elliptic curves

        :param p: First Point you want to add
        :param q: Second Point you want to add
        :param P: Prime number in the module of the equation Y^2 = X^3 + A*X + B (mod p)
        :param A: Coefficient of the first-order term of the equation Y^2 = X^3 + A*X + B (mod p)
        :return: Point that represents the sum of First and Second Point
        )r   _jacobianAddr   )r	   r   qr   r   s        r   addzMath.add   sM       S__Q//1C1CQJJA
 
 	
r   c                     |dk    rdS d}d}||z  }|}|dk    r#||z  }|||z  z
  }|||z  z
  }	|}|}|	}|}|dk    #||z  S )z
        Extended Euclidean Algorithm. It's the 'division' in elliptic curves

        :param x: Divisor
        :param n: Mod for division
        :return: Value representing the division
            r    )
r	   xr   lmhmlowhighrnmnws
             r   invzMath.inv)   s     661!eAggAb1fBaBDBCB Agg Avr   c                 8    t          |j        |j        d          S )z
        Convert point to Jacobian coordinates

        :param p: First Point you want to add
        :return: Point in Jacobian coordinates
        r   )r   r!   y)r	   r   s     r   r   zMath._toJacobianE   s     QS!#q!!!r   c                     |                      |j        |          }|j        |dz  z  |z  }|j        |dz  z  |z  }t	          ||d          S )z
        Convert point back from Jacobian coordinates

        :param p: First Point you want to add
        :param P: Prime number in the module of the equation Y^2 = X^3 + A*X + B (mod p)
        :return: Point in default coordinates
              r   )r)   zr!   r+   r   )r	   r   r   r/   r!   r+   s         r   r   zMath._fromJacobianO   sO     GGACOOS16\QS16\QQ1~~r   c                 @   |j         dk    rt          ddd          S |j         dz  |z  }d|j        z  |z  |z  }d|j        dz  z  ||j        dz  z  z   |z  }|dz  d|z  z
  |z  }|||z
  z  d|dz  z  z
  |z  }d|j         z  |j        z  |z  }	t          |||	          S )ac  
        Double a point in elliptic curves

        :param p: Point you want to double
        :param P: Prime number in the module of the equation Y^2 = X^3 + A*X + B (mod p)
        :param A: Coefficient of the first-order term of the equation Y^2 = X^3 + A*X + B (mod p)
        :return: Point that represents the sum of First and Second Point
        r   r-   r   r.      )r+   r   r!   r/   )
r	   r   r   r   ysqSMnxnynzs
             r   _jacobianDoublezMath._jacobianDouble^   s     3!88Aq>>!sax1nWs]a\AqL(A-dQUla1r6lQ\)Q.!#gmq RR   r   c                    |j         dk    r|S |j         dk    r|S |j        |j        dz  z  |z  }|j        |j        dz  z  |z  }|j         |j        dz  z  |z  }|j         |j        dz  z  |z  }||k    r.||k    rt          ddd          S |                     |||          S ||z
  }	||z
  }
|	|	z  |z  }|	|z  |z  }||z  |z  }|
dz  |z
  d|z  z
  |z  }|
||z
  z  ||z  z
  |z  }|	|j        z  |j        z  |z  }t          |||          S )a  
        Add two points in elliptic curves

        :param p: First Point you want to add
        :param q: Second Point you want to add
        :param P: Prime number in the module of the equation Y^2 = X^3 + A*X + B (mod p)
        :param A: Coefficient of the first-order term of the equation Y^2 = X^3 + A*X + B (mod p)
        :return: Point that represents the sum of First and Second Point
        r   r-   r.   r   )r+   r!   r/   r   r8   )r	   r   r   r   r   U1U2S1S2HRH2H3U1H2r5   r6   r7   s                    r   r   zMath._jacobianAddt   sM    3!88H3!88HcAC1Hn!cAC1Hn!cAC1Hn!cAC1Hn!88RxxQ1~~%&&q!Q///GG!eq["f\R1}1frkAH$)4"9oR'1,!#gmq RR   r   c                    |j         dk    s|dk    rt          ddd          S |dk    r|S |dk     s||k    r|                     |||z  |||          S |dz  dk    r1|                     |                     ||dz  |||          ||          S |                     |                     |                     ||dz  |||          ||          |||          S )a  
        Multily point and scalar in elliptic curves

        :param p: First Point to mutiply
        :param n: Scalar to mutiply
        :param N: Order of the elliptic curve
        :param P: Prime number in the module of the equation Y^2 = X^3 + A*X + B (mod p)
        :param A: Coefficient of the first-order term of the equation Y^2 = X^3 + A*X + B (mod p)
        :return: Point that represents the sum of First and Second Point
        r   r   r-   )r+   r   r   r8   r   r   s         r   r   zMath._jacobianMultiply   s     3!88qAvvAq>>!66Hq55AFF((AE1a;;;Ea<<&&%%aaAq991a    5 5aaAq I I1aPPRSUVXY
 
 	
r   N)__name__
__module____qualname__classmethodr   r   r   r)   r   r   r8   r   r   r    r   r   r   r      s       3 3 [3 
 
 [
 
 
 [
   [6 " " ["   [ ! ! [!* "! "! ["!H 
 
 [
 
 
r   r   N)pointr   r   r    r   r   <module>rI      sS         q
 q
 q
 q
 q
 q
 q
 q
 q
 q
r   