kryptools 0.9.1__tar.gz → 0.9.2__tar.gz
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-0.9.1 → kryptools-0.9.2}/PKG-INFO +1 -1
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/__init__.py +2 -2
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/factor.py +35 -4
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/lat.py +2 -2
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/nt.py +22 -3
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools.egg-info/PKG-INFO +1 -1
- {kryptools-0.9.1 → kryptools-0.9.2}/pyproject.toml +1 -1
- {kryptools-0.9.1 → kryptools-0.9.2}/LICENSE +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/README.md +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/Zmod.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/dlp.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/dlp_bsgs.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/dlp_ic.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/dlp_qs.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/dlp_rho.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/ec.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/factor_dix.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/factor_ecm.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/factor_fmt.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/factor_pm1.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/factor_qs.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/la.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/poly.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools/primes.py +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools.egg-info/SOURCES.txt +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools.egg-info/dependency_links.txt +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/kryptools.egg-info/top_level.txt +0 -0
- {kryptools-0.9.1 → kryptools-0.9.2}/setup.cfg +0 -0
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Implemenation of same basic algorithms used in cryptography.
|
|
3
3
|
"""
|
|
4
4
|
|
|
5
|
-
from .nt import egcd, cf, convergents, legendre_symbol, jacobi_symbol, sqrt_mod, euler_phi, carmichael_lambda, order, crt
|
|
5
|
+
from .nt import egcd, cf, convergents, legendre_symbol, jacobi_symbol, sqrt_mod, euler_phi, carmichael_lambda, moebius_mu, order, crt
|
|
6
6
|
from .primes import sieve_eratosthenes, is_prime, next_prime, previous_prime, random_prime, random_strongprime, is_safeprime, random_safeprime
|
|
7
|
-
from .factor import factorint
|
|
7
|
+
from .factor import factorint, divisors
|
|
8
8
|
from .dlp import dlog
|
|
9
9
|
from .ec import EC_Weierstrass
|
|
10
10
|
from .la import Matrix, zeros, eye
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Factorization of integers:
|
|
3
3
|
factorint(n) factorize the integer n into prime factors
|
|
4
|
+
divisors(n) unsorted list of all divisors of the integer n
|
|
4
5
|
"""
|
|
5
6
|
|
|
6
|
-
from math import isqrt, gcd
|
|
7
|
+
from math import isqrt, gcd, prod
|
|
7
8
|
from .primes import sieve_eratosthenes, is_prime
|
|
8
9
|
from .factor_pm1 import _pm1_parameters, factor_pm1
|
|
9
10
|
from .factor_ecm import _ecm_parameters, factor_ecm
|
|
@@ -13,7 +14,6 @@ from .factor_ecm import _ecm_parameters, factor_ecm
|
|
|
13
14
|
# Factoring
|
|
14
15
|
|
|
15
16
|
|
|
16
|
-
|
|
17
17
|
def _factor_fermat(n: int, steps: int = 10) -> list:
|
|
18
18
|
"Fermat method"
|
|
19
19
|
parameters = {11: (12, 6), 23: (12, 0),
|
|
@@ -29,12 +29,12 @@ def _factor_fermat(n: int, steps: int = 10) -> list:
|
|
|
29
29
|
return a - b
|
|
30
30
|
|
|
31
31
|
def factorint(n: int, verbose: int = 0) -> dict:
|
|
32
|
-
"Factor
|
|
32
|
+
"Factor an ineger `n` into prime factors."
|
|
33
33
|
prime_factors = {}
|
|
34
34
|
if not isinstance(n, int):
|
|
35
35
|
raise ValueError("Number to be factored must be an integer!")
|
|
36
36
|
if n == 0:
|
|
37
|
-
return
|
|
37
|
+
return {0: 1}
|
|
38
38
|
if n < 0:
|
|
39
39
|
n *= -1
|
|
40
40
|
prime_factors[-1] = 1
|
|
@@ -140,3 +140,34 @@ def factorint(n: int, verbose: int = 0) -> dict:
|
|
|
140
140
|
return prime_factors
|
|
141
141
|
|
|
142
142
|
raise ValueError("Incomplete factorization:", prime_factors, remaining_factors, new_factors)
|
|
143
|
+
|
|
144
|
+
# Divisors
|
|
145
|
+
|
|
146
|
+
def divisors(n:int, proper:bool = False) -> int:
|
|
147
|
+
"""Returns an unsorted list of all divisors of an integer `n`."""
|
|
148
|
+
facctordict= factorint(n)
|
|
149
|
+
primes = [p for p in facctordict]
|
|
150
|
+
nprimes = len(primes)
|
|
151
|
+
multiplicites = [facctordict[p] for p in primes]
|
|
152
|
+
|
|
153
|
+
exponents = [0] * nprimes
|
|
154
|
+
if proper:
|
|
155
|
+
divisors = []
|
|
156
|
+
else:
|
|
157
|
+
divisors = [1]
|
|
158
|
+
i = 0
|
|
159
|
+
while True:
|
|
160
|
+
exponents[i] += 1
|
|
161
|
+
if exponents[i] > multiplicites[i]:
|
|
162
|
+
while exponents[i] > multiplicites[i]:
|
|
163
|
+
exponents[i] = 0
|
|
164
|
+
i += 1
|
|
165
|
+
exponents[i] += 1
|
|
166
|
+
i = 0
|
|
167
|
+
d = prod(primes[i] ** exponents[i] for i in range(nprimes))
|
|
168
|
+
if d == n:
|
|
169
|
+
if proper:
|
|
170
|
+
return divisors
|
|
171
|
+
divisors.append(n)
|
|
172
|
+
return divisors
|
|
173
|
+
divisors.append(d)
|
|
@@ -81,7 +81,7 @@ def babai_round_cvp(x: Matrix, U: Matrix) -> Matrix:
|
|
|
81
81
|
return U * k
|
|
82
82
|
|
|
83
83
|
def babai_round_bnd(U: Matrix) -> float:
|
|
84
|
-
"Bound for Babai's rounding algorithm for solving the
|
|
84
|
+
"Bound for Babai's rounding algorithm for solving the CVPwith respect to the sup norm."
|
|
85
85
|
return floor(1 / (2 * max([ U.inv()[i,:].norm(1) for i in range(U.rows)])))
|
|
86
86
|
|
|
87
87
|
def babai_plane_cvp(x: Matrix, U: Matrix) -> Matrix:
|
|
@@ -93,7 +93,7 @@ def babai_plane_cvp(x: Matrix, U: Matrix) -> Matrix:
|
|
|
93
93
|
return (x - y).applyfunc(round)
|
|
94
94
|
|
|
95
95
|
def babai_plane_bnd(U: Matrix, p = 2) -> float:
|
|
96
|
-
"Bound for Babai's closest plane algorithm for solving the CVP."
|
|
96
|
+
"Bound for Babai's closest plane algorithm for solving the CVP with respect to the Euclidean norm (p=2) or sup norm (p=inf)."
|
|
97
97
|
Us = gram_schmidt(U)[0]
|
|
98
98
|
return float(0.5 * min([Us[:, i].norm(p) for i in range(Us.rows)]))
|
|
99
99
|
|
|
@@ -8,6 +8,7 @@ Number theory tools:
|
|
|
8
8
|
sqrt_mod(n, p) square root of n modulo a prime p
|
|
9
9
|
euler_phi(n) Euler phi function of n
|
|
10
10
|
carmichael_lambda(n) Carmichael lambda function of n
|
|
11
|
+
moebius_mu(n) Moebius function of n
|
|
11
12
|
order(a, n) oder of a in the multiplicative group Z_n^*
|
|
12
13
|
crt([a1, a2, ...],[m1, m2, ...]) Chinese Remainder Theorem
|
|
13
14
|
"""
|
|
@@ -99,7 +100,7 @@ def jacobi_symbol(a: int, n: int) -> int:
|
|
|
99
100
|
return 0
|
|
100
101
|
|
|
101
102
|
def sqrt_mod(a: int, p: int) -> int:
|
|
102
|
-
"Compute a square root of `a` modulo `p` unsing Cipolla's algorithm."
|
|
103
|
+
"""Compute a square root of `a` modulo `p` unsing Cipolla's algorithm."""
|
|
103
104
|
a %= p
|
|
104
105
|
if a == 0 or a == 1:
|
|
105
106
|
return a
|
|
@@ -125,19 +126,23 @@ def sqrt_mod(a: int, p: int) -> int:
|
|
|
125
126
|
i = i >> 1 # i= i/2
|
|
126
127
|
return y1
|
|
127
128
|
|
|
128
|
-
# Euler phi
|
|
129
|
+
# Euler phi, Carmichael function, Moebius function
|
|
129
130
|
|
|
130
131
|
from .factor import factorint
|
|
131
132
|
|
|
132
133
|
|
|
133
134
|
def euler_phi(n: int) -> int:
|
|
134
135
|
"""Euler's phi function of `n`."""
|
|
136
|
+
if not isinstance(n, int) or n < 1:
|
|
137
|
+
raise ValueError(f"{n} is not a positive integer")
|
|
135
138
|
k = factorint(n)
|
|
136
139
|
return prod([(p - 1) * p ** (k[p] - 1) for p in k])
|
|
137
140
|
|
|
138
141
|
|
|
139
142
|
def carmichael_lambda(n: int) -> int:
|
|
140
143
|
"""Carmichael's lambda function of `n`."""
|
|
144
|
+
if not isinstance(n, int) or n < 1:
|
|
145
|
+
raise ValueError(f"{n} is not a positive integer")
|
|
141
146
|
k = factorint(n)
|
|
142
147
|
lam_all = [] # values corresponding to the prime factors
|
|
143
148
|
for p in k:
|
|
@@ -150,12 +155,26 @@ def carmichael_lambda(n: int) -> int:
|
|
|
150
155
|
lam = lcm(lam, l)
|
|
151
156
|
return lam
|
|
152
157
|
|
|
158
|
+
def moebius_mu(n: int) -> int:
|
|
159
|
+
"""Moebius' mu function of `n`."""
|
|
160
|
+
if not isinstance(n, int) or n < 0:
|
|
161
|
+
raise ValueError(f"{n} is not a nonegative integer")
|
|
162
|
+
if n == 0:
|
|
163
|
+
return 0
|
|
164
|
+
factors = factorint(n)
|
|
165
|
+
if any(i > 1 for i in factors.values()):
|
|
166
|
+
return 0
|
|
167
|
+
if len(factors) % 2 == 1:
|
|
168
|
+
return -1
|
|
169
|
+
return 1
|
|
170
|
+
|
|
153
171
|
# Order in Z_p^*
|
|
154
172
|
|
|
155
173
|
def order(a: int, n: int, factor=False) -> int:
|
|
156
174
|
"""Compute the order of `a` in the group Z_n^*."""
|
|
157
175
|
a %= n
|
|
158
|
-
|
|
176
|
+
if a == 0 or gcd(a, n) != 1:
|
|
177
|
+
raise ValueError(f"{a} and {n} are not coprime!")
|
|
159
178
|
factors = {} # We compute euler_phi(n) and its factorization in one pass
|
|
160
179
|
for p, k in factorint(n).items(): # first factorize n
|
|
161
180
|
for pm, km in factorint(p - 1).items(): # factor p-1 and add the factors
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|