3 œäL] ã@sÔdZddlmZmZmZmZddlZddlZddlm Z m Z yjddl m Z m Z mZmZmZejƒdkrleƒ‚yddlmZd d „ZWneefk r¢eƒ‚YnXWn eefk rÆd d „ZYnXd d „ZdS)aN Function for calculating the modular inverse. Exports the following items: - inverse_mod() Source code is derived from http://webpages.charter.net/curryfans/peter/downloads.html, but has been heavily modified to fit into this projects lint settings. The original project license is listed below: Copyright (c) 2014 Peter Pearson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. é)Úunicode_literalsÚdivisionÚabsolute_importÚprint_functionNé)Ú int_to_bytesÚint_from_bytes)Úbuffer_from_bytesÚbytes_from_bufferÚFFIEngineErrorÚLibraryNotFoundErrorÚnullÚPyPy)Ú libcryptocCstjƒ}tt|ƒƒ}tt|ƒƒ}t|ƒ}tj|t|ƒtƒƒ}|dkrPtj|dƒt|ƒ}tj|t|ƒtƒƒ}|dkr€tj|dƒtj tƒ|||ƒ} tj | ƒ} t t j | dƒƒ} t| ƒ} tj| | ƒt| | ƒ} t| ƒ}tj|ƒtj|ƒtj| ƒtj|ƒ|S)z× Compute the modular inverse of a (mod p) :param a: An integer :param p: An integer :return: An integer rré)rZ BN_CTX_newrÚabsr Z BN_bin2bnÚlenr ZBN_set_negativeZBN_mod_inverseZ BN_num_bitsÚintÚmathÚceilZ BN_bn2binr rZBN_freeZ BN_CTX_free)ÚaÚpÚctxZa_bytesZp_bytesZa_bufZa_bnZp_bufZp_bnZr_bnZ r_len_bitsZr_lenZr_bufZr_bytesÚresult©rú7/tmp/pip-install-wfra5znf/asn1crypto/asn1crypto/_int.pyÚ inverse_mod:s.           rc Csš|dks||kr||}||}}d\}}}}xF|dkrtt||ƒ|f\}}}||||||||f\}}}}q0W|dks‚t‚|dkrŽ|S||SdS)z· Compute the modular inverse of a (mod p) :param a: An integer :param p: An integer :return: An integer rrN)rrrr)ÚdivmodÚAssertionError) rrÚcÚdZucZvcZudZvdÚqrrrrks   ( cCsxt|ƒ|krd|}qW|S)a  Ensure a byte string representing a positive integer is a specific width (in bytes) :param bytes_: The integer byte string :param width: The desired width as an integer :return: A byte string of the width specified ó)r)Zbytes_ÚwidthrrrÚ fill_widthŽs r$)Ú__doc__Ú __future__rrrrrÚplatformÚutilrrZ_ffir r r r r Úpython_implementationÚEnvironmentErrorZ_perf._big_num_ctypesrrÚ ImportErrorr$rrrrÚ s    +#