a
    !f                     @   s   d Z ddlZddlmZ ddlmZmZ ddlT ddlm	Z	m
Z
mZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ dd ZG dd deZdd ZdZdZG dd dejZi fddZedkrdd Z ej!dd dS )z.Self-test for the custom module exponentiation    N)list_test_cases)long_to_bytesbytes_to_long)*)load_pycryptodome_raw_libcreate_string_bufferget_raw_bufferc_size_tc_ulonglong)SHAKE128)Integer)_raw_montgomery)StrongRandomc                 C   s   t tj| d}|S )N)data)r   r   new)tagrng r   a/var/www/html/python-backend/venv/lib/python3.9/site-packages/Crypto/SelfTest/Math/test_modexp.py
create_rng9   s    r   c                   @   s   e Zd ZdS )ExceptionModulusN)__name__
__module____qualname__r   r   r   r   r   =   s   r   c           	   	      s   t tt| ||  fdd| ||fD \}}}t }t||||t td}|dkrdt |rtt	d| t
t|}|S )Nc                    s   g | ]}t | qS r   )r   ).0xmax_lenr   r   
<listcomp>C       zmonty_pow.<locals>.<listcomp>       zmonty_pow failed with error: %d)lenr   maxr   r   	monty_powr	   r
   r   
ValueErrorr   r   )	baseexpmodulusZbase_bZexp_bZ	modulus_bouterrorresultr   r   r   r$   @   s&    
	r$   l   MAUjb*a\}8z09c_(LmJC0:8yeZIf7j3DN`&E[@z^:
g8*7cHo)Ra>)<emacCP:I';d<aFuM%tz4CLJ)sCRuUM2
=	PT9ZV!0s_\yTvGv1&;B~:6\.tN}vYC"ca(d	[2\4Y>=tOjEGKaR44<OI*#`( XWp, l   uM~Lo[*QvU%=QU)5d_*iRy^in3&y_!oicpJFXQM0,J&[{"s}N,C20\'j7.8aMRMt
CIsD	koCvj']/S<iJvcTr.wFFuZeq9<*M["t,`@T:KZ
'SnkD5xl!Xyy99vM"*^ x+j~oB8s?vj	rK?/jfe@\6d7lH3 c                   @   sl   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d Zdd Zdd ZdS )
TestModExpc                 C   s   |  dtddd d S )N            )assertEqualr$   )selfr   r   r   
test_small^   s    zTestModExp.test_smallc                 C   s,   d}t |tt}t|tt}| || d S )N   )pow	exponent1modulus1r$   r1   r2   r&   expectedr+   r   r   r   test_large_1a   s    zTestModExp.test_large_1c                 C   s    d}t |dt}| |d d S )Nr4   r   r-   )r$   r7   r1   )r2   r&   r+   r   r   r   test_zero_expg   s    zTestModExp.test_zero_expc                 C   s   t dtt}| |d d S )Nr   )r$   r6   r7   r1   )r2   r+   r   r   r   test_zero_basel   s    zTestModExp.test_zero_basec                 C   s,   d}|  tt|td |  ttddd d S )Nl    r   )assertRaisesr   r$   r6   r2   r&   r   r   r   test_zero_modulusp   s    zTestModExp.test_zero_modulusc                 C   s8   t d }t|t d> t }t|t d> t }| || d S )Ni@   )r7   r5   r$   r1   r8   r   r   r   test_larger_exponentu   s    zTestModExp.test_larger_exponentc                 C   s"   t d? }| tt|tt d  d S )N   r-   )r7   r=   r   r$   r6   r>   r   r   r   test_even_modulus{   s    zTestModExp.test_even_modulusc                 C   s   t  td}tddD ]`}t||dB }t||| }t||}t|||}t	|||}| 
|| qd S )NZTestr-   d   )r   r   updatebranger   
from_bytesreadr5   r$   r1   )r2   prnglengthZmodulus2r&   Z	exponent2r9   r+   r   r   r   test_several_lengths   s    zTestModExp.test_several_lengthsc           	      C   s   t td}tdD ]}tdD ]}|ddB }|d| }||d | }t|||}t|||}| || |d|d | > d N }t|||}t|||}| || q qd S )NzTest variable exponent         r-      r   rF   rG   getrandbitsr5   r$   r1   )	r2   rJ   ijr(   r&   exponentr9   r+   r   r   r   test_variable_exponent   s    z!TestModExp.test_variable_exponentc           	      C   sx   t td}d}tdD ]Z}|d| dB }|d| | }|d| }t|||}t|||}| || qd S )NzTest 63?     rP   r-   rQ   	r2   rJ   rK   _r(   r&   rU   r9   r+   r   r   r   test_stress_63   s    zTestModExp.test_stress_63c           	      C   sx   t td}d}tdD ]Z}|d| dB }|d| | }|d| }t|||}t|||}| || qd S )NzTest 64r@   rX   rP   r-   rQ   rY   r   r   r   test_stress_64   s    zTestModExp.test_stress_64c           	      C   sx   t td}d}tdD ]Z}|d| dB }|d| | }|d| }t|||}t|||}| || qd S )NzTest 65A   rX   rP   r-   rQ   rY   r   r   r   test_stress_65   s    zTestModExp.test_stress_65N)r   r   r   r3   r:   r;   r<   r?   rA   rC   rL   rV   r[   r\   r^   r   r   r   r   r,   \   s   r,   c                 C   s   g }|t t7 }|S N)r   r,   )configtestsr   r   r   	get_tests   s    rb   __main__c                   C   s   t t S r_   )unittestZ	TestSuiterb   r   r   r   r   <lambda>   r   re   suite)ZdefaultTest)"__doc__rd   ZCrypto.SelfTest.st_commonr   ZCrypto.Util.numberr   r   ZCrypto.Util.py3compatZCrypto.Util._raw_apir   r   r   r	   r
   ZCrypto.Hashr   ZCrypto.Math.Numbersr   ZCrypto.Math._IntegerCustomr   ZCrypto.Random.randomr   r   r%   r   r$   r6   r7   ZTestCaser,   rb   r   rf   mainr   r   r   r   <module>"   s&   e