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
/
bridge.h
108 lines (94 loc) · 4 KB
/
bridge.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
#ifndef SECP256K1_BRIDGE_H
#define SECP256K1_BRIDGE_H
#include "library.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Container for a Keypair.
*/
typedef struct {
const unsigned char *privateKey;
const unsigned char *publicKey;
} KeyPair;
/**
* 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]
*/
static KeyPair 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
*/
static const unsigned char* computePublicKey(const secp256k1::ByteArray &key, const secp256k1::PublicKeyLength &length = secp256k1::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
*/
static const unsigned char* 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
*/
static const unsigned char* sign(const secp256k1::ByteArray &msg, const secp256k1::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
*/
static bool verifySignature(const secp256k1::ByteArray &msg, const secp256k1::ByteArray &key, const secp256k1::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
*/
static const unsigned char* encodePublicKey(const secp256k1::ByteArray &key, const secp256k1::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)
*/
static const unsigned char* compressPublicKey(const secp256k1::ByteArray &key);
/**
* 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)
*/
static const unsigned char* decompressPublicKey(const secp256k1::ByteArray &key);
/**
* 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
*/
static const unsigned char* recoverPublicKey(const secp256k1::ByteArray &msg, const secp256k1::ByteArray &sig, const secp256k1::PublicKeyLength &length = secp256k1::PublicKeyLength::Compressed);
#ifdef __cplusplus
} // extern "C"
#endif
#endif //SECP256K1_BRIDGE_H