Search code examples
matlabsignal-processingcommunicationlinear-algebragalois-field

How to perform inverse in GF(2) and multiply in GF(256) in Matlab?


I have a binary matrix A (only 1 and 0), and a vector D in Galois field (256). The vector C is calculated as:

  C = (A^^-1)*D

where A^^-1 denotes the inverse matrix of matrix A in GF(2), * is multiply operation. The result vector C must be in GF(256). I tried to do it in Matlab.

A= [ 1     0     0     1     1     0     0     0     0     0     0     0     0     0;
     1     1     0     0     0     1     0     0     0     0     0     0     0     0;
     1     1     1     0     0     0     1     0     0     0     0     0     0     0;
     0     1     1     1     0     0     0     1     0     0     0     0     0     0;
     0     0     1     1     0     0     0     0     1     0     0     0     0     0;
     1     1     0     1     1     0     0     1     0     1     0     0     0     0;
     1     0     1     1     0     1     0     0     1     0     1     0     0     0;
     1     1     1     0     0     0     1     1     1     0     0     1     0     0;
     0     1     1     1     1     1     1     0     0     0     0     0     1     0;
     0     0     0     0     1     1     1     1     1     0     0     0     0     1;
     0     1     1     1     1     0     1     1     1     0     1     1     1     0;
     0     0     0     1     0     0     0     1     0     0     0     0     0     0;
     0     0     1     0     0     0     0     1     0     0     0     0     0     0;
     1     1     1     1     0     0     0     0     0     0     0     0     0     0]

D=[0;0;0;0;0;0;0;0;0;0;103;198;105;115]
A=gf(A,1);
D=gf(D,8); %%2^8=256
C=inv(A)*D
%% The corrected result must be
%%C=[103;187;125;210;181;220;161;20;175;175;187;187;220;115]

However, for above code, I cannot achieved as my expected result

C=[103;187;125;210;181;220;161;20;175;175;187;187;220;115]

It produces an error as

Error using  *  (line 14)
Orders must match.

Could you help me achieve my expected result?


Solution

  • The error

    Error using * (line 14)
    Orders must match.

    arises because Matlab does not support applying standard mathematical operations (+, *, .*, .^, \, etc.) to Galois Fields of different order, GF(2) and GF(256). The inverse operation, inv(A) is a valid operation and if you perform it on its own as shown by @Ander Biguri it will succeed and generate the inverse of A in GF(2). Your calculation breaks down when you attempt to multiply inv(A) and D as the orders of these Galois Fields do not match.

    In this example it is unnecessary to explicitly define A as a Galois Field of order 2, GF(2) as multiplication in GF(256) takes place in GF(2). That is 1 + 1 = 0.

    If we thus modify the code that creates the Galois Field for A to

    A = gf(A, 8);
    

    we can then perform

    C = inv(A)*D
    

    which produces

    C = GF(2^8) array. Primitive polynomial = D^8+D^4+D^3+D^2+1 (285 decimal)
    
    Array elements = 
    
             103
             187
             125
             210
             181
             220
             161
              20
             175
             175
             187
             187
             220
             115
    

    C is thus in GF(256) and produces the expected result.