orders.c (2704B)
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 // Use values as in params/params-sphincs-shake-128s.h 6 #define SPX_FORS_HEIGHT 12 7 #define SPX_FORS_TREES 14 8 9 /* 10 * Implementation as in ref/fors.c. 11 */ 12 static void message_to_indices(uint32_t *indices, const unsigned char *m) 13 { 14 unsigned int i, j; 15 unsigned int offset = 0; 16 17 for (i = 0; i < SPX_FORS_TREES; i++) { 18 indices[i] = 0; 19 for (j = 0; j < SPX_FORS_HEIGHT; j++) { 20 indices[i] ^= ((m[offset >> 3] >> (offset & 0x7)) & 1u) << j; 21 offset++; 22 } 23 } 24 } 25 26 /* 27 * Implementation prior to commit 28 * 74b618d4b1311a9946170fbcb85d9bca06033460 29 */ 30 static void message_to_indices_old(uint32_t *indices, const unsigned char *m) 31 { 32 unsigned int i, j; 33 unsigned int offset = 0; 34 35 for (i = 0; i < SPX_FORS_TREES; i++) { 36 indices[i] = 0; 37 for (j = 0; j < SPX_FORS_HEIGHT; j++) { 38 indices[i] <<= 1; 39 indices[i] ^= (m[offset >> 3] >> (offset & 0x7)) & 0x1; 40 offset++; 41 } 42 } 43 } 44 45 /* 46 * Algorithm 3 base_2b, with 47 * b = SPX_FORS_HEIGHT = 12 48 * out_len = SPX_FORS_TREES = 14 49 */ 50 static void message_to_indices_fips(uint32_t* indices, const unsigned char* m) 51 { 52 uint64_t in = 0; 53 uint64_t bits = 0; 54 uint64_t total = 0; 55 56 size_t out; 57 for(out = 0; out < SPX_FORS_TREES; out++) { 58 while(bits < SPX_FORS_HEIGHT) { 59 total = (total << 8) + ((uint64_t) m[in]); // overflow ok 60 in++; 61 bits += 8; 62 } 63 bits -= SPX_FORS_HEIGHT; 64 indices[out] = (total >> bits) % (1 << SPX_FORS_HEIGHT); 65 } 66 } 67 68 int main(void) 69 { 70 // Bit string 71 // 111111110000000100...0 72 unsigned char m[(SPX_FORS_HEIGHT*SPX_FORS_TREES+7)/8]; 73 m[0] = 0xff; 74 m[1] = 0x01; 75 size_t i; 76 for(i = 2; i < (SPX_FORS_HEIGHT*SPX_FORS_TREES+7)/8; i++) { 77 m[i] = 0x00; 78 } 79 80 // Computing indices using implementation from the reference 81 // implementation. 82 uint32_t indices[SPX_FORS_TREES]; 83 message_to_indices(indices, m); 84 85 for(i = 0; i < SPX_FORS_TREES; i++) { 86 printf("ref impl index (%zu): %lu\n", i, indices[i]); 87 } 88 89 // Computing it using a implementation of the function from 90 // the FIPS.205 way. 91 uint32_t indices_std[SPX_FORS_TREES]; 92 message_to_indices_fips(indices_std, m); 93 94 for(i = 0; i < SPX_FORS_TREES; i++) { 95 printf("fips.205 index (%zu): %lu\n", i, indices_std[i]); 96 } 97 98 // Computing the indices using the implementation from the 99 // reference implementation before commit 100 // 74b618d4b1311a9946170fbcb85d9bca06033460 101 uint32_t indices_old[SPX_FORS_TREES]; 102 message_to_indices_old(indices_old, m); 103 104 for(i = 0; i < SPX_FORS_TREES; i++) { 105 printf("old impl index (%zu): %lu\n", i, indices_old[i]); 106 } 107 }