| 1 | #include "common.h" |
| 2 | |
| 3 | #ifdef _MSC_VER |
| 4 | #include <intrin.h> |
| 5 | #else |
| 6 | #include <x86intrin.h> |
| 7 | #endif |
| 8 | |
| 9 | __m128i reduce(__m128i prod_high, __m128i prod_low); |
| 10 | void clmult(__m128i *prod_high, __m128i *prod_low, __m128i a, __m128i b); |
| 11 | __m128i multx(__m128i a); |
| 12 | __m128i swap(__m128i a); |
| 13 | |
| 14 | void test_reduce_1(void) |
| 15 | { |
| 16 | uint8_t prod_high[16] = { 0xB7, 0xD5, 0xA2, 0x4C, 0xC4, 0x84, 0xB3, 0x23, 0xA8, 0x70, 0x56, 0x4A, 0xD7, 0xEE, 0x79, 0x01 }; |
| 17 | uint8_t prod_low[16] = { 0xFB, 0x3A, 0xB9, 0x7E, 0xB2, 0x9E, 0xDF, 0xFC, 0x44, 0xE9, 0xCB, 0x94, 0xD8, 0x83, 0xD2, 0x8F }; |
| 18 | uint8_t out[16]; |
| 19 | uint8_t expected[16] = { 0xFA, 0x4E, 0x14, 0xF6, 0xBE, 0x8D, 0xCD, 0x17, 0xCD, 0x00, 0xE3, 0x00, 0x12, 0x29, 0x45, 0x2A }; |
| 20 | __m128i r1, r2, r3; |
| 21 | int result; |
| 22 | |
| 23 | memcpy(&r1, prod_high, 16); |
| 24 | memcpy(&r2, prod_low, 16); |
| 25 | r3 = reduce(r1, r2); |
| 26 | memcpy(out, &r3, 16); |
| 27 | |
| 28 | result = memcmp(expected, out, 16); |
| 29 | assert(result == 0); |
| 30 | } |
| 31 | |
| 32 | void test_multx_1(void) |
| 33 | { |
| 34 | uint8_t zeroes[16] = { 0 }; |
| 35 | uint8_t out[16]; |
| 36 | __m128i r1, r2; |
| 37 | int result; |
| 38 | |
| 39 | memcpy(&r1, zeroes, 16); |
| 40 | r2 = multx(r1); |
| 41 | memcpy(out, &r2, 16); |
| 42 | |
| 43 | result = memcmp(zeroes, out, 16); |
| 44 | assert(result == 0); |
| 45 | } |
| 46 | |
| 47 | void test_multx_2(void) |
| 48 | { |
| 49 | uint8_t in[16]; |
| 50 | uint8_t expected[16]; |
| 51 | uint8_t out[16]; |
| 52 | __m128i r1, r2; |
| 53 | int result; |
| 54 | |
| 55 | memset(in, 0x55, 16); |
| 56 | memset(expected, 0xAA, 16); |
| 57 | |
| 58 | memcpy(&r1, in, 16); |
| 59 | r2 = multx(r1); |
| 60 | memcpy(out, &r2, 16); |
| 61 | |
| 62 | result = memcmp(expected, out, 16); |
| 63 | assert(result == 0); |
| 64 | } |
| 65 | |
| 66 | void test_multx_3(void) |
| 67 | { |
| 68 | uint8_t in[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80 }; |
| 69 | uint8_t expected[16] = { 1, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0xc2 }; |
| 70 | uint8_t out[16]; |
| 71 | __m128i r1, r2; |
| 72 | int result; |
| 73 | |
| 74 | memcpy(&r1, in, 16); |
| 75 | r2 = multx(r1); |
| 76 | memcpy(out, &r2, 16); |
| 77 | |
| 78 | result = memcmp(expected, out, 16); |
| 79 | assert(result == 0); |
| 80 | } |
| 81 | |
| 82 | void test_swap_1(void) |
| 83 | { |
| 84 | uint8_t in[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; |
| 85 | uint8_t expected[16] = { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; |
| 86 | uint8_t out[16]; |
| 87 | __m128i r1, r2; |
| 88 | int result; |
| 89 | |
| 90 | memcpy(&r1, in, 16); |
| 91 | r2 = swap(r1); |
| 92 | memcpy(out, &r2, 16); |
| 93 | |
| 94 | result = memcmp(expected, out, 16); |
| 95 | assert(result == 0); |
| 96 | } |
| 97 | |
| 98 | void test_clmul_1(void) |
| 99 | { |
| 100 | uint8_t in_a[16] = { 1, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 101 | uint8_t in_b[16] = { 0xFF, 0, 0, 0, 0, 0 ,0, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0xFF }; |
| 102 | uint8_t exp_lo[16] = { 0xFF, 0, 0, 0, 0, 0 ,0, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0xFF }; |
| 103 | uint8_t exp_hi[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 104 | uint8_t out_hi[16], out_lo[16]; |
| 105 | __m128i r1, r2, r3, r4; |
| 106 | int result; |
| 107 | |
| 108 | memcpy(&r1, in_a, 16); |
| 109 | memcpy(&r2, in_b, 16); |
| 110 | clmult(&r3, &r4, r1, r2); |
| 111 | memcpy(out_hi, &r3, 16); |
| 112 | memcpy(out_lo, &r4, 16); |
| 113 | |
| 114 | result = memcmp(exp_lo, out_lo, 16); |
| 115 | assert(result == 0); |
| 116 | |
| 117 | result = memcmp(exp_hi, out_hi, 16); |
| 118 | assert(result == 0); |
| 119 | |
| 120 | clmult(&r3, &r4, r2, r1); |
| 121 | memcpy(out_hi, &r3, 16); |
| 122 | memcpy(out_lo, &r4, 16); |
| 123 | |
| 124 | result = memcmp(exp_lo, out_lo, 16); |
| 125 | assert(result == 0); |
| 126 | |
| 127 | result = memcmp(exp_hi, out_hi, 16); |
| 128 | assert(result == 0); |
| 129 | } |
| 130 | |
| 131 | void test_clmul_2(void) |
| 132 | { |
| 133 | uint8_t in_a[16] = { 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x81 }; |
| 134 | uint8_t in_b[16] = { 0xee, 0x89, 0xf9, 0x4f, 0x98, 0x24, 0xff, 0xff, 0x90, 0x00, 0x90, 0xff, 0x8f, 0x89, 0xff, 0xff }; |
| 135 | |
| 136 | uint8_t exp_lo[16] = { 0x84, 0x1c, 0xe5, 0xb3, 0x1c, 0x3e, 0x59, 0xf8, 0x92, 0x36, 0x18, 0x72, 0x9d, 0x7c, 0x56, 0xb7 }; |
| 137 | uint8_t exp_hi[16] = { 0xe4, 0x8b, 0xea, 0xb5, 0x05, 0xe0, 0x0e, 0x23, 0x01, 0xda, 0x4e, 0x9f, 0xb2, 0xc5, 0x01, 0x7f }; |
| 138 | |
| 139 | uint8_t out_hi[16], out_lo[16]; |
| 140 | __m128i r1, r2, r3, r4; |
| 141 | int result; |
| 142 | |
| 143 | memcpy(&r1, in_a, 16); |
| 144 | memcpy(&r2, in_b, 16); |
| 145 | |
| 146 | clmult(&r3, &r4, r1, r2); |
| 147 | memcpy(out_hi, &r3, 16); |
| 148 | memcpy(out_lo, &r4, 16); |
| 149 | |
| 150 | result = memcmp(exp_lo, out_lo, 16); |
| 151 | assert(result == 0); |
| 152 | |
| 153 | result = memcmp(exp_hi, out_hi, 16); |
| 154 | assert(result == 0); |
| 155 | |
| 156 | clmult(&r3, &r4, r2, r1); |
| 157 | memcpy(out_hi, &r3, 16); |
| 158 | memcpy(out_lo, &r4, 16); |
| 159 | |
| 160 | result = memcmp(exp_lo, out_lo, 16); |
| 161 | assert(result == 0); |
| 162 | |
| 163 | result = memcmp(exp_hi, out_hi, 16); |
| 164 | assert(result == 0); |
| 165 | } |
| 166 | |
| 167 | int main(void) |
| 168 | { |
| 169 | test_reduce_1(); |
| 170 | |
| 171 | test_multx_1(); |
| 172 | test_multx_2(); |
| 173 | test_multx_3(); |
| 174 | |
| 175 | test_swap_1(); |
| 176 | |
| 177 | test_clmul_1(); |
| 178 | test_clmul_2(); |
| 179 | return 0; |
| 180 | } |