Add Elliptic Curve and docs

This commit is contained in:
geoffrey 2026-02-15 09:14:59 +01:00
parent a8b05c2320
commit 66b5741b28
23 changed files with 2147 additions and 21 deletions

159
Cryptotools/Groups/curve.py Normal file

@ -0,0 +1,159 @@
#!/usr/bin/env python3
from Cryptotools.Groups.elliptic import Point
import numpy as np
from math import sqrt
class Curve:
# Curve
WEIERSTRASS = 0
MONTGOMERY = 1
def __init__(self, a, b, t):
self._a = a
self._b = b
if t not in (Curve.WEIERSTRASS, Curve.MONTGOMERY):
raise Exception(f"The type of the curve is not recognized")
self._type = t
self._xtmp = np.linspace(-5, 5, 500).tolist()
self._x = list()
self._y = list()
self._yn = list()
self._points = list()
self._pointsSym = list()
def f(self, x):
if self._type == Curve.WEIERSTRASS:
y = pow(x, 3) + (self._a * x) + self._b
if self._type == Curve.MONTGOMERY:
y = pow(x, 3) + (3 * pow(x, 2)) + x
if y > 0:
return sqrt(y)
return None
def generatePoints(self):
for x in self._xtmp:
y = self.f(x)
if y is None:
continue
self._x.append(x)
self._y.append(y)
self._yn.append(-y)
self._points.append(Point(
x,
y
))
self._pointsSym.append(Point(
x,
-y
))
@property
def x(self):
return self._x
@property
def y(self):
return self._y
@property
def yn(self):
return self._yn
def getPoints(self):
return self._points
def getPointsSym(self):
return self._pointsSym
def add(self, P, Q) -> Point:
"""
This function operathe addition operation on two points P and Q
Args:
P (Object): The first Point on the curve
Q (Object): The second Point on the curve
Returns:
Return the Point object R
"""
## Check if P or Q are infinity
if (P.x, P.y) == (0, 0) and (Q.x, Q.y) == (0, 0):
return Point(0, 0)
elif (P.x, P.y) == (0, 0):
return Point(Q.x, Q.y)
elif (Q.x, Q.y) == (0, 0):
return Point(P.x, P.y)
# point doubling
if P.x == Q.x:
# Infinity
if P.y != Q.y or Q.y == 0:
return Point(0, 0)
# Point doubling
try:
inv = pow(2 * P.y, -1); # It's working with the inverse modular, WHY ???
m = ((3 * pow(P.x, 2)) + self._a) * inv
except ValueError:
return Point(0, 0)
else:
try:
inv = pow(Q.x - P.x, -1)
m = (Q.y - P.y) * inv
except ValueError:
# May call this Exception: base is not invertible for the given modulus
# I return an Infinity point until I fixed that
return Point(0, 0)
xr = (pow(m, 2) - P.x - Q.x)
yr = (m * (P.x - xr)) - P.y
return Point(xr, yr)
def scalar(self, P, n) -> Point:
"""
This function compute a Scalar Multiplication of P, n time. This algorithm is also known as Double and Add.
Args:
P (point): the Point to multiplication
n (Integer): multiplicate n time P
Returns:
Return the result of the Scalar multiplication
"""
binary = bin(n)[2:]
binary = binary[::-1] # We need to reverse the binary
nP = Point(0, 0)
Rtmp = P
for b in binary:
if b == '1':
nP = self.add(nP, Rtmp)
Rtmp = self.add(Rtmp, Rtmp) # Double P
return nP
def find_reverse(self, P):
"""
This function return the reverse of the Point P
Args:
P (Point): Point object to find
Returns:
Return the object Pr, which is the reverse point of P
"""
Pr = None
for p in self._pointsSym:
if P.x == p.x and -P.y == p.y:
Pr = Point(p.x, p.y)
break
return Pr

@ -0,0 +1,34 @@
#!/usr/bin/env python3
class Point:
"""
This simple class represent the Point at the coordinate x and y in a plan
Attributes:
x (Integer): Position at the x
y (Integer): Position at the y
"""
def __init__(self, x, y):
self._x = x
self._y = y
@property
def x(self):
return self._x
@property
def y(self):
return self._y
@x.setter
def x(self, x):
self._x = x
@y.setter
def y(self, y):
self._y = y
def __eq__(self, other):
print(self._x, other.x)
return self._x == other.x and self._y == other.y

6
docs/curves.md Normal file

@ -0,0 +1,6 @@
# Elliptic Curves
## Points
::: Cryptotools.Groups.point
## Curves
::: Cryptotools.Groups.curve

30
docs/example-curves.md Normal file

@ -0,0 +1,30 @@
# Generating Elliptic Curves
In the following section, we are going to see how to generate Elliptic Curves and draw the curve with Python.
## A Weierstrass Curve
The python code below draw a Weierstrass Curve:
```
#!/usr/bin/env python3
import matplotlib.pyplot as plt
from Cryptotools.Groups.curve import Curve
a = 3
b = 8
curve = Curve(a, b, Curve.WEIERSTRASS)
x = curve.x
curve.generatePoints()
y = curve.y
yn = curve.yn
points = curve.getPoints()
plt.figure(figsize=(10, 6))
plt.plot(x, y, color='b', label=f'$y^2 = x^3 + {a}x + {b}$')
plt.plot(x, yn, color='b', )
plt.legend()
plt.show()
```

@ -5,11 +5,12 @@
* Low-Level Cryptographic
* [Number Theory](/number-theory)
* [Group Theory](/group-theory)
* [Curves](/curves)
* Public Keys:
* [RSA](/rsa)
* Utils
* [Utils](/utils)
* Examples:
* [Generating RSA keys](/examples-rsa-keys)
* [Generating RSA keys](/example-rsa-keys)
* [Generate curves](/example-curves)

@ -0,0 +1,24 @@
#!/usr/bin/env python3
import matplotlib.pyplot as plt
from Cryptotools.Groups.curve import Curve
a = 3
b = 8
curve = Curve(a, b, Curve.WEIERSTRASS)
x = curve.x
curve.generatePoints()
y = curve.y
yn = curve.yn
points = curve.getPoints()
#print(x)
#print(y)
plt.figure(figsize=(10, 6))
plt.plot(x, y, color='b', label=f'$y^2 = x^3 + {a}x + {b}$')
plt.plot(x, yn, color='b', )
plt.legend()
plt.show()

@ -0,0 +1,48 @@
#!/usr/bin/env python3
import matplotlib.pyplot as plt
from Cryptotools.Groups.curve import Curve
a = 3
b = 8
curve = Curve(a, b, Curve.WEIERSTRASS)
x = curve.x
curve.generatePoints()
y = curve.y
yn = curve.yn
points = curve.getPoints()
P = points[10]
Q = points[55]
R = points[247]
#print(f"{P.x} {P.y}")
#print(f"{Q.x} {Q.y}")
# Make an addition
Rp = curve.add(P, Q)
#print(f"{Rp.x} {Rp.y}")
#print(yn)
#print(x)
#print(y)
plt.figure(figsize=(10, 6))
plt.plot(x, y, color='b', label=f'$y^2 = x^3 + {a}x + {b}$')
plt.plot(x, yn, color='b', )
plt.plot(P.x, P.y, marker='o', color="red")
plt.annotate('P', (P.x, P.y + 0.5))
plt.plot(Q.x, Q.y, marker='o', color="red")
plt.annotate('Q', (Q.x, Q.y + 0.5))
plt.plot(R.x, R.y, marker='o', color="red")
plt.annotate('R', (R.x - 0.2, R.y + 0.5))
plt.plot(Rp.x, Rp.y, marker='o', color="red")
plt.annotate('R\'', (Rp.x - 0.2, Rp.y + 0.5))
plt.axline([P.x, P.y], [Q.x, Q.y], color="red")
plt.axline([R.x, R.y], [Rp.x, Rp.y], linestyle="--", color="red")
plt.legend()
plt.show()

@ -0,0 +1,44 @@
#!/usr/bin/env python3
import matplotlib.pyplot as plt
from Cryptotools.Groups.curve import Curve
from Cryptotools.Groups.point import Point
a = 3
b = 8
curve = Curve(a, b, Curve.WEIERSTRASS)
x = curve.x
curve.generatePoints()
y = curve.y
yn = curve.yn
points = curve.getPoints()
P = points[10]
nP = curve.scalar(P, 5)
print(f"{nP.x} {nP.y}")
# For testing, we may add n times with the addition operation for the Scalar Multiplication
tmp = Point(0, 0)
for i in range(5):
tmp = curve.add(P, tmp)
# Unfortunately, the result is approximatively the same
# the multiplication need to be more accurate
# if tmp == nP:
# print(True)
plt.figure(figsize=(10, 6))
plt.plot(x, y, color='b', label=f'$y^2 = x^3 + {a}x + {b}$')
plt.plot(x, yn, color='b', )
plt.plot(P.x, P.y, marker='o', color="red")
plt.annotate('P', (P.x, P.y + 0.5))
plt.plot(nP.x, nP.y, marker='o', color="red")
plt.annotate('nP', (nP.x, nP.y + 0.5))
plt.legend()
plt.show()

@ -0,0 +1,43 @@
#!/usr/bin/env python3
from Cryptotools.Groups.point import Point
from Cryptotools.Groups.curve import Curve
import numpy as np
import matplotlib.pyplot as plt
from math import sqrt
a = 3
b = 8
curve = Curve(a, b, Curve.WEIERSTRASS)
x = curve.x
curve.generatePoints()
y = curve.y
yn = curve.yn
points = curve.getPoints()
pointsReverse = curve.getPointsSym()
P = points[10]
Q = curve.find_reverse(P)
# We made the addition
# The result is the point at infinity
R = curve.add(P, Q)
print(f"{R.x} {R.y}")
plt.figure(figsize=(10, 6))
plt.plot(x, y, color='b', label=f'$y^2 = x^3 + {a}x + {b}$')
plt.plot(x, yn, color='b', )
plt.plot(P.x, P.y, marker='o', color="red")
plt.annotate('P', (P.x, P.y + 0.5))
plt.plot(Q.x, Q.y, marker='o', color="red")
plt.text(-1 , 11, f"R = infinity")
plt.annotate('Q', (Q.x, Q.y + 0.5))
plt.axline([P.x, P.y], [Q.x, Q.y], color="red")
plt.legend()
plt.show()

@ -11,9 +11,11 @@ nav:
- Low-level cryptographic:
- Number theory: number-theory.md
- Group theory: group-theory.md
- Curves: curves.md
- Public Keys:
- RSA: rsa.md
- Utils:
- Utils: utils.md
- Examples:
- Generating RSA Keys: examples-rsa-keys.md
- Generating RSA Keys: example-rsa-keys.md
- Generating Curves: example-curves.md

@ -43,15 +43,24 @@
</li>
<li class="toctree-l1"><a class="reference internal" href="/group-theory/">Group theory</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="/curves/">Curves</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Public Keys</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="/rsa/">RSA</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Utils</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="/utils/">Utils</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Examples</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="/examples-rsa-keys/">Generating RSA Keys</a>
<li class="toctree-l1"><a class="reference internal" href="/example-rsa-keys/">Generating RSA Keys</a>
</li>
<li class="toctree-l1"><a class="" href="/example-curves.md">Generating Curves</a>
</li>
</ul>
</div>

1096
site/curves/index.html Normal file

File diff suppressed because it is too large Load Diff

@ -14,7 +14,7 @@
<script>
// Current page data
var mkdocs_page_name = "Generating RSA Keys";
var mkdocs_page_input_path = "examples-rsa-keys.md";
var mkdocs_page_input_path = "example-rsa-keys.md";
var mkdocs_page_url = null;
</script>
@ -50,18 +50,27 @@
</li>
<li class="toctree-l1"><a class="reference internal" href="../group-theory/">Group theory</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../curves/">Curves</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Public Keys</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../rsa/">RSA</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Utils</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../utils/">Utils</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Examples</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal current" href="#">Generating RSA Keys</a>
<ul class="current">
</ul>
</li>
<li class="toctree-l1"><a class="" href="../example-curves.md">Generating Curves</a>
</li>
</ul>
</div>
</div>
@ -116,7 +125,7 @@ print(f&quot;Plaintext: {plaintext}&quot;)
</div>
</div><footer>
<div class="rst-footer-buttons" role="navigation" aria-label="Footer Navigation">
<a href="../rsa/" class="btn btn-neutral float-left" title="RSA"><span class="icon icon-circle-arrow-left"></span> Previous</a>
<a href="../utils/" class="btn btn-neutral float-left" title="Utils"><span class="icon icon-circle-arrow-left"></span> Previous</a>
</div>
<hr/>
@ -139,7 +148,7 @@ print(f&quot;Plaintext: {plaintext}&quot;)
<span class="rst-current-version" data-toggle="rst-current-version">
<span><a href="../rsa/" style="color: #fcfcfc">&laquo; Previous</a></span>
<span><a href="../utils/" style="color: #fcfcfc">&laquo; Previous</a></span>
</span>

@ -120,15 +120,24 @@
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../curves/">Curves</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Public Keys</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../rsa/">RSA</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Utils</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../utils/">Utils</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Examples</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../examples-rsa-keys/">Generating RSA Keys</a>
<li class="toctree-l1"><a class="reference internal" href="../example-rsa-keys/">Generating RSA Keys</a>
</li>
<li class="toctree-l1"><a class="" href="../example-curves.md">Generating Curves</a>
</li>
</ul>
</div>
@ -3015,7 +3024,7 @@ In Group Theory, an identity element is an element in the group which do not cha
</div><footer>
<div class="rst-footer-buttons" role="navigation" aria-label="Footer Navigation">
<a href="../number-theory/" class="btn btn-neutral float-left" title="Number theory"><span class="icon icon-circle-arrow-left"></span> Previous</a>
<a href="../rsa/" class="btn btn-neutral float-right" title="RSA">Next <span class="icon icon-circle-arrow-right"></span></a>
<a href="../curves/" class="btn btn-neutral float-right" title="Curves">Next <span class="icon icon-circle-arrow-right"></span></a>
</div>
<hr/>
@ -3041,7 +3050,7 @@ In Group Theory, an identity element is an element in the group which do not cha
<span><a href="../number-theory/" style="color: #fcfcfc">&laquo; Previous</a></span>
<span><a href="../rsa/" style="color: #fcfcfc">Next &raquo;</a></span>
<span><a href="../curves/" style="color: #fcfcfc">Next &raquo;</a></span>
</span>
</div>

@ -50,15 +50,24 @@
</li>
<li class="toctree-l1"><a class="reference internal" href="group-theory/">Group theory</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="curves/">Curves</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Public Keys</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="rsa/">RSA</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Utils</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="utils/">Utils</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Examples</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="examples-rsa-keys/">Generating RSA Keys</a>
<li class="toctree-l1"><a class="reference internal" href="example-rsa-keys/">Generating RSA Keys</a>
</li>
<li class="toctree-l1"><a class="" href="example-curves.md">Generating Curves</a>
</li>
</ul>
</div>
@ -91,14 +100,20 @@
<li>Low-Level Cryptographic<ul>
<li><a href="/number-theory">Number Theory</a></li>
<li><a href="/group-theory">Group Theory</a></li>
<li><a href="/curves">Curves</a></li>
</ul>
</li>
<li>Public Keys:<ul>
<li><a href="/rsa">RSA</a></li>
</ul>
</li>
<li>Utils<ul>
<li><a href="/utils">Utils</a></li>
</ul>
</li>
<li>Examples:<ul>
<li><a href="/examples-rsa-keys">Generating RSA keys</a></li>
<li><a href="/example-rsa-keys">Generating RSA keys</a></li>
<li><a href="/example-curves">Generate curves</a></li>
</ul>
</li>
</ul>
@ -144,5 +159,5 @@
<!--
MkDocs version : 1.6.1
Build Date UTC : 2026-02-11 16:14:17.229949+00:00
Build Date UTC : 2026-02-15 08:12:33.471313+00:00
-->

@ -54,15 +54,24 @@
</li>
<li class="toctree-l1"><a class="reference internal" href="../group-theory/">Group theory</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../curves/">Curves</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Public Keys</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../rsa/">RSA</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Utils</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../utils/">Utils</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Examples</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../examples-rsa-keys/">Generating RSA Keys</a>
<li class="toctree-l1"><a class="reference internal" href="../example-rsa-keys/">Generating RSA Keys</a>
</li>
<li class="toctree-l1"><a class="" href="../example-curves.md">Generating Curves</a>
</li>
</ul>
</div>

@ -52,15 +52,24 @@
</li>
<li class="toctree-l1"><a class="reference internal" href="../group-theory/">Group theory</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../curves/">Curves</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Public Keys</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../rsa/">RSA</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Utils</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../utils/">Utils</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Examples</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../examples-rsa-keys/">Generating RSA Keys</a>
<li class="toctree-l1"><a class="reference internal" href="../example-rsa-keys/">Generating RSA Keys</a>
</li>
<li class="toctree-l1"><a class="" href="../example-curves.md">Generating Curves</a>
</li>
</ul>
</div>

@ -80,15 +80,24 @@
</li>
<li class="toctree-l1"><a class="reference internal" href="../group-theory/">Group theory</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../curves/">Curves</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Public Keys</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../rsa/">RSA</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Utils</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../utils/">Utils</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Examples</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../examples-rsa-keys/">Generating RSA Keys</a>
<li class="toctree-l1"><a class="reference internal" href="../example-rsa-keys/">Generating RSA Keys</a>
</li>
<li class="toctree-l1"><a class="" href="../example-curves.md">Generating Curves</a>
</li>
</ul>
</div>

Binary file not shown.

@ -50,6 +50,8 @@
</li>
<li class="toctree-l1"><a class="reference internal" href="../group-theory/">Group theory</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../curves/">Curves</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Public Keys</span></p>
<ul class="current">
@ -58,9 +60,16 @@
</ul>
</li>
</ul>
<p class="caption"><span class="caption-text">Utils</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../utils/">Utils</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Examples</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../examples-rsa-keys/">Generating RSA Keys</a>
<li class="toctree-l1"><a class="reference internal" href="../example-rsa-keys/">Generating RSA Keys</a>
</li>
<li class="toctree-l1"><a class="" href="../example-curves.md">Generating Curves</a>
</li>
</ul>
</div>
@ -1041,8 +1050,8 @@ The key is a tuple of the key and the modulus n</p>
</div>
</div><footer>
<div class="rst-footer-buttons" role="navigation" aria-label="Footer Navigation">
<a href="../group-theory/" class="btn btn-neutral float-left" title="Group theory"><span class="icon icon-circle-arrow-left"></span> Previous</a>
<a href="../examples-rsa-keys/" class="btn btn-neutral float-right" title="Generating RSA Keys">Next <span class="icon icon-circle-arrow-right"></span></a>
<a href="../curves/" class="btn btn-neutral float-left" title="Curves"><span class="icon icon-circle-arrow-left"></span> Previous</a>
<a href="../utils/" class="btn btn-neutral float-right" title="Utils">Next <span class="icon icon-circle-arrow-right"></span></a>
</div>
<hr/>
@ -1065,10 +1074,10 @@ The key is a tuple of the key and the modulus n</p>
<span class="rst-current-version" data-toggle="rst-current-version">
<span><a href="../group-theory/" style="color: #fcfcfc">&laquo; Previous</a></span>
<span><a href="../curves/" style="color: #fcfcfc">&laquo; Previous</a></span>
<span><a href="../examples-rsa-keys/" style="color: #fcfcfc">Next &raquo;</a></span>
<span><a href="../utils/" style="color: #fcfcfc">Next &raquo;</a></span>
</span>
</div>

Binary file not shown.

561
site/utils/index.html Normal file

@ -0,0 +1,561 @@
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="../img/favicon.ico" />
<title>Utils - CryptoTools documentation</title>
<link rel="stylesheet" href="../css/theme.css" />
<link rel="stylesheet" href="../css/theme_extra.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css" />
<link href="../assets/_mkdocstrings.css" rel="stylesheet" />
<script>
// Current page data
var mkdocs_page_name = "Utils";
var mkdocs_page_input_path = "utils.md";
var mkdocs_page_url = null;
</script>
<!--[if lt IE 9]>
<script src="../js/html5shiv.min.js"></script>
<![endif]-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href=".." class="icon icon-home"> CryptoTools documentation
</a>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../introduction/">Introduction</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/">Installation</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Low-level cryptographic</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../number-theory/">Number theory</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../group-theory/">Group theory</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../curves/">Curves</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Public Keys</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../rsa/">RSA</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Utils</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal current" href="#">Utils</a>
<ul class="current">
</ul>
</li>
</ul>
<p class="caption"><span class="caption-text">Examples</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../example-rsa-keys/">Generating RSA Keys</a>
</li>
<li class="toctree-l1"><a class="" href="../example-curves.md">Generating Curves</a>
</li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="Mobile navigation menu">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="..">CryptoTools documentation</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content"><div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href=".." class="icon icon-home" aria-label="Docs"></a></li>
<li class="breadcrumb-item">Utils</li>
<li class="breadcrumb-item active">Utils</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div class="section" itemprop="articleBody">
<p>CryptoTools provides several utils function wich can be used for cryptography purposes</p>
<div class="doc doc-object doc-module">
<a id="Cryptotools.Utils.utils"></a>
<div class="doc doc-contents first">
<div class="doc doc-children">
<div class="doc doc-object doc-function">
<h2 id="Cryptotools.Utils.utils.bin_expo" class="doc doc-heading">
<code class="highlight language-python"><span class="n">bin_expo</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span></code>
</h2>
<div class="doc doc-contents ">
<p>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.</p>
<table class="field-list">
<colgroup>
<col class="field-name" />
<col class="field-body" />
</colgroup>
<tbody valign="top">
<tr class="field">
<th class="field-name">Parameters:</th>
<td class="field-body">
<ul class="first simple">
<li>
<b><code>n</code></b>
(<code><span title="Integer">Integer</span></code>)
<div class="doc-md-description">
<p>it's the base</p>
</div>
</li>
<li>
<b><code>e</code></b>
(<code><span title="Integer">Integer</span></code>)
<div class="doc-md-description">
<p>it's the exponent</p>
</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<table class="field-list">
<colgroup>
<col class="field-name" />
<col class="field-body" />
</colgroup>
<tbody valign="top">
<tr class="field">
<th class="field-name">Returns:</th>
<td class="field-body">
<ul class="first simple">
<li>
<code><span title="int">int</span></code>
<div class="doc-md-description">
<p>Return the result of the exponentation of n ** e</p>
</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<details class="quote">
<summary>Source code in <code>Cryptotools/Utils/utils.py</code></summary>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">35</span>
<span class="normal">36</span>
<span class="normal">37</span>
<span class="normal">38</span>
<span class="normal">39</span>
<span class="normal">40</span>
<span class="normal">41</span>
<span class="normal">42</span>
<span class="normal">43</span>
<span class="normal">44</span>
<span class="normal">45</span>
<span class="normal">46</span>
<span class="normal">47</span>
<span class="normal">48</span>
<span class="normal">49</span>
<span class="normal">50</span>
<span class="normal">51</span>
<span class="normal">52</span>
<span class="normal">53</span>
<span class="normal">54</span>
<span class="normal">55</span>
<span class="normal">56</span>
<span class="normal">57</span>
<span class="normal">58</span>
<span class="normal">59</span>
<span class="normal">60</span>
<span class="normal">61</span>
<span class="normal">62</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="k">def</span> <span class="nf">bin_expo</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> This function perform an binary exponentiation, also known as Exponentiation squaring.</span>
<span class="sd"> A binary exponentiation is the process for computing an integer power of a number, such as a ** n.</span>
<span class="sd"> For doing that, the first step is to convert the exponent into a binary representation</span>
<span class="sd"> And for each 1 bit, we compute the exponent.</span>
<span class="sd"> Args:</span>
<span class="sd"> n (Integer): it&#39;s the base</span>
<span class="sd"> e (Integer): it&#39;s the exponent</span>
<span class="sd"> Returns:</span>
<span class="sd"> Return the result of the exponentation of n ** e</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">binary</span> <span class="o">=</span> <span class="nb">bin</span><span class="p">(</span><span class="n">e</span><span class="p">)[</span><span class="mi">2</span><span class="p">:]</span> <span class="c1"># Remove the prefix 0b</span>
<span class="n">r</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">exp</span> <span class="o">=</span> <span class="mi">1</span>
<span class="c1"># We need to reverse, and to start from the right to left</span>
<span class="c1"># Otherwise, we do on left to the right</span>
<span class="c1"># It&#39;s dirty, maybe we can find another way to do that</span>
<span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">binary</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
<span class="k">if</span> <span class="n">b</span> <span class="o">==</span> <span class="s1">&#39;1&#39;</span><span class="p">:</span>
<span class="n">r</span> <span class="o">*=</span> <span class="n">n</span> <span class="o">**</span> <span class="n">exp</span>
<span class="nb">print</span><span class="p">(</span><span class="n">n</span> <span class="o">**</span> <span class="n">exp</span><span class="p">,</span> <span class="n">exp</span><span class="p">)</span>
<span class="n">exp</span> <span class="o">*=</span> <span class="mi">2</span>
<span class="k">return</span> <span class="n">r</span>
</code></pre></div></td></tr></table></div>
</details>
</div>
</div>
<div class="doc doc-object doc-function">
<h2 id="Cryptotools.Utils.utils.egcd" class="doc doc-heading">
<code class="highlight language-python"><span class="n">egcd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span></code>
</h2>
<div class="doc doc-contents ">
<p>This function compute the Extended Euclidean algorithm
https://user.eng.umd.edu/~danadach/Cryptography_20/ExtEuclAlg.pdf
https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm</p>
<table class="field-list">
<colgroup>
<col class="field-name" />
<col class="field-body" />
</colgroup>
<tbody valign="top">
<tr class="field">
<th class="field-name">Parameters:</th>
<td class="field-body">
<ul class="first simple">
<li>
<b><code>a</code></b>
(<code><span title="Integer">Integer</span></code>)
<div class="doc-md-description">
<p>the number a</p>
</div>
</li>
<li>
<b><code>b</code></b>
(<code><span title="Integer">Integer</span></code>)
<div class="doc-md-description">
<p>the number b</p>
</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<details class="quote">
<summary>Source code in <code>Cryptotools/Utils/utils.py</code></summary>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">18</span>
<span class="normal">19</span>
<span class="normal">20</span>
<span class="normal">21</span>
<span class="normal">22</span>
<span class="normal">23</span>
<span class="normal">24</span>
<span class="normal">25</span>
<span class="normal">26</span>
<span class="normal">27</span>
<span class="normal">28</span>
<span class="normal">29</span>
<span class="normal">30</span>
<span class="normal">31</span>
<span class="normal">32</span>
<span class="normal">33</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="k">def</span> <span class="nf">egcd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> This function compute the Extended Euclidean algorithm</span>
<span class="sd"> https://user.eng.umd.edu/~danadach/Cryptography_20/ExtEuclAlg.pdf</span>
<span class="sd"> https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm</span>
<span class="sd"> Args:</span>
<span class="sd"> a (Integer): the number a</span>
<span class="sd"> b (Integer): the number b</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">a</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="n">b</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span>
<span class="n">gcd</span><span class="p">,</span> <span class="n">x1</span><span class="p">,</span> <span class="n">y1</span> <span class="o">=</span> <span class="n">egcd</span><span class="p">(</span><span class="n">b</span> <span class="o">%</span> <span class="n">a</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">y1</span> <span class="o">-</span> <span class="p">(</span><span class="n">b</span> <span class="o">//</span> <span class="n">a</span><span class="p">)</span> <span class="o">*</span> <span class="n">x1</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">x1</span>
<span class="k">return</span> <span class="n">gcd</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
</code></pre></div></td></tr></table></div>
</details>
</div>
</div>
<div class="doc doc-object doc-function">
<h2 id="Cryptotools.Utils.utils.exponent_squaring" class="doc doc-heading">
<code class="highlight language-python"><span class="n">exponent_squaring</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span></code>
</h2>
<div class="doc doc-contents ">
<p>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
}</p>
<table class="field-list">
<colgroup>
<col class="field-name" />
<col class="field-body" />
</colgroup>
<tbody valign="top">
<tr class="field">
<th class="field-name">Parameters:</th>
<td class="field-body">
<ul class="first simple">
<li>
<b><code>n</code></b>
(<code><span title="Integer">Integer</span></code>)
<div class="doc-md-description">
<p>n is the base of n ** e</p>
</div>
</li>
<li>
<b><code>e</code></b>
(<code><span title="Integer">Integer</span></code>)
<div class="doc-md-description">
<p>e is the exponent</p>
</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<details class="quote">
<summary>Source code in <code>Cryptotools/Utils/utils.py</code></summary>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">64</span>
<span class="normal">65</span>
<span class="normal">66</span>
<span class="normal">67</span>
<span class="normal">68</span>
<span class="normal">69</span>
<span class="normal">70</span>
<span class="normal">71</span>
<span class="normal">72</span>
<span class="normal">73</span>
<span class="normal">74</span>
<span class="normal">75</span>
<span class="normal">76</span>
<span class="normal">77</span>
<span class="normal">78</span>
<span class="normal">79</span>
<span class="normal">80</span>
<span class="normal">81</span>
<span class="normal">82</span>
<span class="normal">83</span>
<span class="normal">84</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="k">def</span> <span class="nf">exponent_squaring</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">e</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> This function perform an exponentiation squaring, which compute an integer power of a number based on the following algorithm:</span>
<span class="sd"> n ** e = {</span>
<span class="sd"> 1 # if n is 0</span>
<span class="sd"> (n ** (e / 2)) ** 2 # if n is even</span>
<span class="sd"> ((n ** (e - 1 / 2)) ** 2) * n # if n is odd</span>
<span class="sd"> }</span>
<span class="sd"> Args:</span>
<span class="sd"> n (Integer): n is the base of n ** e</span>
<span class="sd"> e (Integer): e is the exponent</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">e</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="n">exponent_squaring</span><span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">n</span><span class="p">,</span> <span class="o">-</span><span class="n">e</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">e</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="mi">1</span>
<span class="k">elif</span> <span class="n">e</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> <span class="c1"># n is odd</span>
<span class="k">return</span> <span class="n">exponent_squaring</span><span class="p">(</span><span class="n">n</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span> <span class="p">(</span><span class="n">e</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">n</span>
<span class="k">elif</span> <span class="n">e</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="c1"># n is even</span>
<span class="k">return</span> <span class="n">exponent_squaring</span><span class="p">(</span><span class="n">n</span> <span class="o">*</span> <span class="n">n</span><span class="p">,</span> <span class="n">e</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
</code></pre></div></td></tr></table></div>
</details>
</div>
</div>
<div class="doc doc-object doc-function">
<h2 id="Cryptotools.Utils.utils.gcd" class="doc doc-heading">
<code class="highlight language-python"><span class="n">gcd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span></code>
</h2>
<div class="doc doc-contents ">
<p>This function calculate the GCD (Greatest Common Divisor of the number a of b
Args:
a (Integer): the number a
b (integer): the number b</p>
<details class="return" open>
<summary>Return</summary>
<p>the GCD</p>
</details>
<details class="quote">
<summary>Source code in <code>Cryptotools/Utils/utils.py</code></summary>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span>
<span class="normal">16</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="k">def</span> <span class="nf">gcd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> This function calculate the GCD (Greatest Common Divisor of the number a of b</span>
<span class="sd"> Args:</span>
<span class="sd"> a (Integer): the number a</span>
<span class="sd"> b (integer): the number b</span>
<span class="sd"> Return:</span>
<span class="sd"> the GCD </span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">b</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="n">a</span>
<span class="k">return</span> <span class="n">gcd</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">%</span><span class="n">b</span><span class="p">)</span>
</code></pre></div></td></tr></table></div>
</details>
</div>
</div>
</div>
</div>
</div>
</div>
</div><footer>
<div class="rst-footer-buttons" role="navigation" aria-label="Footer Navigation">
<a href="../rsa/" class="btn btn-neutral float-left" title="RSA"><span class="icon icon-circle-arrow-left"></span> Previous</a>
<a href="../example-rsa-keys/" class="btn btn-neutral float-right" title="Generating RSA Keys">Next <span class="icon icon-circle-arrow-right"></span></a>
</div>
<hr/>
<div role="contentinfo">
<!-- Copyright etc -->
</div>
Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<div class="rst-versions" role="note" aria-label="Versions">
<span class="rst-current-version" data-toggle="rst-current-version">
<span><a href="../rsa/" style="color: #fcfcfc">&laquo; Previous</a></span>
<span><a href="../example-rsa-keys/" style="color: #fcfcfc">Next &raquo;</a></span>
</span>
</div>
<script src="../js/jquery-3.6.0.min.js"></script>
<script>var base_url = "..";</script>
<script src="../js/theme_extra.js"></script>
<script src="../js/theme.js"></script>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>