zeby ograniczyć ilość obliczeń
This commit is contained in:
Kacper Kruczek 2018-07-01 15:27:15 +02:00
parent a698eefa97
commit 60b29ca3d8
3 changed files with 26 additions and 18 deletions

View File

@ -1,6 +1,7 @@
const crc = require("./crc.js"); const crc = require("./crc.js");
let message = process.argv[3]; const message = process.argv[3];
let flag = process.argv[2]; const flag = process.argv[2];
switch (flag) { switch (flag) {
case '-e': case '-e':
console.log(crc.encode(message).encoded); console.log(crc.encode(message).encoded);

30
crc.js
View File

@ -1,9 +1,8 @@
let Polynomial = require('./polynomial.js'); const Polynomial = require('./polynomial.js');
let L = new Polynomial.Class(2, new Array(16).fill(1)); const L = new Polynomial.Class(2, new Array(16).fill(1));
let X = new Polynomial.Class(2, [0, 1]); const X16 = new Polynomial.Class(2, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); //jako ze można to od razu wymnożyć
let X16 = new Polynomial.Class(2, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); //jako ze można to od razu wymnożyć const G = new Polynomial.Class(2, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]); //jako ze można to od razu wymnożyć
let G = new Polynomial.Class(2, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]); //jako ze można to od razu wymnożyć
const to_bin = a => { const to_bin = a => {
var result = ""; var result = "";
@ -17,7 +16,7 @@ const to_ascii = a => {
a = a.join(''); a = a.join('');
a = parseInt(a, 2); a = parseInt(a, 2);
//nie znalazłem innego sposobu w js na osiągniecie tego efektu.. //nie znalazłem innego sposobu w js na osiągniecie tego efektu..
// if (a < 126) //ponieważ wieksze liczby nie należą do typowego Ascii // if (a < 126) //ponieważ wieksze liczby nie należą do typowego Ascii//porzucone ze wzgledu na decode
return String.fromCharCode(a); return String.fromCharCode(a);
// else { // else {
// return "0x" + a.toString(16); // return "0x" + a.toString(16);
@ -26,6 +25,12 @@ const to_ascii = a => {
} }
const mod8format = array => {
while (array.length % 8 != 0) {
array.push(0);
}
return array;
}
const fcs = m => { const fcs = m => {
let bits = m.map(to_bin); //message in binary let bits = m.map(to_bin); //message in binary
@ -35,7 +40,7 @@ const fcs = m => {
fcs = Polynomial.add(fcs, fcs = Polynomial.add(fcs,
Polynomial.multiply( Polynomial.multiply(
Polynomial.power(X, m.length * 8), Polynomial.Mono(m.length * 8, 1, 2),
L L
) )
) )
@ -45,19 +50,14 @@ const fcs = m => {
return fcs.coefficients; return fcs.coefficients;
} }
function mod8format(lst) {
while (lst.length % 8 != 0) {
lst.push(0);
}
return lst;
}
function check(m) {
const check = m => {
let bits = m.map(to_bin); //message in binary let bits = m.map(to_bin); //message in binary
bits = bits.join('').split('').reverse(); //reverse binary decoded message bits = bits.join('').split('').reverse(); //reverse binary decoded message
let fcs = Polynomial.power(X, bits.length); let fcs = Polynomial.Mono(bits.length, 1, 2);
let C = new Polynomial.Class(2, bits); let C = new Polynomial.Class(2, bits);

View File

@ -170,4 +170,11 @@ function gcd(p1, p2) {
} }
exports.gcd = gcd; exports.gcd = gcd;
function Mono(n, c, mod) {
let coef = new Array(n).fill(0);
coef.push(c);
return new Polynomial(mod, coef);
}
exports.Mono = Mono;