This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.h
159 lines (137 loc) · 6.02 KB
/
library.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef SECP256K1_LIBRARY_H
#define SECP256K1_LIBRARY_H
#include <climits>
#include <memory>
#include <random>
#include <utility>
#include <vector>
#include "include/secp256k1.h"
#include "include/secp256k1_ecdh.h"
#include "include/secp256k1_recovery.h"
namespace secp256k1 {
typedef std::vector<unsigned char> ByteArray;
/**
* Number of Bytes in a ECDSA private key.
*/
constexpr inline static int8_t PrivateKeyLength = 32;
/**
* Number of Bytes in a ECDH secret.
*/
constexpr inline static int8_t SecretLength = 32;
/**
* Number of Bytes in a ECDSA signature (excluding a recovery ID)
*/
constexpr inline static int8_t SignatureLength = 64;
/**
* Number of Bytes in a message, to compute the signature for
*/
constexpr inline static int8_t MessageLength = 32;
/**
* Number of Bytes of a key, 33 for compressed keys, 65 for uncompressed keys
*/
enum PublicKeyLength {
Compressed = 33,
Uncompressed = 65,
};
class Encryption final {
public:
Encryption(Encryption const&) = delete;
void operator=(Encryption const&) = delete;
/**
* Get the instance of the Encryption class.
*
* @return the singleton instance
*/
static Encryption& getInstance()
{
// Guaranteed to be destroyed.
// Instantiated on first use.
// Thread-safe
static Encryption instance;
return instance;
}
/**
* Generate a new random ECDSA key pair as a pair of private and public key.
*
* @return a new random ECDSA key pair in the form [private, public]
*/
std::pair<std::unique_ptr<ByteArray>, std::unique_ptr<ByteArray>> createKeyPair();
/**
* Compute the public key from a private key
*
* @param key - the private key to compute the public key for
* @param length - the length of the resulting public key
* @return the public key encoded according to the given length
*/
std::unique_ptr<ByteArray> computePublicKey(const secp256k1::ByteArray &key, const PublicKeyLength &length = PublicKeyLength::Compressed);
/**
* Compute an EC Diffie-Hellman secret in constant time
*
* @param privateKey - the private key, which is used as a 32-byte scalar with which to multiply the point
* @param publicKey - the public key, to compute the secret with
* @return a 32-byte vector containing the ECDH secret computed from the point and scalar
*/
std::unique_ptr<secp256k1::ByteArray> computeSecret(const secp256k1::ByteArray &privateKey, const secp256k1::ByteArray &publicKey);
/**
* Sign creates a recoverable ECDSA signature.
* The produced signature is in the 65-byte [R || S || V] format where V is 0 or 1.
*
* The caller is responsible for ensuring that msg cannot be chosen
* directly by an attacker. It is usually preferable to use a cryptographic
* hash function on any input before handing it to this function.
*
* @param msg - message data to sign with a private key
* @param key - private key to create the signature with
* @return the signature of the message using the private key
*/
std::unique_ptr<ByteArray> sign(const ByteArray &msg, const ByteArray &key);
/**
* Check that the given public key created the signature over the message
*
* @param msg - the message the signature was build upon
* @param key - the public key of the signer
* @param sig - the produced signature to verify, without the recovery id (compact format, 64-Bytes)
* @return a Boolean indicating if the signature over the message was created using the public key
*/
bool verifySignature(const ByteArray &msg, const ByteArray &key, const ByteArray &sig);
/**
* Encode a public key according to the specified length.
* This method can be used to convert between public key formats.
* The input/output formats are chosen depending on the length of the input/output buffers.
*
* @param key - the public key to encode
* @param length - the length of the resulting public key
* @return the public key encoded according to the given length
*/
std::unique_ptr<ByteArray> encodePublicKey(const secp256k1::ByteArray &key, const PublicKeyLength &length);
/**
* Encode the given public key into a 33-byte compressed format.
*
* @param key - the public key to format
* @return the public key in compressed format (33-Byte)
*/
std::unique_ptr<ByteArray> compressPublicKey(const secp256k1::ByteArray &key) { return encodePublicKey(key, PublicKeyLength::Compressed); }
/**
* Parses a public key in the 33-byte compressed format and encode it into a 65-byte uncompressed format.
*
* @param key - the public key to format
* @return the public key in uncompressed format (65-Byte)
*/
std::unique_ptr<ByteArray> decompressPublicKey(const secp256k1::ByteArray &key) { return encodePublicKey(key, PublicKeyLength::Uncompressed); }
/**
* Recover the public key of an encoded compact signature.
*
* @param msg - the message the signature was created on
* @param sig - the signature in order to recover the public key
* @param length - the length of the resulting public key
* @return the public key encoded according to the given length
*/
std::unique_ptr<ByteArray> recoverPublicKey(const ByteArray &msg, const ByteArray &sig, const PublicKeyLength &length = PublicKeyLength::Compressed);
private:
Encryption();
~Encryption();
// a context for signing and signature verification.
secp256k1_context* ctx;
};
}
#endif //SECP256K1_LIBRARY_H