kryptools 0.9.3__tar.gz → 0.9.4__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.
Files changed (28) hide show
  1. {kryptools-0.9.3 → kryptools-0.9.4}/PKG-INFO +1 -1
  2. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/__init__.py +1 -1
  3. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/primes.py +79 -72
  4. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools.egg-info/PKG-INFO +1 -1
  5. {kryptools-0.9.3 → kryptools-0.9.4}/pyproject.toml +1 -1
  6. {kryptools-0.9.3 → kryptools-0.9.4}/LICENSE +0 -0
  7. {kryptools-0.9.3 → kryptools-0.9.4}/README.md +0 -0
  8. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/Zmod.py +0 -0
  9. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/dlp.py +0 -0
  10. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/dlp_bsgs.py +0 -0
  11. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/dlp_ic.py +0 -0
  12. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/dlp_qs.py +0 -0
  13. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/dlp_rho.py +0 -0
  14. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/ec.py +0 -0
  15. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/factor.py +0 -0
  16. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/factor_dix.py +0 -0
  17. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/factor_ecm.py +0 -0
  18. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/factor_fmt.py +0 -0
  19. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/factor_pm1.py +0 -0
  20. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/factor_qs.py +0 -0
  21. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/la.py +0 -0
  22. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/lat.py +0 -0
  23. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/nt.py +0 -0
  24. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools/poly.py +0 -0
  25. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools.egg-info/SOURCES.txt +0 -0
  26. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools.egg-info/dependency_links.txt +0 -0
  27. {kryptools-0.9.3 → kryptools-0.9.4}/kryptools.egg-info/top_level.txt +0 -0
  28. {kryptools-0.9.3 → kryptools-0.9.4}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kryptools
3
- Version: 0.9.3
3
+ Version: 0.9.4
4
4
  Summary: Implemenation of same basic algorithms used in cryptography.
5
5
  Author-email: Gerald Teschl <gerald.teschl@univie.ac.at>
6
6
  Project-URL: Homepage, https://github.com/teschlg/kryptools
@@ -3,7 +3,7 @@ Implemenation of same basic algorithms used in cryptography.
3
3
  """
4
4
 
5
5
  from .nt import egcd, cf, convergents, legendre_symbol, jacobi_symbol, sqrt_mod, euler_phi, carmichael_lambda, moebius_mu, order, crt
6
- from .primes import sieve_eratosthenes, prime_pi, is_prime, next_prime, previous_prime, random_prime, random_strongprime, is_safeprime, random_safeprime
6
+ from .primes import sieve_eratosthenes, prime_pi, is_prime, next_prime, previous_prime, random_prime, random_strongprime, is_safeprime, random_safeprime, is_blumprime, random_blumprime, miller_rabin_test, lucas_test
7
7
  from .factor import factorint, divisors
8
8
  from .dlp import dlog
9
9
  from .ec import EC_Weierstrass
@@ -12,8 +12,9 @@ Tools for prime numbers:
12
12
  is_blumprime(n) test if n is a Blum prime
13
13
  random_blumprime(l) find a pseudorandom Blum prime with bit length at least l
14
14
  miller_rabin_test(n, b) Miller-Rabin primality test with base b
15
+ lucas_test(n) strong Lucas test
15
16
  """
16
- from math import isqrt, gcd, floor
17
+ from math import isqrt, gcd, floor, ceil
17
18
  from random import randint
18
19
  from .nt import jacobi_symbol
19
20
 
@@ -71,75 +72,6 @@ def miller_rabin_test(n: int, bases: list[int] | int) -> bool:
71
72
  return True
72
73
  return False
73
74
 
74
- def is_prime(n: int) -> bool:
75
- """Test if an integer `n` if probable prime."""
76
- if n < 18446744073709551616: # https://miller-rabin.appspot.com
77
- return miller_rabin_test(n, [2, 325, 9375, 28178, 450775, 9780504, 1795265022])
78
- if n < 3317044064679887385961981:
79
- return miller_rabin_test(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41])
80
- return miller_rabin_test(n, [2]) and _is_strong_lucas_prp(n) # Baillie–PSW primality test
81
-
82
- def next_prime(n: int) -> int:
83
- """Find the next prime larger or equal `n`."""
84
- if n < 3:
85
- return 2
86
- n |= 1 # make sure n is odd
87
- while not is_prime(n):
88
- n += 2
89
- return n
90
-
91
- def previous_prime(n: int) -> int:
92
- """Find the previous prime smaller or equal `n`."""
93
- if n < 3:
94
- return 2
95
- n += (n & 1) - 1 # make sure n is odd
96
- while not is_prime(n):
97
- n -= 2
98
- return n
99
-
100
- def random_prime(l: int) -> int:
101
- """Find a pseudorandom prime with bit length at least `l`."""
102
- return next_prime(randint(2 ** (l - 1), 2**l - 1))
103
-
104
- def random_strongprime(l: int) -> int:
105
- """Find a pseudorandom strong prime with bit length at least `l` using Gordon's algorithm."""
106
- t = random_prime(l)
107
- s = random_prime(l)
108
- u = 2 * t
109
- uu = u * randint(1, 100)
110
- while not is_prime(uu + 1):
111
- uu += u
112
- r = uu + 1
113
- u = 2 * r * s
114
- uu = u * randint(1, 100) + 2 * s * pow(s, r - 2, r) - 1
115
- while not is_prime(uu):
116
- uu += u
117
- return t, s, r, uu
118
-
119
- def is_safeprime(p: int) -> bool:
120
- """Tests if a number is a safe prime."""
121
- return is_prime(p) and is_prime((p - 1) // 2)
122
-
123
- def random_safeprime(l: int) -> int:
124
- """Find a pseudorandom safe prime with bit length at least `l` and `ord(2)=(p-1)/2`."""
125
- p = randint(2 ** (l - 1), 2**l - 1)
126
- p = p - (p % 24) + 23
127
- while not is_safeprime(p):
128
- p += 24
129
- return p
130
-
131
- def is_blumprime(p: int) -> bool:
132
- """Tests if a number is a Blum prime."""
133
- return p % 4 == 3 and is_prime(p)
134
-
135
- def random_blumprime(l: int) -> int:
136
- """Find a pseudorandom Blum prime with bit length at least `l`."""
137
- p = randint(2 ** (l - 1), 2**l - 1)
138
- p = p - (p % 4) + 3
139
- while not is_prime(p):
140
- p += 4
141
- return p
142
-
143
75
  def _lucas_sequence(n, D, k):
144
76
  """Evaluate a Lucas sequence."""
145
77
  # P = 1
@@ -165,8 +97,8 @@ def _lucas_sequence(n, D, k):
165
97
  return U % n, V % n, Qk
166
98
 
167
99
 
168
- def _is_strong_lucas_prp(n: int) -> bool:
169
- """Strong Lucas primality test."""
100
+ def lucas_test(n: int) -> bool:
101
+ """Run a strong Lucas primality test on `n`."""
170
102
 
171
103
  # remove powers of 2 from n+1 (= k * 2**s)
172
104
  k = (n + 1) // 2
@@ -200,3 +132,78 @@ def _is_strong_lucas_prp(n: int) -> bool:
200
132
  return True
201
133
  Qk = pow(Qk, 2, n)
202
134
  return False
135
+
136
+ def is_prime(n: int) -> bool:
137
+ """Test if an integer `n` if probable prime."""
138
+ if n < 18446744073709551616: # https://miller-rabin.appspot.com
139
+ return miller_rabin_test(n, [2, 325, 9375, 28178, 450775, 9780504, 1795265022])
140
+ if n < 3317044064679887385961981:
141
+ return miller_rabin_test(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41])
142
+ return miller_rabin_test(n, [2]) and lucas_test(n) # Baillie–PSW primality test
143
+
144
+ def is_safeprime(p: int) -> bool:
145
+ """Tests if a number is a safe prime."""
146
+ return is_prime(p) and is_prime((p - 1) // 2)
147
+
148
+ def is_blumprime(p: int) -> bool:
149
+ """Tests if a number is a Blum prime."""
150
+ return p % 4 == 3 and is_prime(p)
151
+
152
+
153
+ # Generation of primes
154
+
155
+
156
+ def next_prime(n: int) -> int:
157
+ """Find the next prime larger or equal `n`."""
158
+ if n < 3:
159
+ return 2
160
+ n = ceil(n)
161
+ n |= 1 # make sure n is odd
162
+ while not is_prime(n):
163
+ n += 2
164
+ return n
165
+
166
+ def previous_prime(n: int) -> int:
167
+ """Find the previous prime smaller or equal `n`."""
168
+ if n < 3:
169
+ return 2
170
+ n = floor(n)
171
+ n += (n & 1) - 1 # make sure n is odd
172
+ while not is_prime(n):
173
+ n -= 2
174
+ return n
175
+
176
+ def random_prime(l: int) -> int:
177
+ """Find a pseudorandom prime with bit length at least `l`."""
178
+ return next_prime(randint(2 ** (l - 1), 2**l - 1))
179
+
180
+ def random_strongprime(l: int) -> int:
181
+ """Find a pseudorandom strong prime with bit length at least `l` using Gordon's algorithm."""
182
+ t = random_prime(l)
183
+ s = random_prime(l)
184
+ u = 2 * t
185
+ uu = u * randint(1, 100)
186
+ while not is_prime(uu + 1):
187
+ uu += u
188
+ r = uu + 1
189
+ u = 2 * r * s
190
+ uu = u * randint(1, 100) + 2 * s * pow(s, r - 2, r) - 1
191
+ while not is_prime(uu):
192
+ uu += u
193
+ return t, s, r, uu
194
+
195
+ def random_safeprime(l: int) -> int:
196
+ """Find a pseudorandom safe prime with bit length at least `l` and `ord(2)=(p-1)/2`."""
197
+ p = randint(2 ** (l - 1), 2**l - 1)
198
+ p = p - (p % 24) + 23 # make sure p % 24 = 23
199
+ while not is_safeprime(p):
200
+ p += 24
201
+ return p
202
+
203
+ def random_blumprime(l: int) -> int:
204
+ """Find a pseudorandom Blum prime with bit length at least `l`."""
205
+ p = randint(2 ** (l - 1), 2**l - 1)
206
+ p = p - (p % 4) + 3 # make sure p % 4 = 3
207
+ while not is_prime(p):
208
+ p += 4
209
+ return p
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kryptools
3
- Version: 0.9.3
3
+ Version: 0.9.4
4
4
  Summary: Implemenation of same basic algorithms used in cryptography.
5
5
  Author-email: Gerald Teschl <gerald.teschl@univie.ac.at>
6
6
  Project-URL: Homepage, https://github.com/teschlg/kryptools
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "kryptools"
3
- version = "0.9.3"
3
+ version = "0.9.4"
4
4
  authors = [
5
5
  { name="Gerald Teschl", email="gerald.teschl@univie.ac.at" },
6
6
  ]
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