
    ei                         d Z d Zd Zd ZdS )u  keccakf1600.py

Keccak is a family of hash functions based on the sponge construction. It was
chosen by NIST to become the SHA-3 standard. This code implements the
Keccak-f[1600] permutation. Detailed information about this function can be
found on the official site [1]. Original implementation [2] by the Keccak Team.

Some caveats about the implementation:
    * width `b` of permutation is fixed as 1600 (5 * 5 * 64), which means
    the number of rounds is 24 (12 + 2ℓ, ℓ = log_2(b / 25))
    * ρ step could have its offsets pre-computed as the array `r`.
    * ι step could have its round constants pre-computed as the array `RC`.

[1] http://keccak.noekeon.org/
[2] https://git.io/vKfkb
c                    d }d }d d t          d          D             t          d          D ]>}t          d          D ],d|dz  z   z  } || ||dz                      |         <   -?d}t          d          D ]u}fd	t          d          D             fd
t          d          D             fdt          d          D             ddd         d         c}}t          d          D ]E}d|z  dz  z   dz  c}|dz   |dz   z  dz  }	|                   ||	          c}|         <   Ft          d          D ]`fdt          d          D             }
t          d          D ]2}|
|         |
|dz   dz            |
|dz   dz           z  z  |         <   3at          d          D ]7}|dz  |dz	  dz  z  dz  }|dz  rd         dxx         dd|z  dz
  z  z  cc<   8wt          d          D ]>}t          d          D ],d|dz  z   z  } ||                            | ||dz   <   -?| S )u  The inner permutation for the Keccak sponge function.

    The Keccak-f permutation is an iterated construction consisting of a
    sequence of almost identical rounds. It operates on a state array with
    each of the twenty-four rounds performing five steps, described below
    with detail.

    The loops above and below the core of the permutation are used to save and
    restore the state array to a stream of bytes, used outside the permutation.
    The original state array has three dimensions, whereas this characteristic
    can be cleverly optimized to a 5x5 matrix with 64-bit words. As such, this
    implementation makes use of this trick and stores an entire lane (a z-axis
    set of bits within the state) as a single word.

    The θ step diffuses the bits alongside the state array by calculating the
    parity of nearby columns relative to a lane.

    The ρ and π steps are merged; together, they move more bits around
    according to two alike recurrence relations.

    The χ step is similar to an S-box permutation; it makes the whole round
    non-linear with a few logic operations on bits inside a line.

    The ι step is a simple LFSR that breaks the symmetry of the rounds. It
    generates constants by doing computations according to the round number
    and its previous output, modulo polynomials over GF(2)[x].

    Args:
        state:  square matrix of order 5 that holds the input bytes.

    Returns:
        state:  bytes permuted by Keccak-f[1600].
    c                 T     t           fdt          d          D                       S )z
        Saves each byte on its respective position within a 64-bit word.

        Args:
            b:  partial list of bytes from input.

        Returns:
            Sum of list with numbers shifted.
        c              3   4   K   | ]}|         d |z  z  V  dS )   N ).0ibs     d/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/static_dependencies/keccak/keccak.py	<genexpr>z0keccak_f_1600.<locals>.load64.<locals>.<genexpr>C   s/      77AaDQUO777777    r   )sumrange)r	   s   `r
   load64zkeccak_f_1600.<locals>.load649   s.     7777eAhh777777r   c                 T     t           fdt          d          D                       S )z
        Transforms a 64-bit word into a list of bytes.

        Args:
            a:  64-bit word.

        Returns:
            List of bytes separated by position on the word.
        c              3   .   K   | ]}d |z  z	  dz  V  dS )r      Nr   )r   r   as     r
   r   z1keccak_f_1600.<locals>.store64.<locals>.<genexpr>O   s0      ;;QQ1q5\S(;;;;;;r   r   )listr   )r   s   `r
   store64zkeccak_f_1600.<locals>.store64E   s.     ;;;;%((;;;;;;r   c                 0    | d|dz  z
  z	  | |dz  z  z   dz  S )a=  
        Denotes the bitwise cyclic shift operation, moving bit at position
        `i` into position `i + n` (modulo the lane size).

        Args:
            a:  lane with a 64-bit word, or elements from the state array.
            n:  offset for rotation.

        Returns:
            The rotated lane.
        @   l            r   )r   ns     r
   rotatezkeccak_f_1600.<locals>.rotateQ   s)     rQV}%!B-8WEEr   c                 @    g | ]}d  t          d          D             S )c                     g | ]}d S )    r   r   _s     r
   
<listcomp>z,keccak_f_1600.<locals>.<listcomp>.<listcomp>_   s    			!			r      r   r   s     r
   r   z!keccak_f_1600.<locals>.<listcomp>_   s-    1111		U1XX			111r   r    r         c                     g | ]L}|         d          |         d         z  |         d         z  |         d         z  |         d         z  MS )r   r"            r   )r   xAs     r
   r   z!keccak_f_1600.<locals>.<listcomp>g   sU    OOOQqT!WqtAw1a(1Q472QqT!W<OOOr   c                 ^    g | ])}|d z
  dz            |d z   dz           d           z  *S )r"   r    r   )r   r(   Cr   s     r
   r   z!keccak_f_1600.<locals>.<listcomp>h   sB    JJJAQA{^ffQA{^Q777JJJr   c                 L    g | ]fd t          d          D              S )c                 >    g | ]}         |                  z  S r   r   )r   yr)   Dr(   s     r
   r   z,keccak_f_1600.<locals>.<listcomp>.<listcomp>i   s)    ///ad1g!n///r   r    r!   )r   r(   r)   r/   s    @r
   r   z!keccak_f_1600.<locals>.<listcomp>i   s;    BBBA//////eAhh///BBBr   r   r%   r&   c                 ,    g | ]}|                  S r   r   )r   r(   r)   r.   s     r
   r   z!keccak_f_1600.<locals>.<listcomp>r   s!    +++Q1a+++r      q   r   r!   )stater   r   r(   r   Rr   currenttoffsetTjr)   r+   r/   r   r.   s               @@@@@r
   keccak_f_1600r:      s+   F
8 
8 
8
< 
< 
<F F F 	21a111A1XX / /q 	/ 	/AQQYAfU1q1u9-..AaDGG	/ 	
A2YY / /OOOOeAhhOOOJJJJJqJJJBBBBBqBBB1ad1g1gr 	@ 	@Aq1uq1u})DAq1uQ'A-F tAww(?(?GQqT!WWq 	F 	FA+++++%((+++A1XX F FA$Aq1ukN?aQ!n"DE!QF q 	/ 	/Aq&a1f_-4A1u /!Q1!q&A..	/
 1XX 0 0q 	0 	0AQQYA&wqtAw//E!a!e)	0 Lr   c                    t          | |z   dz            }| dz  dd}}}|t          |          k     rzt          t          |          |z
  |          }t          |          D ]}	||	xx         ||	|z            z  cc<   ||z  }||k    rt	          |          }d}|t          |          k     z||xx         |z  cc<   |dz  r||dz
  k    rt	          |          }||dz
  xx         dz  cc<   t	          |          }t                      }
|r5t          ||          }|
|d|         z  }
||z  }|rt	          |          }|5|
S )a  
    The general sponge function, consisting of the inner permutation and a
    padding rule (`pad10*1`). It consists of three main parts.
        * absorbing, where the input will be permuted repeatedly every time
            it is divided into a block of size `r + c`.
        * padding, where an oddly sized last block will be filled with bits
            until it can be fed into the permutation.
        * squeezing, where the output's blocks will be permuted more times
            until they are concatenated to the desired size.

    Args:
        r:          rate, or the number of input bits processed or output bits
                    generated per invocation of the underlying function
        c:          capacity, or the width of the underlying function minus
                    the rate
        _input:     list of bytes containing the desired object to be hashed
        suffix:     distinguishes the inputs arising from SHA-3/SHAKE functions
        output_len: length of hash output.

    Returns:
        Hash of the input bytes.
    r   r      r"   N)	bytearraylenminr   r:   )rc_inputsuffix
output_lenr3   
rate_bytesblockr7   r   outputs              r
   KeccakrH      s   . q1ul##E !Q1vJ
3v;;

CKK&(*55u 	+ 	+A!HHHq6z**HHHH%J!%((EE 3v;;

 
%LLLFLLL %Ej1n55e$$	*q.T!%  E[[F
 )J
++%-e
 	)!%((E  ) Mr   c                 H    d}d}t          d|dz  z
  |dz  | ||dz            S )a  
    FIPS 202 generalized instance of the SHA-3 hash function.

    Args:
        size:   instance of desired SHA3 algorithm.
        _input: list of bytes to compute a hash from.

    Returns:
        Instance of the Keccak permutation that calculates the hash.
    r   r"   i@  r%   r   )rH   )rB   sizepaddings      r
   SHA3rL      s4     DG$/4!8VWdaiHHHr   N)__doc__r:   rH   rL   r   r   r
   <module>rN      sP    $j j jZ1 1 1hI I I I Ir   