Polynomial.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Class Polynomial
  4. *
  5. * @filesource Polynomial.php
  6. * @created 25.11.2015
  7. * @package chillerlan\QRCode
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2015 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCode;
  13. /**
  14. *
  15. */
  16. class Polynomial{
  17. /**
  18. * @var array
  19. */
  20. public $num = [];
  21. /**
  22. * @var array
  23. */
  24. protected $EXP_TABLE = [];
  25. /**
  26. * @var array
  27. */
  28. protected $LOG_TABLE = [];
  29. /**
  30. * Polynomial constructor.
  31. *
  32. * @param array $num
  33. * @param int $shift
  34. */
  35. public function __construct(array $num = [1], $shift = 0){
  36. $this->setNum($num, $shift)->setTables();
  37. }
  38. /**
  39. * @param array $num
  40. * @param int $shift
  41. *
  42. * @return $this
  43. */
  44. public function setNum(array $num, $shift = 0){
  45. $offset = 0;
  46. $numCount = count($num);
  47. while($offset < $numCount && $num[$offset] === 0){
  48. $offset++;
  49. }
  50. $this->num = array_fill(0, $numCount - $offset + $shift, 0);
  51. $i = 0;
  52. while($i < $numCount - $offset){
  53. $this->num[$i] = $num[$i + $offset];
  54. $i++;
  55. }
  56. return $this;
  57. }
  58. /**
  59. *
  60. */
  61. protected function setTables(){
  62. $this->EXP_TABLE = $this->LOG_TABLE = array_fill(0, 256, 0);
  63. $i = 0;
  64. while($i < 8){
  65. $this->EXP_TABLE[$i] = 1 << $i;
  66. $i++;
  67. }
  68. $i = 8;
  69. while($i < 256){
  70. $this->EXP_TABLE[$i] = $this->EXP_TABLE[$i - 4] ^ $this->EXP_TABLE[$i - 5] ^ $this->EXP_TABLE[$i - 6] ^ $this->EXP_TABLE[$i - 8];
  71. $i++;
  72. }
  73. $i = 0;
  74. while($i < 255){
  75. $this->LOG_TABLE[$this->EXP_TABLE[$i]] = $i;
  76. $i++;
  77. }
  78. }
  79. /**
  80. * @param array $e
  81. */
  82. public function multiply(array $e){
  83. $n = array_fill(0, count($this->num) + count($e) - 1, 0);
  84. foreach($this->num as $i => &$vi){
  85. foreach($e as $j => &$vj){
  86. $n[$i + $j] ^= $this->gexp($this->glog($vi) + $this->glog($vj));
  87. }
  88. }
  89. $this->setNum($n);
  90. }
  91. /**
  92. * @param array $e
  93. */
  94. public function mod(array $e){
  95. $n = $this->num;
  96. if(count($n) - count($e) < 0){
  97. return;
  98. }
  99. $ratio = $this->glog($n[0]) - $this->glog($e[0]);
  100. foreach($e as $i => &$v){
  101. $n[$i] ^= $this->gexp($this->glog($v) + $ratio);
  102. }
  103. $this->setNum($n)->mod($e);
  104. }
  105. /**
  106. * @param int $n
  107. *
  108. * @return int
  109. * @throws \chillerlan\QRCode\QRCodeException
  110. */
  111. public function glog($n){
  112. if($n < 1){
  113. throw new QRCodeException('log('.$n.')');
  114. }
  115. return $this->LOG_TABLE[$n];
  116. }
  117. /**
  118. * @param int $n
  119. *
  120. * @return int
  121. */
  122. public function gexp($n){
  123. if($n < 0){
  124. $n += 255;
  125. }
  126. elseif($n >= 256){
  127. $n -= 255;
  128. }
  129. return $this->EXP_TABLE[$n];
  130. }
  131. }