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/runtime/cm_blob_unit.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 / runtime / cm_blob_unit.c` 149 lines · 6059 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #include "cudapp_native.h" 3 #include "sha256.h" 4 #include "runtime/rx_costmodel.h" 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 void oracle_cm_init(RxCostModel *, unsigned); 12 int oracle_cm_serialize(const RxCostModel *, uint8_t *, size_t, size_t *); 13 int oracle_cm_deserialize(RxCostModel *, const uint8_t *, size_t); 14 void oracle_cm_digest(const RxCostModel *, uint8_t[32]); 15 16 static uint64_t step(unsigned mode, uint64_t s[16], uint8_t b[128]) { 17 uint64_t result; 18 assert(!omega_cm_digest(&result, mode, s, 16, b, 128)); 19 return result; 20 } 21 static void digest(const uint8_t *p, size_t n, uint8_t out[32]) { 22 uint64_t state[16]; 23 uint8_t block[128]; 24 assert(!step(0, state, block)); 25 size_t offset = 0; 26 for (size_t i = 0; i < n / 64; ++i) { 27 memcpy(block, p + offset, 64); 28 assert(!step(1, state, block)); 29 offset += 64; 30 } 31 state[9] = n - offset; 32 memcpy(block, p + offset, n - offset); 33 assert(!step(2, state, block)); 34 memcpy(out, block, 32); 35 } 36 static uint64_t policy(unsigned mode, uint64_t w[64]) { 37 uint64_t result; 38 assert(!omega_cm_blob(&result, mode, w, 64)); 39 return result; 40 } 41 static uint64_t pair(uint32_t a, uint32_t b, unsigned big) { 42 return big ? ((uint64_t)a << 32) | b : ((uint64_t)b << 32) | a; 43 } 44 static unsigned header_ok(const uint64_t w[64], unsigned big) { 45 uint32_t h[8]; 46 for (unsigned i = 0; i < 4; ++i) { 47 h[2*i] = (uint32_t)(big ? w[16+i] >> 32 : w[16+i]); 48 h[2*i+1] = (uint32_t)(big ? w[16+i] : w[16+i] >> 32); 49 } 50 return h[0] == 0x4d43474f && h[1] == 1 && h[2] > 0 && h[2] <= 8 && 51 h[3] == 8 && h[4] == 9 && h[5] == 8 && h[6] == 3 ? h[2] : 0; 52 } 53 int main(void) { 54 uint8_t *input = malloc(65536); 55 assert(input); 56 uint64_t rng = 1; 57 for (size_t i = 0; i < 65536; ++i) { 58 rng = rng * UINT64_C(`6364136223846793005`) + 1; 59 input[i] = (uint8_t)(rng >> 32); 60 } 61 const size_t lengths[] = {0,1,55,56,57,63,64,65,127,128,129,4095,4096,4097,28112,65536}; 62 for (size_t i = 0; i < sizeof lengths / sizeof *lengths; ++i) { 63 uint8_t actual[32], expected[32]; 64 digest(input, lengths[i], actual); 65 sha256_hash(input, lengths[i], expected); 66 assert(!memcmp(actual, expected, 32)); 67 } 68 for (size_t n = 0; n < 256; ++n) { 69 uint8_t a[32], b[32]; 70 digest(input, n, a); 71 sha256_hash(input, n, b); 72 assert(!memcmp(a, b, 32)); 73 } 74 uint64_t state[16] = {0}; 75 uint8_t block[128] = {0}; 76 assert(step(3, state, block) == 1); 77 for (unsigned i = 0; i < 256; ++i) { 78 block[32+i/8] ^= (uint8_t)(1u << (i%8)); 79 assert(step(3, state, block) == 0); 80 block[32+i/8] ^= (uint8_t)(1u << (i%8)); 81 } 82 state[9] = 64; 83 assert(step(2, state, block) == 1); 84 for (unsigned big = 0; big < 2; ++big) { 85 for (unsigned n = 0; n < 11; ++n) { 86 uint64_t words[64] = {0}; 87 words[0] = n; 88 words[1] = UINT64_C(`4562254508917369340`); 89 words[63] = big; 90 assert(policy(0, words) == 1); 91 uint64_t expected[5] = {pair(0x4d43474f,1,big),pair(n,8,big),pair(9,8,big),pair(3,0,big),words[1]}; 92 assert(!memcmp(words+16, expected, sizeof expected)); 93 assert(policy(1, words) == header_ok(words, big)); 94 for (unsigned j = 0; j < 5; ++j) for (unsigned bit = 0; bit < 64; ++bit) { 95 words[16+j] ^= UINT64_C(1) << bit; 96 assert(policy(1, words) == header_ok(words, big)); 97 words[16+j] ^= UINT64_C(1) << bit; 98 } 99 } 100 } 101 const double scales[] = {-INFINITY,-1,-0.0,0,nextafter(1,0),1,nextafter(1,2),nextafter(1e6,0),1e6,INFINITY,NAN}; 102 for (size_t i = 0; i < sizeof scales / sizeof *scales; ++i) { 103 uint64_t words[64] = {0}; 104 memcpy(words, scales+i, sizeof(double)); 105 assert(policy(2, words) == (uint64_t)(scales[i] >= 1 && scales[i] < 1e6)); 106 } 107 const uint64_t sizes[] = {0,1,28111,28112,28113,UINT64_MAX}; 108 for (size_t i = 0; i < sizeof sizes / sizeof *sizes; ++i) { 109 uint64_t words[64] = {sizes[i],28112}; 110 assert(policy(3, words) == (uint64_t)(sizes[i] >= 28112)); 111 assert(policy(4, words) == (uint64_t)(sizes[i] == 28112)); 112 } 113 const size_t blob_size = rx_cm_blob_size(); 114 assert(blob_size <= 32768); 115 for (unsigned arms = 0; arms < 11; ++arms) { 116 RxCostModel actual, expected; 117 rx_cm_init(&actual, arms); 118 oracle_cm_init(&expected, arms); 119 assert(!memcmp(&actual, &expected, sizeof actual)); 120 for (size_t cap = blob_size - 1; cap <= blob_size + 1; ++cap) { 121 size_t an = 0, en = 0; 122 int ar = rx_cm_serialize(&actual, input, cap, &an); 123 int er = oracle_cm_serialize(&actual, input + 32768, cap, &en); 124 assert(ar == er && an == en); 125 if (!ar) assert(!memcmp(input, input + 32768, an)); 126 } 127 uint8_t ad[32], ed[32]; 128 rx_cm_digest(&actual, ad); 129 oracle_cm_digest(&actual, ed); 130 assert(!memcmp(ad, ed, 32)); 131 assert(rx_cm_deserialize(&actual, input, blob_size) == 132 oracle_cm_deserialize(&expected, input, blob_size)); 133 assert(!memcmp(&actual, &expected, sizeof actual)); 134 } 135 for (size_t i = 0; i < 32; ++i) { 136 RxCostModel actual, expected; 137 rx_cm_init(&actual, 3); 138 expected = actual; 139 size_t length; 140 assert(!oracle_cm_serialize(&actual, input, blob_size, &length)); 141 input[blob_size - 32 + i] ^= 1; 142 assert(rx_cm_deserialize(&actual, input, blob_size) == RX_CM_ERR_DIGEST); 143 assert(oracle_cm_deserialize(&expected, input, blob_size) == RX_CM_ERR_DIGEST); 144 assert(!memcmp(&actual, &expected, sizeof actual)); 145 } 146 free(input); 147 puts("CM BLOB/DIGEST differential PASS (native/big-endian headers, bit mutants, SHA-256 block/tail boundaries)"); 148 return 0; 149 }