一、实验原理(精简)

图1:基本原理

图2:指数对数表

二、代码实现

import time

def extract_info(str_polynimial):
    length=len(str_polynimial)
    add=1    
    for i in range(length):
        if i==0:  
            j=0
            while(j!=length and str_polynimial[j]!='x'):
                j+=1
            if j+1==length or str_polynimial[j+1]!='^':
                index_list = [0] *2
                str_coefficient = "".join([str(item) for item in str_polynimial[0:j]])
                if str_coefficient=="":
                    str_coefficient='1'
                index=1
                index_list[index] = int(str_coefficient)
            elif str_polynimial[j+1]=='^':
                k=j+2
                while(k!=length and str_polynimial[k]!='+' and str_polynimial[k]!='-'):
                    k+=1
                str_index = "".join([str(item) for item in str_polynimial[j+2:k]])
                highest_index = int(str_index)  
                index_list = [0] * (highest_index + 1)
                index = highest_index
                if j == 0:  
                    index_list[index] = 1
                else:
                    str_coefficient = "".join([str(item) for item in str_polynimial[0:j]])
                    index_list[index] = int(str_coefficient)

        elif str_polynimial[i]!='+' and str_polynimial[i]!='-':
            continue
        else:
            j = i
            while (j!=length and str_polynimial[j] != 'x'):
                j += 1
            str_coefficient = "".join([str(item) for item in str_polynimial[i+1:j]])
            if str_coefficient=="":     
                str_coefficient='1'
            if str_polynimial[i]=='-':      
                str_coefficient="-"+str_coefficient
            if j==length:      
                str_index='0'
            elif (j+1)==length or str_polynimial[j+1]=='+'or str_polynimial[j+1]=='-':        
                str_index = '1'
            else:
                k=j+2
                while(k!=length and str_polynimial[k]!='+' and str_polynimial[k]!='-'):
                    k+=1
                str_index="".join([str(item) for item in str_polynimial[j+2:k]])        
            index_list[int(str_index)]=int(str_coefficient)
    index_list.reverse()
    return index_list

def translation(list):
    str_polynimial=""
    for i in range(len(list)):
        if list[i]==0:
            continue
        index=len(list)-i-1       
        coefficient=list[i]      
        if index==0:
            string=str(coefficient)
        elif index==1:
            if coefficient==1:
                string = 'x'
            else:
                string=str(coefficient)+'x'
        else:
            if coefficient==1:
                string = 'x' + '^' + str(index)
            else:
                string=str(coefficient)+'x'+'^'+str(index)
        if i==0 or list[i]<0:
            str_polynimial=str_polynimial+string
        elif list[i]>0:
            str_polynimial=str_polynimial+'+'+string

    if str_polynimial=="":
        str_polynimial="0"
    return str_polynimial

def Multiplication(list,a):
    result=[]
    for i in range(len(list)):
        result.append(list[i]*a%2)
    return result

def Multiplication2(list1,list2):
    a=list1.copy()
    b=list2.copy()
    result=[0]
    for i in range(len(b)):
        product=Multiplication(a,b[i])
        product.extend([0]*(len(b)-1-i))
        result=Add(result,product)
    return result

def Subtraction(list1,list2):
    a=list1.copy()
    b=list2.copy()
    result=[]
    for i in range(len(a)):
        result.append((a[i]-b[i])%2)
    return result

def Subtraction2(list1,list2):
    a=list1.copy()
    b=list2.copy()
    if len(a)<len(b):
        a.reverse()
        a.extend([0]*(len(b)-len(a)))
        a.reverse()
    elif len(a)>len(b):
        b.reverse()
        b.extend([0] * (len(a) - len(b)))
        b.reverse()
    result=Subtraction(a,b)
    return result

def Add(list1,list2):
    a=list1.copy()
    b=list2.copy()
    result=[]
    if len(a)>len(b):
        b.reverse()
        b.extend([0]*(len(a)-len(b)))
        b.reverse()
        max=len(a)
    else:
        a.reverse()
        a.extend([0]*(len(b)-len(a)))
        a.reverse()
        max=len(b)
    for i in range(max):
        result.append((a[i]+b[i])%2)
    for j in range(len(result)):  
        if result[0] == 0:
            result.remove(0)
        else:
            break
    return result

def Division(list1,list2):
    r=list1.copy()
    b=list2.copy()
    if len(r)<len(b):
        return [0],list1
    q=[0]*(len(r)-len(b)+1)
    for i in range(len(q)):
        if len(r)>=len(b):
            index = len(r) - len(b) + 1  
            q[-index] = int(r[0] / b[0])
            b_=b.copy()
            b_.extend([0] * (len(r) - len(b)))
            b_=Multiplication(b_,q[i])
            r = Subtraction(r ,b_)
            for j in range(len(r)):     
                if r[0]==0:
                    r.remove(0)
                else:
                    break
        else:
            break
    return q,r

def G28_Multiplication(list1,list2,w):
    a=list1.copy()
    b=list2.copy()
    Mul=Multiplication2(a,b)
    _,r=Division(Mul,w)
    return r

def Extend_Euclid(list1,list2,p):
    f=list1.copy()
    g=list2.copy()
    u_2=[1]
    u_1=[0]
    v_2=[0]
    v_1=[1]
    while(g!=[]):
        q,r=Division(f,g)
        u=Subtraction2(u_2,Multiplication2(q,u_1))
        v=Subtraction2(v_2,Multiplication2(q,v_1))
        f,g=g,r
        u_2,u_1=u_1,u
        v_2,v_1=v_1,v

    d,u,v=f,u_2,v_2
    return d,u,v

def tran_2_to_10(list):
    result=0
    for i in range(len(list)):
        if list[i]==1:
            result+=2**(len(list)-1-i)
    return result

def tran_10_to_2_list(number_code):
    list=[]
    number=number_code
    while(number!=0):
        r=number%2
        list.append(int(r))
        number=(number-r)/2
    list.reverse()
    return list

def test():
    w = [1, 0, 1, 1, 0, 0, 0, 1, 1]  
    x = [1, 0] 
    table = [1]
    table_F = [0] * 256
    for i in range(255):
        if i == 254: 
            table.append(0)
            table_F[0] = 255
            break
        _, item = Division(x, w) 
        number = tran_2_to_10(item)
        table_F[number] = i + 1
        table.append(number)
        x.append(0)
    print("GF(256)域下,建立指数对数表如下:")
  

    for i in range(len(table)):print("{:<4}".format(i), end=' ')
    print("\n")
    for j in table:print("{:<4}".format(j), end=' ')
    print("\n")
    for k in table_F:print("{:<4}".format(k), end=' ')
    print("\n*******************************************")
    a = input("请输入第一个多项式:")
    list_a = extract_info(a)
    a_number = tran_2_to_10(list_a)
    b = input("请输入第二个多项式:")
    list_b = extract_info(b)
    b_number = tran_2_to_10(list_b)
    num_product = table[(table_F[a_number] + table_F[b_number]) % 255]  
    list_product = tran_10_to_2_list(num_product)
    str_product = translation(list_product)  
    inverse_a = table[255 - table_F[a_number]]
    list_inverse = tran_10_to_2_list(inverse_a)
    str_inverse_a = translation(list_inverse)
    print("GF(2^8)下多项式积为:"+str_product)
    print("GF(2^8)下多项式"+a+"逆元为:"+str_inverse_a)
    return 0

if __name__ == '__main__':
test()

三、生成指数对数表示例

图3:节选部分指数对数表

---END---