This function perform an binary exponentiation, also known as Exponentiation squaring.
A binary exponentiation is the process for computing an integer power of a number, such as a ** n.
For doing that, the first step is to convert the exponent into a binary representation
And for each 1 bit, we compute the exponent.
| Parameters: |
-
n
(Integer)
–
-
e
(Integer)
–
|
| Returns: |
-
int
–
Return the result of the exponentation of n ** e
|
Source code in Cryptotools/Utils/utils.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 | def bin_expo(n, e) -> int:
"""
This function perform an binary exponentiation, also known as Exponentiation squaring.
A binary exponentiation is the process for computing an integer power of a number, such as a ** n.
For doing that, the first step is to convert the exponent into a binary representation
And for each 1 bit, we compute the exponent.
Args:
n (Integer): it's the base
e (Integer): it's the exponent
Returns:
Return the result of the exponentation of n ** e
"""
binary = bin(e)[2:] # Remove the prefix 0b
r = 1
exp = 1
# We need to reverse, and to start from the right to left
# Otherwise, we do on left to the right
# It's dirty, maybe we can find another way to do that
for b in binary[::-1]:
if b == '1':
r *= n ** exp
print(n ** exp, exp)
exp *= 2
return r
|