Skip to content

Instantly share code, notes, and snippets.

@2bbb
Created June 1, 2017 11:06
Show Gist options
  • Select an option

  • Save 2bbb/5d4c3bc15ab5c0789cbda389b94fe532 to your computer and use it in GitHub Desktop.

Select an option

Save 2bbb/5d4c3bc15ab5c0789cbda389b94fe532 to your computer and use it in GitHub Desktop.
MatF2
const MatF2 = {
from_string: M => M.split('\n').map(l => l.replace(/ /g, '')).filter(l => l != '').map(l => l.split('').map(b => 0 ^ b)),
translate: M => M[0].map(() => (M.map(() => 0))).map((l, i) => l.map((_, j) => M[j][i])),
product: (G, H) => {
const res = [];
for(let i = 0; i < G.length; i++) {
const arr = [];
for(let j = 0; j < H[0].length; j++) {
let tmp = 0;
for(let k = 0; k < H.length; k++) {
tmp ^= G[i][k] * H[k][j];
}
arr.push(tmp);
}
res.push(arr);
}
return res;
},
info: M => console.log(M.length, M[0].length),
print: M => console.log(M.map(l => l.join(',')).join('\n') + '\n')
};
const G_str = `
1000 0000 0000 0000 0000 0000 0000 111
0100 0000 0000 0000 0000 0000 0001 011
0010 0000 0000 0000 0000 0000 0001 101
0001 0000 0000 0000 0000 0000 0001 110
0000 1000 0000 0000 0000 0000 0010 011
0000 0100 0000 0000 0000 0000 0010 101
0000 0010 0000 0000 0000 0000 0010 110
0000 0001 0000 0000 0000 0000 0011 001
0000 0000 1000 0000 0000 0000 0011 010
0000 0000 0100 0000 0000 0000 0011 100
0000 0000 0010 0000 0000 0000 0011 111
0000 0000 0001 0000 0000 0000 0011 110
0000 0000 0000 1000 0000 0000 0011 101
0000 0000 0000 0100 0000 0000 0011 011
0000 0000 0000 0010 0000 0000 0011 000
0000 0000 0000 0001 0000 0000 0010 111
0000 0000 0000 0000 1000 0000 0010 100
0000 0000 0000 0000 0100 0000 0010 010
0000 0000 0000 0000 0010 0000 0010 001
0000 0000 0000 0000 0001 0000 0001 111
0000 0000 0000 0000 0000 1000 0001 100
0000 0000 0000 0000 0000 0100 0001 010
0000 0000 0000 0000 0000 0010 0001 001
0000 0000 0000 0000 0000 0001 0000 110
0000 0000 0000 0000 0000 0000 1000 101
0000 0000 0000 0000 0000 0000 0100 011
`;
const H_str = `
1110 1101 0010 1101 0011 0010 1100 0010
1101 1010 1011 0101 0101 0101 0100 0100
1011 0110 0111 1001 1001 1001 1000 1000
0111 0001 1111 1110 0001 1110 0001 0000
0000 1111 1111 1111 1110 0000 0010 0000
1111 1111 1111 1111 1111 1111 1111 1111
`;
const G = MatF2.from_string(G_str);
const H = MatF2.from_string(H_str);
const GT = MatF2.translate(G);
const HT = MatF2.translate(H);
MatF2.print(MatF2.product(G, HT));
MatF2.print(MatF2.product(H, GT));
console.log(H.map(l => (l.reduce((sum, x) => sum * 2 + x, 0)).toString(16)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment