Debian Codegraph @

src/ec_ws_p384.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2018, Helder Eijs <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include <assert.h>
33
34 #include "common.h"
35 #include "endianess.h"
36 #include "multiply.h"
37 #include "mont.h"
38 #include "ec.h"
39 #include "modexp_utils.h"
40
41 #include <sys/time.h>
42
43 #include "ec_ws.c"
44
45 #define BYTES 48
46
47 int main(void)
48 {
49 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";
50 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";
51 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";
52 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";
53 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";
54 uint8_t x[BYTES], y[BYTES];
55 uint8_t exp[BYTES];
56 EcContext *ec_ctx;
57 EcPoint *ecp = NULL;
58 EcPoint *gp = NULL;
59 unsigned i;
60 struct timeval start, stop;
61 double duration_ms, rate;
62
63 #define ITERATIONS 1000U
64
65 /* Make almost-worst case exponent */
66 for (i=0; i<BYTES; i++) {
67 exp[i] = (uint8_t)(0xFF - i);
68 }
69
70 ec_ws_new_context(&ec_ctx, p384_mod, b, order, BYTES, /* seed */ 4);
71
72 ec_ws_new_point(&gp, p384_Gx, p384_Gy, BYTES, ec_ctx);
73 ec_ws_clone(&ecp, gp);
74
75 /** Scalar multiplications by G **/
76 gettimeofday(&start, NULL);
77 for (i=0; i<ITERATIONS; i++) {
78 ec_ws_copy(ecp, gp);
79 ec_ws_scalar(ecp, exp, BYTES, 0xFFF);
80 ec_ws_get_xy(x, y, BYTES, ecp);
81 }
82 gettimeofday(&stop, NULL);
83 duration_ms = (double)(stop.tv_sec - start.tv_sec) * 1000 + (double)(stop.tv_usec - start.tv_usec) / 1000;
84 rate = ITERATIONS / (duration_ms/1000);
85 printf("Speed (scalar mult by G) = %.0f op/s\n", rate);
86
87 ec_ws_get_xy(x, y, BYTES, ecp);
88 printf("X: ");
89 for (i=0; i<BYTES; i++)
90 printf("%02X", x[i]);
91 printf("\n");
92 printf("Y: ");
93 for (i=0; i<BYTES; i++)
94 printf("%02X", y[i]);
95 printf("\n");
96
97 #if 1
98 /** Scalar multiplications by arbitrary point **/
99 ec_ws_double(ecp);
100 gettimeofday(&start, NULL);
101 for (i=0; i<ITERATIONS; i++) {
102 ec_ws_scalar(ecp, exp, BYTES, 0xFFF);
103 ec_ws_get_xy(x, y, BYTES, ecp);
104 }
105 gettimeofday(&stop, NULL);
106 duration_ms = (double)(stop.tv_sec - start.tv_sec) * 1000 + (double)(stop.tv_usec - start.tv_usec) / 1000;
107 rate = ITERATIONS / (duration_ms/1000);
108 printf("Speed (scalar mult by P) = %.0f op/s\n", rate);
109
110 ec_ws_get_xy(x, y, BYTES, ecp);
111 printf("X: ");
112 for (i=0; i<BYTES; i++)
113 printf("%02X", x[i]);
114 printf("\n");
115 printf("Y: ");
116 for (i=0; i<BYTES; i++)
117 printf("%02X", y[i]);
118 printf("\n");
119 #endif
120
121 ec_ws_free_point(gp);
122 ec_ws_free_point(ecp);
123 ec_free_context(ec_ctx);
124
125 return 0;
126 }