Skip to content

Commit 4c25f6e

Browse files
committed
Abstract out verify logic for fe_mul
1 parent e179e65 commit 4c25f6e

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

src/field.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ static const secp256k1_fe secp256k1_const_beta = SECP256K1_FE_CONST(
9090
# define secp256k1_fe_negate secp256k1_fe_impl_negate
9191
# define secp256k1_fe_mul_int secp256k1_fe_impl_mul_int
9292
# define secp256k1_fe_add secp256k1_fe_impl_add
93+
# define secp256k1_fe_mul secp256k1_fe_impl_mul
9394
#endif /* !defined(VERIFY) */
9495

9596
/** Normalize a field element.
@@ -225,8 +226,14 @@ static void secp256k1_fe_mul_int(secp256k1_fe *r, int a);
225226
*/
226227
static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a);
227228

228-
/** Sets a field element to be the product of two others. Requires the inputs' magnitudes to be at most 8.
229-
* The output magnitude is 1 (but not guaranteed to be normalized). */
229+
/** Multiply two field elements.
230+
*
231+
* On input, a and b must be valid field elements; r does not need to be initialized.
232+
* r and a may point to the same object, but neither can be equal to b. The magnitudes
233+
* of a and b must not exceed 8.
234+
* Performs {r = a * b}
235+
* On output, r will have magnitude 1, but won't be normalized.
236+
*/
230237
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe * SECP256K1_RESTRICT b);
231238

232239
/** Sets a field element to be the square of another. Requires the input's magnitude to be at most 8.

src/field_10x26_impl.h

+1-14
Original file line numberDiff line numberDiff line change
@@ -1027,21 +1027,8 @@ SECP256K1_INLINE static void secp256k1_fe_sqr_inner(uint32_t *r, const uint32_t
10271027
}
10281028
#endif
10291029

1030-
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe * SECP256K1_RESTRICT b) {
1031-
#ifdef VERIFY
1032-
VERIFY_CHECK(a->magnitude <= 8);
1033-
VERIFY_CHECK(b->magnitude <= 8);
1034-
secp256k1_fe_verify(a);
1035-
secp256k1_fe_verify(b);
1036-
VERIFY_CHECK(r != b);
1037-
VERIFY_CHECK(a != b);
1038-
#endif
1030+
SECP256K1_INLINE static void secp256k1_fe_impl_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe * SECP256K1_RESTRICT b) {
10391031
secp256k1_fe_mul_inner(r->n, a->n, b->n);
1040-
#ifdef VERIFY
1041-
r->magnitude = 1;
1042-
r->normalized = 0;
1043-
secp256k1_fe_verify(r);
1044-
#endif
10451032
}
10461033

10471034
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a) {

src/field_5x52_impl.h

+1-14
Original file line numberDiff line numberDiff line change
@@ -361,21 +361,8 @@ SECP256K1_INLINE static void secp256k1_fe_impl_add(secp256k1_fe *r, const secp25
361361
r->n[4] += a->n[4];
362362
}
363363

364-
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe * SECP256K1_RESTRICT b) {
365-
#ifdef VERIFY
366-
VERIFY_CHECK(a->magnitude <= 8);
367-
VERIFY_CHECK(b->magnitude <= 8);
368-
secp256k1_fe_verify(a);
369-
secp256k1_fe_verify(b);
370-
VERIFY_CHECK(r != b);
371-
VERIFY_CHECK(a != b);
372-
#endif
364+
SECP256K1_INLINE static void secp256k1_fe_impl_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe * SECP256K1_RESTRICT b) {
373365
secp256k1_fe_mul_inner(r->n, a->n, b->n);
374-
#ifdef VERIFY
375-
r->magnitude = 1;
376-
r->normalized = 0;
377-
secp256k1_fe_verify(r);
378-
#endif
379366
}
380367

381368
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a) {

src/field_impl.h

+14
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,20 @@ SECP256K1_INLINE static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_f
286286
r->normalized = 0;
287287
secp256k1_fe_verify(r);
288288
}
289+
290+
static void secp256k1_fe_impl_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe * SECP256K1_RESTRICT b);
291+
SECP256K1_INLINE static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe * SECP256K1_RESTRICT b) {
292+
secp256k1_fe_verify(a);
293+
secp256k1_fe_verify(b);
294+
VERIFY_CHECK(a->magnitude <= 8);
295+
VERIFY_CHECK(b->magnitude <= 8);
296+
VERIFY_CHECK(r != b);
297+
VERIFY_CHECK(a != b);
298+
secp256k1_fe_impl_mul(r, a, b);
299+
r->magnitude = 1;
300+
r->normalized = 0;
301+
secp256k1_fe_verify(r);
302+
}
289303
#endif /* defined(VERIFY) */
290304

291305
#endif /* SECP256K1_FIELD_IMPL_H */

0 commit comments

Comments
 (0)