Debian Codegraph @

src/make_p384_table.c

raw symbols SCIP
1 #include "common.h"
2 #include "ec.h"
3 #include "endianess.h"
4
5 #define BITS 384
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 p384_mod[BYTES] = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff";
27 const uint8_t b[BYTES] = "\xb3\x31\x2f\xa7\xe2\x3e\xe7\xe4\x98\x8e\x05\x6b\xe3\xf8\x2d\x19\x18\x1d\x9c\x6e\xfe\x81\x41\x12\x03\x14\x08\x8f\x50\x13\x87\x5a\xc6\x56\x39\x8d\x8a\x2e\xd1\x9d\x2a\x85\xc8\xed\xd3\xec\x2a\xef";
28 const uint8_t order[BYTES] = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xc7\x63\x4d\x81\xf4\x37\x2d\xdf\x58\x1a\x0d\xb2\x48\xb0\xa7\x7a\xec\xec\x19\x6a\xcc\xc5\x29\x73";
29 const uint8_t p384_Gx[BYTES] = "\xaa\x87\xca\x22\xbe\x8b\x05\x37\x8e\xb1\xc7\x1e\xf3\x20\xad\x74\x6e\x1d\x3b\x62\x8b\xa7\x9b\x98\x59\xf7\x41\xe0\x82\x54\x2a\x38\x55\x02\xf2\x5d\xbf\x55\x29\x6c\x3a\x54\x5e\x38\x72\x76\x0a\xb7";
30 const uint8_t p384_Gy[BYTES] = "\x36\x17\xde\x4a\x96\x26\x2c\x6f\x5d\x9e\x98\xbf\x92\x92\xdc\x29\xf8\xf4\x1d\xbd\x28\x9a\x14\x7c\xe9\xda\x31\x13\xb5\xf0\xb8\xc0\x0a\x60\xb1\xce\x1d\x7e\x81\x9d\x7a\x43\x1d\x7c\x90\xea\x0e\x5f";
31 uint8_t xz[BYTES] = { 0 }, yz[BYTES] = { 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, p384_mod, b, order, BYTES, 0);
39 ec_ws_new_point(&g, p384_Gx, p384_Gy, BYTES, ec_ctx);
40
41 /** TODO: accept this as input **/
42 window_size = 5;
43
44 points_per_table = 1U << window_size;
45 n_tables = (BITS+window_size-1)/window_size;
46
47 /** Create table with points 0, G, 2G, 3G, .. (2**window_size-1)G **/
48 window = (EcPoint**)calloc(points_per_table, sizeof(EcPoint*));
49 ec_ws_new_point(&window[0], xz, yz, BYTES, 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 \"p384_table.h\"\n");
58 printf("const unsigned p384_n_tables = %d;\n", n_tables);
59 printf("const unsigned p384_window_size = %d;\n", window_size);
60 printf("const unsigned p384_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 p384_tables[%d][%d][2][%d] = {\n", n_tables, points_per_table, WORDS);
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[WORDS], yw[WORDS];
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, 6);
83 printf(" },\n");
84 printf(" { ");
85 print_64bit_array(yw, 6);
86 printf(" }\n");
87 printf(" }%s\n", j==points_per_table-1 ? "" : ",");
88 }
89 printf(" }%s\n", i==n_tables-1 ? "" : ",");
90
91 /* Move from G to G*2^{w} */
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 }