Last active
April 8, 2025 12:27
-
-
Save martandrMC/cac80ed594ef36a08a2366056b2249ea to your computer and use it in GitHub Desktop.
Pearson-Like Hash S-Box Finder (Useful for making minimal hash maps for keyword identification in lexers)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
The MIT License (MIT) | |
Copyright (c) 2025 martandrMC | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
""" | |
This is a revision of my previous finder written in Java: | |
https://gist.github.com/martandrMC/ab888dbeb265b340b5ab1285ed1dd972 | |
This one's written in Python and uses Z3 to solve the S-Box. | |
This time it can handle about 60 keywords before it becomes | |
unreasonably slow, which is just enough to handle the C keyword set. | |
This version does not actually qualify as a Pearson Hash due to | |
the non-injective nature of the generated S-Box. However it still | |
fulfills its hereby given purpose. | |
""" | |
import z3 | |
from typing import List | |
def pearson_hash(sbox: List[int], source: str) -> int: | |
result: int = 0 | |
for char in source: | |
result = sbox[result ^ ord(char)] | |
return result | |
def find_sbox(keys: List[str]) -> List[int]: | |
if(len(keys) > 256): return None | |
solver = z3.Solver() | |
sbox = z3.Array('sbox', z3.BitVecSort(8), z3.BitVecSort(8)) | |
hashes = z3.Array('hashes', z3.BitVecSort(8), z3.BitVecSort(8)) | |
for i in range(len(keys)): | |
for j in range(i + 1, len(keys)): | |
solver.add(hashes[i] != hashes[j]) | |
# Injective S-Box Constraint (Makes it go MUCH slower) | |
# for i in range(256): | |
# for j in range(i + 1, 256): | |
# solver.add(sbox[i] != sbox[j]) | |
for i,k in enumerate(keys): | |
solver.add(pearson_hash(sbox, k) == hashes[i]) | |
if solver.check() != z3.sat: return None | |
model = solver.model() | |
sbox_result = [0] * 256 | |
for i in range(256): | |
sbox_result[i] = model.eval(sbox[i]).as_long() | |
return sbox_result | |
def main(): | |
keys = [] | |
while True: | |
try: keys.append(input()) | |
except EOFError: break | |
sbox = find_sbox(keys) | |
if sbox == None: return | |
kmap = [-1] * 256 | |
for i,k in enumerate(keys): | |
kmap[pearson_hash(sbox, k)] = i | |
for i in range(16): | |
for j in range(16): | |
item = sbox[16*i + j] | |
print(f'0x{item:02x}', end=',') | |
if j < 15: print(' ', end='') | |
print('') | |
print('') | |
for i in range(16): | |
for j in range(16): | |
item = kmap[16*i + j] | |
print(f'{item:3d}', end=',') | |
if j < 15: print(' ', end='') | |
print('') | |
if __name__ == "__main__": main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment