using UdonSharp;

// Hash commitments to each seat's hole cards, published at the deal so a new table owner can check
// the cards players report back after the dealer leaves. Udon has no System.Security.Cryptography.
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class HoldemCommit : UdonSharpBehaviour
{
    // First 8 bytes of SHA-256 over the fields, big-endian
    public static long Commit(int handId, int seat, int card0, int card1, int saltA, int saltB)
    {
        byte[] msg = new byte[24];
        WriteInt(msg, 0, handId);
        WriteInt(msg, 4, seat);
        WriteInt(msg, 8, card0);
        WriteInt(msg, 12, card1);
        WriteInt(msg, 16, saltA);
        WriteInt(msg, 20, saltB);
        uint[] h = Sha256(msg);
        long high = h[0];
        long low = h[1];
        return (high << 32) | low;
    }

    // Udon casts are checked, so every narrowing cast is masked into range first
    private static void WriteInt(byte[] buf, int at, int value)
    {
        buf[at] = (byte)((value >> 24) & 255);
        buf[at + 1] = (byte)((value >> 16) & 255);
        buf[at + 2] = (byte)((value >> 8) & 255);
        buf[at + 3] = (byte)(value & 255);
    }

    // SHA-256 digest as eight big-endian words
    public static uint[] Sha256(byte[] message)
    {
        uint[] k = new uint[]
        {
            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
        };
        uint[] h = new uint[]
        {
            0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
        };

        // Pad: 0x80, zeros, then the bit length in the last 8 bytes of a 64-byte block
        int len = message.Length;
        int padded = ((len + 8) / 64 + 1) * 64;
        byte[] data = new byte[padded];
        for (int i = 0; i < len; i++) data[i] = message[i];
        data[len] = 0x80;
        long bits = (long)len * 8;
        for (int i = 0; i < 8; i++) data[padded - 1 - i] = (byte)((bits >> (8 * i)) & 255);

        uint[] w = new uint[64];
        for (int block = 0; block < padded; block += 64)
        {
            for (int t = 0; t < 16; t++)
            {
                int at = block + t * 4;
                w[t] = ((uint)data[at] << 24) | ((uint)data[at + 1] << 16) | ((uint)data[at + 2] << 8) | data[at + 3];
            }
            for (int t = 16; t < 64; t++)
            {
                uint w15 = w[t - 15];
                uint w2 = w[t - 2];
                uint s0 = Rotr(w15, 7) ^ Rotr(w15, 18) ^ (w15 >> 3);
                uint s1 = Rotr(w2, 17) ^ Rotr(w2, 19) ^ (w2 >> 10);
                w[t] = w[t - 16] + s0 + w[t - 7] + s1;
            }

            uint a = h[0], b = h[1], c = h[2], d = h[3], e = h[4], f = h[5], g = h[6], hh = h[7];
            for (int t = 0; t < 64; t++)
            {
                uint sum1 = Rotr(e, 6) ^ Rotr(e, 11) ^ Rotr(e, 25);
                uint ch = (e & f) ^ (~e & g);
                uint temp1 = hh + sum1 + ch + k[t] + w[t];
                uint sum0 = Rotr(a, 2) ^ Rotr(a, 13) ^ Rotr(a, 22);
                uint maj = (a & b) ^ (a & c) ^ (b & c);
                uint temp2 = sum0 + maj;
                hh = g;
                g = f;
                f = e;
                e = d + temp1;
                d = c;
                c = b;
                b = a;
                a = temp1 + temp2;
            }
            h[0] += a; h[1] += b; h[2] += c; h[3] += d;
            h[4] += e; h[5] += f; h[6] += g; h[7] += hh;
        }
        return h;
    }

    private static uint Rotr(uint x, int n)
    {
        return (x >> n) | (x << (32 - n));
    }
}
