
    ,h#                     p    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y)    )is_native_int)number)long_to_bytesbytes_to_long)get_random_bytesc                 N    || kD  r|| }} d}|r|dz  r|| z  }| dz  } |dz  }|r|S )z!Multiply two polynomials in GF(2)r       )f1f2zs      _/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/Crypto/Protocol/SecretSharing.py	_mult_gf2r   (   sJ     
BwRB	A
6GA
q
q	 
 H    c                     | |k  rd| fS t         j                  }d}| } ||      } ||      |k\  r.d ||      |z
  z  }||z  }|t        ||      z  } ||      |k\  r.||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                   P    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y)_ElementzElement of GF(2^128) field         c                 |    t        |      r|| _        yt        |      dk(  rt        |      | _        y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__T   s8     ''DK2%'6DKWXXr   c                 4    | j                   |j                   k(  S Nr"   )r%   others     r   __eq__z_Element.__eq__c   s    {{ell**r   c                     | j                   S )z7Return the field element, encoded as a 128-bit integer.r*   r%   s    r   __int__z_Element.__int__f   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.encodej   s    T[["--r   c                    | j                   }|j                   }||kD  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  }|rt        |      S )Nr   r   r   r	   )base)r"   irr_polyr   intbin)	r%   factorr   r   mask1vr   mask2mask3s	            r   __mul__z_Element.__mul__n   s   [[]] 7B==RH$A;S11BFAB#-A6E!a%eema&71%<=A!GAQ#XN+AB/#5A>E!dmm+,%%-!2Cq1HIA1HB  {r   c                 F    t        | j                  |j                  z        S r)   )r   r"   )r%   terms     r   __add__z_Element.__add__   s    dkk122r   c                     | j                   dk(  rt        d      | j                   | j                  }}d\  }}|dkD  r7t        ||      d   }||t	        ||      z  }}||t	        ||      z  }}|dkD  r7t        |      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dmmBB1fR #Ai2..Bi2..B 1f |r   c                 `    t        | j                        }t        |dz
        D ]  }|| z  }	 |S )Nr	   )r   r"   range)r%   exponentresult_s       r   __pow__z_Element.__pow__   s6    $++&x!|$ 	#Ad]F	#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+.23"r   r   c                   4    e Zd ZdZedd       Zedd       Zy)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.
    c           	          t        | dz
        D cg c]  }t        t        d             }}|j                  t        |             d }t        d|dz         D cg c]  }| ||||      f c}S c c}w c c}w )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 number of shares needed to reconstruct the secret.
          n (integer):
            The number of shares to create (at least ``k``).
          secret (byte string):
            A byte string of 16 bytes (e.g. an AES 128 key).
          ssss (bool):
            If ``True``, the shares can be used with the ``ssss`` utility
            (without using the "diffusion layer").
            Default: ``False``.

        Return (tuples):
            ``n`` tuples, one per participant.
            A tuple contains two items:

            1. the unique index (an integer)
            2. the share (16 bytes)
        r	   r!   c                     t        |       }t        d      }|D ]
  }||z  |z   } |r|t        |       t        |      z  z  }|j                         S )Nr   )r   r#   r1   )usercoeffsssssidxsharecoeffs         r   
make_sharez Shamir.split.<locals>.make_share   s[    4.C QKE ,ee+, $3v;66<<>!r   )rG   r   rngappend)knsecretrV   irU   rZ   s          r   splitzShamir.split   sv    N .31q5\:(3r7#::hv&'
	"" ;@1q5/JQJq&$/0JJ/ ;. Ks   A2A7c                    t        |       }g }| D ]Z  }t        |d         t        |d         }t        fd|D              rt        d      |r||z  z  }|j	                  |f       \ t        d      }t        |      D ]d  }||   \  }}	t        d      }
t        d      }t        |      D ]  }||   d   }||k7  s|
|z  }
|||z   z  } ||	|
z  |j                         z  z  }f |j                         S )a  Recombine a secret, if enough shares are presented.

        Args:
          shares (tuples):
            The *k* tuples, each containing the index (an integer) and
            the share (a byte string, 16 bytes long) that were assigned to
            a participant.

            .. note::

                Pass exactly as many share as they are required,
                and no more.

          ssss (bool):
            If ``True``, the shares were produced by the ``ssss`` utility
            (without using the "diffusion layer").
            Default: ``False``.

        Return:
            The original secret, as a byte string (16 bytes long).
        r   r	   c              3   .   K   | ]  }|d    k(    yw)r   Nr
   ).0yrW   s     r   	<genexpr>z!Shamir.combine.<locals>.<genexpr>  s     211Q43;2s   zDuplicate share)r#   r   anyr$   r\   rG   rE   r1   )sharesrV   r]   	gf_sharesxvaluerI   jx_jy_j	numeratordenominatormx_mrW   s                 @r   combinezShamir.combine   s"   L K	 	+A1Q4.CQqTNE2	22 !233!c5\*	+ !q 	>A |HC I"1+K1X -l1o6$I39,K	-
 cIo(;(;(===F	> }}r   N)F)rL   rM   rN   rO   staticmethodra   rs   r
   r   r   rQ   rQ      s2     =K =K~ ? ?r   rQ   N)Crypto.Util.py3compatr   Crypto.Utilr   Crypto.Util.numberr   r   Crypto.Randomr   r[   r   r   objectr   rQ   r
   r   r   <module>rz      s;   D 0  ; 1,Rv RjGV Gr   