Skip to content

Commit 597128d

Browse files
committed
Make num optional
1 parent 659b554 commit 597128d

File tree

9 files changed

+46
-6
lines changed

9 files changed

+46
-6
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/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/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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@ 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

7879
/** Get the order of the group as a number. */
7980
static void secp256k1_scalar_order_get_num(secp256k1_num_t *r);
81+
#endif
8082

8183
/** Compare two scalars. */
8284
static int secp256k1_scalar_eq(const secp256k1_scalar_t *a, const secp256k1_scalar_t *b);

src/scalar_impl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
#endif
2626

2727
typedef struct {
28+
#ifndef USE_NUM_NONE
2829
secp256k1_num_t order;
30+
#endif
2931
#ifdef USE_ENDOMORPHISM
3032
secp256k1_num_t a1b2, b1, a2;
3133
#endif
@@ -40,13 +42,15 @@ static void secp256k1_scalar_start(void) {
4042
/* Allocate. */
4143
secp256k1_scalar_consts_t *ret = (secp256k1_scalar_consts_t*)malloc(sizeof(secp256k1_scalar_consts_t));
4244

45+
#ifndef USE_NUM_NONE
4346
static const unsigned char secp256k1_scalar_consts_order[] = {
4447
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
4548
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
4649
0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
4750
0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41
4851
};
4952
secp256k1_num_set_bin(&ret->order, secp256k1_scalar_consts_order, sizeof(secp256k1_scalar_consts_order));
53+
#endif
5054
#ifdef USE_ENDOMORPHISM
5155
static const unsigned char secp256k1_scalar_consts_a1b2[] = {
5256
0x30,0x86,0xd2,0x21,0xa7,0xd4,0x6b,0xcd,
@@ -80,6 +84,7 @@ static void secp256k1_scalar_stop(void) {
8084
free(c);
8185
}
8286

87+
#ifndef USE_NUM_NONE
8388
static void secp256k1_scalar_get_num(secp256k1_num_t *r, const secp256k1_scalar_t *a) {
8489
unsigned char c[32];
8590
secp256k1_scalar_get_b32(c, a);
@@ -89,6 +94,7 @@ static void secp256k1_scalar_get_num(secp256k1_num_t *r, const secp256k1_scalar_
8994
static void secp256k1_scalar_order_get_num(secp256k1_num_t *r) {
9095
*r = secp256k1_scalar_consts->order;
9196
}
97+
#endif
9298

9399
static void secp256k1_scalar_inverse(secp256k1_scalar_t *r, const secp256k1_scalar_t *x) {
94100
/* First compute x ^ (2^N - 1) for some values of N. */

src/tests.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void random_scalar_order(secp256k1_scalar_t *num) {
9191

9292
/***** NUM TESTS *****/
9393

94+
#ifndef USE_NUM_NONE
9495
void random_num_negate(secp256k1_num_t *num) {
9596
if (secp256k1_rand32() & 1)
9697
secp256k1_num_negate(num);
@@ -184,6 +185,7 @@ void run_num_smalltests(void) {
184185
test_num_add_sub();
185186
}
186187
}
188+
#endif
187189

188190
/***** SCALAR TESTS *****/
189191

@@ -203,6 +205,7 @@ void scalar_test(void) {
203205
random_scalar_order_test(&s2);
204206
secp256k1_scalar_get_b32(c, &s2);
205207

208+
#ifndef USE_NUM_NONE
206209
secp256k1_num_t snum, s1num, s2num;
207210
secp256k1_scalar_get_num(&snum, &s);
208211
secp256k1_scalar_get_num(&s1num, &s1);
@@ -212,6 +215,7 @@ void scalar_test(void) {
212215
secp256k1_scalar_order_get_num(&order);
213216
secp256k1_num_t half_order = order;
214217
secp256k1_num_shift(&half_order, 1);
218+
#endif
215219

216220
{
217221
/* Test that fetching groups of 4 bits from a scalar and recursing n(i)=16*n(i-1)+p(i) reconstructs it. */
@@ -249,6 +253,7 @@ void scalar_test(void) {
249253
CHECK(secp256k1_scalar_eq(&n, &s));
250254
}
251255

256+
#ifndef USE_NUM_NONE
252257
{
253258
/* Test that adding the scalars together is equal to adding their numbers together modulo the order. */
254259
secp256k1_num_t rnum;
@@ -303,17 +308,20 @@ void scalar_test(void) {
303308
/* Negating zero should still result in zero. */
304309
CHECK(secp256k1_scalar_is_zero(&neg));
305310
}
311+
#endif
306312

307313
{
308314
/* Test that scalar inverses are equal to the inverse of their number modulo the order. */
309315
if (!secp256k1_scalar_is_zero(&s)) {
310316
secp256k1_scalar_t inv;
311317
secp256k1_scalar_inverse(&inv, &s);
318+
#ifndef USE_NUM_NONE
312319
secp256k1_num_t invnum;
313320
secp256k1_num_mod_inverse(&invnum, &snum, &order);
314321
secp256k1_num_t invnum2;
315322
secp256k1_scalar_get_num(&invnum2, &inv);
316323
CHECK(secp256k1_num_eq(&invnum, &invnum2));
324+
#endif
317325
secp256k1_scalar_mul(&inv, &inv, &s);
318326
/* Multiplying a scalar with its inverse must result in one. */
319327
CHECK(secp256k1_scalar_is_one(&inv));
@@ -411,6 +419,7 @@ void run_scalar_tests(void) {
411419
CHECK(secp256k1_scalar_is_zero(&o));
412420
}
413421

422+
#ifndef USE_NUM_NONE
414423
{
415424
// A scalar with value of the curve order should be 0.
416425
secp256k1_num_t order;
@@ -423,6 +432,7 @@ void run_scalar_tests(void) {
423432
CHECK(overflow == 1);
424433
CHECK(secp256k1_scalar_is_zero(&zero));
425434
}
435+
#endif
426436
}
427437

428438
/***** FIELD TESTS *****/
@@ -1080,8 +1090,10 @@ int main(int argc, char **argv) {
10801090
/* initialize */
10811091
secp256k1_start(SECP256K1_START_SIGN | SECP256K1_START_VERIFY);
10821092

1093+
#ifndef USE_NUM_NONE
10831094
/* num tests */
10841095
run_num_smalltests();
1096+
#endif
10851097

10861098
/* scalar tests */
10871099
run_scalar_tests();

0 commit comments

Comments
 (0)