o
    5h                     @   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rqdd Z ej!dd dS 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   t/var/www/html/construction_image-detection-poc/venv/lib/python3.10/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r2t |r:t	d| t
t|}|S )Nc                    s   g | ]}t | qS r   )r   ).0xmax_lenr   r   
<listcomp>C   s    zmonty_pow.<locals>.<listcomp>       zmonty_pow failed with error: %d)lenr   maxr   r   	monty_powr	   r
   r   
ValueErrorr   r   )	baseexpmodulusbase_bexp_b	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#   r3   r4   r%   expectedr-   r   r   r   test_large_1a   s   zTestModExp.test_large_1c                 C   s    d}t |dt}| |d d S )Nr6   r   r/   )r#   r9   r3   )r4   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#   r8   r9   r3   )r4   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#   r8   r4   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@   )r9   r7   r#   r3   r:   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/   )r9   r?   r   r#   r8   r@   r   r   r   test_even_modulus{   s   zTestModExp.test_even_modulusc                 C   s   t  td}tddD ]0}t||dB }t||| }t||}t|||}t	|||}| 
|| qd S )NTestr/   d   )r   r   updatebranger   
from_bytesreadr7   r#   r3   )r4   prnglengthmodulus2r%   	exponent2r;   r-   r   r   r   test_several_lengths   s   zTestModExp.test_several_lengthsc           	      C   s   t td}tdD ]P}tdD ]I}|ddB }|d| }||d | }t|||}t|||}| || |d|d | > d N }t|||}t|||}| || qq
d S )NzTest variable exponent         r/      r   rI   rJ   getrandbitsr7   r#   r3   )	r4   rM   ijr'   r%   exponentr;   r-   r   r   r   test_variable_exponent   s   z!TestModExp.test_variable_exponentc           	      C   x   t td}d}tdD ]-}|d| dB }|d| | }|d| }t|||}t|||}| || qd S )NzTest 63?     rU   r/   rV   	r4   rM   rN   _r'   r%   rZ   r;   r-   r   r   r   test_stress_63      zTestModExp.test_stress_63c           	      C   r\   )NzTest 64rB   r^   rU   r/   rV   r_   r   r   r   test_stress_64   rb   zTestModExp.test_stress_64c           	      C   r\   )NzTest 65A   r^   rU   r/   rV   r_   r   r   r   test_stress_65   rb   zTestModExp.test_stress_65N)r   r   r   r5   r<   r=   r>   rA   rC   rE   rQ   r[   ra   rc   re   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   ri   __main__c                   C   s   t t S rf   )unittest	TestSuiteri   r   r   r   r   <lambda>   s    rm   suite)defaultTest)"__doc__rk   Crypto.SelfTest.st_commonr   Crypto.Util.numberr   r   Crypto.Util.py3compatCrypto.Util._raw_apir   r   r   r	   r
   Crypto.Hashr   Crypto.Math.Numbersr   Crypto.Math._IntegerCustomr   Crypto.Random.randomr   r   r$   r   r#   r8   r9   TestCaser.   ri   r   rn   mainr   r   r   r   <module>   s*   !e