QRPolynomial.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import QRMath from "./QRMath";
  2. export default class QRPolynomial {
  3. constructor(num, shift) {
  4. if (num.length == undefined) {
  5. throw new Error(num.length + "/" + shift);
  6. }
  7. var offset = 0;
  8. while (offset < num.length && num[offset] == 0) {
  9. offset++;
  10. }
  11. this.num = new Array(num.length - offset + shift);
  12. for (var i = 0; i < num.length - offset; i++) {
  13. this.num[i] = num[i + offset];
  14. }
  15. }
  16. get(index) {
  17. return this.num[index];
  18. }
  19. getLength() {
  20. return this.num.length;
  21. }
  22. multiply(e) {
  23. var num = new Array(this.getLength() + e.getLength() - 1);
  24. for (var i = 0; i < this.getLength(); i++) {
  25. for (var j = 0; j < e.getLength(); j++) {
  26. num[i + j] ^= QRMath.gexp(QRMath.glog(this.get(i)) + QRMath.glog(e.get(j)));
  27. }
  28. }
  29. return new QRPolynomial(num, 0);
  30. }
  31. mod(e) {
  32. if (this.getLength() - e.getLength() < 0) {
  33. return this;
  34. }
  35. var ratio = QRMath.glog(this.get(0)) - QRMath.glog(e.get(0));
  36. var num = new Array(this.getLength());
  37. for (var i = 0; i < this.getLength(); i++) {
  38. num[i] = this.get(i);
  39. }
  40. for (var i = 0; i < e.getLength(); i++) {
  41. num[i] ^= QRMath.gexp(QRMath.glog(e.get(i)) + ratio);
  42. }
  43. return new QRPolynomial(num, 0).mod(e);
  44. }
  45. };