From ebca1d4826fe46149b44d550f6de4fd25b20d1e7 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 9 Mar 2020 14:49:12 -0700 Subject: [PATCH] Add extended_gcd and modular_inverse functions. --- src/mega/crypto.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/mega/crypto.py b/src/mega/crypto.py index 1ca6752..f9a0d4b 100644 --- a/src/mega/crypto.py +++ b/src/mega/crypto.py @@ -106,6 +106,23 @@ def str_to_a32(b): def mpi_to_int(s): return int(binascii.hexlify(s[2:]), 16) +def extended_gcd(a, b): + if a == 0: + return (b, 0, 1) + else: + g, y, x = extended_gcd(b % a, a) + return (g, x - (b // a) * y, y) + +def modular_inverse(a, m): + ''' + Thank you Mart Bakhoff for this solution. + https://stackoverflow.com/a/9758173 + ''' + g, x, y = extended_gcd(a, m) + if g != 1: + raise Exception('modular inverse does not exist') + else: + return x % m def base64_url_decode(data): data += '==' [(2 - len(data) * 3) % 4:]