explore more variable changes

This commit is contained in:
2026-06-08 11:03:04 +02:00
parent 8ba3ebf1a1
commit c9957ab4ab
+22 -15
View File
@@ -11,22 +11,32 @@ def count_monomials(poly):
def apply_variable_change(model, coeffs): 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 orig_y = model.y_bits
subs = {} subs = {}
for i in range(1, 5):
for j in range(8): for r in range(5):
subs[orig_x[i][j]] = orig_y[i][j] for j in range(1, 8):
for j in range(8): subs[orig_x[r][j]] = orig_y[r][j]
new_expr = orig_y[0][j]
for i, ai in enumerate(coeffs, start=1): new_expr = orig_y[r][0]
for j, ai in enumerate(coeffs, start=1):
if ai: if ai:
new_expr = new_expr + orig_y[i][j] new_expr += orig_y[r][j]
subs[orig_x[0][j]] = new_expr subs[orig_x[r][0]] = new_expr
new_R = [] new_R = []
for i in range(8): for i in range(8):
row = [poly.subs(subs) for poly in model.R_bits[i]] row = [poly.subs(subs) for poly in model.R_bits[i]]
new_R.append(row) new_R.append(row)
return new_R return new_R
@@ -44,14 +54,13 @@ def run_exhaustive(steps: int, target_reg: int = 0, target_bit: int = -1):
bits = [target_bit] bits = [target_bit]
best_coeffs: tuple | None = None best_coeffs: tuple | None = None
best_label: str = ""
best_total: int = -1 best_total: int = -1
best_R: list | None = None 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) new_R = apply_variable_change(snapshot, coeffs)
label = "".join(map(str, 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 total = 0
for j in bits: 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: if best_total == -1 or total < best_total:
best_total = total best_total = total
best_coeffs = coeffs best_coeffs = coeffs
best_label = label
best_R = new_R best_R = new_R
print("\nBest variable change (fewest total monomials)") 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(f" Total monomials: {best_total}")
print(" Polynomial:\n") print(" Polynomial:\n")
for j in bits: for j in bits: