import t, c


# ==============================================
# SHA256 哈希算法
# ==============================================
SHA256_BLOCK_LEN:  t.CDefine = 64
SHA256_DIGEST_LEN: t.CDefine = 32

# SHA256 常量K
sha256_K: t.CArray[t.CUInt32T, 64] = [
    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
]

# SHA256 辅助函数
def  s0(x: t.CUInt32T) -> t.CUInt32T: return ((x >>  7) | (x << 25)) ^ ((x >> 18) | (x << 14)) ^ ((x >>  3))
def  s1(x: t.CUInt32T) -> t.CUInt32T: return ((x >> 17) | (x << 15)) ^ ((x >> 19) | (x << 13)) ^ ((x >> 10))
def  S0(x: t.CUInt32T) -> t.CUInt32T: return ((x >>  2) | (x << 30)) ^ ((x >> 13) | (x << 19)) ^ ((x >> 22) | (x << 10))
def  S1(x: t.CUInt32T) -> t.CUInt32T: return ((x >>  6) | (x << 26)) ^ ((x >> 11) | (x << 21)) ^ ((x >> 25) | (x <<  7))
def  Ch(x: t.CUInt32T, y: t.CUInt32T, z: t.CUInt32T) -> t.CUInt32T: return (x & y) ^ (~x & z)
def Maj(x: t.CUInt32T, y: t.CUInt32T, z: t.CUInt32T) -> t.CUInt32T: return (x & y) ^ (x & z) ^ (y & z)

# SHA256 上下文
@t.Object
class sha256:
    state: t.CArray[t.CUInt32T, 8]
    count: t.CUInt64T
    buf:   t.CArray[t.CUInt8T, SHA256_BLOCK_LEN]
    def __init__(self):
        memset(self, 0, sha256.__sizeof__())
        self.state[0] = 0x6a09e667
        self.state[1] = 0xbb67ae85
        self.state[2] = 0x3c6ef372
        self.state[3] = 0xa54ff53a
        self.state[4] = 0x510e527f
        self.state[5] = 0x9b05688c
        self.state[6] = 0x1f83d9ab
        self.state[7] = 0x5be0cd19
        self.count = 0
        # memset(self.buf, 0, SHA256_BLOCK_LEN)
    # 单分组压缩
    def transform(self, block: t.CUInt8T | t.CPtr):
        w: t.CArray[t.CUInt32T, 64]
        for i in range(16):
            w[i] = ((t.CUInt32T(block[i * 4 + 0]) << 24) |
                    (t.CUInt32T(block[i * 4 + 1]) << 16) |
                    (t.CUInt32T(block[i * 4 + 2]) << 8 ) |
                    (t.CUInt32T(block[i * 4 + 3]) << 0 ))
        # 扩展64字
        for i in range(16, 64):
            w[i] = w[i-16] + s0(w[i-15]) + w[i-7] + s1(w[i-2])
        a: t.CUInt32T = self.state[0]
        b: t.CUInt32T = self.state[1]
        c: t.CUInt32T = self.state[2]
        d: t.CUInt32T = self.state[3]
        e: t.CUInt32T = self.state[4]
        f: t.CUInt32T = self.state[5]
        g: t.CUInt32T = self.state[6]
        h: t.CUInt32T = self.state[7]
        t1: t.CUInt32T
        t2: t.CUInt32T
        # 64步迭代
        for i in range(64):
            t1 = h + S1(e) + Ch(e,f,g) + sha256_K[i] + w[i]
            t2 = S0(a) + Maj(a,b,c)
            h = g; g = f; f = e
            e = d + t1
            d = c; c = b; b = a
            a = t1 + t2
        # 累加状态
        self.state[0] += a
        self.state[1] += b
        self.state[2] += c
        self.state[3] += d
        self.state[4] += e
        self.state[5] += f
        self.state[6] += g
        self.state[7] += h
    def update(self, s: str):
        data: t.CUInt8T | t.CPtr = t.CUInt8T(s, t.CPtr)
        length: t.CSizeT = len(s)
        idx: t.CSizeT = self.count % SHA256_BLOCK_LEN
        self.count += length
        if idx > 0:
            left: t.CSizeT = SHA256_BLOCK_LEN - idx
            if length >= left:
                memcpy(self.buf + idx, data, left)
                self.transform(self.buf)
                data += left
                length -= left
                idx = 0
        while length >= SHA256_BLOCK_LEN:
            self.transform(data)
            data += SHA256_BLOCK_LEN
            length -= SHA256_BLOCK_LEN
        if length > 0:
            memcpy(self.buf + idx, data, length)
    def final(self, out: t.CArray[t.CUInt8T, SHA256_DIGEST_LEN]):
        idx: t.CSizeT = self.count % SHA256_BLOCK_LEN
        self.buf[idx] = 0x80
        idx += 1
        if idx > SHA256_BLOCK_LEN - 8:
            memset(self.buf + idx, 0, SHA256_BLOCK_LEN - idx)
            self.transform(self.buf)
            idx = 0
        memset(self.buf + idx, 0, SHA256_BLOCK_LEN - 8 - idx)
        bits: t.CUInt64T = self.count << 3
        for i in range(8):
            self.buf[SHA256_BLOCK_LEN-8+i] = (bits >> (56 - i*8)) & 0xFF
        self.transform(self.buf)
        # 大端输出32字节摘要
        for i in range(8):
            out[i * 4 + 0] = (self.state[i] >> 24) & 0xFF
            out[i * 4 + 1] = (self.state[i] >> 16) & 0xFF
            out[i * 4 + 2] = (self.state[i] >>  8) & 0xFF
            out[i * 4 + 3] = (self.state[i] >>  0) & 0xFF
