Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tp_rsa_python
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
flavio.morrone
tp_rsa_python
Commits
8a9a4702
Commit
8a9a4702
authored
2 years ago
by
adrian.spycher
Browse files
Options
Downloads
Patches
Plain Diff
complete algo.py
parent
3fe94d7c
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
algo.py
+111
-0
111 additions, 0 deletions
algo.py
with
111 additions
and
0 deletions
algo.py
+
111
−
0
View file @
8a9a4702
import
math
def
bachet_bezout
(
a
,
b
):
"""
Does the Bachet-Bezout algorithm on a of b
Args:
a (uint): the number
b (uint): the modulo
Returns:
uint: An array with the pgdc and the coefficients of bachet_bezout, [pgdc, u, v]
"""
if
(
a
==
0
):
return
0
if
(
b
==
0
):
return
-
1
u
=
[
1
,
0
]
v
=
[
0
,
1
]
q
=
1
r
=
1
i
=
2
while
(
r
>
0
):
q
=
a
//
b
r
=
a
%
b
u
.
append
(
u
[
i
-
2
]
-
q
*
u
[
i
-
1
])
v
.
append
(
v
[
i
-
2
]
-
q
*
v
[
i
-
1
])
a
=
b
b
=
r
i
+=
1
return
a
,
u
[
i
-
2
],
v
[
i
-
2
]
def
exponentiation_rapide
(
a
,
exp
,
n
):
"""
Does the quick explanation of a pow x modulo n
Args:
a (uint): the number
exp (uint): the exponent of the number
n (uint): the modulo
Returns:
uint: An array with the pgdc and the coefficients of bachet_bezout, [pgdc, u, v]
"""
if
(
a
==
0
):
return
0
if
(
exp
==
0
):
return
1
r
=
1
b
=
a
%
n
while
(
exp
>
0
):
y
=
exp
%
2
r
=
(
r
*
b
**
y
)
%
n
b
=
(
b
**
2
)
%
n
exp
=
exp
//
2
return
r
def
is_square
(
a
):
"""
Check if a number is a perfect square, using the Newton methode
from https://stackoverflow.com/questions/2489435/check-if-a-number-is-a-perfect-square
Args:
a (uint): number checked
Returns:
bool: true if the number is a perfect square, otherwise flase
"""
x
=
a
//
2
seen
=
set
([
x
])
while
x
*
x
!=
a
:
x
=
(
x
+
(
a
//
x
))
//
2
if
x
in
seen
:
return
False
seen
.
add
(
x
)
return
True
def
fermat_factorization
(
n
):
"""
Does the Fermat
'
s factorization on n,
n = a² - b² = (a + b) * (a - b) = p * q <=> b² = a² - n
Args:
n (uint): number used
Returns:
tuple of uint: the two coeficient a and b
"""
a
=
math
.
ceil
(
math
.
sqrt
(
n
))
b
=
0
while
(
True
):
b2
=
a
**
2
-
n
if
(
is_square
(
b2
)):
b
=
math
.
sqrt
(
b2
)
break
a
+=
1
return
(
a
,
b
)
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