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

84 lines
2.7 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.
"""Keccak-256(以太坊用的哈希;与 NIST SHA3-256 的差别只在 domain padding)。
用途:FISCO 国密链的 ABI JSON 里 `selector` 是一个二元数组
`[keccak_selector, sm3_selector]`,executor 按链类型取
`selector[0]`(标准版)/ `selector[1]`(国密版)
(`bcos-executor/src/dag/Abi.cpp`)。
标准库 `hashlib` 只有 `sha3_256`(NIST SHA3,padding 是 0x06),
没有 keccak-256(以太坊,padding 是 0x01),所以这里手写一份。
"""
# ---- Keccak-f[1600] ----
_RC = [
0x0000000000000001, 0x0000000000008082, 0x800000000000808A,
0x8000000080008000, 0x000000000000808B, 0x0000000080000001,
0x8000000080008081, 0x8000000000008009, 0x000000000000008A,
0x0000000000000088, 0x0000000080008009, 0x000000008000000A,
0x000000008000808B, 0x800000000000008B, 0x8000000000008089,
0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
0x000000000000800A, 0x800000008000000A, 0x8000000080008081,
0x8000000000008080, 0x0000000080000001, 0x8000000080008008,
]
_ROT = [
[0, 36, 3, 41, 18],
[1, 44, 10, 45, 2],
[62, 6, 43, 15, 61],
[28, 55, 25, 21, 56],
[27, 20, 39, 8, 14],
]
_MASK = (1 << 64) - 1
def _rotl64(x, n):
n &= 63
if n == 0:
return x
return ((x << n) | (x >> (64 - n))) & _MASK
def _keccak_f(state):
for rnd in range(24):
# θ
c = [state[x][0] ^ state[x][1] ^ state[x][2] ^ state[x][3] ^ state[x][4] for x in range(5)]
d = [c[(x - 1) % 5] ^ _rotl64(c[(x + 1) % 5], 1) for x in range(5)]
for x in range(5):
for y in range(5):
state[x][y] ^= d[x]
# ρ + π
b = [[0] * 5 for _ in range(5)]
for x in range(5):
for y in range(5):
b[y][(2 * x + 3 * y) % 5] = _rotl64(state[x][y], _ROT[x][y])
# χ
for x in range(5):
for y in range(5):
state[x][y] = b[x][y] ^ ((~b[(x + 1) % 5][y] & _MASK) & b[(x + 2) % 5][y])
# ι
state[0][0] ^= _RC[rnd]
return state
def keccak256(data: bytes) -> bytes:
"""以太坊的 keccak-256(padding 0x01,不是 SHA3 的 0x06)。"""
rate = 136 # 1088 bit
# padding: 0x01 ... 0x80
msg = bytearray(data)
msg.append(0x01)
while len(msg) % rate != 0:
msg.append(0x00)
msg[-1] |= 0x80
state = [[0] * 5 for _ in range(5)]
for off in range(0, len(msg), rate):
block = msg[off:off + rate]
for i in range(rate // 8):
lane = int.from_bytes(block[i * 8:i * 8 + 8], "little")
state[i % 5][i // 5] ^= lane
_keccak_f(state)
out = bytearray()
for i in range(4): # 256 bit
out += state[i % 5][i // 5].to_bytes(8, "little")
return bytes(out[:32])