Skip to content

Commit 65d82a3

Browse files
committed
Abstract out verify logic for fe_negate
1 parent 1446708 commit 65d82a3

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

src/field.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
8787
# define secp256k1_fe_cmp_var secp256k1_fe_impl_cmp_var
8888
# define secp256k1_fe_set_b32 secp256k1_fe_impl_set_b32
8989
# define secp256k1_fe_get_b32 secp256k1_fe_impl_get_b32
90+
# define secp256k1_fe_negate secp256k1_fe_impl_negate
9091
#endif /* !defined(VERIFY) */
9192

9293
/** Normalize a field element.
@@ -192,8 +193,13 @@ static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a);
192193
*/
193194
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a);
194195

195-
/** Set a field element equal to the additive inverse of another. Takes a maximum magnitude of the input
196-
* as an argument. The magnitude of the output is one higher. */
196+
/** Negate a field element.
197+
*
198+
* On input, r does not need to be initialized. a must be a valid field element with
199+
* magnitude not exceeding m. m must be an integer in [0,31].
200+
* Performs {r = -a}.
201+
* On output, r will not be normalized, and will have magnitude m+1.
202+
*/
197203
static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m);
198204

199205
/** Adds a small integer (up to 0x7FFF) to r. The resulting magnitude increases by one. */

src/field_10x26_impl.h

+5-10
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,15 @@ static void secp256k1_fe_impl_get_b32(unsigned char *r, const secp256k1_fe *a) {
349349
r[31] = a->n[0] & 0xff;
350350
}
351351

352-
SECP256K1_INLINE static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m) {
353-
#ifdef VERIFY
354-
VERIFY_CHECK(a->magnitude <= m);
355-
secp256k1_fe_verify(a);
352+
SECP256K1_INLINE static void secp256k1_fe_impl_negate(secp256k1_fe *r, const secp256k1_fe *a, int m) {
353+
/* For all legal values of m (0..31), the following properties hold: */
356354
VERIFY_CHECK(0x3FFFC2FUL * 2 * (m + 1) >= 0x3FFFFFFUL * 2 * m);
357355
VERIFY_CHECK(0x3FFFFBFUL * 2 * (m + 1) >= 0x3FFFFFFUL * 2 * m);
358356
VERIFY_CHECK(0x3FFFFFFUL * 2 * (m + 1) >= 0x3FFFFFFUL * 2 * m);
359357
VERIFY_CHECK(0x03FFFFFUL * 2 * (m + 1) >= 0x03FFFFFUL * 2 * m);
360-
#endif
358+
359+
/* Due to the properties above, the left hand in the subtractions below is never less than
360+
* the right hand. */
361361
r->n[0] = 0x3FFFC2FUL * 2 * (m + 1) - a->n[0];
362362
r->n[1] = 0x3FFFFBFUL * 2 * (m + 1) - a->n[1];
363363
r->n[2] = 0x3FFFFFFUL * 2 * (m + 1) - a->n[2];
@@ -368,11 +368,6 @@ SECP256K1_INLINE static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k
368368
r->n[7] = 0x3FFFFFFUL * 2 * (m + 1) - a->n[7];
369369
r->n[8] = 0x3FFFFFFUL * 2 * (m + 1) - a->n[8];
370370
r->n[9] = 0x03FFFFFUL * 2 * (m + 1) - a->n[9];
371-
#ifdef VERIFY
372-
r->magnitude = m + 1;
373-
r->normalized = 0;
374-
secp256k1_fe_verify(r);
375-
#endif
376371
}
377372

378373
SECP256K1_INLINE static void secp256k1_fe_mul_int(secp256k1_fe *r, int a) {

src/field_5x52_impl.h

+5-10
Original file line numberDiff line numberDiff line change
@@ -318,24 +318,19 @@ static void secp256k1_fe_impl_get_b32(unsigned char *r, const secp256k1_fe *a) {
318318
r[31] = a->n[0] & 0xFF;
319319
}
320320

321-
SECP256K1_INLINE static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m) {
322-
#ifdef VERIFY
323-
VERIFY_CHECK(a->magnitude <= m);
324-
secp256k1_fe_verify(a);
321+
SECP256K1_INLINE static void secp256k1_fe_impl_negate(secp256k1_fe *r, const secp256k1_fe *a, int m) {
322+
/* For all legal values of m (0..31), the following properties hold: */
325323
VERIFY_CHECK(0xFFFFEFFFFFC2FULL * 2 * (m + 1) >= 0xFFFFFFFFFFFFFULL * 2 * m);
326324
VERIFY_CHECK(0xFFFFFFFFFFFFFULL * 2 * (m + 1) >= 0xFFFFFFFFFFFFFULL * 2 * m);
327325
VERIFY_CHECK(0x0FFFFFFFFFFFFULL * 2 * (m + 1) >= 0x0FFFFFFFFFFFFULL * 2 * m);
328-
#endif
326+
327+
/* Due to the properties above, the left hand in the subtractions below is never less than
328+
* the right hand. */
329329
r->n[0] = 0xFFFFEFFFFFC2FULL * 2 * (m + 1) - a->n[0];
330330
r->n[1] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[1];
331331
r->n[2] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[2];
332332
r->n[3] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[3];
333333
r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * (m + 1) - a->n[4];
334-
#ifdef VERIFY
335-
r->magnitude = m + 1;
336-
r->normalized = 0;
337-
secp256k1_fe_verify(r);
338-
#endif
339334
}
340335

341336
SECP256K1_INLINE static void secp256k1_fe_mul_int(secp256k1_fe *r, int a) {

src/field_impl.h

+11
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,17 @@ SECP256K1_INLINE static void secp256k1_fe_get_b32(unsigned char *r, const secp25
253253
VERIFY_CHECK(a->normalized);
254254
secp256k1_fe_impl_get_b32(r, a);
255255
}
256+
257+
static void secp256k1_fe_impl_negate(secp256k1_fe *r, const secp256k1_fe *a, int m);
258+
SECP256K1_INLINE static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m) {
259+
secp256k1_fe_verify(a);
260+
VERIFY_CHECK(m >= 0 && m <= 31);
261+
VERIFY_CHECK(a->magnitude <= m);
262+
secp256k1_fe_impl_negate(r, a, m);
263+
r->magnitude = m + 1;
264+
r->normalized = 0;
265+
secp256k1_fe_verify(r);
266+
}
256267
#endif /* defined(VERIFY) */
257268

258269
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)