Begin group C interface + start/stop

This commit is contained in:
Pieter Wuille 2013-03-31 06:34:15 +02:00
parent b3e15b5b19
commit 254327e49a
6 changed files with 80 additions and 4 deletions

View File

@ -1,8 +1,10 @@
#ifndef _SECP256K1_
#define _SECP256K1_
namespace secp256k1 {
int VerifyECDSA(const unsigned char *msg, int msglen, const unsigned char *sig, int siglen, const unsigned char *pubkey, int pubkeylen);
extern "C" {
void secp256k1_start(void);
void secp256k1_stop(void);
int secp256k1_ecdsa_verify(const unsigned char *msg, int msglen, const unsigned char *sig, int siglen, const unsigned char *pubkey, int pubkeylen);
}
#endif

View File

@ -6,6 +6,55 @@
#include "num.h"
#include "field.h"
extern "C" {
typedef struct {
secp256k1_fe_t x;
secp256k1_fe_t y;
int infinity;
} secp256k1_ge_t;
typedef struct {
secp256k1_fe_t x;
secp256k1_fe_t y;
secp256k1_fe_t z;
int infinity;
} secp256k1_gej_t;
typedef struct {
secp256k1_num_t order;
secp256k1_ge_t g;
secp256k1_fe_t beta;
secp256k1_num_t lambda, a1b2, b1, a2;
} secp256k1_ge_consts_t;
static secp256k1_ge_consts_t *secp256k1_ge_consts = NULL;
void static secp256k1_ge_start(void);
void static secp256k1_ge_stop(void);
void static secp256k1_ge_set_infinity(secp256k1_ge_t *r);
void static secp256k1_ge_set_xy(secp256k1_ge_t *r, const secp256k1_fe_t *x, const secp256k1_fe_t *y);
int static secp256k1_ge_is_infinity(const secp256k1_ge_t *a);
void static secp256k1_ge_neg(secp256k1_ge_t *r, const secp256k1_ge_t *a);
void static secp256k1_ge_get_hex(char *r, int *rlen, const secp256k1_ge_t *a);
void static secp256k1_ge_set_gej(secp256k1_ge_t *r, const secp256k1_gej_t *a);
void static secp256k1_gej_set_infinity(secp256k1_gej_t *r);
void static secp256k1_gej_set_xy(secp256k1_gej_t *r, const secp256k1_fe_t *x, const secp256k1_fe_t *y);
void static secp256k1_gej_set_xo(secp256k1_gej_t *r, const secp256k1_fe_t *x, int compressed);
void static secp256k1_gej_set_ge(secp256k1_gej_t *r, const secp256k1_ge_t *a);
void static secp256k1_gej_get_x(secp256k1_fe_t *r, const secp256k1_gej_t *a);
void static secp256k1_gej_neg(secp256k1_gej_t *r, const secp256k1_gej_t *a);
int static secp256k1_gej_is_infinity(const secp256k1_gej_t *a);
void static secp256k1_gej_double(secp256k1_gej_t *r, const secp256k1_gej_t *a);
void static secp256k1_gej_add(secp256k1_gej_t *r, const secp256k1_gej_t *a, const secp256k1_gej_t *b);
void static secp256k1_gej_add_ge(secp256k1_gej_t *r, const secp256k1_gej_t *a, const secp256k1_ge_t *b);
void static secp256k1_gej_get_hex(char *r, int *rlen, const secp256k1_gej_t *a);
void static secp256k1_gej_mul_lambda(secp256k1_gej_t *r, const secp256k1_gej_t *a);
void static secp256k1_gej_split_exp(secp256k1_num_t *r1, secp256k1_num_t *r2, const secp256k1_num_t *a);
}
namespace secp256k1 {
class GroupElemJac;

View File

@ -12,6 +12,7 @@
extern "C" {
void static secp256k1_num_start(void);
void static secp256k1_num_stop(void);
void static secp256k1_num_init(secp256k1_num_t *r);
void static secp256k1_num_free(secp256k1_num_t *r);
void static secp256k1_num_copy(secp256k1_num_t *r, const secp256k1_num_t *a);

View File

@ -21,6 +21,13 @@ void static secp256k1_num_start(void) {
gmp_randinit_default(secp256k1_num_state.rng);
}
void static secp256k1_num_stop(void) {
if (!secp256k1_num_state.initialized)
return;
secp256k1_num_state.initialized = 0;
gmp_randclear(secp256k1_num_state.rng);
}
void static secp256k1_num_init(secp256k1_num_t *r) {
mpz_init(r->bn);
}

View File

@ -9,6 +9,10 @@
void static secp256k1_num_start() {
}
void static secp256k1_num_stop() {
}
void static secp256k1_num_init(secp256k1_num_t *r) {
BN_init(&r->bn);
}

View File

@ -4,9 +4,22 @@
#include "ecmult.cpp"
#include "ecdsa.cpp"
namespace secp256k1 {
int VerifyECDSA(const unsigned char *msg, int msglen, const unsigned char *sig, int siglen, const unsigned char *pubkey, int pubkeylen) {
extern "C" void secp256k1_start(void) {
secp256k1_num_start();
secp256k1_fe_start();
GetGroupConst();
GetECMultConsts();
}
extern "C" void secp256k1_stop(void) {
secp256k1_fe_stop();
secp256k1_num_stop();
}
extern "C" int secp256k1_ecdsa_verify(const unsigned char *msg, int msglen, const unsigned char *sig, int siglen, const unsigned char *pubkey, int pubkeylen) {
int ret = -3;
secp256k1_num_t m;
secp256k1_num_init(&m);
@ -34,5 +47,5 @@ end:
return ret;
}
}