a
    db
                     @   s^   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 g dZ
dd	 Zd
d ZdddZdS )a  
RFC 6979:
    Deterministic Usage of the Digital Signature Algorithm (DSA) and
    Elliptic Curve Digital Signature Algorithm (ECDSA)

    http://tools.ietf.org/html/rfc6979

Many thanks to Coda Hale for his implementation in Go language:
    https://github.com/codahale/rfc6979
    N)hexlify   )number_to_stringnumber_to_string_crop
bit_length)hmac_compat)r   bits2intbits2octets
generate_kc                 C   s2   t t| d}t| d }||kr.||| ? S |S )N      )intr   len)dataqlenxl r   ^/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/ecdsa/rfc6979.pyr      s
    r   c                 C   s,   t | t|}|| }|dk r"|}t||S )Nr   )r   r   r   )r   orderZz1Zz2r   r   r   r	       s
    r	       c                 C   sz  t | }| j}|d d }tt|| tt|| t|f}	d| }
d| }tj||d}||
d  |	D ]}|| qn| }t||
| }
tj||d}||
d  |	D ]}|| q| }t||
| }
d}t	||k rt||
| }
||
7 }qt
||}d|  kr2| k rLn n|dkrD|S |d8 }t||
d | }t||
| }
qd	S )
a  
        order - order of the DSA generator used in the signature
        secexp - secure exponent (private key) in numeric form
        hash_func - reference to the same hash function used for generating
            hash
        data - hash in binary form of the signing data
        retry_gen - int - how many good 'k' values to skip before returning
        extra_entropy - extra added data in binary form as per section-3.6 of
            rfc6979
       r          )	digestmodr   r   r   N)r   digest_sizer   r   r	   hmacnewupdatedigestr   r   )r   ZsecexpZ	hash_funcr   Z	retry_genZextra_entropyr   ZholenZrolenbxvkitsecretr   r   r   r
   +   s@    


r
   )r   r   )__doc__r   binasciir   utilr   r   r   Z_compatr   __all__r   r	   r
   r   r   r   r   <module>   s   	