From d35ff846a870c66406b251fac1ce20aafb43af96 Mon Sep 17 00:00:00 2001 From: "Pavel V. Shatov (Meister)" Date: Tue, 11 Feb 2020 15:58:29 +0300 Subject: Update STM32 demo driver. --- stm32/modexpng_driver_sample.c | 134 ++++++++++++++++++++++++++--------------- 1 file changed, 87 insertions(+), 47 deletions(-) diff --git a/stm32/modexpng_driver_sample.c b/stm32/modexpng_driver_sample.c index f455b55..d87926a 100644 --- a/stm32/modexpng_driver_sample.c +++ b/stm32/modexpng_driver_sample.c @@ -199,15 +199,55 @@ int _sign_handler(uint32_t key_length, uint32_t use_crt, uint32_t first_run, #define sign_using_crt(k,f,m,n,nf,nc,x,y,p,q,pf,pc,qf,qc,dp,dq,qinv,s,xm,ym) \ _sign_handler (k,1,f,m,n,nf,nc,x,y,p,q,pf,pc,qf,qc,dp,dq,NULL,qinv,s,xm,ym) + - +// +// dirty workarounds +// +#define _ntohl(n) (((((unsigned long)(n) & 0xFF)) << 24)| \ + ((((unsigned long)(n) & 0xFF00)) << 8) | \ + ((((unsigned long)(n) & 0xFF0000)) >> 8) | \ + ((((unsigned long)(n) & 0xFF000000)) >> 24)) + +#define _htonl(n) (((((unsigned long)(n) & 0xFF)) << 24)| \ + ((((unsigned long)(n) & 0xFF00)) << 8) | \ + ((((unsigned long)(n) & 0xFF0000)) >> 8) | \ + ((((unsigned long)(n) & 0xFF000000)) >> 24)) + + +// +// Core Offset +// +#define MODEXPNG_CORE_NUM 0x26 + + +// +// more dirty workarounds +// +static void _fmc_read_32(uint32_t from_addr, uint32_t *to_ptr) +{ + uint32_t src_addr = FMC_FPGA_BASE_ADDR + (((256 << 2) * MODEXPNG_CORE_NUM + from_addr) & FMC_FPGA_ADDR_MASK); + uint32_t t = *((uint32_t *)src_addr); + *to_ptr = _ntohl(t); +} + +static void _fmc_write_32(uint32_t to_addr, uint32_t value) +{ + uint32_t t = _htonl(value); + uint32_t dst_addr = FMC_FPGA_BASE_ADDR + (((256 << 2) * MODEXPNG_CORE_NUM + to_addr) & FMC_FPGA_ADDR_MASK); + *(uint32_t *)dst_addr = t; +} + + // // test routine // int main() { - int ok; - int first_run; + int ok, first_run; + long long int iters; + + ok = sizeof iters; // initialize stm_init(); @@ -224,9 +264,9 @@ int main() uint32_t core_name1; uint32_t core_version; - fmc_read_32(CORE_ADDR_NAME0, &core_name0); - fmc_read_32(CORE_ADDR_NAME1, &core_name1); - fmc_read_32(CORE_ADDR_VERSION, &core_version); + _fmc_read_32(CORE_ADDR_NAME0, &core_name0); + _fmc_read_32(CORE_ADDR_NAME1, &core_name1); + _fmc_read_32(CORE_ADDR_VERSION, &core_version); // "mode", "xpng" if ((core_name0 != 0x6D6F6465) || (core_name1 != 0x78706E67)) @@ -237,7 +277,7 @@ int main() // check, that reference code works correctly ok = 1; - + /**/ ok = ok && check_montgomery_factor(1024, N_1024, N_FACTOR_1024); ok = ok && check_montgomery_factor( 512, P_1024, P_FACTOR_1024); ok = ok && check_montgomery_factor( 512, Q_1024, Q_FACTOR_1024); @@ -247,17 +287,17 @@ int main() ok = ok && check_montgomery_factor(4096, N_4096, N_FACTOR_4096); ok = ok && check_montgomery_factor(2048, P_4096, P_FACTOR_4096); ok = ok && check_montgomery_factor(2048, Q_4096, Q_FACTOR_4096); - + /**//**/ ok = ok && check_modulus_coeff(1024, N_1024, N_COEFF_1024); ok = ok && check_modulus_coeff( 512, P_1024, P_COEFF_1024); ok = ok && check_modulus_coeff( 512, Q_1024, Q_COEFF_1024); ok = ok && check_modulus_coeff(2048, N_2048, N_COEFF_2048); ok = ok && check_modulus_coeff(1024, P_2048, P_COEFF_2048); ok = ok && check_modulus_coeff(1024, Q_2048, Q_COEFF_2048); -// ok = ok && check_modulus_coeff(4096, N_4096, N_COEFF_4096); // SLOW (~20 sec) +// ok = ok && check_modulus_coeff(4096, N_4096, N_COEFF_4096); // SLOW (~20 sec) ok = ok && check_modulus_coeff(2048, P_4096, P_COEFF_4096); ok = ok && check_modulus_coeff(2048, Q_4096, Q_COEFF_4096); - + /**/ if (!ok) { led_off(LED_GREEN); led_on(LED_RED); @@ -265,14 +305,14 @@ int main() } // repeat forever - ok = 1, first_run = 1; + ok = 1, first_run = 1, iters = 0; while (1) - { + { ok = ok && sign_without_crt(1024, first_run, M_1024, N_1024, N_FACTOR_1024, N_COEFF_1024, X_1024, Y_1024, D_1024, S_1024, XM_1024, YM_1024); - + ok = ok && sign_without_crt(2048, first_run, M_2048, N_2048, N_FACTOR_2048, N_COEFF_2048, X_2048, Y_2048, D_2048, S_2048, @@ -282,14 +322,14 @@ int main() M_4096, N_4096, N_FACTOR_4096, N_COEFF_4096, X_4096, Y_4096, D_4096, S_4096, XM_4096, YM_4096); - + ok = ok && sign_using_crt(1024, first_run, M_1024, N_1024, N_FACTOR_1024, N_COEFF_1024, X_1024, Y_1024, P_1024, Q_1024, P_FACTOR_1024, P_COEFF_1024, Q_FACTOR_1024, Q_COEFF_1024, DP_1024, DQ_1024, QINV_1024, S_1024, XM_1024, YM_1024); - + ok = ok && sign_using_crt(2048, first_run, M_2048, N_2048, N_FACTOR_2048, N_COEFF_2048, X_2048, Y_2048, P_2048, Q_2048, @@ -303,13 +343,13 @@ int main() P_FACTOR_4096, P_COEFF_4096, Q_FACTOR_4096, Q_COEFF_4096, DP_4096, DQ_4096, QINV_4096, S_4096, XM_4096, YM_4096); - + if (!ok) { led_off(LED_GREEN); led_on(LED_RED); } - first_run = 0; + first_run = 0, iters++; toggle_yellow_led(); } @@ -395,33 +435,33 @@ int _sign_handler(uint32_t key_length, uint32_t use_crt, uint32_t first_run, // note, that n_coeff is one word larger, than the modulus, so we need a single // extra write after the word-by-word loop for (i=0, j=num_words-1; i