Skip to content

Commit 7e7ad7f

Browse files
committed
Abstract out verify logic for fe_mul_int
1 parent 65d82a3 commit 7e7ad7f

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

src/field.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
8888
# define secp256k1_fe_set_b32 secp256k1_fe_impl_set_b32
8989
# define secp256k1_fe_get_b32 secp256k1_fe_impl_get_b32
9090
# define secp256k1_fe_negate secp256k1_fe_impl_negate
91+
# define secp256k1_fe_mul_int secp256k1_fe_impl_mul_int
9192
#endif /* !defined(VERIFY) */
9293

9394
/** Normalize a field element.
@@ -205,8 +206,13 @@ static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m);
205206
/** Adds a small integer (up to 0x7FFF) to r. The resulting magnitude increases by one. */
206207
static void secp256k1_fe_add_int(secp256k1_fe *r, int a);
207208

208-
/** Multiplies the passed field element with a small integer constant. Multiplies the magnitude by that
209-
* small integer. */
209+
/** Multiply a field element with a small integer.
210+
*
211+
* On input, r must be a valid field element. a must be an integer in [0,32].
212+
* The magnitude of r times a must not exceed 32.
213+
* Performs {r *= a}.
214+
* On output, r's magnitude is multiplied by a, and r will not be normalized.
215+
*/
210216
static void secp256k1_fe_mul_int(secp256k1_fe *r, int a);
211217

212218
/** Adds a field element to another. The result has the sum of the inputs' magnitudes as magnitude. */

src/field_10x26_impl.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ SECP256K1_INLINE static void secp256k1_fe_impl_negate(secp256k1_fe *r, const sec
370370
r->n[9] = 0x03FFFFFUL * 2 * (m + 1) - a->n[9];
371371
}
372372

373-
SECP256K1_INLINE static void secp256k1_fe_mul_int(secp256k1_fe *r, int a) {
373+
SECP256K1_INLINE static void secp256k1_fe_impl_mul_int(secp256k1_fe *r, int a) {
374374
r->n[0] *= a;
375375
r->n[1] *= a;
376376
r->n[2] *= a;
@@ -381,11 +381,6 @@ SECP256K1_INLINE static void secp256k1_fe_mul_int(secp256k1_fe *r, int a) {
381381
r->n[7] *= a;
382382
r->n[8] *= a;
383383
r->n[9] *= a;
384-
#ifdef VERIFY
385-
r->magnitude *= a;
386-
r->normalized = 0;
387-
secp256k1_fe_verify(r);
388-
#endif
389384
}
390385

391386
SECP256K1_INLINE static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a) {

src/field_5x52_impl.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,12 @@ SECP256K1_INLINE static void secp256k1_fe_impl_negate(secp256k1_fe *r, const sec
333333
r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * (m + 1) - a->n[4];
334334
}
335335

336-
SECP256K1_INLINE static void secp256k1_fe_mul_int(secp256k1_fe *r, int a) {
336+
SECP256K1_INLINE static void secp256k1_fe_impl_mul_int(secp256k1_fe *r, int a) {
337337
r->n[0] *= a;
338338
r->n[1] *= a;
339339
r->n[2] *= a;
340340
r->n[3] *= a;
341341
r->n[4] *= a;
342-
#ifdef VERIFY
343-
r->magnitude *= a;
344-
r->normalized = 0;
345-
secp256k1_fe_verify(r);
346-
#endif
347342
}
348343

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

src/field_impl.h

+11
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,17 @@ SECP256K1_INLINE static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k
264264
r->normalized = 0;
265265
secp256k1_fe_verify(r);
266266
}
267+
268+
static void secp256k1_fe_impl_mul_int(secp256k1_fe *r, int a);
269+
SECP256K1_INLINE static void secp256k1_fe_mul_int(secp256k1_fe *r, int a) {
270+
secp256k1_fe_verify(r);
271+
VERIFY_CHECK(a >= 0 && a <= 32);
272+
VERIFY_CHECK(a*r->magnitude <= 32);
273+
secp256k1_fe_impl_mul_int(r, a);
274+
r->magnitude *= a;
275+
r->normalized = 0;
276+
secp256k1_fe_verify(r);
277+
}
267278
#endif /* defined(VERIFY) */
268279

269280
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)