From 329b539c504768124d174a3381155a9fab980054 Mon Sep 17 00:00:00 2001 From: Zhi Guan Date: Tue, 23 Apr 2024 09:57:37 +0800 Subject: [PATCH] Add sm9 speed test --- CMakeLists.txt | 1 + tests/sm9test.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9455c4f0e..4480e6b50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ SET(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake") include_directories(include) +add_compile_options(-O3) option(ENABLE_SM4_TBOX "Enable SM4 merged S-Box implementation" ON) option(ENABLE_SM4_AARCH64 "Enable SM4 AARCH64 assembly implementation" OFF) diff --git a/tests/sm9test.c b/tests/sm9test.c index 0a576d26e..978c25df8 100644 --- a/tests/sm9test.c +++ b/tests/sm9test.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -553,6 +554,50 @@ int test_sm9_z256_twist_point() { "934FDDA6D3AB48C8571CE2354B79742AA498CB8CDDE6BD1FA5946345A1A652F6" +static int test_sm9_z256_pairing_speed(void) +{ + SM9_Z256_TWIST_POINT Ppubs = { + {{0x8F14D65696EA5E32, 0x414D2177386A92DD, 0x6CE843ED24A3B573, 0x29DBA116152D1F78}, + {0x0AB1B6791B94C408, 0x1CE0711C5E392CFB, 0xE48AFF4B41B56501, 0x9F64080B3084F733}}, + {{0x0E75C05FB4E3216D, 0x1006E85F5CDFF073, 0x1A7CE027B7A46F74, 0x41E00A53DDA532DA}, + {0xE89E1408D0EF1C25, 0xAD3E2FDB1A77F335, 0xB57329F447E3A0CB, 0x69850938ABEA0112}}, + {{1,0,0,0}, {0,0,0,0}}, + }; + SM9_Z256_POINT P1 = { + {0xe8c4e4817c66dddd, 0xe1e4086909dc3280, 0xf5ed0704487d01d6, 0x93de051d62bf718f}, + {0x0c464cd70a3ea616, 0x1c1c00cbfa602435, 0x631065125c395bbc, 0x21fe8dda4f21e607}, + {1,0,0,0}, + }; + sm9_z256_fp12_t r; + + clock_t begin, end; + double seconds; + int i; + + sm9_z256_modp_to_mont(Ppubs.X[0], Ppubs.X[0]); + sm9_z256_modp_to_mont(Ppubs.Y[0], Ppubs.Y[0]); + sm9_z256_modp_to_mont(Ppubs.Z[0], Ppubs.Z[0]); + sm9_z256_modp_to_mont(Ppubs.X[1], Ppubs.X[1]); + sm9_z256_modp_to_mont(Ppubs.Y[1], Ppubs.Y[1]); + sm9_z256_modp_to_mont(Ppubs.Z[1], Ppubs.Z[1]); + + sm9_z256_modp_to_mont(P1.X, P1.X); + sm9_z256_modp_to_mont(P1.Y, P1.Y); + sm9_z256_modp_to_mont(P1.Z, P1.Z); + + begin = clock(); + for (i = 0; i < 256; i++) { + sm9_z256_pairing(r, &Ppubs, &P1); + } + end = clock(); + seconds = (double)(end - begin)/CLOCKS_PER_SEC; + + printf("%s: %d pairings per seconds\n", __FUNCTION__, (int)(256/seconds)); + return 1; +} + + + int test_sm9_z256_pairing() { SM9_Z256_POINT _P1 = { @@ -800,6 +845,7 @@ int main(void) { if (test_sm9_z256_ciphertext() != 1) goto err; if (test_sm9_z256_encrypt() != 1) goto err; if (test_sm9_z256_exchange() != 1) goto err; + if (test_sm9_z256_pairing_speed() != 1) goto err; printf("%s all tests passed\n", __FILE__); return 0;