U
    Š¾úgë  ã                   @   s’   d dl mZmZ d dlmZmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZ dd„ Zdd„ Zd	d
„ Zdd„ ZG dd„ deƒZdd„ Zddd„ZdS )é    )ÚbchrÚconcat_buffers)ÚVoidPointerÚSmartPointerÚcreate_string_bufferÚget_raw_bufferÚc_size_tÚc_uint8_ptrÚc_ubyte)Úlong_to_bytes)Ú_raw_keccak_libc                 C   sD   | dd> k r| dkst ‚| dkr$dn|  ¡ d d }t|ƒt| ƒ S )z2Left encode function as defined in NIST SP 800-185é   éø  r   é   é   )ÚAssertionErrorÚ
bit_lengthr   r   ©ÚxÚnum© r   ú=/tmp/pip-unpacked-wheel-_q8s9isk/Cryptodome/Hash/cSHAKE128.pyÚ_left_encode+   s    r   c                 C   sD   | dd> k r| dkst ‚| dkr$dn|  ¡ d d }t| ƒt|ƒ S )z3Right encode function as defined in NIST SP 800-185r   r   r   r   r   )r   r   r   r   r   r   r   r   Ú_right_encode6   s    r   c                 C   s.   t | ƒd }|dd> kr tdƒ‚tt|ƒ| ƒS )z4Encode string function as defined in NIST SP 800-185r   r   r   z$String too large to encode in cSHAKE)ÚlenÚ
ValueErrorr   r   )r   Zbitlenr   r   r   Ú_encode_strA   s    r   c                 C   s.   t t|ƒ| ƒ}|t|ƒ|  | }|d|  S )z2Zero pad byte string as defined in NIST SP 800-185ó    )r   r   r   )r   ÚlengthZto_padZnpadr   r   r   Ú_bytepadK   s    r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú
cSHAKE_XOFz]A cSHAKE hash object.
    Do not instantiate directly.
    Use the :func:`new` function.
    c           	      C   s¨   t ƒ }|s|r8t|ƒt|ƒ }t|d| d ƒ}d| _n
d }d| _t | ¡ t|d ƒtdƒ¡}|rpt	d| ƒ‚t
| ¡ tjƒ| _d| _|r–|  |¡ |r¤|  |¡ d S )Ni@  r   é   é   é   z#Error %d while instantiating cSHAKEF)r   r   r   Ú_paddingr   Zkeccak_initZ
address_ofr   r
   r   r   ÚgetZkeccak_destroyÚ_stateÚ_is_squeezingÚupdate)	ÚselfÚdataÚcustomÚcapacityÚfunctionÚstateZprefix_unpadÚprefixÚresultr   r   r   Ú__init__]   s.    

þÿÿ
zcSHAKE_XOF.__init__c                 C   sH   | j rtdƒ‚t | j ¡ t|ƒtt|ƒƒ¡}|rDt	d|| j
f ƒ‚| S )z¼Continue hashing of a message by consuming the next chunk of data.

        Args:
            data (byte string/byte array/memoryview): The next chunk of the message being hashed.
        z/You cannot call 'update' after the first 'read'z Error %d while updating %s state)r'   Ú	TypeErrorr   Zkeccak_absorbr&   r%   r	   r   r   r   Úname)r)   r*   r0   r   r   r   r(   x   s    
þÿzcSHAKE_XOF.updatec                 C   sL   d| _ t|ƒ}t | j ¡ |t|ƒt| jƒ¡}|rDt	d|| j
f ƒ‚t|ƒS )ah  
        Compute the next piece of XOF output.

        .. note::
            You cannot use :meth:`update` anymore after the first call to
            :meth:`read`.

        Args:
            length (integer): the amount of bytes this method must return

        :return: the next piece of XOF output (of the given length)
        :rtype: byte string
        Tz!Error %d while extracting from %s)r'   r   r   Zkeccak_squeezer&   r%   r   r
   r$   r   r3   r   )r)   r   Zbfrr0   r   r   r   ÚreadŠ   s    ýÿzcSHAKE_XOF.readN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r1   r(   r4   r   r   r   r   r    W   s   r    c                 C   s   t | |d|ƒS )Né   ©r    )r*   r+   r-   r   r   r   Ú_new¦   s    r;   Nc                 C   s   t | |ddƒS )az  Return a fresh instance of a cSHAKE128 object.

    Args:
       data (bytes/bytearray/memoryview):
        Optional.
        The very first chunk of the message to hash.
        It is equivalent to an early call to :meth:`update`.
       custom (bytes):
        Optional.
        A customization bytestring (``S`` in SP 800-185).

    :Return: A :class:`cSHAKE_XOF` object
    r9   ó    r:   )r*   r+   r   r   r   Únew«   s    r=   )NN)ZCryptodome.Util.py3compatr   r   ZCryptodome.Util._raw_apir   r   r   r   r   r	   r
   ZCryptodome.Util.numberr   ZCryptodome.Hash.keccakr   r   r   r   r   Úobjectr    r;   r=   r   r   r   r   Ú<module>   s   $
O