|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from cryptographic_functions import modulo_inverse_multiplicative |
| 4 | +from cryptographic_functions import shared_functions |
| 5 | +from tabulate import tabulate |
| 6 | + |
| 7 | +__author__ = "Lukas Zorn" |
| 8 | +__copyright__ = "Copyright 2021 Lukas Zorn" |
| 9 | +__license__ = "GNU GPLv3" |
| 10 | + |
| 11 | + |
| 12 | +# Extended elliptic curve point verification |
| 13 | +def on_curve(curve, p, print_header=None): |
| 14 | + if not print_header: |
| 15 | + print(tabulate([['Verifikation eines Punktes auf der elliptischen Kurve']], tablefmt='fancy_grid')) |
| 16 | + else: |
| 17 | + print(tabulate([[f'<AUXILIARY {print_header}>Verifikation eines Punktes auf der elliptischen Kurve']], |
| 18 | + tablefmt='fancy_grid')) |
| 19 | + |
| 20 | + # Unpack all curve parameters and the point into its components |
| 21 | + a, b, n = curve |
| 22 | + x_p, y_p = p |
| 23 | + |
| 24 | + v_p = (y_p ** 2) % n == ((x_p ** 3) + (a * x_p) + b) % n |
| 25 | + |
| 26 | + # Calculation path output |
| 27 | + print( |
| 28 | + f'Durch das Einsetzen des Punktes P = (x_p|y_p) = ({x_p}|{y_p}) in die elliptischen Kurve y^2 = x^3 + a * x + ' |
| 29 | + f'b im GF({n}) wird berechnet:\n' |
| 30 | + f'{y_p}^2 = {x_p}^3 + {a} * {x_p} + {b} mod {n}\n' |
| 31 | + f'{y_p ** 2} = {x_p ** 3} + {a * x_p} + {b} mod {n}\n' |
| 32 | + f'{y_p ** 2} = {(x_p ** 3) + (a * x_p) + b} mod {n}\n' |
| 33 | + f'{(y_p ** 2) % n} = {((x_p ** 3) + (a * x_p) + b) % n}', end='\n\n') |
| 34 | + if v_p: |
| 35 | + print(f'Folglich liegt der Punkt P auf der elliptischen Kurve, da {(y_p ** 2) % n} = ' |
| 36 | + f'{((x_p ** 3) + (a * x_p) + b) % n}.') |
| 37 | + else: |
| 38 | + print(f'Folglich liegt der Punkt P nicht auf der elliptischen Kurve, da {(y_p ** 2) % n} != ' |
| 39 | + f'{((x_p ** 3) + (a * x_p) + b) % n}.') |
| 40 | + shared_functions.print_auxiliary(print_header) |
| 41 | + return v_p |
| 42 | + |
| 43 | + |
| 44 | +# Elliptic curve point addition |
| 45 | +def addition(curve, p, q, print_matrix=False, print_linear_factorization=True): |
| 46 | + print(tabulate([['Addition von Punkten auf der elliptischen Kurve']], tablefmt='fancy_grid')) |
| 47 | + |
| 48 | + # Unpack all curve parameters and both points into its components |
| 49 | + a, b, n = curve |
| 50 | + x_p, y_p = p |
| 51 | + x_q, y_q = q |
| 52 | + |
| 53 | + # Choose a point p that lies on the elliptic curve |
| 54 | + if not on_curve(curve, p, 1): |
| 55 | + print(f'Der Punkt P = ({x_p}|{y_p}) muss auf der elliptischen Kurve liegen.') |
| 56 | + return -1 |
| 57 | + |
| 58 | + # Choose a point q that lies on the elliptic curve |
| 59 | + if not on_curve(curve, q, 2): |
| 60 | + print(f'Der Punkt Q = ({x_q}|{y_q}) muss auf der elliptischen Kurve liegen.') |
| 61 | + return -1 |
| 62 | + |
| 63 | + # Calculation of m |
| 64 | + m_n = (y_p - y_q) % n |
| 65 | + m_d = modulo_inverse_multiplicative.mim(n, (x_p - x_q) % n, print_matrix, print_linear_factorization, 3) |
| 66 | + m = (m_n * m_d) % n |
| 67 | + |
| 68 | + # Calculation of x_r, y_r and y_r_i |
| 69 | + x_r = ((m ** 2) - x_p - x_q) % n |
| 70 | + y_r = (y_p - m * (x_p - x_r)) % n |
| 71 | + y_r_i = -y_r % n |
| 72 | + |
| 73 | + # Choose a point r that lies on the elliptic curve |
| 74 | + if not on_curve(curve, (x_r, y_r_i), 4): |
| 75 | + print(f'Der Punkt R = ({x_r}|{y_r_i}) muss auf der elliptischen Kurve liegen.') |
| 76 | + return -1 |
| 77 | + |
| 78 | + # Calculation path output |
| 79 | + print( |
| 80 | + f'Im endlichen Zahlenkörper GF({n}) sollen auf Basis der Kurve y^2 = x^3 + {a} * x + {b} die Punkte ' |
| 81 | + f'P = ({x_p}|{y_p}) und Q = ({x_q}|{y_q}) additiv verknüpft werden, um den Punkt R zu bestimmen.', |
| 82 | + end='\n\n') |
| 83 | + print( |
| 84 | + f'(1) Verifiziere, dass P = ({x_p}|{y_p}) auf der elliptischen Kurve liegt:\n' |
| 85 | + f'<AUXILIARY 1>Achtung: Die Namen der Variablen können abweichen!</AUXILIARY 1>\n' |
| 86 | + f'(2) Verifiziere, dass Q = ({x_q}|{y_q}) auf der elliptischen Kurve liegt:\n' |
| 87 | + f'<AUXILIARY 2>Achtung: Die Namen der Variablen können abweichen!</AUXILIARY 2>', end='\n\n') |
| 88 | + print( |
| 89 | + f'Für die additive Verknüpfung der beiden Punkte wird nun die Steigung m in GF({n}) berechnet:\n' |
| 90 | + f'm = (y_p - y_q) / (x_p - x_q) % n\n' |
| 91 | + f'm = (y_p - y_q) * (x_p - x_q)^-1 % n\n' |
| 92 | + f'm = ({y_p} - {y_q}) * ({x_p} - {x_q})^-1 % {n}\n' |
| 93 | + f'm = {m_n} * {x_p - x_q}^-1 % {n}\n' |
| 94 | + f'<AUXILIARY 3>Achtung: Die Namen der Variablen können abweichen!</AUXILIARY 3>\n' |
| 95 | + f'm = {m_n} * {m_d} % {n}\n' |
| 96 | + f'm = {m}', end='\n\n') |
| 97 | + print( |
| 98 | + f'Daraus folgt für die Berechnung von -R = (x_r|y_r):\n' |
| 99 | + f'x_r = ((m ** 2) - x_p - x_q) % n\n' |
| 100 | + f'x_r = (({m} ** 2) - {x_p} - {x_q}) % {n}\n' |
| 101 | + f'x_r = {(m ** 2) - x_p - x_q} % {n}\n' |
| 102 | + f'x_r = {x_r}\n' |
| 103 | + f'y_r = (y_p - m * (x_p - x_r)) % n\n' |
| 104 | + f'y_r = ({y_p} - {m} * ({x_p} - {x_r})) % {n}\n' |
| 105 | + f'y_r = {y_p - m * (x_p - x_r)} % {n}\n' |
| 106 | + f'y_r = {y_r}', end='\n\n') |
| 107 | + print( |
| 108 | + f'Aus dem Punkt -R = ({x_r}|{y_r}) kann nun mittels Punktnegation für y_r der Punkt R = ({x_r}|{y_r_i}) ' |
| 109 | + f'berechnet werden:\n' |
| 110 | + f'y_r_i = -(y_r) % n\n' |
| 111 | + f'y_r_i = {-y_r} % {n}\n' |
| 112 | + f'y_r_i = {y_r_i}', end='\n\n') |
| 113 | + print( |
| 114 | + f'(3) Verifiziere, dass R = ({x_r}|{y_r_i}) auf der elliptischen Kurve liegt:\n' |
| 115 | + f'<AUXILIARY 4>Achtung: Die Namen der Variablen können abweichen!</AUXILIARY 4>', end='\n\n') |
| 116 | + return x_r, y_r_i |
0 commit comments