
    tZh"                     X    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 Zd	 Zdd
Zy)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                 f    t        t        |       d      }t        |       dz  }||kD  r|||z
  z	  S |S )N      )intr   len)dataqlenxls       i/var/www/html/wine-match-dev/backend/winematch-backend/venv/lib/python3.12/site-packages/ecdsa/rfc6979.pyr	   r	      s9    GDM2AD	AA4xQXH    c                 \    t        | t        |            }||z
  }|dk  r|}t        ||      S )Nr   )r	   r   r   )r   orderz1z2s       r   r
   r
       s5    	$
5)	*B	eB	Av U++r   c                 "   t        |       } |       j                  }|dz   dz  }t        t        ||             t        t	        ||             t        |      f}	d|z  }
d|z  }t        j                  ||      }|j                  |
dz          |	D ]  }|j                  |        |j                         }t        j                  ||
|      j                         }
t        j                  ||      }|j                  |
dz          |	D ]  }|j                  |        |j                         }t        j                  ||
|      j                         }
	 d}t        |      |k  r9t        j                  ||
|      j                         }
||
z  }t        |      |k  r9t        ||      }d|cxk  r| k  rn n|dk  r|S |dz  }t        j                  ||
dz   |      j                         }t        j                  ||
|      j                         }
)	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          )	digestmodr   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secrets                  r   r   r   +   s    eDK##EAX!OE$VU34Ke,-M"
B 	%A 	%A 	i(AHHQ[ 		
A 	Ay!((*A 	i(AHHQ[ 		
A 	Ay!((*A  !funAy)002AFA !fun
 !T"A~NIHHQGY/668HHQ9%,,.% r   )r   r   )__doc__r!   binasciir   utilr   r   r   _compatr   __all__r	   r
   r    r   r   <module>r7      s3   	   E E  
 B,F/r   