U
    Mf                     @   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   H/tmp/pip-unpacked-wheel-l_0d1exj/Cryptodome/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   s     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#   r0   r1   r%   expectedr*   r   r   r   test_large_1a   s    zTestModExp.test_large_1c                 C   s    d}t |dt}| |d d S )Nr3   r   r,   )r#   r6   r0   )r1   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#   r5   r6   r0   )r1   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#   r5   r1   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@   )r6   r4   r#   r0   r7   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,   )r6   r<   r   r#   r5   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readr4   r#   r0   )r1   prnglengthZmodulus2r%   Z	exponent2r8   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   rE   rF   getrandbitsr4   r#   r0   )	r1   rI   ijr'   r%   exponentr8   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?     rO   r,   rP   	r1   rI   rJ   _r'   r%   rT   r8   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?   rW   rO   r,   rP   rX   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   rW   rO   r,   rP   rX   r   r   r   test_stress_65   s    zTestModExp.test_stress_65N)r   r   r   r2   r9   r:   r;   r>   r@   rB   rK   rU   rZ   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    ra   __main__c                   C   s   t t S r^   )unittestZ	TestSuitera   r   r   r   r   <lambda>       rd   suite)ZdefaultTest)"__doc__rc   ZCryptodome.SelfTest.st_commonr   ZCryptodome.Util.numberr   r   ZCryptodome.Util.py3compatZCryptodome.Util._raw_apir   r   r   r	   r
   ZCryptodome.Hashr   ZCryptodome.Math.Numbersr   ZCryptodome.Math._IntegerCustomr   ZCryptodome.Random.randomr   r   r$   r   r#   r5   r6   ZTestCaser+   ra   r   rf   mainr   r   r   r   <module>"   s&   e