6616680fc6
Free check (DeepSeek v4.1 Flash scorecard) and deep report (Opus 5.5) over a generalised evidence pack from the read-only chain tools; fact gate blocks any address, tx or number not in the evidence; MySQL store; hub sign-in, sponsor line recorder, vouchers, ad slots in iframes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
54 lines
2.4 KiB
Python
54 lines
2.4 KiB
Python
"""Pure-Python Keccak-256 (the Ethereum variant, not NIST SHA3), for function selectors and event topics from a verified ABI.
|
|
Small inputs only (signatures), so speed does not matter."""
|
|
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]]
|
|
M = (1 << 64) - 1
|
|
|
|
|
|
def _rol(x, n): return ((x << n) | (x >> (64 - n))) & M if n else x
|
|
|
|
|
|
def _f(A):
|
|
for rnd in range(24):
|
|
C = [A[x][0] ^ A[x][1] ^ A[x][2] ^ A[x][3] ^ A[x][4] for x in range(5)]
|
|
D = [C[(x - 1) % 5] ^ _rol(C[(x + 1) % 5], 1) for x in range(5)]
|
|
A = [[A[x][y] ^ D[x] for y in range(5)] for x in range(5)]
|
|
B = [[0] * 5 for _ in range(5)]
|
|
for x in range(5):
|
|
for y in range(5):
|
|
B[y][(2 * x + 3 * y) % 5] = _rol(A[x][y], ROT[x][y])
|
|
A = [[B[x][y] ^ ((~B[(x + 1) % 5][y]) & B[(x + 2) % 5][y]) for y in range(5)] for x in range(5)]
|
|
A[0][0] ^= RC[rnd]
|
|
return A
|
|
|
|
|
|
def keccak256(data: bytes) -> bytes:
|
|
rate = 136
|
|
p = bytearray(data) + b'\x01' + b'\x00' * ((rate - (len(data) + 1) % rate) % rate)
|
|
p[-1] |= 0x80
|
|
A = [[0] * 5 for _ in range(5)]
|
|
for off in range(0, len(p), rate):
|
|
blk = p[off:off + rate]
|
|
for i in range(rate // 8):
|
|
x, y = i % 5, i // 5
|
|
A[x][y] ^= int.from_bytes(blk[8 * i:8 * i + 8], 'little')
|
|
A = _f(A)
|
|
out = b''
|
|
for i in range(4):
|
|
x, y = i % 5, i // 5
|
|
out += A[x][y].to_bytes(8, 'little')
|
|
return out
|
|
|
|
|
|
def selector(sig): return '0x' + keccak256(sig.encode()).hex()[:8]
|
|
def topic(sig): return '0x' + keccak256(sig.encode()).hex()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
assert selector('transfer(address,uint256)') == '0xa9059cbb', selector('transfer(address,uint256)')
|
|
assert topic('Transfer(address,address,uint256)') == '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
|
|
print('ok')
|