Files
admin 237e646962 feat(chain): 国密链智能合约真链实证部署/调用/读回打通
- 钉死 InvalidSignature 根因:节点 3.17.1 走 fast_sm2_verify,验签 e=SM3(ZA||dataHash)(distid=1234567812345678);旧实现签裸摘要口径不符。
- PrehashedSm2Signer 改为带 ZA 口径;compute_za 从 OpenSSL 曲线读 a/Gx/Gy,杜绝手抄 a=p-3 错写 fc 的坑。
- chain contract deploy/call/read 子命令落地,opts 支持 --arg 多值;接真链跑通部署+recordHardRule+读回+负向矩阵。
- selftest-fisco-contract.py 接入 ci.sh 第 22 条(22 自测+22 真链批,缺环境显式 SKIPPED)。
- docs/catalog 纠正 eth_ 端点与「不二次 SM3」过时口径;合约源码与 combined.json 入仓。
2026-11-08 13:19:17 +08:00

107 lines
3.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""SM3(GB/T 32905-2016)纯 Python 实现。
用途:链层判据脚本需要在**不依赖 Rust 工具链**(本机 cargo 被沙箱 SIGKILL)
的前提下重算国密链的函数选择子。
★ 本实现与 `mpc_core::sm3` 逐字节一致(已用同一批输入对拍:
"" → 1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b
"abc" → 66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0
"abcd"×4 → 639c6f6b30d93ecebd559a953ba2eb72705db7d2be82bbf32979380e02124971
)。**判据脚本绝不能自己实现一遍被测算法**,所以这里同时带
标准向量自检,错了立刻炸,不许静默通过。
"""
_SM3_IV = [
0x7380166F, 0x4914B2B9, 0x172442D7, 0xDA8A0600,
0xA96F30BC, 0x163138AA, 0xE38DEE4D, 0xB0FB0E4E,
]
def _rotl(x, n):
n &= 31
if n == 0:
return x
return ((x << n) | (x >> (32 - n))) & 0xFFFFFFFF
def _p0(x):
return x ^ _rotl(x, 9) ^ _rotl(x, 17)
def _p1(x):
return x ^ _rotl(x, 15) ^ _rotl(x, 23)
def _ff(j, x, y, z):
if j < 16:
return x ^ y ^ z
return (x & y) | (x & z) | (y & z)
def _gg(j, x, y, z):
if j < 16:
return x ^ y ^ z
return (x & y) | ((~x & 0xFFFFFFFF) & z)
def sm3(data: bytes) -> bytes:
"""SM3 摘要(32 字节)。"""
msg = bytearray(data)
ln = len(data) * 8
msg.append(0x80)
while len(msg) % 64 != 56:
msg.append(0)
msg += ln.to_bytes(8, "big")
v = list(_SM3_IV)
for off in range(0, len(msg), 64):
block = msg[off:off + 64]
w = [int.from_bytes(block[i * 4:i * 4 + 4], "big") for i in range(16)]
for j in range(16, 68):
# GB/T 32905-2016 §5.3.2:
# W[j] = P1(W[j-16] ⊕ W[j-9] ⊕ ROTL(W[j-3],15)) ⊕ ROTL(W[j-13],7) ⊕ W[j-6]
w.append(_p1(w[j - 16] ^ w[j - 9] ^ _rotl(w[j - 3], 15))
^ _rotl(w[j - 13], 7) ^ w[j - 6])
# GB/T 32905-2016 §5.3.3: W1[j] = W[j] ⊕ W[j+4](**+4**)。
# 写成 w[j-4] 时 j=0 会命中 Python 负索引(w[-4] == w[64]),
# 摘要与标准向量全不符却不报错 —— 这种 bug 最难查。
w1 = [w[j] ^ w[j + 4] for j in range(64)]
a, b, c, d, e, f, g, h = v
for j in range(64):
t = 0x79CC4519 if j < 16 else 0x7A879D8A
# GB/T 32905-2016 §5.3.1: SS1 = ROTL((a<<<12) + e + ROTL(T_j, j mod 32), 7)
tj = _rotl(t, j % 32)
ss1 = _rotl((_rotl(a, 12) + e + tj) & 0xFFFFFFFF, 7)
ss2 = ss1 ^ _rotl(a, 12)
tt1 = (_ff(j, a, b, c) + d + ss2 + w1[j]) & 0xFFFFFFFF
tt2 = (_gg(j, e, f, g) + h + ss1 + w[j]) & 0xFFFFFFFF
d = c
c = _rotl(b, 9)
b = a
a = tt1
h = g
g = _rotl(f, 19)
f = e
e = _p0(tt2)
v = [x ^ y for x, y in zip(v, [a, b, c, d, e, f, g, h])]
return b"".join(x.to_bytes(4, "big") for x in v)
# GB/T 32905-2016 附录 A / 与 mpc_core::sm3 对拍过的向量。
SM3_VECTORS = [
(b"", "1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b"),
(b"abc", "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0"),
(b"abcd" * 4, "639c6f6b30d93ecebd559a953ba2eb72705db7d2be82bbf32979380e02124971"),
]
def self_check() -> None:
"""标准向量自检。**不通过就抛异常**,绝不容忍「算错了但没人发现」。"""
for data, want in SM3_VECTORS:
got = sm3(data).hex()
if got != want:
raise AssertionError(
f"SM3 实现与标准向量不符:input={data!r} got={got} want={want}"
)