From 4c027a808292389ded9970f7a2c03ecc96b3d5df Mon Sep 17 00:00:00 2001 From: StarCells Date: Sun, 13 Sep 2026 04:27:49 +0000 Subject: [PATCH] =?UTF-8?q?raw=5Finc=20=E5=AE=8C=E6=95=B4=E4=BF=AE?= =?UTF-8?q?=E6=B3=95=20(C):=20=E4=BD=93=E5=86=85=20%3/%4=20=E5=AF=B9?= =?UTF-8?q?=E8=B0=83=20+=20=E8=A1=A5=20bit=5Fraw=5Fadd=20=E5=BC=8F?= =?UTF-8?q?=E5=85=A5=E5=8F=A3=E6=B8=85=E9=9B=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - bit_raw_inc 不清 S/Co/tmp 的 LSB 就进比特循环;入口 S 残留时 A 被污染(A_out = A ^ S_in), raw_add 因为每比特开头清 S/Co/buf 而免疫(对照实测) - 修法 A(对调调用实参)/B(对调宏体)只在 S 入口干净时正确(2/10),C = B + 三块清零 → 10/10 脏入口 + 22/22 干净 - tools/stdmac_inc_state_probe.py: 入口脏值扫描 + raw_add 对照;tests/inc_fix_probe_{B,C}.r8asm.frag --- tests/inc_fix_probe_B.r8asm.frag | 79 ++++++++++++++++++++++++++ tests/inc_fix_probe_C.r8asm.frag | 96 ++++++++++++++++++++++++++++++++ tools/stdmac_inc_fixB_probe.py | 44 +++++++++++++++ tools/stdmac_inc_state_probe.py | 64 +++++++++++++++++++++ 4 files changed, 283 insertions(+) create mode 100644 tests/inc_fix_probe_B.r8asm.frag create mode 100644 tests/inc_fix_probe_C.r8asm.frag create mode 100644 tools/stdmac_inc_fixB_probe.py create mode 100644 tools/stdmac_inc_state_probe.py diff --git a/tests/inc_fix_probe_B.r8asm.frag b/tests/inc_fix_probe_B.r8asm.frag new file mode 100644 index 0000000..0aff263 --- /dev/null +++ b/tests/inc_fix_probe_B.r8asm.frag @@ -0,0 +1,79 @@ +%macro bit_raw_inc_B 5 + +.anchor before_bit_raw_inc + +.tp %2 +biz + .tp %1 + flb + .tp %3 + flb + .tp %2 + flb +rnz + +.tp %1 +biz + .tp %4 + set_1 + .tp %5 + set_1 + .tp %1 + flb +rnz + +.tp %3 +biz + .tp %1 + set_1 + .tp %2 + set_1 + .tp %4 + set_1 + .tp %3 + flb +rnz + +.tp %5 +biz + .tp %1 + flb + .tp %4 + flb + .tp %3 + flb + .tp %5 + flb +rnz + +.tp before_bit_raw_inc + +%endmacro + +; raw_inc_B: 调用与链式 glue 与现役 raw_inc 完全相同(只有体内 %3/%4 已对调) +%macro raw_inc_B 5 +.anchor before_raw_inc_B + +.tp %2 +bit_copy %5 %4 +.tp %5 +ror + +%rep _CELL_SIZE + bit_raw_inc_B %1 %2 %3 %4 %5 + .tp %4 + clb_bit_mov %2 + .tp %1 + ror + .tp %3 + ror +%endrep + +.tp %2 +clb_bit_mov %4 +.tp %5 +rol +clb_bit_mov %2 + +.tp before_raw_inc_B +%endmacro diff --git a/tests/inc_fix_probe_C.r8asm.frag b/tests/inc_fix_probe_C.r8asm.frag new file mode 100644 index 0000000..03d7c90 --- /dev/null +++ b/tests/inc_fix_probe_C.r8asm.frag @@ -0,0 +1,96 @@ +%macro bit_raw_inc_C 5 +.anchor before_bit_raw_inc_C + +.tp %3 +biz + flb +rnz + +.tp %4 +biz + flb +rnz + +.tp %5 +biz + flb +rnz + +.anchor before_bit_raw_inc + +.tp %2 +biz + .tp %1 + flb + .tp %3 + flb + .tp %2 + flb +rnz + +.tp %1 +biz + .tp %4 + set_1 + .tp %5 + set_1 + .tp %1 + flb +rnz + +.tp %3 +biz + .tp %1 + set_1 + .tp %2 + set_1 + .tp %4 + set_1 + .tp %3 + flb +rnz + +.tp %5 +biz + .tp %1 + flb + .tp %4 + flb + .tp %3 + flb + .tp %5 + flb +rnz + +.tp before_bit_raw_inc + + +.tp before_bit_raw_inc_C +%endmacro + +%macro raw_inc_C 5 +.anchor before_raw_inc_C + +.tp %2 +bit_copy %5 %4 +.tp %5 +ror + +%rep _CELL_SIZE + bit_raw_inc_C %1 %2 %3 %4 %5 + .tp %4 + clb_bit_mov %2 + .tp %1 + ror + .tp %3 + ror +%endrep + +.tp %2 +clb_bit_mov %4 +.tp %5 +rol +clb_bit_mov %2 + +.tp before_raw_inc_C +%endmacro diff --git a/tools/stdmac_inc_fixB_probe.py b/tools/stdmac_inc_fixB_probe.py new file mode 100644 index 0000000..0deb339 --- /dev/null +++ b/tools/stdmac_inc_fixB_probe.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +"""stdmac_inc_fixB_probe.py — raw_inc 两种修法对拍(A=改调用实参 / B=改宏体 %3/%4)。""" +import itertools +import os +import sys + +HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.join(HERE, "tools")) +from stdmac_matrix import PRELUDE, build_tools, run_case, set_cell # noqa: E402 + +build_tools() +FRAG = open(os.path.join(HERE, "tests", "inc_fix_probe.r8asm.frag")).read() +FRAG += open(os.path.join(HERE, "tests", "inc_fix_probe_B.r8asm.frag")).read() + +VALS = [0, 1, 2, 3, 5, 126, 127, 128, 170, 254, 255] +VARIANTS = (("现役 raw_inc", "raw_inc A Ci S Co tmp"), + ("A: 换调用实参", "raw_inc_fix A Ci S Co tmp"), + ("B: 换宏体 %3/%4", "raw_inc_B A Ci S Co tmp")) + +for variant, call in VARIANTS: + bad = [] + for a, cin in itertools.product(VALS, [0, 1]): + src = PRELUDE + FRAG + set_cell("A", a) + set_cell("Ci", cin) + src += "%s\noutput A\noutput Ci\noutput S\noutput Co\n" % call + got = run_case(src) + want = [a, cin, (a + cin) & 0xFF, (a + cin) >> 8] + if got != want: + bad.append((a, cin, want, got)) + chain = [] + p = call.split() # [name, A, Ci, S, Co, tmp] + call2 = " ".join([p[0], p[3], p[2], "D", p[4], p[5]]) # 第二级:把上一级 S 当输入,输出到独立 cell D + for a in (126, 254, 127): + src = PRELUDE + FRAG + set_cell("A", a) + set_cell("Ci", 1) + ".var D 10\n.tp D\n.xor 0\n" + src += "%s\n" % call + "output S\noutput Co\n" + src += "%s\n" % call2 + "output D\noutput Co\n" + got = run_case(src) + want = [a + 1, 0, (a + 2) & 0xFF, (a + 2) >> 8] + if got != want: + chain.append((a, want, got)) + print("%-18s 单次失败 %2d / %d 链式失败 %d / 3" % (variant, len(bad), len(VALS) * 2, len(chain))) + for a, cin, want, got in bad[:3]: + print(" 单次 A=%-4d Ci=%d want=%s got=%s" % (a, cin, want, got)) + for a, want, got in chain: + print(" 链式 A=%d want=%s got=%s" % (a, want, got)) diff --git a/tools/stdmac_inc_state_probe.py b/tools/stdmac_inc_state_probe.py new file mode 100644 index 0000000..efd6976 --- /dev/null +++ b/tools/stdmac_inc_state_probe.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +"""stdmac_inc_state_probe.py — 入口状态敏感度:raw_add 对照 + raw_inc 三种修法。 + +关键问题:bit_raw_add 每个比特开头显式清 S/Co/buf 的 LSB;bit_raw_inc 一个都不清。 +入口 S/Co/tmp 残留时会不会污染 A? +""" +import itertools +import os +import sys + +HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.join(HERE, "tools")) +from stdmac_matrix import PRELUDE, build_tools, run_case, set_cell # noqa: E402 + +build_tools() +FRAG = "".join(open(os.path.join(HERE, "tests", f)).read() for f in + ("inc_fix_probe.r8asm.frag", "inc_fix_probe_B.r8asm.frag", "inc_fix_probe_C.r8asm.frag")) + +DIRTY = (0, 1, 117, 127, 255) + + +def case(macro, call, a=126, cin=1, s_in=0, co_in=0): + src = PRELUDE + FRAG + set_cell("A", a) + set_cell("Ci", cin) + src += ".tp S\n.xor %d\n.tp Co\n.xor %d\n" % (s_in, co_in) + src += "%s\noutput A\noutput Ci\noutput S\noutput Co\n" % call + return run_case(src) + + +print("=== 对照:raw_add 在 S/Co 入口脏值下的行为(output A,B,Ci,S,Co) ===") +for s_in, co_in in ((0, 0), (117, 0), (255, 1)): + src = PRELUDE + FRAG + set_cell("A", 126) + set_cell("B", 11) + set_cell("Ci", 1) + src += ".tp S\n.xor %d\n.tp Co\n.xor %d\n" % (s_in, co_in) + src += "raw_add A B Ci S Co tmp\noutput A\noutput B\noutput Ci\noutput S\noutput Co\n" + got = run_case(src) + want = [126, 11, 1, 138, 0] + print(" S_in=%-4d Co_in=%d -> %-26s %s" % (s_in, co_in, got, "" if got == want else "<== 错")) + +print("\n=== raw_inc 三修法:S 入口脏值扫描(期望 A=126 Ci=1 S=127 Co=0) ===") +VARIANTS = (("现役 raw_inc", "raw_inc A Ci S Co tmp"), + ("A 换实参", "raw_inc_fix A Ci S Co tmp"), + ("B 换宏体", "raw_inc_B A Ci S Co tmp"), + ("C 换宏体+入口清零", "raw_inc_C A Ci S Co tmp")) +for name, call in VARIANTS: + rows = [] + for s_in, co_in in itertools.product(DIRTY, (0, 1)): + got = case(name, call, s_in=s_in, co_in=co_in) + rows.append((s_in, co_in, got, got == [126, 1, 127, 0])) + ok = sum(1 for r in rows if r[3]) + print(" %-16s 通过 %2d / %d" % (name, ok, len(rows))) + for s_in, co_in, got, good in rows: + if not good: + print(" S_in=%-4d Co_in=%d -> %s" % (s_in, co_in, got)) + +print("\n=== C 变体:干净入口下的全表复核 ===") +VALS = [0, 1, 2, 3, 5, 126, 127, 128, 170, 254, 255] +bad = [] +for a, cin in itertools.product(VALS, [0, 1]): + got = case("C", "raw_inc_C A Ci S Co tmp", a=a, cin=cin, s_in=0, co_in=0) + want = [a, cin, (a + cin) & 0xFF, (a + cin) >> 8] + if got != want: + bad.append((a, cin, want, got)) +print(" raw_inc_C 失败 %d / %d" % (len(bad), len(VALS) * 2)) +for a, cin, want, got in bad[:5]: + print(" A=%-4d Ci=%d want=%s got=%s" % (a, cin, want, got))