a
    dbS
                     @   sL   d Z ddlZddlmZmZ ddlmZ dd Zdd Zd	d
 Z	dd Z
dS )z(Functions for generating random numbers.    N)common	transform)bytec                 C   sJ   t | d\}}t|}|dkrFttd}|d| L }t|| }|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      )divmodosurandomordr   )nbitsnbytesZrbits
randomdataZrandomvalue r   \/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/rsa/randnum.pyread_random_bits   s    
r   c                 C   s&   t | }t|}|d| d > O }|S )z8Reads a random integer of approximately nbits bits.
    r   )r   r   Z	bytes2int)r   r   valuer   r   r   read_random_int0   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   c                 C   sF   t | }d}t|}|| kr qB|r8|d dk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   )Zmaxvaluer   triesr   r   r   r   randintK   s    

r   )__doc__r   rsar   r   Zrsa._compatr   r   r   r   r   r   r   r   r   <module>   s   