Polynomial.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var math = require('./math');
  2. function QRPolynomial(num, shift) {
  3. if (num.length == undefined) {
  4. throw new Error(num.length + "/" + shift);
  5. }
  6. var offset = 0;
  7. while (offset < num.length && num[offset] == 0) {
  8. offset++;
  9. }
  10. this.num = new Array(num.length - offset + shift);
  11. for (var i = 0; i < num.length - offset; i++) {
  12. this.num[i] = num[i + offset];
  13. }
  14. }
  15. QRPolynomial.prototype = {
  16. get : function(index) {
  17. return this.num[index];
  18. },
  19. getLength : function() {
  20. return this.num.length;
  21. },
  22. multiply : function(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] ^= math.gexp(math.glog(this.get(i) ) + math.glog(e.get(j) ) );
  27. }
  28. }
  29. return new QRPolynomial(num, 0);
  30. },
  31. mod : function(e) {
  32. if (this.getLength() - e.getLength() < 0) {
  33. return this;
  34. }
  35. var ratio = math.glog(this.get(0) ) - math.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] ^= math.gexp(math.glog(e.get(i) ) + ratio);
  42. }
  43. // recursive call
  44. return new QRPolynomial(num, 0).mod(e);
  45. }
  46. };
  47. module.exports = QRPolynomial;