1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include<stdio.h> #include<sys/time.h> #include<openssl/bn.h> struct timeval timestart, timeend ; float timeuse_encrypt = 0 , timeuse_decrypt = 0, timeuse_add = 0 , timeuse_mul = 0 , timeuse = 0 ; int main() { BN_CTX *ctx = BN_CTX_new(); BN_CTX_start(ctx); BIGNUM *r1 = BN_CTX_get(ctx); BIGNUM *r2 = BN_CTX_get(ctx); BIGNUM *n = BN_CTX_get(ctx); BIGNUM *tmp = BN_CTX_get(ctx); BIGNUM *dv = BN_CTX_get(ctx); BIGNUM *rem = BN_CTX_get(ctx); BN_rand(r1, 236896 , 0, 0); BN_rand(r2, 236896 , 0, 0); BN_rand(n, 236896 , 0, 0); gettimeofday(×tart, NULL); BN_mod_mul(tmp , r1 , r2 , n , ctx); gettimeofday(&timeend, NULL); timeuse = 1000000 * (timeend.tv_sec - timestart.tv_sec) + timeend.tv_usec - timestart.tv_usec; printf("\nmod mul time:%f ms\n", timeuse / 1000); gettimeofday(×tart, NULL); BN_mul(tmp , r1 , r2 , ctx); gettimeofday(&timeend, NULL); timeuse = 1000000 * (timeend.tv_sec - timestart.tv_sec) + timeend.tv_usec - timestart.tv_usec; printf("\nmul time:%f ms\n", timeuse / 1000);
gettimeofday(×tart, NULL); BN_div(dv , rem , tmp ,n , ctx); gettimeofday(&timeend, NULL); timeuse = 1000000 * (timeend.tv_sec - timestart.tv_sec) + timeend.tv_usec - timestart.tv_usec; printf("\ndiv time:%f ms\n", timeuse / 1000); gettimeofday(×tart, NULL); BN_mod(tmp , tmp , n , ctx); gettimeofday(&timeend, NULL); timeuse = 1000000 * (timeend.tv_sec - timestart.tv_sec) + timeend.tv_usec - timestart.tv_usec; printf("\nmod time:%f ms\n", timeuse / 1000); BN_CTX_end(ctx); BN_CTX_free(ctx); return 0; }
|