]> git.codecow.com Git - Monocypher.git/commitdiff
Use pow() for exponentiation in Python 3
authorLoup Vaillant <loup@loup-vaillant.fr>
Tue, 18 Feb 2020 17:25:11 +0000 (18:25 +0100)
committerLoup Vaillant <loup@loup-vaillant.fr>
Tue, 18 Feb 2020 17:25:11 +0000 (18:25 +0100)
Much faster this way.

tests/gen/elligator.py

index acb13d5a8cc083616c9793365da953d1a1b407fb..5f4e91bbbbb07110abc137834ac1ac0a1423fac6 100755 (executable)
@@ -69,21 +69,8 @@ def print_little(n):
 def binary(b):
     return [int(c) for c in list(format(b, 'b'))]
 
-def exp(a, b):
-    """
-    a^b mod p
-    b must be positive
-    """
-    d = 1
-    for i in binary(b):
-        d = (d*d) % p
-        if i == 1:
-            d = (d*a) % p
-    return d
-
-def invert(n):
-    """Modular invert of n"""
-    return exp(n, p-2)
+def exp(a, b): return pow(a, b, p)
+def invert(n): return exp(n, p-2)
 
 def m_abs(n):
     """Modular absolute value of n, to canonicalise square roots."""