Skip to content

Commit ca94c5e

Browse files
committed
2 parents 72949d4 + 92ed2c5 commit ca94c5e

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ julia> e = 65537
4141
julia> d = invmod(e, (p-1)*(q-1)) # compute RSA decryption exponent
4242
32362883641310315451899592262377172791965856192371946631485250568611645044625881242387678564972226360689108476233462883544705990145324113781489121643593621753163078450834460663942035227770596133499206721223993086064885467845603112395435294663436699341967664046213003429586468421266641276398515468366056248785
4343

44-
julia> factor_with_ed(n, e, d) # factor n with n and d
44+
julia> factor_with_ed(n, e, d) # factor n with e and d
4545
(11209007052907094316298587866392085453240213973638699831846376613053337678939099626874977325024647359864974367465362518878257931790980202563932031187056729, 11753513928682888932534842071560505691719602160983337271901213762089936749492510218729499032535262339600976674663969869808030961514878135483359095845990203)
4646

4747
julia> p

docs/src/api.md

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ CryptoUtils.convergents
3232
CryptoUtils.surd
3333
CryptoUtils.hoc_sqrt
3434
CryptoUtils.tonelli_shanks
35+
CryptoUtils.is_generator
36+
CryptoUtils.get_safe_prime_generator
3537
```
3638

3739

src/CryptoUtils.jl

+51-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export legendre, jacobi, sqrt_mod_prime, find_quadratic_non_residue, is_quadrati
77

88
export n2b, b2n
99

10-
export random_prime, safe_prime, tower_two_prime, get_first_primes, twin_primes
10+
export random_prime, safe_prime, tower_two_prime, get_first_primes, twin_primes,
11+
is_generator, get_safe_prime_generator
1112

1213
export factor_with_ed, wiener
1314

@@ -359,6 +360,55 @@ function surd(n::BigInt, k::Int64)
359360
end
360361

361362

363+
"""
364+
is_generator(g::Integer, q::Integer, factors::Array) -> Bool
365+
366+
Returns true if `g` is a generator of `Z_q` where `q` is prime and
367+
`factors` is the prime factorization of `q - 1 = p1^e1 * p2^e2 ... * pk^ek`.
368+
369+
```
370+
q = 2^7 * 5 + 1
371+
is_generator(2, q, [2, 5]) -> false
372+
is_generator(3, q, [2, 5]) -> true
373+
```
374+
"""
375+
function is_generator(g::Integer, q::Integer, factors::Array)::Bool
376+
if q % 2 == 0 && !isprime(q)
377+
throw("Argument q should be an odd prime.")
378+
end
379+
n = q - 1
380+
for factor in factors
381+
if powermod(g, div(n,factor), q) == 1
382+
return false
383+
end
384+
end
385+
return true
386+
end
387+
388+
389+
"""
390+
get_safe_prime_generator(q::BigInt) -> BigInt
391+
392+
Returns a generator of `Z_q`, where `q = 2 * p + 1` with `q, p` primes.
393+
"""
394+
function get_safe_prime_generator(q::BigInt)::BigInt
395+
if q % 2 == 0 && !isprime(q)
396+
throw("Argument q should be an odd prime.")
397+
end
398+
399+
p = div(q - 1, 2)
400+
factors = [2, p]
401+
402+
@label sample_generator
403+
g = rand(1 : q - 1)
404+
405+
if !is_generator(g, q, factors)
406+
@goto sample_generator
407+
end
408+
409+
return g
410+
end
411+
362412

363413

364414
################################################################

0 commit comments

Comments
 (0)