Polynomial.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. for($i = 0; $i < $numCount - $offset; $i++){
  52. $this->num[$i] = $num[$i + $offset];
  53. }
  54. return $this;
  55. }
  56. /**
  57. *
  58. */
  59. protected function setTables(){
  60. $this->EXP_TABLE = $this->LOG_TABLE = array_fill(0, 256, 0);
  61. for($i = 0; $i < 8; $i++){
  62. $this->EXP_TABLE[$i] = 1 << $i;
  63. }
  64. for($i = 8; $i < 256; $i++){
  65. $this->EXP_TABLE[$i] = $this->EXP_TABLE[$i - 4] ^ $this->EXP_TABLE[$i - 5] ^ $this->EXP_TABLE[$i - 6] ^ $this->EXP_TABLE[$i - 8];
  66. }
  67. for($i = 0; $i < 255; $i++){
  68. $this->LOG_TABLE[$this->EXP_TABLE[$i]] = $i;
  69. }
  70. }
  71. /**
  72. * @param array $e
  73. */
  74. public function multiply(array $e){
  75. $n = array_fill(0, count($this->num) + count($e) - 1, 0);
  76. foreach($this->num as $i => &$vi){
  77. foreach($e as $j => &$vj){
  78. $n[$i + $j] ^= $this->gexp($this->glog($vi) + $this->glog($vj));
  79. }
  80. }
  81. $this->setNum($n);
  82. }
  83. /**
  84. * @param array $e
  85. */
  86. public function mod(array $e){
  87. $n = $this->num;
  88. if(count($n) - count($e) < 0){
  89. return;
  90. }
  91. $ratio = $this->glog($n[0]) - $this->glog($e[0]);
  92. foreach($e as $i => &$v){
  93. $n[$i] ^= $this->gexp($this->glog($v) + $ratio);
  94. }
  95. $this->setNum($n)->mod($e);
  96. }
  97. /**
  98. * @param int $n
  99. *
  100. * @return int
  101. * @throws \chillerlan\QRCode\QRCodeException
  102. */
  103. public function glog($n){
  104. if($n < 1){
  105. throw new QRCodeException('log('.$n.')');
  106. }
  107. return $this->LOG_TABLE[$n];
  108. }
  109. /**
  110. * @param int $n
  111. *
  112. * @return int
  113. */
  114. public function gexp($n){
  115. switch(true){
  116. case $n < 0 : $n += 255; break;
  117. case $n >= 256: $n -= 255; break;
  118. }
  119. return $this->EXP_TABLE[$n];
  120. }
  121. }