char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
ed1c3a20f
char/aliceai-t5-moe
1/* 2SHA-1 in C 3By Steve Reid <steve@edmweb.com> 4100% Public Domain 5 6Test Vectors (from FIPS PUB 180-1) 7"abc" 8A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D 9"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 1084983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 11A million repetitions of "a" 1234AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F 13*/ 14 15/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */ 16/* #define SHA1HANDSOFF * Copies data before messing with it. */ 17 18#define SHA1HANDSOFF 19 20#include <stdio.h> 21#include <string.h> 22 23/* for uint32_t */ 24#include <stdint.h> 25 26#include "sha1.h" 27 28namespace vendor_hash { 29 30 31#define rol (value ,bits ) (((value) << (bits)) | ((value) >> (32 - (bits)))) 32 33/* blk0() and blk() perform the initial expand. */ 34/* I got the idea of expanding during the round function from SSLeay */ 35#if BYTE_ORDER == LITTLE_ENDIAN 36#define blk0 (i ) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ 37 |(rol(block->l[i],8)&0x00FF00FF)) 38#elif BYTE_ORDER == BIG_ENDIAN 39#define blk0 (i ) block->l[i] 40#else 41#error "Endianness not defined!" 42#endif 43#define blk (i ) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ 44 ^block->l[(i+2)&15]^block->l[i&15],1)) 45 46/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ 47#define R0 (v ,w ,x ,y ,z ,i ) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); 48#define R1 (v ,w ,x ,y ,z ,i ) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); 49#define R2 (v ,w ,x ,y ,z ,i ) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); 50#define R3 (v ,w ,x ,y ,z ,i ) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); 51#define R4 (v ,w ,x ,y ,z ,i ) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); 52 53 54/* Hash a single 512-bit block. This is the core of the algorithm. */ 55 56void SHA1Transform ( 57uint32_t state [5 ], 58const unsigned char buffer [64 ] 59) 60{ 61uint32_t a ,b ,c ,d ,e ; 62 63typedef union 64 { 65unsigned char c [64 ]; 66uint32_t l [16 ]; 67 }CHAR64LONG16 ; 68 69#ifdef SHA1HANDSOFF 70CHAR64LONG16 block [1 ];/* use array to appear as a pointer */ 71 72memcpy (block ,buffer ,64 ); 73#else 74/* The following had better never be used because it causes the 75* pointer-to-const buffer to be cast into a pointer to non-const. 76* And the result is written through. I threw a "const" in, hoping 77* this will cause a diagnostic. 78*/ 79CHAR64LONG16 * block = (const CHAR64LONG16 * )buffer ; 80#endif 81/* Copy context->state[] to working vars */ 82a = state [0 ]; 83b = state [1 ]; 84c = state [2 ]; 85d = state [3 ]; 86e = state [4 ]; 87/* 4 rounds of 20 operations each. Loop unrolled. */ 88R0 (a ,b ,c ,d ,e ,0 ); 89R0 (e ,a ,b ,c ,d ,1 ); 90R0 (d ,e ,a ,b ,c ,2 ); 91R0 (c ,d ,e ,a ,b ,3 ); 92R0 (b ,c ,d ,e ,a ,4 ); 93R0 (a ,b ,c ,d ,e ,5 ); 94R0 (e ,a ,b ,c ,d ,6 ); 95R0 (d ,e ,a ,b ,c ,7 ); 96R0 (c ,d ,e ,a ,b ,8 ); 97R0 (b ,c ,d ,e ,a ,9 ); 98R0 (a ,b ,c ,d ,e ,10 ); 99R0 (e ,a ,b ,c ,d ,11 ); 100R0 (d ,e ,a ,b ,c ,12 ); 101R0 (c ,d ,e ,a ,b ,13 ); 102R0 (b ,c ,d ,e ,a ,14 ); 103R0 (a ,b ,c ,d ,e ,15 ); 104R1 (e ,a ,b ,c ,d ,16 ); 105R1 (d ,e ,a ,b ,c ,17 ); 106R1 (c ,d ,e ,a ,b ,18 ); 107R1 (b ,c ,d ,e ,a ,19 ); 108R2 (a ,b ,c ,d ,e ,20 ); 109R2 (e ,a ,b ,c ,d ,21 ); 110R2 (d ,e ,a ,b ,c ,22 ); 111R2 (c ,d ,e ,a ,b ,23 ); 112R2 (b ,c ,d ,e ,a ,24 ); 113R2 (a ,b ,c ,d ,e ,25 ); 114R2 (e ,a ,b ,c ,d ,26 ); 115R2 (d ,e ,a ,b ,c ,27 ); 116R2 (c ,d ,e ,a ,b ,28 ); 117R2 (b ,c ,d ,e ,a ,29 ); 118R2 (a ,b ,c ,d ,e ,30 ); 119R2 (e ,a ,b ,c ,d ,31 ); 120R2 (d ,e ,a ,b ,c ,32 ); 121R2 (c ,d ,e ,a ,b ,33 ); 122R2 (b ,c ,d ,e ,a ,34 ); 123R2 (a ,b ,c ,d ,e ,35 ); 124R2 (e ,a ,b ,c ,d ,36 ); 125R2 (d ,e ,a ,b ,c ,37 ); 126R2 (c ,d ,e ,a ,b ,38 ); 127R2 (b ,c ,d ,e ,a ,39 ); 128R3 (a ,b ,c ,d ,e ,40 ); 129R3 (e ,a ,b ,c ,d ,41 ); 130R3 (d ,e ,a ,b ,c ,42 ); 131R3 (c ,d ,e ,a ,b ,43 ); 132R3 (b ,c ,d ,e ,a ,44 ); 133R3 (a ,b ,c ,d ,e ,45 ); 134R3 (e ,a ,b ,c ,d ,46 ); 135R3 (d ,e ,a ,b ,c ,47 ); 136R3 (c ,d ,e ,a ,b ,48 ); 137R3 (b ,c ,d ,e ,a ,49 ); 138R3 (a ,b ,c ,d ,e ,50 ); 139R3 (e ,a ,b ,c ,d ,51 ); 140R3 (d ,e ,a ,b ,c ,52 ); 141R3 (c ,d ,e ,a ,b ,53 ); 142R3 (b ,c ,d ,e ,a ,54 ); 143R3 (a ,b ,c ,d ,e ,55 ); 144R3 (e ,a ,b ,c ,d ,56 ); 145R3 (d ,e ,a ,b ,c ,57 ); 146R3 (c ,d ,e ,a ,b ,58 ); 147R3 (b ,c ,d ,e ,a ,59 ); 148R4 (a ,b ,c ,d ,e ,60 ); 149R4 (e ,a ,b ,c ,d ,61 ); 150R4 (d ,e ,a ,b ,c ,62 ); 151R4 (c ,d ,e ,a ,b ,63 ); 152R4 (b ,c ,d ,e ,a ,64 ); 153R4 (a ,b ,c ,d ,e ,65 ); 154R4 (e ,a ,b ,c ,d ,66 ); 155R4 (d ,e ,a ,b ,c ,67 ); 156R4 (c ,d ,e ,a ,b ,68 ); 157R4 (b ,c ,d ,e ,a ,69 ); 158R4 (a ,b ,c ,d ,e ,70 ); 159R4 (e ,a ,b ,c ,d ,71 ); 160R4 (d ,e ,a ,b ,c ,72 ); 161R4 (c ,d ,e ,a ,b ,73 ); 162R4 (b ,c ,d ,e ,a ,74 ); 163R4 (a ,b ,c ,d ,e ,75 ); 164R4 (e ,a ,b ,c ,d ,76 ); 165R4 (d ,e ,a ,b ,c ,77 ); 166R4 (c ,d ,e ,a ,b ,78 ); 167R4 (b ,c ,d ,e ,a ,79 ); 168/* Add the working vars back into context.state[] */ 169state [0 ]+= a ; 170state [1 ]+= b ; 171state [2 ]+= c ; 172state [3 ]+= d ; 173state [4 ]+= e ; 174/* Wipe variables */ 175a = b = c = d = e = 0 ; 176#ifdef SHA1HANDSOFF 177memset (block ,'\0' ,sizeof (block )); 178#endif 179} 180 181 182/* SHA1Init - Initialize new context */ 183 184void SHA1Init ( 185SHA1_CTX * context 186) 187{ 188/* SHA1 initialization constants */ 189context -> state [0 ]= 0x67452301 ; 190context -> state [1 ]= 0xEFCDAB89 ; 191context -> state [2 ]= 0x98BADCFE ; 192context -> state [3 ]= 0x10325476 ; 193context -> state [4 ]= 0xC3D2E1F0 ; 194context -> count [0 ]= context -> count [1 ]= 0 ; 195} 196 197 198/* Run your data through this. */ 199 200void SHA1Update ( 201SHA1_CTX * context , 202const unsigned char * data , 203uint32_t len 204) 205{ 206uint32_t i ; 207 208uint32_t j ; 209 210j = context -> count [0 ]; 211if ((context -> count [0 ]+= len <<3 )< j ) 212context -> count [1 ]++ ; 213context -> count [1 ]+= (len >>29 ); 214j = (j >>3 )& 63 ; 215if ((j + len )> 63 ) 216 { 217memcpy (& context -> buffer [j ],data , (i = 64 - j )); 218SHA1Transform (context -> state ,context -> buffer ); 219for (;i + 63 < len ;i += 64 ) 220 { 221SHA1Transform (context -> state ,& data [i ]); 222 } 223j = 0 ; 224 } 225else 226i = 0 ; 227memcpy (& context -> buffer [j ],& data [i ],len - i ); 228} 229 230 231/* Add padding and return the message digest. */ 232 233void SHA1Final ( 234unsigned char digest [20 ], 235SHA1_CTX * context 236) 237{ 238unsigned i ; 239 240unsigned char finalcount [8 ]; 241 242unsigned char c ; 243 244#if 0 /* untested "improvement" by DHR */ 245/* Convert context->count to a sequence of bytes 246* in finalcount. Second element first, but 247* big-endian order within element. 248* But we do it all backwards. 249*/ 250unsigned char * fcp = & finalcount [8 ]; 251 252for (i = 0 ;i < 2 ;i ++ ) 253 { 254uint32_t t = context -> count [i ]; 255 256int j ; 257 258for (j = 0 ;j < 4 ;t >>=8 ,j ++ ) 259*-- fcp = (unsigned char )t } 260#else 261for (i = 0 ;i < 8 ;i ++ ) 262 { 263finalcount [i ]= (unsigned char ) ((context -> count [(i >=4 ?0 :1 )] >> ((3 - (i & 3 ))* 8 ))& 255 );/* Endian independent */ 264 } 265#endif 266c = 0200 ; 267SHA1Update (context ,& c ,1 ); 268while ((context -> count [0 ]& 504 )!= 448 ) 269 { 270c = 0000 ; 271SHA1Update (context ,& c ,1 ); 272 } 273SHA1Update (context ,finalcount ,8 );/* Should cause a SHA1Transform() */ 274for (i = 0 ;i < 20 ;i ++ ) 275 { 276digest [i ]= (unsigned char ) 277 ((context -> state [i >>2 ] >> ((3 - (i & 3 ))* 8 ))& 255 ); 278 } 279/* Wipe variables */ 280memset (context ,'\0' ,sizeof (* context )); 281memset (& finalcount ,'\0' ,sizeof (finalcount )); 282} 283 284void SHA1 ( 285char * hash_out , 286const char * str , 287uint32_t len ) 288{ 289SHA1_CTX ctx ; 290unsigned int ii ; 291 292SHA1Init (& ctx ); 293for (ii = 0 ;ii < len ;ii += 1 ) 294SHA1Update (& ctx , (const unsigned char * )str + ii ,1 ); 295SHA1Final ((unsigned char * )hash_out ,& ctx ); 296} 297 298}// namespace vendor_hash 299