o
    Ìn~b½  ã                   @   sŽ   d dl 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)Ú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J ‚| dkrdn|   ¡ d d }t|ƒt| ƒ S )z2Left encode function as defined in NIST SP 800-185é   éø  r   é   é   )Ú
bit_lengthr   r
   ©ÚxÚnum© r   úD/usr/local/lib/python3.10/dist-packages/Cryptodome/Hash/cSHAKE128.pyÚ_left_encode+   ó   r   c                 C   sD   | dd> k r
| dksJ ‚| 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   Ú_right_encode6   r   r   c                 C   s,   t | ƒd }|dd> krtdƒ‚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   Zbitlenr   r   r   Ú_encode_strA   s   r   c                 C   s,   t |ƒ|  }|t|ƒ|  | }|d|  S )z2Zero pad byte string as defined in NIST SP 800-185ó    )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|rt|ƒt|ƒ }t|d| d ƒ}d| _nd }d| _t | ¡ t|d ƒtdƒ¡}|r8t	d| ƒ‚t
| ¡ tjƒ| _d| _|rK|  |¡ |rT|  |¡ d S 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__]   s0   

þÿÿ
ÿzcSHAKE_XOF.__init__c                 C   sH   | j rtdƒ‚t | j ¡ t|ƒtt|ƒƒ¡}|r"t	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)   r/   r   r   r   r'   x   s   
þÿzcSHAKE_XOF.updatec                 C   sL   d| _ t|ƒ}t | j ¡ |t|ƒt| jƒ¡}|r"t	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   r2   r   )r(   r   Zbfrr/   r   r   r   ÚreadŠ   s   ýÿzcSHAKE_XOF.readN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   r'   r3   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
    r8   ó    r9   )r)   r*   r   r   r   Únew«   s   r<   )NN)ZCryptodome.Util.py3compatr   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