diff --git a/polynomial.py b/polynomial.py index 0c0b5d7dd6ff00171bee6734eb865a3bfb060cbb..d5dce8c7974afacfe2fde3e0862b52098bc50859 100644 --- a/polynomial.py +++ b/polynomial.py @@ -146,24 +146,40 @@ def compute_lagrange_polynomial(points, prime_number): def reed_solomon(points, data_length, last_error_index, prime_number): + combination_length = data_length - len(points[:last_error_index]) + + # Parse each combination of points possible (exclude the correct points) + 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 + sub_points = list(x) + points[last_error_index + 1:] + + # Create the lagrange polynomial with the sublist of points + lagrange = compute_lagrange_polynomial(sub_points, prime_number) + + # Parse each points to verify if the polynomial is correct + for p in points: + x = p[0] + # Pass the x value of each points through the lagrange polynomial + y = lagrange.pass_x_throughout(x) % prime_number + # If the result is the same that the y value, then the point is correct + if y == p[1]: + nb_valid_points += 1 + + # 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 + # Decode the message + output = '' + for i in range(data_length): + output += chr(lagrange.pass_x_throughout(i) % prime_number) + + return output + return None def main(): - # m + 2n - # 25 + 2 * 11 = 47 message = {"data_length": 25, "last_error_index": 23, "prime_number": 401, "points": [67, 101, 38, 109, 101, 115, 133, 118, 103, 128, 62, 118, 97, 156, 116, 77, 49, 56, 86, 112, 171, 105, 176, 116, 115, 183, 30, 315, 368, 29, 352, 54, 333, 198, 139, 234, 321, 92, 5, 272, 396, 265, 397, 386, 229, 153, 276]} - # > 23 => 24 à 46 = 23 points - # Il faut trouver 2 points dans [0 ; 23] - print(len(message["points"])) - print(message["points"][: 23 + 1]) - print(message["points"][23 + 1 :]) - # 25 points - # Polynome qui passe les 25 points possiblement juste - # On test tous les points, si le polynome passe par m + n points c'est le bon - # 0 à 24 - # 0, 1, 2, 3 - points = [(x, y) for x, y in enumerate(message["points"])] print(points) corrected_data = reed_solomon(points, message["data_length"], message["last_error_index"], message["prime_number"])