@@ -330,7 +330,22 @@ static int esp_get_rinv(MATH_INT_T *rinv, MATH_INT_T *M, word32 exp)
330330/* Z = X * Y; */
331331int esp_mp_mul (MATH_INT_T * X , MATH_INT_T * Y , MATH_INT_T * Z )
332332{
333- int ret = 0 ;
333+ int ret ;
334+
335+ #ifdef WOLFSSL_SP_INT_NEGATIVE
336+ /* neg check: X*Y becomes negative */
337+ int neg ;
338+
339+ /* aka (X->sign == Y->sign) ? MP_ZPOS : MP_NEG; , but with mp_isneg(): */
340+ neg = (mp_isneg (X ) == mp_isneg (Y )) ? MP_ZPOS : MP_NEG ;
341+ if (neg ) {
342+ /* Negative numbers are relatively infrequent.
343+ * May be interesting during verbose debugging: */
344+ ESP_LOGV (TAG , "mp_isneg(X) = %d; mp_isneg(Y) = %d; neg = %d " ,
345+ mp_isneg (X ), mp_isneg (Y ), neg );
346+ }
347+ #endif
348+ ret = MP_OKAY ; /* assume success until proven wrong */
334349
335350#if CONFIG_IDF_TARGET_ESP32S3
336351
@@ -345,10 +360,6 @@ int esp_mp_mul(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* Z)
345360 int WordsForOperand = bits2words (MinXYBits );
346361 int WordsForResult = bits2words (BitsInX + BitsInY );
347362
348- #ifdef WOLFSSL_SP_INT_NEGATIVE
349- int neg ;
350- neg = (X -> sign == Y -> sign ) ? MP_ZPOS : MP_NEG ;
351- #endif
352363 /* Make sure we are within capabilities of hardware. */
353364 if ( (WordsForOperand * BITS_IN_ONE_WORD ) > ESP_HW_MULTI_RSAMAX_BITS ) {
354365 ESP_LOGW (TAG , "exceeds max bit length(2048)" );
@@ -377,8 +388,9 @@ int esp_mp_mul(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* Z)
377388 return ret ;
378389 }
379390
380- /* 2. Disable completion interrupt signal; we don't use. */
381- DPORT_REG_WRITE (RSA_INTERRUPT_REG , 0 ); // 0 => no interrupt; 1 => interrupt on completion.
391+ /* 2. Disable completion interrupt signal; we don't use.
392+ ** 0 => no interrupt; 1 => interrupt on completion. */
393+ DPORT_REG_WRITE (RSA_INTERRUPT_REG , 0 );
382394
383395 /* 3. Write number of words required for result. */
384396 if ( (WordsForOperand * BITS_IN_ONE_WORD * 2 ) > ESP_HW_RSAMAX_BIT ) {
@@ -407,11 +419,6 @@ int esp_mp_mul(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* Z)
407419 /* 7. clear and release HW */
408420 esp_mp_hw_unlock ();
409421
410- #ifdef WOLFSSL_SP_INT_NEGATIVE
411- Z -> sign = (Z -> used > 0 ) ? neg : MP_ZPOS ;
412- #endif
413-
414- return ret ;
415422 /* end if CONFIG_IDF_TARGET_ESP32S3 */
416423
417424#else /* not CONFIG_IDF_TARGET_ESP32S3 */
@@ -422,11 +429,6 @@ int esp_mp_mul(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* Z)
422429 word32 maxWords_sz ;
423430 word32 hwWords_sz ;
424431
425- /* neg check - X*Y becomes negative */
426- #ifdef WOLFSSL_SP_INT_NEGATIVE
427- neg = mp_isneg (X ) != mp_isneg (Y ) ? 1 : 0 ;
428- #endif
429-
430432 /* ask bits number */
431433 Xs = mp_count_bits (X );
432434 Ys = mp_count_bits (Y );
@@ -492,15 +494,18 @@ int esp_mp_mul(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* Z)
492494 /* step.7 clear and release HW */
493495 esp_mp_hw_unlock ();
494496
497+ #endif /* CONFIG_IDF_TARGET_ESP32S3 or not */
495498
499+ /* common exit for all chipset types */
496500#ifdef WOLFSSL_SP_INT_NEGATIVE
497501 if (!mp_iszero (Z ) && neg ) {
502+ /* for non-zero negative numbers, set negative flag for our result:
503+ * Z->sign = FP_NEG */
498504 mp_setneg (Z );
499505 }
500506#endif
501507
502508 return ret ;
503- #endif /* CONFIG_IDF_TARGET_ESP32S3 or not */
504509}
505510
506511/* Z = X * Y (mod M) */
@@ -596,8 +601,9 @@ int esp_mp_mulmod(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* M, MATH_INT_T* Z)
596601 return ret ;
597602 }
598603
599- /* 2. Disable completion interrupt signal; we don't use. */
600- DPORT_REG_WRITE (RSA_INTERRUPT_REG , 0 ); // 0 => no interrupt; 1 => interrupt on completion.
604+ /* 2. Disable completion interrupt signal; we don't use.
605+ ** 0 => no interrupt; 1 => interrupt on completion. */
606+ DPORT_REG_WRITE (RSA_INTERRUPT_REG , 0 );
601607
602608 /* 3. Write (N_result_bits/32 - 1) to the RSA_MODE_REG. */
603609 OperandBits = max (max (Xs , Ys ), Ms );
@@ -815,8 +821,9 @@ int esp_mp_exptmod(MATH_INT_T* X, MATH_INT_T* Y, word32 Ys, MATH_INT_T* M, MATH_
815821 return ret ;
816822 }
817823
818- /* 2. Disable completion interrupt signal; we don't use. */
819- DPORT_REG_WRITE (RSA_INTERRUPT_REG , 0 ); // 0 => no interrupt; 1 => interrupt on completion.
824+ /* 2. Disable completion interrupt signal; we don't use.
825+ ** 0 => no interrupt; 1 => interrupt on completion. */
826+ DPORT_REG_WRITE (RSA_INTERRUPT_REG , 0 );
820827
821828 /* 3. Write (N_result_bits/32 - 1) to the RSA_MODE_REG. */
822829 OperandBits = max (max (Xs , Ys ), Ms );
0 commit comments