forked from microsoft/PQCrypto-SIDH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sike.c
121 lines (100 loc) · 3.96 KB
/
test_sike.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/********************************************************************************************
* SIDH: an efficient supersingular isogeny cryptography library
*
* Abstract: benchmarking/testing isogeny-based key encapsulation mechanism
*********************************************************************************************/
// Benchmark and test parameters
#if defined(OPTIMIZED_GENERIC_IMPLEMENTATION) || (TARGET == TARGET_ARM)
#define BENCH_LOOPS 5 // Number of iterations per bench
#define TEST_LOOPS 5 // Number of iterations per test
#else
#define BENCH_LOOPS 100
#define TEST_LOOPS 10
#endif
int cryptotest_kem()
{ // Testing KEM
unsigned int i;
unsigned char sk[CRYPTO_SECRETKEYBYTES] = {0};
unsigned char pk[CRYPTO_PUBLICKEYBYTES] = {0};
unsigned char ct[CRYPTO_CIPHERTEXTBYTES] = {0};
unsigned char ss[CRYPTO_BYTES] = {0};
unsigned char ss_[CRYPTO_BYTES] = {0};
bool passed = true;
printf("\n\nTESTING ISOGENY-BASED KEY ENCAPSULATION MECHANISM %s\n", SCHEME_NAME);
printf("--------------------------------------------------------------------------------------------------------\n\n");
for (i = 0; i < TEST_LOOPS; i++)
{
crypto_kem_keypair(pk, sk);
crypto_kem_enc(ct, ss, pk);
crypto_kem_dec(ss_, ct, sk);
if (memcmp(ss, ss_, CRYPTO_BYTES) != 0) {
passed = false;
break;
}
}
if (passed == true) printf(" KEM tests .................................................... PASSED");
else { printf(" KEM tests ... FAILED"); printf("\n"); return FAILED; }
printf("\n");
return PASSED;
}
int cryptorun_kem()
{ // Benchmarking key exchange
unsigned int n;
unsigned char sk[CRYPTO_SECRETKEYBYTES] = {0};
unsigned char pk[CRYPTO_PUBLICKEYBYTES] = {0};
unsigned char ct[CRYPTO_CIPHERTEXTBYTES] = {0};
unsigned char ss[CRYPTO_BYTES] = {0};
unsigned char ss_[CRYPTO_BYTES] = {0};
unsigned long long cycles, cycles1, cycles2;
printf("\n\nBENCHMARKING ISOGENY-BASED KEY ENCAPSULATION MECHANISM %s\n", SCHEME_NAME);
printf("--------------------------------------------------------------------------------------------------------\n\n");
// Benchmarking key generation
cycles = 0;
for (n = 0; n < BENCH_LOOPS; n++)
{
cycles1 = cpucycles();
crypto_kem_keypair(pk, sk);
cycles2 = cpucycles();
cycles = cycles+(cycles2-cycles1);
}
printf(" Key generation runs in ....................................... %10lld ", cycles/BENCH_LOOPS); print_unit;
printf("\n");
// Benchmarking encapsulation
cycles = 0;
for (n = 0; n < BENCH_LOOPS; n++)
{
cycles1 = cpucycles();
crypto_kem_enc(ct, ss, pk);
cycles2 = cpucycles();
cycles = cycles+(cycles2-cycles1);
}
printf(" Encapsulation runs in ........................................ %10lld ", cycles/BENCH_LOOPS); print_unit;
printf("\n");
// Benchmarking decapsulation
cycles = 0;
for (n = 0; n < BENCH_LOOPS; n++)
{
cycles1 = cpucycles();
crypto_kem_dec(ss_, ct, sk);
cycles2 = cpucycles();
cycles = cycles+(cycles2-cycles1);
}
printf(" Decapsulation runs in ........................................ %10lld ", cycles/BENCH_LOOPS); print_unit;
printf("\n");
return PASSED;
}
int main()
{
int Status = PASSED;
Status = cryptotest_kem(); // Test key encapsulation mechanism
if (Status != PASSED) {
printf("\n\n Error detected: KEM_ERROR_SHARED_KEY \n\n");
return FAILED;
}
Status = cryptorun_kem(); // Benchmark key encapsulation mechanism
if (Status != PASSED) {
printf("\n\n Error detected: KEM_ERROR_SHARED_KEY \n\n");
return FAILED;
}
return Status;
}