File size: 2,289 Bytes
7e9dc27 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
require("bit");
-- finite field with base 2 and modulo irreducible polynom x^8+x^4+x^3+x+1 = 0x11d
local private = {};
local public = {};
aeslua.gf = public;
-- private data of gf
private.n = 0x100;
private.ord = 0xff;
private.irrPolynom = 0x11b;
private.exp = {};
private.log = {};
--
-- add two polynoms (its simply xor)
--
function public.add(operand1, operand2)
return bit.bxor(operand1,operand2);
end
--
-- subtract two polynoms (same as addition)
--
function public.sub(operand1, operand2)
return bit.bxor(operand1,operand2);
end
--
-- inverts element
-- a^(-1) = g^(order - log(a))
--
function public.invert(operand)
-- special case for 1
if (operand == 1) then
return 1;
end;
-- normal invert
local exponent = private.ord - private.log[operand];
return private.exp[exponent];
end
--
-- multiply two elements using a logarithm table
-- a*b = g^(log(a)+log(b))
--
function public.mul(operand1, operand2)
if (operand1 == 0 or operand2 == 0) then
return 0;
end
local exponent = private.log[operand1] + private.log[operand2];
if (exponent >= private.ord) then
exponent = exponent - private.ord;
end
return private.exp[exponent];
end
--
-- divide two elements
-- a/b = g^(log(a)-log(b))
--
function public.div(operand1, operand2)
if (operand1 == 0) then
return 0;
end
-- TODO: exception if operand2 == 0
local exponent = private.log[operand1] - private.log[operand2];
if (exponent < 0) then
exponent = exponent + private.ord;
end
return private.exp[exponent];
end
--
-- print logarithmic table
--
function public.printLog()
for i = 1, private.n do
print("log(", i-1, ")=", private.log[i-1]);
end
end
--
-- print exponentiation table
--
function public.printExp()
for i = 1, private.n do
print("exp(", i-1, ")=", private.exp[i-1]);
end
end
--
-- calculate logarithmic and exponentiation table
--
function private.initMulTable()
local a = 1;
for i = 0,private.ord-1 do
private.exp[i] = a;
private.log[a] = i;
-- multiply with generator x+1 -> left shift + 1
a = bit.bxor(bit.lshift(a, 1), a);
-- if a gets larger than order, reduce modulo irreducible polynom
if a > private.ord then
a = public.sub(a, private.irrPolynom);
end
end
end
private.initMulTable();
return public; |