U
    ڲg
                     @   sh   d Z ddlZddlZddlmZmZ eedddZeedddZ	eedd	d
Z
eedddZdS )z(Functions for generating random numbers.    N)common	transform)nbitsreturnc                 C   sN   t | d\}}t|}|dkrJttd}|d| L }td|| }|S )zReads 'nbits' random bits.

    If nbits isn't a whole number of bytes, an extra byte will be appended with
    only the lower bits set.
       r      B)divmodosurandomordstructpack)r   nbytesZrbits
randomdataZrandomvalue r   //tmp/pip-unpacked-wheel-1z1vrokf/rsa/randnum.pyread_random_bits   s    
r   c                 C   s&   t | }t|}|d| d > O }|S )z3Reads a random integer of approximately nbits bits.r   )r   r   Z	bytes2int)r   r   valuer   r   r   read_random_int.   s    
r   c                 C   s   t | }|dB S )zhReads a random odd integer of approximately nbits bits.

    >>> read_random_odd_int(512) & 1
    1
    r   )r   )r   r   r   r   r   read_random_odd_int;   s    r   )maxvaluer   c                 C   sF   t | }d}t|}|| kr qB|d dkr8|r8|d8 }|d7 }q|S )zReturns a random integer x with 1 <= x <= maxvalue

    May take a very long time in specific situations. If maxvalue needs N bits
    to store, the closer maxvalue is to (2 ** N) - 1, the faster this function
    is.
    r   
   r   )r   bit_sizer   )r   r   triesr   r   r   r   randintH   s    

r   )__doc__r
   r   rsar   r   intbytesr   r   r   r   r   r   r   r   <module>   s   