From c9957ab4ab38fcfc86d82c1286b748087335f835 Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Mon, 8 Jun 2026 11:03:04 +0200 Subject: [PATCH] explore more variable changes --- src/tea3/variable_search.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/tea3/variable_search.py b/src/tea3/variable_search.py index ecc2216..f641a49 100644 --- a/src/tea3/variable_search.py +++ b/src/tea3/variable_search.py @@ -11,22 +11,32 @@ def count_monomials(poly): def apply_variable_change(model, coeffs): - orig_x = [list(model.v[i * 8 : (i + 1) * 8]) for i in range(5)] + """ + Variable change inside each 8-bit register: + x_{r,0} = y_{r,0} + a1*y_{r,1} + ... + a7*y_{r,7} + x_{r,j} = y_{r,j} for j = 1..7 + for every register r in {0,...,4}. + """ + orig_x = [list(model.v[r * 8 : (r + 1) * 8]) for r in range(5)] orig_y = model.y_bits + subs = {} - for i in range(1, 5): - for j in range(8): - subs[orig_x[i][j]] = orig_y[i][j] - for j in range(8): - new_expr = orig_y[0][j] - for i, ai in enumerate(coeffs, start=1): + + for r in range(5): + for j in range(1, 8): + subs[orig_x[r][j]] = orig_y[r][j] + + new_expr = orig_y[r][0] + for j, ai in enumerate(coeffs, start=1): if ai: - new_expr = new_expr + orig_y[i][j] - subs[orig_x[0][j]] = new_expr + new_expr += orig_y[r][j] + subs[orig_x[r][0]] = new_expr + new_R = [] for i in range(8): row = [poly.subs(subs) for poly in model.R_bits[i]] new_R.append(row) + return new_R @@ -44,14 +54,13 @@ def run_exhaustive(steps: int, target_reg: int = 0, target_bit: int = -1): bits = [target_bit] best_coeffs: tuple | None = None - best_label: str = "" best_total: int = -1 best_R: list | None = None - for idx, coeffs in enumerate(iproduct([0, 1], repeat=4)): + for idx, coeffs in enumerate(iproduct([0, 1], repeat=7)): new_R = apply_variable_change(snapshot, coeffs) label = "".join(map(str, coeffs)) - print(f"\n[{idx:02d}] (a1,a2,a3,a4) = {label}") + print(f"\n[{idx:03d}] (a1,a2,a3,a4,a5,a6,a7) = {label}") total = 0 for j in bits: @@ -63,12 +72,10 @@ def run_exhaustive(steps: int, target_reg: int = 0, target_bit: int = -1): if best_total == -1 or total < best_total: best_total = total best_coeffs = coeffs - best_label = label best_R = new_R - print("\nBest variable change (fewest total monomials)") - print(f" (a1,a2,a3,a4) = ({', '.join(map(str, best_coeffs))})") + print(f" (a1,a2,a3,a4,a5,a6,a7) = ({', '.join(map(str, best_coeffs))})") print(f" Total monomials: {best_total}") print(" Polynomial:\n") for j in bits: