-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.cpp
More file actions
112 lines (91 loc) · 3.81 KB
/
bench.cpp
File metadata and controls
112 lines (91 loc) · 3.81 KB
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
#include <iostream>
#include <chrono>
#include "shrincs.h"
using namespace std;
using namespace SHRINCS;
// Just leave it here, in case we want to print signatures in hex for debugging
void print_hex(const unsigned char* data, size_t len) {
for (size_t i = 0; i < len; ++i) {
printf("%02x", data[i]);
}
printf("\n");
}
std::vector<unsigned char> hex_to_bytes(std::string hex) {
// Видаляємо "0x", якщо він є
if (hex.compare(0, 2, "0x") == 0) {
hex = hex.substr(2);
}
if (hex.length() % 2 != 0) {
throw std::runtime_error("Hex string must have an even length");
}
std::vector<unsigned char> bytes;
for (size_t i = 0; i < hex.length(); i += 2) {
std::string byteString = hex.substr(i, 2);
unsigned char byte = (unsigned char) strtol(byteString.c_str(), nullptr, 16);
bytes.push_back(byte);
}
return bytes;
}
unsigned char hexCharToInt(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return 0;
}
void hexStringToBytes(const std::string& hex, unsigned char* buffer) {
for (size_t i = 0; i < hex.length(); i += 2) {
buffer[i / 2] = (hexCharToInt(hex[i]) << 4) | hexCharToInt(hex[i + 1]);
}
}
int main()
{
PublicKey pk = PublicKey();
SecretKey sk = SecretKey();
State state = State();
shrincs_key_gen(pk, sk, state);
// pk.seed = hex_to_bytes("b635ef10af1b120cec1095d7745fec06");
// pk.root = hex_to_bytes("0f8fbe02641f3479bd3131c0d5253926");
// memcpy(sk.pk.seed.data(), pk.seed.data(), N);
// memcpy(sk.pk.root.data(), pk.root.data(), N);
// sk.sf = hex_to_bytes("e24e6a43f74040ce1cbc1bcde351374b");
// sk.sl = hex_to_bytes("9cf1c451ce3c9345581de66456b2b5f0");
// sk.prf = hex_to_bytes("e6efd5f7e551153b470c2fd76129e895");
// sk.seed = hex_to_bytes("c9fbfa59c5b83297f517a346b0f2c655");
// state.q = 0;
// state.valid = true;
// print_hex(pk.seed.data(), N);
// print_hex(pk.root.data(), N);
// print_hex(sk.sf.data(), N);
// print_hex(sk.sl.data(), N);
// print_hex(sk.prf.data(), N);
// print_hex(sk.seed.data(), N);
unsigned char* message = new unsigned char[32]();
// hexStringToBytes("8a276ceb95d10ed7705c9e25c9987cb4b1eaf73bcae7f922058c4e46e906a778", message);
auto start = std::chrono::high_resolution_clock::now();
auto signature = shrincs_sign_stateful(message, sk, state);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
std::cout << "Stateful signing time: " << elapsed.count() << " ms" << std::endl;
// print_hex(signature, N + WOTS_SIGN_LEN + state.q * N);
start = std::chrono::high_resolution_clock::now();
bool is_valid = shrincs_verify(message, signature, WOTS_SIGN_LEN + state.q * N + N, pk);
end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
std::cout << "Stateful verification time: " << elapsed.count() << " ms" << std::endl;
if (!is_valid) std::cout << "Error!" << std::endl;
delete[] signature;
start = std::chrono::high_resolution_clock::now();
signature = shrincs_sign_stateless(message, sk);
end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
std::cout << "Stateless signing time: " << elapsed.count() << " ms" << std::endl;
// print_hex(signature, SL_SIZE);
start = std::chrono::high_resolution_clock::now();
is_valid = shrincs_verify(message, signature, SL_SIZE, pk);
end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
std::cout << "Stateless verification time: " << elapsed.count() << " ms" << std::endl;
delete[] signature;
if (!is_valid) std::cout << "Error!" << std::endl;
return 0;
}