/usr/lib/python3/dist-packages/Cryptodome/SelfTest/Util/test_number.py is in python3-pycryptodome 3.4.7-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | # -*- coding: utf-8 -*-
#
# SelfTest/Util/test_number.py: Self-test for parts of the Cryptodome.Util.number module
#
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
#
# ===================================================================
# The contents of this file are dedicated to the public domain. To
# the extent that dedication to the public domain is not available,
# everyone is granted a worldwide, perpetual, royalty-free,
# non-exclusive license to exercise all rights associated with the
# contents of this file for any purpose whatsoever.
# No rights are reserved.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ===================================================================
"""Self-tests for (some of) Cryptodome.Util.number"""
from Cryptodome.Util.py3compat import *
import unittest
class MyError(Exception):
"""Dummy exception used for tests"""
# NB: In some places, we compare tuples instead of just output values so that
# if any inputs cause a test failure, we'll be able to tell which ones.
class MiscTests(unittest.TestCase):
def setUp(self):
global number, math
from Cryptodome.Util import number
import math
def test_ceil_shift(self):
"""Util.number.ceil_shift"""
self.assertRaises(AssertionError, number.ceil_shift, -1, 1)
self.assertRaises(AssertionError, number.ceil_shift, 1, -1)
# b = 0
self.assertEqual(0, number.ceil_shift(0, 0))
self.assertEqual(1, number.ceil_shift(1, 0))
self.assertEqual(2, number.ceil_shift(2, 0))
self.assertEqual(3, number.ceil_shift(3, 0))
# b = 1
self.assertEqual(0, number.ceil_shift(0, 1))
self.assertEqual(1, number.ceil_shift(1, 1))
self.assertEqual(1, number.ceil_shift(2, 1))
self.assertEqual(2, number.ceil_shift(3, 1))
# b = 2
self.assertEqual(0, number.ceil_shift(0, 2))
self.assertEqual(1, number.ceil_shift(1, 2))
self.assertEqual(1, number.ceil_shift(2, 2))
self.assertEqual(1, number.ceil_shift(3, 2))
self.assertEqual(1, number.ceil_shift(4, 2))
self.assertEqual(2, number.ceil_shift(5, 2))
self.assertEqual(2, number.ceil_shift(6, 2))
self.assertEqual(2, number.ceil_shift(7, 2))
self.assertEqual(2, number.ceil_shift(8, 2))
self.assertEqual(3, number.ceil_shift(9, 2))
for b in range(3, 1+129, 3): # 3, 6, ... , 129
self.assertEqual(0, number.ceil_shift(0, b))
n = 1
while n <= 2**(b+2):
(q, r) = divmod(n-1, 2**b)
expected = q + int(not not r)
self.assertEqual((n-1, b, expected),
(n-1, b, number.ceil_shift(n-1, b)))
(q, r) = divmod(n, 2**b)
expected = q + int(not not r)
self.assertEqual((n, b, expected),
(n, b, number.ceil_shift(n, b)))
(q, r) = divmod(n+1, 2**b)
expected = q + int(not not r)
self.assertEqual((n+1, b, expected),
(n+1, b, number.ceil_shift(n+1, b)))
n *= 2
def test_ceil_div(self):
"""Util.number.ceil_div"""
self.assertRaises(TypeError, number.ceil_div, "1", 1)
self.assertRaises(ZeroDivisionError, number.ceil_div, 1, 0)
self.assertRaises(ZeroDivisionError, number.ceil_div, -1, 0)
# b = -1
self.assertEqual(0, number.ceil_div(0, -1))
self.assertEqual(-1, number.ceil_div(1, -1))
self.assertEqual(-2, number.ceil_div(2, -1))
self.assertEqual(-3, number.ceil_div(3, -1))
# b = 1
self.assertEqual(0, number.ceil_div(0, 1))
self.assertEqual(1, number.ceil_div(1, 1))
self.assertEqual(2, number.ceil_div(2, 1))
self.assertEqual(3, number.ceil_div(3, 1))
# b = 2
self.assertEqual(0, number.ceil_div(0, 2))
self.assertEqual(1, number.ceil_div(1, 2))
self.assertEqual(1, number.ceil_div(2, 2))
self.assertEqual(2, number.ceil_div(3, 2))
self.assertEqual(2, number.ceil_div(4, 2))
self.assertEqual(3, number.ceil_div(5, 2))
# b = 3
self.assertEqual(0, number.ceil_div(0, 3))
self.assertEqual(1, number.ceil_div(1, 3))
self.assertEqual(1, number.ceil_div(2, 3))
self.assertEqual(1, number.ceil_div(3, 3))
self.assertEqual(2, number.ceil_div(4, 3))
self.assertEqual(2, number.ceil_div(5, 3))
self.assertEqual(2, number.ceil_div(6, 3))
self.assertEqual(3, number.ceil_div(7, 3))
# b = 4
self.assertEqual(0, number.ceil_div(0, 4))
self.assertEqual(1, number.ceil_div(1, 4))
self.assertEqual(1, number.ceil_div(2, 4))
self.assertEqual(1, number.ceil_div(3, 4))
self.assertEqual(1, number.ceil_div(4, 4))
self.assertEqual(2, number.ceil_div(5, 4))
self.assertEqual(2, number.ceil_div(6, 4))
self.assertEqual(2, number.ceil_div(7, 4))
self.assertEqual(2, number.ceil_div(8, 4))
self.assertEqual(3, number.ceil_div(9, 4))
# b = -4
self.assertEqual(3, number.ceil_div(-9, -4))
self.assertEqual(2, number.ceil_div(-8, -4))
self.assertEqual(2, number.ceil_div(-7, -4))
self.assertEqual(2, number.ceil_div(-6, -4))
self.assertEqual(2, number.ceil_div(-5, -4))
self.assertEqual(1, number.ceil_div(-4, -4))
self.assertEqual(1, number.ceil_div(-3, -4))
self.assertEqual(1, number.ceil_div(-2, -4))
self.assertEqual(1, number.ceil_div(-1, -4))
self.assertEqual(0, number.ceil_div(0, -4))
self.assertEqual(0, number.ceil_div(1, -4))
self.assertEqual(0, number.ceil_div(2, -4))
self.assertEqual(0, number.ceil_div(3, -4))
self.assertEqual(-1, number.ceil_div(4, -4))
self.assertEqual(-1, number.ceil_div(5, -4))
self.assertEqual(-1, number.ceil_div(6, -4))
self.assertEqual(-1, number.ceil_div(7, -4))
self.assertEqual(-2, number.ceil_div(8, -4))
self.assertEqual(-2, number.ceil_div(9, -4))
def test_exact_log2(self):
"""Util.number.exact_log2"""
self.assertRaises(TypeError, number.exact_log2, "0")
self.assertRaises(ValueError, number.exact_log2, -1)
self.assertRaises(ValueError, number.exact_log2, 0)
self.assertEqual(0, number.exact_log2(1))
self.assertEqual(1, number.exact_log2(2))
self.assertRaises(ValueError, number.exact_log2, 3)
self.assertEqual(2, number.exact_log2(4))
self.assertRaises(ValueError, number.exact_log2, 5)
self.assertRaises(ValueError, number.exact_log2, 6)
self.assertRaises(ValueError, number.exact_log2, 7)
e = 3
n = 8
while e < 16:
if n == 2**e:
self.assertEqual(e, number.exact_log2(n), "expected=2**%d, n=%d" % (e, n))
e += 1
else:
self.assertRaises(ValueError, number.exact_log2, n)
n += 1
for e in range(16, 1+64, 2):
self.assertRaises(ValueError, number.exact_log2, 2**e-1)
self.assertEqual(e, number.exact_log2(2**e))
self.assertRaises(ValueError, number.exact_log2, 2**e+1)
def test_exact_div(self):
"""Util.number.exact_div"""
# Positive numbers
self.assertEqual(1, number.exact_div(1, 1))
self.assertRaises(ValueError, number.exact_div, 1, 2)
self.assertEqual(1, number.exact_div(2, 2))
self.assertRaises(ValueError, number.exact_div, 3, 2)
self.assertEqual(2, number.exact_div(4, 2))
# Negative numbers
self.assertEqual(-1, number.exact_div(-1, 1))
self.assertEqual(-1, number.exact_div(1, -1))
self.assertRaises(ValueError, number.exact_div, -1, 2)
self.assertEqual(1, number.exact_div(-2, -2))
self.assertEqual(-2, number.exact_div(-4, 2))
# Zero dividend
self.assertEqual(0, number.exact_div(0, 1))
self.assertEqual(0, number.exact_div(0, 2))
# Zero divisor (allow_divzero == False)
self.assertRaises(ZeroDivisionError, number.exact_div, 0, 0)
self.assertRaises(ZeroDivisionError, number.exact_div, 1, 0)
# Zero divisor (allow_divzero == True)
self.assertEqual(0, number.exact_div(0, 0, allow_divzero=True))
self.assertRaises(ValueError, number.exact_div, 1, 0, allow_divzero=True)
def test_floor_div(self):
"""Util.number.floor_div"""
self.assertRaises(TypeError, number.floor_div, "1", 1)
for a in range(-10, 10):
for b in range(-10, 10):
if b == 0:
self.assertRaises(ZeroDivisionError, number.floor_div, a, b)
else:
self.assertEqual((a, b, int(math.floor(float(a) / b))),
(a, b, number.floor_div(a, b)))
def test_getStrongPrime(self):
"""Util.number.getStrongPrime"""
self.assertRaises(ValueError, number.getStrongPrime, 256)
self.assertRaises(ValueError, number.getStrongPrime, 513)
bits = 512
x = number.getStrongPrime(bits)
self.assertNotEqual(x % 2, 0)
self.assertEqual(x > (1 << bits-1)-1, 1)
self.assertEqual(x < (1 << bits), 1)
e = 2**16+1
x = number.getStrongPrime(bits, e)
self.assertEqual(number.GCD(x-1, e), 1)
self.assertNotEqual(x % 2, 0)
self.assertEqual(x > (1 << bits-1)-1, 1)
self.assertEqual(x < (1 << bits), 1)
e = 2**16+2
x = number.getStrongPrime(bits, e)
self.assertEqual(number.GCD((x-1)>>1, e), 1)
self.assertNotEqual(x % 2, 0)
self.assertEqual(x > (1 << bits-1)-1, 1)
self.assertEqual(x < (1 << bits), 1)
def test_isPrime(self):
"""Util.number.isPrime"""
self.assertEqual(number.isPrime(-3), False) # Regression test: negative numbers should not be prime
self.assertEqual(number.isPrime(-2), False) # Regression test: negative numbers should not be prime
self.assertEqual(number.isPrime(1), False) # Regression test: isPrime(1) caused some versions of PyCryptodome to crash.
self.assertEqual(number.isPrime(2), True)
self.assertEqual(number.isPrime(3), True)
self.assertEqual(number.isPrime(4), False)
self.assertEqual(number.isPrime(2**1279-1), True)
self.assertEqual(number.isPrime(-(2**1279-1)), False) # Regression test: negative numbers should not be prime
# test some known gmp pseudo-primes taken from
# http://www.trnicely.net/misc/mpzspsp.html
for composite in (43 * 127 * 211, 61 * 151 * 211, 15259 * 30517,
346141 * 692281, 1007119 * 2014237, 3589477 * 7178953,
4859419 * 9718837, 2730439 * 5460877,
245127919 * 490255837, 963939391 * 1927878781,
4186358431 * 8372716861, 1576820467 * 3153640933):
self.assertEqual(number.isPrime(int(composite)), False)
def test_size(self):
self.assertEqual(number.size(2),2)
self.assertEqual(number.size(3),2)
self.assertEqual(number.size(0xa2),8)
self.assertEqual(number.size(0xa2ba40),8*3)
self.assertEqual(number.size(0xa2ba40ee07e3b2bd2f02ce227f36a195024486e49c19cb41bbbdfbba98b22b0e577c2eeaffa20d883a76e65e394c69d4b3c05a1e8fadda27edb2a42bc000fe888b9b32c22d15add0cd76b3e7936e19955b220dd17d4ea904b1ec102b2e4de7751222aa99151024c7cb41cc5ea21d00eeb41f7c800834d2c6e06bce3bce7ea9a5), 1024)
def get_tests(config={}):
from Cryptodome.SelfTest.st_common import list_test_cases
tests = list_test_cases(MiscTests)
return tests
if __name__ == '__main__':
suite = lambda: unittest.TestSuite(get_tests())
unittest.main(defaultTest='suite')
# vim:set ts=4 sw=4 sts=4 expandtab:
|