# @file wots_checksum.py
# @version 1.1 (2026-01-16T14:29Z)
# @author David Ireland <https://di-mgt.com.au/contact>
# @copyright 2023-26 DI Management Services Pty Ltd
# @license Apache-2.0

"""Split 16-byte message into 4-bit blocks then append 3 x 4-bit checksum."""

w = 16
def wots_chain(msghex, show_debug=False):
    # Split hex string into list of 4-bit nibbles
    # (Cheat: we can just split hex string into separate digits)
    msg = [int(x, 16) for x in msghex]
    if show_debug: print(f"Input blocks={msg}")
    # Compute csum
    csum = 0
    for i in range(len(msg)):
        csum += int(w - 1 - msg[i]) 
    csum &= 0xfff   # truncate to 12 bits
    if show_debug: print(f"checksum={csum:03x}")
    msg.append((csum >> 8) & 0xF)
    msg.append((csum >> 4) & 0xF)
    msg.append((csum >> 0) & 0xF)
    return msg

# Input FORS public key to first WOTS signature
input = '33af163817cd6c2bea881ddf7d2b89ab'
print(f"Input={input}")
msg = wots_chain(input, show_debug=True)
print("Final blocks =", [hex(x) for x in msg])
print(f"len={len(msg)}")
print("m =", msg)