# @file sig_3_message_to_indices.py
# @version 1.1.0 (2026-02-15T08:23Z)
# @author David Ireland <https://di-mgt.com.au/contact>
# @copyright 2023-26 DI Management Services Pty Ltd
# @license Apache-2.0

"""Interpret 25-byte mhash as 33 * 6-bit unsigned integers
using base_2^b function Algorithm 4 FIPS.205
"""

def base_2_b(X, b, out_len):
    # Algorithm 4: base_2^b(X, b, out_len)
    # Compute the base 2^b representation of X
    # INPUT:
    # Byte string X, integer b, output length out_len.
    two_to_b = 1 << b  # 2^b
    baseb = [0] * out_len
    i = 0
    bits = 0
    total = 0

    for out in range(out_len):
        while bits < b:
            total = (total << 8) + X[i]
            i = i + 1
            bits = bits + 8
        bits = bits - b
        baseb[out] = (total >> bits) & (two_to_b - 1)  # mod 2^b
        total &= (two_to_b - 1)
    return baseb

X = bytes.fromhex('cafc639d5e550807f84bae9f7eb8da2077cd579fd484522072')
print(f"X={X.hex()}")
indicies = base_2_b(X, 6, 33)
print("indicies:\n", [m for m in indicies])
# [50, 47, 49, 35, 39, 21, 57, 21, 2, 0, 31,
# 56, 18, 58, 58, 31, 31, 43, 35, 26, 8, 7,
# 31, 13, 21, 57, 63, 20, 33, 5, 8, 32, 28]