forked from Waqar144/secp256k1-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secp256k1-ferro.cpp
64 lines (51 loc) · 1.24 KB
/
secp256k1-ferro.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "secp256k1-ferro.hpp"
#include <cassert>
#include <iostream>
#include <vector>
#include <sstream>
#include <iomanip>
/**
* @brief Secp256K1::Secp256K1
*/
Secp256K1::Secp256K1()
: ctx(secp256k1_context_create(SECP256K1_CONTEXT_SIGN))
{
}
Secp256K1::~Secp256K1()
{
secp256k1_context_destroy(ctx);
}
/**
* @brief verifies private key and generates corresponding public key
* @param privateKey - in hexadecimal
*/
Secp256K1::Secp256K1(const std::string& privateKey)
: ctx(secp256k1_context_create(SECP256K1_CONTEXT_SIGN))
{
privKey.assign(privateKey.begin(), privateKey.end());
createPublicKey();
}
std::vector<uint8_t> Secp256K1::publicKey() const
{
return pubKey;
}
std::vector<uint8_t> Secp256K1::privateKey() const
{
return privKey;
}
bool Secp256K1::createPublicKey(bool compressed)
{
// Calculate public key.
secp256k1_pubkey pubkey;
int ret = secp256k1_ec_pubkey_create(ctx, &pubkey, &privKey[0]);
if (ret != 1) {
return false;
}
// Serialize public key.
size_t outSize = PUBLIC_KEY_SIZE;
secp256k1_ec_pubkey_serialize(
ctx, pubkey_, &outSize, &pubkey,
compressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
// Succeed.
return true;
}