ó
Që?[c           @   sÁ   d  Z  d Z d d l Z e j d d k rK e j d d k rK d d l Tn  d d l Z d e j f d	     YZ d
 e j f d     YZ i  d  Z	 e
 d k r˝ d   Z e j d d  n  d S(   s+   Self-tests for (some of) Crypto.Util.numbers   $Id$i˙˙˙˙Ni    i   i   (   t   *t	   MiscTestsc           B   sY   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(	   c         C   s    d d l  m a d d  l a d  S(   Ni˙˙˙˙(   t   number(   t   Crypto.UtilR   t   math(   t   self(    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt   setUp'   s    c         C   s˝  |  j  t t j d d  |  j  t t j d d  |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d	 d   |  j d t j d
 d   |  j d t j d d   xt d d d  D]x} |  j d t j d |   d } xM| d | d k r´t | d d |  \ } } | t |  } |  j | d | | f | d | t j | d |  f  t | d |  \ } } | t |  } |  j | | | f | | t j | |  f  t | d d |  \ } } | t |  } |  j | d | | f | d | t j | d |  f  | d 9} qhWq=Wd S(   s   Util.number.ceil_shifti˙˙˙˙i   i    i   i   i   i   i   i   i   i	   i   l    l    Ni   (   t   assertRaisest   AssertionErrorR   t
   ceil_shiftt   assertEqualt   ranget   divmodt   int(   R   t   bt   nt   qt   rt   expected(    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt   test_ceil_shift,   sJ    $$c         C   să  |  j  t t j d d  |  j  t t j d d  |  j  t t j d d  |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d	 d   |  j d t j d
 d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d	 d   |  j d t j d
 d   |  j d t j d d   |  j d t j d d   |  j d t j d d	   |  j d t j d d	   |  j d t j d d	   |  j d t j d d	   |  j d t j d	 d	   |  j d t j d
 d	   |  j d t j d d	   |  j d t j d d	   |  j d t j d d	   |  j d t j d d	   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d	 d   |  j d t j d
 d   |  j d t j d d   |  j d t j d d   |  j d t j d d   |  j d t j d d   d S(   s   Util.number.ceil_divt   1i   i    i˙˙˙˙iţ˙˙˙i   iý˙˙˙i   i   i   i   i   i   i	   i÷˙˙˙iü˙˙˙iř˙˙˙iů˙˙˙iú˙˙˙iű˙˙˙N(   R   t	   TypeErrorR   t   ceil_divt   ZeroDivisionErrorR
   (   R   (    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt   test_ceil_div_   sl    c         C   sá  |  j  t t j d  |  j  t t j d  |  j  t t j d  |  j d t j d   |  j d t j d   |  j  t t j d  |  j d t j d   |  j  t t j d  |  j  t t j d	  |  j  t t j d
  d } d } xs | d k  rf| d | k rC|  j | t j |  d | | f  | d 7} n |  j  t t j |  | d 7} qô Wxs t d d d  D]_ } |  j  t t j d | d  |  j | t j d |   |  j  t t j d | d  qzWd S(   s   Util.number.exact_log2t   0i˙˙˙˙i    i   i   i   i   i   i   i   i   i   s   expected=2**%d, n=%di@   l    NiA   (   R   R   R   t
   exact_log2t
   ValueErrorR
   R   (   R   t   eR   (    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt   test_exact_log2¤   s,    &c         C   sž  |  j  d t j d d   |  j t t j d d  |  j  d t j d d   |  j t t j d d  |  j  d t j d d   |  j  d t j d d   |  j  d t j d d   |  j t t j d d  |  j  d t j d d   |  j  d t j d d   |  j  d t j d d   |  j  d t j d d   |  j t t j d d  |  j t t j d d  |  j  d t j d d d	 t  |  j t t j d d d	 t d
 S(   s   Util.number.exact_divi   i   i   i   i˙˙˙˙iţ˙˙˙iü˙˙˙i    t   allow_divzeroN(   R
   R   t	   exact_divR   R   R   t   True(   R   (    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt   test_exact_divż   s     "c      	   C   sŔ   |  j  t t j d d  x  t d d  D] } x t d d  D]u } | d k rm |  j  t t j | |  q? |  j | | t t j	 t
 |  |   f | | t j | |  f  q? Wq) Wd S(   s   Util.number.floor_divR   i   iö˙˙˙i
   i    N(   R   R   R   t	   floor_divR   R   R
   R   R   t   floort   float(   R   t   aR   (    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt   test_floor_divÜ   s    +c         C   sŠ  |  j  t t j d  |  j  t t j d  d } t j |  } |  j | d d  |  j | d | d >d k d  |  j | d | >k  d  d } t j | |  } |  j t j | d |  d  |  j | d d  |  j | d | d >d k d  |  j | d | >k  d  d } t j | |  } |  j t j | d d ?|  d  |  j | d d  |  j | d | d >d k d  |  j | d | >k  d  d	 S(   s   Util.number.getStrongPrimei   i  i   i   i    l    i   i   Ni   i  i   i  (   R   R   R   t   getStrongPrimet   assertNotEqualR
   t   GCD(   R   t   bitst   xR   (    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt   test_getStrongPrimeç   s&    " "$"c         C   s   |  j  t j d  t  |  j  t j d  t  |  j  t j d  t  |  j  t j d  t  |  j  t j d  t  |  j  t j d  t  |  j  t j d$  t  |  j  t j d'  t  xQ d) d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 f D]% } |  j  t j t |   t  qó Wd" S(6   s   Util.number.isPrimeiý˙˙˙iţ˙˙˙i   i   i   i   l    i˙  i+   i   iÓ   i=   i   i;  i5w  l   H
 l   9 l   ^ l   <= l   eEm l   É
Ű l   & l   5L(l   Ç)S l   SŚ l   ďZ8l   Ý5q:l   ?érl   }Ňe l   Bs l   =f l   ó^ř; l   ĺ=ńw NlV                                                                                                                                                                              lV   ˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙ lV                                                                                                                                                                              lV   ˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙ lŞ˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙ iU  i iű#  iá§ idÁl   u^žß l   ł+%!al   MnÔzž]l   g4Đ+ l   I>6l   S{AKWl   ĂbĘ\\QN l   ăzIlv3 l   _VB;.( (   R
   R   t   isPrimet   FalseR    t   long(   R   t	   composite(    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt   test_isPrimeý   s    	c         C   s   |  j  t j d  d  |  j  t j d  d  |  j  t j d  d  |  j  t j d  d  |  j  t j d  d  d  S(	   Ni   i   i˘   i   i@ş˘ lE   Ľ)ýď8^n, ž´n:zb´|DELU"ęN79YÁ ŞSžh"ś*f8<>kŽtkÂ26"z źBT6ű	íV´ ĎRNccË\Ä˘Ő]đ]rX"Sw~oÝ Ë8I(jćO3˝2Ç¸Ň
 i   i   (   R
   R   t   size(   R   (    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt	   test_size  s
    (   t   __name__t
   __module__R   R   R   R   R!   R&   R,   R1   R3   (    (    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyR   &   s   		3	E					t   FastmathTestsc           B   s   e  Z d    Z d   Z RS(   c         C   s   d d l  m a d  S(   Ni˙˙˙˙(   R   (   R   R   (   R   (    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyR     s    c         C   sK   d } d } t  j j | |  } |  j | | j  |  j | | j  d S(   sb   Test that mpzToLongObj and longObjToMPZ (internal functions) roundtrip negative numbers correctly.lř˙˙˙     ?<\"XéX\Ą	l    N(   R   t	   _fastmatht   rsa_constructR
   R   R   (   R   R   R   t   k(    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt8   test_negative_number_roundtrip_mpzToLongObj_longObjToMPZ  s
    (   R4   R5   R   R:   (    (    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyR6     s   	c         C   sô   d d l  m } | t  } y$ d d l m } | | t  7} Wn­ t k
 rď d d l m } d d  l	 } d d  l
 } | j j | j j | j j | j | j      d | d   } | j j |  rđ t d d d	 |   qđ n X| S(
   Ni˙˙˙˙(   t   list_test_cases(   R7   (   t   get_config_vars   /../../PublicKey/_fastmatht   SOs-   While the _fastmath module exists, importing s<   it failed. This may point to the gmp or mpir shared library s.   not being in the path. _fastmath was found at (   t   Crypto.SelfTest.st_commonR;   R   t   Crypto.PublicKeyR7   R6   t   ImportErrort   distutils.sysconfigR<   t   inspectt   os.patht   patht   normpatht   dirnamet   abspatht   getfilet   currentframet   exists(   t   configR;   t   testsR7   R<   RB   t   ost   _fm_path(    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt	   get_tests$  s    	>t   __main__c           C   s   t  j t    S(   N(   t   unittestt	   TestSuiteRO   (    (    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt   <lambda>7  s    t   defaultTestt   suite(   t   __doc__t   __revision__t   syst   version_infot   Crypto.Util.py21compatRQ   t   TestCaseR   R6   RO   R4   RU   t   main(    (    (    s?   lib/python2.7/site-packages/Crypto/SelfTest/Util/test_number.pyt   <module>   s   &ń	