From a8b05c2320a68cb82a26c697914e53cf32bfcfda Mon Sep 17 00:00:00 2001 From: geoffrey Date: Wed, 11 Feb 2026 17:18:55 +0100 Subject: [PATCH] Add bin expo functions and update docs --- Cryptotools/Utils/utils.py | 52 ++++++++++++++++++++++++++++++++++++- docs/index.md | 2 ++ docs/utils.md | 3 +++ mkdocs.yml | 2 ++ site/index.html | 2 +- site/sitemap.xml.gz | Bin 127 -> 127 bytes 6 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 docs/utils.md diff --git a/Cryptotools/Utils/utils.py b/Cryptotools/Utils/utils.py index 9090efe..4b87c04 100644 --- a/Cryptotools/Utils/utils.py +++ b/Cryptotools/Utils/utils.py @@ -31,4 +31,54 @@ def egcd(a, b): x = y1 - (b // a) * x1 y = x1 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) diff --git a/docs/index.md b/docs/index.md index b575c8e..f434fae 100644 --- a/docs/index.md +++ b/docs/index.md @@ -7,6 +7,8 @@ * [Group Theory](/group-theory) * Public Keys: * [RSA](/rsa) +* Utils + * [Utils](/utils) * Examples: * [Generating RSA keys](/examples-rsa-keys) diff --git a/docs/utils.md b/docs/utils.md new file mode 100644 index 0000000..d97e9fd --- /dev/null +++ b/docs/utils.md @@ -0,0 +1,3 @@ +CryptoTools provides several utils function wich can be used for cryptography purposes + +::: Cryptotools.Utils.utils diff --git a/mkdocs.yml b/mkdocs.yml index 04cebae..f04ea4d 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -13,5 +13,7 @@ nav: - Group theory: group-theory.md - Public Keys: - RSA: rsa.md + - Utils: + - Utils: utils.md - Examples: - Generating RSA Keys: examples-rsa-keys.md diff --git a/site/index.html b/site/index.html index b2600a2..87dfc67 100644 --- a/site/index.html +++ b/site/index.html @@ -144,5 +144,5 @@ diff --git a/site/sitemap.xml.gz b/site/sitemap.xml.gz index d0bfde9ed880dae009f7fc1d8bb38efd3a74e72b..a7f3a5c62b68f6f7e0866da58ffd8d36aedca294 100644 GIT binary patch delta 13 Ucmb=gXP58h;AlA3J(0Zv03MYDp#T5? delta 13 Ucmb=gXP58h;AjYlnaExN02_+~`~Uy|