Add bin expo functions and update docs
This commit is contained in:
parent
50f7b1159e
commit
a8b05c2320
@ -32,3 +32,53 @@ def egcd(a, b):
|
|||||||
y = x1
|
y = x1
|
||||||
return gcd, x, y
|
return gcd, x, y
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
def exponent_squaring(n, e):
|
||||||
|
"""
|
||||||
|
This function perform an exponentiation squaring, which compute an integer power of a number based on the following algorithm:
|
||||||
|
n ** e = {
|
||||||
|
1 # if n is 0
|
||||||
|
(n ** (e / 2)) ** 2 # if n is even
|
||||||
|
((n ** (e - 1 / 2)) ** 2) * n # if n is odd
|
||||||
|
}
|
||||||
|
|
||||||
|
Args:
|
||||||
|
n (Integer): n is the base of n ** e
|
||||||
|
e (Integer): e is the exponent
|
||||||
|
"""
|
||||||
|
if e < 0:
|
||||||
|
return exponent_squaring(1 / n, -e)
|
||||||
|
elif e == 0:
|
||||||
|
return 1
|
||||||
|
elif e % 2 == 1: # n is odd
|
||||||
|
return exponent_squaring(n * n, (e - 1) / 2) * n
|
||||||
|
elif e % 2 == 0: # n is even
|
||||||
|
return exponent_squaring(n * n, e / 2)
|
||||||
|
|||||||
@ -7,6 +7,8 @@
|
|||||||
* [Group Theory](/group-theory)
|
* [Group Theory](/group-theory)
|
||||||
* Public Keys:
|
* Public Keys:
|
||||||
* [RSA](/rsa)
|
* [RSA](/rsa)
|
||||||
|
* Utils
|
||||||
|
* [Utils](/utils)
|
||||||
* Examples:
|
* Examples:
|
||||||
* [Generating RSA keys](/examples-rsa-keys)
|
* [Generating RSA keys](/examples-rsa-keys)
|
||||||
|
|
||||||
|
|||||||
3
docs/utils.md
Normal file
3
docs/utils.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
CryptoTools provides several utils function wich can be used for cryptography purposes
|
||||||
|
|
||||||
|
::: Cryptotools.Utils.utils
|
||||||
@ -13,5 +13,7 @@ nav:
|
|||||||
- Group theory: group-theory.md
|
- Group theory: group-theory.md
|
||||||
- Public Keys:
|
- Public Keys:
|
||||||
- RSA: rsa.md
|
- RSA: rsa.md
|
||||||
|
- Utils:
|
||||||
|
- Utils: utils.md
|
||||||
- Examples:
|
- Examples:
|
||||||
- Generating RSA Keys: examples-rsa-keys.md
|
- Generating RSA Keys: examples-rsa-keys.md
|
||||||
|
|||||||
@ -144,5 +144,5 @@
|
|||||||
|
|
||||||
<!--
|
<!--
|
||||||
MkDocs version : 1.6.1
|
MkDocs version : 1.6.1
|
||||||
Build Date UTC : 2026-01-06 07:48:31.465035+00:00
|
Build Date UTC : 2026-02-11 16:14:17.229949+00:00
|
||||||
-->
|
-->
|
||||||
|
|||||||
Binary file not shown.
Loading…
Reference in New Issue
Block a user