From 597128d389fe6bf1c85e6aa09c08e0083c89ff43 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Fri, 28 Nov 2014 01:23:55 +0100 Subject: [PATCH] Make num optional --- .travis.yml | 1 + configure.ac | 19 +++++++++++++------ src/field.h | 2 ++ src/field_impl.h | 4 ++++ src/num.h | 4 ++++ src/num_impl.h | 2 ++ src/scalar.h | 2 ++ src/scalar_impl.h | 6 ++++++ src/tests.c | 12 ++++++++++++ 9 files changed, 46 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 24a86b561b..12692deafa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,6 +18,7 @@ env: - FIELD=64bit ENDOMORPHISM=yes - FIELD=32bit - FIELD=32bit ENDOMORPHISM=yes + - BIGNUM=none - BUILD=distcheck - EXTRAFLAGS=CFLAGS=-DDETERMINISTIC before_script: ./autogen.sh diff --git a/configure.ac b/configure.ac index 88ca8dfa58..f0d69f93de 100644 --- a/configure.ac +++ b/configure.ac @@ -95,7 +95,7 @@ AC_ARG_ENABLE(endomorphism, AC_ARG_WITH([field], [AS_HELP_STRING([--with-field=gmp|64bit|64bit_asm|32bit|auto], [Specify Field Implementation. Default is auto])],[req_field=$withval], [req_field=auto]) -AC_ARG_WITH([bignum], [AS_HELP_STRING([--with-bignum=gmp|auto], +AC_ARG_WITH([bignum], [AS_HELP_STRING([--with-bignum=gmp|none|auto], [Specify Bignum Implementation. Default is auto])],[req_bignum=$withval], [req_bignum=auto]) AC_ARG_WITH([scalar], [AS_HELP_STRING([--with-scalar=64bit|32bit|auto], @@ -179,7 +179,7 @@ if test x"$req_bignum" = x"auto"; then fi if test x"$set_bignum" = x; then - AC_MSG_ERROR([no working bignum implementation found]) + set_bignum=none fi else set_bignum=$req_bignum @@ -187,8 +187,7 @@ else gmp) SECP_GMP_CHECK ;; - openssl) - SECP_OPENSSL_CHECK + none) ;; *) AC_MSG_ERROR([invalid bignum implementation selection]) @@ -221,11 +220,16 @@ esac # select bignum implementation case $set_bignum in gmp) - AC_DEFINE(HAVE_LIBGMP,1,[Define this symbol if libgmp is installed]) - AC_DEFINE(USE_NUM_GMP, 1, [Define this symbol to use the gmp implementation]) + AC_DEFINE(HAVE_LIBGMP, 1, [Define this symbol if libgmp is installed]) + AC_DEFINE(USE_NUM_GMP, 1, [Define this symbol to use the gmp implementation for num]) AC_DEFINE(USE_FIELD_INV_NUM, 1, [Define this symbol to use the num-based field inverse implementation]) AC_DEFINE(USE_SCALAR_INV_NUM, 1, [Define this symbol to use the num-based scalar inverse implementation]) ;; +none) + AC_DEFINE(USE_NUM_NONE, 1, [Define this symbol to use no num implementation]) + AC_DEFINE(USE_FIELD_INV_BUILTIN, 1, [Define this symbol to use the native field inverse implementation]) + AC_DEFINE(USE_SCALAR_INV_BUILTIN, 1, [Define this symbol to use the native scalar inverse implementation]) + ;; *) AC_MSG_ERROR([invalid bignum implementation]) ;; @@ -266,6 +270,9 @@ if test x"$set_field" = x"gmp" || test x"$set_bignum" = x"gmp"; then fi if test x"$use_endomorphism" = x"yes"; then + if test x"$set_bignum" = x"none"; then + AC_MSG_ERROR([Cannot use endomorphism optimization without a bignum implementation]) + fi AC_DEFINE(USE_ENDOMORPHISM, 1, [Define this symbol to use endomorphism]) fi diff --git a/src/field.h b/src/field.h index cbd48c00fd..4b7e1d4243 100644 --- a/src/field.h +++ b/src/field.h @@ -33,7 +33,9 @@ #endif typedef struct { +#ifndef USE_NUM_NONE secp256k1_num_t p; +#endif secp256k1_fe_t order; } secp256k1_fe_consts_t; diff --git a/src/field_impl.h b/src/field_impl.h index f26648733a..a8a8105843 100644 --- a/src/field_impl.h +++ b/src/field_impl.h @@ -267,16 +267,20 @@ static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe_t r[len], const se } static void secp256k1_fe_start(void) { +#ifndef USE_NUM_NONE static const unsigned char secp256k1_fe_consts_p[] = { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F }; +#endif if (secp256k1_fe_consts == NULL) { secp256k1_fe_inner_start(); secp256k1_fe_consts_t *ret = (secp256k1_fe_consts_t*)malloc(sizeof(secp256k1_fe_consts_t)); +#ifndef USE_NUM_NONE secp256k1_num_set_bin(&ret->p, secp256k1_fe_consts_p, sizeof(secp256k1_fe_consts_p)); +#endif secp256k1_fe_consts = ret; } } diff --git a/src/num.h b/src/num.h index 7beaf1d897..9e073b4a59 100644 --- a/src/num.h +++ b/src/num.h @@ -7,6 +7,8 @@ #ifndef _SECP256K1_NUM_ #define _SECP256K1_NUM_ +#ifndef USE_NUM_NONE + #if defined HAVE_CONFIG_H #include "libsecp256k1-config.h" #endif @@ -65,3 +67,5 @@ static int secp256k1_num_is_neg(const secp256k1_num_t *a); static void secp256k1_num_negate(secp256k1_num_t *r); #endif + +#endif diff --git a/src/num_impl.h b/src/num_impl.h index f73d3ceea8..0b0e3a072a 100644 --- a/src/num_impl.h +++ b/src/num_impl.h @@ -15,6 +15,8 @@ #if defined(USE_NUM_GMP) #include "num_gmp_impl.h" +#elif defined(USE_NUM_NONE) +/* Nothing. */ #else #error "Please select num implementation" #endif diff --git a/src/scalar.h b/src/scalar.h index 5b38c60acb..b5c43b45bf 100644 --- a/src/scalar.h +++ b/src/scalar.h @@ -72,11 +72,13 @@ static int secp256k1_scalar_is_one(const secp256k1_scalar_t *a); /** Check whether a scalar is higher than the group order divided by 2. */ static int secp256k1_scalar_is_high(const secp256k1_scalar_t *a); +#ifndef USE_NUM_NONE /** Convert a scalar to a number. */ static void secp256k1_scalar_get_num(secp256k1_num_t *r, const secp256k1_scalar_t *a); /** Get the order of the group as a number. */ static void secp256k1_scalar_order_get_num(secp256k1_num_t *r); +#endif /** Compare two scalars. */ static int secp256k1_scalar_eq(const secp256k1_scalar_t *a, const secp256k1_scalar_t *b); diff --git a/src/scalar_impl.h b/src/scalar_impl.h index 91f9bb11f6..3e8d8b8c56 100644 --- a/src/scalar_impl.h +++ b/src/scalar_impl.h @@ -25,7 +25,9 @@ #endif typedef struct { +#ifndef USE_NUM_NONE secp256k1_num_t order; +#endif #ifdef USE_ENDOMORPHISM secp256k1_num_t a1b2, b1, a2; #endif @@ -40,6 +42,7 @@ static void secp256k1_scalar_start(void) { /* Allocate. */ secp256k1_scalar_consts_t *ret = (secp256k1_scalar_consts_t*)malloc(sizeof(secp256k1_scalar_consts_t)); +#ifndef USE_NUM_NONE static const unsigned char secp256k1_scalar_consts_order[] = { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE, @@ -47,6 +50,7 @@ static void secp256k1_scalar_start(void) { 0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41 }; secp256k1_num_set_bin(&ret->order, secp256k1_scalar_consts_order, sizeof(secp256k1_scalar_consts_order)); +#endif #ifdef USE_ENDOMORPHISM static const unsigned char secp256k1_scalar_consts_a1b2[] = { 0x30,0x86,0xd2,0x21,0xa7,0xd4,0x6b,0xcd, @@ -80,6 +84,7 @@ static void secp256k1_scalar_stop(void) { free(c); } +#ifndef USE_NUM_NONE static void secp256k1_scalar_get_num(secp256k1_num_t *r, const secp256k1_scalar_t *a) { unsigned char c[32]; secp256k1_scalar_get_b32(c, a); @@ -89,6 +94,7 @@ static void secp256k1_scalar_get_num(secp256k1_num_t *r, const secp256k1_scalar_ static void secp256k1_scalar_order_get_num(secp256k1_num_t *r) { *r = secp256k1_scalar_consts->order; } +#endif static void secp256k1_scalar_inverse(secp256k1_scalar_t *r, const secp256k1_scalar_t *x) { /* First compute x ^ (2^N - 1) for some values of N. */ diff --git a/src/tests.c b/src/tests.c index 48f96068e0..b384e8ae4a 100644 --- a/src/tests.c +++ b/src/tests.c @@ -91,6 +91,7 @@ void random_scalar_order(secp256k1_scalar_t *num) { /***** NUM TESTS *****/ +#ifndef USE_NUM_NONE void random_num_negate(secp256k1_num_t *num) { if (secp256k1_rand32() & 1) secp256k1_num_negate(num); @@ -184,6 +185,7 @@ void run_num_smalltests(void) { test_num_add_sub(); } } +#endif /***** SCALAR TESTS *****/ @@ -203,6 +205,7 @@ void scalar_test(void) { random_scalar_order_test(&s2); secp256k1_scalar_get_b32(c, &s2); +#ifndef USE_NUM_NONE secp256k1_num_t snum, s1num, s2num; secp256k1_scalar_get_num(&snum, &s); secp256k1_scalar_get_num(&s1num, &s1); @@ -212,6 +215,7 @@ void scalar_test(void) { secp256k1_scalar_order_get_num(&order); secp256k1_num_t half_order = order; secp256k1_num_shift(&half_order, 1); +#endif { /* 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) { CHECK(secp256k1_scalar_eq(&n, &s)); } +#ifndef USE_NUM_NONE { /* Test that adding the scalars together is equal to adding their numbers together modulo the order. */ secp256k1_num_t rnum; @@ -303,17 +308,20 @@ void scalar_test(void) { /* Negating zero should still result in zero. */ CHECK(secp256k1_scalar_is_zero(&neg)); } +#endif { /* Test that scalar inverses are equal to the inverse of their number modulo the order. */ if (!secp256k1_scalar_is_zero(&s)) { secp256k1_scalar_t inv; secp256k1_scalar_inverse(&inv, &s); +#ifndef USE_NUM_NONE secp256k1_num_t invnum; secp256k1_num_mod_inverse(&invnum, &snum, &order); secp256k1_num_t invnum2; secp256k1_scalar_get_num(&invnum2, &inv); CHECK(secp256k1_num_eq(&invnum, &invnum2)); +#endif secp256k1_scalar_mul(&inv, &inv, &s); /* Multiplying a scalar with its inverse must result in one. */ CHECK(secp256k1_scalar_is_one(&inv)); @@ -411,6 +419,7 @@ void run_scalar_tests(void) { CHECK(secp256k1_scalar_is_zero(&o)); } +#ifndef USE_NUM_NONE { // A scalar with value of the curve order should be 0. secp256k1_num_t order; @@ -423,6 +432,7 @@ void run_scalar_tests(void) { CHECK(overflow == 1); CHECK(secp256k1_scalar_is_zero(&zero)); } +#endif } /***** FIELD TESTS *****/ @@ -1080,8 +1090,10 @@ int main(int argc, char **argv) { /* initialize */ secp256k1_start(SECP256K1_START_SIGN | SECP256K1_START_VERIFY); +#ifndef USE_NUM_NONE /* num tests */ run_num_smalltests(); +#endif /* scalar tests */ run_scalar_tests();