3
uQha                 @   s"   d Z ddlmZ G dd dZdS )z/
This module holds a fast Factorization class.
    )randintc               @   s(   e Zd ZdZedd Zedd ZdS )FactorizationzB
    Simple module to factorize large numbers really quickly.
    c             C   s  |d dkrd|d fS t d|d t d|d t d|d   }}}d } }}d }}	x|dkr|}x$t|D ]}
t|d|| | }qtW d}xr||k o|dkr|}	xBtt||| D ],}
t|d|| | }|t||  | }qW | j||}||7 }qW |d9 }q\W ||krZx:t|	d|| | }	| jt||	 |}|dkr P q W |||  }}||k rz||fS ||fS )a  
        Factorizes the given large integer.

        Implementation from https://comeoncodeon.wordpress.com/2010/09/18/pollard-rho-brent-integer-factorization/.

        :param pq: the prime pair pq.
        :return: a tuple containing the two factors p and q.
           r      )r   rangepowminabsgcd)clsZpqycmgrqxZysikp r   A/tmp/pip-build-2nz6shyl/telethon/telethon/crypto/factorization.py	factorize   s4    
.

zFactorization.factorizec             C   s   x|r|| |  } }qW | S )z
        Calculates the Greatest Common Divisor.

        :param a: the first number.
        :param b: the second number.
        :return: GCD(a, b)
        r   )abr   r   r   r
   7   s    	zFactorization.gcdN)__name__
__module____qualname____doc__classmethodr   staticmethodr
   r   r   r   r   r      s   ,r   N)r   randomr   r   r   r   r   r   <module>   s   