PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/big_int/tests/example.php

https://github.com/ryantenney/php7
PHP | 205 lines | 126 code | 22 blank | 57 comment | 0 complexity | 4c4d9274359c87d8f93064c97b181883 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, MPL-2.0-no-copyleft-exception, BSD-2-Clause
  1. <?php
  2. /***********************************************************************
  3. Copyright 2004, 2005 Alexander Valyalkin
  4. These sources is free software. You can redistribute it and/or
  5. modify it freely. You can use it with any free or commercial
  6. software.
  7. These sources is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY. Without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. You may contact the author by:
  11. e-mail: valyala@gmail.com
  12. *************************************************************************/
  13. require_once(dirname(__FILE__) . '/std_header.php');
  14. echo '<h1>BIG_INT test and example of usage</h1>' . "\n";
  15. // assign a big number to $a
  16. $a = bi_from_str('12345678901234567890');
  17. // wiew, what returns var_dump($a) (it is NOT a simple integer, but BIG_INT resource)
  18. echo 'var_dump($a) = ' , "<br/>\n";
  19. var_dump($a);
  20. echo "<br/>\n";
  21. // now show decimal value of $a
  22. echo '$a = [', bi_to_str($a), "]<br/>\n";
  23. /*
  24. second argument of bi_from_str() and bi_to_str() - is the base
  25. of string representation of number. It can be from 2 to 36 inclusive
  26. */
  27. $str = 'abcdef012789';
  28. echo 'hex value of $str = [', $str, "]<br/>\n";
  29. $b = bi_from_str($str, 16);
  30. echo 'decimal value of $b = [', bi_to_str($b), "]<br/>\n";
  31. echo 'binary value of $b = [', bi_to_str($b, 2), "]<br/>\n";
  32. /*
  33. string bi_base_convert(string $num, int $base_from, int $base_to)
  34. Converts string representation of $num from base $base_from to $base_to
  35. */
  36. $num = '-12345678900987654321';
  37. echo 'Decimal number [', $num, '] equals to [', bi_base_convert($num, 10, 36), '] by base 36', "<br/>\n";
  38. /*
  39. basic functions
  40. */
  41. echo '<h3>basic functions</h3>' . "\n";
  42. $c = bi_add($a, $b);
  43. echo '$a + $b = [', bi_to_str($c), "]<br/>\n";
  44. $c = bi_sub($a, $b);
  45. echo '$a - $b = [', bi_to_str($c), "]<br/>\n";
  46. $c = bi_mul($a, $b);
  47. echo '$a * $b = [', bi_to_str($c), "]<br/>\n";
  48. $c = bi_div($a, $b);
  49. echo '$a / $b = [', bi_to_str($c), "]<br/>\n";
  50. $c = bi_mod($a, $b);
  51. echo '$a % $b = [', bi_to_str($c), "]<br/>\n";
  52. $c = bi_abs($a);
  53. echo 'abs($a) = [', bi_to_str($c), "]<br/>\n";
  54. $c = bi_neg($a);
  55. echo 'neg($a) = [', bi_to_str($c), "]<br/>\n";
  56. $c = bi_inc($a);
  57. echo 'inc($a) = [', bi_to_str($c), "]<br/>\n";
  58. $c = bi_dec($a);
  59. echo 'dec($a) = [', bi_to_str($c), "]<br/>\n";
  60. $c = bi_sqr($a);
  61. echo 'sqr($a) = [', bi_to_str($c), "]<br/>\n";
  62. echo 'cmp($a, $b) = ', bi_cmp($a, $b), "<br/>\n";
  63. echo 'cmp_abs($a, $b) = ', bi_cmp_abs($a, $b), "<br/>\n";
  64. echo 'is_zero($a) = ', bi_is_zero($a) ? 'true' : 'false', "<br/>\n";
  65. echo 'is_one($a) = ', bi_is_one($a) ? 'true' : 'false', "<br/>\n";
  66. echo 'sign($a) = ', bi_sign($a), "<br/>\n";
  67. /*
  68. bitset functions
  69. */
  70. echo '<h3>bitset functions</h3>' . "\n";
  71. echo '<div style="text-align:right">';
  72. echo '$a = [', bi_to_str($a, 2), "]<br/>\n";
  73. echo '$b = [', bi_to_str($b, 2), "]<br/>\n";
  74. $c = bi_or($a, $b);
  75. echo '$a or $b = [', bi_to_str($c, 2), "]<br/>\n";
  76. $c = bi_xor($a, $b);
  77. echo '$a xor $b = [', bi_to_str($c, 2), "]<br/>\n";
  78. $c = bi_and($a, $b);
  79. echo '$a and $b = [', bi_to_str($c, 2), "]<br/>\n";
  80. $c = bi_andnot($a, $b);
  81. echo '$a andnot $b = [', bi_to_str($c, 2), "]<br/>\n";
  82. $c = bi_set_bit($a, 0);
  83. echo 'set_bit($a, 0) = [', bi_to_str($c, 2), "]<br/>\n";
  84. $c = bi_clr_bit($a, 0);
  85. echo 'clr_bit($a, 0) = [', bi_to_str($c, 2), "]<br/>\n";
  86. $c = bi_inv_bit($a, 0);
  87. echo 'inv_bit($a, 0) = [', bi_to_str($c, 2), "]<br/>\n";
  88. $c = bi_subint($a, 10, 20);
  89. echo 'subint($a, 10, 20) = [', bi_to_str($c, 2), "]<br/>\n";
  90. $c = bi_rshift($a, 10);
  91. echo '$a >> 10 = [', bi_to_str($c, 2), "]<br/>\n";
  92. $c = bi_lshift($a, 10);
  93. echo '$a << 10 = [', bi_to_str($c, 2), "]<br/>\n";
  94. echo '</div>', "<br/>\n";
  95. echo 'Hamming distance($a, $b) = ', bi_hamming_distance($a, $b), "<br/>\n";
  96. echo 'bit_len($a) = ', bi_bit_len($a), "<br/>\n";
  97. echo 'bit1_cnt($a) = ', bi_bit1_cnt($a), "<br/>\n";
  98. echo 'test_bit($a, 0) = ', bi_test_bit($a, 0), "<br/>\n";
  99. echo 'scan0_bit($a, 0) = ', bi_scan0_bit($a, 0), "<br/>\n";
  100. echo 'scan1_bit($a, 0) = ', bi_scan1_bit($a, 0), "<br/>\n";
  101. echo '<h3>pseudorandom functions</h3>' . "\n";
  102. /*
  103. resource bi_rand(int $bit_len[, string $rand_func_name])
  104. Returns pseudorandom number with $bit_len bit length.
  105. Attention: do not use this function in cryptographic
  106. or security applications! The function can be used only
  107. for educational purposes :)
  108. */
  109. $c = bi_rand(100);
  110. echo 'rand(100) = ', bi_to_str($c), "<br/>\n";
  111. $c = bi_rand(100, 'mt_rand'); // use mt_rand() as random generator
  112. echo 'rand(100, "mt_rand") = ', bi_to_str($c), "<br/>\n";
  113. /*
  114. functions for modular arithmetic calculations
  115. */
  116. echo '<h3>modular arithmetic functions</h3>' . "\n";
  117. // find next pseudoprime number after $c
  118. $modulus = bi_next_prime($c);
  119. echo '$modulus = next_prime($c) = [', bi_to_str($modulus), "]<br/>\n";
  120. $c = bi_addmod($a, $b, $modulus);
  121. echo '$a + $b (mod $modulus) = [', bi_to_str($c), "]<br/>\n";
  122. $c = bi_submod($a, $b, $modulus);
  123. echo '$a - $b (mod $modulus) = [', bi_to_str($c), "]<br/>\n";
  124. $c = bi_mulmod($a, $b, $modulus);
  125. echo '$a * $b (mod $modulus) = [', bi_to_str($c), "]<br/>\n";
  126. $c = bi_divmod($a, $b, $modulus);
  127. echo '$a / $b (mod $modulus) = [', bi_to_str($c), "]<br/>\n";
  128. $c = bi_powmod($a, $b, $modulus);
  129. echo 'pow($a, $b) (mod $modulus) = [', bi_to_str($c), "]<br/>\n";
  130. $c = bi_factmod(1000, $modulus);
  131. echo '1000! (mod $modulus) = [', bi_to_str($c), "]<br/>\n";
  132. $c = bi_absmod(-1, $modulus);
  133. echo '-1 (mod $modulus) = [', bi_to_str($c), "]<br/>\n";
  134. $c = bi_invmod($a, $modulus);
  135. echo '1 / $a (mod $modulus) = [', bi_to_str($c), "]<br/>\n";
  136. $c = bi_sqrmod($a, $modulus);
  137. echo 'sqr($a) (mod $modulus) = [', bi_to_str($c), "]<br/>\n";
  138. echo 'cmp($a, $b) (mod $modulus) = ', bi_cmpmod($a, $b, $modulus), "<br/>\n";
  139. /*
  140. other functions
  141. */
  142. echo '<h3>other functions</h3>' . "\n";
  143. /*
  144. attention: second parameter of bi_pow() must have
  145. integer type (not BIG_INT!)
  146. */
  147. $c = bi_pow($a, 4);
  148. echo 'pow($a, 4) = [', bi_to_str($c), "]<br/>\n";
  149. // argument of bi_fact() must have integer type (not BIG_INT)
  150. $c = bi_fact(100);
  151. echo '100! = [', bi_to_str($c), "]<br/>\n";
  152. $c = bi_sqrt($a);
  153. echo 'sqrt($a) = [', bi_to_str($c), "]<br/>\n";
  154. $c = bi_sqrt_rem($a);
  155. echo 'sqrt_rem($a) = [', bi_to_str($c), "]<br/>\n";
  156. $c = bi_gcd($a, $b);
  157. echo 'GCD($a, $b) = [', bi_to_str($c), "]<br/>\n";
  158. // Miller-Rabin primality test of $a
  159. echo 'miller_test($a, $b) = ', bi_miller_test($a, $b), "<br/>\n";
  160. // primality test of $a
  161. echo 'is_prime($a) = ', bi_is_prime($a), "<br/>\n";
  162. // Jacobi symbol ($a|$b)
  163. echo 'jacobi($a, $b) = ', bi_jacobi($a, $b), "<br/>\n";
  164. $c = bi_div_extended($a, $b);
  165. echo '$a = $b * ', bi_to_str($c[0]), ' + ', bi_to_str($c[1]), "<br/>\n";
  166. $c = bi_gcd_extended($a, $b);
  167. echo 'abs($a) * ', bi_to_str($c[1]), ' + abs($b) * ', bi_to_str($c[2]), ' = ', bi_to_str($c[0]), "<br/>\n";
  168. // serialize $a into sequence of bytes
  169. $str = bi_serialize($a);
  170. echo '$str = serialize($a) = [', base64_encode($str), "]<br/>\n";
  171. // unserialize $a from sequence of bytes
  172. $a = bi_unserialize($str);
  173. echo 'unserialize($str) = [', bi_to_str($a), "]<br/>\n";
  174. // show package info
  175. var_dump(bi_info());
  176. echo 'the list of all functions in BIG_INT module:', "<br/>\n";
  177. echo implode(", ", get_extension_funcs('big_int'));
  178. echo "<br/>\n";
  179. ?>