# @file wots_checksum.py (2023-03-16T14:29Z)
# @author David Ireland <www.di-mgt.com.au/contact>
# @copyright 2023 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_csum=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]
    #print(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_csum: print(f"csum={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
msg = wots_chain('dd84d9c7667367183c4826ce1e1d0048', True)
print(msg)
print([hex(x) for x in msg])
print(f"len={len(msg)}")