Skip to content

Commit 533bc74

Browse files
authored
Merge pull request #233 from tomato42/more-test-coverage
More test coverage
2 parents 61133a6 + 52f335f commit 533bc74

File tree

5 files changed

+160
-2
lines changed

5 files changed

+160
-2
lines changed

src/ecdsa/keys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ def from_pem(cls, string, hashfunc=sha1):
960960
:return: Initialised SigningKey object
961961
:rtype: SigningKey
962962
"""
963-
if not PY2 and isinstance(string, str):
963+
if not PY2 and isinstance(string, str): # pragma: no branch
964964
string = string.encode()
965965

966966
# The privkey pem may have multiple sections, commonly it also has

src/ecdsa/test_jacobi.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,29 @@ def test_add_same_scale_points(self, a_mul, b_mul, new_z):
312312

313313
self.assertEqual(c, j_g * (a_mul + b_mul))
314314

315+
def test_add_same_scale_points_static(self):
316+
j_g = generator_brainpoolp160r1
317+
p = curve_brainpoolp160r1.p()
318+
a = j_g * 11
319+
a.scale()
320+
z1 = 13
321+
x = PointJacobi(
322+
curve_brainpoolp160r1,
323+
a.x() * z1 ** 2 % p,
324+
a.y() * z1 ** 3 % p,
325+
z1,
326+
)
327+
y = PointJacobi(
328+
curve_brainpoolp160r1,
329+
a.x() * z1 ** 2 % p,
330+
a.y() * z1 ** 3 % p,
331+
z1,
332+
)
333+
334+
c = a + a
335+
336+
self.assertEqual(c, x + y)
337+
315338
@settings(max_examples=14)
316339
@given(
317340
st.integers(
@@ -363,6 +386,30 @@ def test_add_different_scale_points(self, a_mul, b_mul, new_z):
363386

364387
self.assertEqual(c, j_g * (a_mul + b_mul))
365388

389+
def test_add_different_scale_points_static(self):
390+
j_g = generator_brainpoolp160r1
391+
p = curve_brainpoolp160r1.p()
392+
a = j_g * 11
393+
a.scale()
394+
z1 = 13
395+
x = PointJacobi(
396+
curve_brainpoolp160r1,
397+
a.x() * z1 ** 2 % p,
398+
a.y() * z1 ** 3 % p,
399+
z1,
400+
)
401+
z2 = 29
402+
y = PointJacobi(
403+
curve_brainpoolp160r1,
404+
a.x() * z2 ** 2 % p,
405+
a.y() * z2 ** 3 % p,
406+
z2,
407+
)
408+
409+
c = a + a
410+
411+
self.assertEqual(c, x + y)
412+
366413
def test_add_point_3_times(self):
367414
j_g = PointJacobi.from_affine(generator_256)
368415

src/ecdsa/test_keys.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
sigdecode_der,
2323
sigdecode_strings,
2424
)
25-
from .curves import NIST256p
25+
from .curves import NIST256p, Curve, BRAINPOOLP160r1
26+
from .ellipticcurve import Point
27+
from .ecdsa import generator_brainpoolp160r1
2628

2729

2830
class TestVerifyingKeyFromString(unittest.TestCase):
@@ -207,6 +209,17 @@ def test_VerifyingKey_inequality_on_same_curve(self):
207209
def test_SigningKey_inequality_on_same_curve(self):
208210
self.assertNotEqual(self.sk, self.sk2)
209211

212+
def test_inequality_on_wrong_types(self):
213+
self.assertNotEqual(self.vk, self.sk)
214+
215+
def test_from_public_point_old(self):
216+
pj = self.vk.pubkey.point
217+
point = Point(pj.curve(), pj.x(), pj.y())
218+
219+
vk = VerifyingKey.from_public_point(point, self.vk.curve)
220+
221+
self.assertEqual(vk, self.vk)
222+
210223

211224
class TestSigningKey(unittest.TestCase):
212225
"""
@@ -458,3 +471,37 @@ def test_SigningKey_with_unlikely_value():
458471
vk = sk.verifying_key
459472
sig = sk.sign(b"hello")
460473
assert vk.verify(sig, b"hello")
474+
475+
476+
def test_SigningKey_with_custom_curve_old_point():
477+
generator = generator_brainpoolp160r1
478+
generator = Point(
479+
generator.curve(), generator.x(), generator.y(), generator.order(),
480+
)
481+
482+
curve = Curve(
483+
"BRAINPOOLP160r1",
484+
generator.curve(),
485+
generator,
486+
(1, 3, 36, 3, 3, 2, 8, 1, 1, 1),
487+
)
488+
489+
sk = SigningKey.from_secret_exponent(12, curve)
490+
491+
sk2 = SigningKey.from_secret_exponent(12, BRAINPOOLP160r1)
492+
493+
assert sk.privkey == sk2.privkey
494+
495+
496+
def test_VerifyingKey_inequality_with_different_curves():
497+
sk1 = SigningKey.from_secret_exponent(2, BRAINPOOLP160r1)
498+
sk2 = SigningKey.from_secret_exponent(2, NIST256p)
499+
500+
assert sk1.verifying_key != sk2.verifying_key
501+
502+
503+
def test_VerifyingKey_inequality_with_different_secret_points():
504+
sk1 = SigningKey.from_secret_exponent(2, BRAINPOOLP160r1)
505+
sk2 = SigningKey.from_secret_exponent(3, BRAINPOOLP160r1)
506+
507+
assert sk1.verifying_key != sk2.verifying_key

src/ecdsa/test_numbertheory.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,32 @@ def test_square_root_mod_prime_for_small_primes(prime):
8282
square_root_mod_prime(nonsquare, prime)
8383

8484

85+
def test_square_root_mod_prime_for_2():
86+
a = square_root_mod_prime(1, 2)
87+
assert a == 1
88+
89+
90+
def test_square_root_mod_prime_for_small_prime():
91+
root = square_root_mod_prime(98 ** 2 % 101, 101)
92+
assert root * root % 101 == 9
93+
94+
95+
def test_square_root_mod_prime_for_p_congruent_5():
96+
p = 13
97+
assert p % 8 == 5
98+
99+
root = square_root_mod_prime(3, p)
100+
assert root * root % p == 3
101+
102+
103+
def test_square_root_mod_prime_for_p_congruent_5_large_d():
104+
p = 29
105+
assert p % 8 == 5
106+
107+
root = square_root_mod_prime(4, p)
108+
assert root * root % p == 4
109+
110+
85111
@st.composite
86112
def st_two_nums_rel_prime(draw):
87113
# 521-bit is the biggest curve we operate on, use 1024 for a bit
@@ -324,6 +350,32 @@ def test_factorization(self, num):
324350
mult *= i[0] ** i[1]
325351
assert mult == num
326352

353+
def test_factorisation_smallprimes(self):
354+
exp = 101 * 103
355+
assert 101 in smallprimes
356+
assert 103 in smallprimes
357+
factors = factorization(exp)
358+
mult = 1
359+
for i in factors:
360+
mult *= i[0] ** i[1]
361+
assert mult == exp
362+
363+
def test_factorisation_not_smallprimes(self):
364+
exp = 1231 * 1237
365+
assert 1231 not in smallprimes
366+
assert 1237 not in smallprimes
367+
factors = factorization(exp)
368+
mult = 1
369+
for i in factors:
370+
mult *= i[0] ** i[1]
371+
assert mult == exp
372+
373+
def test_jacobi_with_zero(self):
374+
assert jacobi(0, 3) == 0
375+
376+
def test_jacobi_with_one(self):
377+
assert jacobi(1, 3) == 1
378+
327379
@settings(**HYP_SETTINGS)
328380
@given(st.integers(min_value=3, max_value=1000).filter(lambda x: x % 2))
329381
def test_jacobi(self, mod):

src/ecdsa/test_pyecdsa.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
except ImportError:
66
import unittest
77
import os
8+
import sys
89
import shutil
910
import subprocess
1011
import pytest
@@ -1396,6 +1397,17 @@ def test_trytryagain(self):
13961397
b("6fa59d73bf0446ae8743cf748fc5ac11d5585a90356417e97155c3bc"),
13971398
)
13981399

1400+
def test_trytryagain_single(self):
1401+
tta = util.randrange_from_seed__trytryagain
1402+
order = 2 ** 8 - 2
1403+
seed = b"text"
1404+
n = tta(seed, order)
1405+
# known issue: https://github.com/warner/python-ecdsa/issues/221
1406+
if sys.version_info < (3, 0): # pragma: no branch
1407+
self.assertEqual(n, 228)
1408+
else:
1409+
self.assertEqual(n, 18)
1410+
13991411
@given(st.integers(min_value=0, max_value=10 ** 200))
14001412
def test_randrange(self, i):
14011413
# util.randrange does not provide long-term stability: we might

0 commit comments

Comments
 (0)