1818#include " keyswitch_int.h"
1919#include " ntt_int.h"
2020
21+ #ifdef FPGA_USE_INTEL_HEXL
22+ #include " hexl/hexl.hpp"
23+ #endif
24+
2125namespace intel {
2226namespace hexl {
2327namespace fpga {
@@ -38,14 +42,24 @@ static DEV_TYPE get_device() {
3842 char * env = getenv (" RUN_CHOICE" );
3943 if (env) {
4044 int e = atoi (env);
41- FPGA_ASSERT ((e >= 1 ) && (e <= 2 ));
45+ FPGA_ASSERT ((e >= 0 ) && (e <= 2 ));
4246 d = DEV_TYPE (e);
4347 }
44- if (d == FPGA)
48+ switch (d) {
49+ case CPU:
50+ std::cout << " Running using HEXL CPU ..." << std::endl;
51+ break ;
52+ case EMU:
53+ std::cout << " Running using FPGA Emulator ..." << std::endl;
54+ break ;
55+ case FPGA:
4556 std::cout << " Running using Physical FPGA device accelerator ..."
4657 << std::endl;
47- else
48- std::cout << " Running using FPGA Emulator..." << std::endl;
58+ break ;
59+ default :
60+ FPGA_ASSERT (0 );
61+ break ;
62+ }
4963 return d;
5064}
5165
@@ -127,6 +141,9 @@ static Buffer fpga_buffer(g_fpga_bufsize, g_batch_size_dyadic_mult,
127141 g_batch_size_KeySwitch);
128142
129143void attach_fpga_pooling () {
144+ if (g_choice == CPU) {
145+ return ;
146+ }
130147 std::cout << " Running on FPGA: Creating Static FPGA Device Context ... "
131148 << std::endl;
132149 exit_signal = std::promise<bool >();
@@ -138,6 +155,9 @@ void attach_fpga_pooling() {
138155}
139156
140157void detach_fpga_pooling () {
158+ if (g_choice == CPU) {
159+ return ;
160+ }
141161 exit_signal.set_value (true );
142162 delete pool;
143163 pool = nullptr ;
@@ -171,6 +191,20 @@ static void fpga_DyadicMultiply(uint64_t* results, const uint64_t* operand1,
171191 }
172192}
173193
194+ static void cpu_DyadicMultiply (uint64_t * results, const uint64_t * operand1,
195+ const uint64_t * operand2, uint64_t n,
196+ const uint64_t * moduli, uint64_t n_moduli) {
197+ FPGA_ASSERT (g_choice == CPU);
198+
199+ #ifdef FPGA_USE_INTEL_HEXL
200+ namespace ns = intel::hexl::internal;
201+ ns::DyadicMultiply (results, operand1, operand2, n, moduli, n_moduli);
202+ #else
203+ std::cerr << " HEXL CPU version not supported" << std::endl;
204+ exit (1 );
205+ #endif
206+ }
207+
174208bool DyadicMultiplyCompleted_int () {
175209 bool all_done = false ;
176210 while (!all_done) {
@@ -200,13 +234,16 @@ void DyadicMultiply_int(uint64_t* results, const uint64_t* operand1,
200234 const uint64_t * operand2, uint64_t n,
201235 const uint64_t * moduli, uint64_t n_moduli) {
202236 switch (g_choice) {
237+ case CPU:
238+ cpu_DyadicMultiply (results, operand1, operand2, n, moduli, n_moduli);
239+ break ;
203240 case EMU:
204241 case FPGA:
205242 fpga_DyadicMultiply (results, operand1, operand2, n, moduli, n_moduli);
206243 break ;
207244 default :
208245 std::cerr << " ERROR: Invalid RUN_CHOICE envvar. Set to a valid "
209- " value {1, or 2}, where 1:EMU, 2:FPGA."
246+ " value {0, 1, or 2}, where 0:CPU, 1:EMU, 2:FPGA."
210247 << std::endl;
211248 FPGA_ASSERT (0 );
212249 break ;
@@ -277,6 +314,10 @@ void INTT_int(uint64_t* coeff_poly, const uint64_t* inv_root_of_unity_powers,
277314 uint64_t coeff_modulus, uint64_t inv_n, uint64_t inv_n_w,
278315 uint64_t n) {
279316 switch (g_choice) {
317+ case CPU:
318+ std::cerr << " HEXL CPU version not supported" << std::endl;
319+ FPGA_ASSERT (0 );
320+ break ;
280321 case EMU:
281322 case FPGA:
282323 fpga_INTT (coeff_poly, inv_root_of_unity_powers,
@@ -285,7 +326,7 @@ void INTT_int(uint64_t* coeff_poly, const uint64_t* inv_root_of_unity_powers,
285326 break ;
286327 default :
287328 std::cerr << " ERROR: Invalid RUN_CHOICE envvar. Set to a valid "
288- " value {1, or 2}, where 1:EMU, 2:FPGA."
329+ " value {0, 1, or 2}, where 0:CPU, 1:EMU, 2:FPGA."
289330 << std::endl;
290331 FPGA_ASSERT (0 );
291332 break ;
@@ -353,14 +394,18 @@ void NTT_int(uint64_t* coeff_poly, const uint64_t* root_of_unity_powers,
353394 const uint64_t * precon_root_of_unity_powers,
354395 uint64_t coeff_modulus, uint64_t n) {
355396 switch (g_choice) {
397+ case CPU:
398+ std::cerr << " HEXL CPU version not supported" << std::endl;
399+ FPGA_ASSERT (0 );
400+ break ;
356401 case EMU:
357402 case FPGA:
358403 fpga_NTT (coeff_poly, root_of_unity_powers, precon_root_of_unity_powers,
359404 coeff_modulus, n);
360405 break ;
361406 default :
362407 std::cerr << " ERROR: Invalid RUN_CHOICE envvar. Set to a valid "
363- " value {0, 1, or 2}, where 1:EMU, 2:FPGA."
408+ " value {0, 1, or 2}, where 0:CPU, 1:EMU, 2:FPGA."
364409 << std::endl;
365410 FPGA_ASSERT (0 );
366411 break ;
@@ -417,6 +462,26 @@ static void fpga_KeySwitch(uint64_t* result, const uint64_t* t_target_iter_ptr,
417462 }
418463}
419464
465+ static void cpu_KeySwitch (uint64_t * result, const uint64_t * t_target_iter_ptr,
466+ uint64_t n, uint64_t decomp_modulus_size,
467+ uint64_t key_modulus_size, uint64_t rns_modulus_size,
468+ uint64_t key_component_count, const uint64_t * moduli,
469+ const uint64_t ** k_switch_keys,
470+ const uint64_t * modswitch_factors,
471+ const uint64_t * twiddle_factors) {
472+ FPGA_ASSERT (g_choice == CPU);
473+
474+ #ifdef FPGA_USE_INTEL_HEXL
475+ namespace ns = intel::hexl::internal;
476+ ns::KeySwitch (result, t_target_iter_ptr, n, decomp_modulus_size,
477+ key_modulus_size, rns_modulus_size, key_component_count,
478+ moduli, k_switch_keys, modswitch_factors, twiddle_factors);
479+ #else
480+ std::cerr << " HEXL CPU version not supported" << std::endl;
481+ exit (1 );
482+ #endif
483+ }
484+
420485bool KeySwitchCompleted_int () {
421486 bool all_done = false ;
422487 while (!all_done) {
@@ -450,6 +515,12 @@ void KeySwitch_int(uint64_t* result, const uint64_t* t_target_iter_ptr,
450515 const uint64_t * modswitch_factors,
451516 const uint64_t * twiddle_factors) {
452517 switch (g_choice) {
518+ case CPU:
519+ cpu_KeySwitch (result, t_target_iter_ptr, n, decomp_modulus_size,
520+ key_modulus_size, rns_modulus_size, key_component_count,
521+ moduli, k_switch_keys, modswitch_factors,
522+ twiddle_factors);
523+ break ;
453524 case EMU:
454525 case FPGA:
455526 fpga_KeySwitch (result, t_target_iter_ptr, n, decomp_modulus_size,
@@ -459,7 +530,7 @@ void KeySwitch_int(uint64_t* result, const uint64_t* t_target_iter_ptr,
459530 break ;
460531 default :
461532 std::cerr << " ERROR: Invalid RUN_CHOICE envvar. Set to a valid "
462- " value {1, or 2}, where 1:EMU, 2:FPGA."
533+ " value {0, 1, or 2}, where 0:CPU, 1:EMU, 2:FPGA."
463534 << std::endl;
464535 FPGA_ASSERT (0 );
465536 break ;
0 commit comments