o
    ƒªRh"  ã                   @   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   úO/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/ecdsa/rfc6979.pyr      s
   r   c                 C   s,   t | t|ƒƒ}|| }|dk r|}t||ƒS )Nr   )r   r   r   )r   ÚorderÚz1Ú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 ]}| |¡ q7| ¡ }t ||
|¡ ¡ }
tj||d}| |
d ¡ |	D ]}| |¡ q\| ¡ }t ||
|¡ ¡ }
	 d}t	|ƒ|k rt ||
|¡ ¡ }
||
7 }t	|ƒ|k szt
||ƒ}d|  krœ| k r¨n n
|d	kr¤|S |d8 }t ||
d |¡ ¡ }t ||
|¡ ¡ }
qr)
aD  
    Generate the ``k`` value - the nonce for DSA.

    :param int order: order of the DSA generator used in the signature
    :param int secexp: secure exponent (private key) in numeric form
    :param hash_func: reference to the same hash function used for generating
        hash, like :py:class:`hashlib.sha1`
    :param bytes data: hash in binary form of the signing data
    :param int retry_gen: how many good 'k' values to skip before returning
    :param bytes extra_entropy: additional added data in binary form as per
        section-3.6 of rfc6979
    :rtype: int
    é   r   ó   ó    )Ú	digestmodTr   r   r   )r   Údigest_sizer   r   r	   ÚhmacÚnewÚupdateÚdigestr   r   )r   ÚsecexpÚ	hash_funcr   Ú	retry_genÚextra_entropyr   ÚholenÚrolenÚbxÚvÚkÚiÚtÚsecretr   r   r   r
   +   sF   ýþ
îr
   )r   r   )Ú__doc__r   Úbinasciir   Úutilr   r   r   Ú_compatr   Ú__all__r   r	   r
   r   r   r   r   Ú<module>   s    	