Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RSA
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
nicolas.albanesi
RSA
Commits
aae1593d
Commit
aae1593d
authored
3 years ago
by
jonas.stirnema
Browse files
Options
Downloads
Patches
Plain Diff
Primability without rapid expo
parent
e13c7661
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/prime.py
+4
-9
4 additions, 9 deletions
src/prime.py
with
4 additions
and
9 deletions
src/prime.py
+
4
−
9
View file @
aae1593d
...
@@ -15,7 +15,7 @@ def is_prime_number(number):
...
@@ -15,7 +15,7 @@ def is_prime_number(number):
return
False
return
False
return
True
return
True
def
is_p
o
rbably_prime
(
number
):
def
is_pr
o
bably_prime
(
number
):
"""
Check wether a number is probably prime
"""
Check wether a number is probably prime
Miller Rabin Primality Test
Miller Rabin Primality Test
FERMAT DIT a^(p−1) = 1 mod p.
FERMAT DIT a^(p−1) = 1 mod p.
...
@@ -32,26 +32,21 @@ def is_porbably_prime(number):
...
@@ -32,26 +32,21 @@ def is_porbably_prime(number):
return
False
return
False
d
=
((
number
-
1
)
&
-
(
number
-
1
)).
bit_length
()
-
1
# Get the number of witness we need
d
=
((
number
-
1
)
&
-
(
number
-
1
)).
bit_length
()
-
1
# Get the number of witness we need
print
(
d
)
r
=
int
((
number
-
1
)
/
2
**
d
)
# Rest after factors of 2
r
=
int
((
number
-
1
)
/
2
**
d
)
# Rest after factors of 2
print
(
r
)
for
i
in
range
(
5
):
for
i
in
range
(
d
):
if
(
test_rabin
(
number
,
r
,
d
))
==
False
:
if
(
test_rabin
(
number
,
r
,
d
))
==
False
:
return
False
return
False
return
True
# PASSED D WITNESSES - PROBABLY PRIME
return
True
# PASSED D WITNESSES - PROBABLY PRIME
# CHECK A RANDOM NUMBER AS WITNESS
# CHECK A RANDOM NUMBER AS WITNESS
# d is the number
def
test_rabin
(
number
,
r
,
d
):
def
test_rabin
(
number
,
r
,
d
):
from
math
import
log2
from
math
import
log2
from
random
import
randint
from
random
import
randint
a
=
randint
(
3
,
number
-
2
)
# Get random a in 3..p-2
a
=
randint
(
3
,
number
-
2
)
# Get random a in 3..p-2
for
i
in
range
(
0
,
d
):
for
i
in
range
(
0
,
d
):
tested
=
a
^
(
r
*
2
**
i
)
%
number
tested
=
a
**
(
r
*
2
**
i
)
%
number
if
tested
==
1
or
tested
==
number
-
1
:
if
tested
==
1
or
tested
==
number
-
1
:
return
t
rue
# a is not witness of Miller-Rabin
return
T
rue
# a is not witness of Miller-Rabin
return
False
# Number is not prime
return
False
# Number is not prime
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment