Skip to content

Commit e179e65

Browse files
committed
Abstract out verify logic for fe_add
1 parent 7e7ad7f commit e179e65

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

src/field.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
8989
# define secp256k1_fe_get_b32 secp256k1_fe_impl_get_b32
9090
# define secp256k1_fe_negate secp256k1_fe_impl_negate
9191
# define secp256k1_fe_mul_int secp256k1_fe_impl_mul_int
92+
# define secp256k1_fe_add secp256k1_fe_impl_add
9293
#endif /* !defined(VERIFY) */
9394

9495
/** Normalize a field element.
@@ -215,7 +216,13 @@ static void secp256k1_fe_add_int(secp256k1_fe *r, int a);
215216
*/
216217
static void secp256k1_fe_mul_int(secp256k1_fe *r, int a);
217218

218-
/** Adds a field element to another. The result has the sum of the inputs' magnitudes as magnitude. */
219+
/** Increment a field element by another.
220+
*
221+
* On input, r and a must be valid field elements, not necessarily normalized.
222+
* The sum of their magnitudes must not exceed 32.
223+
* Performs {r += a}.
224+
* On output, r will not be normalized, and will have magnitude incremented by a's.
225+
*/
219226
static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a);
220227

221228
/** Sets a field element to be the product of two others. Requires the inputs' magnitudes to be at most 8.

src/field_10x26_impl.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,7 @@ SECP256K1_INLINE static void secp256k1_fe_impl_mul_int(secp256k1_fe *r, int a) {
383383
r->n[9] *= a;
384384
}
385385

386-
SECP256K1_INLINE static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a) {
387-
secp256k1_fe_verify(a);
386+
SECP256K1_INLINE static void secp256k1_fe_impl_add(secp256k1_fe *r, const secp256k1_fe *a) {
388387
r->n[0] += a->n[0];
389388
r->n[1] += a->n[1];
390389
r->n[2] += a->n[2];
@@ -395,11 +394,6 @@ SECP256K1_INLINE static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_f
395394
r->n[7] += a->n[7];
396395
r->n[8] += a->n[8];
397396
r->n[9] += a->n[9];
398-
#ifdef VERIFY
399-
r->magnitude += a->magnitude;
400-
r->normalized = 0;
401-
secp256k1_fe_verify(r);
402-
#endif
403397
}
404398

405399
SECP256K1_INLINE static void secp256k1_fe_add_int(secp256k1_fe *r, int a) {

src/field_5x52_impl.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -353,18 +353,12 @@ SECP256K1_INLINE static void secp256k1_fe_add_int(secp256k1_fe *r, int a) {
353353
#endif
354354
}
355355

356-
SECP256K1_INLINE static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a) {
357-
secp256k1_fe_verify(a);
356+
SECP256K1_INLINE static void secp256k1_fe_impl_add(secp256k1_fe *r, const secp256k1_fe *a) {
358357
r->n[0] += a->n[0];
359358
r->n[1] += a->n[1];
360359
r->n[2] += a->n[2];
361360
r->n[3] += a->n[3];
362361
r->n[4] += a->n[4];
363-
#ifdef VERIFY
364-
r->magnitude += a->magnitude;
365-
r->normalized = 0;
366-
secp256k1_fe_verify(r);
367-
#endif
368362
}
369363

370364
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe * SECP256K1_RESTRICT b) {

src/field_impl.h

+11
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ SECP256K1_INLINE static void secp256k1_fe_mul_int(secp256k1_fe *r, int a) {
275275
r->normalized = 0;
276276
secp256k1_fe_verify(r);
277277
}
278+
279+
static void secp256k1_fe_impl_add(secp256k1_fe *r, const secp256k1_fe *a);
280+
SECP256K1_INLINE static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a) {
281+
secp256k1_fe_verify(r);
282+
secp256k1_fe_verify(a);
283+
VERIFY_CHECK(r->magnitude + a->magnitude <= 32);
284+
secp256k1_fe_impl_add(r, a);
285+
r->magnitude += a->magnitude;
286+
r->normalized = 0;
287+
secp256k1_fe_verify(r);
288+
}
278289
#endif /* defined(VERIFY) */
279290

280291
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)