diff options
author | Yesudeep Mangalapilly <yesudeep@gmail.com> | 2011-08-12 15:09:40 +0530 |
---|---|---|
committer | Yesudeep Mangalapilly <yesudeep@gmail.com> | 2011-08-12 15:09:40 +0530 |
commit | 5b48c520c1f4f408af9a0932b99da87b2ecf368a (patch) | |
tree | e7accaa2bc7f22df0d730b820d7cad1e638abfba /rsa | |
parent | 3780830bae12d3f2f6f6145eb62727cadf734928 (diff) | |
download | rsa-5b48c520c1f4f408af9a0932b99da87b2ecf368a.tar.gz |
Adds a much faster bit_size implementation causing an overall speed up.
* Note that the machine aligned integer_to_bytes is still faster than
the older version.
Diffstat (limited to 'rsa')
-rw-r--r-- | rsa/common.py | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/rsa/common.py b/rsa/common.py index 33714a6..11e52ec 100644 --- a/rsa/common.py +++ b/rsa/common.py @@ -17,8 +17,50 @@ '''Common functionality shared by several modules.''' +def bit_size(num): + """ + Number of bits needed to represent a integer excluding any prefix + 0 bits. + + >>> bit_size(1023) + 10 + >>> bit_size(1024) + 11 + >>> bit_size(1025) + 11 + + >>> bit_size(1 << 1024) + 1025 + >>> bit_size((1 << 1024) + 1) + 1025 + >>> bit_size((1 << 1024) - 1) + 1024 + + :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. + """ + if num == 0: + return 0 + if num < 0: + num = -num + + # Make sure this is an int and not a float. + num & 1 + + hex_num = "%x" % num + return ((len(hex_num) - 1) * 4) + { + '0':0, '1':1, '2':2, '3':2, + '4':3, '5':3, '6':3, '7':3, + '8':4, '9':4, 'a':4, 'b':4, + 'c':4, 'd':4, 'e':4, 'f':4, + }[hex_num[0]] + -def bit_size(number): +def _bit_size(number): '''Returns the number of bits required to hold a specific long number. >>> bit_size(1023) |