@@ -2961,6 +2961,69 @@ static void run_field_convert(void) {
2961
2961
CHECK (secp256k1_memcmp_var (& fes2 , & fes , sizeof (fes )) == 0 );
2962
2962
}
2963
2963
2964
+ static void run_field_be32_overflow (void ) {
2965
+ {
2966
+ static const unsigned char zero_overflow [32 ] = {
2967
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
2968
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
2969
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
2970
+ 0xFF , 0xFF , 0xFF , 0xFE , 0xFF , 0xFF , 0xFC , 0x2F ,
2971
+ };
2972
+ static const unsigned char zero [32 ] = { 0x00 };
2973
+ unsigned char out [32 ];
2974
+ secp256k1_fe fe ;
2975
+ CHECK (secp256k1_fe_set_b32 (& fe , zero_overflow ) == 0 );
2976
+ CHECK (secp256k1_fe_normalizes_to_zero (& fe ) == 1 );
2977
+ secp256k1_fe_normalize (& fe );
2978
+ CHECK (secp256k1_fe_is_zero (& fe ) == 1 );
2979
+ secp256k1_fe_get_b32 (out , & fe );
2980
+ CHECK (secp256k1_memcmp_var (out , zero , 32 ) == 0 );
2981
+ }
2982
+ {
2983
+ static const unsigned char one_overflow [32 ] = {
2984
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
2985
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
2986
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
2987
+ 0xFF , 0xFF , 0xFF , 0xFE , 0xFF , 0xFF , 0xFC , 0x30 ,
2988
+ };
2989
+ static const unsigned char one [32 ] = {
2990
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
2991
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
2992
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
2993
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 ,
2994
+ };
2995
+ unsigned char out [32 ];
2996
+ secp256k1_fe fe ;
2997
+ CHECK (secp256k1_fe_set_b32 (& fe , one_overflow ) == 0 );
2998
+ secp256k1_fe_normalize (& fe );
2999
+ CHECK (secp256k1_fe_cmp_var (& fe , & secp256k1_fe_one ) == 0 );
3000
+ secp256k1_fe_get_b32 (out , & fe );
3001
+ CHECK (secp256k1_memcmp_var (out , one , 32 ) == 0 );
3002
+ }
3003
+ {
3004
+ static const unsigned char ff_overflow [32 ] = {
3005
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3006
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3007
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3008
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3009
+ };
3010
+ static const unsigned char ff [32 ] = {
3011
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
3012
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
3013
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
3014
+ 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x03 , 0xD0 ,
3015
+ };
3016
+ unsigned char out [32 ];
3017
+ secp256k1_fe fe ;
3018
+ const secp256k1_fe fe_ff = SECP256K1_FE_CONST (0 , 0 , 0 , 0 , 0 , 0 , 0x01 , 0x000003d0 );
3019
+ CHECK (secp256k1_fe_set_b32 (& fe , ff_overflow ) == 0 );
3020
+ secp256k1_fe_normalize (& fe );
3021
+ CHECK (secp256k1_fe_cmp_var (& fe , & fe_ff ) == 0 );
3022
+ secp256k1_fe_get_b32 (out , & fe );
3023
+ CHECK (secp256k1_memcmp_var (out , ff , 32 ) == 0 );
3024
+ }
3025
+ }
3026
+
2964
3027
/* Returns true if two field elements have the same representation. */
2965
3028
static int fe_identical (const secp256k1_fe * a , const secp256k1_fe * b ) {
2966
3029
int ret = 1 ;
@@ -7515,6 +7578,7 @@ int main(int argc, char **argv) {
7515
7578
run_field_half ();
7516
7579
run_field_misc ();
7517
7580
run_field_convert ();
7581
+ run_field_be32_overflow ();
7518
7582
run_fe_mul ();
7519
7583
run_sqr ();
7520
7584
run_sqrt ();
0 commit comments