Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ISC_122 - Travail Pratique 001 - Code de Reed-Solomon
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
florian.burgener
ISC_122 - Travail Pratique 001 - Code de Reed-Solomon
Commits
fafe370e
Commit
fafe370e
authored
3 years ago
by
gawen.ackerman
Browse files
Options
Downloads
Patches
Plain Diff
refactoring
parent
48a4b7d4
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
polynomial.py
+29
-27
29 additions, 27 deletions
polynomial.py
with
29 additions
and
27 deletions
polynomial.py
+
29
−
27
View file @
fafe370e
...
@@ -11,7 +11,8 @@ def unicode_superscripts(number):
...
@@ -11,7 +11,8 @@ def unicode_superscripts(number):
Returns:
Returns:
str: The unicode superscripts string.
str: The unicode superscripts string.
"""
"""
exponent_dict
=
{
"
0
"
:
"
⁰
"
,
"
1
"
:
"
¹
"
,
"
2
"
:
"
²
"
,
"
3
"
:
"
³
"
,
"
4
"
:
"
⁴
"
,
"
5
"
:
"
⁵
"
,
"
6
"
:
"
⁶
"
,
"
7
"
:
"
⁷
"
,
"
8
"
:
"
⁸
"
,
"
9
"
:
"
⁹
"
}
exponent_dict
=
{
"
0
"
:
"
⁰
"
,
"
1
"
:
"
¹
"
,
"
2
"
:
"
²
"
,
"
3
"
:
"
³
"
,
"
4
"
:
"
⁴
"
,
"
5
"
:
"
⁵
"
,
"
6
"
:
"
⁶
"
,
"
7
"
:
"
⁷
"
,
"
8
"
:
"
⁸
"
,
"
9
"
:
"
⁹
"
}
return
(
"
⁻
"
if
number
<
0
else
""
)
+
""
.
join
(
exponent_dict
[
x
]
for
x
in
str
(
abs
(
number
)))
return
(
"
⁻
"
if
number
<
0
else
""
)
+
""
.
join
(
exponent_dict
[
x
]
for
x
in
str
(
abs
(
number
)))
...
@@ -59,12 +60,11 @@ class Polynomial:
...
@@ -59,12 +60,11 @@ class Polynomial:
"""
"""
a
=
list
(
self
.
value
)
a
=
list
(
self
.
value
)
b
=
list
(
other
.
value
)
b
=
list
(
other
.
value
)
c
=
[]
c
=
[]
# itertools pad 0 to the lowest list
for
(
a
,
b
)
in
itertools
.
zip_longest
(
a
,
b
,
fillvalue
=
0
):
c
.
append
(
a
+
b
)
# itertools pad 0 to the lowest list.
for
(
ai
,
bi
)
in
itertools
.
zip_longest
(
a
,
b
,
fillvalue
=
0
):
c
.
append
(
ai
+
bi
)
return
Polynomial
(
tuple
(
c
))
return
Polynomial
(
tuple
(
c
))
def
__mul__
(
self
,
other
):
def
__mul__
(
self
,
other
):
...
@@ -83,6 +83,7 @@ class Polynomial:
...
@@ -83,6 +83,7 @@ class Polynomial:
for
i
in
range
(
len
(
a
)):
for
i
in
range
(
len
(
a
)):
for
j
in
range
(
len
(
b
)):
for
j
in
range
(
len
(
b
)):
# Sum of the product of a[i] by b[j] at exponent i + j.
c
[
i
+
j
]
+=
a
[
i
]
*
b
[
j
]
c
[
i
+
j
]
+=
a
[
i
]
*
b
[
j
]
return
Polynomial
(
tuple
(
c
))
return
Polynomial
(
tuple
(
c
))
...
@@ -93,18 +94,14 @@ class Polynomial:
...
@@ -93,18 +94,14 @@ class Polynomial:
other (int): The modulo to apply.
other (int): The modulo to apply.
Returns:
Returns:
Polynomial: The result of the mod
olu
operation.
Polynomial: The result of the mod
ulo
operation.
"""
"""
a
=
list
(
self
.
value
)
result
=
list
(
self
.
value
)
result
=
[
0
]
*
len
(
a
)
for
i
in
range
(
len
(
a
)):
for
i
in
range
(
len
(
result
)):
result
[
i
]
=
a
[
i
]
%
other
result
[
i
]
%=
other
for
i
in
reversed
(
range
(
len
(
result
))):
while
result
[
-
1
]
==
0
and
len
(
result
)
>
1
:
if
result
[
i
]
==
0
:
result
=
result
[:
-
1
]
del
result
[
i
]
else
:
break
return
Polynomial
(
tuple
(
result
))
return
Polynomial
(
tuple
(
result
))
def
__str__
(
self
):
def
__str__
(
self
):
...
@@ -145,25 +142,29 @@ def get_bezout_coefficients(a, b):
...
@@ -145,25 +142,29 @@ def get_bezout_coefficients(a, b):
x
=
[
1
,
0
]
x
=
[
1
,
0
]
y
=
[
0
,
1
]
y
=
[
0
,
1
]
q
=
[
0
,
0
]
q
=
[
0
,
0
]
i
=
1
i
=
2
while
r
[
i
]
>
0
:
while
True
:
i
+=
1
r
.
append
(
r
[
i
-
2
]
%
r
[
i
-
1
])
r
.
append
(
r
[
i
-
2
]
%
r
[
i
-
1
])
# Continue until the rest is equal to 0
if
r
[
i
]
==
0
:
break
q
.
append
(
int
(
r
[
i
-
2
]
/
r
[
i
-
1
]))
q
.
append
(
int
(
r
[
i
-
2
]
/
r
[
i
-
1
]))
x
.
append
(
x
[
i
-
2
]
-
q
[
i
]
*
x
[
i
-
1
])
y
.
append
(
y
[
i
-
2
]
-
q
[
i
]
*
y
[
i
-
1
])
i
+=
1
if
r
[
i
]
>
0
:
x
.
append
(
x
[
i
-
2
]
-
q
[
i
]
*
x
[
i
-
1
])
y
.
append
(
y
[
i
-
2
]
-
q
[
i
]
*
y
[
i
-
1
])
return
x
[
-
1
],
y
[
-
1
]
return
x
[
-
1
],
y
[
-
1
]
def
modular_inverse
(
a
,
n
):
def
modular_inverse
(
a
,
n
):
"""
Compute the modular inverse of a number a mod
olu
n.
"""
Compute the modular inverse of a number a mod
ulo
n.
Args:
Args:
a (int): The number to reverse.
a (int): The number to reverse.
n (int): The mod
olu
.
n (int): The mod
ulo
.
Returns:
Returns:
int: The reversed number.
int: The reversed number.
...
@@ -196,7 +197,7 @@ def compute_lagrange_polynomial(points, prime_number):
...
@@ -196,7 +197,7 @@ def compute_lagrange_polynomial(points, prime_number):
for
j
,
(
xj
,
_
)
in
enumerate
(
points
):
for
j
,
(
xj
,
_
)
in
enumerate
(
points
):
if
j
!=
i
:
if
j
!=
i
:
Li_polynomial
*=
Polynomial
((
-
xj
,
1
))
Li_polynomial
*=
Polynomial
((
-
xj
,
1
))
Li_polynomial
*=
Polynomial
((
modular_inverse
(
xi
-
xj
,
prime_number
)
,
))
Li_polynomial
*=
Polynomial
((
modular_inverse
(
xi
-
xj
,
prime_number
)))
Li_polynomial
*=
Polynomial
((
yi
,))
Li_polynomial
*=
Polynomial
((
yi
,))
L_polynomial
+=
Li_polynomial
L_polynomial
+=
Li_polynomial
return
L_polynomial
%
prime_number
return
L_polynomial
%
prime_number
...
@@ -218,13 +219,13 @@ def reed_solomon(points, data_length, last_error_index, prime_number):
...
@@ -218,13 +219,13 @@ def reed_solomon(points, data_length, last_error_index, prime_number):
# Parse each combination of points possible (exclude the correct points)
# Parse each combination of points possible (exclude the correct points)
for
x
in
itertools
.
combinations
(
points
[:
last_error_index
+
1
],
combination_length
):
for
x
in
itertools
.
combinations
(
points
[:
last_error_index
+
1
],
combination_length
):
nb_valid_points
=
0
# Create a sublist of points with all corrects points and the current combination of points
# Create a sublist of points with all corrects points and the current combination of points
sub_points
=
list
(
x
)
+
points
[
last_error_index
+
1
:]
sub_points
=
list
(
x
)
+
points
[
last_error_index
+
1
:]
# Create the lagrange polynomial with the sublist of points
# Create the lagrange polynomial with the sublist of points
lagrange
=
compute_lagrange_polynomial
(
sub_points
,
prime_number
)
lagrange
=
compute_lagrange_polynomial
(
sub_points
,
prime_number
)
nb_valid_points
=
0
# Parse each points to verify if the polynomial is correct
# Parse each points to verify if the polynomial is correct
for
p
in
points
:
for
p
in
points
:
x
=
p
[
0
]
x
=
p
[
0
]
...
@@ -235,7 +236,8 @@ def reed_solomon(points, data_length, last_error_index, prime_number):
...
@@ -235,7 +236,8 @@ def reed_solomon(points, data_length, last_error_index, prime_number):
nb_valid_points
+=
1
nb_valid_points
+=
1
# Verify if we have enough valid points, so it must be equal or higher than m + n points
# Verify if we have enough valid points, so it must be equal or higher than m + n points
if
nb_valid_points
>=
data_length
+
(
len
(
points
)
-
data_length
)
//
2
:
# // = euclid division
# // = euclid division
if
nb_valid_points
>=
data_length
+
(
len
(
points
)
-
data_length
)
//
2
:
# Decode the message
# Decode the message
output
=
""
output
=
""
for
i
in
range
(
data_length
):
for
i
in
range
(
data_length
):
...
...
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