ó
j4Vdc           @   so  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l m Z d d l m Z d d l	 m
 Z
 d d l m Z m Z m Z m Z d! Z e j e Œ  Z d d „ Z d d" d „  ƒ  YZ d „  Z d „  Z d „  Z e d „ Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d „  Z& d „  Z' d  „  Z( d S(#   iÿÿÿÿ(   t   divisionN(   t   sha256i   (   t   der(   t   orderlen(   t   PY3t   int2bytet   bt   nexti   iH  i='  c         C   s¦   | d k r t j } n  |  d k s* t ‚ t |  ƒ } d } xS | d k r‘ | d 8} t | | ƒ ƒ d } d | k o‚ |  k  n r? | Sq? q? Wt d |  ƒ ‚ d S(   sÉ  Return a random integer k such that 1 <= k < order, uniformly
    distributed across that range. For simplicity, this only behaves well if
    'order' is fairly close (but below) a power of 256. The try-try-again
    algorithm we use takes longer and longer time (on average) to complete as
    'order' falls, rising to a maximum of avg=512 loops for the worst-case
    (256**k)+1 . All of the standard curves behave well. There is a cutoff at
    10k loops (which raises RuntimeError) to prevent an infinite loop when
    something is really broken like the entropy function not working.

    Note that this function is not declared to be forwards-compatible: we may
    change the behavior in future releases. The entropy= argument (which
    should get a callable that behaves like os.urandom) can be used to
    achieve stability within a given release (for repeatable unit tests), but
    should not be used as a long-term-compatible key generation algorithm.
    i   i'  i    sl   randrange() tried hard but gave up, either something is very wrong or you got realllly unlucky. Order was %xN(   t   Nonet   ost   urandomt   AssertionErrorR   t   string_to_numbert   RuntimeError(   t   ordert   entropyt   bytest   dont_try_forevert	   candidate(    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt	   randrange   s    
t   PRNGc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   |  j  | ƒ |  _ d  S(   N(   t   block_generatort	   generator(   t   selft   seed(    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   __init__A   s    c         C   sI   g  t  | ƒ D] } t |  j ƒ ^ q } t r8 t | ƒ Sd j | ƒ Sd  S(   Nt    (   t   rangeR   R   R   R   t   join(   R   t   numbytest   it   a(    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   __call__D   s    (
c         c   sS   d } xF t  rN x/ t d | | f j ƒ  ƒ j ƒ  D] } | Vq2 W| d 7} q	 Wd  S(   Ni    s
   prng-%d-%si   (   t   TrueR   t   encodet   digest(   R   R   t   countert   byte(    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyR   M   s
    	)	(   t   __name__t
   __module__R   R    R   (    (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyR   ;   s   			c         C   su   t  |  ƒ d t | ƒ ƒ } t t j | ƒ d ƒ | d d } d | k oW | k  n sq t d | | f ƒ ‚ | S(   Ni   i   i   (   R   R   t   intt   binasciit   hexlifyR   (   R   R   t   baset   number(    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt%   randrange_from_seed__overshoot_moduloT   s    $1c         C   s   d |  >d S(   Ni   (    (   t   numbits(    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   lsb_of_ones`   s    c         C   sA   t  t j |  d d ƒ d ƒ } | d } | d } | | | f S(   Ni   i   i   (   R(   t   matht   log(   R   t   bitsR   t	   extrabits(    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   bits_and_bytesb   s     

c         C   s˜   t  | ƒ \ } } } | r( | d 7} n  | |  ƒ j ƒ  |  } d | t | ƒ | } d t t j | ƒ d ƒ } d | k o‰ | k  n s” t ‚ | S(   Ni   s    i   (   R4   R#   t   lenR(   R)   R*   R   (   R   R   t   hashmodR2   R   R3   R+   R,   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt#   randrange_from_seed__truncate_bytess   s    "c         C   sÝ   t  t j | d d ƒ d ƒ } | d d } | |  ƒ j ƒ  |  } d | t | ƒ | } d | | } | r› t t | d ƒ t | ƒ @ƒ | d } n  d t  t j	 | ƒ d ƒ } d | k oÎ | k  n sÙ t
 ‚ | S(   Ni   i   i   i   s    i    i   (   R(   R0   R1   R#   R5   R   t   ordR/   R)   R*   R   (   R   R   R6   R2   t   maxbytesR+   t   topbitsR,   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt"   randrange_from_seed__truncate_bits€   s     +"c         C   sµ   | d k s t  ‚ t | ƒ \ } } } t |  ƒ } x{ t r° t d ƒ } | rs t t | d ƒ ƒ t | ƒ @ƒ } n  t | | | ƒ ƒ d } d | k o¤ | k  n r6 | Sq6 Wd  S(   Ni   R   (	   R   R4   R   R!   R   R   R8   R/   R   (   R   R   R2   R   R3   t   generatet	   extrabytet   guess(    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt    randrange_from_seed__trytryagainŽ   s    	%c         C   sk   t  | ƒ } d t d | ƒ d } t j | |  j ƒ  ƒ } t | ƒ | k sg t t | ƒ | f ƒ ‚ | S(   Ns   %0i   t   x(   R   t   strR)   t	   unhexlifyR"   R5   R   (   t   numR   t   lt   fmt_strt   string(    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   number_to_string¡   s
    *c         C   sE   t  | ƒ } d t d | ƒ d } t j | |  j ƒ  ƒ } | |  S(   Ns   %0i   R@   (   R   RA   R)   RB   R"   (   RC   R   RD   RE   RF   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   number_to_string_crop¨   s    c         C   s   t  t j |  ƒ d ƒ S(   Ni   (   R(   R)   R*   (   RF   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyR   ®   s    c         C   sL   t  | ƒ } t |  ƒ | k s6 t t |  ƒ | f ƒ ‚ t t j |  ƒ d ƒ S(   Ni   (   R   R5   R   R(   R)   R*   (   RF   R   RD   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   string_to_number_fixedlen±   s    *c         C   s(   t  |  | ƒ } t  | | ƒ } | | f S(   N(   RG   (   t   rt   sR   t   r_strt   s_str(    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   sigencode_stringsº   s    c         C   s    t  |  | | ƒ \ } } | | S(   N(   RN   (   RJ   RK   R   RL   RM   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   sigencode_string¿   s    c         C   s"   t  j t  j |  ƒ t  j | ƒ ƒ S(   N(   R   t   encode_sequencet   encode_integer(   RJ   RK   R   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   sigencode_derÅ   s    c         C   s-   | | d k r | | } n  t  |  | | ƒ S(   Ni   (   RN   (   RJ   RK   R   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   sigencode_strings_canonizeË   s    c         C   s-   | | d k r | | } n  t  |  | | ƒ S(   Ni   (   RO   (   RJ   RK   R   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   sigencode_string_canonizeÐ   s    c         C   s-   | | d k r | | } n  t  |  | | ƒ S(   Ni   (   RR   (   RJ   RK   R   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   sigencode_der_canonizeÕ   s    c         C   sn   t  | ƒ } t |  ƒ d | k s> t t |  ƒ d | f ƒ ‚ t |  |  | ƒ } t |  | | ƒ } | | f S(   Ni   (   R   R5   R   RI   (   t	   signatureR   RD   RJ   RK   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   sigdecode_stringÛ   s
    2c         C   s”   |  \ } } t  | ƒ } t | ƒ | k sB t t | ƒ | f ƒ ‚ t | ƒ | k sl t t | ƒ | f ƒ ‚ t | | ƒ } t | | ƒ } | | f S(   N(   R   R5   R   RI   (   t
   rs_stringsR   RL   RM   RD   RJ   RK   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   sigdecode_stringsâ   s    **c         C   s«   t  j |  ƒ \ } } | t d ƒ k rF t  j d t j | ƒ ƒ ‚ n  t  j | ƒ \ } } t  j | ƒ \ } } | t d ƒ k r¡ t  j d t j | ƒ ƒ ‚ n  | | f S(   NR   s   trailing junk after DER sig: %ss#   trailing junk after DER numbers: %s(   R   t   remove_sequenceR   t   UnexpectedDERR)   R*   t   remove_integer(   t   sig_derR   RX   t   emptyRJ   t   restRK   (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   sigdecode_derë   s    		(   i   i   iH  i='  i   i   (    ()   t
   __future__R    R	   R0   R)   t   hashlibR   R   R   t   curvesR   t   sixR   R   R   R   t   oid_ecPublicKeyt
   encode_oidt   encoded_oid_ecPublicKeyR   R   R   R-   R/   R4   R7   R;   R?   RG   RH   R   RI   RN   RO   RR   RS   RT   RU   RW   RY   R`   (    (    (    sX   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/ecdsa/util.pyt   <module>   s<   "(																		