Created
February 20, 2022 12:28
-
-
Save urish/c4c85a03dfb10b2a3a895e3c3aecd36c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function galoisMultiply1(x, y) { | |
const Zi = new Uint32Array(4); | |
const Vi = new Uint32Array(y); | |
for (let i = 0; i < 128; i++) { | |
const xi = (x[i >> 5] & (1 << (31 - (i % 32)))) !== 0; | |
if (xi) { | |
Zi[0] ^= Vi[0]; | |
Zi[1] ^= Vi[1]; | |
Zi[2] ^= Vi[2]; | |
Zi[3] ^= Vi[3]; | |
} | |
const lsbValue = Vi[3] & 1; | |
Vi[3] = (Vi[3] >>> 1) | ((Vi[2] & 1) << 31); | |
Vi[2] = (Vi[2] >>> 1) | ((Vi[1] & 1) << 31); | |
Vi[1] = (Vi[1] >>> 1) | ((Vi[0] & 1) << 31); | |
Vi[0] = Vi[0] >>> 1; | |
if (lsbValue) { | |
Vi[0] ^= 0xe1000000; | |
} | |
} | |
return Zi; | |
} | |
function galoisMultiply2(x, y) { | |
const Zi = new Uint32Array(4); | |
const Vi = new Uint32Array(y); | |
for (let i = 0; i < 128; i++) { | |
const xi = (x[i >> 5] & (1 << (31 - (i % 32)))) !== 0; | |
if (xi) { | |
Zi[0] ^= Vi[0]; | |
Zi[1] ^= Vi[1]; | |
Zi[2] ^= Vi[2]; | |
Zi[3] ^= Vi[3]; | |
} | |
const lsbValue = !!(Vi[3] & 1); | |
Vi[3] = (Vi[3] >>> 1) | ((Vi[2] & 1) << 31); | |
Vi[2] = (Vi[2] >>> 1) | ((Vi[1] & 1) << 31); | |
Vi[1] = (Vi[1] >>> 1) | ((Vi[0] & 1) << 31); | |
Vi[0] = Vi[0] >>> 1; | |
if (lsbValue) { | |
Vi[0] ^= 0xe1000000; | |
} | |
} | |
return Zi; | |
} | |
function testGalois(fn) { | |
const x = new Uint32Array([2425393296, 2425393296, 2425393296, 2425393296]); | |
const y = new Uint32Array([3692772234, 3144594163, 3014792419, 2386452453]); | |
const expected = [2793996538, 14267392, 3369100906, 2506634262]; | |
for (let i = 0; i < 100; i++) { | |
const result = Array.from(fn(x, y)); | |
if (JSON.stringify(result) != JSON.stringify(expected)) { | |
console.error('Failed on iteration', i, 'expected', expected, 'result', result) | |
return; | |
} | |
} | |
console.log('Pass!'); | |
} | |
console.log('Testing galoisMultiply1') | |
testGalois(galoisMultiply1); | |
console.log('Testing galoisMultiply2') | |
testGalois(galoisMultiply2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Minimized test case: