# AES SBOX Masking in Python

I am trying to create an AES Sbox masking based on this paper. In the paper, they tried to mask the sbox described in this other paper. The inversion is been done in GF(4). I know that it is mostly implemented in hardware, while the software implementation requires use of EEA or sbox table. However, I am implementing it in python anyway but stuck in the process. The below code is for the first part of the AES Subbyte, I haven't computed the second part - Affine Transformation. Can someone who is vast in this field assist with any hint on how to implement the SBOX generation and its masking based on the first paper?

def hex_to_binary(hex_digit):
# Convert hexadecimal to binary and remove the '0b' prefix
binary_equivalent = bin(int(hex_digit, 16))[2:]

# Ensure the binary representation is 8 bits long
binary_equivalent = binary_equivalent.zfill(4)

return binary_equivalent

def main():
try:
# Get user input for two hex digits
hex_digit1 = input("Enter the first hex digit: ").upper()
hex_digit2 = input("Enter the second hex digit: ").upper()

# Convert hex digits to 8-bit binary equivalents
binary1 = hex_to_binary(hex_digit1)
binary2 = hex_to_binary(hex_digit2)

bin_combi = binary1+binary2
print(bin_combi)
inverse_4(bin_combi)
# Display the results
print(f"Binary equivalent of {hex_digit1}: {binary1}")
print(f"Binary equivalent of {hex_digit2}: {binary2}")
except ValueError:

def inverse_4(bin_combi):
a0 = int(str(bin_combi)[7:8])
a1 = int(str(bin_combi)[6:7])
a2 = int(str(bin_combi)[5:6])
a3 = int(str(bin_combi)[4:5])
a4 = int(str(bin_combi)[3:4])
a5 = int(str(bin_combi)[2:3])
a6 = int(str(bin_combi)[1:2])
a7 = int(str(bin_combi)[0:1])

aA = a1 ^ a7
aB = a5 ^ a7
aC = a4 ^ a6

al0 = aC ^ a0 ^ a5
al1 = a1 ^ a2
al2 = aA
al3 = a2 ^ a4

ah0 = aC ^ a5
ah1 = aA ^ aC
ah2 = aB ^ a2 ^ a3
ah3 = aB

inverse_8(ah0, ah1, ah2,ah3,al0,al1,al2,al3)

def inverse_8(ah0, ah1, ah2,ah3,al0,al1,al2,al3):
aA = al1 ^ ah3
aB = ah0 ^ ah1

a0 = al0 ^ ah0
a1 = aB ^ ah3
a2 = aA ^ aB
a3 = aB ^ al1 ^ ah2
a4 = aA ^ aB ^ al3
a5 = aB ^ al2
a6 = aA ^ al2 ^ al3 ^ ah0
a7 = aB ^ al2 ^ ah3

print(f'S_8: {a7}{a6}{a5}{a4}{a3}{a2}{a1}{a0}')