U
    ccf(                     @   sv  U d dl mZ d dlmZ d dlmZmZ d dlm	Z	 e
 Zeed< e d Zeed< e Zeed< e Zeed	< e Zeed
< e Zeed< eeef dddZeeeef dddZeeedddZeeedddZeedddZeedddZeedddZeedd d!ZG d"d# d#Z e ed$d%d&d'Z!e eed(d)d*Z"e eee#d+d,d-Z$d$S ).    )Tuple)
exceptions)ffilib)ensurecrypto_sign_BYTES   crypto_sign_SEEDBYTEScrypto_sign_PUBLICKEYBYTEScrypto_sign_SECRETKEYBYTEScrypto_sign_curve25519_BYTES crypto_sign_ed25519ph_STATEBYTESreturnc                  C   s`   t dt} t dt}t| |}t|dkdtjd t 	| tdd t 	|tdd fS )zu
    Returns a randomly generated public key and secret key.

    :rtype: (bytes(public_key), bytes(secret_key))
    unsigned char[]r   Unexpected library errorZraisingN)
r   newr
   r   r   crypto_sign_keypairr   excRuntimeErrorbuffer)pkskrc r   =/tmp/pip-unpacked-wheel-e6sdgn5m/nacl/bindings/crypto_sign.pyr       s    r   )seedr   c                 C   sx   t | tkrtdtdt}tdt}t	||| }t
|dkdtjd t|tdd t|tdd fS )z
    Computes and returns the public key and secret key using the seed ``seed``.

    :param seed: bytes
    :rtype: (bytes(public_key), bytes(secret_key))
    zInvalid seedr   r   r   r   N)lenr	   r   
ValueErrorr   r   r
   r   r   crypto_sign_seed_keypairr   r   r   )r   r   r   r   r   r   r   r    2   s    
r    )messager   r   c                 C   s`   t dt| t }t d}t||| t| |}t|dkdtjd t 	||d dd S )z
    Signs the message ``message`` using the secret key ``sk`` and returns the
    signed message.

    :param message: bytes
    :param sk: bytes
    :rtype: bytes
    r   unsigned long long *r   r   r   N)
r   r   r   r   r   crypto_signr   r   r   r   )r!   r   signedZ
signed_lenr   r   r   r   r#   H   s
    	
r#   )r$   r   r   c                 C   sV   t dt| }t d}t||| t| |dkr>tdt ||d dd S )z
    Verifies the signature of the signed message ``signed`` using the public
    key ``pk`` and returns the unsigned message.

    :param signed: bytes
    :param pk: bytes
    :rtype: bytes
    r   r"   r   Signature was forged or corruptN)r   r   r   r   crypto_sign_openr   BadSignatureErrorr   )r$   r   r!   Zmessage_lenr   r   r   r&   Z   s    	

r&   )public_key_bytesr   c                 C   sZ   t | tkrtdt}td|}t|| }t	|dkdtj
d t||dd S )a  
    Converts a public Ed25519 key (encoded as bytes ``public_key_bytes``) to
    a public Curve25519 key as bytes.

    Raises a ValueError if ``public_key_bytes`` is not of length
    ``crypto_sign_PUBLICKEYBYTES``

    :param public_key_bytes: bytes
    :rtype: bytes
    zInvalid curve public keyr   r   r   r   N)r   r
   r   r   r   r   r   r   $crypto_sign_ed25519_pk_to_curve25519r   r   r   )r(   Zcurve_public_key_lenZcurve_public_keyr   r   r   r   r)   o   s    
 r)   )secret_key_bytesr   c                 C   sZ   t | tkrtdt}td|}t|| }t	|dkdtj
d t||dd S )a  
    Converts a secret Ed25519 key (encoded as bytes ``secret_key_bytes``) to
    a secret Curve25519 key as bytes.

    Raises a ValueError if ``secret_key_bytes``is not of length
    ``crypto_sign_SECRETKEYBYTES``

    :param secret_key_bytes: bytes
    :rtype: bytes
    zInvalid curve secret keyr   r   r   r   N)r   r   r   r   r   r   r   r   $crypto_sign_ed25519_sk_to_curve25519r   r   r   )r*   Zcurve_secret_key_lenZcurve_secret_keyr   r   r   r   r+      s    
 r+   c                 C   s"   t | tkrtd| td S )a	  
    Extract the public Ed25519 key from a secret Ed25519 key (encoded
    as bytes ``secret_key_bytes``).

    Raises a ValueError if ``secret_key_bytes``is not of length
    ``crypto_sign_SECRETKEYBYTES``

    :param secret_key_bytes: bytes
    :rtype: bytes
    Invalid secret keyNr   r   r   r   r	   r*   r   r   r   crypto_sign_ed25519_sk_to_pk   s    
r/   c                 C   s"   t | tkrtd| dt S )z
    Extract the seed from a secret Ed25519 key (encoded
    as bytes ``secret_key_bytes``).

    Raises a ValueError if ``secret_key_bytes``is not of length
    ``crypto_sign_SECRETKEYBYTES``

    :param secret_key_bytes: bytes
    :rtype: bytes
    r,   Nr-   r.   r   r   r   crypto_sign_ed25519_sk_to_seed   s    
r0   c                   @   s$   e Zd ZdZdgZddddZdS )crypto_sign_ed25519ph_statezO
    State object wrapping the sha-512 state used in ed25519ph computation
    stateNr   c                 C   s2   t dt| _t| j}t|dkdtjd d S )Nr   r   r   r   )	r   r   r   r2   r   Zcrypto_sign_ed25519ph_initr   r   r   )selfr   r   r   r   __init__   s     z$crypto_sign_ed25519ph_state.__init__)__name__
__module____qualname____doc__	__slots__r4   r   r   r   r   r1      s   r1   N)edphpmsgr   c                 C   sX   t t| tdtjd t t|tdtjd t| j|t	|}t |dkdtj
d dS )z
    Update the hash state wrapped in edph

    :param edph: the ed25519ph state being updated
    :type edph: crypto_sign_ed25519ph_state
    :param pmsg: the partial message
    :type pmsg: bytes
    :rtype: None
    /edph parameter must be a ed25519ph_state objectr   z%pmsg parameter must be a bytes objectr   r   N)r   
isinstancer1   r   	TypeErrorbytesr   crypto_sign_ed25519ph_updater2   r   r   )r:   r;   r   r   r   r   r@      s    r@   )r:   r   r   c                 C   s   t t| tdtjd t t|tdtjd t t|tkdttjd t	
dt}t| j|t	j|}t |dkdtjd t	|tdd S )	aR  
    Create a signature for the data hashed in edph
    using the secret key sk

    :param edph: the ed25519ph state for the data
                 being signed
    :type edph: crypto_sign_ed25519ph_state
    :param sk: the ed25519 secret part of the signing key
    :type sk: bytes
    :return: ed25519ph signature
    :rtype: bytes
    r<   r   z+secret key parameter must be a bytes objectz secret key must be {} bytes longr   r   r   N)r   r=   r1   r   r>   r?   r   r   formatr   r   r   r   "crypto_sign_ed25519ph_final_creater2   ZNULLr   r   )r:   r   	signaturer   r   r   r   rB      s4    
   rB   )r:   rC   r   r   c                 C   s   t t| tdtjd t t|tdtjd t t|tkdttjd t t|tdtjd t t|t	kdt	tjd t
| j||}|dkrtdd	S )
a  
    Verify a prehashed signature using the public key pk

    :param edph: the ed25519ph state for the data
                 being verified
    :type edph: crypto_sign_ed25519ph_state
    :param signature: the signature being verified
    :type signature: bytes
    :param pk: the ed25519 public part of the signing key
    :type pk: bytes
    :return: True if the signature is valid
    :rtype: boolean
    :raises exc.BadSignatureError: if the signature is not valid
    r<   r   z*signature parameter must be a bytes objectzsignature must be {} bytes longz+public key parameter must be a bytes objectz public key must be {} bytes longr   r%   T)r   r=   r1   r   r>   r?   r   r   rA   r
   r   "crypto_sign_ed25519ph_final_verifyr2   r'   )r:   rC   r   r   r   r   r   rD     s>    


rD   )%typingr   Znaclr   r   Znacl._sodiumr   r   Znacl.exceptionsr   Zcrypto_sign_bytesr   int__annotations__Zcrypto_sign_secretkeybytesr	   Zcrypto_sign_publickeybytesr
   r   Zcrypto_box_secretkeybytesr   Z crypto_sign_ed25519ph_statebytesr   r?   r   r    r#   r&   r)   r+   r/   r0   r1   r@   rB   boolrD   r   r   r   r   <module>   s@      *  