bitcoin-atm
bitcoin atm for pyc inc.
git clone https://9o.is/git/bitcoin-atm.git
gf256.js
(2589B)
1 /*
2 Ported to JavaScript by Lazar Laszlo 2011
3
4 lazarsoft@gmail.com, www.lazarsoft.info
5
6 */
7
8 /*
9 *
10 * Copyright 2007 ZXing authors
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24
25
26 function GF256( primitive)
27 {
28 this.expTable = new Array(256);
29 this.logTable = new Array(256);
30 var x = 1;
31 for (var i = 0; i < 256; i++)
32 {
33 this.expTable[i] = x;
34 x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
35 if (x >= 0x100)
36 {
37 x ^= primitive;
38 }
39 }
40 for (var i = 0; i < 255; i++)
41 {
42 this.logTable[this.expTable[i]] = i;
43 }
44 // logTable[0] == 0 but this should never be used
45 var at0=new Array(1);at0[0]=0;
46 this.zero = new GF256Poly(this, new Array(at0));
47 var at1=new Array(1);at1[0]=1;
48 this.one = new GF256Poly(this, new Array(at1));
49
50 this.__defineGetter__("Zero", function()
51 {
52 return this.zero;
53 });
54 this.__defineGetter__("One", function()
55 {
56 return this.one;
57 });
58 this.buildMonomial=function( degree, coefficient)
59 {
60 if (degree < 0)
61 {
62 throw "System.ArgumentException";
63 }
64 if (coefficient == 0)
65 {
66 return zero;
67 }
68 var coefficients = new Array(degree + 1);
69 for(var i=0;i<coefficients.length;i++)coefficients[i]=0;
70 coefficients[0] = coefficient;
71 return new GF256Poly(this, coefficients);
72 }
73 this.exp=function( a)
74 {
75 return this.expTable[a];
76 }
77 this.log=function( a)
78 {
79 if (a == 0)
80 {
81 throw "System.ArgumentException";
82 }
83 return this.logTable[a];
84 }
85 this.inverse=function( a)
86 {
87 if (a == 0)
88 {
89 throw "System.ArithmeticException";
90 }
91 return this.expTable[255 - this.logTable[a]];
92 }
93 this.multiply=function( a, b)
94 {
95 if (a == 0 || b == 0)
96 {
97 return 0;
98 }
99 if (a == 1)
100 {
101 return b;
102 }
103 if (b == 1)
104 {
105 return a;
106 }
107 return this.expTable[(this.logTable[a] + this.logTable[b]) % 255];
108 }
109 }
110
111 GF256.QR_CODE_FIELD = new GF256(0x011D);
112 GF256.DATA_MATRIX_FIELD = new GF256(0x012D);
113
114 GF256.addOrSubtract=function( a, b)
115 {
116 return a ^ b;
117 }