char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
39be55c97
char/aliceai-t5-moe
1/* Crypto/Sha256.c -- SHA-256 Hash 22010-06-11 : Igor Pavlov : Public domain 3This code is based on public domain code from Wei Dai's Crypto++ library. */ 4 5#include "rotate-bits/rotate-bits.h" 6#include "sha256.h" 7 8/* define it for speed optimization */ 9#define _SHA256_UNROLL 10#define _SHA256_UNROLL2 11 12void 13sha256_init (sha256_t * p ) 14{ 15p -> state [0 ]= 0x6a09e667 ; 16p -> state [1 ]= 0xbb67ae85 ; 17p -> state [2 ]= 0x3c6ef372 ; 18p -> state [3 ]= 0xa54ff53a ; 19p -> state [4 ]= 0x510e527f ; 20p -> state [5 ]= 0x9b05688c ; 21p -> state [6 ]= 0x1f83d9ab ; 22p -> state [7 ]= 0x5be0cd19 ; 23p -> count = 0 ; 24} 25 26#define S0 (x ) (ROTR32(x, 2) ^ ROTR32(x,13) ^ ROTR32(x, 22)) 27#define S1 (x ) (ROTR32(x, 6) ^ ROTR32(x,11) ^ ROTR32(x, 25)) 28#define s0 (x ) (ROTR32(x, 7) ^ ROTR32(x,18) ^ (x >> 3)) 29#define s1 (x ) (ROTR32(x,17) ^ ROTR32(x,19) ^ (x >> 10)) 30 31#define blk0 (i ) (W[i] = data[i]) 32#define blk2 (i ) (W[i&15] += s1(W[(i-2)&15]) + W[(i-7)&15] + s0(W[(i-15)&15])) 33 34#define Ch (x ,y ,z ) (z^(x&(y^z))) 35#define Maj (x ,y ,z ) ((x&y)|(z&(x|y))) 36 37#define a (i ) T[(0-(i))&7] 38#define b (i ) T[(1-(i))&7] 39#define c (i ) T[(2-(i))&7] 40#define d (i ) T[(3-(i))&7] 41#define e (i ) T[(4-(i))&7] 42#define f (i ) T[(5-(i))&7] 43#define g (i ) T[(6-(i))&7] 44#define h (i ) T[(7-(i))&7] 45 46 47#ifdef _SHA256_UNROLL2 48 49#define R (a ,b ,c ,d ,e ,f ,g ,h ,i ) h += S1(e) + Ch(e,f,g) + K[i+j] + (j?blk2(i):blk0(i));\ 50 d += h; h += S0(a) + Maj(a, b, c) 51 52#define RX_8 (i ) \ 53 R(a,b,c,d,e,f,g,h, i); \ 54 R(h,a,b,c,d,e,f,g, (i+1)); \ 55 R(g,h,a,b,c,d,e,f, (i+2)); \ 56 R(f,g,h,a,b,c,d,e, (i+3)); \ 57 R(e,f,g,h,a,b,c,d, (i+4)); \ 58 R(d,e,f,g,h,a,b,c, (i+5)); \ 59 R(c,d,e,f,g,h,a,b, (i+6)); \ 60 R(b,c,d,e,f,g,h,a, (i+7)) 61 62#else 63 64#define R (i ) h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[i+j] + (j?blk2(i):blk0(i));\ 65 d(i) += h(i); h(i) += S0(a(i)) + Maj(a(i), b(i), c(i)) 66 67#ifdef _SHA256_UNROLL 68 69#define RX_8 (i ) R(i+0); R(i+1); R(i+2); R(i+3); R(i+4); R(i+5); R(i+6); R(i+7); 70 71#endif 72 73#endif 74 75static const uint32_t K [64 ]= { 760x428a2f98 ,0x71374491 ,0xb5c0fbcf ,0xe9b5dba5 , 770x3956c25b ,0x59f111f1 ,0x923f82a4 ,0xab1c5ed5 , 780xd807aa98 ,0x12835b01 ,0x243185be ,0x550c7dc3 , 790x72be5d74 ,0x80deb1fe ,0x9bdc06a7 ,0xc19bf174 , 800xe49b69c1 ,0xefbe4786 ,0x0fc19dc6 ,0x240ca1cc , 810x2de92c6f ,0x4a7484aa ,0x5cb0a9dc ,0x76f988da , 820x983e5152 ,0xa831c66d ,0xb00327c8 ,0xbf597fc7 , 830xc6e00bf3 ,0xd5a79147 ,0x06ca6351 ,0x14292967 , 840x27b70a85 ,0x2e1b2138 ,0x4d2c6dfc ,0x53380d13 , 850x650a7354 ,0x766a0abb ,0x81c2c92e ,0x92722c85 , 860xa2bfe8a1 ,0xa81a664b ,0xc24b8b70 ,0xc76c51a3 , 870xd192e819 ,0xd6990624 ,0xf40e3585 ,0x106aa070 , 880x19a4c116 ,0x1e376c08 ,0x2748774c ,0x34b0bcb5 , 890x391c0cb3 ,0x4ed8aa4a ,0x5b9cca4f ,0x682e6ff3 , 900x748f82ee ,0x78a5636f ,0x84c87814 ,0x8cc70208 , 910x90befffa ,0xa4506ceb ,0xbef9a3f7 ,0xc67178f2 92}; 93 94static void 95sha256_transform (uint32_t * state ,const uint32_t * data ) 96{ 97uint32_t W [16 ]= {0 }; 98unsigned j ; 99#ifdef _SHA256_UNROLL2 100uint32_t a ,b ,c ,d ,e ,f ,g ,h ; 101a = state [0 ]; 102b = state [1 ]; 103c = state [2 ]; 104d = state [3 ]; 105e = state [4 ]; 106f = state [5 ]; 107g = state [6 ]; 108h = state [7 ]; 109#else 110uint32_t T [8 ]; 111for (j = 0 ;j < 8 ;j ++ ) 112T [j ]= state [j ]; 113#endif 114 115for (j = 0 ;j < 64 ;j += 16 ) 116 { 117#if defined(_SHA256_UNROLL )|| defined(_SHA256_UNROLL2 ) 118RX_8 (0 );RX_8 (8 ); 119#else 120unsigned i ; 121for (i = 0 ;i < 16 ;i ++ ) {R (i ); } 122#endif 123 } 124 125#ifdef _SHA256_UNROLL2 126state [0 ]+= a ; 127state [1 ]+= b ; 128state [2 ]+= c ; 129state [3 ]+= d ; 130state [4 ]+= e ; 131state [5 ]+= f ; 132state [6 ]+= g ; 133state [7 ]+= h ; 134#else 135for (j = 0 ;j < 8 ;j ++ ) 136state [j ]+= T [j ]; 137#endif 138 139/* Wipe variables */ 140/* memset(W, 0, sizeof(W)); */ 141/* memset(T, 0, sizeof(T)); */ 142} 143 144#undef S0 145#undef S1 146#undef s0 147#undef s1 148 149static void 150sha256_write_byte_block (sha256_t * p ) 151{ 152uint32_t data32 [16 ]; 153unsigned i ; 154for (i = 0 ;i < 16 ;i ++ ) 155data32 [i ]= 156 ((uint32_t )(p -> buffer [i * 4 ]) <<24 )+ 157 ((uint32_t )(p -> buffer [i * 4 + 1 ]) <<16 )+ 158 ((uint32_t )(p -> buffer [i * 4 + 2 ]) <<8 )+ 159 ((uint32_t )(p -> buffer [i * 4 + 3 ])); 160sha256_transform (p -> state ,data32 ); 161} 162 163 164void 165sha256_hash (unsigned char * buf ,const unsigned char * data ,size_t size ) 166{ 167sha256_t hash ; 168sha256_init (& hash ); 169sha256_update (& hash ,data ,size ); 170sha256_final (& hash ,buf ); 171} 172 173 174void 175sha256_update (sha256_t * p ,const unsigned char * data ,size_t size ) 176{ 177uint32_t curBufferPos = (uint32_t )p -> count & 0x3F ; 178while (size > 0 ) 179 { 180p -> buffer [curBufferPos ++ ]= * data ++ ; 181p -> count ++ ; 182size -- ; 183if (curBufferPos == 64 ) 184 { 185curBufferPos = 0 ; 186sha256_write_byte_block (p ); 187 } 188 } 189} 190 191 192void 193sha256_final (sha256_t * p ,unsigned char * digest ) 194{ 195uint64_t lenInBits = (p -> count <<3 ); 196uint32_t curBufferPos = (uint32_t )p -> count & 0x3F ; 197unsigned i ; 198p -> buffer [curBufferPos ++ ]= 0x80 ; 199while (curBufferPos != (64 - 8 )) 200 { 201curBufferPos &=0x3F ; 202if (curBufferPos == 0 ) 203sha256_write_byte_block (p ); 204p -> buffer [curBufferPos ++ ]= 0 ; 205 } 206for (i = 0 ;i < 8 ;i ++ ) 207 { 208p -> buffer [curBufferPos ++ ]= (unsigned char )(lenInBits >>56 ); 209lenInBits <<=8 ; 210 } 211sha256_write_byte_block (p ); 212 213for (i = 0 ;i < 8 ;i ++ ) 214 { 215* digest ++ = (unsigned char )(p -> state [i ] >>24 ); 216* digest ++ = (unsigned char )(p -> state [i ] >>16 ); 217* digest ++ = (unsigned char )(p -> state [i ] >>8 ); 218* digest ++ = (unsigned char )(p -> state [i ]); 219 } 220sha256_init (p ); 221}