Skip to content

Instantly share code, notes, and snippets.

@Swizzzer
Last active June 30, 2025 17:51
Show Gist options
  • Select an option

  • Save Swizzzer/817fc5fff28f07e857b58d4d5a0c7ebb to your computer and use it in GitHub Desktop.

Select an option

Save Swizzzer/817fc5fff28f07e857b58d4d5a0c7ebb to your computer and use it in GitHub Desktop.
Simple script to determine if an S-Box is linear
from sage.crypto.boolean_function import BooleanFunction
def is_linear_sbox(sbox: list[int]) -> bool:
input_size = (len(sbox) - 1).bit_length()
output_size = max(sbox).bit_length()
print(f"[*] Input bits: {input_size}, Output bits: {output_size}")
is_linear = True
for bit_index in range(output_size):
truth_table = [(sbox[i] >> bit_index) & 1 for i in range(2**input_size)]
f = BooleanFunction(truth_table)
deg = f.algebraic_degree()
print(f"[+] The {bit_index} bit output function's algebra degree: {deg}")
if deg > 1:
is_linear = False
break
return is_linear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment