Debian Codegraph @

src/ed448_perf.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2022, 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 "modexp_utils.h"
39
40 #include <sys/time.h>
41
42 #include "ed448.c"
43
44 #define COORD_LEN 56
45
46 int main(void)
47 {
48 uint8_t x[COORD_LEN], y[COORD_LEN];
49 uint8_t exp[56];
50 PointEd448 *gp = NULL;
51 unsigned i;
52 int res;
53 EcContext *ec_ctx;
54 struct timeval start, stop;
55 double duration_ms, rate;
56 const uint8_t Gx[COORD_LEN] = "\x4f\x19\x70\xc6\x6b\xed\x0d\xed\x22\x1d\x15\xa6\x22\xbf\x36\xda\x9e\x14\x65\x70\x47\x0f\x17\x67\xea\x6d\xe3\x24\xa3\xd3\xa4\x64\x12\xae\x1a\xf7\x2a\xb6\x65\x11\x43\x3b\x80\xe1\x8b\x00\x93\x8e\x26\x26\xa8\x2b\xc7\x0c\xc0\x5e";
57 const uint8_t Gy[COORD_LEN] = "\x69\x3f\x46\x71\x6e\xb6\xbc\x24\x88\x76\x20\x37\x56\xc9\xc7\x62\x4b\xea\x73\x73\x6c\xa3\x98\x40\x87\x78\x9c\x1e\x05\xa0\xc2\xd7\x3a\xd3\xff\x1c\xe6\x7c\x39\xc4\xfd\xbd\x13\x2c\x4e\xd7\xc8\xad\x98\x08\x79\x5b\xf2\x30\xfa\x14";
58
59 #define ITERATIONS 5000U
60
61 /* Make almost-worst case exponent */
62 for (i=0; i<sizeof(exp); i++) {
63 exp[i] = (uint8_t)(0xFF - i);
64 }
65
66 res = ed448_new_context(&ec_ctx);
67 assert(res == 0);
68 res = ed448_new_point(&gp, Gx, Gy, COORD_LEN, ec_ctx);
69 assert(res == 0);
70
71 /** Scalar multiplications by arbitrary point **/
72 gettimeofday(&start, NULL);
73 for (i=0; i<ITERATIONS; i++) {
74 ed448_scalar(gp, exp, sizeof(exp), 0xFFF);
75 }
76 gettimeofday(&stop, NULL);
77 duration_ms = (double)(stop.tv_sec - start.tv_sec) * 1000 + (double)(stop.tv_usec - start.tv_usec) / 1000;
78 rate = ITERATIONS / (duration_ms/1000);
79 printf("Speed (scalar mult by P) = %.0f op/s\n", rate);
80
81 ed448_get_xy(x, y, sizeof(x), gp);
82 printf("X: ");
83 for (i=0; i<sizeof(x); i++)
84 printf("%02X", x[i]);
85 printf("\n");
86 printf("Y: ");
87 for (i=0; i<sizeof(y); i++)
88 printf("%02X", y[i]);
89 printf("\n");
90
91 ed448_free_point(gp);
92 ed448_free_context(ec_ctx);
93
94 return 0;
95 }