Skip to content

Commit bd313f7

Browse files
committed
Merge pull request #119
597128d Make num optional (Pieter Wuille) 659b554 Make constant initializers independent from num (Pieter Wuille)
2 parents 276f987 + 597128d commit bd313f7

File tree

12 files changed

+139
-101
lines changed

12 files changed

+139
-101
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ env:
1818
- FIELD=64bit ENDOMORPHISM=yes
1919
- FIELD=32bit
2020
- FIELD=32bit ENDOMORPHISM=yes
21+
- BIGNUM=none
2122
- BUILD=distcheck
2223
- EXTRAFLAGS=CFLAGS=-DDETERMINISTIC
2324
before_script: ./autogen.sh

configure.ac

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ AC_ARG_ENABLE(endomorphism,
9595
AC_ARG_WITH([field], [AS_HELP_STRING([--with-field=gmp|64bit|64bit_asm|32bit|auto],
9696
[Specify Field Implementation. Default is auto])],[req_field=$withval], [req_field=auto])
9797

98-
AC_ARG_WITH([bignum], [AS_HELP_STRING([--with-bignum=gmp|auto],
98+
AC_ARG_WITH([bignum], [AS_HELP_STRING([--with-bignum=gmp|none|auto],
9999
[Specify Bignum Implementation. Default is auto])],[req_bignum=$withval], [req_bignum=auto])
100100

101101
AC_ARG_WITH([scalar], [AS_HELP_STRING([--with-scalar=64bit|32bit|auto],
@@ -179,16 +179,15 @@ if test x"$req_bignum" = x"auto"; then
179179
fi
180180

181181
if test x"$set_bignum" = x; then
182-
AC_MSG_ERROR([no working bignum implementation found])
182+
set_bignum=none
183183
fi
184184
else
185185
set_bignum=$req_bignum
186186
case $set_bignum in
187187
gmp)
188188
SECP_GMP_CHECK
189189
;;
190-
openssl)
191-
SECP_OPENSSL_CHECK
190+
none)
192191
;;
193192
*)
194193
AC_MSG_ERROR([invalid bignum implementation selection])
@@ -221,11 +220,16 @@ esac
221220
# select bignum implementation
222221
case $set_bignum in
223222
gmp)
224-
AC_DEFINE(HAVE_LIBGMP,1,[Define this symbol if libgmp is installed])
225-
AC_DEFINE(USE_NUM_GMP, 1, [Define this symbol to use the gmp implementation])
223+
AC_DEFINE(HAVE_LIBGMP, 1, [Define this symbol if libgmp is installed])
224+
AC_DEFINE(USE_NUM_GMP, 1, [Define this symbol to use the gmp implementation for num])
226225
AC_DEFINE(USE_FIELD_INV_NUM, 1, [Define this symbol to use the num-based field inverse implementation])
227226
AC_DEFINE(USE_SCALAR_INV_NUM, 1, [Define this symbol to use the num-based scalar inverse implementation])
228227
;;
228+
none)
229+
AC_DEFINE(USE_NUM_NONE, 1, [Define this symbol to use no num implementation])
230+
AC_DEFINE(USE_FIELD_INV_BUILTIN, 1, [Define this symbol to use the native field inverse implementation])
231+
AC_DEFINE(USE_SCALAR_INV_BUILTIN, 1, [Define this symbol to use the native scalar inverse implementation])
232+
;;
229233
*)
230234
AC_MSG_ERROR([invalid bignum implementation])
231235
;;
@@ -266,6 +270,9 @@ if test x"$set_field" = x"gmp" || test x"$set_bignum" = x"gmp"; then
266270
fi
267271

268272
if test x"$use_endomorphism" = x"yes"; then
273+
if test x"$set_bignum" = x"none"; then
274+
AC_MSG_ERROR([Cannot use endomorphism optimization without a bignum implementation])
275+
fi
269276
AC_DEFINE(USE_ENDOMORPHISM, 1, [Define this symbol to use endomorphism])
270277
fi
271278

src/ecdsa_impl.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ static void secp256k1_ecdsa_start(void) {
2929
/* Allocate. */
3030
secp256k1_ecdsa_consts_t *ret = (secp256k1_ecdsa_consts_t*)malloc(sizeof(secp256k1_ecdsa_consts_t));
3131

32-
unsigned char p[32];
33-
secp256k1_num_get_bin(p, 32, &secp256k1_ge_consts->order);
34-
secp256k1_fe_set_b32(&ret->order_as_fe, p);
35-
32+
static const unsigned char order[] = {
33+
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
34+
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
35+
0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
36+
0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41
37+
};
38+
39+
secp256k1_fe_set_b32(&ret->order_as_fe, order);
3640
secp256k1_fe_negate(&ret->p_minus_order, &ret->order_as_fe, 1);
3741
secp256k1_fe_normalize(&ret->p_minus_order);
3842

src/field.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
#endif
3434

3535
typedef struct {
36+
#ifndef USE_NUM_NONE
3637
secp256k1_num_t p;
38+
#endif
3739
secp256k1_fe_t order;
3840
} secp256k1_fe_consts_t;
3941

src/field_impl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,20 @@ static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe_t r[len], const se
267267
}
268268

269269
static void secp256k1_fe_start(void) {
270+
#ifndef USE_NUM_NONE
270271
static const unsigned char secp256k1_fe_consts_p[] = {
271272
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
272273
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
273274
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
274275
0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F
275276
};
277+
#endif
276278
if (secp256k1_fe_consts == NULL) {
277279
secp256k1_fe_inner_start();
278280
secp256k1_fe_consts_t *ret = (secp256k1_fe_consts_t*)malloc(sizeof(secp256k1_fe_consts_t));
281+
#ifndef USE_NUM_NONE
279282
secp256k1_num_set_bin(&ret->p, secp256k1_fe_consts_p, sizeof(secp256k1_fe_consts_p));
283+
#endif
280284
secp256k1_fe_consts = ret;
281285
}
282286
}

src/group.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ typedef struct {
2727

2828
/** Global constants related to the group */
2929
typedef struct {
30-
secp256k1_num_t order; /* the order of the curve (= order of its generator) */
31-
secp256k1_num_t half_order; /* half the order of the curve (= order of its generator) */
3230
secp256k1_ge_t g; /* the generator point */
3331

3432
#ifdef USE_ENDOMORPHISM

src/group_impl.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,6 @@ static void secp256k1_gej_mul_lambda(secp256k1_gej_t *r, const secp256k1_gej_t *
413413

414414

415415
static void secp256k1_ge_start(void) {
416-
static const unsigned char secp256k1_ge_consts_order[] = {
417-
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
418-
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
419-
0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
420-
0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41
421-
};
422416
static const unsigned char secp256k1_ge_consts_g_x[] = {
423417
0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,
424418
0x55,0xA0,0x62,0x95,0xCE,0x87,0x0B,0x07,
@@ -442,9 +436,6 @@ static void secp256k1_ge_start(void) {
442436
#endif
443437
if (secp256k1_ge_consts == NULL) {
444438
secp256k1_ge_consts_t *ret = (secp256k1_ge_consts_t*)malloc(sizeof(secp256k1_ge_consts_t));
445-
secp256k1_num_set_bin(&ret->order, secp256k1_ge_consts_order, sizeof(secp256k1_ge_consts_order));
446-
secp256k1_num_copy(&ret->half_order, &ret->order);
447-
secp256k1_num_shift(&ret->half_order, 1);
448439
#ifdef USE_ENDOMORPHISM
449440
VERIFY_CHECK(secp256k1_fe_set_b32(&ret->beta, secp256k1_ge_consts_beta));
450441
#endif

src/num.h

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

10+
#ifndef USE_NUM_NONE
11+
1012
#if defined HAVE_CONFIG_H
1113
#include "libsecp256k1-config.h"
1214
#endif
@@ -65,3 +67,5 @@ static int secp256k1_num_is_neg(const secp256k1_num_t *a);
6567
static void secp256k1_num_negate(secp256k1_num_t *r);
6668

6769
#endif
70+
71+
#endif

src/num_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#if defined(USE_NUM_GMP)
1717
#include "num_gmp_impl.h"
18+
#elif defined(USE_NUM_NONE)
19+
/* Nothing. */
1820
#else
1921
#error "Please select num implementation"
2022
#endif

src/scalar.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@ static int secp256k1_scalar_is_one(const secp256k1_scalar_t *a);
7272
/** Check whether a scalar is higher than the group order divided by 2. */
7373
static int secp256k1_scalar_is_high(const secp256k1_scalar_t *a);
7474

75+
#ifndef USE_NUM_NONE
7576
/** Convert a scalar to a number. */
7677
static void secp256k1_scalar_get_num(secp256k1_num_t *r, const secp256k1_scalar_t *a);
7778

79+
/** Get the order of the group as a number. */
80+
static void secp256k1_scalar_order_get_num(secp256k1_num_t *r);
81+
#endif
82+
7883
/** Compare two scalars. */
7984
static int secp256k1_scalar_eq(const secp256k1_scalar_t *a, const secp256k1_scalar_t *b);
8085

0 commit comments

Comments
 (0)