@@ -2022,30 +2022,56 @@ static void run_int128_test_case(void) {
20222022        }
20232023        CHECK (secp256k1_i128_eq_var (& swa , & swz ) ==  expect );
20242024    }
2025-     /* test secp256k1_i128_check_pow2 */ 
2025+     /* test secp256k1_i128_check_pow2 (sign == 1)  */ 
20262026    {
20272027        int  expect  =  (uc  &  1 );
20282028        int  pos  =  ub  % 127 ;
20292029        if  (expect ) {
2030-             /* If expect==1, set swz to exactly (2 <<  pos) . */ 
2030+             /* If expect==1, set swz to exactly 2^ pos. */ 
20312031            uint64_t  hi  =  0 ;
20322032            uint64_t  lo  =  0 ;
2033-             if  (pos  &  64 ) {
2033+             if  (pos  >=  64 ) {
20342034                hi  =  (((uint64_t )1 ) << (pos  &  63 ));
20352035            } else  {
20362036                lo  =  (((uint64_t )1 ) << (pos  &  63 ));
20372037            }
20382038            secp256k1_i128_load (& swz , hi , lo );
20392039        } else  {
2040-             /* If expect==0, set swz = swa, but update expect=1 if swa happens to equal (2 <<  pos) . */ 
2041-             if  (pos  &  64 ) {
2040+             /* If expect==0, set swz = swa, but update expect=1 if swa happens to equal 2^ pos. */ 
2041+             if  (pos  >=  64 ) {
20422042                if  ((v [1 ] ==  (((uint64_t )1 ) << (pos  &  63 ))) &&  v [0 ] ==  0 ) expect  =  1 ;
20432043            } else  {
20442044                if  ((v [0 ] ==  (((uint64_t )1 ) << (pos  &  63 ))) &&  v [1 ] ==  0 ) expect  =  1 ;
20452045            }
20462046            swz  =  swa ;
20472047        }
2048-         CHECK (secp256k1_i128_check_pow2 (& swz , pos ) ==  expect );
2048+         CHECK (secp256k1_i128_check_pow2 (& swz , pos , 1 ) ==  expect );
2049+     }
2050+     /* test secp256k1_i128_check_pow2 (sign == -1) */ 
2051+     {
2052+         int  expect  =  (uc  &  1 );
2053+         int  pos  =  ub  % 127 ;
2054+         if  (expect ) {
2055+             /* If expect==1, set swz to exactly -2^pos. */ 
2056+             uint64_t  hi  =  ~(uint64_t )0 ;
2057+             uint64_t  lo  =  ~(uint64_t )0 ;
2058+             if  (pos  >= 64 ) {
2059+                 hi  <<= (pos  &  63 );
2060+                 lo  =  0 ;
2061+             } else  {
2062+                 lo  <<= (pos  &  63 );
2063+             }
2064+             secp256k1_i128_load (& swz , hi , lo );
2065+         } else  {
2066+             /* If expect==0, set swz = swa, but update expect=1 if swa happens to equal -2^pos. */ 
2067+             if  (pos  >= 64 ) {
2068+                 if  ((v [1 ] ==  ((~(uint64_t )0 ) << (pos  &  63 ))) &&  v [0 ] ==  0 ) expect  =  1 ;
2069+             } else  {
2070+                 if  ((v [0 ] ==  ((~(uint64_t )0 ) << (pos  &  63 ))) &&  v [1 ] ==  ~(uint64_t )0 ) expect  =  1 ;
2071+             }
2072+             swz  =  swa ;
2073+         }
2074+         CHECK (secp256k1_i128_check_pow2 (& swz , pos , -1 ) ==  expect );
20492075    }
20502076}
20512077
0 commit comments