kryptools 0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kryptools/Zmod.py +115 -0
- kryptools/__init__.py +13 -0
- kryptools/dlp.py +76 -0
- kryptools/dlp_bsgs.py +29 -0
- kryptools/dlp_ic.py +136 -0
- kryptools/dlp_qs.py +297 -0
- kryptools/dlp_rho.py +62 -0
- kryptools/ec.py +383 -0
- kryptools/factor.py +138 -0
- kryptools/factor_ecm.py +140 -0
- kryptools/factor_pm1.py +58 -0
- kryptools/factor_qs.py +174 -0
- kryptools/la.py +223 -0
- kryptools/lat.py +168 -0
- kryptools/nt.py +271 -0
- kryptools/poly.py +232 -0
- kryptools/primes.py +123 -0
- kryptools-0.1.dist-info/LICENSE +21 -0
- kryptools-0.1.dist-info/METADATA +35 -0
- kryptools-0.1.dist-info/RECORD +22 -0
- kryptools-0.1.dist-info/WHEEL +5 -0
- kryptools-0.1.dist-info/top_level.txt +1 -0
kryptools/primes.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Tools for prime numbers:
|
|
3
|
+
sieve_eratosthenes(B) a tuple of all primes up to including B
|
|
4
|
+
isprime(n) test if n is probably prime
|
|
5
|
+
miller_rabin_test(n, b) Miller-Rabin primality test with base b
|
|
6
|
+
"""
|
|
7
|
+
from math import isqrt
|
|
8
|
+
|
|
9
|
+
# Erathostenes
|
|
10
|
+
|
|
11
|
+
def sieve_eratosthenes(B: int) -> list:
|
|
12
|
+
""" "Returns a list of all primes up to (including) max."""
|
|
13
|
+
B1 = (isqrt(B) -1)//2
|
|
14
|
+
B = (B - 1)//2
|
|
15
|
+
is_prime = [True] * (B + 1) # to begin with, all numbers are potentially prime
|
|
16
|
+
# sieve out the primes p=2*q+1 starting at 3 in steps of 2 (ignoring even numbers)
|
|
17
|
+
for q in range(1, B1 + 1):
|
|
18
|
+
if is_prime[q]: # sieve out all multiples; numbers p*q with q<p were already sieved out previously
|
|
19
|
+
qq = (q << 1) * (q + 1)
|
|
20
|
+
p = (q << 1) | 1
|
|
21
|
+
is_prime[qq :: p] = [False] * ((B - qq) // p + 1)
|
|
22
|
+
|
|
23
|
+
return tuple([2] + [2 * q + 1 for q in range(1, B + 1) if is_prime[q]])
|
|
24
|
+
|
|
25
|
+
# Primality testing
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def miller_rabin_test(n: int, bases: list[int] | int) -> bool:
|
|
29
|
+
"""Run a Miller-Rabin test with given bases on n."""
|
|
30
|
+
if n < 2:
|
|
31
|
+
return False
|
|
32
|
+
if n % 2 == 0:
|
|
33
|
+
return n == 2
|
|
34
|
+
m = (n - 1) // 2
|
|
35
|
+
k = 1
|
|
36
|
+
while m % 2 == 0:
|
|
37
|
+
m //= 2
|
|
38
|
+
k += 1
|
|
39
|
+
if isinstance(bases, int):
|
|
40
|
+
bases = [bases]
|
|
41
|
+
for a in bases:
|
|
42
|
+
b = pow(a, m, n)
|
|
43
|
+
if b == 1 or b == n - 1:
|
|
44
|
+
return True
|
|
45
|
+
for _ in range(1, k):
|
|
46
|
+
b = pow(b, 2, n)
|
|
47
|
+
if b == 1:
|
|
48
|
+
return False
|
|
49
|
+
if b == n - 1:
|
|
50
|
+
return True
|
|
51
|
+
return False
|
|
52
|
+
|
|
53
|
+
def isprime(n: int) -> bool:
|
|
54
|
+
"""Test if an integer n if probable prime."""
|
|
55
|
+
if n < 18446744073709551616: # https://miller-rabin.appspot.com
|
|
56
|
+
return miller_rabin_test(n, [2, 325, 9375, 28178, 450775, 9780504, 1795265022])
|
|
57
|
+
if n < 3317044064679887385961981:
|
|
58
|
+
return miller_rabin_test(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41])
|
|
59
|
+
return miller_rabin_test(n, [2]) and _is_strong_lucas_prp(n) # Baillie–PSW primality test
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _lucas_sequence(n, D, k):
|
|
63
|
+
"""Evaluate a Lucas sequence."""
|
|
64
|
+
# P = 1
|
|
65
|
+
Q = (1 - D)//4
|
|
66
|
+
U = 1
|
|
67
|
+
V = 1
|
|
68
|
+
Qk = Q
|
|
69
|
+
b = k.bit_length()
|
|
70
|
+
while b > 1:
|
|
71
|
+
U = (U * V) % n
|
|
72
|
+
V = (V * V - 2 * Qk) % n
|
|
73
|
+
Qk *= Qk
|
|
74
|
+
b -= 1
|
|
75
|
+
if (k >> (b - 1)) & 1:
|
|
76
|
+
U, V = U + V, V + U * D
|
|
77
|
+
if U & 1:
|
|
78
|
+
U += n
|
|
79
|
+
if V & 1:
|
|
80
|
+
V += n
|
|
81
|
+
U, V = U >> 1, V >> 1
|
|
82
|
+
Qk *= Q
|
|
83
|
+
Qk %= n
|
|
84
|
+
return U % n, V % n, Qk
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _is_strong_lucas_prp(n: int) -> bool:
|
|
88
|
+
"""Strong Lucas primality test."""
|
|
89
|
+
from math import gcd
|
|
90
|
+
from .nt import jacobi_symbol
|
|
91
|
+
|
|
92
|
+
# remove powers of 2 from n+1 (= k * 2**s)
|
|
93
|
+
k = (n + 1) // 2
|
|
94
|
+
s = 1
|
|
95
|
+
while k % 2 == 0:
|
|
96
|
+
k //= 2
|
|
97
|
+
s += 1
|
|
98
|
+
# gernerate parameters
|
|
99
|
+
D = 5
|
|
100
|
+
while True:
|
|
101
|
+
g = gcd(abs(D), n)
|
|
102
|
+
if 1 < g < n:
|
|
103
|
+
return False
|
|
104
|
+
if jacobi_symbol(D, n) == -1:
|
|
105
|
+
break
|
|
106
|
+
if D > 0:
|
|
107
|
+
D = -D - 2
|
|
108
|
+
else:
|
|
109
|
+
D = -D + 2
|
|
110
|
+
|
|
111
|
+
if D == 0:
|
|
112
|
+
return False
|
|
113
|
+
|
|
114
|
+
U, V, Qk = _lucas_sequence(n, D, k)
|
|
115
|
+
|
|
116
|
+
if U == 0 or V == 0:
|
|
117
|
+
return True
|
|
118
|
+
for _ in range(1, s):
|
|
119
|
+
V = (V*V - 2*Qk) % n
|
|
120
|
+
if V == 0:
|
|
121
|
+
return True
|
|
122
|
+
Qk = pow(Qk, 2, n)
|
|
123
|
+
return False
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Gerald Teschl
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: kryptools
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: Implemenation of same basic algorithms used in cryptography.
|
|
5
|
+
Author-email: Gerald Teschl <gerald.teschl@univie.ac.at>
|
|
6
|
+
Project-URL: Homepage, https://github.com/teschlg/kryptools
|
|
7
|
+
Project-URL: Issues, https://github.com/teschlg/kryptools/issues
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
|
|
15
|
+
# kryptools
|
|
16
|
+
Gerald Teschl <Gerald.Teschl@univie.ac.at>
|
|
17
|
+
|
|
18
|
+
This package was written for my course on cryptography. Consequently its intention is
|
|
19
|
+
mainly educational, that is, to show how basic algorithms are implemented. In particular,
|
|
20
|
+
you are welcome to read (and modify) the source. Any suggestions on how to make the
|
|
21
|
+
code more readable or make it better are welcome. However, my main goal is to keep
|
|
22
|
+
it simple and readability will be preferred over small speed improvements.
|
|
23
|
+
|
|
24
|
+
The tools contained are:
|
|
25
|
+
|
|
26
|
+
* number theory: sqrt modulo primes, crt, continued fractions, etc.
|
|
27
|
+
* primes: Sieve of Erathostenes, primality tests
|
|
28
|
+
* solvers for discrete logarithms (naive, Pollard rho, Shanks baby step/giant step, index calculus, quadratic sieve)
|
|
29
|
+
* integer factorization (Pollard p-1, Lentra's ECM, Dixon, basic quadratic sieve)
|
|
30
|
+
* linear algebra: Hermite normal form, Gram-Schmidt
|
|
31
|
+
* lattices: Babai rounding/nearest plane, lattice reduction
|
|
32
|
+
|
|
33
|
+
* Matrix: a class for Matrices (inverse, det, reduced echelon form, etc.)
|
|
34
|
+
* Poly: a class for polynomials (division, modulo)
|
|
35
|
+
* Zmod: a class for the ring of integers modulo an integer
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
kryptools/Zmod.py,sha256=a6o9G4zYY2gVAfFk26Rp7A5PBq7lZwfPGpcoLvIiD9A,3601
|
|
2
|
+
kryptools/__init__.py,sha256=Y11XqaBakobArs2pPy4npuvgxXwx5KP5DTFHR80Sku8,411
|
|
3
|
+
kryptools/dlp.py,sha256=7uwpG19KchCCLm_O3I4kjzhr3Q4My81pISpnjE5ENsw,2462
|
|
4
|
+
kryptools/dlp_bsgs.py,sha256=QXrEUXy8xBWUYUSszd_91q0fFkgS_fGP8lBqecK3uts,803
|
|
5
|
+
kryptools/dlp_ic.py,sha256=EhbE5dTRBIAkrwvyZB2SpmtDYa2sYrRPcGkd4P6aeiw,5660
|
|
6
|
+
kryptools/dlp_qs.py,sha256=_PU9hCNEsarQHnHCvIGEpVmEUeJy3GKLre53iIW6fI4,12328
|
|
7
|
+
kryptools/dlp_rho.py,sha256=_eKsPtcEyuqwIppajDJD_Yg2bpJQnhHV3RavjK4uPjs,2357
|
|
8
|
+
kryptools/ec.py,sha256=vkZqxiBrU65CD0HTji5VaUoWlxojjJwY2NGmjZgPuIY,12924
|
|
9
|
+
kryptools/factor.py,sha256=ww9iemZoryFq4EeyuyF1scyULuRggoDy5nCx4afoy2c,4763
|
|
10
|
+
kryptools/factor_ecm.py,sha256=mjfC3qplwRwdJ6CQWFO9osZkkfypigzBBnfl3zgSxs8,3773
|
|
11
|
+
kryptools/factor_pm1.py,sha256=po0xL9AsgMPudSUvDHlaoBNXt0IGQOAWfp992K-_ho8,1401
|
|
12
|
+
kryptools/factor_qs.py,sha256=Owd8y5dVI4dA7uAZJ7d1JG8mY2lyOaV37MxVERoD7mw,7290
|
|
13
|
+
kryptools/la.py,sha256=_5sib8tieZpH6jCdk4s4xZNzO_6AlIWC9BWl_Q-ofz8,7621
|
|
14
|
+
kryptools/lat.py,sha256=F0C87RIwUSP71djPoZbO1NIb4A1zrK6JsrT708DdsP8,5739
|
|
15
|
+
kryptools/nt.py,sha256=Uvij66tsw4fX5VfkD0IN33cKNvV1mX2qfb73VB4oRJk,8950
|
|
16
|
+
kryptools/poly.py,sha256=TUUWebAQbcb5QLeh_-BnJJN73cztKGa-QqT54WZ2m1o,7678
|
|
17
|
+
kryptools/primes.py,sha256=zRdu2qo77m9ZwVYzJ4Yta22BZ9f7sVGX5tpHzB8dNPk,3329
|
|
18
|
+
kryptools-0.1.dist-info/LICENSE,sha256=nbdxuiueFAHRMXONMrQxyVQN2Eaihou4_txaLrrxKy4,1070
|
|
19
|
+
kryptools-0.1.dist-info/METADATA,sha256=Ez-BNCP9voyB8PPj8zPQE4o0rQ_1aAyvvT8IDRDKYFQ,1649
|
|
20
|
+
kryptools-0.1.dist-info/WHEEL,sha256=mguMlWGMX-VHnMpKOjjQidIo1ssRlCFu4a4mBpz1s2M,91
|
|
21
|
+
kryptools-0.1.dist-info/top_level.txt,sha256=jliXVAiLrIHcNLJgMHbQjo2F2Q3Ospv1jx6BAIM-6gI,10
|
|
22
|
+
kryptools-0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
kryptools
|