-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqrme.c
109 lines (90 loc) · 3.33 KB
/
qrme.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
#include <stdio.h>
#include <stdlib.h>
#include "include/encryption.h"
#include "include/utils.h"
#include "include/model.h"
int main(int argc, char* argv[]) {
if (argc != 3) {
printf("Usage: %s <model_file> <secret_key_file>\n", argv[0]);
return 1;
}
const char* model_file = argv[1];
const char* secret_key_file = argv[2];
Model* model = NULL;
uint8_t* secret_key = NULL;
size_t secret_key_len;
FILE* key_file = NULL;
float* input = NULL;
float* output = NULL;
int ret = 1; // Default to error
printf("Initializing encryption...\n");
init_encryption();
init_random(); // Initialize random number generator
key_file = fopen(secret_key_file, "rb");
if (!key_file) {
fprintf(stderr, "Error: Unable to open secret key file.\n");
goto cleanup;
}
fseek(key_file, 0, SEEK_END);
secret_key_len = ftell(key_file);
fseek(key_file, 0, SEEK_SET);
printf("Allocating memory for secret key (%zu bytes)...\n", secret_key_len);
secret_key = secure_realloc(NULL, secret_key_len);
if (!secret_key) {
fprintf(stderr, "Error: Unable to allocate memory for secret key.\n");
goto cleanup;
}
printf("Reading secret key...\n");
if (fread(secret_key, 1, secret_key_len, key_file) != secret_key_len) {
fprintf(stderr, "Error: Unable to read secret key.\n");
goto cleanup;
}
printf("Loading and decrypting model...\n");
model = load_model(model_file, secret_key, secret_key_len);
if (!model) {
fprintf(stderr, "Error: %s\n", get_model_error());
goto cleanup;
}
printf("Model loaded and decrypted successfully.\n");
// Get the input size from the first layer of the model
size_t input_size = model->layers[0].cols;
size_t output_size = model->layers[model->num_layers - 1].rows;
printf("Model structure:\n");
printf(" Number of layers: %zu\n", model->num_layers);
for (size_t i = 0; i < model->num_layers; i++) {
printf(" Layer %zu: %zu x %zu\n", i, model->layers[i].rows, model->layers[i].cols);
}
printf("Model input size: %zu\n", input_size);
printf("Model output size: %zu\n", output_size);
// Generate random input for testing
input = generate_random_float_array(input_size, -1.0f, 1.0f);
if (!input) {
fprintf(stderr, "Error: Unable to generate random input.\n");
goto cleanup;
}
printf("Generated random input:\n");
print_float_array(input, input_size, "Input");
// Allocate memory for output
output = secure_realloc(NULL, output_size * sizeof(float));
if (!output) {
fprintf(stderr, "Error: Unable to allocate memory for output.\n");
goto cleanup;
}
// Perform inference
printf("Performing inference...\n");
if (inference(model, input, input_size, output, output_size) != 0) {
fprintf(stderr, "Error: Failed to perform inference. %s\n", get_model_error());
goto cleanup;
}
printf("Inference result:\n");
print_float_array(output, output_size, "Output");
ret = 0; // Success
cleanup:
if (key_file) fclose(key_file);
if (secret_key) secure_free((void**)&secret_key);
if (input) secure_free((void**)&input);
if (output) secure_free((void**)&output);
if (model) free_model(model);
cleanup_encryption();
return ret;
}