| 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 "ec.h" |
| 39 | #include "modexp_utils.h" |
| 40 | |
| 41 | #include <sys/time.h> |
| 42 | |
| 43 | #include "ed25519.c" |
| 44 | |
| 45 | int main(void) |
| 46 | { |
| 47 | uint8_t x[32], y[32]; |
| 48 | uint8_t exp[32]; |
| 49 | Point *gp = NULL; |
| 50 | unsigned i; |
| 51 | struct timeval start, stop; |
| 52 | double duration_ms, rate; |
| 53 | const uint8_t Gx[32] = "\x21\x69\x36\xd3\xcd\x6e\x53\xfe\xc0\xa4\xe2\x31\xfd\xd6\xdc\x5c\x69\x2c\xc7\x60\x95\x25\xa7\xb2\xc9\x56\x2d\x60\x8f\x25\xd5\x1a"; |
| 54 | const uint8_t Gy[32] = "\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x66\x58"; |
| 55 | |
| 56 | #define ITERATIONS 1000U |
| 57 | |
| 58 | /* Make almost-worst case exponent */ |
| 59 | for (i=0; i<32; i++) { |
| 60 | exp[i] = (uint8_t)(0xFF - i); |
| 61 | } |
| 62 | |
| 63 | ed25519_new_point(&gp, Gx, Gy, 32, NULL); |
| 64 | |
| 65 | /** Scalar multiplications by arbitrary point **/ |
| 66 | gettimeofday(&start, NULL); |
| 67 | for (i=0; i<ITERATIONS; i++) { |
| 68 | ed25519_scalar(gp, exp, 32, 0xFFF); |
| 69 | } |
| 70 | gettimeofday(&stop, NULL); |
| 71 | duration_ms = (double)(stop.tv_sec - start.tv_sec) * 1000 + (double)(stop.tv_usec - start.tv_usec) / 1000; |
| 72 | rate = ITERATIONS / (duration_ms/1000); |
| 73 | printf("Speed (scalar mult by P) = %.0f op/s\n", rate); |
| 74 | |
| 75 | ed25519_get_xy(x, y, 32, gp); |
| 76 | printf("X: "); |
| 77 | for (i=0; i<32; i++) |
| 78 | printf("%02X", x[i]); |
| 79 | printf("\n"); |
| 80 | printf("Y: "); |
| 81 | for (i=0; i<32; i++) |
| 82 | printf("%02X", y[i]); |
| 83 | printf("\n"); |
| 84 | |
| 85 | ed25519_free_point(gp); |
| 86 | |
| 87 | return 0; |
| 88 | } |