| 1 |
#include "common.h" |
| 2 |
#include "ec.h" |
| 3 |
#include "endianess.h" |
| 4 |
|
| 5 |
#define BITS 256 |
| 6 |
#define BYTES BITS/8 |
| 7 |
#define WORDS BITS/64 |
| 8 |
|
| 9 |
static void print_64bit_array(uint64_t *x, unsigned len) |
| 10 |
{ |
| 11 |
unsigned i, j; |
| 12 |
|
| 13 |
for (i=0; i<len; i++) { |
| 14 |
printf("0x"); |
| 15 |
for (j=0; j<8; j++) { |
| 16 |
printf("%02X", (uint8_t)(x[i] >> ((7-j)*8))); |
| 17 |
} |
| 18 |
printf("ULL"); |
| 19 |
if (i!=(len-1)) |
| 20 |
printf(","); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
int main(void) |
| 25 |
{ |
| 26 |
const uint8_t p256_mod[32] = "\xff\xff\xff\xff\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; |
| 27 |
const uint8_t b[32] = "\x5a\xc6\x35\xd8\xaa\x3a\x93\xe7\xb3\xeb\xbd\x55\x76\x98\x86\xbc\x65\x1d\x06\xb0\xcc\x53\xb0\xf6\x3b\xce\x3c\x3e\x27\xd2\x60\x4b"; |
| 28 |
const uint8_t order[32] = "\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xbc\xe6\xfa\xad\xa7\x17\x9e\x84\xf3\xb9\xca\xc2\xfc\x63\x25\x51"; |
| 29 |
const uint8_t p256_Gx[32] = "\x6b\x17\xd1\xf2\xe1\x2c\x42\x47\xf8\xbc\xe6\xe5\x63\xa4\x40\xf2\x77\x03\x7d\x81\x2d\xeb\x33\xa0\xf4\xa1\x39\x45\xd8\x98\xc2\x96"; |
| 30 |
const uint8_t p256_Gy[32] = "\x4f\xe3\x42\xe2\xfe\x1a\x7f\x9b\x8e\xe7\xeb\x4a\x7c\x0f\x9e\x16\x2b\xce\x33\x57\x6b\x31\x5e\xce\xcb\xb6\x40\x68\x37\xbf\x51\xf5"; |
| 31 |
uint8_t xz[32] = { 0 }, yz[32] = { 0 }; |
| 32 |
EcContext *ec_ctx; |
| 33 |
EcPoint *g = NULL; |
| 34 |
EcPoint **window = NULL; |
| 35 |
int i, j; |
| 36 |
unsigned n_tables, points_per_table, window_size; |
| 37 |
|
| 38 |
ec_ws_new_context(&ec_ctx, p256_mod, b, order, 32, 0); |
| 39 |
ec_ws_new_point(&g, p256_Gx, p256_Gy, 32, ec_ctx); |
| 40 |
|
| 41 |
|
| 42 |
window_size = 5; |
| 43 |
|
| 44 |
points_per_table = 1U << window_size; |
| 45 |
n_tables = (256+window_size-1)/window_size; |
| 46 |
|
| 47 |
|
| 48 |
window = (EcPoint**)calloc(points_per_table, sizeof(EcPoint*)); |
| 49 |
ec_ws_new_point(&window[0], xz, yz, 32, ec_ctx); |
| 50 |
for (i=1; i<points_per_table; i++) { |
| 51 |
ec_ws_clone(&window[i], window[i-1]); |
| 52 |
ec_ws_add(window[i], g); |
| 53 |
} |
| 54 |
|
| 55 |
printf("/* This file was automatically generated, do not edit */\n"); |
| 56 |
printf("#include \"common.h\"\n"); |
| 57 |
printf("#include \"p256_table.h\"\n"); |
| 58 |
printf("const unsigned p256_n_tables = %d;\n", n_tables); |
| 59 |
printf("const unsigned p256_window_size = %d;\n", window_size); |
| 60 |
printf("const unsigned p256_points_per_table = %d;\n", points_per_table); |
| 61 |
printf("/* Affine coordinates in Montgomery form */\n"); |
| 62 |
printf("/* Table size: %u kbytes */\n", (unsigned)(n_tables*points_per_table*2*WORDS*sizeof(uint64_t))); |
| 63 |
printf("const uint64_t p256_tables[%d][%d][2][4] = {\n", n_tables, points_per_table); |
| 64 |
|
| 65 |
for (i=0; i<n_tables; i++) { |
| 66 |
|
| 67 |
printf(" { /* Table #%u */\n", i); |
| 68 |
for (j=0; j<points_per_table; j++) { |
| 69 |
uint64_t xw[4], yw[4]; |
| 70 |
|
| 71 |
if (j == 0) { |
| 72 |
memcpy(xw, xz, sizeof xw); |
| 73 |
memcpy(yw, yz, sizeof yw); |
| 74 |
} else { |
| 75 |
ec_ws_normalize(window[j]); |
| 76 |
memcpy(xw, window[j]->x, sizeof xw); |
| 77 |
memcpy(yw, window[j]->y, sizeof yw); |
| 78 |
} |
| 79 |
|
| 80 |
printf(" { /* Point #%d */\n", j); |
| 81 |
printf(" { "); |
| 82 |
print_64bit_array(xw, 4); |
| 83 |
printf(" },\n"); |
| 84 |
printf(" { "); |
| 85 |
print_64bit_array(yw, 4); |
| 86 |
printf(" }\n"); |
| 87 |
printf(" }%s\n", j==points_per_table-1 ? "" : ","); |
| 88 |
} |
| 89 |
printf(" }%s\n", i==n_tables-1 ? "" : ","); |
| 90 |
|
| 91 |
|
| 92 |
for (j=0; j<window_size; j++) |
| 93 |
ec_ws_double(g); |
| 94 |
|
| 95 |
for (j=1; j<points_per_table; j++) { |
| 96 |
ec_ws_copy(window[j], window[j-1]); |
| 97 |
ec_ws_add(window[j], g); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
printf("};\n"); |
| 102 |
|
| 103 |
for (i=0; i<points_per_table; i++) { |
| 104 |
ec_ws_free_point(window[i]); |
| 105 |
} |
| 106 |
free(window); |
| 107 |
ec_ws_free_point(g); |
| 108 |
ec_free_context(ec_ctx); |
| 109 |
|
| 110 |
return 0; |
| 111 |
} |