Browse Source

Switch num/field to extern C; small fixes

master
Pieter Wuille 10 years ago
parent
commit
fba1d58d57
  1. 8
      Makefile
  2. 4
      field.cpp
  3. 4
      field.h
  4. 22
      field_5x52.cpp
  5. 4
      field_5x52.h
  6. 4
      num.h
  7. 4
      num_gmp.cpp
  8. 4
      num_gmp.h

8
Makefile

@ -1,7 +1,7 @@ @@ -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 @@ -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)

4
field.cpp

@ -1,6 +1,8 @@ @@ -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) { @@ -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
}
}

4
field.h

@ -15,6 +15,8 @@ @@ -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); @@ -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

22
field_5x52.cpp

@ -7,6 +7,8 @@ @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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 @@ -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) { @@ -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) { @@ -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
}
}

4
field_5x52.h

@ -3,6 +3,8 @@ @@ -3,6 +3,8 @@
#include <stdint.h>
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 { @@ -12,4 +14,6 @@ typedef struct {
#endif
} secp256k1_fe_t;
}
#endif

4
num.h

@ -9,6 +9,8 @@ @@ -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 @@ -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

4
num_gmp.cpp

@ -5,6 +5,8 @@ @@ -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) { @@ -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);
}
}

4
num_gmp.h

@ -3,8 +3,12 @@ @@ -3,8 +3,12 @@
#include <gmp.h>
extern "C" {
typedef struct {
mpz_t bn;
} secp256k1_num_t;
}
#endif

Loading…
Cancel
Save