
    ¿igZ"                     ~    d dl mZ d dlmZ d dlmZmZ d dlmZ	 d Z
d Z G d de          Z G d	 d
e          ZdS )    )is_native_int)number)long_to_bytesbytes_to_long)get_random_bytesc                 N    || k    r|| }} d}|r|dz  r|| z  }| dz  } |dz  }||S )z!Multiply two polynomials in GF(2)r       )f1f2zs      ]/var/www/html/afkarena/venv/lib/python3.11/site-packages/Cryptodome/Protocol/SecretSharing.py	_mult_gf2r   (   s[     
BwwRB	A
 6 	GA
q
q	  
 H    c                     | |k     rd| fS t           j        }d}| } ||          } ||          |k    r8d ||          |z
  z  }||z  }|t          ||          z  } ||          |k    8||fS )z
    Compute division of polynomials over GF(2).
    Given a and b, it finds two polynomials q and r such that:

    a = b*q + r with deg(r)<deg(b)
    r   r	   )r   sizer   )abdegqrdss          r   _div_gf2r   7   s     	
A!t
+C	A	AAA
#a&&A++##a&&1*	Q	Yq!__ #a&&A++ q6Mr   c                   R    e Zd ZdZdddz  z   Zd Zd Zd Zd Zd	 Z	d
 Z
d Zd ZdS )_ElementzElement of GF(2^128) field         c                     t          |          r	|| _        dS t          |          dk    rt          |          | _        dS t	          d          )zInitialize the element to a certain value.

        The value passed as parameter is internally encoded as
        a 128-bit integer, where each bit represents a polynomial
        coefficient. The LSB is the constant coefficient.
           z8The encoded value must be an integer or a 16 byte stringN)r   _valuelenr   
ValueError)selfencoded_values     r   __init__z_Element.__init__S   sU     '' 	Y'DKKK2%%'66DKKKWXXXr   c                 "    | j         |j         k    S Nr"   )r%   others     r   __eq__z_Element.__eq__b   s    {el**r   c                     | j         S )z7Return the field element, encoded as a 128-bit integer.r*   r%   s    r   __int__z_Element.__int__e   s
    {r   c                 ,    t          | j        d          S )z6Return the field element, encoded as a 16 byte string.r!   )r   r"   r.   s    r   encodez_Element.encodei   s    T["---r   c                    | j         }|j         }||k    r||}}| j        ||fv rt          d          S ddz  }|d}}|rt          t	          |dz            dd          dz  d          }|||z  z  ||z
  dz
  |z  z  }|dz  }t          t	          |dz	  dz            dd          dz  d          }||| j        z  z  ||z
  dz
  |z  z  }|dz  }|t          |          S )Nr   r   r   r	   )base)r"   irr_polyr   intbin)	r%   factorr   r   mask1vr   mask2mask3s	            r   __mul__z_Element.__mul__m   s"   [] 77B=RH$$A;;S11 	BFABB#-A666E!a%eema&71%<=A!GAQ#XN++ABB/#5A>>>E!dm+,%%-!2Cq1HIA1HB  	 {{r   c                 :    t          | j        |j        z            S r)   )r   r"   )r%   terms     r   __add__z_Element.__add__   s    dk1222r   c                    | j         dk    rt          d          | j         | j        }}d\  }}|dk    rFt          ||          d         }||t	          ||          z  }}||t	          ||          z  }}|dk    Ft          |          S )z0Return the inverse of this element in GF(2^128).r   zInversion of zero)r	   r   )r"   r$   r4   r   r   r   )r%   r0r1s0s1r   s         r   inversez_Element.inverse   s     ;!0111dmBB1ffR  #Ai2...Bi2...B 1ff ||r   c                 b    t          | j                  }t          |dz
            D ]}|| z  }|S )Nr	   )r   r"   range)r%   exponentresult_s       r   __pow__z_Element.__pow__   s<    $+&&x!|$$ 	# 	#Ad]FFr   N)__name__
__module____qualname____doc__r4   r'   r,   r/   r1   r<   r?   rE   rK   r
   r   r   r   r   M   s        $$ c)HY Y Y+ + +  . . .  23 3 3  "    r   r   c                   B    e Zd ZdZedd            Zedd            ZdS )ShamirzShamir's secret sharing scheme.

    A secret is split into ``n`` shares, and it is sufficient to collect
    ``k`` of them to reconstruct the secret.
    Fc                     d t          | dz
            D                                 t          |                     d fdt          d|dz             D             S )a  Split a secret into ``n`` shares.

        The secret can be reconstructed later using just ``k`` shares
        out of the original ``n``.
        Each share must be kept confidential to the person it was
        assigned to.

        Each share is associated to an index (starting from 1).

        Args:
          k (integer):
            The sufficient number of shares to reconstruct the secret (``k < n``).
          n (integer):
            The number of shares that this method will create.
          secret (byte string):
            A byte string of 16 bytes (e.g. the AES 128 key).
          ssss (bool):
            If ``True``, the shares can be used with the ``ssss`` utility.
            Default: ``False``.

        Return (tuples):
            ``n`` tuples. A tuple is meant for each participant and it contains two items:

            1. the unique index (an integer)
            2. the share (a byte string, 16 bytes)
        c                 F    g | ]}t          t          d                     S )r!   )r   rng).0is     r   
<listcomp>z Shamir.split.<locals>.<listcomp>   s&    :::(3r77##:::r   r	   c                     t          |           }t          d          }|D ]
}||z  |z   }|r"|t          |           t          |          z  z  }|                                S )Nr   )r   r#   r1   )usercoeffsssssidxsharecoeffs         r   
make_sharez Shamir.split.<locals>.make_share   sh    4..CQKKE , ,ee+ 7$3v;;66<<>>!r   c                 .    g | ]}| |          fS r
   r
   )rU   rV   rZ   r_   r[   s     r   rW   z Shamir.split.<locals>.<listcomp>   s,    JJJQJJq&$//0JJJr   )rG   appendr   )knsecretr[   rZ   r_   s      `@@r   splitzShamir.split   s}    J ;:U1q5\\:::hv&&'''
	" 	" 	" KJJJJJ%1q5//JJJJr   c                 v   t          |           }g }| D ]w}t          |d                   t          |d                   }t          fd|D                       rt          d          |r||z  z  }|                    |f           xt          d          }t          |          D ]{}||         \  }}	t          d          }
t          d          }t          |          D ]#}||         d         }||k    r|
|z  }
|||z   z  }$||	|
z  |                                z  z  }||                                S )a  Recombine a secret, if enough shares are presented.

        Args:
          shares (tuples):
            The *k* tuples, each containin the index (an integer) and
            the share (a byte string, 16 bytes long) that were assigned to
            a participant.
          ssss (bool):
            If ``True``, the shares were produced by the ``ssss`` utility.
            Default: ``False``.

        Return:
            The original secret, as a byte string (16 bytes long).
        r   r	   c              3   0   K   | ]}|d          k    V  dS )r   Nr
   )rU   yr\   s     r   	<genexpr>z!Shamir.combine.<locals>.<genexpr>  s+      2211Q43;222222r   zDuplicate share)r#   r   anyr$   ra   rG   rE   r1   )sharesr[   rb   	gf_sharesxvaluerI   jx_jy_j	numeratordenominatormx_mr\   s                 @r   combinezShamir.combine   s]   > KK	 	+ 	+A1Q4..CQqTNNE2222	22222 4 !2333 "!c5\****!q 	> 	>A |HC I"1++K1XX - -l1o66$I39,KcIo(;(;(=(===FF}}r   N)F)rL   rM   rN   rO   staticmethodre   rv   r
   r   r   rQ   rQ      sc          3K 3K 3K \3Kj 7 7 7 \7 7 7r   rQ   N)Cryptodome.Util.py3compatr   Cryptodome.Utilr   Cryptodome.Util.numberr   r   Cryptodome.Randomr   rT   r   r   objectr   rQ   r
   r   r   <module>r}      s   D 4 3 3 3 3 3 " " " " " " ? ? ? ? ? ? ? ? 5 5 5 5 5 5    ,Q Q Q Q Qv Q Q Qhu u u u uV u u u u ur   