Skip to content

Commit e9fd3df

Browse files
field: Improve docs and tests of secp256k1_fe_set_b32
1 parent ca92a35 commit e9fd3df

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/field.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ static int secp256k1_fe_equal_var(const secp256k1_fe *a, const secp256k1_fe *b);
7575
/** Compare two field elements. Requires both inputs to be normalized */
7676
static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b);
7777

78-
/** Set a field element equal to 32-byte big endian value. If successful, the resulting field element is normalized. */
78+
/** Set a field element equal to 32-byte big endian value.
79+
* Returns 1 if no overflow occurred, and then the output is normalized.
80+
* Returns 0 if overflow occurred, and then the output is only weakly normalized. */
7981
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a);
8082

8183
/** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */

src/tests.c

+64
Original file line numberDiff line numberDiff line change
@@ -2961,6 +2961,69 @@ static void run_field_convert(void) {
29612961
CHECK(secp256k1_memcmp_var(&fes2, &fes, sizeof(fes)) == 0);
29622962
}
29632963

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+
29643027
/* Returns true if two field elements have the same representation. */
29653028
static int fe_identical(const secp256k1_fe *a, const secp256k1_fe *b) {
29663029
int ret = 1;
@@ -7515,6 +7578,7 @@ int main(int argc, char **argv) {
75157578
run_field_half();
75167579
run_field_misc();
75177580
run_field_convert();
7581+
run_field_be32_overflow();
75187582
run_fe_mul();
75197583
run_sqr();
75207584
run_sqrt();

0 commit comments

Comments
 (0)