o
    6dc-                  	   @   s  d dl Z d dlmZ zd dlZW n ey   d dlZY nw d dlmZ d dl	Z	d dl
mZmZmZ z
d dl
mZ dZW n eyG   dZY nw ddlmZmZmZmZmZmZmZmZmZmZmZ d	Ze	jd
dd eedd edd D dd Z e	jdg ddd Z!e	jdedd Z"dd Z#dd Z$dd Z%dd Z&G dd  d ej'Z(ej)d!d" Z*ej)d#d$ Z+ej)d%d& Z,ej)d'd( Z-ej)d)d* Z.i Z/erej0ej1ge/d+< d,e/d-< e2e/Z3d.e3d/< G d0d1 d1ej'Z4G d2d3 d3ej'Z5dS )4    N)reduce)givensettingsexample)HealthCheckTF   )SquareRootErrorJacobiErrorfactorizationgcdlcmjacobiinverse_modis_prime
next_primesmallprimessquare_root_mod_prime)i@ iA i)A i/A iEA iSA iYA i]A iA iA iA iA iA iA iA iB iB iB i+B i/B zprime, next_pc                 C   s   g | ]\}}||fqS  r   ).0pqr   r   nC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\ecdsa/test_numbertheory.py
<listcomp>:       r   c                 C   s   t | |ksJ d S Nr   )primeZnext_pr   r   r   test_next_prime9   s   r   val)r   r   r   c                 C   s   t | dksJ d S N   r   )r   r   r   r    test_next_prime_with_nums_less_2@   s   r"   r   c              	   C   s   t  }tdd| d  D ]}|| |  }|| t|| }|| |  |ks(J qtd| D ]!}||v r5q.tt t||  W d    n1 sJw   Y  q.d S )Nr   r   r!   )setrangeaddr   pytestZraisesr   )r   squaresnumsqrootZ	nonsquarer   r   r   +test_square_root_mod_prime_for_small_primesE   s   

r+   c                  C   s   t dd} | dksJ d S )Nr   r!   r   )ar   r   r    test_square_root_mod_prime_for_2V   s   
r.   c                  C   s"   t dd} | |  d dksJ d S )N	   e   r,   )r*   r   r   r   *test_square_root_mod_prime_for_small_prime[   s   
r1   c                  C   6   d} | d dks
J t d| }|| |  dksJ d S )N            r,   r   r*   r   r   r   ,test_square_root_mod_prime_for_p_congruent_5`      
r8   c                  C   r2   )N   r4   r5      r,   r7   r   r   r   4test_square_root_mod_prime_for_p_congruent_5_large_dh   r9   r<   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestSquareRootModPrimec                 C   s:   |  t tdd W d    d S 1 sw   Y  d S )N       )assertRaisesr	   r   selfr   r   r   test_power_of_2_pq   s   "z(TestSquareRootModPrime.test_power_of_2_pc                 C   J   |  t}tdd W d    n1 sw   Y  | dt|j d S )Nr>      zno square rootr@   r   r   ZassertInstr	exceptionrB   er   r   r   test_no_squareu      z%TestSquareRootModPrime.test_no_squarec                 C   rD   )Nr>   !   p is not primerF   rI   r   r   r   test_non_prime{   rL   z%TestSquareRootModPrime.test_non_primec                 C   rD   )Ni  i  rN   rF   rI   r   r   r   test_non_prime_with_negative   rL   z3TestSquareRootModPrime.test_non_prime_with_negativeN)__name__
__module____qualname__rC   rK   rO   rP   r   r   r   r   r=   p   s
    r=   c                    sB   | t jddd d | t jd d d fdd}| fS )Nr!      	min_value	max_valuer   c                    s   t |  dkS Nr   r   xmodr   r   <lambda>   s    z'st_two_nums_rel_prime.<locals>.<lambda>)stintegersfilter)drawr(   r   r\   r   st_two_nums_rel_prime   s   
rc   c                 O   s8   d|vrd|d< | t tt j|i |tB }|S )NrV   r   )r_   sampled_fromr   r`   ra   r   )rb   argskwargsr   r   r   r   	st_primes   s   rg   c                 C   s@   | t dd d}| tjdd|d  d}|| | }||fS )Nr!   rT   rW   r   r   rU   )rg   r_   r`   )rb   r   r(   r)   r   r   r   st_num_square_prime   s   ri   c                    sx   | t jtdd dddd| t jt ddd}ttj|d | t jdddfd	d
} fdd|D S )zM
    Strategy that returns lists of numbers, all having a common factor.
    r!      rh   r   
   min_sizemax_size   rU   c                        t jt jt  ddd| dS N   )rn   r   rl   r_   listsrd   n)primesr   r   r^      
    z&st_comp_with_com_fac.<locals>.<lambda>c                    s   g | ]}t tj|d   qS r   r   operatormulr   nums)com_facr   r   r      s    z(st_comp_with_com_fac.<locals>.<listcomp>)	r_   rt   rg   rd   r   r{   r|   r`   flatmap)rb   Zcom_fac_primesZcomp_primesr   )r   rw   r   st_comp_with_com_fac   s   

r   c                    s   | t jtdd ddddd}| t jt |dt|d ddttjd}fdd	|D   s6J s:J | t jdd
d	 fdd}dd	 |D }| t jdt|d}|
|| |S )zQ
    Strategy that returns lists of numbers that don't have a common factor.
    r!   rj   rh   rk   T)rm   rn   uniquer   c                    s   g | ]}| vr|qS r   r   )r   i)uncom_fac_primesr   r   r      r   z&st_comp_no_com_fac.<locals>.<listcomp>ro   rU   c                    rp   rq   rs   ru   )leftover_primesr   r   r^      rx   z$st_comp_no_com_fac.<locals>.<lambda>c                 S   s   g | ]	}t tj|d qS ry   rz   r}   r   r   r   r      s    r   )r_   rt   rg   rd   lenr   r{   r|   r`   r   insert)rb   rw   Z	uncom_facZnumber_primesnumbersZ	insert_atr   )r   r   r   st_comp_no_com_fac   s4   


r   Zsuppress_health_checki  deadlinerk   Zmax_examplesc                   @   sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )TestIsPrimec                 C      t dsJ d S )N   r   rA   r   r   r   test_very_small_prime     z!TestIsPrime.test_very_small_primec                 C      t drJ d S )N   r   rA   r   r   r   test_very_small_composite  r   z%TestIsPrime.test_very_small_compositec                 C   r   )Ni[r   rA   r   r   r   test_small_prime
  r   zTestIsPrime.test_small_primec                 C   r   )Ni(  r   rA   r   r   r   test_special_composite  r   z"TestIsPrime.test_special_compositec                 C      t dd d s
J d S )Nr!      i)  r   rA   r   r   r   test_medium_prime_1     zTestIsPrime.test_medium_prime_1c                 C   r   )Nr!   r   i-  r   rA   r   r   r   test_medium_prime_2  r   zTestIsPrime.test_medium_prime_2c                 C      t dd d r
J d S )Nr!   r   i0  r   rA   r   r   r   test_medium_trivial_composite     z)TestIsPrime.test_medium_trivial_compositec                 C   r   )Nr!   r   i/  r   rA   r   r   r   !test_medium_non_trivial_composite  r   z-TestIsPrime.test_medium_non_trivial_compositec                 C   r   )Nr!   i   i  r   rA   r   r   r   test_large_prime  r   zTestIsPrime.test_large_primeN)rQ   rR   rS   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    r   c                   @   s  e Zd Zdd Zeeded.i ee	e
 dd Zeeded.i ee	e dd Ze	ejejdd	d
 dddddd Zdd Ze	ejejdd	d
 dddddd Zeeded.i ee	e dd Zed.i ee	ejdddededdd Zdd Zdd Zdd  Zd!d" Zed.i ee	ejd#d$dd%d& d'd( Ze	e d)d* Z d+d, Z!d-S )/TestNumbertheoryc                 C   s<   t ddddks
J t g ddksJ t ddksJ d S )Ni            )r   r   r   r6   rY   rA   r   r   r   test_gcd$     zTestNumbertheory.test_gcdzwHypothesis 2.0.0 can't be made tolerant of hard to meet requirements (like `is_prime()`), the test case times-out on itc                 C   s:   t |}d|v s|dksJ |D ]
}|| dksJ qd S Nr   r   rY   rB   r   rv   r   r   r   r   test_gcd_with_com_factor)  s
   	z)TestNumbertheory.test_gcd_with_com_factorc                 C   s   t |}|dks
J d S rX   rY   )rB   r   rv   r   r   r   test_gcd_with_uncom_factor7  s   	z+TestNumbertheory.test_gcd_with_uncom_factorr   r!   i    rU   ro   rl   c                 C   s&   t |}|D ]
}|| dksJ qd S Nr   rY   r   r   r   r   test_gcd_with_random_numbersC  s   z-TestNumbertheory.test_gcd_with_random_numbersc                 C   s<   t ddddks
J t g ddksJ t ddksJ d S )Nr6   r      r   )r6   r   r   r   rA   r   r   r   test_lcmP  r   zTestNumbertheory.test_lcmc                 C   s&   t |}|D ]
}|| dksJ qd S r   r   r   r   r   r   test_lcm_with_random_numbersU  s   z-TestNumbertheory.test_lcm_with_random_numbersc                 C   s*   |\}}t ||}|| | |ksJ d S r   r,   )rB   valsZsquarer   calcr   r   r   test_square_root_mod_primea  s   	
z+TestNumbertheory.test_square_root_mod_primel    J)l   5yl   1n
Yc                 C   s:   t |}d}|D ]}||d |d  9 }q||ksJ d S r   )r
   )rB   r(   factorsmultr   r   r   r   test_factorizationo  s
   z#TestNumbertheory.test_factorizationc                 C   sV   d}dt v sJ dt v sJ t|}d}|D ]}||d |d  9 }q||ks)J d S )Ni(  r0   g   r   r   r   r
   rB   expr   r   r   r   r   r   test_factorisation_smallprimesz     z/TestNumbertheory.test_factorisation_smallprimesc                 C   sV   d}dt vsJ dt vsJ t|}d}|D ]}||d |d  9 }q||ks)J d S )Ni;< i  i  r   r   r   r   r   r   r   "test_factorisation_not_smallprimes  r   z3TestNumbertheory.test_factorisation_not_smallprimesc                 C      t dddks	J d S )Nr   r6   r   rA   r   r   r   test_jacobi_with_zero     z&TestNumbertheory.test_jacobi_with_zeroc                 C   r   )Nr   r6   r   rA   r   r   r   test_jacobi_with_one  r   z%TestNumbertheory.test_jacobi_with_oner6   i  c                 C   s   | d S r    r   rZ   r   r   r   r^     s    zTestNumbertheory.<lambda>c                 C   s   t |r:t }td|D ]}t|| |dksJ ||| |  qtd|D ]}||vr7t||dks7J q(d S t|}td|D ]}d}|D ]}|t||d |d  9 }qI|t||ksbJ qCd S )Nr   r   r   )r   r#   r$   r   r%   r
   )rB   r]   r'   r*   r   r   r-   cr   r   r   test_jacobi  s"   zTestNumbertheory.test_jacobic                 C   sF   |\}}t ||}d|  k r|k sJ  J || | dks!J d S )Nr   r   r   )rB   r~   r(   r]   invr   r   r   test_inverse_mod  s   
z!TestNumbertheory.test_inverse_modc                 C   s   dt ddks	J d S )Nr      r   rA   r   r   r   test_inverse_mod_with_zero  r   z+TestNumbertheory.test_inverse_mod_with_zeroNr   )"rQ   rR   rS   r   unittestZ
skipUnless
HC_PRESENTr   HYP_SLOW_SETTINGSr   r   r   r   r   r_   rt   r`   r   r   r   HYP_SETTINGSri   r   r   r   r   r   r   r   ra   r   rc   r   r   r   r   r   r   r   #  sn    




r   )6r{   	functoolsr   Z	unittest2r   ImportErrorZhypothesis.strategies
strategiesr_   r&   Z
hypothesisr   r   r   r   r   Znumbertheoryr   r	   r
   r   r   r   r   r   r   r   r   Z	BIGPRIMESmarkZparametrizezipr   r"   r+   r.   r1   r8   r<   ZTestCaser=   Z	compositerc   rg   ri   r   r   r   Zfilter_too_muchZtoo_slowdictr   r   r   r   r   r   r   <module>   sf    4$








. 