-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_main.c
185 lines (149 loc) · 4.5 KB
/
test_main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// test_main.c
// Copyright (c) 2023 Raccoon Signature Team. See LICENSE.
// === private tests and benchmarks
#ifndef NIST_KAT
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "plat_local.h"
#include "racc_core.h"
#include "nist_random.h"
#include "mask_random.h"
#include "racc_serial.h"
#include "mont64.h"
#include "polyr.h"
#include "sha3_t.h"
#include "api.h"
// [debug] (shake) checksums of data
void dbg_chk(const char *label, uint8_t *data, size_t data_sz)
{
size_t i;
uint8_t md[16] = {0};
shake256(md, sizeof(md), data, data_sz);
printf("%s: ", label);
for (i = 0; i < sizeof(md); i++) {
printf("%02x", md[i]);
}
printf(" (%zu)\n", data_sz);
}
// [debug] dump a hex string
void dbg_hex(const char *label, const uint8_t *data, size_t data_sz)
{
size_t i;
printf("%s= ", label);
for (i = 0; i < data_sz; i++) {
printf("%02x", data[i]);
}
printf("\n");
}
// standard library process time
static inline double cpu_clock_secs()
{
return ((double)clock()) / ((double)CLOCKS_PER_SEC);
}
// maximum message size
#define MAX_MSG 256
int main()
{
size_t i;
// message to be signed (used in checksums)
uint8_t msg[MAX_MSG] = "abc";
size_t mlen = 3;
// timing
size_t iter = 100;
double ts, to;
uint64_t cc;
int fail = 0;
// buffers for serialized
uint8_t pk[CRYPTO_PUBLICKEYBYTES] = {0};
uint8_t sk[CRYPTO_SECRETKEYBYTES] = {0};
uint8_t sm[CRYPTO_BYTES + MAX_MSG] = {0};
uint8_t m2[MAX_MSG] = {0};
unsigned long long smlen = 0;
unsigned long long mlen2 = 0;
// masking random
fail += mask_random_selftest();
if (fail > 0) {
printf("mask_random_selftest() fail= %d\n", fail);
}
// initialize nist pseudo random
uint8_t seed[48];
for (i = 0; i < 48; i++) {
seed[i] = i;
}
nist_randombytes_init(seed, NULL, 256);
// (start)
printf("CRYPTO_ALGNAME\t= %s\n", CRYPTO_ALGNAME);
printf("CRYPTO_PUBLICKEYBYTES\t= %d\n", CRYPTO_PUBLICKEYBYTES);
printf("CRYPTO_SECRETKEYBYTES\t= %d\n", CRYPTO_SECRETKEYBYTES);
printf("CRYPTO_BYTES\t\t= %d\n", CRYPTO_BYTES);
// === keygen ===
crypto_sign_keypair(pk, sk);
dbg_chk(CRYPTO_ALGNAME ".pk", pk, CRYPTO_PUBLICKEYBYTES);
dbg_chk(CRYPTO_ALGNAME ".sk", sk, CRYPTO_SECRETKEYBYTES);
// === sign ===
smlen = 0;
crypto_sign(sm, &smlen, msg, mlen, sk);
dbg_chk(CRYPTO_ALGNAME ".sm", sm, (size_t) smlen);
// === verify ===
mlen2 = 0;
memset(m2, 0, sizeof(m2));
fail += crypto_sign_open(m2, &mlen2, sm, smlen, pk) == 0 ? 0 : 1;
fail += (mlen == mlen2 && memcmp(msg, m2, mlen) == 0) ? 0 : 1;
sm[123]++; // corrupt it -- expect fail
fail += crypto_sign_open(m2, &mlen2, sm, smlen, pk) != 0 ? 0 : 1;
printf("verify fail= %d\n", fail);
#ifdef BENCH_TIMEOUT
to = BENCH_TIMEOUT;
#else
to = 1.0; // timeout threshold (seconds)
#endif
printf("=== Bench ===\n");
iter = 16;
do {
iter *= 2;
ts = cpu_clock_secs();
cc = plat_get_cycle();
for (i = 0; i < iter; i++) {
crypto_sign_keypair(pk, sk);
}
cc = plat_get_cycle() - cc;
ts = cpu_clock_secs() - ts;
} while (ts < to);
printf("%s\tKeyGen() %5zu:\t%8.3f ms\t%8.3f Mcyc\n", CRYPTO_ALGNAME, iter,
1000.0 * ts / ((double)iter), 1E-6 * ((double) (cc / iter)));
iter = 16;
do {
iter *= 2;
crypto_sign_keypair(pk, sk);
ts = cpu_clock_secs();
cc = plat_get_cycle();
for (i = 0; i < iter; i++) {
crypto_sign(sm, &smlen, msg, mlen, sk);
}
cc = plat_get_cycle() - cc;
ts = cpu_clock_secs() - ts;
} while (ts < to);
printf("%s\t Sign() %5zu:\t%8.3f ms\t%8.3f Mcyc\n", CRYPTO_ALGNAME, iter,
1000.0 * ts / ((double)iter), 1E-6 * ((double) (cc / iter)));
iter = 16;
do {
iter *= 2;
crypto_sign_keypair(pk, sk);
crypto_sign(sm, &smlen, msg, mlen, sk);
ts = cpu_clock_secs();
cc = plat_get_cycle();
// repeats the same verify..
for (i = 0; i < iter; i++) {
fail += crypto_sign_open(m2, &mlen2, sm, smlen, pk) == 0 ? 0 : 1;
}
cc = plat_get_cycle() - cc;
ts = cpu_clock_secs() - ts;
} while (ts < to);
printf("%s\tVerify() %5zu:\t%8.3f ms\t%8.3f Mcyc\n", CRYPTO_ALGNAME, iter,
1000.0 * ts / ((double)iter), 1E-6 * ((double) (cc / iter)));
return 0;
}
// NIST_KAT
#endif