From 58f88248c6d51730f298af7fc48c791d0b6341cc Mon Sep 17 00:00:00 2001 From: programmingAthlete Date: Sat, 25 Nov 2023 23:01:31 +0100 Subject: [PATCH] util --- src/crypto_VDF/utils.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/crypto_VDF/utils.py diff --git a/src/crypto_VDF/utils.py b/src/crypto_VDF/utils.py new file mode 100644 index 0000000..6e703d8 --- /dev/null +++ b/src/crypto_VDF/utils.py @@ -0,0 +1,34 @@ +def exp_modular(a: int, exponent: int, n: int) -> int: + """ + Modular exponentiation + + :param a: number to exponentiate + :param exponent: exponent + :param n: modulus + :return: a^exponent (mod n) + """ + exp = int_2_base(exponent, 2) + c = a + for i in range(1, len(exp)): + c = c * c % n + if exp[i] == 1: + c = c * a % n + return c + + +def int_2_base(a: int, base: int) -> list: + """ + Convert integer to base "base" + + :param a: integer to convert to big number form in base "base" + :param base: base into which to convert integer "a" + :return: bin number form in base "base" corresponding to "a" + """ + x = a + reminders = [] + while x != 0: + q = x // base + r = x % base + x = q + reminders.append(r) + return reminders[::-1]