-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_vector_encryption.cpp
More file actions
485 lines (385 loc) · 18.5 KB
/
example_vector_encryption.cpp
File metadata and controls
485 lines (385 loc) · 18.5 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
╔═════════════════════════════════════════════════════════════════════╗
║ ThemisDB - Hybrid Database System ║
╠═════════════════════════════════════════════════════════════════════╣
File: example_vector_encryption.cpp ║
Version: 0.0.34 ║
Last Modified: 2026-03-09 03:52:18 ║
Author: unknown ║
╠═════════════════════════════════════════════════════════════════════╣
Quality Metrics: ║
• Maturity Level: 🟢 PRODUCTION-READY ║
• Quality Score: 100.0/100 ║
• Total Lines: 484 ║
• Open Issues: TODOs: 0, Stubs: 0 ║
╠═════════════════════════════════════════════════════════════════════╣
Revision History: ║
• 2a1fb0423 2026-03-03 Merge branch 'develop' into copilot/audit-src-module-docu... ║
• a629043ab 2026-02-22 Audit: document gaps found - benchmarks and stale annotat... ║
╠═════════════════════════════════════════════════════════════════════╣
Status: ✅ Production Ready ║
╚═════════════════════════════════════════════════════════════════════╝
*/
/**
* Vector Encryption Example
*
* Demonstrates how to use the complete vector encryption features in ThemisDB:
* - Phase 1: Encrypting vectors in RocksDB
* - Phase 2: Encrypting HNSW index files
* - Migration from plaintext to encrypted data
*
* Compile: g++ -std=c++17 example_vector_encryption.cpp -o example_vector_encryption -lthemis_core
* Run: ./example_vector_encryption
*/
#include "index/vector_index.h"
#include "storage/rocksdb_wrapper.h"
#include "storage/base_entity.h"
#include "security/encryption.h"
#include "security/mock_key_provider.h"
#include "utils/logger.h"
#include <iostream>
#include <vector>
#include <memory>
#include <filesystem>
using namespace themis;
namespace fs = std::filesystem;
void printHeader(const std::string& title) {
std::cout << "\n========================================\n";
std::cout << title << "\n";
std::cout << "========================================\n";
}
void printStatus(const std::string& message, bool success = true) {
std::cout << (success ? "✓ " : "✗ ") << message << std::endl;
}
/**
* Example 1: Basic Vector Encryption (Phase 1)
*/
void example1_basic_vector_encryption() {
printHeader("Example 1: Basic Vector Encryption");
// 1. Setup database and encryption
std::string db_path = "/tmp/themis_example_encrypt";
fs::remove_all(db_path);
auto db = std::make_unique<RocksDBWrapper>(db_path);
// Initialize key provider and field encryption
auto key_provider = std::make_shared<MockKeyProvider>();
key_provider->createKey("vector_embeddings", 1);
auto field_encryption = std::make_shared<FieldEncryption>(key_provider);
EncryptedField<std::vector<float>>::setFieldEncryption(field_encryption);
printStatus("Database and encryption initialized");
// 2. Create VectorIndexManager and enable encryption
VectorIndexManager vim(*db);
vim.init("documents", 768, VectorIndexManager::Metric::COSINE);
// Enable Phase 1: Vector encryption
vim.setVectorEncryptionEnabled(true);
printStatus("Vector encryption enabled");
std::cout << " Encryption status: " << (vim.isVectorEncryptionEnabled() ? "ENABLED" : "DISABLED") << std::endl;
// 3. Add encrypted vectors
std::cout << "\nAdding 100 encrypted vectors..." << std::endl;
for (int i = 0; i < 100; ++i) {
// Create a 768-dimensional vector
std::vector<float> embedding(768);
for (int j = 0; j < 768; ++j) {
embedding[j] = static_cast<float>(i + j) / 1000.0f;
}
// Create entity with vector
BaseEntity entity("doc_" + std::to_string(i));
entity.setField("embedding", embedding);
entity.setField("title", "Document " + std::to_string(i));
// Add to index (automatically encrypted)
auto status = vim.addEntity(entity);
if (!status.ok) {
std::cerr << "Failed to add entity: " << status.message << std::endl;
return;
}
}
printStatus("100 vectors added and encrypted");
// 4. Verify encryption in storage
auto stored = db->get("documents:doc_0");
if (stored.has_value()) {
BaseEntity loaded = BaseEntity::deserialize("doc_0", *stored);
bool has_encrypted = loaded.hasField("embedding_encrypted");
bool has_plaintext = loaded.hasField("embedding");
std::cout << "\nStorage verification:" << std::endl;
std::cout << " Has encrypted field: " << (has_encrypted ? "YES" : "NO") << std::endl;
std::cout << " Has plaintext field: " << (has_plaintext ? "YES" : "NO") << std::endl;
if (has_encrypted && !has_plaintext) {
printStatus("Vectors are properly encrypted in storage", true);
}
}
// 5. Search encrypted vectors
std::cout << "\nSearching encrypted vectors..." << std::endl;
std::vector<float> query(768, 0.05f);
auto [search_status, results] = vim.searchKnn(query, 5);
if (search_status.ok) {
printStatus("Search successful on encrypted data");
std::cout << "\nTop 5 results:" << std::endl;
for (size_t i = 0; i < results.size(); ++i) {
std::cout << " " << (i+1) << ". " << results[i].pk
<< " (distance: " << results[i].distance << ")" << std::endl;
}
}
// Cleanup
db.reset();
fs::remove_all(db_path);
}
/**
* Example 2: HNSW Index Encryption (Phase 2)
*/
void example2_hnsw_index_encryption() {
printHeader("Example 2: HNSW Index Encryption");
std::string db_path = "/tmp/themis_example_hnsw";
std::string hnsw_path = "/tmp/themis_example_hnsw_index";
fs::remove_all(db_path);
fs::remove_all(hnsw_path);
auto db = std::make_unique<RocksDBWrapper>(db_path);
// Initialize encryption
auto key_provider = std::make_shared<MockKeyProvider>();
key_provider->createKey("hnsw_index", 1);
auto field_encryption = std::make_shared<FieldEncryption>(key_provider);
EncryptedField<std::vector<uint8_t>>::setFieldEncryption(field_encryption);
VectorIndexManager vim(*db);
vim.init("documents", 384, VectorIndexManager::Metric::COSINE);
// Enable Phase 2: HNSW index encryption
vim.setHnswEncryptionEnabled(true);
printStatus("HNSW index encryption enabled");
// Add vectors
std::cout << "\nAdding 500 vectors..." << std::endl;
for (int i = 0; i < 500; ++i) {
std::vector<float> embedding(384, static_cast<float>(i) / 500.0f);
BaseEntity entity("doc_" + std::to_string(i));
entity.setField("embedding", embedding);
vim.addEntity(entity);
}
printStatus("500 vectors added");
// Save encrypted HNSW index
std::cout << "\nSaving encrypted HNSW index..." << std::endl;
auto save_status = vim.saveIndex(hnsw_path);
if (save_status.ok) {
printStatus("HNSW index saved (encrypted)");
// Verify encrypted files
bool encrypted_exists = fs::exists(hnsw_path + "/index.bin.encrypted");
bool plaintext_exists = fs::exists(hnsw_path + "/index.bin");
std::cout << "\nFile verification:" << std::endl;
std::cout << " index.bin.encrypted: " << (encrypted_exists ? "EXISTS" : "NOT FOUND") << std::endl;
std::cout << " index.bin (plaintext): " << (plaintext_exists ? "EXISTS" : "NOT FOUND") << std::endl;
if (encrypted_exists && !plaintext_exists) {
printStatus("HNSW index properly encrypted on disk", true);
}
}
// Load encrypted index
std::cout << "\nLoading encrypted HNSW index..." << std::endl;
VectorIndexManager vim2(*db);
vim2.init("documents", 384, VectorIndexManager::Metric::COSINE);
auto load_status = vim2.loadIndex(hnsw_path);
if (load_status.ok) {
printStatus("Encrypted index loaded successfully");
// Test search
std::vector<float> query(384, 0.5f);
auto [search_status, results] = vim2.searchKnn(query, 3);
if (search_status.ok) {
printStatus("Search works with encrypted index");
std::cout << "\nTop 3 results:" << std::endl;
for (size_t i = 0; i < results.size(); ++i) {
std::cout << " " << (i+1) << ". " << results[i].pk << std::endl;
}
}
}
// Cleanup
db.reset();
fs::remove_all(db_path);
fs::remove_all(hnsw_path);
}
/**
* Example 3: Full Encryption (Both Phases)
*/
void example3_full_encryption() {
printHeader("Example 3: Full Encryption (Phase 1 + Phase 2)");
std::string db_path = "/tmp/themis_example_full";
std::string hnsw_path = "/tmp/themis_example_full_index";
fs::remove_all(db_path);
fs::remove_all(hnsw_path);
auto db = std::make_unique<RocksDBWrapper>(db_path);
// Initialize encryption for both phases
auto key_provider = std::make_shared<MockKeyProvider>();
key_provider->createKey("vector_embeddings", 1);
key_provider->createKey("hnsw_index", 1);
auto field_encryption = std::make_shared<FieldEncryption>(key_provider);
EncryptedField<std::vector<float>>::setFieldEncryption(field_encryption);
EncryptedField<std::vector<uint8_t>>::setFieldEncryption(field_encryption);
VectorIndexManager vim(*db);
vim.init("documents", 256, VectorIndexManager::Metric::COSINE);
// Enable BOTH Phase 1 and Phase 2
vim.setVectorEncryptionEnabled(true);
vim.setHnswEncryptionEnabled(true);
printStatus("Full encryption enabled (vectors + HNSW index)");
std::cout << " Vector encryption: " << (vim.isVectorEncryptionEnabled() ? "ON" : "OFF") << std::endl;
std::cout << " HNSW encryption: " << (vim.isHnswEncryptionEnabled() ? "ON" : "OFF") << std::endl;
// Add data
std::cout << "\nAdding 200 vectors with full encryption..." << std::endl;
for (int i = 0; i < 200; ++i) {
std::vector<float> embedding(256);
for (int j = 0; j < 256; ++j) {
embedding[j] = std::sin(static_cast<float>(i + j) * 0.1f);
}
BaseEntity entity("doc_" + std::to_string(i));
entity.setField("embedding", embedding);
entity.setField("category", "category_" + std::to_string(i % 5));
vim.addEntity(entity);
}
printStatus("200 vectors added (all encrypted)");
// Save encrypted index
vim.saveIndex(hnsw_path);
printStatus("Encrypted HNSW index saved");
// Simulate server restart
std::cout << "\n--- Simulating Server Restart ---" << std::endl;
VectorIndexManager vim2(*db);
vim2.init("documents", 256, VectorIndexManager::Metric::COSINE);
// Load encrypted index
vim2.loadIndex(hnsw_path);
printStatus("Encrypted index loaded after restart");
// Verify no plaintext on disk
std::cout << "\nSecurity verification:" << std::endl;
bool no_plaintext_vectors = !db->get("documents:doc_0")->empty(); // Encrypted storage
bool no_plaintext_index = !fs::exists(hnsw_path + "/index.bin");
std::cout << " Vectors encrypted in DB: " << (no_plaintext_vectors ? "YES" : "NO") << std::endl;
std::cout << " HNSW index encrypted: " << (no_plaintext_index ? "YES" : "NO") << std::endl;
if (no_plaintext_vectors && no_plaintext_index) {
printStatus("100% at-rest encryption achieved!", true);
}
// Test search
std::vector<float> query(256, 0.0f);
auto [search_status, results] = vim2.searchKnn(query, 5);
if (search_status.ok) {
printStatus("Search successful with full encryption");
}
// Cleanup
db.reset();
fs::remove_all(db_path);
fs::remove_all(hnsw_path);
}
/**
* Example 4: Migration from Plaintext to Encrypted
*/
void example4_migration() {
printHeader("Example 4: Migration from Plaintext to Encrypted");
std::string db_path = "/tmp/themis_example_migration";
fs::remove_all(db_path);
auto db = std::make_unique<RocksDBWrapper>(db_path);
// Step 1: Create plaintext data
std::cout << "\nStep 1: Creating plaintext data..." << std::endl;
{
VectorIndexManager vim(*db);
vim.init("documents", 128, VectorIndexManager::Metric::COSINE);
// Add plaintext vectors (encryption disabled)
for (int i = 0; i < 50; ++i) {
std::vector<float> embedding(128, static_cast<float>(i) / 50.0f);
BaseEntity entity("doc_" + std::to_string(i));
entity.setField("embedding", embedding);
vim.addEntity(entity);
}
printStatus("50 plaintext vectors added");
}
// Step 2: Enable encryption
std::cout << "\nStep 2: Enabling encryption..." << std::endl;
auto key_provider = std::make_shared<MockKeyProvider>();
key_provider->createKey("vector_embeddings", 1);
auto field_encryption = std::make_shared<FieldEncryption>(key_provider);
EncryptedField<std::vector<float>>::setFieldEncryption(field_encryption);
VectorIndexManager vim(*db);
vim.init("documents", 128, VectorIndexManager::Metric::COSINE);
vim.setVectorEncryptionEnabled(true);
printStatus("Encryption enabled");
// Step 3: Add new encrypted vectors
std::cout << "\nStep 3: Adding new encrypted vectors..." << std::endl;
for (int i = 50; i < 100; ++i) {
std::vector<float> embedding(128, static_cast<float>(i) / 100.0f);
BaseEntity entity("doc_" + std::to_string(i));
entity.setField("embedding", embedding);
vim.addEntity(entity);
}
printStatus("50 encrypted vectors added");
// Step 4: Verify mixed mode works
std::cout << "\nStep 4: Testing mixed mode (plaintext + encrypted)..." << std::endl;
vim.rebuildFromStorage();
std::vector<float> query(128, 0.5f);
auto [search_status, results] = vim.searchKnn(query, 10);
if (search_status.ok && results.size() == 10) {
printStatus("Mixed mode works (50 plaintext + 50 encrypted)", true);
std::cout << " Total vectors searchable: " << results.size() << std::endl;
}
std::cout << "\nNote: Use migrate_vector_encryption tool to encrypt remaining plaintext vectors" << std::endl;
// Cleanup
db.reset();
fs::remove_all(db_path);
}
/**
* Example 5: Auto-Save Configuration
*/
void example5_auto_save() {
printHeader("Example 5: Auto-Save with Encryption");
std::string db_path = "/tmp/themis_example_autosave";
std::string hnsw_path = "/tmp/themis_example_autosave_index";
fs::remove_all(db_path);
fs::remove_all(hnsw_path);
auto db = std::make_unique<RocksDBWrapper>(db_path);
// Initialize encryption
auto key_provider = std::make_shared<MockKeyProvider>();
key_provider->createKey("hnsw_index", 1);
auto field_encryption = std::make_shared<FieldEncryption>(key_provider);
EncryptedField<std::vector<uint8_t>>::setFieldEncryption(field_encryption);
VectorIndexManager vim(*db);
vim.init("documents", 128, VectorIndexManager::Metric::COSINE);
vim.setHnswEncryptionEnabled(true);
// Configure auto-save
vim.setAutoSavePath(hnsw_path, true);
printStatus("Auto-save configured with encryption");
std::cout << " Save path: " << hnsw_path << std::endl;
// Add vectors
std::cout << "\nAdding 100 vectors..." << std::endl;
for (int i = 0; i < 100; ++i) {
std::vector<float> embedding(128, static_cast<float>(i) / 100.0f);
BaseEntity entity("doc_" + std::to_string(i));
entity.setField("embedding", embedding);
vim.addEntity(entity);
}
printStatus("100 vectors added");
// Shutdown triggers auto-save
std::cout << "\nShutting down (triggers auto-save)..." << std::endl;
auto status = vim.shutdown();
if (status.ok) {
printStatus("Auto-save completed on shutdown");
// Verify encrypted index was saved
if (fs::exists(hnsw_path + "/index.bin.encrypted")) {
printStatus("Encrypted index file created automatically", true);
}
}
// Cleanup
db.reset();
fs::remove_all(db_path);
fs::remove_all(hnsw_path);
}
int main() {
std::cout << "===========================================\n";
std::cout << "ThemisDB Vector Encryption Examples\n";
std::cout << "===========================================\n";
std::cout << "\nThis demo shows the complete encryption workflow:\n";
std::cout << "- Phase 1: Vector encryption in RocksDB\n";
std::cout << "- Phase 2: HNSW index file encryption\n";
std::cout << "- Migration and auto-save features\n";
try {
example1_basic_vector_encryption();
example2_hnsw_index_encryption();
example3_full_encryption();
example4_migration();
example5_auto_save();
printHeader("All Examples Completed Successfully!");
std::cout << "\nNext steps:\n";
std::cout << "1. Run integration tests: ./test_vector_encryption_integration\n";
std::cout << "2. Use migration tool: ./migrate_vector_encryption --help\n";
std::cout << "3. See documentation in docs/security/\n";
} catch (const std::exception& e) {
std::cerr << "\nError: " << e.what() << std::endl;
return 1;
}
return 0;
}