//----------------------------------------------------------------------------------------------------------------------------------------------
// 3/9/2017 Nathan Mariels
//
// to compile on command line:
//
// cc -O3 -o sha256fixedpointsmt sha256fixedpointsmt.c
//
//----------------------------------------------------------------------------------------------------------------------------------------------

const char c_infostr[] = {"                                                                                                                                                       \
 input :  block <16 words>                                                                                                                                                        \n\
          example: ./sha256fixedpointsmt 4E617468 616E4D61 7269656C 73800000 00 00 00 00 00 00 00 00 00 00 00 68                                                                  \n\n\
 output:  ivec <8 words>                                                                                                                                                          \n\
          block <16 words>                                                                                                                                                        \n\
          sha256 hash of input block starting from ivec <8 words>                                                                                                                 \n\n\
 note  :  to verify, ./sha256mt <ivec:8> block <block:16>                                                                                                                         \n\
          example:   ./sha256mt 2EC557A2 0B6E2499 0CF13E72 2CDD2309 CD4AB124 B54D3298 9FBAAA26 595767F4 4E617468 616E4D61 7269656C 73800000 00 00 00 00 00 00 00 00 00 00 00 68   \n\n\
"};

//----------------------------------------------------------------------------------------------------------------------------------------------
// include

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//----------------------------------------------------------------------------------------------------------------------------------------------
// macros

#define            loop(X,Y)               for(X=0;X<(Y);X++)
#define            fill(X,Y,Z)             for(int filltemp=0;filltemp<(Y);filltemp++) X[filltemp]=(Z)
#define            copy(X,Y,Z)             for(int copytemp=0;copytemp<(Z);copytemp++) X[copytemp]=Y[copytemp]
#define            c_char_to_hex(X)        ((((X)>='0')&&((X)<='9'))?((X)-'0'):(((((X)|32)>='a')&&(((X)|32)<='f'))?(((X)|32)-'a'+10):0))

#define            sha256_rightrotate(X,Y) (((X)>>(Y))|((X)<<(32-(Y))))
#define            sha256_rightshift(X,Y)  (((X)>>(Y)))
#define            sha256_ws0(X)           (sha256_rightrotate((X), 7)^sha256_rightrotate((X),18)^sha256_rightshift((X),3))
#define            sha256_ws1(X)           (sha256_rightrotate((X),17)^sha256_rightrotate((X),19)^sha256_rightshift((X),10))
#define            sha256_s0(X)            (sha256_rightrotate((X), 2)^sha256_rightrotate((X),13)^sha256_rightrotate((X),22))
#define            sha256_s1(X)            (sha256_rightrotate((X), 6)^sha256_rightrotate((X),11)^sha256_rightrotate((X),25))
#define            sha256_xa(W,X,Y,Z)      (((W)&(X))^((Y)&(Z)))

//----------------------------------------------------------------------------------------------------------------------------------------------
// typedef

typedef    unsigned int    uint32;

//----------------------------------------------------------------------------------------------------------------------------------------------
// const

#define            k_hash_array_size        96

const uint32         sha256_k[k_hash_array_size] = {     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,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

//----------------------------------------------------------------------------------------------------------------------------------------------
// structs

typedef struct mt_sha256_state {
    uint32        a[k_hash_array_size];
    uint32        b[k_hash_array_size];
    uint32        w[k_hash_array_size];
    uint32        pl[8];
    uint32        out[8];
} mt_sha256_state;

//----------------------------------------------------------------------------------------------------------------------------------------------
// globals


//----------------------------------------------------------------------------------------------------------------------------------------------
// prototypes

uint32 mt_cmd_line_get_hex (const char *ptr);
int main (int argc, const char * argv[]);

//----------------------------------------------------------------------------------------------------------------------------------------------
// code

int main (int argc, const char * argv[])
{
    mt_sha256_state   *hs, *h1, sha_state;
    int               i;
    
    hs = &sha_state;
    h1 = &sha_state;
    
    if (argc != 17) {
        i = 0;
        while (c_infostr[i]) putchar ((int)c_infostr[i++]);
        return 0;
    }
    i = 0;
    while (i != 16) {
        hs->w[i] = mt_cmd_line_get_hex (argv[i+1]);
        i++;
    }
    
    // hash forward
    for (i=16; i<(k_hash_array_size-8); i++) {
        hs->w[i] = hs->w[i-16] + sha256_ws0 (hs->w[i-15]) + hs->w[i-7] + sha256_ws1(hs->w[i-2]);
    }
    
    loop (i, 8) {
        hs->pl [i] = 0;
    }
    
    loop (i, 4) {
        hs->a[i] = hs->pl [3-i];
        hs->b[i] = hs->pl [7-i];
    }
    
    for (i=0; i<(k_hash_array_size-16); i++) {
        hs->b[i+4] = hs->w[i]  + sha256_k[i] + hs->a[i] + hs->b[i] + sha256_s1 (hs->b[i+3]) + (hs->b[i+3] & hs->b[i+2]) + (~hs->b[i+3] & hs->b[i+1]);
        hs->a[i+4] = hs->b[i+4] - hs->a[i] + sha256_s0 (hs->a[i+3]) + ((((hs->a[i+3] ^ hs->a[i+1]) & hs->a[i+2]) ^ (hs->a[i+1] & hs->a[i+3])));
    }
    
    loop (i, 4) {
        hs->out[i]   = hs->pl [i] + hs->a[67-i];
        hs->out[i+4] = hs->pl [i+4] + hs->b[67-i];
    }
    
    // zero offset
    loop (i, 4) {
        hs->a[64+i] = 0;
        hs->b[64+i] = 0;
    }

    // hash backward
    for (i=63; i>=0; i--) {
        hs->a[i] = hs->b[i+4] - hs->a[i+4] + sha256_s0 (hs->a[i+3]) + ((((hs->a[i+3] ^ hs->a[i+1]) & hs->a[i+2]) ^ (hs->a[i+1] & hs->a[i+3])));
        hs->b[i] = -hs->w[i] - sha256_k[i] - hs->a[i] + hs->b[i+4] - sha256_s1 (hs->b[i+3]) - (hs->b[i+3] & hs->b[i+2]) - (~hs->b[i+3] & hs->b[i+1]);
    }

    // new ivec
    loop (i, 4) {
        hs->pl[i]   = hs->a[3-i];
        hs->pl[i+4] = hs->b[3-i];
    }
    
    // hash again
    for (i=0; i<(k_hash_array_size-16); i++) {
        hs->b[i+4] = hs->w[i]  + sha256_k[i] + hs->a[i] + hs->b[i] + sha256_s1 (hs->b[i+3]) + (hs->b[i+3] & hs->b[i+2]) + (~hs->b[i+3] & hs->b[i+1]);
        hs->a[i+4] = hs->b[i+4] - hs->a[i] + sha256_s0 (hs->a[i+3]) + ((((hs->a[i+3] ^ hs->a[i+1]) & hs->a[i+2]) ^ (hs->a[i+1] & hs->a[i+3])));
    }
    
    loop (i, 4) {
        hs->out[i]   = hs->pl [i] + hs->a[67-i];
        hs->out[i+4] = hs->pl [i+4] + hs->b[67-i];
    }

    // print
    printf ("ivec:   ");
    for (i=0; i<8; i++) {
        printf ("%08X ", h1->pl[i]);
    }
    printf ("\nblock: ");
    for (i=0; i<16; i++) {
        printf (" %08X", h1->w[i]);
    }
    printf ("\nhash:  ");
    for (i=0; i<8; i++) {
        printf (" %08X", h1->out[i]);
    }
    
    printf ("\n\n");
    for (i=0; i<8; i++) {
        printf ("%08X ", h1->pl[i]);
    }
    printf (" ");
    for (i=0; i<16; i++) {
        printf (" %08X", h1->w[i]);
    }
    printf ("  ");
    for (i=0; i<8; i++) {
        printf (" %08X", h1->out[i]);
    }
    printf ("\n\n");
}



//----------------------------------------------------------------------------------------------------------------------------------------------

uint32 mt_cmd_line_get_hex (const char *ptr)
{
    int        x = 0;
    uint32    r = 0;
    
    while ((*ptr) && (x < 8)) {
        r = r << 4;
        r = r + c_char_to_hex (*ptr);
        ptr++;
        x++;
    }
    return (r);
}


//----------------------------------------------------------------------------------------------------------------------------------------------
