o
    6h                  	   @   sd  d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZ dd d	 Zd
ZeedZdZeedZeedZdd d Zdd ZeeeeeeZejeeedee e eddZdd dd  d ZdZde ZdZeedZeedZdd d Zdd ZeeeeeeZejeeedee e eddZG dd  d eZG d!d" d"eZdS )#z6Implementation of Edwards Digital Signature Algorithm.    N   	shake_256)ellipticcurve)remove_whitespace
bit_lengthbytes_to_intint_to_bytescompat26_str         M37095705934669439343138083508754565189542113879843219016388785533085940283555   M15112221349535400772501151409588531511454012693041857206046113283949847762202M46316835694926478169428394003475163141307993866256225615783033603165251855960   l	   S9i@eM^w|o c                 C   s   t dt|  S )Nsha512)hashlibnewr
   digestdata r   _/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/ecdsa/eddsa.py_sha512(   s   r   T)	generatori     iWg   ڇ224580040295924300187604334099896036246789641632564134246125461686950415467406032909029192869357953282578032075146446173674602635247710ڇ298819210078481492676017930443930673437544040154080242095928241372331506189835876003536878655418784733982303233503462500531545062832660i  l   ;O)u9d
NG\cIoSDY%	c; c                 C   s
   t | dS )Nr   r   r   r   r   r   	_shake256I      
r#   c                   @   s\   e Zd ZdZdddZdd Zdd Zed	d
 Zej	dd
 Zdd Z
dd Zdd ZdS )	PublicKeyz7Public key for the Edwards Digital Signature Algorithm.Nc                 C   st   || _ | | _|| _t| j d d d | _t|| jkr(td| j|r/|| _	d S t
j| j|| _	d S )Nr      r   z5Incorrect size of the public key, expected: {0} bytes)r   curve_PublicKey__encodedr   pbaselenlen
ValueErrorformat_PublicKey__pointr   PointEdwards
from_bytes)selfr   
public_keypublic_pointr   r   r   __init__V   s   


zPublicKey.__init__c                 C   &   t |tr| j|jko| j|jkS tS N)
isinstancer%   r'   r(   NotImplementedr1   otherr   r   r   __eq__i   s   
zPublicKey.__eq__c                 C   
   | |k S r6   r   r9   r   r   r   __ne__p   r$   zPublicKey.__ne__c                 C      | j S r6   r.   r1   r   r   r   points      zPublicKey.pointc                 C   s   | j |kr	td|| _ d S )Nz)Can't change the coordinates of the point)r.   r,   r9   r   r   r   rA   w   s   

c                 C   r>   r6   r?   r@   r   r   r   r3   }      zPublicKey.public_pointc                 C   r>   r6   )r(   r@   r   r   r   r2      rC   zPublicKey.public_keyc                 C   s   t |}t|d| j krtdd| j tj| j|d| j }t	|| jd d}|| j
 kr9tdt }| jtkrEtd}t	| j||  | j | d}| j
| | j| | krgtddS )z(Verify a Pure EdDSA signature over data.r   z-Invalid signature length, expected: {0} bytesNlittlezInvalid signature
   SigEd448  T)r
   r+   r*   r,   r-   r   r/   r0   r'   r   r   order	bytearraycurve_ed448	hash_functo_bytesr(   r.   )r1   r   	signatureRSdomkr   r   r   verify   s.   
zPublicKey.verifyr6   )__name__
__module____qualname____doc__r4   r;   r=   propertyrA   setterr3   r2   rP   r   r   r   r   r%   S   s    


r%   c                   @   sL   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dd Z
dd ZdS )
PrivateKeyz8Private key for the Edwards Digital Signature Algorithm.c                 C   s   || _ | | _t| j d d d | _t|| jkr%td| jt|| _	t
| j|| _d | _| jd | j }| |}t|d}|| _d S )Nr   r&   r   z2Incorrect size of private key, expected: {0} bytesrD   )r   r'   r   r)   r*   r+   r,   r-   bytes_PrivateKey__private_keyrG   rI   _PrivateKey__h_PrivateKey__public_key
_key_pruner   _PrivateKey__s)r1   r   private_keyascalarr   r   r   r4      s    




zPrivateKey.__init__c                 C   r>   r6   )rY   r@   r   r   r   r^      rB   zPrivateKey.private_keyc                 C   r5   r6   )r7   rW   r'   rY   r8   r9   r   r   r   r;      s
   

zPrivateKey.__eq__c                 C   r<   r6   r   r9   r   r   r   r=      r$   zPrivateKey.__ne__c                 C   s   | j  }|dkrd}n|dkrd}ntd|d  d|> d  M  < t| j  }|d dkr?d|d< |d	  d
O  < |S |d d|d > d @ d|d d > B |d< |S )Nr   r   r      z&Only cofactor 4 and 8 curves supportedr   r   r      )r'   cofactorr,   r   r)   )r1   keyhh_loglr   r   r   r\      s   
,zPrivateKey._key_prunec                 C   s2   | j r| j S | j| j }t| j| || _ | j S )z9Generate the public key based on the included private key)r[   r   r]   r%   rJ   )r1   r3   r   r   r   r2      s   zPrivateKey.public_keyc           	      C   s   t |}|   }| j| jd }t }| jtkrtd}t| j|| | d}| j	| 
 }t| j|| | | d}|| j	 ; }||| j  | j	  }|t|| jd S )z)Perform a Pure EdDSA signature over data.NrE   rD   )r
   r2   rZ   r*   rG   r'   rH   r   rI   r   rJ   rF   r]   r	   )	r1   r   AprefixrN   rrL   rO   rM   r   r   r   sign   s   
zPrivateKey.signN)rQ   rR   rS   rT   r4   rU   r^   r;   r=   r\   r2   rl   r   r   r   r   rW      s    
rW   )rT   r   _sha3r    r   _compatr   r   r   r	   r
   _p_aint_d_h_Gx_Gy_rr   	CurveEdTwcurve_ed25519r/   generator_ed25519r#   rH   generator_ed448objectr%   rW   r   r   r   r   <module>   sf    	O