UNPKG

1.33 kBJavaScriptView Raw
1var QRMath = require('./QRMath');
2
3function QRPolynomial(num, shift) {
4 if (num.length == undefined) {
5 throw new Error(num.length + "/" + shift);
6 }
7
8 var offset = 0;
9
10 while (offset < num.length && num[offset] == 0) {
11 offset++;
12 }
13
14 this.num = new Array(num.length - offset + shift);
15 for (var i = 0; i < num.length - offset; i++) {
16 this.num[i] = num[i + offset];
17 }
18}
19
20QRPolynomial.prototype = {
21
22 get : function(index) {
23 return this.num[index];
24 },
25
26 getLength : function() {
27 return this.num.length;
28 },
29
30 multiply : function(e) {
31
32 var num = new Array(this.getLength() + e.getLength() - 1);
33
34 for (var i = 0; i < this.getLength(); i++) {
35 for (var j = 0; j < e.getLength(); j++) {
36 num[i + j] ^= QRMath.gexp(QRMath.glog(this.get(i) ) + QRMath.glog(e.get(j) ) );
37 }
38 }
39
40 return new QRPolynomial(num, 0);
41 },
42
43 mod : function(e) {
44
45 if (this.getLength() - e.getLength() < 0) {
46 return this;
47 }
48
49 var ratio = QRMath.glog(this.get(0) ) - QRMath.glog(e.get(0) );
50
51 var num = new Array(this.getLength() );
52
53 for (var i = 0; i < this.getLength(); i++) {
54 num[i] = this.get(i);
55 }
56
57 for (var i = 0; i < e.getLength(); i++) {
58 num[i] ^= QRMath.gexp(QRMath.glog(e.get(i) ) + ratio);
59 }
60
61 // recursive call
62 return new QRPolynomial(num, 0).mod(e);
63 }
64};
65
66module.exports = QRPolynomial;