How to write this formula out as an algebraic equation

63 Views Asked by At

I've been developing some basic hash algorithms for cryptanalylis purposes, and would like to know how to represent it as an algebraic equation. The algorithm follows thus:

Basic python code

p = [97, 111, 105, 122, 108, 107]
s = [107, 101, 121, 107, 101, 121]
c = []

for i in range(len(p)):

    alpha = p[i + 1:] + p[:i]  # With p as 97 alpha = [111, ..., 107], with p as 111, [105, ..., 97]

    tmp = p[i]  # First run 97, 2nd run 111 etc.

    for x in alhpa:  # 1st run 111, then 105 etc.

        tmp = tmp ^ x ^ s[x] if s[x] exists else s[x%len(s)]  # If s[x] exists XOR against that or s[x mod len(s)] to make s[x] exist
    c.append(tmp)  # Adds tmp to the output.

Sample output of above algorithm:

With p as [97, 111, 105, 122, 108, 107]
With s as [107, 101, 121, 107, 101, 121]
New cycle 1
[97, 'xor', 111, 'xor', 107] -> 101
[101, 'xor', 105, 'xor', 107] -> 103
[103, 'xor', 122, 'xor', 121] -> 100
[100, 'xor', 108, 'xor', 107] -> 99
[99, 'xor', 107, 'xor', 121] -> 113
New cycle 2
[111, 'xor', 105, 'xor', 107] -> 109
[109, 'xor', 122, 'xor', 121] -> 110
[110, 'xor', 108, 'xor', 107] -> 105
[105, 'xor', 107, 'xor', 121] -> 123
[123, 'xor', 97, 'xor', 101] -> 127
New cycle 3
[105, 'xor', 122, 'xor', 121] -> 106
[106, 'xor', 108, 'xor', 107] -> 109
[109, 'xor', 107, 'xor', 121] -> 127
[127, 'xor', 97, 'xor', 101] -> 123
[123, 'xor', 111, 'xor', 107] -> 127
New cycle 4
[122, 'xor', 108, 'xor', 107] -> 125
[125, 'xor', 107, 'xor', 121] -> 111
[111, 'xor', 97, 'xor', 101] -> 107
[107, 'xor', 111, 'xor', 107] -> 111
[111, 'xor', 105, 'xor', 107] -> 109
New cycle 5
[108, 'xor', 107, 'xor', 121] -> 126
[126, 'xor', 97, 'xor', 101] -> 122
[122, 'xor', 111, 'xor', 107] -> 126
[126, 'xor', 105, 'xor', 107] -> 124
[124, 'xor', 122, 'xor', 121] -> 127
New cycle 6
[107, 'xor', 97, 'xor', 101] -> 111
[111, 'xor', 111, 'xor', 107] -> 107
[107, 'xor', 105, 'xor', 107] -> 105
[105, 'xor', 122, 'xor', 121] -> 106
[106, 'xor', 108, 'xor', 107] -> 109
[113, 127, 127, 109, 127, 109]  # ASCII encoded output - value of c
['q', '\x7f', '\x7f', 'm', '\x7f', 'm']  # ASCII decoded output - value of c

How would I write this as a single formula, that would still be valid no matter what the length of $p$ is?