[python] QR code application of Reed-solomon codes (3)

    (Note: This part of the blog mainly combines the PPT of Mr. Zhou and the information found, trying to explain the mathematical principles and information theory principles of Reed-Solomon Codes, and discuss the application of Reed-Solomon Codes in QR codes.)

        In fact, the multiplication shown above is not the most convenient, because it has to go through many cycles, so there is a multiplication based on logarithms, and the Galois field is used to cleverly turn multiplication into addition.

        We know that multiplication is very easy for modulo 2, just shift one bit to the left (similar to multiplying by 2, which is the power of itself plus one), and then add 100011101 (a prime polynomial with one more bit than 8bits, similar to Prime numbers) are XORed, and the result of XOR is the coefficient of the corresponding polynomial, for example:

α ^ 0 = 00000001 α ^ 4 = 00010000 α ^ 8 = 00011101 α ^ 12 = 11001101
       α ^ 1 = 00000010 α ^ 5 = 00100000 α ^ 9 = 00111010 α ^ 13 = 10000111
       α ^ 2 = 00000100 α ^ 6 = 01000000 α ^ 10 = 01110100 α ^ 14 = 00010011
       α ^ 3 = 00001000 α ^ 7 = 10000000 α ^ 11 = 11101000 α ^ 15 = 00100110
       (Note: α^7 * α exceeds 8-bit and needs to be XORed with 100011101 to get α^8, and so on.)
To explain here, why XOR with 100011101?

      100011101= 1*x^8+0*x^7+0*x^6+0*x^5+1*x^4+1*x^3+1*x^2+0*x^1+ 1*x^0, we said earlier that mod(p) can control the result within [0, p-1], so when we put a^k mod (100011101) = 100011101 - a^k, the polynomial The four operations of subtraction and addition are XOR.

        So we go on like this until a^255 = 0000 0001 ends, we can express all the numbers in [1, 255] as Galois field symbols, from which we can find another multiplication, such as a^34 * a ^8 = a^(34+8) = a^42, then we convert a^42 to get the product.

        So how do we turn the power of the generator into a polynomial? In fact, there is a table to look up, and Python can also be used to generate a list:

table = [0]*512   
m_table = [0]*256  
def tables(prim = 0x11B):  
    x = 1;     //g^0  
    for i in range(0,255):    
        table[i] = x
        m_table[x] = i
        x<<1      
        if x & 0x100 : #The highest index has reached 8, and it needs to be XORed on prim
            x ^= prim #Use the multiplication skills mentioned earlier  
   

         For the inverse metatable, we can also describe it in Python. If a and b are inverses of each other, then a*b = e. Expressed in terms of generators: a^n* a^m = e = 1. And because e = a^0 = a^255, so g^k * g(255-k) = g^(k + 255 -k) = e. So g^k and g^(255-k) are inverses of each other. For the polynomial value val, find its inverse. You can first find the g power corresponding to val. That is, how many powers of g are equal to val. It can be queried through the reverse table, set to k. Then the power of its inverse is 255-k. At this point, you can query it through the forward table. The implementation code is as follows:

        

inverse_table = [0]*256
def inverseTable(table, m_table):  
    for i in range(1,255): #0 has no inverse, so start at 1  
        k = m_table[i];  
        k = 255 - k;  
        inverse_table[i] = table[k]

Next, we can directly ask for multiplication. Is it easy to fry chicken?

def table_mul(x,y):
    if x==0 or y==0:
        return 0
    return table[m_table[x] + m_table[y]]
       Okay, I can finally change the division method tomorrow~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325621159&siteId=291194637