@qntx/nostr-native 0.0.0-stage → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-APACHE +201 -0
- package/LICENSE-MIT +21 -0
- package/NostrNative.podspec +37 -0
- package/README.md +30 -2
- package/android/CMakeLists.txt +38 -0
- package/android/build.gradle +136 -0
- package/android/gradle.properties +4 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/cpp-adapter.cpp +7 -0
- package/android/src/main/java/com/margelo/nitro/nostrnative/NostrNativePackage.java +29 -0
- package/cpp/HybridNostrNative.cpp +52 -0
- package/cpp/HybridNostrNative.hpp +22 -0
- package/cpp/nostr_core.c +128 -0
- package/cpp/nostr_core.h +65 -0
- package/cpp/nostr_native.c +54 -0
- package/cpp/nostr_native.h +47 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.mjs +26 -0
- package/dist/index.mjs.map +1 -0
- package/nitro.json +19 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/NostrNative+autolinking.cmake +81 -0
- package/nitrogen/generated/android/NostrNative+autolinking.gradle +27 -0
- package/nitrogen/generated/android/NostrNativeOnLoad.cpp +49 -0
- package/nitrogen/generated/android/NostrNativeOnLoad.hpp +34 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nostrnative/NostrNativeOnLoad.kt +35 -0
- package/nitrogen/generated/ios/NostrNative+autolinking.rb +62 -0
- package/nitrogen/generated/ios/NostrNative-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/NostrNative-Swift-Cxx-Bridge.hpp +27 -0
- package/nitrogen/generated/ios/NostrNative-Swift-Cxx-Umbrella.hpp +38 -0
- package/nitrogen/generated/ios/NostrNativeAutolinking.mm +35 -0
- package/nitrogen/generated/ios/NostrNativeAutolinking.swift +16 -0
- package/nitrogen/generated/shared/c++/HybridNostrNativeSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridNostrNativeSpec.hpp +64 -0
- package/package.json +86 -4
- package/react-native.config.js +12 -0
- package/vendor/secp256k1/COPYING +19 -0
- package/vendor/secp256k1/include/secp256k1.h +931 -0
- package/vendor/secp256k1/include/secp256k1_ecdh.h +63 -0
- package/vendor/secp256k1/include/secp256k1_ellswift.h +200 -0
- package/vendor/secp256k1/include/secp256k1_extrakeys.h +250 -0
- package/vendor/secp256k1/include/secp256k1_musig.h +588 -0
- package/vendor/secp256k1/include/secp256k1_preallocated.h +134 -0
- package/vendor/secp256k1/include/secp256k1_recovery.h +123 -0
- package/vendor/secp256k1/include/secp256k1_schnorrsig.h +179 -0
- package/vendor/secp256k1/include/secp256k1_silentpayments.h +396 -0
- package/vendor/secp256k1/src/assumptions.h +87 -0
- package/vendor/secp256k1/src/checkmem.h +117 -0
- package/vendor/secp256k1/src/ecdsa.h +21 -0
- package/vendor/secp256k1/src/ecdsa_impl.h +309 -0
- package/vendor/secp256k1/src/eckey.h +28 -0
- package/vendor/secp256k1/src/eckey_impl.h +94 -0
- package/vendor/secp256k1/src/ecmult.h +64 -0
- package/vendor/secp256k1/src/ecmult_compute_table.h +16 -0
- package/vendor/secp256k1/src/ecmult_compute_table_impl.h +49 -0
- package/vendor/secp256k1/src/ecmult_const.h +38 -0
- package/vendor/secp256k1/src/ecmult_const_impl.h +402 -0
- package/vendor/secp256k1/src/ecmult_gen.h +145 -0
- package/vendor/secp256k1/src/ecmult_gen_compute_table.h +14 -0
- package/vendor/secp256k1/src/ecmult_gen_compute_table_impl.h +108 -0
- package/vendor/secp256k1/src/ecmult_gen_impl.h +347 -0
- package/vendor/secp256k1/src/ecmult_impl.h +869 -0
- package/vendor/secp256k1/src/field.h +351 -0
- package/vendor/secp256k1/src/field_10x26.h +57 -0
- package/vendor/secp256k1/src/field_10x26_impl.h +1210 -0
- package/vendor/secp256k1/src/field_5x52.h +62 -0
- package/vendor/secp256k1/src/field_5x52_impl.h +496 -0
- package/vendor/secp256k1/src/field_5x52_int128_impl.h +274 -0
- package/vendor/secp256k1/src/field_impl.h +457 -0
- package/vendor/secp256k1/src/group.h +218 -0
- package/vendor/secp256k1/src/group_impl.h +1014 -0
- package/vendor/secp256k1/src/hash.h +56 -0
- package/vendor/secp256k1/src/hash_impl.h +397 -0
- package/vendor/secp256k1/src/hsort.h +33 -0
- package/vendor/secp256k1/src/hsort_impl.h +125 -0
- package/vendor/secp256k1/src/int128.h +90 -0
- package/vendor/secp256k1/src/int128_impl.h +18 -0
- package/vendor/secp256k1/src/int128_native.h +19 -0
- package/vendor/secp256k1/src/int128_native_impl.h +94 -0
- package/vendor/secp256k1/src/int128_struct.h +14 -0
- package/vendor/secp256k1/src/int128_struct_impl.h +205 -0
- package/vendor/secp256k1/src/modinv32.h +43 -0
- package/vendor/secp256k1/src/modinv32_impl.h +725 -0
- package/vendor/secp256k1/src/modinv64.h +47 -0
- package/vendor/secp256k1/src/modinv64_impl.h +780 -0
- package/vendor/secp256k1/src/modules/extrakeys/main_impl.h +292 -0
- package/vendor/secp256k1/src/modules/schnorrsig/main_impl.h +256 -0
- package/vendor/secp256k1/src/precomputed_ecmult.c +16456 -0
- package/vendor/secp256k1/src/precomputed_ecmult.h +40 -0
- package/vendor/secp256k1/src/precomputed_ecmult_gen.c +1779 -0
- package/vendor/secp256k1/src/precomputed_ecmult_gen.h +28 -0
- package/vendor/secp256k1/src/scalar.h +105 -0
- package/vendor/secp256k1/src/scalar_4x64.h +19 -0
- package/vendor/secp256k1/src/scalar_4x64_impl.h +1005 -0
- package/vendor/secp256k1/src/scalar_8x32.h +19 -0
- package/vendor/secp256k1/src/scalar_8x32_impl.h +820 -0
- package/vendor/secp256k1/src/scalar_impl.h +321 -0
- package/vendor/secp256k1/src/scalar_low.h +24 -0
- package/vendor/secp256k1/src/scalar_low_impl.h +217 -0
- package/vendor/secp256k1/src/scratch.h +44 -0
- package/vendor/secp256k1/src/scratch_impl.h +99 -0
- package/vendor/secp256k1/src/secp256k1.c +855 -0
- package/vendor/secp256k1/src/selftest.h +41 -0
- package/vendor/secp256k1/src/util.h +479 -0
- package/vendor/secp256k1/src/util_local_visibility.h +12 -0
- package/vendor/secp256k1/vendored.json +6 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
#ifndef SECP256K1_PREALLOCATED_H
|
|
2
|
+
#define SECP256K1_PREALLOCATED_H
|
|
3
|
+
|
|
4
|
+
#include "secp256k1.h"
|
|
5
|
+
|
|
6
|
+
#ifdef __cplusplus
|
|
7
|
+
extern "C" {
|
|
8
|
+
#endif
|
|
9
|
+
|
|
10
|
+
/* The module provided by this header file is intended for settings in which it
|
|
11
|
+
* is not possible or desirable to rely on dynamic memory allocation. It provides
|
|
12
|
+
* functions for creating, cloning, and destroying secp256k1 context objects in a
|
|
13
|
+
* contiguous fixed-size block of memory provided by the caller.
|
|
14
|
+
*
|
|
15
|
+
* Context objects created by functions in this module can be used like contexts
|
|
16
|
+
* objects created by functions in secp256k1.h, i.e., they can be passed to any
|
|
17
|
+
* API function that expects a context object (see secp256k1.h for details). The
|
|
18
|
+
* only exception is that context objects created by functions in this module
|
|
19
|
+
* must be destroyed using nostr_native_secp256k1_context_preallocated_destroy (in this
|
|
20
|
+
* module) instead of nostr_native_secp256k1_context_destroy (in secp256k1.h).
|
|
21
|
+
*
|
|
22
|
+
* It is guaranteed that functions in this module will not call malloc or its
|
|
23
|
+
* friends realloc, calloc, and free.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** Determine the memory size of a secp256k1 context object to be created in
|
|
27
|
+
* caller-provided memory.
|
|
28
|
+
*
|
|
29
|
+
* The purpose of this function is to determine how much memory must be provided
|
|
30
|
+
* to nostr_native_secp256k1_context_preallocated_create.
|
|
31
|
+
*
|
|
32
|
+
* Returns: the required size of the caller-provided memory block
|
|
33
|
+
* In: flags: which parts of the context to initialize.
|
|
34
|
+
*/
|
|
35
|
+
SECP256K1_API size_t nostr_native_secp256k1_context_preallocated_size(
|
|
36
|
+
unsigned int flags
|
|
37
|
+
) SECP256K1_WARN_UNUSED_RESULT;
|
|
38
|
+
|
|
39
|
+
/** Create a secp256k1 context object in caller-provided memory.
|
|
40
|
+
*
|
|
41
|
+
* The caller must provide a pointer to a rewritable contiguous block of memory
|
|
42
|
+
* of size at least nostr_native_secp256k1_context_preallocated_size(flags) bytes, suitably
|
|
43
|
+
* aligned to hold an object of any type.
|
|
44
|
+
*
|
|
45
|
+
* The block of memory is exclusively owned by the created context object during
|
|
46
|
+
* the lifetime of this context object, which begins with the call to this
|
|
47
|
+
* function and ends when a call to nostr_native_secp256k1_context_preallocated_destroy
|
|
48
|
+
* (which destroys the context object again) returns. During the lifetime of the
|
|
49
|
+
* context object, the caller is obligated not to access this block of memory,
|
|
50
|
+
* i.e., the caller may not read or write the memory, e.g., by copying the memory
|
|
51
|
+
* contents to a different location or trying to create a second context object
|
|
52
|
+
* in the memory. In simpler words, the prealloc pointer (or any pointer derived
|
|
53
|
+
* from it) should not be used during the lifetime of the context object.
|
|
54
|
+
*
|
|
55
|
+
* Returns: pointer to newly created context object.
|
|
56
|
+
* In: prealloc: pointer to a rewritable contiguous block of memory of
|
|
57
|
+
* size at least nostr_native_secp256k1_context_preallocated_size(flags)
|
|
58
|
+
* bytes, as detailed above.
|
|
59
|
+
* flags: which parts of the context to initialize.
|
|
60
|
+
*
|
|
61
|
+
* See nostr_native_secp256k1_context_create (in secp256k1.h) for further details.
|
|
62
|
+
*
|
|
63
|
+
* See also nostr_native_secp256k1_context_randomize (in secp256k1.h)
|
|
64
|
+
* and nostr_native_secp256k1_context_preallocated_destroy.
|
|
65
|
+
*/
|
|
66
|
+
SECP256K1_API nostr_native_secp256k1_context *nostr_native_secp256k1_context_preallocated_create(
|
|
67
|
+
void *prealloc,
|
|
68
|
+
unsigned int flags
|
|
69
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
|
|
70
|
+
|
|
71
|
+
/** Determine the memory size of a secp256k1 context object to be copied into
|
|
72
|
+
* caller-provided memory.
|
|
73
|
+
*
|
|
74
|
+
* Returns: the required size of the caller-provided memory block.
|
|
75
|
+
* In: ctx: pointer to a context to copy.
|
|
76
|
+
*/
|
|
77
|
+
SECP256K1_API size_t nostr_native_secp256k1_context_preallocated_clone_size(
|
|
78
|
+
const nostr_native_secp256k1_context *ctx
|
|
79
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
|
|
80
|
+
|
|
81
|
+
/** Copy a secp256k1 context object into caller-provided memory.
|
|
82
|
+
*
|
|
83
|
+
* The caller must provide a pointer to a rewritable contiguous block of memory
|
|
84
|
+
* of size at least nostr_native_secp256k1_context_preallocated_size(flags) bytes, suitably
|
|
85
|
+
* aligned to hold an object of any type.
|
|
86
|
+
*
|
|
87
|
+
* The block of memory is exclusively owned by the created context object during
|
|
88
|
+
* the lifetime of this context object, see the description of
|
|
89
|
+
* nostr_native_secp256k1_context_preallocated_create for details.
|
|
90
|
+
*
|
|
91
|
+
* Cloning nostr_native_secp256k1_context_static is not possible, and should not be emulated by
|
|
92
|
+
* the caller (e.g., using memcpy). Create a new context instead.
|
|
93
|
+
*
|
|
94
|
+
* Returns: pointer to a newly created context object.
|
|
95
|
+
* Args: ctx: pointer to a context to copy (not nostr_native_secp256k1_context_static).
|
|
96
|
+
* In: prealloc: pointer to a rewritable contiguous block of memory of
|
|
97
|
+
* size at least nostr_native_secp256k1_context_preallocated_size(flags)
|
|
98
|
+
* bytes, as detailed above.
|
|
99
|
+
*/
|
|
100
|
+
SECP256K1_API nostr_native_secp256k1_context *nostr_native_secp256k1_context_preallocated_clone(
|
|
101
|
+
const nostr_native_secp256k1_context *ctx,
|
|
102
|
+
void *prealloc
|
|
103
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_WARN_UNUSED_RESULT;
|
|
104
|
+
|
|
105
|
+
/** Destroy a secp256k1 context object that has been created in
|
|
106
|
+
* caller-provided memory.
|
|
107
|
+
*
|
|
108
|
+
* The context pointer may not be used afterwards.
|
|
109
|
+
*
|
|
110
|
+
* The context to destroy must have been created using
|
|
111
|
+
* nostr_native_secp256k1_context_preallocated_create or nostr_native_secp256k1_context_preallocated_clone.
|
|
112
|
+
* If the context has instead been created using nostr_native_secp256k1_context_create or
|
|
113
|
+
* nostr_native_secp256k1_context_clone, the behaviour is undefined. In that case,
|
|
114
|
+
* nostr_native_secp256k1_context_destroy must be used instead.
|
|
115
|
+
*
|
|
116
|
+
* If required, it is the responsibility of the caller to deallocate the block
|
|
117
|
+
* of memory properly after this function returns, e.g., by calling free on the
|
|
118
|
+
* preallocated pointer given to nostr_native_secp256k1_context_preallocated_create or
|
|
119
|
+
* nostr_native_secp256k1_context_preallocated_clone.
|
|
120
|
+
*
|
|
121
|
+
* Args: ctx: pointer to a context to destroy, constructed using
|
|
122
|
+
* nostr_native_secp256k1_context_preallocated_create or
|
|
123
|
+
* nostr_native_secp256k1_context_preallocated_clone
|
|
124
|
+
* (i.e., not nostr_native_secp256k1_context_static).
|
|
125
|
+
*/
|
|
126
|
+
SECP256K1_API void nostr_native_secp256k1_context_preallocated_destroy(
|
|
127
|
+
nostr_native_secp256k1_context *ctx
|
|
128
|
+
) SECP256K1_ARG_NONNULL(1);
|
|
129
|
+
|
|
130
|
+
#ifdef __cplusplus
|
|
131
|
+
}
|
|
132
|
+
#endif
|
|
133
|
+
|
|
134
|
+
#endif /* SECP256K1_PREALLOCATED_H */
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
#ifndef SECP256K1_RECOVERY_H
|
|
2
|
+
#define SECP256K1_RECOVERY_H
|
|
3
|
+
|
|
4
|
+
#include "secp256k1.h"
|
|
5
|
+
|
|
6
|
+
#ifdef __cplusplus
|
|
7
|
+
extern "C" {
|
|
8
|
+
#endif
|
|
9
|
+
|
|
10
|
+
/** Opaque data structure that holds a parsed ECDSA signature,
|
|
11
|
+
* supporting pubkey recovery.
|
|
12
|
+
*
|
|
13
|
+
* The exact representation of data inside is implementation defined and not
|
|
14
|
+
* guaranteed to be portable between different platforms or versions. It is
|
|
15
|
+
* however guaranteed to be 65 bytes in size, and can be safely copied/moved.
|
|
16
|
+
* If you need to convert to a format suitable for storage or transmission, use
|
|
17
|
+
* the nostr_native_secp256k1_ecdsa_signature_serialize_* and
|
|
18
|
+
* nostr_native_secp256k1_ecdsa_signature_parse_* functions.
|
|
19
|
+
*
|
|
20
|
+
* Furthermore, it is guaranteed that identical signatures (including their
|
|
21
|
+
* recoverability) will have identical representation, so they can be
|
|
22
|
+
* memcmp'ed.
|
|
23
|
+
*/
|
|
24
|
+
typedef struct nostr_native_secp256k1_ecdsa_recoverable_signature {
|
|
25
|
+
unsigned char data[65];
|
|
26
|
+
} nostr_native_secp256k1_ecdsa_recoverable_signature;
|
|
27
|
+
|
|
28
|
+
/** Parse a compact ECDSA signature (64 bytes + recovery id).
|
|
29
|
+
*
|
|
30
|
+
* Returns: 1 when the signature could be parsed, 0 otherwise
|
|
31
|
+
* Args: ctx: pointer to a context object
|
|
32
|
+
* Out: sig: pointer to a signature object
|
|
33
|
+
* In: input64: pointer to a 64-byte compact signature
|
|
34
|
+
* recid: the recovery id (0, 1, 2 or 3)
|
|
35
|
+
*/
|
|
36
|
+
SECP256K1_API int nostr_native_secp256k1_ecdsa_recoverable_signature_parse_compact(
|
|
37
|
+
const nostr_native_secp256k1_context *ctx,
|
|
38
|
+
nostr_native_secp256k1_ecdsa_recoverable_signature *sig,
|
|
39
|
+
const unsigned char *input64,
|
|
40
|
+
int recid
|
|
41
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
|
42
|
+
|
|
43
|
+
/** Convert a recoverable signature into a normal signature.
|
|
44
|
+
*
|
|
45
|
+
* Returns: 1
|
|
46
|
+
* Args: ctx: pointer to a context object.
|
|
47
|
+
* Out: sig: pointer to a normal signature.
|
|
48
|
+
* In: sigin: pointer to a recoverable signature.
|
|
49
|
+
*/
|
|
50
|
+
SECP256K1_API int nostr_native_secp256k1_ecdsa_recoverable_signature_convert(
|
|
51
|
+
const nostr_native_secp256k1_context *ctx,
|
|
52
|
+
nostr_native_secp256k1_ecdsa_signature *sig,
|
|
53
|
+
const nostr_native_secp256k1_ecdsa_recoverable_signature *sigin
|
|
54
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
|
55
|
+
|
|
56
|
+
/** Serialize an ECDSA signature in compact format (64 bytes + recovery id).
|
|
57
|
+
*
|
|
58
|
+
* Returns: 1
|
|
59
|
+
* Args: ctx: pointer to a context object.
|
|
60
|
+
* Out: output64: pointer to a 64-byte array of the compact signature.
|
|
61
|
+
* recid: pointer to an integer to hold the recovery id.
|
|
62
|
+
* In: sig: pointer to an initialized signature object.
|
|
63
|
+
*/
|
|
64
|
+
SECP256K1_API int nostr_native_secp256k1_ecdsa_recoverable_signature_serialize_compact(
|
|
65
|
+
const nostr_native_secp256k1_context *ctx,
|
|
66
|
+
unsigned char *output64,
|
|
67
|
+
int *recid,
|
|
68
|
+
const nostr_native_secp256k1_ecdsa_recoverable_signature *sig
|
|
69
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
|
|
70
|
+
|
|
71
|
+
/** Create a recoverable ECDSA signature.
|
|
72
|
+
*
|
|
73
|
+
* Returns: 1: signature created
|
|
74
|
+
* 0: the nonce generation function failed, or the secret key was invalid.
|
|
75
|
+
* Args: ctx: pointer to a context object (not nostr_native_secp256k1_context_static).
|
|
76
|
+
* Out: sig: pointer to a signature object.
|
|
77
|
+
* In: msghash32: the 32-byte message hash being signed.
|
|
78
|
+
* seckey: pointer to a 32-byte secret key.
|
|
79
|
+
* noncefp: pointer to a nonce generation function. If NULL,
|
|
80
|
+
* nostr_native_secp256k1_nonce_function_default is used.
|
|
81
|
+
* ndata: pointer to arbitrary data used by the nonce generation function
|
|
82
|
+
* (can be NULL for nostr_native_secp256k1_nonce_function_default).
|
|
83
|
+
*/
|
|
84
|
+
SECP256K1_API int nostr_native_secp256k1_ecdsa_sign_recoverable(
|
|
85
|
+
const nostr_native_secp256k1_context *ctx,
|
|
86
|
+
nostr_native_secp256k1_ecdsa_recoverable_signature *sig,
|
|
87
|
+
const unsigned char *msghash32,
|
|
88
|
+
const unsigned char *seckey,
|
|
89
|
+
nostr_native_secp256k1_nonce_function noncefp,
|
|
90
|
+
const void *ndata
|
|
91
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
|
|
92
|
+
|
|
93
|
+
/** Recover an ECDSA public key from a signature.
|
|
94
|
+
*
|
|
95
|
+
* Successful public key recovery guarantees that the signature, after normalization,
|
|
96
|
+
* passes `nostr_native_secp256k1_ecdsa_verify`. Thus, explicit verification is not necessary.
|
|
97
|
+
*
|
|
98
|
+
* However, a recoverable signature that successfully passes `nostr_native_secp256k1_ecdsa_recover`,
|
|
99
|
+
* when converted to a non-recoverable signature (using
|
|
100
|
+
* `nostr_native_secp256k1_ecdsa_recoverable_signature_convert`), is not guaranteed to be
|
|
101
|
+
* normalized and thus not guaranteed to pass `nostr_native_secp256k1_ecdsa_verify`. If a
|
|
102
|
+
* normalized signature is required, call `nostr_native_secp256k1_ecdsa_signature_normalize`
|
|
103
|
+
* after `nostr_native_secp256k1_ecdsa_recoverable_signature_convert`.
|
|
104
|
+
*
|
|
105
|
+
* Returns: 1: public key successfully recovered
|
|
106
|
+
* 0: otherwise.
|
|
107
|
+
* Args: ctx: pointer to a context object.
|
|
108
|
+
* Out: pubkey: pointer to the recovered public key.
|
|
109
|
+
* In: sig: pointer to initialized signature that supports pubkey recovery.
|
|
110
|
+
* msghash32: the 32-byte message hash assumed to be signed.
|
|
111
|
+
*/
|
|
112
|
+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ecdsa_recover(
|
|
113
|
+
const nostr_native_secp256k1_context *ctx,
|
|
114
|
+
nostr_native_secp256k1_pubkey *pubkey,
|
|
115
|
+
const nostr_native_secp256k1_ecdsa_recoverable_signature *sig,
|
|
116
|
+
const unsigned char *msghash32
|
|
117
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
|
|
118
|
+
|
|
119
|
+
#ifdef __cplusplus
|
|
120
|
+
}
|
|
121
|
+
#endif
|
|
122
|
+
|
|
123
|
+
#endif /* SECP256K1_RECOVERY_H */
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
#ifndef SECP256K1_SCHNORRSIG_H
|
|
2
|
+
#define SECP256K1_SCHNORRSIG_H
|
|
3
|
+
|
|
4
|
+
#include "secp256k1.h"
|
|
5
|
+
#include "secp256k1_extrakeys.h"
|
|
6
|
+
|
|
7
|
+
#ifdef __cplusplus
|
|
8
|
+
extern "C" {
|
|
9
|
+
#endif
|
|
10
|
+
|
|
11
|
+
/** This module implements a variant of Schnorr signatures compliant with
|
|
12
|
+
* Bitcoin Improvement Proposal 340 "Schnorr Signatures for secp256k1"
|
|
13
|
+
* (https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** A pointer to a function to deterministically generate a nonce.
|
|
17
|
+
*
|
|
18
|
+
* Same as nostr_native_secp256k1_nonce function with the exception of accepting an
|
|
19
|
+
* additional pubkey argument and not requiring an attempt argument. The pubkey
|
|
20
|
+
* argument can protect signature schemes with key-prefixed challenge hash
|
|
21
|
+
* inputs against reusing the nonce when signing with the wrong precomputed
|
|
22
|
+
* pubkey.
|
|
23
|
+
*
|
|
24
|
+
* Returns: 1 if a nonce was successfully generated. 0 will cause signing to
|
|
25
|
+
* return an error.
|
|
26
|
+
* Out: nonce32: pointer to a 32-byte array to be filled by the function
|
|
27
|
+
* In: msg: the message being verified. Is NULL if and only if msglen
|
|
28
|
+
* is 0.
|
|
29
|
+
* msglen: the length of the message
|
|
30
|
+
* key32: pointer to a 32-byte secret key (will not be NULL)
|
|
31
|
+
* xonly_pk32: the 32-byte serialized xonly pubkey corresponding to key32
|
|
32
|
+
* (will not be NULL)
|
|
33
|
+
* algo: pointer to an array describing the signature
|
|
34
|
+
* algorithm (will not be NULL)
|
|
35
|
+
* algolen: the length of the algo array
|
|
36
|
+
* data: arbitrary data pointer that is passed through
|
|
37
|
+
*
|
|
38
|
+
* Except for test cases, this function should compute some cryptographic hash of
|
|
39
|
+
* the message, the key, the pubkey, the algorithm description, and data.
|
|
40
|
+
*/
|
|
41
|
+
typedef int (*nostr_native_secp256k1_nonce_function_hardened)(
|
|
42
|
+
unsigned char *nonce32,
|
|
43
|
+
const unsigned char *msg,
|
|
44
|
+
size_t msglen,
|
|
45
|
+
const unsigned char *key32,
|
|
46
|
+
const unsigned char *xonly_pk32,
|
|
47
|
+
const unsigned char *algo,
|
|
48
|
+
size_t algolen,
|
|
49
|
+
void *data
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
/** An implementation of the nonce generation function as defined in Bitcoin
|
|
53
|
+
* Improvement Proposal 340 "Schnorr Signatures for secp256k1"
|
|
54
|
+
* (https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki).
|
|
55
|
+
*
|
|
56
|
+
* If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
|
|
57
|
+
* auxiliary random data as defined in BIP-340. If the data pointer is NULL,
|
|
58
|
+
* the nonce derivation procedure follows BIP-340 by setting the auxiliary
|
|
59
|
+
* random data to zero. The algo argument must be non-NULL, otherwise the
|
|
60
|
+
* function will fail and return 0. The hash will be tagged with algo.
|
|
61
|
+
* Therefore, to create BIP-340 compliant signatures, algo must be set to
|
|
62
|
+
* "BIP0340/nonce" and algolen to 13.
|
|
63
|
+
*/
|
|
64
|
+
SECP256K1_API const nostr_native_secp256k1_nonce_function_hardened nostr_native_secp256k1_nonce_function_bip340;
|
|
65
|
+
|
|
66
|
+
/** Data structure that contains additional arguments for schnorrsig_sign_custom.
|
|
67
|
+
*
|
|
68
|
+
* A schnorrsig_extraparams structure object can be initialized correctly by
|
|
69
|
+
* setting it to SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT.
|
|
70
|
+
*
|
|
71
|
+
* Members:
|
|
72
|
+
* magic: set to SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC at initialization
|
|
73
|
+
* and has no other function than making sure the object is
|
|
74
|
+
* initialized.
|
|
75
|
+
* noncefp: pointer to a nonce generation function. If NULL,
|
|
76
|
+
* nostr_native_secp256k1_nonce_function_bip340 is used
|
|
77
|
+
* ndata: pointer to arbitrary data used by the nonce generation function
|
|
78
|
+
* (can be NULL). If it is non-NULL and
|
|
79
|
+
* nostr_native_secp256k1_nonce_function_bip340 is used, then ndata must be a
|
|
80
|
+
* pointer to 32-byte auxiliary randomness as per BIP-340.
|
|
81
|
+
*/
|
|
82
|
+
typedef struct nostr_native_secp256k1_schnorrsig_extraparams {
|
|
83
|
+
unsigned char magic[4];
|
|
84
|
+
nostr_native_secp256k1_nonce_function_hardened noncefp;
|
|
85
|
+
void *ndata;
|
|
86
|
+
} nostr_native_secp256k1_schnorrsig_extraparams;
|
|
87
|
+
|
|
88
|
+
#define SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC { 0xda, 0x6f, 0xb3, 0x8c }
|
|
89
|
+
#define SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT {\
|
|
90
|
+
SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC,\
|
|
91
|
+
NULL,\
|
|
92
|
+
NULL\
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Create a Schnorr signature.
|
|
96
|
+
*
|
|
97
|
+
* Does _not_ strictly follow BIP-340 because it does not verify the resulting
|
|
98
|
+
* signature. Instead, you can manually use nostr_native_secp256k1_schnorrsig_verify and
|
|
99
|
+
* abort if it fails.
|
|
100
|
+
*
|
|
101
|
+
* This function only signs 32-byte messages. If you have messages of a
|
|
102
|
+
* different size (or the same size but without a context-specific tag
|
|
103
|
+
* prefix), it is recommended to create a 32-byte message hash with
|
|
104
|
+
* nostr_native_secp256k1_tagged_sha256 and then sign the hash. Tagged hashing allows
|
|
105
|
+
* providing an context-specific tag for domain separation. This prevents
|
|
106
|
+
* signatures from being valid in multiple contexts by accident.
|
|
107
|
+
*
|
|
108
|
+
* Returns 1 on success, 0 on failure.
|
|
109
|
+
* Args: ctx: pointer to a context object (not nostr_native_secp256k1_context_static).
|
|
110
|
+
* Out: sig64: pointer to a 64-byte array to store the serialized signature.
|
|
111
|
+
* In: msg32: the 32-byte message being signed.
|
|
112
|
+
* keypair: pointer to an initialized keypair.
|
|
113
|
+
* aux_rand32: 32 bytes of fresh randomness. While recommended to provide
|
|
114
|
+
* this, it is only supplemental to security and can be NULL. A
|
|
115
|
+
* NULL argument is treated the same as an all-zero one. See
|
|
116
|
+
* BIP-340 "Default Signing" for a full explanation of this
|
|
117
|
+
* argument and for guidance if randomness is expensive.
|
|
118
|
+
*/
|
|
119
|
+
SECP256K1_API int nostr_native_secp256k1_schnorrsig_sign32(
|
|
120
|
+
const nostr_native_secp256k1_context *ctx,
|
|
121
|
+
unsigned char *sig64,
|
|
122
|
+
const unsigned char *msg32,
|
|
123
|
+
const nostr_native_secp256k1_keypair *keypair,
|
|
124
|
+
const unsigned char *aux_rand32
|
|
125
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
|
|
126
|
+
|
|
127
|
+
/** Create a Schnorr signature with a more flexible API.
|
|
128
|
+
*
|
|
129
|
+
* Same arguments as nostr_native_secp256k1_schnorrsig_sign32 except that it allows signing
|
|
130
|
+
* variable length messages and accepts a pointer to an extraparams object that
|
|
131
|
+
* allows customizing signing by passing additional arguments.
|
|
132
|
+
*
|
|
133
|
+
* Equivalent to nostr_native_secp256k1_schnorrsig_sign32(..., aux_rand32) if msglen is 32
|
|
134
|
+
* and extraparams is initialized as follows:
|
|
135
|
+
* ```
|
|
136
|
+
* nostr_native_secp256k1_schnorrsig_extraparams extraparams = SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT;
|
|
137
|
+
* extraparams.ndata = (unsigned char*)aux_rand32;
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* Returns 1 on success, 0 on failure.
|
|
141
|
+
* Args: ctx: pointer to a context object (not nostr_native_secp256k1_context_static).
|
|
142
|
+
* Out: sig64: pointer to a 64-byte array to store the serialized signature.
|
|
143
|
+
* In: msg: the message being signed. Can only be NULL if msglen is 0.
|
|
144
|
+
* msglen: length of the message.
|
|
145
|
+
* keypair: pointer to an initialized keypair.
|
|
146
|
+
* extraparams: pointer to an extraparams object (can be NULL).
|
|
147
|
+
*/
|
|
148
|
+
SECP256K1_API int nostr_native_secp256k1_schnorrsig_sign_custom(
|
|
149
|
+
const nostr_native_secp256k1_context *ctx,
|
|
150
|
+
unsigned char *sig64,
|
|
151
|
+
const unsigned char *msg,
|
|
152
|
+
size_t msglen,
|
|
153
|
+
const nostr_native_secp256k1_keypair *keypair,
|
|
154
|
+
nostr_native_secp256k1_schnorrsig_extraparams *extraparams
|
|
155
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(5);
|
|
156
|
+
|
|
157
|
+
/** Verify a Schnorr signature.
|
|
158
|
+
*
|
|
159
|
+
* Returns: 1: correct signature
|
|
160
|
+
* 0: incorrect signature
|
|
161
|
+
* Args: ctx: pointer to a context object.
|
|
162
|
+
* In: sig64: pointer to the 64-byte signature to verify.
|
|
163
|
+
* msg: the message being verified. Can only be NULL if msglen is 0.
|
|
164
|
+
* msglen: length of the message
|
|
165
|
+
* pubkey: pointer to an x-only public key to verify with
|
|
166
|
+
*/
|
|
167
|
+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_schnorrsig_verify(
|
|
168
|
+
const nostr_native_secp256k1_context *ctx,
|
|
169
|
+
const unsigned char *sig64,
|
|
170
|
+
const unsigned char *msg,
|
|
171
|
+
size_t msglen,
|
|
172
|
+
const nostr_native_secp256k1_xonly_pubkey *pubkey
|
|
173
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(5);
|
|
174
|
+
|
|
175
|
+
#ifdef __cplusplus
|
|
176
|
+
}
|
|
177
|
+
#endif
|
|
178
|
+
|
|
179
|
+
#endif /* SECP256K1_SCHNORRSIG_H */
|