ó
¾^Yc           @   se   d  Z  d   Z d   Z d   Z d   Z d   Z d   Z e d k ra d d	 l Z e j	   n  d	 S(
   s/   Common functionality shared by several modules.c         C   sĘ   |  d k r d S|  d k  r& |  }  n  |  d @d |  } t  |  d d i d d 6d d 6d d 6d d	 6d
 d 6d
 d 6d
 d 6d
 d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6| d S(   ss  
    Number of bits needed to represent a integer excluding any prefix
    0 bits.

    As per definition from https://wiki.python.org/moin/BitManipulation and
    to match the behavior of the Python 3 API.

    Usage::

        >>> bit_size(1023)
        10
        >>> bit_size(1024)
        11
        >>> bit_size(1025)
        11

    :param num:
        Integer value. If num is 0, returns 0. Only the absolute value of the
        number is considered. Therefore, signed integers will be abs(num)
        before the number's bit length is determined.
    :returns:
        Returns the number of bits in the integer.
    i    i   s   %xi   t   0t   1i   t   2t   3i   t   4t   5t   6t   7t   8t   9t   at   bt   ct   dt   et   f(   t   len(   t   numt   hex_num(    (    s'   /tmp/pip-build-kpPAdC/rsa/rsa/common.pyt   bit_size   s    

c         C   sZ   |  d k  r t  d |    n  |  d k r/ d Sd } x |  rU | d 7} |  d L}  q8 W| S(   sM   
    Returns the number of bits required to hold a specific long number.
    i    s%   Only nonnegative numbers possible: %si   (   t
   ValueError(   t   numbert   bits(    (    s'   /tmp/pip-build-kpPAdC/rsa/rsa/common.pyt	   _bit_size=   s    	
c         C   s>   t  t |   d  \ } } | s- |  d k r: | d 7} n  | S(   s  
    Returns the number of bytes required to hold a specific long number.

    The number of bytes is rounded up.

    Usage::

        >>> byte_size(1 << 1023)
        128
        >>> byte_size((1 << 1024) - 1)
        128
        >>> byte_size(1 << 1024)
        129

    :param number:
        An unsigned integer
    :returns:
        The number of bytes required to hold a specific long number.
    i   i    i   (   t   divmodR   (   R   t   quantat   mod(    (    s'   /tmp/pip-build-kpPAdC/rsa/rsa/common.pyt	   byte_sizeQ   s    c   	      C   s»   d } d } d } d } |  } | } xU | d k r{ |  | } | |  | }  } | | | | } } | | | | } } q' W| d k  r | | 7} n  | d k  r® | | 7} n  |  | | f S(   s@   Returns a tuple (r, i, j) such that r = gcd(a, b) = ia + jb
    i    i   (    (	   R
   R   t   xt   yt   lxt   lyt   oat   obt   q(    (    s'   /tmp/pip-build-kpPAdC/rsa/rsa/common.pyt   extended_gcdl   s     
c         C   sA   t  |  |  \ } } } | d k r= t d |  | f   n  | S(   s`   Returns x^-1 (mod n)

    >>> inverse(7, 4)
    3
    >>> (inverse(143, 4) * 143) % 4
    1
    i   s*   x (%d) and n (%d) are not relatively prime(   R#   R   (   R   t   nt   dividert   invt   _(    (    s'   /tmp/pip-build-kpPAdC/rsa/rsa/common.pyt   inverse   s    	c   	      C   sz   d } d } x | D] } | | 9} q WxL t  | |   D]; \ } } | | } t | |  } | | | | | } q7 W| S(   s  Chinese Remainder Theorem.

    Calculates x such that x = a[i] (mod m[i]) for each i.

    :param a_values: the a-values of the above equation
    :param modulo_values: the m-values of the above equation
    :returns: x such that x = a[i] (mod m[i]) for each i


    >>> crt([2, 3], [3, 5])
    8

    >>> crt([2, 3, 2], [3, 5, 7])
    23

    >>> crt([2, 3, 0], [7, 11, 15])
    135
    i   i    (   t   zipR(   (	   t   a_valuest   modulo_valuest   mR   t   modulot   m_it   a_it   M_iR&   (    (    s'   /tmp/pip-build-kpPAdC/rsa/rsa/common.pyt   crt   s    
t   __main__i’’’’N(
   t   __doc__R   R   R   R#   R(   R1   t   __name__t   doctestt   testmod(    (    (    s'   /tmp/pip-build-kpPAdC/rsa/rsa/common.pyt   <module>   s   	)					#