From fba1d58d57c49360b96f5416298892601b2ee9eb Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sun, 31 Mar 2013 03:46:01 +0200 Subject: [PATCH] Switch num/field to extern C; small fixes --- Makefile | 8 ++++---- field.cpp | 4 ++++ field.h | 4 ++++ field_5x52.cpp | 22 +++++++++++++--------- field_5x52.h | 4 ++++ num.h | 4 ++++ num_gmp.cpp | 4 ++++ num_gmp.h | 4 ++++ 8 files changed, 41 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 2f0454d9a0c..8babaf068b8 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ FLAGS_COMMON:=-Wall -FLAGS_PROD:=-DNDEBUG -O2 -march=native -FLAGS_DEBUG:=-DVERIFY_MAGNITUDE -ggdb3 -O1 -FLAGS_TEST:=-DVERIFY_MAGNITUDE -ggdb3 -O2 -march=native +FLAGS_PROD:=-DNDEBUG -O3 -march=native +FLAGS_DEBUG:=-DVERIFY -ggdb3 -O1 +FLAGS_TEST:=-DVERIFY -ggdb3 -O2 -march=native SECP256K1_FILES := num.h field.h field_5x52.h group.h ecmult.h ecdsa.h \ num.cpp field.cpp field_5x52.cpp group.cpp ecmult.cpp ecdsa.cpp @@ -58,4 +58,4 @@ bench-$(CONF): $(SECP256K1_FILES) bench.cpp $(CXX) $(FLAGS_COMMON) $(FLAGS_PROD) $(FLAGS_CONF) bench.cpp $(LIBS) -o bench-$(CONF) tests-$(CONF): $(SECP256K1_FILES) tests.cpp - $(CXX) $(FLAGS_COMMON) $(FLAGS_DEBUG) $(FLAGS_CONF) tests.cpp $(LIBS) -o tests-$(CONF) + $(CXX) $(FLAGS_COMMON) $(FLAGS_TEST) $(FLAGS_CONF) tests.cpp $(LIBS) -o tests-$(CONF) diff --git a/field.cpp b/field.cpp index ca201d15a3b..9c214e6bbad 100644 --- a/field.cpp +++ b/field.cpp @@ -1,6 +1,8 @@ // just one implementation for now #include "field_5x52.cpp" +extern "C" { + static const unsigned char secp256k1_fe_consts_p[] = { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, @@ -149,3 +151,5 @@ void static secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a) { secp256k1_fe_set_b32(&c, b); #endif } + +} diff --git a/field.h b/field.h index a266b878cbc..7441a87937f 100644 --- a/field.h +++ b/field.h @@ -15,6 +15,8 @@ // just one implementation for now #include "field_5x52.h" +extern "C" { + typedef struct { secp256k1_num_t p; } secp256k1_fe_consts_t; @@ -85,4 +87,6 @@ void static secp256k1_fe_get_hex(char *r, int *rlen, const secp256k1_fe_t *a); /** Convert a hexadecimal string to a field element. */ void static secp256k1_fe_set_hex(secp256k1_fe_t *r, const char *a, int alen); +} + #endif diff --git a/field_5x52.cpp b/field_5x52.cpp index b5a8f517c03..f86b6b44ec2 100644 --- a/field_5x52.cpp +++ b/field_5x52.cpp @@ -7,6 +7,8 @@ #include "lin64.h" #endif +extern "C" { + /** Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F, * represented as 5 uint64_t's in base 2^52. The values are allowed to contain >52 each. In particular, * each FieldElem has a 'magnitude' associated with it. Internally, a magnitude M means each element @@ -58,7 +60,7 @@ void static secp256k1_fe_normalize(secp256k1_fe_t *r) { #endif } -void static secp256k1_fe_set_int(secp256k1_fe_t *r, int a) { +void static inline secp256k1_fe_set_int(secp256k1_fe_t *r, int a) { r->n[0] = a; r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0; #ifdef VERIFY @@ -68,14 +70,14 @@ void static secp256k1_fe_set_int(secp256k1_fe_t *r, int a) { } // TODO: not constant time! -int static secp256k1_fe_is_zero(const secp256k1_fe_t *a) { +int static inline secp256k1_fe_is_zero(const secp256k1_fe_t *a) { #ifdef VERIFY assert(a->normalized); #endif return (a->n[0] == 0 && a->n[1] == 0 && a->n[2] == 0 && a->n[3] == 0 && a->n[4] == 0); } -int static secp256k1_fe_is_odd(const secp256k1_fe_t *a) { +int static inline secp256k1_fe_is_odd(const secp256k1_fe_t *a) { #ifdef VERIFY assert(a->normalized); #endif @@ -83,7 +85,7 @@ int static secp256k1_fe_is_odd(const secp256k1_fe_t *a) { } // TODO: not constant time! -int static secp256k1_fe_equal(const secp256k1_fe_t *a, const secp256k1_fe_t *b) { +int static inline secp256k1_fe_equal(const secp256k1_fe_t *a, const secp256k1_fe_t *b) { #ifdef VERIFY assert(a->normalized); assert(b->normalized); @@ -122,7 +124,7 @@ void static secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe_t *a) { } } -void static secp256k1_fe_negate(secp256k1_fe_t *r, const secp256k1_fe_t *a, int m) { +void static inline secp256k1_fe_negate(secp256k1_fe_t *r, const secp256k1_fe_t *a, int m) { #ifdef VERIFY assert(a->magnitude <= m); r->magnitude = m + 1; @@ -135,7 +137,7 @@ void static secp256k1_fe_negate(secp256k1_fe_t *r, const secp256k1_fe_t *a, int r->n[4] = 0x0FFFFFFFFFFFFULL * (m + 1) - a->n[4]; } -void static secp256k1_fe_mul_int(secp256k1_fe_t *r, int a) { +void static inline secp256k1_fe_mul_int(secp256k1_fe_t *r, int a) { #ifdef VERIFY r->magnitude *= a; r->normalized = false; @@ -147,7 +149,7 @@ void static secp256k1_fe_mul_int(secp256k1_fe_t *r, int a) { r->n[4] *= a; } -void static secp256k1_fe_add(secp256k1_fe_t *r, const secp256k1_fe_t *a) { +void static inline secp256k1_fe_add(secp256k1_fe_t *r, const secp256k1_fe_t *a) { #ifdef VERIFY r->magnitude += a->magnitude; r->normalized = 0; @@ -273,7 +275,9 @@ void static secp256k1_fe_sqr(secp256k1_fe_t *r, const secp256k1_fe_t *a) { #endif #ifdef VERIFY - assert(a->magnitude <= 8); - a->normalized = 0; + r->magnitude = 1; + r->normalized = 0; #endif } + +} diff --git a/field_5x52.h b/field_5x52.h index f6be2089cbe..472161a91a5 100644 --- a/field_5x52.h +++ b/field_5x52.h @@ -3,6 +3,8 @@ #include +extern "C" { + typedef struct { // X = sum(i=0..4, elem[i]*2^52) mod n uint64_t n[5]; @@ -12,4 +14,6 @@ typedef struct { #endif } secp256k1_fe_t; +} + #endif diff --git a/num.h b/num.h index 58723c51524..fe600db6275 100644 --- a/num.h +++ b/num.h @@ -9,6 +9,8 @@ #error "Please select num implementation" #endif +extern "C" { + void static secp256k1_num_start(void); void static secp256k1_num_init(secp256k1_num_t *r); void static secp256k1_num_free(secp256k1_num_t *r); @@ -37,4 +39,6 @@ void static secp256k1_num_split(secp256k1_num_t *rl, secp256k1_num_t *rh, const void static secp256k1_num_negate(secp256k1_num_t *r); void static secp256k1_num_set_rand(secp256k1_num_t *r, const secp256k1_num_t *a); +} + #endif diff --git a/num_gmp.cpp b/num_gmp.cpp index 5facce29808..e89e1b0a07e 100644 --- a/num_gmp.cpp +++ b/num_gmp.cpp @@ -5,6 +5,8 @@ #include "num.h" +extern "C" { + typedef struct { int initialized; gmp_randstate_t rng; @@ -146,3 +148,5 @@ void static secp256k1_num_negate(secp256k1_num_t *r) { void static secp256k1_num_set_rand(secp256k1_num_t *r, const secp256k1_num_t *a) { mpz_urandomm(r->bn, secp256k1_num_state.rng, a->bn); } + +} diff --git a/num_gmp.h b/num_gmp.h index d908e001123..cad5d109b24 100644 --- a/num_gmp.h +++ b/num_gmp.h @@ -3,8 +3,12 @@ #include +extern "C" { + typedef struct { mpz_t bn; } secp256k1_num_t; +} + #endif