Remove OpenSSL bignum implementation

pull/11871/head
Pieter Wuille 10 years ago
parent 9c698f16d6
commit 520ba3c921

@ -1,28 +1,21 @@
language: cpp language: cpp
compiler: gcc compiler: gcc
install: install:
- if [ "$BIGNUM" = "gmp" -o "$FIELD" = "gmp" ]; then sudo apt-get install -qq libgmp-dev; fi - sudo apt-get install -qq libssl-dev
- if [ "$BIGNUM" = "gmp" -o "$BIGNUM" = "auto" -o "$FIELD" = "gmp" ]; then sudo apt-get install -qq libgmp-dev; fi
- if [ "$FIELD" = "64bit_asm" ]; then sudo apt-get install -qq yasm; fi - if [ "$FIELD" = "64bit_asm" ]; then sudo apt-get install -qq yasm; fi
env: env:
global: global:
- FIELD=auto BIGNUM=auto ENDOMORPHISM=no BUILD=check - FIELD=auto BIGNUM=auto ENDOMORPHISM=no BUILD=check
matrix: matrix:
- FIELD=gmp BIGNUM=gmp - FIELD=gmp
- FIELD=gmp BIGNUM=openssl - FIELD=gmp ENDOMORPHISM=yes
- FIELD=64bit_asm BIGNUM=gmp - FIELD=64bit_asm
- FIELD=64bit_asm BIGNUM=openssl - FIELD=64bit_asm ENDOMORPHISM=yes
- FIELD=64bit BIGNUM=gmp - FIELD=64bit
- FIELD=64bit BIGNUM=openssl - FIELD=64bit ENDOMORPHISM=yes
- FIELD=32bit BIGNUM=gmp - FIELD=32bit
- FIELD=32bit BIGNUM=openssl - FIELD=32bit ENDOMORPHISM=yes
- FIELD=gmp BIGNUM=gmp ENDOMORPHISM=yes
- FIELD=gmp BIGNUM=openssl ENDOMORPHISM=yes
- FIELD=64bit_asm BIGNUM=gmp ENDOMORPHISM=yes
- FIELD=64bit_asm BIGNUM=openssl ENDOMORPHISM=yes
- FIELD=64bit BIGNUM=gmp ENDOMORPHISM=yes
- FIELD=64bit BIGNUM=openssl ENDOMORPHISM=yes
- FIELD=32bit BIGNUM=gmp ENDOMORPHISM=yes
- FIELD=32bit BIGNUM=openssl ENDOMORPHISM=yes
- BUILD=distcheck - BUILD=distcheck
before_script: ./autogen.sh before_script: ./autogen.sh
script: ./configure --enable-endomorphism=$ENDOMORPHISM --with-field=$FIELD --with-bignum=$BIGNUM && make -j2 $BUILD script: ./configure --enable-endomorphism=$ENDOMORPHISM --with-field=$FIELD --with-bignum=$BIGNUM && make -j2 $BUILD

@ -8,8 +8,6 @@ noinst_HEADERS += src/scalar.h
noinst_HEADERS += src/scalar_impl.h noinst_HEADERS += src/scalar_impl.h
noinst_HEADERS += src/group.h noinst_HEADERS += src/group.h
noinst_HEADERS += src/group_impl.h noinst_HEADERS += src/group_impl.h
noinst_HEADERS += src/num_openssl.h
noinst_HEADERS += src/num_openssl_impl.h
noinst_HEADERS += src/num_gmp.h noinst_HEADERS += src/num_gmp.h
noinst_HEADERS += src/num_gmp_impl.h noinst_HEADERS += src/num_gmp_impl.h
noinst_HEADERS += src/ecdsa.h noinst_HEADERS += src/ecdsa.h

@ -58,7 +58,7 @@ AC_ARG_ENABLE(endomorphism,
AC_ARG_WITH([field], [AS_HELP_STRING([--with-field=gmp|64bit|64bit_asm|32bit|auto], 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]) [Specify Field Implementation. Default is auto])],[req_field=$withval], [req_field=auto])
AC_ARG_WITH([bignum], [AS_HELP_STRING([--with-bignum=gmp|openssl|auto], AC_ARG_WITH([bignum], [AS_HELP_STRING([--with-bignum=gmp|auto],
[Specify Bignum Implementation. Default is auto])],[req_bignum=$withval], [req_bignum=auto]) [Specify Bignum Implementation. Default is auto])],[req_bignum=$withval], [req_bignum=auto])
AC_CHECK_TYPES([__int128]) AC_CHECK_TYPES([__int128])
@ -132,11 +132,6 @@ if test x"$has_libcrypto" == x"yes" && test x"$has_openssl_ec" = x; then
],[has_openssl_ec=yes], [has_openssl_ec=no]) ],[has_openssl_ec=yes], [has_openssl_ec=no])
AC_MSG_RESULT([$has_openssl_ec]) AC_MSG_RESULT([$has_openssl_ec])
fi fi
if test x"$set_bignum" = x"openssl"; then
if test x"$has_libcrypto" != x"yes"; then
AC_MSG_ERROR([$set_bignum field support explicitly requested but libcrypto was not found])
fi
fi
]) ])
AC_CHECK_DECL(__builtin_expect,AC_DEFINE(HAVE_BUILTIN_EXPECT,1,[Define this symbol if __builtin_expect is available]),,) AC_CHECK_DECL(__builtin_expect,AC_DEFINE(HAVE_BUILTIN_EXPECT,1,[Define this symbol if __builtin_expect is available]),,)
@ -202,13 +197,6 @@ if test x"$req_bignum" = x"auto"; then
set_bignum=gmp set_bignum=gmp
fi fi
if test x"$set_bignum" = x; then
SECP_OPENSSL_CHECK
if test x"$has_libcrypto" = x"yes"; then
set_bignum=openssl
fi
fi
if test x"$set_bignum" = x; then if test x"$set_bignum" = x; then
AC_MSG_ERROR([no working bignum implementation found]) AC_MSG_ERROR([no working bignum implementation found])
fi fi
@ -256,12 +244,6 @@ gmp)
AC_DEFINE(USE_NUM_GMP, 1, [Define this symbol to use the gmp implementation]) AC_DEFINE(USE_NUM_GMP, 1, [Define this symbol to use the gmp implementation])
AC_DEFINE(USE_FIELD_INV_NUM, 1, [Define this symbol to use the USE_FIELD_INV_NUM implementation]) AC_DEFINE(USE_FIELD_INV_NUM, 1, [Define this symbol to use the USE_FIELD_INV_NUM implementation])
;; ;;
openssl)
AC_DEFINE(USE_NUM_OPENSSL, 1,[Define this symbol to use the openssl implementation])
AC_DEFINE(USE_FIELD_INV_BUILTIN, 1, [Define this symbol to use the FIELD_INV_BUILTIN implementation])
SECP_INCLUDES="$SSL_CFLAGS $CRYPTO_CFLAGS"
SECP_LIBS="$CRYPTO_LIBS"
;;
*) *)
AC_MSG_ERROR([invalid bignum implementation]) AC_MSG_ERROR([invalid bignum implementation])
;; ;;

@ -11,8 +11,6 @@
#if defined(USE_NUM_GMP) #if defined(USE_NUM_GMP)
#include "num_gmp.h" #include "num_gmp.h"
#elif defined(USE_NUM_OPENSSL)
#include "num_openssl.h"
#else #else
#error "Please select num implementation" #error "Please select num implementation"
#endif #endif

@ -13,8 +13,6 @@
#if defined(USE_NUM_GMP) #if defined(USE_NUM_GMP)
#include "num_gmp_impl.h" #include "num_gmp_impl.h"
#elif defined(USE_NUM_OPENSSL)
#include "num_openssl_impl.h"
#else #else
#error "Please select num implementation" #error "Please select num implementation"
#endif #endif

@ -1,17 +0,0 @@
// Copyright (c) 2013 Pieter Wuille
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef _SECP256K1_NUM_REPR_
#define _SECP256K1_NUM_REPR_
#include <openssl/bn.h>
typedef struct {
BIGNUM bn;
#ifdef VERIFY
void* init;
#endif
} secp256k1_num_t;
#endif

@ -1,263 +0,0 @@
// Copyright (c) 2013 Pieter Wuille
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef _SECP256K1_NUM_REPR_IMPL_H_
#define _SECP256K1_NUM_REPR_IMPL_H_
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include "util.h"
#include "num.h"
void static secp256k1_num_init(secp256k1_num_t *r) {
#ifdef VERIFY
r->init = r;
#endif
BN_init(&r->bn);
}
void static secp256k1_num_free(secp256k1_num_t *r) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
r->init = NULL;
#endif
BN_free(&r->bn);
}
void static secp256k1_num_clear(secp256k1_num_t *r) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
#endif
BN_clear(&r->bn);
}
void static secp256k1_num_copy(secp256k1_num_t *r, const secp256k1_num_t *a) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
VERIFY_CHECK(a->init == a);
#endif
BN_copy(&r->bn, &a->bn);
}
void static secp256k1_num_get_bin(unsigned char *r, unsigned int rlen, const secp256k1_num_t *a) {
#ifdef VERIFY
VERIFY_CHECK(a->init == a);
#endif
unsigned int size = BN_num_bytes(&a->bn);
VERIFY_CHECK(size <= rlen);
memset(r,0,rlen);
BN_bn2bin(&a->bn, r + rlen - size);
}
void static secp256k1_num_set_bin(secp256k1_num_t *r, const unsigned char *a, unsigned int alen) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
#endif
BN_bin2bn(a, alen, &r->bn);
}
void static secp256k1_num_set_int(secp256k1_num_t *r, int a) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
#endif
BN_set_word(&r->bn, a < 0 ? -a : a);
BN_set_negative(&r->bn, a < 0);
}
void static secp256k1_num_mod_inverse(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *m) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
VERIFY_CHECK(a->init == a);
VERIFY_CHECK(m->init == m);
#endif
BN_CTX *ctx = BN_CTX_new();
BN_mod_inverse(&r->bn, &a->bn, &m->bn, ctx);
BN_CTX_free(ctx);
}
void static secp256k1_num_mod_mul(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b, const secp256k1_num_t *m) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
VERIFY_CHECK(a->init == a);
VERIFY_CHECK(b->init == b);
VERIFY_CHECK(m->init == m);
#endif
BN_CTX *ctx = BN_CTX_new();
BN_mod_mul(&r->bn, &a->bn, &b->bn, &m->bn, ctx);
BN_CTX_free(ctx);
}
int static secp256k1_num_cmp(const secp256k1_num_t *a, const secp256k1_num_t *b) {
#ifdef VERIFY
VERIFY_CHECK(a->init == a);
VERIFY_CHECK(b->init == b);
#endif
return BN_ucmp(&a->bn, &b->bn);
}
int static secp256k1_num_eq(const secp256k1_num_t *a, const secp256k1_num_t *b) {
#ifdef VERIFY
VERIFY_CHECK(a->init == a);
VERIFY_CHECK(b->init == b);
#endif
return BN_cmp(&a->bn, &b->bn) == 0;
}
void static secp256k1_num_add(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
VERIFY_CHECK(a->init == a);
VERIFY_CHECK(b->init == b);
#endif
BN_add(&r->bn, &a->bn, &b->bn);
}
void static secp256k1_num_sub(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
VERIFY_CHECK(a->init == a);
VERIFY_CHECK(b->init == b);
#endif
BN_sub(&r->bn, &a->bn, &b->bn);
}
void static secp256k1_num_mul(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
VERIFY_CHECK(a->init == a);
VERIFY_CHECK(b->init == b);
#endif
BN_CTX *ctx = BN_CTX_new();
BN_mul(&r->bn, &a->bn, &b->bn, ctx);
BN_CTX_free(ctx);
}
void static secp256k1_num_div(secp256k1_num_t *r, const secp256k1_num_t *a, const secp256k1_num_t *b) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
VERIFY_CHECK(a->init == a);
VERIFY_CHECK(b->init == b);
#endif
BN_CTX *ctx = BN_CTX_new();
BN_div(&r->bn, NULL, &a->bn, &b->bn, ctx);
BN_CTX_free(ctx);
}
void static secp256k1_num_mod(secp256k1_num_t *r, const secp256k1_num_t *m) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
VERIFY_CHECK(m->init == m);
#endif
BN_CTX *ctx = BN_CTX_new();
BN_nnmod(&r->bn, &r->bn, &m->bn, ctx);
BN_CTX_free(ctx);
}
int static secp256k1_num_bits(const secp256k1_num_t *a) {
#ifdef VERIFY
VERIFY_CHECK(a->init == a);
#endif
return BN_num_bits(&a->bn);
}
int static secp256k1_num_shift(secp256k1_num_t *r, int bits) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
#endif
int ret = BN_is_zero(&r->bn) ? 0 : r->bn.d[0] & ((1 << bits) - 1);
BN_rshift(&r->bn, &r->bn, bits);
return ret;
}
int static secp256k1_num_is_zero(const secp256k1_num_t *a) {
#ifdef VERIFY
VERIFY_CHECK(a->init == a);
#endif
return BN_is_zero(&a->bn);
}
int static secp256k1_num_is_odd(const secp256k1_num_t *a) {
#ifdef VERIFY
VERIFY_CHECK(a->init == a);
#endif
return BN_is_odd(&a->bn);
}
int static secp256k1_num_is_neg(const secp256k1_num_t *a) {
#ifdef VERIFY
VERIFY_CHECK(a->init == a);
#endif
return BN_is_negative(&a->bn);
}
int static secp256k1_num_get_bit(const secp256k1_num_t *a, int pos) {
#ifdef VERIFY
VERIFY_CHECK(a->init == a);
#endif
return BN_is_bit_set(&a->bn, pos);
}
void static secp256k1_num_inc(secp256k1_num_t *r) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
#endif
BN_add_word(&r->bn, 1);
}
void static secp256k1_num_set_hex(secp256k1_num_t *r, const char *a, int alen) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
#endif
char *str = (char*)malloc(alen+1);
memcpy(str, a, alen);
str[alen] = 0;
BIGNUM *pbn = &r->bn;
BN_hex2bn(&pbn, str);
free(str);
}
void static secp256k1_num_get_hex(char *r, int rlen, const secp256k1_num_t *a) {
#ifdef VERIFY
VERIFY_CHECK(a->init == a);
#endif
char *str = BN_bn2hex(&a->bn);
int len = strlen(str);
VERIFY_CHECK(rlen >= len);
for (int i=0; i<rlen-len; i++)
r[i] = '0';
memcpy(r+rlen-len, str, len);
OPENSSL_free(str);
}
void static secp256k1_num_split(secp256k1_num_t *rl, secp256k1_num_t *rh, const secp256k1_num_t *a, int bits) {
#ifdef VERIFY
VERIFY_CHECK(a->init == a);
VERIFY_CHECK(rl->init == rl);
VERIFY_CHECK(rh->init == rh);
#endif
BN_copy(&rl->bn, &a->bn);
BN_rshift(&rh->bn, &a->bn, bits);
BN_mask_bits(&rl->bn, bits);
}
void static secp256k1_num_negate(secp256k1_num_t *r) {
#ifdef VERIFY
VERIFY_CHECK(r->init == r);
#endif
BN_set_negative(&r->bn, !BN_is_negative(&r->bn));
}
int static secp256k1_num_get_bits(const secp256k1_num_t *a, int offset, int count) {
int ret = 0;
for (int i = 0; i < count; i++) {
ret |= BN_is_bit_set(&a->bn, offset + i) << i;
}
return ret;
}
#endif
Loading…
Cancel
Save