3
¬ �eJ"  ã               @   sh   d dl mZ d dlmZ d dlmZmZ d dlmZ	 dd„ Z
dd„ ZG d	d
„ d
eƒZG dd„ deƒZdS )é    )Úis_native_int)Únumber)Úlong_to_bytesÚbytes_to_long)Úget_random_bytesc             C   sD   || kr||  } }d}x(|r>|d@ r,|| N }| dK } |dL }qW |S )z!Multiply two polynomials in GF(2)r   é   © )Úf1Úf2Úzr   r   úd/var/www/html/wholesaler-api-python-env/lib/python3.6/site-packages/Crypto/Protocol/SecretSharing.pyÚ	_mult_gf2(   s    
r   c             C   sf   | |k rd| fS t j}d}| }||ƒ}x6||ƒ|kr\d||ƒ| > }||N }|t||ƒN }q(W ||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Úsr   r   r   Ú_div_gf27   s    r   c               @   s\   e Zd ZdZddd  Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )Ú_ElementzElement of GF(2^128) fieldr   é   é   é€   c             C   s4   t |ƒr|| _n t|ƒdkr(t|ƒ| _ntdƒ‚dS )zïInitialize 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)ÚselfZencoded_valuer   r   r   Ú__init__S   s
    z_Element.__init__c             C   s   | j |j kS )N)r   )r   Úotherr   r   r   Ú__eq__b   s    z_Element.__eq__c             C   s   | j S )z7Return the field element, encoded as a 128-bit integer.)r   )r   r   r   r   Ú__int__e   s    z_Element.__int__c             C   s   t | jdƒS )z6Return the field element, encoded as a 16 byte string.r   )r   r   )r   r   r   r   Úencodei   s    z_Element.encodec       	      C   sæ   | j }|j }||kr|| }}| j||fkr4tdƒS dd }|d }}x–|rÜtt|d@ ƒdd … d dd�}|||A @ || d |@ B }|dK }tt|d? d@ ƒdd … d dd�}||| jA @ || d |@ B }|dL }qHW t|ƒS )Nr   r   r   r   )Úbase)r   Úirr_polyr   ÚintÚbin)	r   Úfactorr	   r
   Zmask1Úvr   Zmask2Zmask3r   r   r   Ú__mul__m   s     

 $z_Element.__mul__c             C   s   t | j|jA ƒS )N)r   r   )r   Útermr   r   r   Ú__add__†   s    z_Element.__add__c             C   st   | j dkrtdƒ‚| j | j }}d\}}xB|dkrjt||ƒd }||t||ƒA  }}||t||ƒA  }}q*W t|ƒS )z0Return the inverse of this element in GF(2^128).r   zInversion of zeror   )r   r   )r   r   r&   r   r   r   )r   Úr0Úr1Ús0Ús1r   r   r   r   Úinverse‰   s    

z_Element.inversec             C   s,   t | jƒ}xt|d ƒD ]}||  }qW |S )Nr   )r   r   Úrange)r   ÚexponentÚresultÚ_r   r   r   Ú__pow__š   s    
z_Element.__pow__Né   é   é‡   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r&   r    r"   r#   r$   r+   r-   r2   r7   r   r   r   r   r   M   s   r   c               @   s,   e Zd ZdZeddd„ƒZed	dd„ƒZdS )
ÚShamirz›Shamir'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                sL   dd„ t | d ƒD ƒ‰ ˆ jt|ƒƒ dd„ ‰‡ ‡‡fdd„t d|d ƒ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             S   s   g | ]}t td ƒƒ‘qS )r   )r   Úrng)Ú.0Úir   r   r   ú
<listcomp>Í   s    z Shamir.split.<locals>.<listcomp>r   c             S   sJ   t | ƒ}t dƒ}x|D ]}|| | }qW |rB|t | ƒt|ƒ 7 }|jƒ S )Nr   )r   r   r$   )ÚuserÚcoeffsÚssssÚidxZshareZcoeffr   r   r   Ú
make_shareÓ   s    
z Shamir.split.<locals>.make_sharec                s   g | ]}|ˆ|ˆ ˆƒf‘qS r   r   )rA   rB   )rE   rH   rF   r   r   rC   Ü   s    )r3   Úappendr   )ÚkÚnZsecretrF   r   )rE   rH   rF   r   Úsplit¨   s    %	zShamir.splitc                sú   t | ƒ}g }x`| D ]X}t|d ƒ‰ t|d ƒ}t‡ fdd„|D ƒƒrLtdƒ‚|r\|ˆ | 7 }|jˆ |fƒ qW tdƒ}xzt|ƒD ]n}|| \}}	tdƒ}
tdƒ}x8t|ƒD ],}|| d }||krª|
|9 }
||| 9 }qªW ||	|
 |jƒ  7 }q€W |jƒ 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   s   | ]}|d  ˆ kV  qdS )r   Nr   )rA   Úy)rG   r   r   ú	<genexpr>  s    z!Shamir.combine.<locals>.<genexpr>zDuplicate share)r   r   Úanyr   rI   r3   r2   r$   )ZsharesrF   rJ   Z	gf_sharesÚxÚvaluer5   ÚjZx_jZy_jÚ	numeratorÚdenominatorÚmZx_mr   )rG   r   ÚcombineÞ   s,    
zShamir.combineN)F)F)r;   r<   r=   r>   ÚstaticmethodrL   rV   r   r   r   r   r?   ¡   s
   5r?   N)ZCrypto.Util.py3compatr   ZCrypto.Utilr   ZCrypto.Util.numberr   r   ZCrypto.Randomr   r@   r   r   Úobjectr   r?   r   r   r   r   Ú<module>"   s   T