Skip to content

Commit 5e84c28

Browse files
modinv: Avoid including stdlib.h
1 parent 5c55a27 commit 5e84c28

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/modinv32.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef SECP256K1_MODINV32_H
88
#define SECP256K1_MODINV32_H
99

10+
#include <stdint.h>
11+
1012
#include "util.h"
1113

1214
/* A signed 30-bit limb representation of integers.

src/modinv32_impl.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88
#define SECP256K1_MODINV32_IMPL_H
99

1010
#include "modinv32.h"
11-
1211
#include "util.h"
1312

14-
#include <stdlib.h>
15-
1613
/* This file implements modular inversion based on the paper "Fast constant-time gcd computation and
1714
* modular inversion" by Daniel J. Bernstein and Bo-Yin Yang.
1815
*
@@ -21,6 +18,14 @@
2118
*/
2219

2320
#ifdef VERIFY
21+
/* Helper function to compute the absolute value of an int32_t.
22+
* (We don't use abs/labs/llabs as they depend on the int sizes and require stdlib.h.) */
23+
static int64_t secp256k1_modinv32_abs(int32_t v) {
24+
VERIFY_CHECK(v > INT32_MIN);
25+
if (v < 0) return -v;
26+
return v;
27+
}
28+
2429
static const secp256k1_modinv32_signed30 SECP256K1_SIGNED30_ONE = {{1}};
2530

2631
/* Compute a*factor and put it in r. All but the top limb in r will be in range [0,2^30). */
@@ -415,8 +420,8 @@ static void secp256k1_modinv32_update_de_30(secp256k1_modinv32_signed30 *d, secp
415420
VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(d, 9, &modinfo->modulus, 1) < 0); /* d < modulus */
416421
VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(e, 9, &modinfo->modulus, -2) > 0); /* e > -2*modulus */
417422
VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(e, 9, &modinfo->modulus, 1) < 0); /* e < modulus */
418-
VERIFY_CHECK(labs(u) <= (M30 + 1 - labs(v))); /* |u|+|v| <= 2^30 */
419-
VERIFY_CHECK(labs(q) <= (M30 + 1 - labs(r))); /* |q|+|r| <= 2^30 */
423+
VERIFY_CHECK(secp256k1_modinv32_abs(u) <= (M30 + 1 - secp256k1_modinv32_abs(v))); /* |u|+|v| <= 2^30 */
424+
VERIFY_CHECK(secp256k1_modinv32_abs(q) <= (M30 + 1 - secp256k1_modinv32_abs(r))); /* |q|+|r| <= 2^30 */
420425

421426
/* [md,me] start as zero; plus [u,q] if d is negative; plus [v,r] if e is negative. */
422427
sd = d->v[8] >> 31;

src/modinv64_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ typedef struct {
2828

2929
#ifdef VERIFY
3030
/* Helper function to compute the absolute value of an int64_t.
31-
* (we don't use abs/labs/llabs as it depends on the int sizes). */
31+
* (We don't use abs/labs/llabs as they depend on the int sizes and require stdlib.h.) */
3232
static int64_t secp256k1_modinv64_abs(int64_t v) {
3333
VERIFY_CHECK(v > INT64_MIN);
3434
if (v < 0) return -v;

0 commit comments

Comments
 (0)