Ë
    7^(hZ:  ã                   ó¸   — d dl mZmZ d dlmZ d dlmZmZmZm	Z
 d dlmZ d dlmZ d dlmZ  G d„ d«      Z G d	„ d
«      Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zdd„Zdd„Zy)é    )ÚexpÚlog)Ú_randint)Ú	bit_scan1ÚgcdÚinvertÚsqrt)Ú_perfect_power)Úisprime)Ú_sqrt_mod_prime_powerc                   ó   — e Zd Zd„ Zd„ Zd„ Zy)ÚSievePolynomialc                 óh   — || _         || _        |dz  | _        d|z  |z  | _        |dz  |z
  | _        y)a4  This class denotes the sieve polynomial.
        Provide methods to compute `(a*x + b)**2 - N` and
        `a*x + b` when given `x`.

        Parameters
        ==========

        a : parameter of the sieve polynomial
        b : parameter of the sieve polynomial
        N : number to be factored

        é   N)ÚaÚbÚa2ÚabÚb2)Úselfr   r   ÚNs       úN/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/ntheory/qs.pyÚ__init__zSievePolynomial.__init__
   s;   € ð ˆŒØˆŒØ�Q‘$ˆŒØ�A‘#�a‘%ˆŒØ�Q‘$˜‘(ˆ�ó    c                 ó:   — | j                   |z  | j                  z   S ©N)r   r   ©r   Úxs     r   Úeval_uzSievePolynomial.eval_u   s   € Ø�v‰v�a‰x˜$Ÿ&™&Ñ Ð r   c                 óZ   — | j                   |z  | j                  z   |z  | j                  z   S r   )r   r   r   r   s     r   Úeval_vzSievePolynomial.eval_v    s'   € Ø—‘˜‘	˜DŸG™GÑ# QÑ&¨¯©Ñ0Ð0r   N)Ú__name__Ú
__module__Ú__qualname__r   r   r!   © r   r   r   r   	   s   „ òò&!ó1r   r   c                   ó   — e Zd ZdZd„ Zy)ÚFactorBaseElemz7This class stores an element of the `factor_base`.
    c                 óX   — || _         || _        || _        d| _        d| _        d| _        y)zÿ
        Initialization of factor_base_elem.

        Parameters
        ==========

        prime : prime number of the factor_base
        tmem_p : Integer square root of x**2 = n mod prime
        log_p : Compute Natural Logarithm of the prime
        N)ÚprimeÚtmem_pÚlog_pÚsoln1Úsoln2Úb_ainv)r   r)   r*   r+   s       r   r   zFactorBaseElem.__init__'   s0   € ð ˆŒ
ØˆŒØˆŒ
ð ˆŒ
ØˆŒ
Øˆ�r   N)r"   r#   r$   Ú__doc__r   r%   r   r   r'   r'   $   s   „ ñór   r'   c                 ó\  — ddl m} g }d\  }}|j                  d| «      D ]†  }t        ||dz
  dz  |«      dk(  sŒ|dkD  r|€t	        |«      dz
  }|dkD  r|€t	        |«      dz
  }t        ||d«      d   }t        t        |«      dz  «      }|j                  t        |||«      «       Œˆ |||fS )	aç  Generate `factor_base` for Quadratic Sieve. The `factor_base`
    consists of all the points whose ``legendre_symbol(n, p) == 1``
    and ``p < num_primes``. Along with the prime `factor_base` also stores
    natural logarithm of prime and the residue n modulo p.
    It also returns the of primes numbers in the `factor_base` which are
    close to 1000 and 5000.

    Parameters
    ==========

    prime_bound : upper prime bound of the factor_base
    n : integer to be factored
    r   )Úsieve)NNé   r   iè  iˆ  é   )
Úsympy.ntheory.generater1   Ú
primerangeÚpowÚlenr   Úroundr   Úappendr'   )	Úprime_boundÚnr1   Úfactor_baseÚidx_1000Úidx_5000r)   Úresiduer+   s	            r   Ú_generate_factor_baser@   <   sÐ   € õ -Ø€KØ#Ñ€HˆhØ×!Ñ! ! [Ó1ò FˆÜˆq�5˜1‘9 Ñ" EÓ*¨aÓ/Ø�tŠ| Ð 0Ü˜{Ó+¨aÑ/�Ø�tŠ| Ð 0Ü˜{Ó+¨aÑ/�Ü+¨A¨u°aÓ8¸Ñ;ˆGÜœ#˜e›* UÑ*Ó+ˆEØ×Ñœ~¨e°W¸eÓDÕEðFð �X˜{Ð*Ð*r   c              #   ó  K  — t        d| z  «      dz  t        |«      z
  }|xs d}|xs t        |«      dz
  }	 d\  }	}
}t        d«      D ]¤  }d}g }t        |«      |k  rSd}|dk(  s||v r |||«      }|dk(  rŒ||v rŒ||   j                  }||z  }|j	                  |«       t        |«      |k  rŒSt        t        |«      |z
  «      }|�t        |dz
  «      t        |dz
  «      k  sŒŸ|}
|}	|}Œ¦ |	}|
}g }|D ]W  }||   j                  }||   j                  t        ||z  |«      z  |z  }d|z  |kD  r||z
  }|j	                  ||z  |z  «       ŒY t        |«      }t        ||| «      }|D ]£  }||j                  z  dk(  rd|_        Œt        ||j                  «      }|D �cg c]  }d|z  |z  |j                  z  ‘Œ c}|_        ||j                  |z
  z  |j                  z  |_        ||j                   |z
  z  |j                  z  |_        Œ¥ |–— t        ddt        |«      dz
  z  «      D ]É  }t        |«      }d||dz   z	  dz  z  dz
  }|j                  d|z  ||   z  z   }|j                   }t        ||| «      }|D ]q  }|j                  €Œ|j                  ||j                  |   z  z
  |j                  z  |_        |j                  ||j                  |   z  z
  |j                  z  |_        Œs |–— ŒË �ŒÇc c}w ­w)a6   Generate sieve polynomials indefinitely.
    Information such as `soln1` in the `factor_base` associated with
    the polynomial is modified in place.

    Parameters
    ==========

    N : Number to be factored
    M : sieve interval
    factor_base : factor_base primes
    idx_1000 : index of prime number in the factor_base near 1000
    idx_5000 : index of prime number in the factor_base near to 5000
    randint : A callable that takes two integers (a, b) and returns a random integer
              n such that a <= n <= b, similar to `random.randint`.
    r   r   r2   )NNNé2   N)r   r7   Úranger)   r9   r   Úabsr*   r   Úsumr   r,   r.   r-   r   r   r   )r   ÚMr<   r=   r>   ÚrandintÚ
approx_valÚstartÚendÚbest_aÚbest_qÚ
best_ratioÚ_r   ÚqÚrand_pÚpÚratioÚBÚvalÚq_lÚgammar   ÚgÚfbÚa_invÚb_elemÚiÚvÚneg_pows                                 r   Ú_generate_polynomialr^   Y   sF  è ø€ ô  �Q�q‘S“˜!‘œc !›fÑ$€JØŠM˜€EØ
Ò
,”s˜;Ó'¨!Ñ+€CØ
à%5Ñ"ˆ�˜
Ü�r“ò 	#ˆAØˆAØˆAÜ�a“&˜:Ò%Ø�Ø ’k V¨q¡[Ù$ U¨CÓ0�Fð  “k V¨q¢[à Ñ'×-Ñ-�Ø�Q‘�Ø—‘˜Ô ô �a“&˜:Ó%ô œ˜A› Ñ+Ó,ˆEØÐ!¤S¨°©£^´c¸*Àq¹.Ó6IÓ%IØ�Ø�Ø"‘
ð	#ð" ˆØˆØˆØò 	#ˆCØ˜cÑ"×(Ñ(ˆCØ Ñ$×+Ñ+¬f°Q¸#±X¸sÓ.CÑCÀcÑIˆEØ�‰w˜Š}Ø˜e™�Ø�H‰H�Q˜‘V˜E‘\Õ"ð	#ô �‹FˆÜ˜A˜q !Ó$ˆØò 	;ˆBØ�2—8‘8‰|˜qÒ Ø�”ØÜ˜1˜bŸh™hÓ'ˆEØABÖC°v˜˜6™ %™¨"¯(©(Ó2ÒCˆBŒIØ˜rŸy™y¨1™}Ñ-°·±Ñ9ˆBŒHØ §	¡	˜z¨A™~Ñ.°"·(±(Ñ:ˆB�Hð	;ð Šô �q˜!œc !›f Q™h™-Ó(ò 	ˆAÜ˜!“ˆAØ˜!  A¡™,¨!Ñ+Ñ,¨qÑ0ˆGØ—‘�a˜‘i  !¡‘nÑ$ˆAØ—‘ˆAÜ  1 aÓ(ˆAØ!ò H�Ø—8‘8Ð#ØØŸH™H w¨r¯y©y¸©|Ñ';Ñ;¸r¿x¹xÑG�”ØŸH™H w¨r¯y©y¸©|Ñ';Ñ;¸r¿x¹xÑG�•ð	Hð
 ‹Gð	ñU ùòH Dùs,   ‚A6LÁ9LÁ>3LÂ26LÃ)B;LÆ$L Ç ELc                 ó¦  — dgd| z  dz   z  }|D ]¿  }|j                   €Œt        | |j                   z   |j                  z  d| z  |j                  «      D ]  }||xx   |j                  z  cc<   Œ |j                  dk(  rŒpt        | |j                  z   |j                  z  d| z  |j                  «      D ]  }||xx   |j                  z  cc<   Œ ŒÁ |S )a¨  Sieve Stage of the Quadratic Sieve. For every prime in the factor_base
    that does not divide the coefficient `a` we add log_p over the sieve_array
    such that ``-M <= soln1 + i*p <=  M`` and ``-M <= soln2 + i*p <=  M`` where `i`
    is an integer. When p = 2 then log_p is only added using
    ``-M <= soln1 + i*p <=  M``.

    Parameters
    ==========

    M : sieve interval
    factor_base : factor_base primes
    r   r   r2   )r,   rC   r)   r+   r-   )rF   r<   Úsieve_arrayÚfactorÚidxs        r   Ú_gen_sieve_arrayrc   ¤   sÖ   € ð �#�q˜‘s˜Q‘w‘-€KØò 	-ˆØ�<‰<ÐØÜ˜!˜fŸl™lÑ*¨f¯l©lÑ:¸A¸a¹CÀÇÁÓNò 	-ˆCØ˜Ó §¡Ñ,Ôð	-à�<‰<˜1ÒØä˜!˜fŸl™lÑ*¨f¯l©lÑ:¸A¸a¹CÀÇÁÓNò 	-ˆCØ˜Ó §¡Ñ,Ôñ	-ð	-ð Ðr   c                 ó   — | dk  r| dz  } d}nd}t        |d«      D ]m  \  }}| |j                  z  rŒd}| |j                  z  } | |j                  z  dk(  r'|dz  }| |j                  z  } | |j                  z  dk(  rŒ'|dz  sŒf|d|z  z  }Œo || fS )zê Check if `num` is smooth with respect to the given `factor_base`
    and compute its factorization vector.

    Parameters
    ==========

    num : integer whose smootheness is to be checked
    factor_base : factor_base primes
    r   éÿÿÿÿr2   r   )Ú	enumerater)   )Únumr<   Úvecr[   rX   Úes         r   Ú_check_smoothnessrj   ¿   s³   € ð ˆQ‚wØˆr‰	ˆØ‰àˆÜ˜;¨Ó*ò 	‰ˆˆ2Ø�—‘Š>ØØˆØ�—‘ÑˆØ�B—H‘H‰n Ò!Ø�‰FˆAØ�B—H‘HÑˆCð �B—H‘H‰n Ó!ð ˆq‹5Ø�1˜‘6‰M‰Cð	ð �ˆ8€Or   c                 ó~  — t        |«      t        | «      dz  z   |z
  dz  }g }t        «       }	d|d   j                  z  }
t        || «      D ]ì  \  }}||k  rŒ|j	                  |«      }t        ||«      \  }}|dk(  r$|j                  |j                  |«      ||f«       ŒU||
k  sŒ[t        |«      sŒg| |z  dk(  r|	j                  |«       Œ�|j                  |«      }||v rO|j                  |«      \  }}}||z  t        || «      z  | z  }||z  |dz  z  }||z  }|j                  |||f«       Œå|||f||<   Œî ||	fS )a)  Trial division stage. Here we trial divide the values generetated
    by sieve_poly in the sieve interval and if it is a smooth number then
    it is stored in `smooth_relations`. Moreover, if we find two partial relations
    with same large prime then they are combined to form a smooth relation.
    First we iterate over sieve array and look for values which are greater
    than accumulated_val, as these values have a high chance of being smooth
    number. Then using these values we find smooth relations.
    In general, let ``t**2 = u*p modN`` and ``r**2 = v*p modN`` be two partial relations
    with the same large prime p. Then they can be combined ``(t*r/p)**2 = u*v modN``
    to form a smooth relation.

    Parameters
    ==========

    N : Number to be factored
    M : sieve interval
    factor_base : factor_base primes
    sieve_array : stores log_p values
    sieve_poly : polynomial from which we find smooth relations
    partial_relations : stores partial relations with one large prime
    ERROR_TERM : error term for accumulated_val
    r   r3   é€   re   r2   r   )r   Úsetr)   rf   r!   rj   r9   r   r   ÚaddÚpopr   )r   rF   r<   r`   Ú
sieve_polyÚpartial_relationsÚ
ERROR_TERMÚaccumulated_valÚsmooth_relationsÚproper_factorÚpartial_relation_upper_boundr   rT   r\   rh   rg   ÚuÚu_prevÚv_prevÚvec_prevs                       r   Ú_trial_division_stager{   Û   su  € ô. ˜1“v¤ A£ q¡Ñ(¨:Ñ5¸Ñ>€OØÐÜ“E€MØ#& {°2¡×'<Ñ'<Ñ#<Ð Ü˜K¨!¨Ó,ò 5‰ˆˆ3Ø�Ò ØØ×Ñ˜aÓ ˆÜ$ Q¨Ó4‰ˆˆSØ�!Š8Ø×#Ñ# Z×%6Ñ%6°qÓ%9¸1¸cÐ$BÕCØÐ/Ó/´G¸CµLØ�3‰w˜!Š|Ø×!Ñ! #Ô&ØØ×!Ñ! !Ó$ˆAØÐ'Ñ'Ø+<×+@Ñ+@ÀÓ+EÑ(�˜ Ø�f‘HœV C¨›^Ñ+¨aÑ/�Ø�f‘H  Q¡Ñ&�Ø�x‘�Ø ×'Ñ'¨¨A¨s¨Õ4à*+¨Q°¨Ð! #Ò&ð'5ð( ˜]Ð*Ð*r   c              #   ó4  K  — |D �cg c]  }|d   ‘Œ	 }}t        |«      }dg|z  }t        |«      D ]_  }d|z  }t        |«      D ]J  }	||	   |z  x}
sŒ|
||	   z  }|||	<   d||	<   t        |	dz   |«      D ]  }||   |z  sŒ||xx   |z  cc<   Œ  Œ_ Œa t        |||«      D ]o  \  }}}|rŒ
|d   |d   }}t        |||«      D ]  \  }}}|sŒ
||z  sŒ||d   z  }||d   z  }Œ! t        |«      }dt	        ||z
  | «      x}cxk  r| k  sŒin Œl|–— Œq yc c}w ­w)a˜   Finds proper factor of N using fast gaussian reduction for modulo 2 matrix.

    Parameters
    ==========

    N : Number to be factored
    smooth_relations : Smooth relations vectors matrix
    col : Number of columns in the matrix

    Reference
    ==========

    .. [1] A fast algorithm for gaussian elimination over GF(2) and
    its implementation on the GAPP. Cetin K.Koc, Sarath N.Arachchige
    r   Fr2   Tr   N)r7   rC   ÚzipÚisqrtr   )r   rt   ÚcolÚ
s_relationÚmatrixÚrowÚmarkÚposÚmr[   rQ   Úadd_colÚjÚmatÚrelrw   r\   Úm1Úmat1Úrel1rW   s                        r   Ú_find_factorr�     sx  è ø€ ð  /?Ö? 
ˆj˜‹mÐ?€FÐ?Ü
ˆf‹+€CØˆ7�S‰=€DÜ�S‹zò 
ˆØ�‰HˆÜ�s“ò 	ˆAØ˜1‘I ‘MÐ!ˆqÑ!Ø˜f Q™i™-�Ø��q‘	Ø��Q‘Ü˜q 1™u cÓ*ò -�AØ˜a‘y 1“}Ø˜q›	 WÑ,œ	ð-ñ ñ	ð
ô ˜4 Ð)9Ó:ò ‰ˆˆ3�ÙØØ�1‰v�s˜1‘vˆ1ˆÜ! $¨Ð0@ÓAò 	‰NˆB��dÚ�c˜D“jØ�T˜!‘W‘�Ø�T˜!‘W‘‘ð	ô
 �!‹HˆØ”S˜˜Q™ “]Ð"�Ô' aÖ'Ø‹Gñùò @ùs.   ‚D‡D“>DÁ,DÁ?ADÃDÃ7DÄ	Dc           	      ó2   — t        t        | ||||«      «      S )aœ  Performs factorization using Self-Initializing Quadratic Sieve.
    In SIQS, let N be a number to be factored, and this N should not be a
    perfect power. If we find two integers such that ``X**2 = Y**2 modN`` and
    ``X != +-Y modN``, then `gcd(X + Y, N)` will reveal a proper factor of N.
    In order to find these integers X and Y we try to find relations of form
    t**2 = u modN where u is a product of small primes. If we have enough of
    these relations then we can form ``(t1*t2...ti)**2 = u1*u2...ui modN`` such that
    the right hand side is a square, thus we found a relation of ``X**2 = Y**2 modN``.

    Here, several optimizations are done like using multiple polynomials for
    sieving, fast changing between polynomials and using partial relations.
    The use of partial relations can speeds up the factoring by 2 times.

    Parameters
    ==========

    N : Number to be Factored
    prime_bound : upper bound for primes in the factor base
    M : Sieve Interval
    ERROR_TERM : Error term for checking smoothness
    seed : seed of random number generator

    Returns
    =======

    set(int) : A set of factors of N without considering multiplicity.
               Returns ``{N}`` if factorization fails.

    Examples
    ========

    >>> from sympy.ntheory import qs
    >>> qs(25645121643901801, 2000, 10000)
    {5394769, 4753701529}
    >>> qs(9804659461513846513, 2000, 10000)
    {4641991, 2112166839943}

    See Also
    ========

    qs_factor

    References
    ==========

    .. [1] https://pdfs.semanticscholar.org/5c52/8a975c1405bd35c65993abf5a4edb667c1db.pdf
    .. [2] https://www.rieselprime.de/ziki/Self-initializing_quadratic_sieve
    )rm   Ú	qs_factor)r   r:   rF   rr   Úseeds        r   Úqsr‘   :  s   € ôb Œy˜˜K¨¨J¸Ó=Ó>Ð>r   c           
      ó  — | dk  rt        d«      ‚i }g }i }| dz  dk(  r'd}| dz  } | dz  dk(  r| dz  } |dz  }| dz  dk(  rŒ||d<   t        | «      rd|| <   |S t        | d«      x}	r|	\  }
}|||
<   |S | }t        |«      }t	        || «      \  }}}t        |«      dz  dz  }t        | |||||«      D ]k  }t        ||«      }t        | ||||||«      \  }}||z  }|D ]/  }||z  rŒ	d}||z  }||z  dk(  r||z  }|dz  }||z  dk(  rŒ|||<   Œ1 |t        |«      k  sŒk n t        | |t        |«      dz   «      D ]D  }||z  dk(  sŒd}||z  }||z  dk(  r||z  }|dz  }||z  dk(  rŒ|||<   |dk(  st        |«      sŒD n |dk7  rd||<   |S )aª   Performs factorization using Self-Initializing Quadratic Sieve.

    Parameters
    ==========

    N : Number to be Factored
    prime_bound : upper bound for primes in the factor base
    M : Sieve Interval
    ERROR_TERM : Error term for checking smoothness
    seed : seed of random number generator

    Returns
    =======

    dict[int, int] : Factors of N.
                     Returns ``{N: 1}`` if factorization fails.
                     Note that the key is not always a prime number.

    Examples
    ========

    >>> from sympy.ntheory import qs_factor
    >>> qs_factor(1009 * 100003, 2000, 10000)
    {1009: 1, 100003: 1}

    See Also
    ========

    qs

    r   zN should be greater than 1r   r2   é   éi   éd   )
Ú
ValueErrorr   r
   r   r@   r7   r^   rc   r{   r�   )r   r:   rF   rr   r�   Úfactorsrt   rq   ri   Úresultr;   ÚN_copyrG   r=   r>   r<   Ú	thresholdrW   r`   Ús_relÚp_frQ   ra   s                          r   r�   r�   n  sK  € ð@ 	ˆ1‚uÜÐ5Ó6Ð6Ø€GØÐØÐð 	ˆ1�u�‚zØˆØ	ˆa‰ˆØ�!‰e�qŠjØ�!‰GˆAØ�‰FˆAð �!‰e�q‹jð ˆ�‰
Üˆq„zØˆ�‰
ØˆÜ  1Ó%Ð%€vÐ%Ø‰ˆˆ1Øˆ�‰
ØˆØ€FÜ�t‹n€GÜ&;¸KÈÓ&KÑ#€Hˆh˜Ü�KÓ  3Ñ&¨Ñ+€IÜ! ! Q¨°X¸xÈÓQò ˆÜ& q¨+Ó6ˆÜ*¨1¨a°¸kÈ1ÐN_ÐakÓl‰
ˆˆsØ˜EÑ!ÐØò 	ˆAØ˜ŠzØØˆAØ�q‰LˆFØ˜1‘* ’/Ø˜1‘�Ø�Q‘�ð ˜1‘* “/ð ˆG�AŠJð	ð œÐ,Ó-Ó-Ùðô  ˜qÐ"2´C¸Ó4DÀqÑ4HÓIò 	ˆØ�F‰?˜aÓØˆAØ�vÑˆFØ˜6‘/ QÒ&Ø˜6Ñ!�Ø�Q‘�ð ˜6‘/ QÓ&ð  ˆG�F‰OØ˜Š{œg f�oÙð	ð �‚{Øˆ�‰Ø€Nr   N)é   iÒ  )Úmathr   r   Úsympy.core.randomr   Úsympy.external.gmpyr   r   r   r	   r~   Úsympy.ntheory.factor_r
   Úsympy.ntheory.primetestr   Úsympy.ntheory.residue_ntheoryr   r   r'   r@   r^   rc   rj   r{   r�   r‘   r�   r%   r   r   ú<module>r¤      s\   ðß Ý &ß EÓ EÝ 0Ý +Ý ?÷1ñ 1÷6ñ ò0+ò:HòVò6ò8/+òd*óZ1?ôhUr   