o
    6Îh§E  ã                   @   s8  d dl mZ d dlZd dlmZmZ d dlmZ ze W n e	y'   e
ZY nw zd dlmZmZ dZdZW n! eyW   dZz
d dlmZ dZW n eyT   dZY nw Y nw es\ergeeeed	ƒƒf ƒZd dlZd dlZd dlZd	d
lmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd„ Z#er¸dd „ Z$ner¿d!d „ Z$nej%d"krÉd#d „ Z$nd$d „ Z$zej&Z'W n e(yß   d%d&„ Z'Y nw d'd(„ Z&d)d*„ Z)d+d,„ Z*d-d.„ Z+d/d0„ Z,d1d2„ Z-d3d4„ Z.d5d6„ Z/d7d8„ Z0d9d:„ Z1d;d<„ Z2d=d>„ Z3d?d@„ Z4g dA¢Z5d a6dS )Bé    )ÚdivisionN)Úinteger_typesÚPY2)Úreduce)ÚpowmodÚmpzTF©r   é   )Ú
bit_lengthc                   @   s   e Zd ZdZdS )ÚErrorz)Base class for exceptions in this module.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úf/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/ecdsa/numbertheory.pyr   /   s    r   c                   @   ó   e Zd ZdS )ÚJacobiErrorN©r   r   r   r   r   r   r   r   5   ó    r   c                   @   r   )ÚSquareRootErrorNr   r   r   r   r   r   9   r   r   c                   @   r   )ÚNegativeExponentErrorNr   r   r   r   r   r   =   r   r   c                 C   s,   t  dt¡ |dk rtd| ƒ‚t| ||ƒS )z+Raise base to exponent, reducing by moduluszRFunction is unused in library code. If you use this code, change to pow() builtin.r   z#Negative exponents (%d) not allowed)ÚwarningsÚwarnÚDeprecationWarningr   Úpow)ÚbaseÚexponentÚmodulusr   r   r   Úmodular_expA   s   ýÿr   c                 C   sš   |d dksJ ‚t |ƒdksJ ‚t | ƒt |ƒkrK| d dkr=tdt |ƒd ƒD ]}| |  | d ||    | | | < q'| dd… } t | ƒt |ƒks| S )zReduce poly by polymod, integer arithmetic modulo p.

    Polynomials are represented as lists of coefficients
    of increasing powers of x.éÿÿÿÿr	   r   é   )ÚlenÚxrange)ÚpolyÚpolymodÚpÚir   r   r   Úpolynomial_reduce_modP   s   
(ür(   c                 C   sr   t | ƒt |ƒ d dg }tt | ƒƒD ]}tt |ƒƒD ]}|||  | | ||   | ||| < qqt|||ƒS )z—Polynomial multiplication modulo a polynomial over ints mod p.

    Polynomials are represented as lists of coefficients
    of increasing powers of x.r	   r   )r"   r#   r(   )Úm1Úm2r%   r&   Úprodr'   Újr   r   r   Úpolynomial_multiply_modg   s   *ÿr-   c                 C   s~   ||k sJ ‚|dkrdgS | }|}|d dkr|}ndg}|dkr=|d }t ||||ƒ}|d dkr9t ||||ƒ}|dks!|S )z—Polynomial exponentiation modulo a polynomial over ints mod p.

    Polynomials are represented as lists of coefficients
    of increasing powers of x.r   r	   r!   )r-   )r   r   r%   r&   ÚGÚkÚsr   r   r   Úpolynomial_exp_mod   s   ür1   c                 C   sâ   |dkst dƒ‚|d dkst dƒ‚| | } | dkrdS | dkr"dS | d}}|d dkr<|d |d }}|d dks-|d dksN|d dksN|d dkrQd}nd	}|dkrY|S |d
 dkrh|d
 dkrh| }|t|| |ƒ S )zJacobi symbolé   zn must be larger than 2r!   r	   zn must be oddr   é   é   r    é   )r   Újacobi)ÚaÚnÚa1Úer0   r   r   r   r6   Ÿ   s*   	
ÿ$r6   c                 C   sv  d|   kr|k sJ ‚ J ‚d|k sJ ‚| dkrdS |dkr | S t | |ƒ}|dkr1td| |f ƒ‚|d dkrAt| |d d |ƒS |d d	kryt| |d d |ƒ}|dkr_t| |d d |ƒS ||d ksgJ ‚d|  td|  |d	 d |ƒ | S trtd
|ƒ}n|}td|ƒD ].}t || d|   |ƒdkr¶| | df}td|d d ||ƒ}|d r°tdƒ‚|d   S qˆtdƒ‚)z)Modular square root of a, mod p, p prime.r   r	   r!   r    z%d has no square root modulo %dr5   r2   r3   é   iÿÿÿ)r   r	   zp is not primezNo b found.)r6   r   r   r   Úminr#   r1   ÚRuntimeError)r7   r&   ÚjacÚdÚ	range_topÚbÚfÚffr   r   r   Úsquare_root_mod_prime¿   s:   
$ûrD   c                 C   ó   | dkrdS t | d|ƒS ©úInverse of a mod m.r   r    )r   ©r7   Úmr   r   r   Úinverse_modò   ó   rJ   c                 C   s€   | dkrdS t | ƒ} t |ƒ}t dƒt dƒ}}| | |}}|dkr<|| }|||  |||  ||f\}}}}|dks"|| S )rG   r   r	   r   ©r7   rI   ÚlmÚhmÚlowÚhighÚrr   r   r   rJ   ú   s   $þ)r2   r3   c                 C   rE   rF   )r   rH   r   r   r   rJ     rK   c                 C   sf   | dkrdS d\}}| | |}}|dkr/|| }|||  |||  ||f\}}}}|dks|| S )rG   r   )r	   r   r	   r   rL   r   r   r   rJ     s   $þc                 C   s   | r||  | } }| s|S )z1Greatest common divisor using Euclid's algorithm.r   ©r7   rA   r   r   r   Úgcd2*  s   ÿrS   c                  G   ó:   t | ƒdkrtt| ƒS t| d dƒrtt| d ƒS | d S )zRGreatest common divisor.

    Usage: gcd([ 2, 4, 6 ])
    or:    gcd(2, 4, 6)
    r	   r   Ú__iter__)r"   r   rS   Úhasattr©r7   r   r   r   Úgcd1  ó
   
rX   c                 C   s   | | t | |ƒ S )z&Least common multiple of two integers.)rX   rR   r   r   r   Úlcm2?  s   rZ   c                  G   rT   )zPLeast common multiple.

    Usage: lcm([ 3, 4, 5 ])
    or:    lcm(3, 4, 5)
    r	   r   rU   )r"   r   rZ   rV   rW   r   r   r   ÚlcmE  rY   r[   c                 C   sN  t | tƒsJ ‚| dk rg S g }tD ]6}|| kr n/t| |ƒ\}}|dkrGd}|| kr@|} t| |ƒ\}}|dkr8n|d }|| ks*| ||f¡ q| td kr¥t| ƒr[| | df¡ |S td }	 |d }t| |ƒ\}}||k rpn*|dkr™d}|} || kr’t| |ƒ\}}|dkrˆn
|} |d }|| ks|| ||f¡ q`| dkr¥| | df¡ |S )z2Decompose n into a list of (prime,exponent) pairs.r!   r   r	   r    )Ú
isinstancer   ÚsmallprimesÚdivmodÚappendÚis_prime)r8   Úresultr?   ÚqrQ   Úcountr   r   r   ÚfactorizationS  sX   û€ìûñrd   c                 C   s‚   t  dt¡ t| tƒsJ ‚| dk rdS d}t| ƒ}|D ]#}|d }|dkr6||d |d   |d d  }q||d d  }q|S )z'Return the Euler totient function of n.ú{Function is unused by library code. If you use this code, please open an issue in https://github.com/tlsfuzzer/python-ecdsar2   r	   r   )r   r   r   r\   r   rd   )r8   ra   rC   rB   r:   r   r   r   Úphi‹  s   ü"rf   c                 C   s   t  dt¡ tt| ƒƒS )zReturn Carmichael function of n.

    Carmichael(n) is the smallest integer x such that
    m**x = 1 mod n for all m relatively prime to n.
    re   )r   r   r   Úcarmichael_of_factorizedrd   )r8   r   r   r   Ú
carmichael¥  s
   ürh   c                 C   sR   t  dt¡ t| ƒdk rdS t| d ƒ}tdt| ƒƒD ]}t|t| | ƒƒ}q|S )zlReturn the Carmichael function of a number that is
    represented as a list of (prime,exponent) pairs.
    re   r	   r   )r   r   r   r"   Úcarmichael_of_ppowerr#   r[   )Úf_listra   r'   r   r   r   rg   ¶  s   ürg   c                 C   sD   t  dt¡ | \}}|dkr|dkrd|d  S |d ||d   S )z:Carmichael function of the given power of the given prime.re   r!   r	   )r   r   r   )Úppr&   r7   r   r   r   ri   Ì  s   üri   c                 C   sZ   t  dt¡ |dkrdS t| |ƒdksJ ‚| }d}|dkr+||  | }|d }|dks|S )z8Return the order of x in the multiplicative group mod m.re   r	   r   )r   r   r   rX   )ÚxrI   Úzra   r   r   r   Ú	order_modÝ  s   ü
þrn   c                 C   sL   t  dt¡ 	 t| |ƒ}|dkr	 | S |}	 t| |ƒ\}}|dkr"n|} qq)z5Return the largest factor of a relatively prime to b.re   r	   r   )r   r   r   rX   r^   )r7   rA   r?   rb   rQ   r   r   r   Úlargest_factor_relatively_prime÷  s"   ü
úüûro   c                 C   s   t  dt¡ t| t|| ƒƒS )z}Return the order of x in the multiplicative group mod m',
    where m' is the largest factor of m relatively prime to x.
    re   )r   r   r   rn   ro   )rl   rI   r   r   r   Úkinda_order_mod  s
   ürp   c                 C   sd  da | td kr| tv rdS dS t| dƒdkrdS d}dt| ƒ }d|  kr,d	ks/J ‚ J ‚d
D ]\}}||k r; n|}q1d}| d }|d dkrX|d }|d }|d dksJt|ƒD ]S}t t¡}t||| ƒ}	|	dkr¯|	| d kr¯d}
|
|d kr¢|	| d kr¢t|	d| ƒ}	|	dkr’|d a  dS |
d }
|
|d kr¢|	| d ks|	| d kr¯|d a  dS q\dS )a@  Return True if x is prime, False otherwise.

    We use the Miller-Rabin test, as given in Menezes et al. p. 138.
    This test is not exact: there are composite values n for which
    it returns True.

    In testing the odd numbers from 10000001 to 19999999,
    about 66 composites got past the first test,
    5 got past the second test, and none got past the third.
    Since factors of 2, 3, 5, 7, and 11 were detected during
    preliminary screening, the number of numbers tested by
    Miller-Rabin was (19999999 - 10000001)*(2/3)*(4/5)*(6/7)
    = 4.57 million.
    r   r    TFi	  r	   é(   é   i @  ))éd   é   )é–   é   )éÈ   é   )éú   é   )i,  é	   )i^  r3   )i  r4   )iÂ  é   )i&  r;   )iŠ  r5   )iR  r2   )i  r!   r!   )Úmiller_rabin_test_countr]   rX   r
   r#   ÚrandomÚchoicer   )r8   ÚtÚn_bitsr/   Úttr0   rQ   r'   r7   Úyr,   r   r   r   r`     sJ   þ
û€r`   c                 C   s4   | dk rdS | d dB }t |ƒs|d }t |ƒr|S )z9Return the smallest prime larger than the starting value.r!   r	   )r`   )Ústarting_valuera   r   r   r   Ú
next_primel  s   ÿr…   )Ér!   r2   r;   r4   rr   é   é   é   é   é   é   é%   é)   é+   é/   é5   é;   é=   éC   éG   éI   éO   éS   éY   éa   ée   ég   ék   ém   éq   é   éƒ   é‰   é‹   é•   é—   é   é£   é§   é­   é³   éµ   é¿   éÁ   éÅ   éÇ   éÓ   éß   éã   éå   éé   éï   éñ   éû   i  i  i  i  i  i  i  i%  i3  i7  i9  i=  iK  iQ  i[  i]  ia  ig  io  iu  i{  i  i…  i  i‘  i™  i£  i¥  i¯  i±  i·  i»  iÁ  iÉ  iÍ  iÏ  iÓ  iß  iç  ië  ió  i÷  iý  i	  i  i  i#  i-  i3  i9  i;  iA  iK  iQ  iW  iY  i_  ie  ii  ik  iw  i  iƒ  i‡  i  i“  i•  i¡  i¥  i«  i³  i½  iÅ  iÏ  i×  iÝ  iã  iç  iï  iõ  iù  i  i  i  i  i)  i+  i5  i7  i;  i=  iG  iU  iY  i[  i_  im  iq  is  iw  i‹  i  i—  i¡  i©  i­  i³  i¹  iÇ  iË  iÑ  i×  iß  iå  iñ  iõ  iû  iý  i  i	  i  i  i  i%  i'  i-  i?  iC  iE  iI  iO  iU  i]  ic  ii  i  i  i‹  i“  i  i£  i©  i±  i½  iÁ  iÇ  iÍ  )7Ú
__future__r   ÚsysÚsixr   r   Ú	six.movesr   r#   Ú	NameErrorÚrangeÚgmpy2r   r   ÚGMPY2ÚGMPYÚImportErrorÚgmpyÚtupleÚtypeÚmathr   r~   Úutilr
   Ú	Exceptionr   r   r   r   r   r(   r-   r1   r6   rD   rJ   Úversion_inforX   rS   ÚAttributeErrorrZ   r[   rd   rf   rh   rg   ri   rn   ro   rp   r`   r…   r]   r}   r   r   r   r   Ú<module>   s†   ÿÿ€ú
  1




þ	8O M