From a095fa9e7977af44b1ea6da43e48ffa759eca088 Mon Sep 17 00:00:00 2001 From: Peter Gerwinski <peter.gerwinski@hs-bochum.de> Date: Thu, 11 Apr 2024 18:07:37 +0200 Subject: [PATCH] Notizen und Beispiele 11.4.2024 --- 20240411/ad-20240411.txt | 27 +++++++++++++++++++++++++++ 20240411/binexp-01.c | 20 ++++++++++++++++++++ 20240411/binexp-02.c | 22 ++++++++++++++++++++++ 20240411/binexp-03.c | 26 ++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 20240411/binexp-01.c create mode 100644 20240411/binexp-02.c create mode 100644 20240411/binexp-03.c diff --git a/20240411/ad-20240411.txt b/20240411/ad-20240411.txt index ab63d15..2323c2a 100644 --- a/20240411/ad-20240411.txt +++ b/20240411/ad-20240411.txt @@ -88,3 +88,30 @@ Vergleichen Sie die Zeitmessungen der alten Variante (O(n)) und der neuen (O(log Auch Zwischenergebnisse bereits melden; dann können wir darüber sprechen. (Auch Fragen dürfen natürlich gestellt werden.;-) +Erweiterter euklidischer Algorithmus, 11.04.2024, 16:38:59 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +gegeben: a, b +berechnet: ggT(a,b) = s · a + t · b # "ggT" = größter gemeinsamer Teiler + ~~~ ~ ~ + +RSA: +gegeben: e, N = p · q, e teilerfremd zu N +gesucht: d mit d · e % N = 1 + +Was hat das eine mit dem anderen zu tun? + +--> Problem umformulieren, so daß es "s · a + t · b" ähnlicher wird + +d · e % N = 1 --> Trick: aus "%" ein "+" machen + +Schreibe "16 % 7 = 2" als "16 = Vielfaches von 7 + 2" als "16 = m · 7 + 2" + +--> d · e + m · N = 1 (m darf auch negativ sein.) + +Dort, wo wir gerne eine 1 hätten, steht der ggT. +Was ist in unserem Fall der ggT? --> = 1, weil e teilerfremd zu N ist. + +Wähle: a = e, b = N, dann gibt uns der erweiterte euklidische Algorithmus: +1 = s · e + t · N +--> s · e + ein Vielfaches von N = 1 +--> s ist das gesuchte d. :-) diff --git a/20240411/binexp-01.c b/20240411/binexp-01.c new file mode 100644 index 0000000..e7ca813 --- /dev/null +++ b/20240411/binexp-01.c @@ -0,0 +1,20 @@ +#include <stdio.h> +#include <stdint.h> +#include <inttypes.h> + +uint64_t bin_exp (uint64_t x, uint64_t e, uint64_t n) +{ + uint64_t result = 1; + for (uint64_t i = 0; i < e; i++) + { + result *= x; // noch nicht binär, sondern Schleife über e --> O(n) + result %= n; + } + return result; +} + +int main (void) +{ + printf ("%" PRIu64 "\n", bin_exp (1117, 2048, 65537)); + return 0; +} diff --git a/20240411/binexp-02.c b/20240411/binexp-02.c new file mode 100644 index 0000000..220d226 --- /dev/null +++ b/20240411/binexp-02.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <stdint.h> +#include <inttypes.h> + +uint64_t bin_exp (uint64_t x, uint64_t e, uint64_t n) +{ + uint64_t result = 1; + while (e) + { + if (e & 1) + result *= x; // binär, aber ohne "modulo" + x *= x; + e >>= 1; + } + return result; +} + +int main (void) +{ + printf ("%" PRIu64 "\n", bin_exp (1117, 2048, 65537)); + return 0; +} diff --git a/20240411/binexp-03.c b/20240411/binexp-03.c new file mode 100644 index 0000000..8a480f3 --- /dev/null +++ b/20240411/binexp-03.c @@ -0,0 +1,26 @@ +#include <stdio.h> +#include <stdint.h> +#include <inttypes.h> + +uint64_t bin_exp (uint64_t x, uint64_t e, uint64_t n) +{ + uint64_t result = 1; + while (e) + { + if (e & 1) + { + result *= x; + result %= n; + } + x *= x; + x %= n; + e >>= 1; + } + return result; +} + +int main (void) +{ + printf ("%" PRIu64 "\n", bin_exp (1117, 2048, 65537)); + return 0; +} -- GitLab