Add speed tests

This commit is contained in:
Zhi Guan
2024-04-22 22:46:23 +08:00
parent ae2f635b7f
commit 242bbb3f6d
4 changed files with 98 additions and 2 deletions

View File

@@ -1219,7 +1219,7 @@ void sm2_z256_point_dbl(SM2_Z256_POINT *R, const SM2_Z256_POINT *A)
// S = S * X1 = 4 * X1 * Y1^2
sm2_z256_modp_mont_mul(S, S, X1);
sm2_z256_print(stderr, 0, 0, "12. S = S * X1 = 4 * X1 * Y1^2", S);
//sm2_z256_print(stderr, 0, 0, "12. S = S * X1 = 4 * X1 * Y1^2", S);
// tmp0 = 2 * S = 8 * X1 * Y1^2
sm2_z256_modp_dbl(tmp0, S);

View File

@@ -172,6 +172,48 @@ static int test_sm2_sign(void)
return 1;
}
static int test_sm2_sign_ctx_speed(void)
{
SM2_KEY sm2_key;
SM2_SIGN_CTX sign_ctx;
uint8_t msg[64 - 32 - 8];
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
size_t siglen;
clock_t start, end;
double seconds;
int i;
sm2_key_generate(&sm2_key);
if (sm2_sign_init(&sign_ctx, &sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
error_print();
return -1;
}
start = clock();
for (i = 0; i < 4096; i++) {
if (sm2_sign_update(&sign_ctx, msg, sizeof(msg)) != 1) {
error_print();
return -1;
}
if (sm2_sign_finish(&sign_ctx, sig, &siglen) != 1) {
error_print();
return -1;
}
sm2_sign_ctx_reset(&sign_ctx);
}
end = clock();
seconds = (double)(end - start)/CLOCKS_PER_SEC;
fprintf(stderr, "sm2_sign_ctx speed : 4096 signs time %f seconds, %f signs per second\n", seconds, 4096/seconds);
return 1;
}
static int test_sm2_sign_ctx(void)
{
int ret;
@@ -298,6 +340,7 @@ int main(void)
if (test_sm2_sign() != 1) goto err;
if (test_sm2_sign_ctx() != 1) goto err;
if (test_sm2_sign_ctx_reset() != 1) goto err;
if (test_sm2_sign_ctx_speed() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:

View File

@@ -12,6 +12,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <gmssl/sm3.h>
#include <gmssl/hex.h>
#include <gmssl/error.h>
@@ -175,10 +176,36 @@ static int test_sm3(void)
return 1;
}
static int test_sm3_speed(void)
{
SM3_CTX sm3_ctx;
uint8_t blocks[4096];
uint8_t dgst[32];
clock_t start, end;
double seconds;
int i;
start = clock();
sm3_init(&sm3_ctx);
for (i = 0; i < 4096; i++) {
sm3_update(&sm3_ctx, blocks, sizeof(blocks));
}
sm3_finish(&sm3_ctx, dgst);
end = clock();
seconds = (double)(end - start)/CLOCKS_PER_SEC;
fprintf(stderr, "sm3 on 16-MiB : time %f seconds : %f MiB per second\n", seconds, 16/seconds);
return 1;
}
int main(void)
{
if (test_sm3() != 1) goto err;
if (test_sm3_speed() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:

View File

@@ -12,6 +12,7 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <gmssl/hex.h>
#include <gmssl/sm4.h>
#include <gmssl/error.h>
@@ -91,12 +92,37 @@ static int test_sm4(void)
}
static int test_sm4_speed(void)
{
SM4_KEY sm4_key;
uint8_t key[16] = {0};
uint8_t block[16] = {0};
clock_t start, end;
double seconds;
int i;
sm4_set_encrypt_key(&sm4_key, key);
start = clock();
for (i = 0; i < 1024*1024; i++) {
sm4_encrypt(&sm4_key, block, block);
}
end = clock();
seconds = (double)(end - start)/ CLOCKS_PER_SEC;
fprintf(stderr, "sm4_encrypt: %f-MiB per seconds\n", 16/seconds);
return 1;
}
int main(void)
{
if (test_sm4() != 1) goto err;
if (test_sm4_speed() != 1) goto err;
/*
if (test_sm4_cbc() != 1) goto err;
if (test_sm4_cbc_padding() != 1) goto err;