Skip to content

Commit 49e6cb9

Browse files
committed
add support for gmpy2
use gmpy2 if possible as it's faster than gmpy (and still developed)
1 parent a12afb5 commit 49e6cb9

File tree

6 files changed

+82
-30
lines changed

6 files changed

+82
-30
lines changed

.travis.yml

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ addons:
1717
- swig
1818
# needed for GMPY
1919
- libgmp-dev
20+
# needed for GMPY2
21+
- libmpfr-dev
22+
- libmpc-dev
2023
before_cache:
2124
- rm -f $HOME/.cache/pip/log/debug.log
2225

@@ -95,19 +98,33 @@ jobs:
9598
sudo: true
9699
env: GMPY=true
97100
- python: 2.7
98-
env: M2CRYPTO=true PYCRYPTO=true GMPY=true
101+
env: GMPY2=true
99102
- python: 3.5
100-
env: M2CRYPTO=true PYCRYPTO=true GMPY=true
103+
env: GMPY2=true
101104
- python: 3.6
102-
env: M2CRYPTO=true PYCRYPTO=true GMPY=true
105+
env: GMPY2=true
103106
- python: 3.7
104107
dist: xenial
105108
sudo: true
106-
env: M2CRYPTO=true PYCRYPTO=true GMPY=true CC_COV=true
109+
env: GMPY2=true
107110
- python: 3.8
108111
dist: xenial
109112
sudo: true
110-
env: M2CRYPTO=true GMPY=true
113+
env: GMPY2=true
114+
- python: 2.7
115+
env: M2CRYPTO=true PYCRYPTO=true GMPY=true GMPY2=true
116+
- python: 3.5
117+
env: M2CRYPTO=true PYCRYPTO=true GMPY=true GMPY2=true
118+
- python: 3.6
119+
env: M2CRYPTO=true PYCRYPTO=true GMPY=true GMPY2=true
120+
- python: 3.7
121+
dist: xenial
122+
sudo: true
123+
env: M2CRYPTO=true PYCRYPTO=true GMPY=true GMPY2=true CC_COV=true
124+
- python: 3.8
125+
dist: xenial
126+
sudo: true
127+
env: M2CRYPTO=true GMPY=true GMPY2=true
111128

112129
before_install:
113130
- |
@@ -138,6 +155,7 @@ install:
138155
- if [[ $PYCRYPTO == 'true' ]]; then travis_retry pip install pycrypto; fi
139156
- if [[ $PYCRYPTODOME == 'true' ]]; then travis_retry pip install pycryptodome; fi
140157
- if [[ $GMPY == 'true' ]]; then travis_retry pip install gmpy; fi
158+
- if [[ $GMPY2 == 'true' ]]; then travis_retry pip install gmpy2; fi
141159
- travis_retry pip install -r requirements.txt
142160
- if [[ $CC_COV == 'true' ]]; then ./cc-test-reporter before-build; fi
143161

scripts/speed.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import timeit
22

3-
from tlslite.utils.cryptomath import gmpyLoaded
3+
from tlslite.utils.cryptomath import gmpyLoaded, GMPY2_LOADED
44

55
print("Acceleration backends loaded:")
66
print("gmpy: {0}".format(gmpyLoaded))
7+
print("gmpy2: {0}".format(GMPY2_LOADED))
78
print("")
89

910
def do(setup_statements, statement):

scripts/tls.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ def printUsage(s=None):
7171
print(" GMPY : Loaded")
7272
else:
7373
print(" GMPY : Not Loaded")
74-
74+
if GMPY2_LOADED:
75+
print(" GMPY2 : Loaded")
76+
else:
77+
print(" GMPY2 : Not Loaded")
78+
7579
print("")
7680
print("""Commands:
7781

tlslite/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
MultiPathTLSXMLRPCServer
2626

2727
from .utils.cryptomath import m2cryptoLoaded, gmpyLoaded, \
28-
pycryptoLoaded, prngName
28+
pycryptoLoaded, prngName, GMPY2_LOADED
2929
from .utils.keyfactory import generateRSAKey, parsePEMKey, \
3030
parseAsPublicKey, parsePrivateKey
3131
from .utils.tackwrapper import tackpyLoaded

tlslite/utils/cryptomath.py

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,27 @@
5454
#Try to load GMPY
5555
try:
5656
import gmpy
57+
gmpy.mpz
5758
gmpyLoaded = True
5859
except ImportError:
5960
gmpyLoaded = False
6061

62+
63+
# Try to load GMPY2
64+
try:
65+
from gmpy2 import powmod
66+
GMPY2_LOADED = True
67+
except ImportError:
68+
GMPY2_LOADED = False
69+
70+
71+
# Use the faster mpz
72+
if GMPY2_LOADED:
73+
from gmpy2 import mpz
74+
elif gmpyLoaded:
75+
from gmpy import mpz
76+
77+
6178
#Try to load pycrypto
6279
# pylint: disable=invalid-name
6380
try:
@@ -292,25 +309,35 @@ def gcd(a,b):
292309
def lcm(a, b):
293310
return (a * b) // gcd(a, b)
294311

295-
#Returns inverse of a mod b, zero if none
296-
#Uses Extended Euclidean Algorithm
297-
def invMod(a, b):
298-
c, d = a, b
299-
uc, ud = 1, 0
300-
while c != 0:
301-
q = d // c
302-
c, d = d-(q*c), c
303-
uc, ud = ud - (q * uc), uc
304-
if d == 1:
305-
return ud % b
306-
return 0
307-
308-
309-
if gmpyLoaded:
312+
# pylint: disable=invalid-name
313+
# disable pylint check as the (a, b) are part of the API
314+
if GMPY2_LOADED:
315+
def invMod(a, b):
316+
"""Return inverse of a mod b, zero if none."""
317+
if a == 0:
318+
return 0
319+
return powmod(a, -1, b)
320+
else:
321+
# Use Extended Euclidean Algorithm
322+
def invMod(a, b):
323+
"""Return inverse of a mod b, zero if none."""
324+
c, d = a, b
325+
uc, ud = 1, 0
326+
while c != 0:
327+
q = d // c
328+
c, d = d-(q*c), c
329+
uc, ud = ud - (q * uc), uc
330+
if d == 1:
331+
return ud % b
332+
return 0
333+
# pylint: enable=invalid-name
334+
335+
336+
if gmpyLoaded or GMPY2_LOADED:
310337
def powMod(base, power, modulus):
311-
base = gmpy.mpz(base)
312-
power = gmpy.mpz(power)
313-
modulus = gmpy.mpz(modulus)
338+
base = mpz(base)
339+
power = mpz(power)
340+
modulus = mpz(modulus)
314341
result = pow(base, power, modulus)
315342
return compatLong(result)
316343
else:

tlslite/utils/python_rsakey.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from .rsakey import *
88
from .pem import *
99
from .deprecations import deprecated_params
10-
if gmpyLoaded:
10+
if GMPY2_LOADED:
11+
from gmpy2 import mpz
12+
elif gmpyLoaded:
1113
from gmpy import mpz
1214

1315
class Python_RSAKey(RSAKey):
@@ -18,7 +20,7 @@ def __init__(self, n=0, e=0, d=0, p=0, q=0, dP=0, dQ=0, qInv=0,
1820
see also generate() and parsePEM()."""
1921
if (n and not e) or (e and not n):
2022
raise AssertionError()
21-
if gmpyLoaded:
23+
if gmpyLoaded or GMPY2_LOADED:
2224
n = mpz(n)
2325
e = mpz(e)
2426
d = mpz(d)
@@ -114,12 +116,12 @@ def generate(bits, key_type="rsa"):
114116
key = Python_RSAKey()
115117
p = getRandomPrime(bits//2, False)
116118
q = getRandomPrime(bits//2, False)
117-
if gmpyLoaded:
119+
if gmpyLoaded or GMPY2_LOADED:
118120
p = mpz(p)
119121
q = mpz(q)
120122
t = lcm(p-1, q-1)
121123
key.n = p * q
122-
if gmpyLoaded:
124+
if gmpyLoaded or GMPY2_LOADED:
123125
key.e = mpz(65537)
124126
else:
125127
key.e = 65537

0 commit comments

Comments
 (0)