3 M(ÌY ã@sPdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zed krLddlZej ƒdS)z/Common functionality shared by several modules.cCsh|dkr dS|dkr| }|d@d|}t|ƒdddddddddddddddddddœ|dS)as Number of bits needed to represent a integer excluding any prefix 0 bits. As per definition from https://wiki.python.org/moin/BitManipulation and to match the behavior of the Python 3 API. Usage:: >>> bit_size(1023) 10 >>> bit_size(1024) 11 >>> bit_size(1025) 11 :param num: Integer value. If num is 0, returns 0. Only the absolute value of the number is considered. Therefore, signed integers will be abs(num) before the number's bit length is determined. :returns: Returns the number of bits in the integer. ééz%xééé)Ú0Ú1Ú2Ú3Ú4Ú5Ú6Ú7Ú8Ú9ÚaÚbÚcÚdÚeÚf)Úlen)ÚnumZhex_num©rú1/private/tmp/pip-build-nl73fm5q/rsa/rsa/common.pyÚbit_sizes rcCsB|dkrtd|ƒ‚|dkr dSd}x|r<|d7}|dL}q&W|S)zM Returns the number of bits required to hold a specific long number. rz%Only nonnegative numbers possible: %sr)Ú ValueError)ÚnumberÚbitsrrrÚ _bit_size=s  rcCs*tt|ƒdƒ\}}|s|dkr&|d7}|S)a” Returns the number of bytes required to hold a specific long number. The number of bytes is rounded up. Usage:: >>> byte_size(1 << 1023) 128 >>> byte_size((1 << 1024) - 1) 128 >>> byte_size(1 << 1024) 129 :param number: An unsigned integer :returns: The number of bytes required to hold a specific long number. érr)Údivmodr)rÚquantaÚmodrrrÚ byte_sizeQs r#c CsŠd}d}d}d}|}|}xF|dkr^||}|||}}||||}}||||}}qW|dkrp||7}|dkr€||7}|||fS)z@Returns a tuple (r, i, j) such that r = gcd(a, b) = ia + jb rrr) rrÚxÚyZlxZlyZoaÚobÚqrrrÚ extended_gcdls  r(cCs,t||ƒ\}}}|dkr(td||fƒ‚|S)z`Returns x^-1 (mod n) >>> inverse(7, 4) 3 >>> (inverse(143, 4) * 143) % 4 1 rz*x (%d) and n (%d) are not relatively prime)r(r)r$ÚnZdividerÚinvÚ_rrrÚinverse…s r,c Cs`d}d}x|D] }||9}qWx>> crt([2, 3], [3, 5]) 8 >>> crt([2, 3, 2], [3, 5, 7]) 23 >>> crt([2, 3, 0], [7, 11, 15]) 135 rr)Úzipr,) Za_valuesZ modulo_valuesÚmr$ZmoduloZm_iZa_iZM_ir*rrrÚcrt–s   r/Ú__main__rN) Ú__doc__rrr#r(r,r/Ú__name__ÚdoctestÚtestmodrrrrÚs)#