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
7e41d528
Commit
7e41d528
authored
3 years ago
by
jonas.stirnema
Browse files
Options
Downloads
Patches
Plain Diff
Broken v2 from polycop eggen
parent
779c07b2
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/exp_rapide.py
+0
-1
0 additions, 1 deletion
src/exp_rapide.py
src/prime.py
+73
-25
73 additions, 25 deletions
src/prime.py
with
73 additions
and
26 deletions
src/exp_rapide.py
+
0
−
1
View file @
7e41d528
...
...
@@ -27,7 +27,6 @@ def exp_rapide(nb: int, exp: int, mod: int) -> int:
return
out
%
mod
if
__name__
==
'
__main__
'
:
u
=
68393426
...
...
This diff is collapsed.
Click to expand it.
src/prime.py
+
73
−
25
View file @
7e41d528
...
...
@@ -15,38 +15,86 @@ def is_prime_number(number):
return
False
return
True
def
is_probably_prime
(
number
):
def
is_probably_prime
(
n
,
k
):
"""
Check wether a number is probably prime
Miller Rabin Primality Test
FERMAT DIT a^(p−1) = 1 mod p.
SOURCES:
http://defeo.lu/in420/DM3%20-%20Test%20de%20Miller-Rabin
https://www.geeksforgeeks.org/primality-test-set-3-miller-rabin/
https://fr.wikipedia.org/wiki/Test_de_primalit%C3%A9_de_Miller-Rabin
V2
"""
if
number
==
1
:
# 1 is not prime
from
random
import
randint
if
n
==
1
:
# 1 is not prime
return
False
if
n
umber
==
2
:
# 2 is a prime n
umber
if
n
==
2
:
# 2 is a prime n
return
True
if
n
umber
>
2
and
number
%
2
==
0
:
# even numbers are not prime
if
n
ot
n
&
0x01
:
# even numbers are not prime
return
False
d
=
((
number
-
1
)
&
-
(
number
-
1
)).
bit_length
()
-
1
# Get the number of witness we need
r
=
int
((
number
-
1
)
/
2
**
d
)
# Rest after factors of 2
for
i
in
range
(
5
):
if
(
test_rabin
(
number
,
r
,
d
))
==
False
:
return
False
return
True
# PASSED D WITNESSES - PROBABLY PRIME
# Get the number of witness we need
# Number of trailing zeroes in the binary representation of n-1
# n - 1 = 2**n * n
s
=
((
n
-
1
)
&
-
(
n
-
1
)).
bit_length
()
-
1
d
=
int
((
n
-
1
)
/
2
**
s
)
# CHECK A RANDOM NUMBER AS WITNESS
def
test_rabin
(
number
,
r
,
d
):
from
math
import
log2
from
random
import
randint
for
i
in
range
(
1
,
n
):
x
=
2
+
randint
(
1
,
n
-
4
)
y
=
pow
(
x
,
d
,
n
)
if
y
!=
1
and
y
!=
n
-
1
:
for
r
in
range
(
1
,
s
-
1
):
y
=
pow
(
y
,
2
,
n
)
if
y
==
n
-
1
:
break
if
y
!=
1
:
return
False
return
True
# Probably Prime
# def is_probably_prime(number):
# """ Check wether a number is probably prime
# Miller Rabin Primality Test
# FERMAT DIT a^(p−1) = 1 mod p.
# SOURCES:
# http://defeo.lu/in420/DM3%20-%20Test%20de%20Miller-Rabin
# https://www.geeksforgeeks.org/primality-test-set-3-miller-rabin/
# https://fr.wikipedia.org/wiki/Test_de_primalit%C3%A9_de_Miller-Rabin
# """
# if number == 1: # 1 is not prime
# return False
# if number == 2: # 2 is a prime number
# return True
# if number > 2 and number % 2 == 0: # even numbers are not prime
# return False
# d = ((number - 1) & -(number - 1)).bit_length() - 1 # Get the number of witness we need
# r = int((number - 1) / 2**d) # Rest after factors of 2
# for i in range(5):
# if ( test_rabin(number, r, d)) == False:
# return False
# return True # PASSED D WITNESSES - PROBABLY PRIME
# # CHECK A RANDOM NUMBER AS WITNESS
# def test_rabin(number, r, d):
# from exp_rapide import power
# from math import log2
# from random import randint
# a = 2 + randint(1, number-4) # Get random a in 3..p-2 > not smaller than 4
# # for i in range(0, d):
# # tested = power(a, (r * power(2, i, number)), number) #a**(r*2**i) % number
# # if tested == 1 or tested == number-1:
# # # return True # a is not witness of Miller-Rabin
# tested = power(a, r, number)
# if tested == 1 or tested == number-1:
# return True # a is not witness of Miller-Rabin
a
=
randint
(
3
,
number
-
2
)
# Get random a in 3..p-2
for
i
in
range
(
0
,
d
):
teste
d
=
a
**
(
r
*
2
**
i
)
%
number
if
tested
==
1
or
tested
==
number
-
1
:
return
True
# a is not witness of Miller-Rabin
# while (d != number - 1):
# tested = (tested * tested) % number;
#
d
*
=
2;
#
if tested == 1 or tested == number-1:
#
return True # a is not witness of Miller-Rabin
return
False
# Number is not prime
\ No newline at end of file
# 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