/tests/Math/BigIntegerTest.php

https://github.com/kea/phpseclib · PHP · 259 lines · 195 code · 55 blank · 9 comment · 0 complexity · 10517a16257d756120ffaa4361e483c6 MD5 · raw file

  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@phpbb.com>
  4. * @copyright MMXII Andreas Fischer
  5. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  6. */
  7. class Math_BigIntegerTest extends PHPUnit_Framework_TestCase
  8. {
  9. public function getInstance($x = 0, $base = 10)
  10. {
  11. return new phpseclib\Math_BigInteger($x, $base);
  12. }
  13. public function testConstructorBase2()
  14. {
  15. // 2**65 = 36893488147419103232
  16. $this->assertSame('36893488147419103232', (string) $this->getInstance('1' . str_repeat('0', 65), 2));
  17. }
  18. public function testConstructorBase10()
  19. {
  20. $this->assertSame('18446744073709551616', (string) $this->getInstance('18446744073709551616'));
  21. }
  22. public function testConstructorBase16()
  23. {
  24. $this->assertSame('50', (string) $this->getInstance('0x32', 16));
  25. $this->assertSame('12345678910', (string) $this->getInstance('0x2DFDC1C3E', 16));
  26. $this->assertSame('18446744073709551615', (string) $this->getInstance('0xFFFFFFFFFFFFFFFF', 16));
  27. $this->assertSame('18446744073709551616', (string) $this->getInstance('0x10000000000000000', 16));
  28. }
  29. public function testToBytes()
  30. {
  31. $this->assertSame(chr(65), $this->getInstance('65')->toBytes());
  32. }
  33. public function testToBytesTwosCompliment()
  34. {
  35. $this->assertSame(chr(126), $this->getInstance('01111110', 2)->toBytes(true));
  36. }
  37. public function testToHex()
  38. {
  39. $this->assertSame('41', $this->getInstance('65')->toHex());
  40. }
  41. public function testToBits()
  42. {
  43. $this->assertSame('1000001', $this->getInstance('65')->toBits());
  44. }
  45. public function testAdd()
  46. {
  47. $x = $this->getInstance('18446744073709551615');
  48. $y = $this->getInstance( '100000000000');
  49. $a = $x->add($y);
  50. $b = $y->add($x);
  51. $this->assertTrue($a->equals($b));
  52. $this->assertTrue($b->equals($a));
  53. $this->assertSame('18446744173709551615', (string) $a);
  54. $this->assertSame('18446744173709551615', (string) $b);
  55. }
  56. public function testSubtract()
  57. {
  58. $x = $this->getInstance('18446744073709551618');
  59. $y = $this->getInstance( '4000000000000');
  60. $this->assertSame('18446740073709551618', (string) $x->subtract($y));
  61. }
  62. public function testMultiply()
  63. {
  64. $x = $this->getInstance('8589934592'); // 2**33
  65. $y = $this->getInstance('36893488147419103232'); // 2**65
  66. $a = $x->multiply($y); // 2**98
  67. $b = $y->multiply($x); // 2**98
  68. $this->assertTrue($a->equals($b));
  69. $this->assertTrue($b->equals($a));
  70. $this->assertSame('316912650057057350374175801344', (string) $a);
  71. $this->assertSame('316912650057057350374175801344', (string) $b);
  72. }
  73. public function testDivide()
  74. {
  75. $x = $this->getInstance('1180591620717411303425'); // 2**70 + 1
  76. $y = $this->getInstance('12345678910');
  77. list($q, $r) = $x->divide($y);
  78. $this->assertSame('95627922070', (string) $q);
  79. $this->assertSame('10688759725', (string) $r);
  80. }
  81. public function testModPow()
  82. {
  83. $a = $this->getInstance('10');
  84. $b = $this->getInstance('20');
  85. $c = $this->getInstance('30');
  86. $d = $a->modPow($b, $c);
  87. $this->assertSame('10', (string) $d);
  88. }
  89. public function testModInverse()
  90. {
  91. $a = $this->getInstance(30);
  92. $b = $this->getInstance(17);
  93. $c = $a->modInverse($b);
  94. $this->assertSame('4', (string) $c);
  95. $d = $a->multiply($c);
  96. list($q, $r) = $d->divide($b);
  97. $this->assertSame('1', (string) $r);
  98. }
  99. public function testExtendedGCD()
  100. {
  101. $a = $this->getInstance(693);
  102. $b = $this->getInstance(609);
  103. $arr = $a->extendedGCD($b);
  104. $this->assertSame('21', (string) $arr['gcd']);
  105. $this->assertSame(21, $a->toString() * $arr['x']->toString() + $b->toString() * $arr['y']->toString());
  106. }
  107. public function testGCD()
  108. {
  109. $x = $this->getInstance(693);
  110. $y = $this->getInstance(609);
  111. $this->assertSame('21', (string) $x->gcd($y));
  112. }
  113. public function testAbs()
  114. {
  115. $x = $this->getInstance('-18446744073709551617');
  116. $y = $x->abs();
  117. $this->assertSame('-18446744073709551617', (string) $x);
  118. $this->assertSame('18446744073709551617', (string) $y);
  119. }
  120. public function testEquals()
  121. {
  122. $x = $this->getInstance('18446744073709551616');
  123. $y = $this->getInstance('18446744073709551616');
  124. $this->assertTrue($x->equals($y));
  125. $this->assertTrue($y->equals($x));
  126. }
  127. public function testCompare()
  128. {
  129. $a = $this->getInstance('-18446744073709551616');
  130. $b = $this->getInstance('36893488147419103232');
  131. $c = $this->getInstance('36893488147419103232');
  132. $d = $this->getInstance('316912650057057350374175801344');
  133. // a < b
  134. $this->assertLessThan(0, $a->compare($b));
  135. $this->assertGreaterThan(0, $b->compare($a));
  136. // b = c
  137. $this->assertSame(0, $b->compare($c));
  138. $this->assertSame(0, $c->compare($b));
  139. // c < d
  140. $this->assertLessThan(0, $c->compare($d));
  141. $this->assertGreaterThan(0, $d->compare($c));
  142. }
  143. public function testBitwiseAND()
  144. {
  145. $x = $this->getInstance('66666666666666666666666', 16);
  146. $y = $this->getInstance('33333333333333333333333', 16);
  147. $z = $this->getInstance('22222222222222222222222', 16);
  148. $this->assertSame($z->toHex(), $x->bitwise_AND($y)->toHex());
  149. }
  150. public function testBitwiseOR()
  151. {
  152. $x = $this->getInstance('11111111111111111111111', 16);
  153. $y = $this->getInstance('EEEEEEEEEEEEEEEEEEEEEEE', 16);
  154. $z = $this->getInstance('FFFFFFFFFFFFFFFFFFFFFFF', 16);
  155. $this->assertSame($z->toHex(), $x->bitwise_OR($y)->toHex());
  156. }
  157. public function testBitwiseXOR()
  158. {
  159. $x = $this->getInstance('AFAFAFAFAFAFAFAFAFAFAFAF', 16);
  160. $y = $this->getInstance('133713371337133713371337', 16);
  161. $z = $this->getInstance('BC98BC98BC98BC98BC98BC98', 16);
  162. $this->assertSame($z->toHex(), $x->bitwise_XOR($y)->toHex());
  163. }
  164. public function testBitwiseNOT()
  165. {
  166. $x = $this->getInstance('EEEEEEEEEEEEEEEEEEEEEEE', 16);
  167. $z = $this->getInstance('11111111111111111111111', 16);
  168. $this->assertSame($z->toHex(), $x->bitwise_NOT()->toHex());
  169. }
  170. public function testBitwiseLeftShift()
  171. {
  172. $x = $this->getInstance('0x0000000FF0000000', 16);
  173. $y = $this->getInstance('0x000FF00000000000', 16);
  174. $this->assertSame($y->toHex(), $x->bitwise_LeftShift(16)->toHex());
  175. }
  176. public function testBitwiseRightShift()
  177. {
  178. $x = $this->getInstance('0x0000000FF0000000', 16);
  179. $y = $this->getInstance('0x00000000000FF000', 16);
  180. $z = $this->getInstance('0x000000000000000F', 16);
  181. $n = $this->getInstance(0);
  182. $this->assertSame($y->toHex(), $x->bitwise_RightShift(16)->toHex());
  183. $this->assertSame($z->toHex(), $x->bitwise_RightShift(32)->toHex());
  184. $this->assertSame($n->toHex(), $x->bitwise_RightShift(36)->toHex());
  185. }
  186. public function testSerializable()
  187. {
  188. $x = $this->getInstance('18446744073709551616');
  189. $y = unserialize(serialize($x));
  190. $this->assertTrue($x->equals($y));
  191. $this->assertTrue($y->equals($x));
  192. $this->assertSame('18446744073709551616', (string) $x);
  193. $this->assertSame('18446744073709551616', (string) $y);
  194. }
  195. public function testClone()
  196. {
  197. $x = $this->getInstance('18446744073709551616');
  198. $y = clone $x;
  199. $this->assertTrue($x->equals($y));
  200. $this->assertTrue($y->equals($x));
  201. $this->assertSame('18446744073709551616', (string) $x);
  202. $this->assertSame('18446744073709551616', (string) $y);
  203. }
  204. }