PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/phpseclib/phpseclib/tests/Unit/Math/BigInteger/TestCase.php

https://gitlab.com/xolotsoft/pumasruiz
PHP | 361 lines | 266 code | 71 blank | 24 comment | 3 complexity | ab5a6605952c9c3216d14309ee938740 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@phpbb.com>
  4. * @copyright 2012 Andreas Fischer
  5. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  6. */
  7. require_once 'Math/BigInteger.php';
  8. abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase
  9. {
  10. static public function setUpBeforeClass()
  11. {
  12. parent::setUpBeforeClass();
  13. self::reRequireFile('Math/BigInteger.php');
  14. }
  15. public function getInstance($x = 0, $base = 10)
  16. {
  17. return new Math_BigInteger($x, $base);
  18. }
  19. public function testConstructorBase2()
  20. {
  21. // 2**65 = 36893488147419103232
  22. $this->assertSame('36893488147419103232', (string) $this->getInstance('1' . str_repeat('0', 65), 2));
  23. }
  24. public function testConstructorBase10()
  25. {
  26. $this->assertSame('18446744073709551616', (string) $this->getInstance('18446744073709551616'));
  27. }
  28. public function testConstructorBase16()
  29. {
  30. $this->assertSame('50', (string) $this->getInstance('0x32', 16));
  31. $this->assertSame('12345678910', (string) $this->getInstance('0x2DFDC1C3E', 16));
  32. $this->assertSame('18446744073709551615', (string) $this->getInstance('0xFFFFFFFFFFFFFFFF', 16));
  33. $this->assertSame('18446744073709551616', (string) $this->getInstance('0x10000000000000000', 16));
  34. }
  35. public function testToBytes()
  36. {
  37. $this->assertSame(chr(65), $this->getInstance('65')->toBytes());
  38. }
  39. public function testToBytesTwosCompliment()
  40. {
  41. $this->assertSame(chr(126), $this->getInstance('01111110', 2)->toBytes(true));
  42. }
  43. public function testToHex()
  44. {
  45. $this->assertSame('41', $this->getInstance('65')->toHex());
  46. }
  47. public function testToBits()
  48. {
  49. $this->assertSame('1000001', $this->getInstance('65')->toBits());
  50. }
  51. public function testAdd()
  52. {
  53. $x = $this->getInstance('18446744073709551615');
  54. $y = $this->getInstance( '100000000000');
  55. $a = $x->add($y);
  56. $b = $y->add($x);
  57. $this->assertTrue($a->equals($b));
  58. $this->assertTrue($b->equals($a));
  59. $this->assertSame('18446744173709551615', (string) $a);
  60. $this->assertSame('18446744173709551615', (string) $b);
  61. }
  62. public function testSubtract()
  63. {
  64. $x = $this->getInstance('18446744073709551618');
  65. $y = $this->getInstance( '4000000000000');
  66. $this->assertSame('18446740073709551618', (string) $x->subtract($y));
  67. }
  68. public function testMultiply()
  69. {
  70. $x = $this->getInstance('8589934592'); // 2**33
  71. $y = $this->getInstance('36893488147419103232'); // 2**65
  72. $a = $x->multiply($y); // 2**98
  73. $b = $y->multiply($x); // 2**98
  74. $this->assertTrue($a->equals($b));
  75. $this->assertTrue($b->equals($a));
  76. $this->assertSame('316912650057057350374175801344', (string) $a);
  77. $this->assertSame('316912650057057350374175801344', (string) $b);
  78. }
  79. public function testDivide()
  80. {
  81. $x = $this->getInstance('1180591620717411303425'); // 2**70 + 1
  82. $y = $this->getInstance('12345678910');
  83. list($q, $r) = $x->divide($y);
  84. $this->assertSame('95627922070', (string) $q);
  85. $this->assertSame('10688759725', (string) $r);
  86. }
  87. public function testModPow()
  88. {
  89. $a = $this->getInstance('10');
  90. $b = $this->getInstance('20');
  91. $c = $this->getInstance('30');
  92. $d = $a->modPow($b, $c);
  93. $this->assertSame('10', (string) $d);
  94. }
  95. public function testModInverse()
  96. {
  97. $a = $this->getInstance(30);
  98. $b = $this->getInstance(17);
  99. $c = $a->modInverse($b);
  100. $this->assertSame('4', (string) $c);
  101. $d = $a->multiply($c);
  102. list($q, $r) = $d->divide($b);
  103. $this->assertSame('1', (string) $r);
  104. }
  105. public function testExtendedGCD()
  106. {
  107. $a = $this->getInstance(693);
  108. $b = $this->getInstance(609);
  109. $arr = $a->extendedGCD($b);
  110. $this->assertSame('21', (string) $arr['gcd']);
  111. $this->assertSame(21, $a->toString() * $arr['x']->toString() + $b->toString() * $arr['y']->toString());
  112. }
  113. public function testGCD()
  114. {
  115. $x = $this->getInstance(693);
  116. $y = $this->getInstance(609);
  117. $this->assertSame('21', (string) $x->gcd($y));
  118. }
  119. public function testAbs()
  120. {
  121. $x = $this->getInstance('-18446744073709551617');
  122. $y = $x->abs();
  123. $this->assertSame('-18446744073709551617', (string) $x);
  124. $this->assertSame('18446744073709551617', (string) $y);
  125. }
  126. public function testEquals()
  127. {
  128. $x = $this->getInstance('18446744073709551616');
  129. $y = $this->getInstance('18446744073709551616');
  130. $this->assertTrue($x->equals($y));
  131. $this->assertTrue($y->equals($x));
  132. }
  133. public function testCompare()
  134. {
  135. $a = $this->getInstance('-18446744073709551616');
  136. $b = $this->getInstance('36893488147419103232');
  137. $c = $this->getInstance('36893488147419103232');
  138. $d = $this->getInstance('316912650057057350374175801344');
  139. // a < b
  140. $this->assertLessThan(0, $a->compare($b));
  141. $this->assertGreaterThan(0, $b->compare($a));
  142. // b = c
  143. $this->assertSame(0, $b->compare($c));
  144. $this->assertSame(0, $c->compare($b));
  145. // c < d
  146. $this->assertLessThan(0, $c->compare($d));
  147. $this->assertGreaterThan(0, $d->compare($c));
  148. }
  149. public function testBitwiseAND()
  150. {
  151. $x = $this->getInstance('66666666666666666666666', 16);
  152. $y = $this->getInstance('33333333333333333333333', 16);
  153. $z = $this->getInstance('22222222222222222222222', 16);
  154. $this->assertSame($z->toHex(), $x->bitwise_AND($y)->toHex());
  155. }
  156. public function testBitwiseOR()
  157. {
  158. $x = $this->getInstance('11111111111111111111111', 16);
  159. $y = $this->getInstance('EEEEEEEEEEEEEEEEEEEEEEE', 16);
  160. $z = $this->getInstance('FFFFFFFFFFFFFFFFFFFFFFF', 16);
  161. $this->assertSame($z->toHex(), $x->bitwise_OR($y)->toHex());
  162. }
  163. public function testBitwiseXOR()
  164. {
  165. $x = $this->getInstance('AFAFAFAFAFAFAFAFAFAFAFAF', 16);
  166. $y = $this->getInstance('133713371337133713371337', 16);
  167. $z = $this->getInstance('BC98BC98BC98BC98BC98BC98', 16);
  168. $this->assertSame($z->toHex(), $x->bitwise_XOR($y)->toHex());
  169. }
  170. public function testBitwiseNOT()
  171. {
  172. $x = $this->getInstance('EEEEEEEEEEEEEEEEEEEEEEE', 16);
  173. $z = $this->getInstance('11111111111111111111111', 16);
  174. $this->assertSame($z->toHex(), $x->bitwise_NOT()->toHex());
  175. }
  176. public function testBitwiseLeftShift()
  177. {
  178. $x = $this->getInstance('0x0000000FF0000000', 16);
  179. $y = $this->getInstance('0x000FF00000000000', 16);
  180. $this->assertSame($y->toHex(), $x->bitwise_LeftShift(16)->toHex());
  181. }
  182. public function testBitwiseRightShift()
  183. {
  184. $x = $this->getInstance('0x0000000FF0000000', 16);
  185. $y = $this->getInstance('0x00000000000FF000', 16);
  186. $z = $this->getInstance('0x000000000000000F', 16);
  187. $n = $this->getInstance(0);
  188. $this->assertSame($y->toHex(), $x->bitwise_RightShift(16)->toHex());
  189. $this->assertSame($z->toHex(), $x->bitwise_RightShift(32)->toHex());
  190. $this->assertSame($n->toHex(), $x->bitwise_RightShift(36)->toHex());
  191. }
  192. public function testSerializable()
  193. {
  194. $x = $this->getInstance('18446744073709551616');
  195. $y = unserialize(serialize($x));
  196. $this->assertTrue($x->equals($y));
  197. $this->assertTrue($y->equals($x));
  198. $this->assertSame('18446744073709551616', (string) $x);
  199. $this->assertSame('18446744073709551616', (string) $y);
  200. }
  201. public function testClone()
  202. {
  203. $x = $this->getInstance('18446744073709551616');
  204. $y = clone $x;
  205. $this->assertTrue($x->equals($y));
  206. $this->assertTrue($y->equals($x));
  207. $this->assertSame('18446744073709551616', (string) $x);
  208. $this->assertSame('18446744073709551616', (string) $y);
  209. }
  210. public function testRandomTwoArgument()
  211. {
  212. $min = $this->getInstance(0);
  213. $max = $this->getInstance('18446744073709551616');
  214. $rand1 = $min->random($min, $max);
  215. // technically $rand1 can equal $min but with the $min and $max we've
  216. // chosen it's just not that likely
  217. $this->assertTrue($rand1->compare($min) > 0);
  218. $this->assertTrue($rand1->compare($max) < 0);
  219. }
  220. public function testRandomOneArgument()
  221. {
  222. $min = $this->getInstance(0);
  223. $max = $this->getInstance('18446744073709551616');
  224. $rand1 = $min->random($max);
  225. $this->assertTrue($rand1->compare($min) > 0);
  226. $this->assertTrue($rand1->compare($max) < 0);
  227. $rand2 = $max->random($min);
  228. $this->assertTrue($rand2->compare($min) > 0);
  229. $this->assertTrue($rand2->compare($max) < 0);
  230. $this->assertFalse($rand1->equals($rand2));
  231. }
  232. /**
  233. * @group github279
  234. */
  235. public function testDiffieHellmanKeyAgreement()
  236. {
  237. if (getenv('TRAVIS') && PHP_VERSION === '5.3.3'
  238. && MATH_BIGINTEGER_MODE === MATH_BIGINTEGER_MODE_INTERNAL
  239. ) {
  240. $this->markTestIncomplete(
  241. 'This test hangs on PHP 5.3.3 using internal mode.'
  242. );
  243. }
  244. // "Oakley Group 14" 2048-bit modular exponentiation group as used in
  245. // SSH2 diffie-hellman-group14-sha1
  246. $prime = $this->getInstance(
  247. 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' .
  248. '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' .
  249. 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' .
  250. 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' .
  251. 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' .
  252. 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' .
  253. '83655D23DCA3AD961C62F356208552BB9ED529077096966D' .
  254. '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' .
  255. 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' .
  256. 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' .
  257. '15728E5A8AACAA68FFFFFFFFFFFFFFFF',
  258. 16
  259. );
  260. $generator = $this->getInstance(2);
  261. /*
  262. Code for generation of $alicePrivate and $bobPrivate.
  263. $one = $this->getInstance(1);
  264. $max = $one->bitwise_leftShift(512)->subtract($one);
  265. $alicePrivate = $one->random($one, $max);
  266. $bobPrivate = $one->random($one, $max);
  267. var_dump($alicePrivate->toHex(), $bobPrivate->toHex());
  268. */
  269. $alicePrivate = $this->getInstance(
  270. '22606EDA7960458BC9D65F46DD96F114F9A004F0493C1F26' .
  271. '2139D2C8063B733162E876182CA3BF063AB1A167ABDB7F03' .
  272. 'E0A225A6205660439F6CE46D252069FF',
  273. 16
  274. );
  275. $bobPrivate = $this->getInstance(
  276. '6E3EFA13A96025D63E4B0D88A09B3A46DDFE9DD3BC9D1655' .
  277. '4898C02B4AC181F0CEB4E818664B12F02C71A07215C400F9' .
  278. '88352A4779F3E88836F7C3D3B3C739DE',
  279. 16
  280. );
  281. $alicePublic = $generator->modPow($alicePrivate, $prime);
  282. $bobPublic = $generator->modPow($bobPrivate, $prime);
  283. $aliceShared = $bobPublic->modPow($alicePrivate, $prime);
  284. $bobShared = $alicePublic->modPow($bobPrivate, $prime);
  285. $this->assertTrue(
  286. $aliceShared->equals($bobShared),
  287. 'Failed asserting that Alice and Bob share the same BigInteger.'
  288. );
  289. }
  290. }