@@ -44,6 +44,25 @@ static int all_bytes_equal(const void* s, unsigned char value, size_t n) {
44
44
return 1 ;
45
45
}
46
46
47
+ /* Debug helper for printing arrays of unsigned char. */
48
+ #define PRINT_BUF (buf , len ) do { \
49
+ printf("%s[%lu] = ", #buf, (unsigned long)len); \
50
+ print_buf_plain(buf, len); \
51
+ } while(0);
52
+ static void print_buf_plain (const unsigned char * buf , size_t len ) {
53
+ size_t i ;
54
+ printf ("{" );
55
+ for (i = 0 ; i < len ; i ++ ) {
56
+ if (i % 8 == 0 ) {
57
+ printf ("\n " );
58
+ } else {
59
+ printf (" " );
60
+ }
61
+ printf ("0x%02X," , buf [i ]);
62
+ }
63
+ printf ("\n}\n" );
64
+ }
65
+
47
66
/* TODO Use CHECK_ILLEGAL(_VOID) everywhere and get rid of the uncounting callback */
48
67
/* CHECK that expr_or_stmt calls the illegal callback of ctx exactly once
49
68
*
@@ -3027,6 +3046,69 @@ static void run_field_convert(void) {
3027
3046
CHECK (secp256k1_memcmp_var (& fes2 , & fes , sizeof (fes )) == 0 );
3028
3047
}
3029
3048
3049
+ static void run_field_be32_overflow (void ) {
3050
+ {
3051
+ static const unsigned char zero_overflow [32 ] = {
3052
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3053
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3054
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3055
+ 0xFF , 0xFF , 0xFF , 0xFE , 0xFF , 0xFF , 0xFC , 0x2F ,
3056
+ };
3057
+ static const unsigned char zero [32 ] = { 0x00 };
3058
+ unsigned char out [32 ];
3059
+ secp256k1_fe fe ;
3060
+ CHECK (secp256k1_fe_set_b32 (& fe , zero_overflow ) == 0 );
3061
+ CHECK (secp256k1_fe_normalizes_to_zero (& fe ) == 1 );
3062
+ secp256k1_fe_normalize (& fe );
3063
+ CHECK (secp256k1_fe_is_zero (& fe ) == 1 );
3064
+ secp256k1_fe_get_b32 (out , & fe );
3065
+ CHECK (secp256k1_memcmp_var (out , zero , 32 ) == 0 );
3066
+ }
3067
+ {
3068
+ static const unsigned char one_overflow [32 ] = {
3069
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3070
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3071
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3072
+ 0xFF , 0xFF , 0xFF , 0xFE , 0xFF , 0xFF , 0xFC , 0x30 ,
3073
+ };
3074
+ static const unsigned char one [32 ] = {
3075
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
3076
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
3077
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
3078
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 ,
3079
+ };
3080
+ unsigned char out [32 ];
3081
+ secp256k1_fe fe ;
3082
+ CHECK (secp256k1_fe_set_b32 (& fe , one_overflow ) == 0 );
3083
+ secp256k1_fe_normalize (& fe );
3084
+ CHECK (secp256k1_fe_cmp_var (& fe , & secp256k1_fe_one ) == 0 );
3085
+ secp256k1_fe_get_b32 (out , & fe );
3086
+ CHECK (secp256k1_memcmp_var (out , one , 32 ) == 0 );
3087
+ }
3088
+ {
3089
+ static const unsigned char ff_overflow [32 ] = {
3090
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3091
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3092
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3093
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
3094
+ };
3095
+ static const unsigned char ff [32 ] = {
3096
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
3097
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
3098
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
3099
+ 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x03 , 0xD0 ,
3100
+ };
3101
+ unsigned char out [32 ];
3102
+ secp256k1_fe fe ;
3103
+ const secp256k1_fe fe_ff = SECP256K1_FE_CONST (0 , 0 , 0 , 0 , 0 , 0 , 0x01 , 0x000003d0 );
3104
+ CHECK (secp256k1_fe_set_b32 (& fe , ff_overflow ) == 0 );
3105
+ secp256k1_fe_normalize (& fe );
3106
+ CHECK (secp256k1_fe_cmp_var (& fe , & fe_ff ) == 0 );
3107
+ secp256k1_fe_get_b32 (out , & fe );
3108
+ CHECK (secp256k1_memcmp_var (out , ff , 32 ) == 0 );
3109
+ }
3110
+ }
3111
+
3030
3112
/* Returns true if two field elements have the same representation. */
3031
3113
static int fe_identical (const secp256k1_fe * a , const secp256k1_fe * b ) {
3032
3114
int ret = 1 ;
@@ -7693,6 +7775,7 @@ int main(int argc, char **argv) {
7693
7775
run_field_half ();
7694
7776
run_field_misc ();
7695
7777
run_field_convert ();
7778
+ run_field_be32_overflow ();
7696
7779
run_fe_mul ();
7697
7780
run_sqr ();
7698
7781
run_sqrt ();
0 commit comments