
    i#                     p    S SK Jr  S SKJr  S SKJrJr  S SKJr	  S r
S r " S S\5      r " S	 S
\5      rg)    )is_native_int)number)long_to_bytesbytes_to_long)get_random_bytesc                 h    X:  a  XpSnU(       a!  US-  (       a  X -  nU S-  n US-  nU(       a  M!  U$ )z!Multiply two polynomials in GF(2)r       )f1f2zs      X/var/www/html/trading/venv/lib/python3.13/site-packages/Crypto/Protocol/SecretSharing.py	_mult_gf2r   (   sD     
wB	A
6GA
q
q	 "
 H    c                     X:  a  SU 4$ [         R                  nSnU nU" U5      nU" U5      U:  a.  SU" U5      U-
  -  nX6-  nU[        X5      -  nU" U5      U:  a  M.  X44$ )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   sv     	
!t
++C	A	AAA
a&A+#a&1*		Yq_ a&A+ 6Mr   c                   X    \ rS rSrSrSSS-  -   rS rS rS rS	 r	S
 r
S rS rS rSrg)_ElementM   zElement of GF(2^128) field         c                     [        U5      (       a  Xl        g[        U5      S:X  a  [        U5      U l        g[	        S5      e)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___Element.__init__T   s9     '''K2%'6DKWXXr   c                 4    U R                   UR                   :H  $ Nr#   )r&   others     r   __eq___Element.__eq__c   s    {{ell**r   c                     U R                   $ )z7Return the field element, encoded as a 128-bit integer.r,   r&   s    r   __int___Element.__int__f   s    {{r   c                 .    [        U R                  S5      $ )z6Return the field element, encoded as a 16 byte string.r"   )r   r#   r1   s    r   encode_Element.encodej   s    T[["--r   c                    U R                   nUR                   nX2:  a  X2p2U R                  X#4;   a  [        S5      $ SS-  nUSpeU(       a|  [        [	        US-  5      SS  S-  SS9nXvU-  -  XG-
  S-
  U-  -  nUS-  n[        [	        US-	  S-  5      SS  S-  SS9nXU R                  -  -  XH-
  S-
  U-  -  nUS-  nU(       a  M|  [        U5      $ )Nr   r   r    r	   )base)r#   irr_polyr   intbin)	r&   factorr   r   mask1vr   mask2mask3s	            r   __mul___Element.__mul__n   s    [[]] 7==RH$A;S11BFAB#-A6Ea%ema&71%<=A!GAQ#XN+AB/#5A>Edmm+,%-!2Cq1HIA1HB b {r   c                 F    [        U R                  UR                  -  5      $ r+   )r   r#   )r&   terms     r   __add___Element.__add__   s    dkk122r   c                     U R                   S:X  a  [        S5      eU R                   U R                  p!Su  p4US:  a2  [        X5      S   nX![	        XR5      -  p!XC[	        XT5      -  pCUS:  a  M2  [        U5      $ )z0Return the inverse of this element in GF(2^128).r   zInversion of zero)r	   r   )r#   r%   r9   r   r   r   )r&   r0r1s0s1r   s         r   inverse_Element.inverse   s{     ;;!011dmmB1f #Ai..i.. 1f |r   c                 b    [        U R                  5      n[        US-
  5       H  nX -  nM	     U$ )Nr	   )r   r#   range)r&   exponentresult_s       r   __pow___Element.__pow__   s/    $++&x!|$A]F %r   r,   N)__name__
__module____qualname____firstlineno____doc__r9   r(   r.   r2   r5   rA   rE   rL   rS   __static_attributes__r
   r   r   r   r   M   s>    $ c)HY+.23"r   r   c                   @    \ rS rSrSr\SS j5       r\SS j5       rSrg)Shamir   zShamir'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           	          [        U S-
  5       Vs/ s H  n[        [        S5      5      PM     nnUR                  [        U5      5        S n[        SUS-   5       Vs/ s H  oDU" XEU5      4PM     sn$ s  snf s  snf )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                     [        U 5      n[        S5      nU H
  nX4-  U-   nM     U(       a  U[        U 5      [        U5      -  -  nUR                  5       $ )Nr   )r   r$   r5   )usercoeffsssssidxsharecoeffs         r   
make_share Shamir.split.<locals>.make_share   sT    4.C QKEe+   $3v;66<<>!r   )rO   r   rngappend)knsecretrb   ira   rf   s          r   splitShamir.split   sw    N .31q5\:\(3r7#\:hv&'
	"" ;@1q5/J/QJq$/0/JJ/ ;. Ks   A6A;c                   ^ [        U 5      n/ nU  Hf  n[        US   5      m[        US   5      n[        U4S jU 5       5      (       a  [        S5      eU(       a  UTU-  -  nUR	                  TU45        Mh     [        S5      n[        U5       Hb  nX7   u  p[        S5      n
[        S5      n[        U5       H  nX<   S   nX:w  d  M  X-  n
XU-   -  nM     XiU
-  UR                  5       -  -  nMd     UR                  5       $ )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   2   >#    U  H  oS    T:H  v   M     g7f)r   Nr
   ).0yrc   s     r   	<genexpr>!Shamir.combine.<locals>.<genexpr>  s     2	1Q43;	s   zDuplicate share)r$   r   anyr%   ri   rO   rL   r5   )sharesrb   rj   	gf_sharesxvaluerQ   jx_jy_j	numeratordenominatormx_mrc   s                 @r   combineShamir.combine   s   L K	A1Q4.CQqTNE2	222 !233!c5\*  !qA |HC I"1+K1Xl1o6$I9,K	 
 Io(;(;(===F  }}r   r
   N)F)	rU   rV   rW   rX   rY   staticmethodrn   r   rZ   r
   r   r   r\   r\      s2     =K =K~ ? ?r   r\   N)Crypto.Util.py3compatr   Crypto.Utilr   Crypto.Util.numberr   r   Crypto.Randomr   rh   r   r   objectr   r\   r
   r   r   <module>r      s;   D 0  ; 1,Rv RjGV Gr   