Skip to content

Commit 000e84a

Browse files
adamantikesybrenstuvel
authored andcommitted
Ceiling division implementation (sybrenstuvel#88)
Created as a new function as it will be needed by the new PKCS#1 2.0 implementation. Specifically, for the MGF1 function used in the OAEP encoding/decoding. This allows us not to have `math` dependencies
1 parent 7ebae9f commit 000e84a

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

rsa/common.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,33 @@ def byte_size(number):
7676
:returns:
7777
The number of bytes required to hold a specific long number.
7878
"""
79-
quanta, mod = divmod(bit_size(number), 8)
80-
if mod or number == 0:
79+
if number == 0:
80+
return 1
81+
return ceil_div(bit_size(number), 8)
82+
83+
84+
def ceil_div(num, div):
85+
"""
86+
Returns the ceiling function of a division between `num` and `div`.
87+
88+
Usage::
89+
90+
>>> ceil_div(100, 7)
91+
15
92+
>>> ceil_div(100, 10)
93+
10
94+
>>> ceil_div(1, 4)
95+
1
96+
97+
:param num: Division's numerator, a number
98+
:param div: Division's divisor, a number
99+
100+
:return: Rounded up result of the division between the parameters.
101+
"""
102+
quanta, mod = divmod(num, div)
103+
if mod:
81104
quanta += 1
82105
return quanta
83-
# return int(math.ceil(bit_size(number) / 8.0))
84106

85107

86108
def extended_gcd(a, b):

0 commit comments

Comments
 (0)