
    wgG                         d dl mZ d dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZmZ  G d d      Z G d d	      Zd
 ZddZd Zd Zd Zd Zd Zd Zd ZddZy)    )_randint)gcdinvertsqrt)_sqrt_mod_prime_power)isprime)logr   c                       e Zd ZddZd Zy)SievePolynomialNc                 .    || _         || _        || _        y)a  This class denotes the seive polynomial.
        If ``g(x) = (a*x + b)**2 - N``. `g(x)` can be expanded
        to ``a*x**2 + 2*a*b*x + b**2 - N``, so the coefficient
        is stored in the form `[a**2, 2*a*b, b**2 - N]`. This
        ensures faster `eval` method because we dont have to
        perform `a**2, 2*a*b, b**2` every time we call the
        `eval` method. As multiplication is more expensive
        than addition, by using modified_coefficient we get
        a faster seiving process.

        Parameters
        ==========

        modified_coeff : modified_coefficient of sieve polynomial
        a : parameter of the sieve polynomial
        b : parameter of the sieve polynomial
        N)modified_coeffab)selfr   r   r   s       U/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/ntheory/qs.py__init__zSievePolynomial.__init__	   s    $ -    c                 @    d}| j                   D ]  }||z  }||z  } |S )z
        Compute the value of the sieve polynomial at point x.

        Parameters
        ==========

        x : Integer parameter for sieve polynomial
        r   )r   )r   xanscoeffs       r   evalzSievePolynomial.eval   s7     (( 	E1HC5LC	 
r   ) NN)__name__
__module____qualname__r   r   r   r   r   r   r      s    ,r   r   c                       e Zd ZdZd Zy)FactorBaseElemz7This class stores an element of the `factor_base`.
    c                 f    || _         || _        || _        d| _        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a_invb_ainv)r   r    r!   r"   s       r   r   zFactorBaseElem.__init__2   s5     




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      i  i     )
sympy.ntheory.generater)   
primerangepowlenr   roundr	   appendr   )	prime_boundnr)   factor_baseidx_1000idx_5000r    residuer"   s	            r   _generate_factor_baser:   F   s     -K#Hh!!![1 Fq519"E*a/t| 0{+a/t| 0{+a/+Aua8;G#e*U*+E~eWeDEF X{**r   Nc                 D   t        |      }t        d| z        |z  }d\  }}	}
|dn|}|t        |      dz
  n|}t        d      D ]  }d}g }||k  rJd}|dk(  s||v r |||      }|dk(  r||v r||   j                  }||z  }|j                  |       ||k  rJ||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        ||z  d|z  |z  ||z  | z
  g||      }|D ]  }||j                  z  dk(  rt        ||j                        |_        |D cg c]!  }d|z  |j                  z  |j                  z  # c}|_        |j                  |j                  |z
  z  |j                  z  |_        |j                  |j                   |z
  z  |j                  z  |_         ||fS c c}w )ag  This step is the initialization of the 1st sieve polynomial.
    Here `a` is selected as a product of several primes of the factor_base
    such that `a` is about to ``sqrt(2*N) / M``. Other initial values of
    factor_base elem are also initialized which includes a_inv, b_ainv, soln1,
    soln2 which are used when the sieve polynomial is changed. The b_ainv
    is required for fast polynomial change as we do not have to calculate
    `2*b*invert(a, prime)` every time.
    We also ensure that the `factor_base` primes which make `a` are between
    1000 and 5000.

    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
    seed : Generate pseudoprime numbers
    r,   )NNNr   r+   2   )r   r   r1   ranger    r3   absr!   r   sumr   r%   r&   r#   r$   )NMr6   r7   r8   seedrandint
approx_valbest_abest_q
best_ratiostartend_r   qrand_ppratioBvalq_lgammar   gfbb_elems                              r   _initialize_first_polynomialrV   c   sg   * tnGacQJ
 "2FFJ!AxE"*"2#k
Q
C2Y *nFA+1 , A+1F#))AFAHHV *n JUQY#j1n2E!EFFJ  	AA
A #$$C ''&c3*??#E37?%KE	C 	AA1ac!eQqS1W-q!4A :rxx<1!RXX&@ABfQvXbhh&1B	HHbii!m,8HHryyj1n-9: a4K Cs   &Hc                    ddl m} d}|}|dz  dk(  r|dz  }|dz  }|dz  dk(  r ||d|z  z        dz  dk(  rd}nd}|j                  d|z  ||dz
     z  z   }	|j                  }
t	        |
|
z  d|
z  |	z  |	|	z  | z
  g|
|	      }|D ]}  }|
|j
                  z  dk(  r|j                  ||j                  |dz
     z  z
  |j
                  z  |_        |j                  ||j                  |dz
     z  z
  |j
                  z  |_         |S )a  Initialization stage of ith poly. After we finish sieving 1`st polynomial
    here we quickly change to the next polynomial from which we will again
    start sieving. Suppose we generated ith sieve polynomial and now we
    want to generate (i + 1)th polynomial, where ``1 <= i <= 2**(j - 1) - 1``
    where `j` is the number of prime factors of the coefficient `a`
    then this function can be used to go to the next polynomial. If
    ``i = 2**(j - 1) - 1`` then go to _initialize_first_polynomial stage.

    Parameters
    ==========

    N : number to be factored
    factor_base : factor_base primes
    i : integer denoting ith polynomial
    g : (i - 1)th polynomial
    B : array that stores a//q_l*gamma
    r   )ceilingr+   r,   )	#sympy.functions.elementary.integersrX   r   r   r   r    r#   r&   r$   )r@   r6   irS   rO   rX   vjneg_powr   r   rT   s               r   _initialize_ith_polyr_      s3   $ <	A	A
a%1*	Q	a a%1* qAqDzQ!#	ai!a%  A	A1ac!eQqS1W-q!4A Drxx<1HHwryyQ'777288CHHwryyQ'777288C	D Hr   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,   r+   )r#   r=   r    r"   r$   )rA   r6   sieve_arrayfactoridxs        r   _gen_sieve_arrayrd      s     #qsQw-K 	-<<!fll*fll:AaCN 	-C,	-<<1!fll*fll:AaCN 	-C,	-	- r   c                    g }| dk  r|j                  d       | dz  } n|j                  d       |D ]u  }| |j                  z  dk7  r|j                  d       'd}| |j                  z  dk(  r'|dz  }| |j                  z  } | |j                  z  dk(  r'|j                  |dz         w | dk(  r|dfS t        |       r| dfS y)ab  Here we check that if `num` is a smooth number or not. If `a` is a smooth
    number then it returns a vector of prime exponents modulo 2. For example
    if a = 2 * 5**2 * 7**3 and the factor base contains {2, 3, 5, 7} then
    `a` is a smooth number and this function returns ([1, 0, 0, 1], True). If
    `a` is a partial relation which means that `a` a has one prime factor
    greater than the `factor_base` then it returns `(a, False)` which denotes `a`
    is a partial relation.

    Parameters
    ==========

    a : integer whose smootheness is to be checked
    factor_base : factor_base primes
    r   r+   rY   r,   TFr*   )r3   r    r   )numr6   vecrb   
factor_exps        r   _check_smoothnessri      s     C
Qw

1r	

1 #"JJqM
FLL A%!OJFLL C FLL A% 	

:>"# axDys|Ezr   c                 f   t        |       }t        ||z        dz  |z
  }g }	t               }
d|d   j                  z  }t	        |      D ]  \  }}||k  r||z
  }|j                  |      }t        ||      \  }}|4|j                  |z  |j                  z   }|du r\|}||kD  r\||vr||f||<   h||   \  }}|j                  |       	 t        ||       }||z  |z  }||z  ||z  z  }t        ||      \  }}|	j                  |||f        |	|
fS # t        $ r |
j                  |       Y w xY w)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-      rY   F)isqrtr	   setr    	enumerater   ri   r   r   popr   ZeroDivisionErroraddr3   )r@   rA   r6   ra   
sieve_polypartial_relations
ERROR_TERMsqrt_naccumulated_valsmooth_relationsproper_factorpartial_relation_upper_boundrc   rP   r   r\   rg   	is_smoothularge_primeu_prevv_prevlarge_prime_invs                          r   _trial_division_stager     s   . 1XF!f*oe+j8OEM#&{2'<'<#< k* -S !GOOA*1k:YLLNZ\\) K99"3323Q!+.!2;!?!%%k2&,[!&<O fH_,fH[!89!21k!BYAs,?-@ ]** ) !%%k2s   DD0/D0c                 @    g }| D ]  }|j                  |d           |S )z|Build a 2D matrix from smooth relations.

    Parameters
    ==========

    smooth_relations : Stores smooth relations
    r,   )r3   )rw   matrix
s_relations      r   _build_matrixr   R  s-     F& %
jm$%Mr   c                    ddl }|j                  |       }t        |      }t        |d         }dg|z  }t        |      D ]p  }t        |      D ]  }||   |   dk(  s n d|<   t        |      D ]>  }||k(  r	||   |   dk(  st        |      D ]  }	||	   |   ||	   |   z   dz  ||	   |<    @ r g }
t	        |      D ]!  \  }}|dk(  s|
j                  ||   |g       # |
||fS )a  Fast gaussian reduction for modulo 2 matrix.

    Parameters
    ==========

    A : Matrix

    Examples
    ========

    >>> from sympy.ntheory.qs import _gauss_mod_2
    >>> _gauss_mod_2([[0, 1, 1], [1, 0, 1], [0, 1, 0], [1, 1, 1]])
    ([[[1, 0, 1], 3]],
     [True, True, True, False],
     [[0, 1, 0], [1, 0, 0], [0, 0, 1], [1, 0, 1]])

    Reference
    ==========

    .. [1] A fast algorithm for gaussian elimination over GF(2) and
    its implementation on the GAPP. Cetin K.Koc, Sarath N.Arachchiger   NFr+   Tr,   )copydeepcopyr1   r=   rn   r3   )Ar   r   rowcolmarkcrc1r2dependent_rowrc   rP   s                r   _gauss_mod_2r   `  s2   , ]]1F
f+C
fQi.C73;D3Z 
Js 	Aay|q 	 Q* 	JBQway}!* JB&,Rjnvbz!}&D%IF2JrNJ		J
J MdO 5S%<  &+s!345 $&&r   c                    | |   d   }||   d   g}||   d   g}| |   d   }	t        |	      D ]h  \  }
}|dk(  st        t        |            D ]F  }||   |
   dk(  s||   dk(  s|j                  ||   d          |j                  ||   d           h j d}d}|D ]  }||z  }	 |D ]  }||z  }	 t	        |      }t        ||z
  |      S )a  Finds proper factor of N. Here, transform the dependent rows as a
    combination of independent rows of the gauss_matrix to form the desired
    relation of the form ``X**2 = Y**2 modN``. After obtaining the desired relation
    we obtain a proper factor of N by `gcd(X - Y, N)`.

    Parameters
    ==========

    dependent_rows : denoted dependent rows in the reduced matrix form
    mark : boolean array to denoted dependent and independent rows
    gauss_matrix : Reduced form of the smooth relations matrix
    index : denoted the index of the dependent_rows
    smooth_relations : Smooth relations vectors matrix
    N : Number to be factored
    r+   r   T)rn   r=   r1   r3   rl   r   )dependent_rowsr   gauss_matrixindexrw   r@   idx_in_smoothindependent_uindependent_vdept_rowrc   rP   r   r{   r\   r[   s                   r   _find_factorr     s'     #5)!,M%m4Q78M%m4Q78Me$Q'Hh' S!8S./ $S)Q.493D!(()9#)>q)AB!(()9#)>q)AB	 	
A	A 	Q 	Q 	aAq1ua=r   c           	         |dz  }t        ||       \  }}}g }d}	i }
t               }dt        |      z  dz  }	 |	dk(  rt        | ||||      \  }}nt	        | ||	      }|	dz  }	|	dt        |      dz
  z  k\  rd}	t        ||      }t        | |||||
|      \  }}||z  }||z  }t        |      t        |      |z   k\  rnt        |      }t        |      \  }}}| }t        t        |            D ]l  }t        ||||||       }|dkD  s|| k  s|j                  |       ||z  dk(  r||z  }||z  dk(  rt        |      r|j                  |        |S |dk(  sk |S  |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
    threshold : Extra smooth relations for factorization
    seed : generate pseudo prime numbers

    Examples
    ========

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

    References
    ==========

    .. [1] https://pdfs.semanticscholar.org/5c52/8a975c1405bd35c65993abf5a4edb667c1db.pdf
    .. [2] https://www.rieselprime.de/ziki/Self-initializing_quadratic_sieve
    r-   r      d   r+   r,   )r:   rm   r1   rV   r_   rd   r   r   r   r=   r   rq   r   )r@   r4   rA   rt   rB   r7   r8   r6   rw   ith_polyrs   rx   	thresholdith_sieve_polyB_arrayra   s_relp_fr   r   r   r   N_copyr   rb   s                            r   qsr     s   N J&;K&K#HhHEM#k""c)I
q=&B1aV^`h&i#NG1![(N\cdNAq3w<!+,,H&q+6*1ak>[lnxy
sE! C$4y$@@  +,F(4V(<%M4Fs=)* 
mT<HXZ[\A:&1*f%6/Q&6! 6/Q&v!!&)  {
 r   )N)   i  )sympy.core.randomr   sympy.external.gmpyr   r   r   rl   sympy.ntheory.residue_ntheoryr   sympy.ntheoryr   mathr	   r   r   r:   rV   r_   rd   ri   r   r   r   r   r   r   r   r   <module>r      sf    & : : ? ! $ $N .+:CL%P6#L<+@*'Z%PJr   