test_Numbers.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. #
  2. # SelfTest/Math/test_Numbers.py: Self-test for Numbers module
  3. #
  4. # ===================================================================
  5. #
  6. # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  7. # All rights reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. #
  13. # 1. Redistributions of source code must retain the above copyright
  14. # notice, this list of conditions and the following disclaimer.
  15. # 2. Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in
  17. # the documentation and/or other materials provided with the
  18. # distribution.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. # POSSIBILITY OF SUCH DAMAGE.
  32. # ===================================================================
  33. """Self-test for Math.Numbers"""
  34. import sys
  35. import unittest
  36. from Cryptodome.SelfTest.st_common import list_test_cases
  37. from Cryptodome.Util.py3compat import *
  38. from Cryptodome.Math._IntegerNative import IntegerNative
  39. class TestIntegerBase(unittest.TestCase):
  40. def setUp(self):
  41. raise NotImplementedError("To be implemented")
  42. def Integers(self, *arg):
  43. return map(self.Integer, arg)
  44. def test_init_and_equality(self):
  45. Integer = self.Integer
  46. v1 = Integer(23)
  47. v2 = Integer(v1)
  48. v3 = Integer(-9)
  49. self.assertRaises(ValueError, Integer, 1.0)
  50. v4 = Integer(10**10)
  51. v5 = Integer(-10**10)
  52. v6 = Integer(0xFFFF)
  53. v7 = Integer(0xFFFFFFFF)
  54. v8 = Integer(0xFFFFFFFFFFFFFFFF)
  55. self.assertEqual(v1, v1)
  56. self.assertEqual(v1, 23)
  57. self.assertEqual(v1, v2)
  58. self.assertEqual(v3, -9)
  59. self.assertEqual(v4, 10 ** 10)
  60. self.assertEqual(v5, -10 ** 10)
  61. self.assertEqual(v6, 0xFFFF)
  62. self.assertEqual(v7, 0xFFFFFFFF)
  63. self.assertEqual(v8, 0xFFFFFFFFFFFFFFFF)
  64. self.assertFalse(v1 == v4)
  65. # Init and comparison between Integer's
  66. v6 = Integer(v1)
  67. self.assertEqual(v1, v6)
  68. self.assertFalse(Integer(0) == None)
  69. def test_conversion_to_int(self):
  70. v1, v2 = self.Integers(-23, 2 ** 1000)
  71. self.assertEqual(int(v1), -23)
  72. self.assertEqual(int(v2), 2 ** 1000)
  73. def test_equality_with_ints(self):
  74. v1, v2, v3 = self.Integers(23, -89, 2 ** 1000)
  75. self.assertTrue(v1 == 23)
  76. self.assertTrue(v2 == -89)
  77. self.assertFalse(v1 == 24)
  78. self.assertTrue(v3 == 2 ** 1000)
  79. def test_conversion_to_str(self):
  80. v1, v2, v3, v4 = self.Integers(20, 0, -20, 2 ** 1000)
  81. self.assertTrue(str(v1) == "20")
  82. self.assertTrue(str(v2) == "0")
  83. self.assertTrue(str(v3) == "-20")
  84. self.assertTrue(str(v4) == "10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376")
  85. def test_repr(self):
  86. v1, v2 = self.Integers(-1, 2**80)
  87. self.assertEqual(repr(v1), "Integer(-1)")
  88. self.assertEqual(repr(v2), "Integer(1208925819614629174706176)")
  89. def test_conversion_to_bytes(self):
  90. Integer = self.Integer
  91. v1 = Integer(0x17)
  92. self.assertEqual(b("\x17"), v1.to_bytes())
  93. v2 = Integer(0xFFFF)
  94. self.assertEqual(b("\xFF\xFF"), v2.to_bytes())
  95. self.assertEqual(b("\x00\xFF\xFF"), v2.to_bytes(3))
  96. self.assertRaises(ValueError, v2.to_bytes, 1)
  97. v3 = Integer(-90)
  98. self.assertRaises(ValueError, v3.to_bytes)
  99. def test_conversion_from_bytes(self):
  100. Integer = self.Integer
  101. v1 = Integer.from_bytes(b("\x00"))
  102. self.assertTrue(isinstance(v1, Integer))
  103. self.assertEqual(0, v1)
  104. v2 = Integer.from_bytes(b("\x00\x00"))
  105. self.assertEqual(0, v2)
  106. v3 = Integer.from_bytes(b("\xFF\xFF"))
  107. self.assertEqual(0xFFFF, v3)
  108. def test_inequality(self):
  109. # Test Integer!=Integer and Integer!=int
  110. v1, v2, v3, v4 = self.Integers(89, 89, 90, -8)
  111. self.assertTrue(v1 != v3)
  112. self.assertTrue(v1 != 90)
  113. self.assertFalse(v1 != v2)
  114. self.assertFalse(v1 != 89)
  115. self.assertTrue(v1 != v4)
  116. self.assertTrue(v4 != v1)
  117. self.assertTrue(self.Integer(0) != None)
  118. def test_less_than(self):
  119. # Test Integer<Integer and Integer<int
  120. v1, v2, v3, v4, v5 = self.Integers(13, 13, 14, -8, 2 ** 10)
  121. self.assertTrue(v1 < v3)
  122. self.assertTrue(v1 < 14)
  123. self.assertFalse(v1 < v2)
  124. self.assertFalse(v1 < 13)
  125. self.assertTrue(v4 < v1)
  126. self.assertFalse(v1 < v4)
  127. self.assertTrue(v1 < v5)
  128. self.assertFalse(v5 < v1)
  129. def test_less_than_or_equal(self):
  130. # Test Integer<=Integer and Integer<=int
  131. v1, v2, v3, v4, v5 = self.Integers(13, 13, 14, -4, 2 ** 10)
  132. self.assertTrue(v1 <= v1)
  133. self.assertTrue(v1 <= 13)
  134. self.assertTrue(v1 <= v2)
  135. self.assertTrue(v1 <= 14)
  136. self.assertTrue(v1 <= v3)
  137. self.assertFalse(v1 <= v4)
  138. self.assertTrue(v1 <= v5)
  139. self.assertFalse(v5 <= v1)
  140. def test_more_than(self):
  141. # Test Integer>Integer and Integer>int
  142. v1, v2, v3, v4, v5 = self.Integers(13, 13, 14, -8, 2 ** 10)
  143. self.assertTrue(v3 > v1)
  144. self.assertTrue(v3 > 13)
  145. self.assertFalse(v1 > v1)
  146. self.assertFalse(v1 > v2)
  147. self.assertFalse(v1 > 13)
  148. self.assertTrue(v1 > v4)
  149. self.assertFalse(v4 > v1)
  150. self.assertTrue(v5 > v1)
  151. self.assertFalse(v1 > v5)
  152. def test_more_than_or_equal(self):
  153. # Test Integer>=Integer and Integer>=int
  154. v1, v2, v3, v4 = self.Integers(13, 13, 14, -4)
  155. self.assertTrue(v3 >= v1)
  156. self.assertTrue(v3 >= 13)
  157. self.assertTrue(v1 >= v2)
  158. self.assertTrue(v1 >= v1)
  159. self.assertTrue(v1 >= 13)
  160. self.assertFalse(v4 >= v1)
  161. def test_bool(self):
  162. v1, v2, v3, v4 = self.Integers(0, 10, -9, 2 ** 10)
  163. self.assertFalse(v1)
  164. self.assertFalse(bool(v1))
  165. self.assertTrue(v2)
  166. self.assertTrue(bool(v2))
  167. self.assertTrue(v3)
  168. self.assertTrue(v4)
  169. def test_is_negative(self):
  170. v1, v2, v3, v4, v5 = self.Integers(-3 ** 100, -3, 0, 3, 3**100)
  171. self.assertTrue(v1.is_negative())
  172. self.assertTrue(v2.is_negative())
  173. self.assertFalse(v4.is_negative())
  174. self.assertFalse(v5.is_negative())
  175. def test_addition(self):
  176. # Test Integer+Integer and Integer+int
  177. v1, v2, v3 = self.Integers(7, 90, -7)
  178. self.assertTrue(isinstance(v1 + v2, self.Integer))
  179. self.assertEqual(v1 + v2, 97)
  180. self.assertEqual(v1 + 90, 97)
  181. self.assertEqual(v1 + v3, 0)
  182. self.assertEqual(v1 + (-7), 0)
  183. self.assertEqual(v1 + 2 ** 10, 2 ** 10 + 7)
  184. def test_subtraction(self):
  185. # Test Integer-Integer and Integer-int
  186. v1, v2, v3 = self.Integers(7, 90, -7)
  187. self.assertTrue(isinstance(v1 - v2, self.Integer))
  188. self.assertEqual(v2 - v1, 83)
  189. self.assertEqual(v2 - 7, 83)
  190. self.assertEqual(v2 - v3, 97)
  191. self.assertEqual(v1 - (-7), 14)
  192. self.assertEqual(v1 - 2 ** 10, 7 - 2 ** 10)
  193. def test_multiplication(self):
  194. # Test Integer-Integer and Integer-int
  195. v1, v2, v3, v4 = self.Integers(4, 5, -2, 2 ** 10)
  196. self.assertTrue(isinstance(v1 * v2, self.Integer))
  197. self.assertEqual(v1 * v2, 20)
  198. self.assertEqual(v1 * 5, 20)
  199. self.assertEqual(v1 * -2, -8)
  200. self.assertEqual(v1 * 2 ** 10, 4 * (2 ** 10))
  201. def test_floor_div(self):
  202. v1, v2, v3 = self.Integers(3, 8, 2 ** 80)
  203. self.assertTrue(isinstance(v1 // v2, self.Integer))
  204. self.assertEqual(v2 // v1, 2)
  205. self.assertEqual(v2 // 3, 2)
  206. self.assertEqual(v2 // -3, -3)
  207. self.assertEqual(v3 // 2 ** 79, 2)
  208. self.assertRaises(ZeroDivisionError, lambda: v1 // 0)
  209. def test_remainder(self):
  210. # Test Integer%Integer and Integer%int
  211. v1, v2, v3 = self.Integers(23, 5, -4)
  212. self.assertTrue(isinstance(v1 % v2, self.Integer))
  213. self.assertEqual(v1 % v2, 3)
  214. self.assertEqual(v1 % 5, 3)
  215. self.assertEqual(v3 % 5, 1)
  216. self.assertEqual(v1 % 2 ** 10, 23)
  217. self.assertRaises(ZeroDivisionError, lambda: v1 % 0)
  218. self.assertRaises(ValueError, lambda: v1 % -6)
  219. def test_simple_exponentiation(self):
  220. v1, v2, v3 = self.Integers(4, 3, -2)
  221. self.assertTrue(isinstance(v1 ** v2, self.Integer))
  222. self.assertEqual(v1 ** v2, 64)
  223. self.assertEqual(pow(v1, v2), 64)
  224. self.assertEqual(v1 ** 3, 64)
  225. self.assertEqual(pow(v1, 3), 64)
  226. self.assertEqual(v3 ** 2, 4)
  227. self.assertEqual(v3 ** 3, -8)
  228. self.assertRaises(ValueError, pow, v1, -3)
  229. def test_modular_exponentiation(self):
  230. v1, v2, v3 = self.Integers(23, 5, 17)
  231. self.assertTrue(isinstance(pow(v1, v2, v3), self.Integer))
  232. self.assertEqual(pow(v1, v2, v3), 7)
  233. self.assertEqual(pow(v1, 5, v3), 7)
  234. self.assertEqual(pow(v1, v2, 17), 7)
  235. self.assertEqual(pow(v1, 5, 17), 7)
  236. self.assertEqual(pow(v1, 0, 17), 1)
  237. self.assertEqual(pow(v1, 1, 2 ** 80), 23)
  238. self.assertEqual(pow(v1, 2 ** 80, 89298), 17689)
  239. self.assertRaises(ZeroDivisionError, pow, v1, 5, 0)
  240. self.assertRaises(ValueError, pow, v1, 5, -4)
  241. self.assertRaises(ValueError, pow, v1, -3, 8)
  242. def test_inplace_exponentiation(self):
  243. v1 = self.Integer(4)
  244. v1.inplace_pow(2)
  245. self.assertEqual(v1, 16)
  246. v1 = self.Integer(4)
  247. v1.inplace_pow(2, 15)
  248. self.assertEqual(v1, 1)
  249. def test_abs(self):
  250. v1, v2, v3, v4, v5 = self.Integers(-2 ** 100, -2, 0, 2, 2 ** 100)
  251. self.assertEqual(abs(v1), 2 ** 100)
  252. self.assertEqual(abs(v2), 2)
  253. self.assertEqual(abs(v3), 0)
  254. self.assertEqual(abs(v4), 2)
  255. self.assertEqual(abs(v5), 2 ** 100)
  256. def test_sqrt(self):
  257. v1, v2, v3, v4 = self.Integers(-2, 0, 49, 10**100)
  258. self.assertRaises(ValueError, v1.sqrt)
  259. self.assertEqual(v2.sqrt(), 0)
  260. self.assertEqual(v3.sqrt(), 7)
  261. self.assertEqual(v4.sqrt(), 10**50)
  262. def test_sqrt_module(self):
  263. # Invalid modulus (non positive)
  264. self.assertRaises(ValueError, self.Integer(5).sqrt, 0)
  265. self.assertRaises(ValueError, self.Integer(5).sqrt, -1)
  266. # Simple cases
  267. assert self.Integer(0).sqrt(5) == 0
  268. assert self.Integer(1).sqrt(5) in (1, 4)
  269. # Test with all quadratic residues in several fields
  270. for p in (11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53):
  271. for i in range(0, p):
  272. square = i**2 % p
  273. res = self.Integer(square).sqrt(p)
  274. assert res in (i, p - i)
  275. # 2 is a non-quadratic reside in Z_11
  276. self.assertRaises(ValueError, self.Integer(2).sqrt, 11)
  277. # 10 is not a prime
  278. self.assertRaises(ValueError, self.Integer(4).sqrt, 10)
  279. # 5 is square residue of 4 and 7
  280. assert self.Integer(5 - 11).sqrt(11) in (4, 7)
  281. assert self.Integer(5 + 11).sqrt(11) in (4, 7)
  282. def test_in_place_add(self):
  283. v1, v2 = self.Integers(10, 20)
  284. v1 += v2
  285. self.assertEqual(v1, 30)
  286. v1 += 10
  287. self.assertEqual(v1, 40)
  288. v1 += -1
  289. self.assertEqual(v1, 39)
  290. v1 += 2 ** 1000
  291. self.assertEqual(v1, 39 + 2 ** 1000)
  292. def test_in_place_sub(self):
  293. v1, v2 = self.Integers(10, 20)
  294. v1 -= v2
  295. self.assertEqual(v1, -10)
  296. v1 -= -100
  297. self.assertEqual(v1, 90)
  298. v1 -= 90000
  299. self.assertEqual(v1, -89910)
  300. v1 -= -100000
  301. self.assertEqual(v1, 10090)
  302. def test_in_place_mul(self):
  303. v1, v2 = self.Integers(3, 5)
  304. v1 *= v2
  305. self.assertEqual(v1, 15)
  306. v1 *= 2
  307. self.assertEqual(v1, 30)
  308. v1 *= -2
  309. self.assertEqual(v1, -60)
  310. v1 *= 2 ** 1000
  311. self.assertEqual(v1, -60 * (2 ** 1000))
  312. def test_in_place_modulus(self):
  313. v1, v2 = self.Integers(20, 7)
  314. v1 %= v2
  315. self.assertEqual(v1, 6)
  316. v1 %= 2 ** 1000
  317. self.assertEqual(v1, 6)
  318. v1 %= 2
  319. self.assertEqual(v1, 0)
  320. def t():
  321. v3 = self.Integer(9)
  322. v3 %= 0
  323. self.assertRaises(ZeroDivisionError, t)
  324. def test_and(self):
  325. v1, v2, v3 = self.Integers(0xF4, 0x31, -0xF)
  326. self.assertTrue(isinstance(v1 & v2, self.Integer))
  327. self.assertEqual(v1 & v2, 0x30)
  328. self.assertEqual(v1 & 0x31, 0x30)
  329. self.assertEqual(v1 & v3, 0xF0)
  330. self.assertEqual(v1 & -0xF, 0xF0)
  331. self.assertEqual(v3 & -0xF, -0xF)
  332. self.assertEqual(v2 & (2 ** 1000 + 0x31), 0x31)
  333. def test_or(self):
  334. v1, v2, v3 = self.Integers(0x40, 0x82, -0xF)
  335. self.assertTrue(isinstance(v1 | v2, self.Integer))
  336. self.assertEqual(v1 | v2, 0xC2)
  337. self.assertEqual(v1 | 0x82, 0xC2)
  338. self.assertEqual(v2 | v3, -0xD)
  339. self.assertEqual(v2 | 2 ** 1000, 2 ** 1000 + 0x82)
  340. def test_right_shift(self):
  341. v1, v2, v3 = self.Integers(0x10, 1, -0x10)
  342. self.assertEqual(v1 >> 0, v1)
  343. self.assertTrue(isinstance(v1 >> v2, self.Integer))
  344. self.assertEqual(v1 >> v2, 0x08)
  345. self.assertEqual(v1 >> 1, 0x08)
  346. self.assertRaises(ValueError, lambda: v1 >> -1)
  347. self.assertEqual(v1 >> (2 ** 1000), 0)
  348. self.assertEqual(v3 >> 1, -0x08)
  349. self.assertEqual(v3 >> (2 ** 1000), -1)
  350. def test_in_place_right_shift(self):
  351. v1, v2, v3 = self.Integers(0x10, 1, -0x10)
  352. v1 >>= 0
  353. self.assertEqual(v1, 0x10)
  354. v1 >>= 1
  355. self.assertEqual(v1, 0x08)
  356. v1 >>= v2
  357. self.assertEqual(v1, 0x04)
  358. v3 >>= 1
  359. self.assertEqual(v3, -0x08)
  360. def l():
  361. v4 = self.Integer(0x90)
  362. v4 >>= -1
  363. self.assertRaises(ValueError, l)
  364. def m1():
  365. v4 = self.Integer(0x90)
  366. v4 >>= 2 ** 1000
  367. return v4
  368. self.assertEqual(0, m1())
  369. def m2():
  370. v4 = self.Integer(-1)
  371. v4 >>= 2 ** 1000
  372. return v4
  373. self.assertEqual(-1, m2())
  374. def _test_left_shift(self):
  375. v1, v2, v3 = self.Integers(0x10, 1, -0x10)
  376. self.assertEqual(v1 << 0, v1)
  377. self.assertTrue(isinstance(v1 << v2, self.Integer))
  378. self.assertEqual(v1 << v2, 0x20)
  379. self.assertEqual(v1 << 1, 0x20)
  380. self.assertEqual(v3 << 1, -0x20)
  381. self.assertRaises(ValueError, lambda: v1 << -1)
  382. self.assertRaises(ValueError, lambda: v1 << (2 ** 1000))
  383. def test_in_place_left_shift(self):
  384. v1, v2, v3 = self.Integers(0x10, 1, -0x10)
  385. v1 <<= 0
  386. self.assertEqual(v1, 0x10)
  387. v1 <<= 1
  388. self.assertEqual(v1, 0x20)
  389. v1 <<= v2
  390. self.assertEqual(v1, 0x40)
  391. v3 <<= 1
  392. self.assertEqual(v3, -0x20)
  393. def l():
  394. v4 = self.Integer(0x90)
  395. v4 <<= -1
  396. self.assertRaises(ValueError, l)
  397. def m():
  398. v4 = self.Integer(0x90)
  399. v4 <<= 2 ** 1000
  400. self.assertRaises(ValueError, m)
  401. def test_get_bit(self):
  402. v1, v2, v3 = self.Integers(0x102, -3, 1)
  403. self.assertEqual(v1.get_bit(0), 0)
  404. self.assertEqual(v1.get_bit(1), 1)
  405. self.assertEqual(v1.get_bit(v3), 1)
  406. self.assertEqual(v1.get_bit(8), 1)
  407. self.assertEqual(v1.get_bit(9), 0)
  408. self.assertRaises(ValueError, v1.get_bit, -1)
  409. self.assertEqual(v1.get_bit(2 ** 1000), 0)
  410. self.assertRaises(ValueError, v2.get_bit, -1)
  411. self.assertRaises(ValueError, v2.get_bit, 0)
  412. self.assertRaises(ValueError, v2.get_bit, 1)
  413. self.assertRaises(ValueError, v2.get_bit, 2 * 1000)
  414. def test_odd_even(self):
  415. v1, v2, v3, v4, v5 = self.Integers(0, 4, 17, -4, -17)
  416. self.assertTrue(v1.is_even())
  417. self.assertTrue(v2.is_even())
  418. self.assertFalse(v3.is_even())
  419. self.assertTrue(v4.is_even())
  420. self.assertFalse(v5.is_even())
  421. self.assertFalse(v1.is_odd())
  422. self.assertFalse(v2.is_odd())
  423. self.assertTrue(v3.is_odd())
  424. self.assertFalse(v4.is_odd())
  425. self.assertTrue(v5.is_odd())
  426. def test_size_in_bits(self):
  427. v1, v2, v3, v4 = self.Integers(0, 1, 0x100, -90)
  428. self.assertEqual(v1.size_in_bits(), 1)
  429. self.assertEqual(v2.size_in_bits(), 1)
  430. self.assertEqual(v3.size_in_bits(), 9)
  431. self.assertRaises(ValueError, v4.size_in_bits)
  432. def test_size_in_bytes(self):
  433. v1, v2, v3, v4, v5, v6 = self.Integers(0, 1, 0xFF, 0x1FF, 0x10000, -9)
  434. self.assertEqual(v1.size_in_bytes(), 1)
  435. self.assertEqual(v2.size_in_bytes(), 1)
  436. self.assertEqual(v3.size_in_bytes(), 1)
  437. self.assertEqual(v4.size_in_bytes(), 2)
  438. self.assertEqual(v5.size_in_bytes(), 3)
  439. self.assertRaises(ValueError, v6.size_in_bits)
  440. def test_perfect_square(self):
  441. self.assertFalse(self.Integer(-9).is_perfect_square())
  442. self.assertTrue(self.Integer(0).is_perfect_square())
  443. self.assertTrue(self.Integer(1).is_perfect_square())
  444. self.assertFalse(self.Integer(2).is_perfect_square())
  445. self.assertFalse(self.Integer(3).is_perfect_square())
  446. self.assertTrue(self.Integer(4).is_perfect_square())
  447. self.assertTrue(self.Integer(39*39).is_perfect_square())
  448. self.assertFalse(self.Integer(39*39+1).is_perfect_square())
  449. for x in range(100, 1000):
  450. self.assertFalse(self.Integer(x**2+1).is_perfect_square())
  451. self.assertTrue(self.Integer(x**2).is_perfect_square())
  452. def test_fail_if_divisible_by(self):
  453. v1, v2, v3 = self.Integers(12, -12, 4)
  454. # No failure expected
  455. v1.fail_if_divisible_by(7)
  456. v2.fail_if_divisible_by(7)
  457. v2.fail_if_divisible_by(2 ** 80)
  458. # Failure expected
  459. self.assertRaises(ValueError, v1.fail_if_divisible_by, 4)
  460. self.assertRaises(ValueError, v1.fail_if_divisible_by, v3)
  461. def test_multiply_accumulate(self):
  462. v1, v2, v3 = self.Integers(4, 3, 2)
  463. v1.multiply_accumulate(v2, v3)
  464. self.assertEqual(v1, 10)
  465. v1.multiply_accumulate(v2, 2)
  466. self.assertEqual(v1, 16)
  467. v1.multiply_accumulate(3, v3)
  468. self.assertEqual(v1, 22)
  469. v1.multiply_accumulate(1, -2)
  470. self.assertEqual(v1, 20)
  471. v1.multiply_accumulate(-2, 1)
  472. self.assertEqual(v1, 18)
  473. v1.multiply_accumulate(1, 2 ** 1000)
  474. self.assertEqual(v1, 18 + 2 ** 1000)
  475. v1.multiply_accumulate(2 ** 1000, 1)
  476. self.assertEqual(v1, 18 + 2 ** 1001)
  477. def test_set(self):
  478. v1, v2 = self.Integers(3, 6)
  479. v1.set(v2)
  480. self.assertEqual(v1, 6)
  481. v1.set(9)
  482. self.assertEqual(v1, 9)
  483. v1.set(-2)
  484. self.assertEqual(v1, -2)
  485. v1.set(2 ** 1000)
  486. self.assertEqual(v1, 2 ** 1000)
  487. def test_inverse(self):
  488. v1, v2, v3, v4, v5, v6 = self.Integers(2, 5, -3, 0, 723872, 3433)
  489. self.assertTrue(isinstance(v1.inverse(v2), self.Integer))
  490. self.assertEqual(v1.inverse(v2), 3)
  491. self.assertEqual(v1.inverse(5), 3)
  492. self.assertEqual(v3.inverse(5), 3)
  493. self.assertEqual(v5.inverse(92929921), 58610507)
  494. self.assertEqual(v6.inverse(9912), 5353)
  495. self.assertRaises(ValueError, v2.inverse, 10)
  496. self.assertRaises(ValueError, v1.inverse, -3)
  497. self.assertRaises(ValueError, v4.inverse, 10)
  498. self.assertRaises(ZeroDivisionError, v2.inverse, 0)
  499. def test_inplace_inverse(self):
  500. v1, v2 = self.Integers(2, 5)
  501. v1.inplace_inverse(v2)
  502. self.assertEqual(v1, 3)
  503. def test_gcd(self):
  504. v1, v2, v3, v4 = self.Integers(6, 10, 17, -2)
  505. self.assertTrue(isinstance(v1.gcd(v2), self.Integer))
  506. self.assertEqual(v1.gcd(v2), 2)
  507. self.assertEqual(v1.gcd(10), 2)
  508. self.assertEqual(v1.gcd(v3), 1)
  509. self.assertEqual(v1.gcd(-2), 2)
  510. self.assertEqual(v4.gcd(6), 2)
  511. def test_lcm(self):
  512. v1, v2, v3, v4, v5 = self.Integers(6, 10, 17, -2, 0)
  513. self.assertTrue(isinstance(v1.lcm(v2), self.Integer))
  514. self.assertEqual(v1.lcm(v2), 30)
  515. self.assertEqual(v1.lcm(10), 30)
  516. self.assertEqual(v1.lcm(v3), 102)
  517. self.assertEqual(v1.lcm(-2), 6)
  518. self.assertEqual(v4.lcm(6), 6)
  519. self.assertEqual(v1.lcm(0), 0)
  520. self.assertEqual(v5.lcm(0), 0)
  521. def test_jacobi_symbol(self):
  522. data = (
  523. (1001, 1, 1),
  524. (19, 45, 1),
  525. (8, 21, -1),
  526. (5, 21, 1),
  527. (610, 987, -1),
  528. (1001, 9907, -1),
  529. (5, 3439601197, -1)
  530. )
  531. js = self.Integer.jacobi_symbol
  532. # Jacobi symbol is always 1 for k==1 or n==1
  533. for k in range(1, 30):
  534. self.assertEqual(js(k, 1), 1)
  535. for n in range(1, 30, 2):
  536. self.assertEqual(js(1, n), 1)
  537. # Fail if n is not positive odd
  538. self.assertRaises(ValueError, js, 6, -2)
  539. self.assertRaises(ValueError, js, 6, -1)
  540. self.assertRaises(ValueError, js, 6, 0)
  541. self.assertRaises(ValueError, js, 0, 0)
  542. self.assertRaises(ValueError, js, 6, 2)
  543. self.assertRaises(ValueError, js, 6, 4)
  544. self.assertRaises(ValueError, js, 6, 6)
  545. self.assertRaises(ValueError, js, 6, 8)
  546. for tv in data:
  547. self.assertEqual(js(tv[0], tv[1]), tv[2])
  548. self.assertEqual(js(self.Integer(tv[0]), tv[1]), tv[2])
  549. self.assertEqual(js(tv[0], self.Integer(tv[1])), tv[2])
  550. def test_jacobi_symbol_wikipedia(self):
  551. # Test vectors from https://en.wikipedia.org/wiki/Jacobi_symbol
  552. tv = [
  553. (3, [(1, 1), (2, -1), (3, 0), (4, 1), (5, -1), (6, 0), (7, 1), (8, -1), (9, 0), (10, 1), (11, -1), (12, 0), (13, 1), (14, -1), (15, 0), (16, 1), (17, -1), (18, 0), (19, 1), (20, -1), (21, 0), (22, 1), (23, -1), (24, 0), (25, 1), (26, -1), (27, 0), (28, 1), (29, -1), (30, 0)]),
  554. (5, [(1, 1), (2, -1), (3, -1), (4, 1), (5, 0), (6, 1), (7, -1), (8, -1), (9, 1), (10, 0), (11, 1), (12, -1), (13, -1), (14, 1), (15, 0), (16, 1), (17, -1), (18, -1), (19, 1), (20, 0), (21, 1), (22, -1), (23, -1), (24, 1), (25, 0), (26, 1), (27, -1), (28, -1), (29, 1), (30, 0)]),
  555. (7, [(1, 1), (2, 1), (3, -1), (4, 1), (5, -1), (6, -1), (7, 0), (8, 1), (9, 1), (10, -1), (11, 1), (12, -1), (13, -1), (14, 0), (15, 1), (16, 1), (17, -1), (18, 1), (19, -1), (20, -1), (21, 0), (22, 1), (23, 1), (24, -1), (25, 1), (26, -1), (27, -1), (28, 0), (29, 1), (30, 1)]),
  556. (9, [(1, 1), (2, 1), (3, 0), (4, 1), (5, 1), (6, 0), (7, 1), (8, 1), (9, 0), (10, 1), (11, 1), (12, 0), (13, 1), (14, 1), (15, 0), (16, 1), (17, 1), (18, 0), (19, 1), (20, 1), (21, 0), (22, 1), (23, 1), (24, 0), (25, 1), (26, 1), (27, 0), (28, 1), (29, 1), (30, 0)]),
  557. (11, [(1, 1), (2, -1), (3, 1), (4, 1), (5, 1), (6, -1), (7, -1), (8, -1), (9, 1), (10, -1), (11, 0), (12, 1), (13, -1), (14, 1), (15, 1), (16, 1), (17, -1), (18, -1), (19, -1), (20, 1), (21, -1), (22, 0), (23, 1), (24, -1), (25, 1), (26, 1), (27, 1), (28, -1), (29, -1), (30, -1)]),
  558. (13, [(1, 1), (2, -1), (3, 1), (4, 1), (5, -1), (6, -1), (7, -1), (8, -1), (9, 1), (10, 1), (11, -1), (12, 1), (13, 0), (14, 1), (15, -1), (16, 1), (17, 1), (18, -1), (19, -1), (20, -1), (21, -1), (22, 1), (23, 1), (24, -1), (25, 1), (26, 0), (27, 1), (28, -1), (29, 1), (30, 1)]),
  559. (15, [(1, 1), (2, 1), (3, 0), (4, 1), (5, 0), (6, 0), (7, -1), (8, 1), (9, 0), (10, 0), (11, -1), (12, 0), (13, -1), (14, -1), (15, 0), (16, 1), (17, 1), (18, 0), (19, 1), (20, 0), (21, 0), (22, -1), (23, 1), (24, 0), (25, 0), (26, -1), (27, 0), (28, -1), (29, -1), (30, 0)]),
  560. (17, [(1, 1), (2, 1), (3, -1), (4, 1), (5, -1), (6, -1), (7, -1), (8, 1), (9, 1), (10, -1), (11, -1), (12, -1), (13, 1), (14, -1), (15, 1), (16, 1), (17, 0), (18, 1), (19, 1), (20, -1), (21, 1), (22, -1), (23, -1), (24, -1), (25, 1), (26, 1), (27, -1), (28, -1), (29, -1), (30, 1)]),
  561. (19, [(1, 1), (2, -1), (3, -1), (4, 1), (5, 1), (6, 1), (7, 1), (8, -1), (9, 1), (10, -1), (11, 1), (12, -1), (13, -1), (14, -1), (15, -1), (16, 1), (17, 1), (18, -1), (19, 0), (20, 1), (21, -1), (22, -1), (23, 1), (24, 1), (25, 1), (26, 1), (27, -1), (28, 1), (29, -1), (30, 1)]),
  562. (21, [(1, 1), (2, -1), (3, 0), (4, 1), (5, 1), (6, 0), (7, 0), (8, -1), (9, 0), (10, -1), (11, -1), (12, 0), (13, -1), (14, 0), (15, 0), (16, 1), (17, 1), (18, 0), (19, -1), (20, 1), (21, 0), (22, 1), (23, -1), (24, 0), (25, 1), (26, 1), (27, 0), (28, 0), (29, -1), (30, 0)]),
  563. (23, [(1, 1), (2, 1), (3, 1), (4, 1), (5, -1), (6, 1), (7, -1), (8, 1), (9, 1), (10, -1), (11, -1), (12, 1), (13, 1), (14, -1), (15, -1), (16, 1), (17, -1), (18, 1), (19, -1), (20, -1), (21, -1), (22, -1), (23, 0), (24, 1), (25, 1), (26, 1), (27, 1), (28, -1), (29, 1), (30, -1)]),
  564. (25, [(1, 1), (2, 1), (3, 1), (4, 1), (5, 0), (6, 1), (7, 1), (8, 1), (9, 1), (10, 0), (11, 1), (12, 1), (13, 1), (14, 1), (15, 0), (16, 1), (17, 1), (18, 1), (19, 1), (20, 0), (21, 1), (22, 1), (23, 1), (24, 1), (25, 0), (26, 1), (27, 1), (28, 1), (29, 1), (30, 0)]),
  565. (27, [(1, 1), (2, -1), (3, 0), (4, 1), (5, -1), (6, 0), (7, 1), (8, -1), (9, 0), (10, 1), (11, -1), (12, 0), (13, 1), (14, -1), (15, 0), (16, 1), (17, -1), (18, 0), (19, 1), (20, -1), (21, 0), (22, 1), (23, -1), (24, 0), (25, 1), (26, -1), (27, 0), (28, 1), (29, -1), (30, 0)]),
  566. (29, [(1, 1), (2, -1), (3, -1), (4, 1), (5, 1), (6, 1), (7, 1), (8, -1), (9, 1), (10, -1), (11, -1), (12, -1), (13, 1), (14, -1), (15, -1), (16, 1), (17, -1), (18, -1), (19, -1), (20, 1), (21, -1), (22, 1), (23, 1), (24, 1), (25, 1), (26, -1), (27, -1), (28, 1), (29, 0), (30, 1)]),
  567. ]
  568. js = self.Integer.jacobi_symbol
  569. for n, kj in tv:
  570. for k, j in kj:
  571. self.assertEqual(js(k, n), j)
  572. def test_hex(self):
  573. v1, = self.Integers(0x10)
  574. self.assertEqual(hex(v1), "0x10")
  575. class TestIntegerInt(TestIntegerBase):
  576. def setUp(self):
  577. self.Integer = IntegerNative
  578. class testIntegerRandom(unittest.TestCase):
  579. def test_random_exact_bits(self):
  580. for _ in range(1000):
  581. a = IntegerNative.random(exact_bits=8)
  582. self.assertFalse(a < 128)
  583. self.assertFalse(a >= 256)
  584. for bits_value in range(1024, 1024 + 8):
  585. a = IntegerNative.random(exact_bits=bits_value)
  586. self.assertFalse(a < 2**(bits_value - 1))
  587. self.assertFalse(a >= 2**bits_value)
  588. def test_random_max_bits(self):
  589. flag = False
  590. for _ in range(1000):
  591. a = IntegerNative.random(max_bits=8)
  592. flag = flag or a < 128
  593. self.assertFalse(a>=256)
  594. self.assertTrue(flag)
  595. for bits_value in range(1024, 1024 + 8):
  596. a = IntegerNative.random(max_bits=bits_value)
  597. self.assertFalse(a >= 2**bits_value)
  598. def test_random_bits_custom_rng(self):
  599. class CustomRNG(object):
  600. def __init__(self):
  601. self.counter = 0
  602. def __call__(self, size):
  603. self.counter += size
  604. return bchr(0) * size
  605. custom_rng = CustomRNG()
  606. a = IntegerNative.random(exact_bits=32, randfunc=custom_rng)
  607. self.assertEqual(custom_rng.counter, 4)
  608. def test_random_range(self):
  609. func = IntegerNative.random_range
  610. for x in range(200):
  611. a = func(min_inclusive=1, max_inclusive=15)
  612. self.assertTrue(1 <= a <= 15)
  613. for x in range(200):
  614. a = func(min_inclusive=1, max_exclusive=15)
  615. self.assertTrue(1 <= a < 15)
  616. self.assertRaises(ValueError, func, min_inclusive=1, max_inclusive=2,
  617. max_exclusive=3)
  618. self.assertRaises(ValueError, func, max_inclusive=2, max_exclusive=3)
  619. def get_tests(config={}):
  620. tests = []
  621. tests += list_test_cases(TestIntegerInt)
  622. try:
  623. from Cryptodome.Math._IntegerGMP import IntegerGMP
  624. class TestIntegerGMP(TestIntegerBase):
  625. def setUp(self):
  626. self.Integer = IntegerGMP
  627. tests += list_test_cases(TestIntegerGMP)
  628. except (ImportError, OSError) as e:
  629. if sys.platform == "win32":
  630. sys.stdout.write("Skipping GMP tests on Windows\n")
  631. else:
  632. sys.stdout.write("Skipping GMP tests (%s)\n" % str(e) )
  633. try:
  634. from Cryptodome.Math._IntegerCustom import IntegerCustom
  635. class TestIntegerCustomModexp(TestIntegerBase):
  636. def setUp(self):
  637. self.Integer = IntegerCustom
  638. tests += list_test_cases(TestIntegerCustomModexp)
  639. except (ImportError, OSError) as e:
  640. sys.stdout.write("Skipping custom modexp tests (%s)\n" % str(e) )
  641. tests += list_test_cases(testIntegerRandom)
  642. return tests
  643. if __name__ == '__main__':
  644. suite = lambda: unittest.TestSuite(get_tests())
  645. unittest.main(defaultTest='suite')