mirror of
https://github.com/eosswedenorg/libantelope
synced 2026-07-04 15:23:41 +02:00
Adding src/libsecp256k1/ec.cpp
This commit is contained in:
parent
2ab5e7b444
commit
54fc8614ef
1 changed files with 52 additions and 41 deletions
|
|
@ -21,53 +21,64 @@
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#ifndef LIBEOSIO_TYPES_H
|
#include <secp256k1.h>
|
||||||
#define LIBEOSIO_TYPES_H
|
#include <secp256k1_ecdh.h>
|
||||||
|
#include <libeosio/ec.hpp>
|
||||||
#include <iostream>
|
#include "rng.h"
|
||||||
#include <array>
|
|
||||||
|
|
||||||
namespace libeosio {
|
namespace libeosio {
|
||||||
|
|
||||||
/**
|
secp256k1_context* ctx;
|
||||||
* Elliptic curve private key size (in bytes)
|
|
||||||
*/
|
|
||||||
#define EC_PRIVKEY_SIZE 32
|
|
||||||
|
|
||||||
/**
|
int ec_init() {
|
||||||
* Elliptic curve public key size (in bytes)
|
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
|
||||||
*
|
return ctx == NULL ? -1 : 0;
|
||||||
* Compressed format: z||x, where byte z specifies which (of the 2) solutions
|
}
|
||||||
* of the quadratic equation y is. Each cordinate is 32 bytes.
|
|
||||||
*/
|
|
||||||
#define EC_PUBKEY_SIZE (32 + 1)
|
|
||||||
|
|
||||||
/**
|
void ec_shutdown() {
|
||||||
* Elliptic curve priv/pub key datastructures.
|
if (ctx) {
|
||||||
*/
|
secp256k1_context_destroy(ctx);
|
||||||
typedef std::array<unsigned char, EC_PRIVKEY_SIZE> ec_privkey_t;
|
ctx = NULL;
|
||||||
typedef std::array<unsigned char, EC_PUBKEY_SIZE> ec_pubkey_t;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
int ec_generate_key(struct ec_keypair *pair) {
|
||||||
* Elliptic curve keypair (public + private)
|
|
||||||
*/
|
|
||||||
struct ec_keypair {
|
|
||||||
ec_privkey_t secret;
|
|
||||||
ec_pubkey_t pub;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
int ret = -1;
|
||||||
* Hashes
|
size_t len;
|
||||||
*/
|
secp256k1_pubkey pub;
|
||||||
typedef struct { unsigned char data[20]; } ripemd160_t;
|
unsigned char seckey[32];
|
||||||
typedef struct { unsigned char data[32]; } sha256_t;
|
unsigned char randomize[32];
|
||||||
|
|
||||||
|
if (!fill_random(randomize, sizeof(randomize))) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (secp256k1_context_randomize(ctx, randomize) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
if (!fill_random(pair->secret.data(), pair->secret.size())) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (secp256k1_ec_seckey_verify(ctx, pair->secret.data())) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (secp256k1_ec_pubkey_create(ctx, &pub, pair->secret.data()) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
len = EC_PUBKEY_SIZE;
|
||||||
|
secp256k1_ec_pubkey_serialize(ctx, pair->pub.data(), &len, &pub, SECP256K1_EC_COMPRESSED);
|
||||||
|
|
||||||
|
if (len != EC_PUBKEY_SIZE) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace libeosio
|
} // namespace libeosio
|
||||||
|
|
||||||
// Stream operators
|
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& os, const libeosio::ec_privkey_t& pk);
|
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& os, const libeosio::ec_pubkey_t& pk);
|
|
||||||
|
|
||||||
#endif /* LIBEOSIO_TYPES_H */
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue