Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/test_core_util.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / test_core_util.c` 88 lines · 4635 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #include "sha256.h" 3 #include 4 #include 5 #include 6 #include 7 void ref_sha256_hash(const uint8_t *, size_t, uint8_t *); 8 static uint64_t rng = 0x4f4d454741ULL; 9 static uint32_t random32(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)rng; } 10 static void hex(const uint8_t *d, char *h) { for(unsigned i=0;i<32;i++) sprintf(h+2*i,"%02x",d[i]); } 11 /* Each vector goes through both unit paths: sha256_hash (one-shot op 3 below 4096 bytes) and init/update/final. */ 12 static int vector(const uint8_t *p,size_t n,const char *want) { uint8_t d[32]; char h[65]; sha256_hash(p,n,d); hex(d,h); if(strcmp(h,want)) { fprintf(stderr,"SHA vector length %zu: %s\n",n,h);return 1;} 13 sha256_ctx c; sha256_init(&c); sha256_update(&c,p,n); sha256_final(&c,d); hex(d,h); if(strcmp(h,want)) { fprintf(stderr,"SHA vector length %zu (streaming): %s\n",n,h);return 1;} return 0; } 14 static int unhex(char c) { 15 if(c>='0'&&c<='9')return c-'0'; 16 if(c>='a'&&c<='f')return c-'a'+10; 17 return -1; 18 } 19 static int cavp(const char *path) { 20 FILE *f=fopen(path,"r");if(!f){perror(path);return 1;} 21 char *line=NULL;size_t cap=0,n=0;unsigned count=0; 22 uint8_t message[65536];int have_message=0,result=0; 23 while(getline(&line,&cap,f)>=0) { 24 if(!strncmp(line,"Len = ",6)) { 25 unsigned long bits=strtoul(line+6,NULL,10); 26 if(bits%8||bits/8>sizeof message){result=1;break;} 27 n=bits/8;have_message=0; 28 } else if(!strncmp(line,"Msg = ",6)) { 29 if(strlen(line+6) 56; 128/64 -> 2; 192/64 -> 3. */ 50 static int partition(void) { 51 static const size_t lens[] = {0, 55, 56, 63, 64, 65, 128, 192}; 52 uint8_t buf[192], a[32], b[32]; 53 for (size_t i = 0; i < sizeof buf; i++) buf[i] = (uint8_t)(i * 17u + 3u); 54 for (size_t k = 0; k < sizeof lens / sizeof lens[0]; k++) { 55 sha256_hash(buf, lens[k], a); 56 ref_sha256_hash(buf, lens[k], b); 57 if (memcmp(a, b, 32)) { fprintf(stderr, "SHA partition length %zu\n", lens[k]); return 1; } 58 } 59 puts("CORE-UTIL SHA partition PASS: 0 55 56 63 64 65 128 192"); 60 return 0; 61 } 62 int main(int argc,char **argv) { 63 uint8_t p[32768],a[32],b[32],c[32]; 64 if(vector((const uint8_t *)"",0,"`e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855`")) return 1; 65 if(vector((const uint8_t *)"abc",3,"`ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad`")) return 1; 66 const char *msg="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; 67 if(vector((const uint8_t *)msg,strlen(msg),"`248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1`")) return 1; 68 if(partition()) return 1; 69 if(argc>1&&!strcmp(argv[1],"--quick"))return 0; 70 if(cavp("tests/vectors/core/SHA256ShortMsg.rsp")|| 71 cavp("tests/vectors/core/SHA256LongMsg.rsp"))return 1; 72 sha256_ctx ctx; sha256_init(&ctx); memset(p,'a',1000); 73 for(unsigned i=0;i<1000;i++) sha256_update(&ctx,p,1000); 74 sha256_final(&ctx,a); char h[65]; hex(a,h); 75 if(strcmp(h,"`cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0`")) return 1; 76 for(unsigned t=0;t<10000;t++) { 77 size_t n=t<512?t:random32()%sizeof p; 78 for(size_t j=0;jn-j)k=n-j;sha256_update(&ctx,p+j,k);j+=k;} 82 sha256_final(&ctx,c); 83 if(argc>1 && !strcmp(argv[1],"--flip") && t==0) a[0]^=1; 84 if(memcmp(a,b,32)||memcmp(a,c,32)) {fprintf(stderr,"SHA differential case %u length %zu\n",t,n);return 1;} 85 } 86 puts("CORE-UTIL SHA PASS: FIPS/CAVP vectors, 10000 random and streaming cases"); 87 return 0; 88 }