@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.
Files changed (106) hide show
  1. package/LICENSE-APACHE +201 -0
  2. package/LICENSE-MIT +21 -0
  3. package/NostrNative.podspec +37 -0
  4. package/README.md +30 -2
  5. package/android/CMakeLists.txt +38 -0
  6. package/android/build.gradle +136 -0
  7. package/android/gradle.properties +4 -0
  8. package/android/src/main/AndroidManifest.xml +2 -0
  9. package/android/src/main/cpp/cpp-adapter.cpp +7 -0
  10. package/android/src/main/java/com/margelo/nitro/nostrnative/NostrNativePackage.java +29 -0
  11. package/cpp/HybridNostrNative.cpp +52 -0
  12. package/cpp/HybridNostrNative.hpp +22 -0
  13. package/cpp/nostr_core.c +128 -0
  14. package/cpp/nostr_core.h +65 -0
  15. package/cpp/nostr_native.c +54 -0
  16. package/cpp/nostr_native.h +47 -0
  17. package/dist/index.d.mts +15 -0
  18. package/dist/index.mjs +26 -0
  19. package/dist/index.mjs.map +1 -0
  20. package/nitro.json +19 -0
  21. package/nitrogen/generated/.gitattributes +1 -0
  22. package/nitrogen/generated/android/NostrNative+autolinking.cmake +81 -0
  23. package/nitrogen/generated/android/NostrNative+autolinking.gradle +27 -0
  24. package/nitrogen/generated/android/NostrNativeOnLoad.cpp +49 -0
  25. package/nitrogen/generated/android/NostrNativeOnLoad.hpp +34 -0
  26. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nostrnative/NostrNativeOnLoad.kt +35 -0
  27. package/nitrogen/generated/ios/NostrNative+autolinking.rb +62 -0
  28. package/nitrogen/generated/ios/NostrNative-Swift-Cxx-Bridge.cpp +17 -0
  29. package/nitrogen/generated/ios/NostrNative-Swift-Cxx-Bridge.hpp +27 -0
  30. package/nitrogen/generated/ios/NostrNative-Swift-Cxx-Umbrella.hpp +38 -0
  31. package/nitrogen/generated/ios/NostrNativeAutolinking.mm +35 -0
  32. package/nitrogen/generated/ios/NostrNativeAutolinking.swift +16 -0
  33. package/nitrogen/generated/shared/c++/HybridNostrNativeSpec.cpp +23 -0
  34. package/nitrogen/generated/shared/c++/HybridNostrNativeSpec.hpp +64 -0
  35. package/package.json +86 -4
  36. package/react-native.config.js +12 -0
  37. package/vendor/secp256k1/COPYING +19 -0
  38. package/vendor/secp256k1/include/secp256k1.h +931 -0
  39. package/vendor/secp256k1/include/secp256k1_ecdh.h +63 -0
  40. package/vendor/secp256k1/include/secp256k1_ellswift.h +200 -0
  41. package/vendor/secp256k1/include/secp256k1_extrakeys.h +250 -0
  42. package/vendor/secp256k1/include/secp256k1_musig.h +588 -0
  43. package/vendor/secp256k1/include/secp256k1_preallocated.h +134 -0
  44. package/vendor/secp256k1/include/secp256k1_recovery.h +123 -0
  45. package/vendor/secp256k1/include/secp256k1_schnorrsig.h +179 -0
  46. package/vendor/secp256k1/include/secp256k1_silentpayments.h +396 -0
  47. package/vendor/secp256k1/src/assumptions.h +87 -0
  48. package/vendor/secp256k1/src/checkmem.h +117 -0
  49. package/vendor/secp256k1/src/ecdsa.h +21 -0
  50. package/vendor/secp256k1/src/ecdsa_impl.h +309 -0
  51. package/vendor/secp256k1/src/eckey.h +28 -0
  52. package/vendor/secp256k1/src/eckey_impl.h +94 -0
  53. package/vendor/secp256k1/src/ecmult.h +64 -0
  54. package/vendor/secp256k1/src/ecmult_compute_table.h +16 -0
  55. package/vendor/secp256k1/src/ecmult_compute_table_impl.h +49 -0
  56. package/vendor/secp256k1/src/ecmult_const.h +38 -0
  57. package/vendor/secp256k1/src/ecmult_const_impl.h +402 -0
  58. package/vendor/secp256k1/src/ecmult_gen.h +145 -0
  59. package/vendor/secp256k1/src/ecmult_gen_compute_table.h +14 -0
  60. package/vendor/secp256k1/src/ecmult_gen_compute_table_impl.h +108 -0
  61. package/vendor/secp256k1/src/ecmult_gen_impl.h +347 -0
  62. package/vendor/secp256k1/src/ecmult_impl.h +869 -0
  63. package/vendor/secp256k1/src/field.h +351 -0
  64. package/vendor/secp256k1/src/field_10x26.h +57 -0
  65. package/vendor/secp256k1/src/field_10x26_impl.h +1210 -0
  66. package/vendor/secp256k1/src/field_5x52.h +62 -0
  67. package/vendor/secp256k1/src/field_5x52_impl.h +496 -0
  68. package/vendor/secp256k1/src/field_5x52_int128_impl.h +274 -0
  69. package/vendor/secp256k1/src/field_impl.h +457 -0
  70. package/vendor/secp256k1/src/group.h +218 -0
  71. package/vendor/secp256k1/src/group_impl.h +1014 -0
  72. package/vendor/secp256k1/src/hash.h +56 -0
  73. package/vendor/secp256k1/src/hash_impl.h +397 -0
  74. package/vendor/secp256k1/src/hsort.h +33 -0
  75. package/vendor/secp256k1/src/hsort_impl.h +125 -0
  76. package/vendor/secp256k1/src/int128.h +90 -0
  77. package/vendor/secp256k1/src/int128_impl.h +18 -0
  78. package/vendor/secp256k1/src/int128_native.h +19 -0
  79. package/vendor/secp256k1/src/int128_native_impl.h +94 -0
  80. package/vendor/secp256k1/src/int128_struct.h +14 -0
  81. package/vendor/secp256k1/src/int128_struct_impl.h +205 -0
  82. package/vendor/secp256k1/src/modinv32.h +43 -0
  83. package/vendor/secp256k1/src/modinv32_impl.h +725 -0
  84. package/vendor/secp256k1/src/modinv64.h +47 -0
  85. package/vendor/secp256k1/src/modinv64_impl.h +780 -0
  86. package/vendor/secp256k1/src/modules/extrakeys/main_impl.h +292 -0
  87. package/vendor/secp256k1/src/modules/schnorrsig/main_impl.h +256 -0
  88. package/vendor/secp256k1/src/precomputed_ecmult.c +16456 -0
  89. package/vendor/secp256k1/src/precomputed_ecmult.h +40 -0
  90. package/vendor/secp256k1/src/precomputed_ecmult_gen.c +1779 -0
  91. package/vendor/secp256k1/src/precomputed_ecmult_gen.h +28 -0
  92. package/vendor/secp256k1/src/scalar.h +105 -0
  93. package/vendor/secp256k1/src/scalar_4x64.h +19 -0
  94. package/vendor/secp256k1/src/scalar_4x64_impl.h +1005 -0
  95. package/vendor/secp256k1/src/scalar_8x32.h +19 -0
  96. package/vendor/secp256k1/src/scalar_8x32_impl.h +820 -0
  97. package/vendor/secp256k1/src/scalar_impl.h +321 -0
  98. package/vendor/secp256k1/src/scalar_low.h +24 -0
  99. package/vendor/secp256k1/src/scalar_low_impl.h +217 -0
  100. package/vendor/secp256k1/src/scratch.h +44 -0
  101. package/vendor/secp256k1/src/scratch_impl.h +99 -0
  102. package/vendor/secp256k1/src/secp256k1.c +855 -0
  103. package/vendor/secp256k1/src/selftest.h +41 -0
  104. package/vendor/secp256k1/src/util.h +479 -0
  105. package/vendor/secp256k1/src/util_local_visibility.h +12 -0
  106. package/vendor/secp256k1/vendored.json +6 -0
@@ -0,0 +1,931 @@
1
+ #ifndef SECP256K1_H
2
+ #define SECP256K1_H
3
+
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+
8
+ #include <stddef.h>
9
+ #include <stdint.h>
10
+
11
+ /** Unless explicitly stated all pointer arguments must not be NULL.
12
+ *
13
+ * The following rules specify the order of arguments in API calls:
14
+ *
15
+ * 1. Context pointers go first, followed by output arguments, combined
16
+ * output/input arguments, and finally input-only arguments.
17
+ * 2. Array lengths always immediately follow the argument whose length
18
+ * they describe, even if this violates rule 1.
19
+ * 3. Within the OUT/OUTIN/IN groups, pointers to data that is typically generated
20
+ * later go first. This means: signatures, public nonces, secret nonces,
21
+ * messages, public keys, secret keys, tweaks.
22
+ * 4. Arguments that are not data pointers go last, from more complex to less
23
+ * complex: function pointers, algorithm names, messages, void pointers,
24
+ * counts, flags, booleans.
25
+ * 5. Opaque data pointers follow the function pointer they are to be passed to.
26
+ */
27
+
28
+ /** Opaque data structure that holds context information
29
+ *
30
+ * The primary purpose of context objects is to store randomization data for
31
+ * enhanced protection against side-channel leakage. This protection is only
32
+ * effective if the context is randomized after its creation. See
33
+ * nostr_native_secp256k1_context_create for creation of contexts and
34
+ * nostr_native_secp256k1_context_randomize for randomization.
35
+ *
36
+ * A secondary purpose of context objects is to store pointers to callback
37
+ * functions that the library will call when certain error states arise. See
38
+ * nostr_native_secp256k1_context_set_error_callback as well as
39
+ * nostr_native_secp256k1_context_set_illegal_callback for details. Future library versions
40
+ * may use context objects for additional purposes.
41
+ *
42
+ * A constructed context can safely be used from multiple threads
43
+ * simultaneously, but API calls that take a non-const pointer to a context
44
+ * need exclusive access to it. In particular this is the case for
45
+ * nostr_native_secp256k1_context_destroy, nostr_native_secp256k1_context_preallocated_destroy,
46
+ * and nostr_native_secp256k1_context_randomize.
47
+ *
48
+ * Regarding randomization, either do it once at creation time (in which case
49
+ * you do not need any locking for the other calls), or use a read-write lock.
50
+ */
51
+ typedef struct nostr_native_secp256k1_context_struct nostr_native_secp256k1_context;
52
+
53
+ /** Opaque data structure that holds a parsed and valid public key.
54
+ *
55
+ * The exact representation of data inside is implementation defined and not
56
+ * guaranteed to be portable between different platforms or versions. It is
57
+ * however guaranteed to be 64 bytes in size, and can be safely copied/moved.
58
+ * If you need to convert to a format suitable for storage or transmission,
59
+ * use nostr_native_secp256k1_ec_pubkey_serialize and nostr_native_secp256k1_ec_pubkey_parse. To
60
+ * compare keys, use nostr_native_secp256k1_ec_pubkey_cmp.
61
+ */
62
+ typedef struct nostr_native_secp256k1_pubkey {
63
+ unsigned char data[64];
64
+ } nostr_native_secp256k1_pubkey;
65
+
66
+ /** Opaque data structure that holds a parsed ECDSA signature.
67
+ *
68
+ * The exact representation of data inside is implementation defined and not
69
+ * guaranteed to be portable between different platforms or versions. It is
70
+ * however guaranteed to be 64 bytes in size, and can be safely copied/moved.
71
+ * If you need to convert to a format suitable for storage, transmission, or
72
+ * comparison, use the nostr_native_secp256k1_ecdsa_signature_serialize_* and
73
+ * nostr_native_secp256k1_ecdsa_signature_parse_* functions.
74
+ */
75
+ typedef struct nostr_native_secp256k1_ecdsa_signature {
76
+ unsigned char data[64];
77
+ } nostr_native_secp256k1_ecdsa_signature;
78
+
79
+ /** A pointer to a function to deterministically generate a nonce.
80
+ *
81
+ * Returns: 1 if a nonce was successfully generated. 0 will cause signing to fail.
82
+ * Out: nonce32: pointer to a 32-byte array to be filled by the function.
83
+ * In: msg32: the 32-byte message hash being verified (will not be NULL)
84
+ * key32: pointer to a 32-byte secret key (will not be NULL)
85
+ * algo16: pointer to a 16-byte array describing the signature
86
+ * algorithm (will be NULL for ECDSA for compatibility).
87
+ * data: Arbitrary data pointer that is passed through.
88
+ * attempt: how many iterations we have tried to find a nonce.
89
+ * This will almost always be 0, but different attempt values
90
+ * are required to result in a different nonce.
91
+ *
92
+ * Except for test cases, this function should compute some cryptographic hash of
93
+ * the message, the algorithm, the key and the attempt.
94
+ */
95
+ typedef int (*nostr_native_secp256k1_nonce_function)(
96
+ unsigned char *nonce32,
97
+ const unsigned char *msg32,
98
+ const unsigned char *key32,
99
+ const unsigned char *algo16,
100
+ void *data,
101
+ unsigned int attempt
102
+ );
103
+
104
+ /* When this header is used at build-time the SECP256K1_BUILD define needs to be set
105
+ * to correctly setup export attributes and nullness checks. This is normally done
106
+ * by secp256k1.c but to guard against this header being included before secp256k1.c
107
+ * has had a chance to set the define (e.g. via test harnesses that just includes
108
+ * secp256k1.c) we set SECP256K1_NO_BUILD when this header is processed without the
109
+ * BUILD define so this condition can be caught.
110
+ */
111
+ #ifndef SECP256K1_BUILD
112
+ # define SECP256K1_NO_BUILD
113
+ #endif
114
+
115
+ /* Symbol visibility. */
116
+ #if !defined(SECP256K1_API) && defined(SECP256K1_NO_API_VISIBILITY_ATTRIBUTES)
117
+ /* The user has requested that we don't specify visibility attributes in
118
+ * the public API.
119
+ *
120
+ * Since all our non-API declarations use the static qualifier, this means
121
+ * that the user can use -fvisibility=<value> to set the visibility of the
122
+ * API symbols. For instance, -fvisibility=hidden can be useful *even for
123
+ * the API symbols*, e.g., when building a static library which is linked
124
+ * into a shared library, and the latter should not re-export the
125
+ * libsecp256k1 API.
126
+ *
127
+ * While visibility is a concept that applies only to shared libraries,
128
+ * setting visibility will still make a difference when building a static
129
+ * library: the visibility settings will be stored in the static library,
130
+ * solely for the potential case that the static library will be linked into
131
+ * a shared library. In that case, the stored visibility settings will
132
+ * resurface and be honored for the shared library. */
133
+ # define SECP256K1_API extern
134
+ #endif
135
+ #if !defined(SECP256K1_API)
136
+ # if defined(SECP256K1_BUILD)
137
+ /* On Windows, assume a shared library only if explicitly requested.
138
+ * 1. If using Libtool, it defines DLL_EXPORT automatically.
139
+ * 2. In other cases, SECP256K1_DLL_EXPORT must be defined. */
140
+ # if defined(_WIN32) && (defined(SECP256K1_DLL_EXPORT) || defined(DLL_EXPORT))
141
+ /* GCC for Windows (e.g., MinGW) accepts the __declspec syntax for
142
+ * MSVC compatibility. A __declspec declaration implies (but is not
143
+ * exactly equivalent to) __attribute__ ((visibility("default"))),
144
+ * and so we actually want __declspec even on GCC, see "Microsoft
145
+ * Windows Function Attributes" in the GCC manual and the
146
+ * recommendations in https://gcc.gnu.org/wiki/Visibility . */
147
+ # define SECP256K1_API extern __declspec(dllexport)
148
+ /* Avoid __attribute__ ((visibility("default"))) on Windows to get rid
149
+ * of warnings when compiling with -flto due to a bug in GCC, see
150
+ * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116478 . */
151
+ # elif !defined(_WIN32) && defined (__GNUC__) && (__GNUC__ >= 4)
152
+ # define SECP256K1_API extern __attribute__ ((visibility("default")))
153
+ # else
154
+ # define SECP256K1_API extern
155
+ # endif
156
+ # else
157
+ /* On Windows, SECP256K1_STATIC must be defined when consuming
158
+ * libsecp256k1 as a static library. Note that SECP256K1_STATIC is a
159
+ * "consumer-only" macro, and it has no meaning when building
160
+ * libsecp256k1. */
161
+ # if defined(_WIN32) && !defined(SECP256K1_STATIC)
162
+ # define SECP256K1_API extern __declspec(dllimport)
163
+ # else
164
+ # define SECP256K1_API extern
165
+ # endif
166
+ # endif
167
+ #endif
168
+
169
+ /* Warning attributes
170
+ * NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out
171
+ * some paranoid null checks. */
172
+ # if defined(__GNUC__)
173
+ # define SECP256K1_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
174
+ # else
175
+ # define SECP256K1_WARN_UNUSED_RESULT
176
+ # endif
177
+ # if !defined(SECP256K1_BUILD) && defined(__GNUC__)
178
+ # define SECP256K1_ARG_NONNULL(_x) __attribute__ ((__nonnull__(_x)))
179
+ # else
180
+ # define SECP256K1_ARG_NONNULL(_x)
181
+ # endif
182
+
183
+ /* Attribute for marking functions, types, and variables as deprecated */
184
+ #if !defined(SECP256K1_BUILD) && defined(__has_attribute)
185
+ # if __has_attribute(__deprecated__)
186
+ # define SECP256K1_DEPRECATED(_msg) __attribute__ ((__deprecated__(_msg)))
187
+ # else
188
+ # define SECP256K1_DEPRECATED(_msg)
189
+ # endif
190
+ #else
191
+ # define SECP256K1_DEPRECATED(_msg)
192
+ #endif
193
+
194
+ /* All flags' lower 8 bits indicate what they're for. Do not use directly. */
195
+ #define SECP256K1_FLAGS_TYPE_MASK ((1 << 8) - 1)
196
+ #define SECP256K1_FLAGS_TYPE_CONTEXT (1 << 0)
197
+ #define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)
198
+ /* The higher bits contain the actual data. Do not use directly. */
199
+ #define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY (1 << 8)
200
+ #define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)
201
+ #define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY (1 << 10)
202
+ #define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
203
+
204
+ /** Context flags to pass to nostr_native_secp256k1_context_create, nostr_native_secp256k1_context_preallocated_size, and
205
+ * nostr_native_secp256k1_context_preallocated_create. */
206
+ #define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
207
+
208
+ /** Deprecated context flags. These flags are treated equivalent to SECP256K1_CONTEXT_NONE. */
209
+ #define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
210
+ #define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
211
+
212
+ /* Testing flag. Do not use. */
213
+ #define SECP256K1_CONTEXT_DECLASSIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY)
214
+
215
+ /** Flag to pass to nostr_native_secp256k1_ec_pubkey_serialize. */
216
+ #define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
217
+ #define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION)
218
+
219
+ /** Prefix byte used to tag various encoded curvepoints for specific purposes */
220
+ #define SECP256K1_TAG_PUBKEY_EVEN 0x02
221
+ #define SECP256K1_TAG_PUBKEY_ODD 0x03
222
+ #define SECP256K1_TAG_PUBKEY_UNCOMPRESSED 0x04
223
+ #define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06
224
+ #define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07
225
+
226
+ /** A built-in constant secp256k1 context object with static storage duration, to be
227
+ * used in conjunction with nostr_native_secp256k1_selftest.
228
+ *
229
+ * This context object offers *only limited functionality* , i.e., it cannot be used
230
+ * for API functions that perform computations involving secret keys, e.g., signing
231
+ * and public key generation. If this restriction applies to a specific API function,
232
+ * it is mentioned in its documentation. See nostr_native_secp256k1_context_create if you need a
233
+ * full context object that supports all functionality offered by the library.
234
+ *
235
+ * It is highly recommended to call nostr_native_secp256k1_selftest before using this context.
236
+ */
237
+ SECP256K1_API const nostr_native_secp256k1_context * const nostr_native_secp256k1_context_static;
238
+
239
+ /** Perform basic self tests (to be used in conjunction with nostr_native_secp256k1_context_static)
240
+ *
241
+ * This function performs self tests that detect some serious usage errors and
242
+ * similar conditions, e.g., when the library is compiled for the wrong endianness.
243
+ * This is a last resort measure to be used in production. The performed tests are
244
+ * very rudimentary and are not intended as a replacement for running the test
245
+ * binaries.
246
+ *
247
+ * It is highly recommended to call this before using nostr_native_secp256k1_context_static.
248
+ * It is not necessary to call this function before using a context created with
249
+ * nostr_native_secp256k1_context_create (or nostr_native_secp256k1_context_preallocated_create), which will
250
+ * take care of performing the self tests.
251
+ *
252
+ * If the tests fail, this function will call the default error callback to abort the
253
+ * program (see nostr_native_secp256k1_context_set_error_callback).
254
+ */
255
+ SECP256K1_API void nostr_native_secp256k1_selftest(void);
256
+
257
+
258
+ /** Create a secp256k1 context object (in dynamically allocated memory).
259
+ *
260
+ * This function uses malloc to allocate memory. It is guaranteed that malloc is
261
+ * called at most once for every call of this function. If you need to avoid dynamic
262
+ * memory allocation entirely, see nostr_native_secp256k1_context_static and the functions in
263
+ * nostr_native_secp256k1_preallocated.h.
264
+ *
265
+ * Returns: pointer to a newly created context object.
266
+ * In: flags: Always set to SECP256K1_CONTEXT_NONE (see below).
267
+ *
268
+ * The only valid non-deprecated flag in recent library versions is
269
+ * SECP256K1_CONTEXT_NONE, which will create a context sufficient for all functionality
270
+ * offered by the library. All other (deprecated) flags will be treated as equivalent
271
+ * to the SECP256K1_CONTEXT_NONE flag. Though the flags parameter primarily exists for
272
+ * historical reasons, future versions of the library may introduce new flags.
273
+ *
274
+ * If the context is intended to be used for API functions that perform computations
275
+ * involving secret keys, e.g., signing and public key generation, then it is highly
276
+ * recommended to call nostr_native_secp256k1_context_randomize on the context before calling
277
+ * those API functions. This will provide enhanced protection against side-channel
278
+ * leakage, see nostr_native_secp256k1_context_randomize for details.
279
+ *
280
+ * Do not create a new context object for each operation, as construction and
281
+ * randomization can take non-negligible time.
282
+ */
283
+ SECP256K1_API nostr_native_secp256k1_context *nostr_native_secp256k1_context_create(
284
+ unsigned int flags
285
+ ) SECP256K1_WARN_UNUSED_RESULT;
286
+
287
+ /** Copy a secp256k1 context object (into dynamically allocated memory).
288
+ *
289
+ * This function uses malloc to allocate memory. It is guaranteed that malloc is
290
+ * called at most once for every call of this function. If you need to avoid dynamic
291
+ * memory allocation entirely, see the functions in nostr_native_secp256k1_preallocated.h.
292
+ *
293
+ * Cloning nostr_native_secp256k1_context_static is not possible, and should not be emulated by
294
+ * the caller (e.g., using memcpy). Create a new context instead.
295
+ *
296
+ * Returns: pointer to a newly created context object.
297
+ * Args: ctx: pointer to a context to copy (not nostr_native_secp256k1_context_static).
298
+ */
299
+ SECP256K1_API nostr_native_secp256k1_context *nostr_native_secp256k1_context_clone(
300
+ const nostr_native_secp256k1_context *ctx
301
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
302
+
303
+ /** Destroy a secp256k1 context object (created in dynamically allocated memory).
304
+ *
305
+ * The context pointer may not be used afterwards.
306
+ *
307
+ * The context to destroy must have been created using nostr_native_secp256k1_context_create
308
+ * or nostr_native_secp256k1_context_clone. If the context has instead been created using
309
+ * nostr_native_secp256k1_context_preallocated_create or nostr_native_secp256k1_context_preallocated_clone, the
310
+ * behaviour is undefined. In that case, nostr_native_secp256k1_context_preallocated_destroy must
311
+ * be used instead.
312
+ *
313
+ * Args: ctx: pointer to a context to destroy, constructed using
314
+ * nostr_native_secp256k1_context_create or nostr_native_secp256k1_context_clone
315
+ * (i.e., not nostr_native_secp256k1_context_static).
316
+ */
317
+ SECP256K1_API void nostr_native_secp256k1_context_destroy(
318
+ nostr_native_secp256k1_context *ctx
319
+ ) SECP256K1_ARG_NONNULL(1);
320
+
321
+ /** Set a callback function to be called when an illegal argument is passed to
322
+ * an API call. It will only trigger for violations that are mentioned
323
+ * explicitly in the header.
324
+ *
325
+ * The philosophy is that these shouldn't be dealt with through a specific
326
+ * return value, as calling code should not have branches to deal with the case
327
+ * that this code itself is broken.
328
+ *
329
+ * On the other hand, during debug stage, one would want to be informed about
330
+ * such mistakes, and the default (crashing) may be inadvisable. Should this
331
+ * callback return instead of crashing, the return value and output arguments
332
+ * of the API function call are undefined. Moreover, the same API call may
333
+ * trigger the callback again in this case.
334
+ *
335
+ * When this function has not been called (or called with fun==NULL), then the
336
+ * default callback will be used. The library provides a default callback which
337
+ * writes the message to stderr and calls abort. This default callback can be
338
+ * replaced at link time if the preprocessor macro
339
+ * USE_EXTERNAL_DEFAULT_CALLBACKS is defined, which is the case if the build
340
+ * has been configured with --enable-external-default-callbacks (GNU Autotools) or
341
+ * -DSECP256K1_USE_EXTERNAL_DEFAULT_CALLBACKS=ON (CMake). Then the
342
+ * following two symbols must be provided to link against:
343
+ * - void nostr_native_secp256k1_default_illegal_callback_fn(const char *message, void *data);
344
+ * - void nostr_native_secp256k1_default_error_callback_fn(const char *message, void *data);
345
+ * The library may call a default callback even before a proper callback data
346
+ * pointer could have been set using nostr_native_secp256k1_context_set_illegal_callback or
347
+ * nostr_native_secp256k1_context_set_error_callback, e.g., when the creation of a context
348
+ * fails. In this case, the corresponding default callback will be called with
349
+ * the data pointer argument set to NULL.
350
+ *
351
+ * Args: ctx: pointer to a context object.
352
+ * In: fun: pointer to a function to call when an illegal argument is
353
+ * passed to the API, taking a message and an opaque pointer.
354
+ * (NULL restores the default callback.)
355
+ * data: the opaque pointer to pass to fun above, must be NULL for the
356
+ * default callback.
357
+ *
358
+ * See also nostr_native_secp256k1_context_set_error_callback.
359
+ */
360
+ SECP256K1_API void nostr_native_secp256k1_context_set_illegal_callback(
361
+ nostr_native_secp256k1_context *ctx,
362
+ void (*fun)(const char *message, void *data),
363
+ const void *data
364
+ ) SECP256K1_ARG_NONNULL(1);
365
+
366
+ /** Set a callback function to be called when an internal consistency check
367
+ * fails.
368
+ *
369
+ * The default callback writes an error message to stderr and calls abort
370
+ * to abort the program.
371
+ *
372
+ * This can only trigger in case of a hardware failure, miscompilation,
373
+ * memory corruption, serious bug in the library, or other error that would
374
+ * result in undefined behaviour. It will not trigger due to mere
375
+ * incorrect usage of the API (see nostr_native_secp256k1_context_set_illegal_callback
376
+ * for that). After this callback returns, anything may happen, including
377
+ * crashing.
378
+ *
379
+ * Args: ctx: pointer to a context object.
380
+ * In: fun: pointer to a function to call when an internal error occurs,
381
+ * taking a message and an opaque pointer (NULL restores the
382
+ * default callback, see nostr_native_secp256k1_context_set_illegal_callback
383
+ * for details).
384
+ * data: the opaque pointer to pass to fun above, must be NULL for the
385
+ * default callback.
386
+ *
387
+ * See also nostr_native_secp256k1_context_set_illegal_callback.
388
+ */
389
+ SECP256K1_API void nostr_native_secp256k1_context_set_error_callback(
390
+ nostr_native_secp256k1_context *ctx,
391
+ void (*fun)(const char *message, void *data),
392
+ const void *data
393
+ ) SECP256K1_ARG_NONNULL(1);
394
+
395
+ /** A pointer to a function implementing SHA256's internal compression function.
396
+ *
397
+ * This function processes one or more contiguous 64-byte message blocks and
398
+ * updates the internal SHA256 state accordingly. The function is not responsible
399
+ * for counting consumed blocks or bytes, nor for performing padding.
400
+ *
401
+ * In/Out: state: pointer to eight 32-bit words representing the current internal state;
402
+ * the state is updated in place.
403
+ * In: blocks64: pointer to concatenation of n_blocks blocks, of 64 bytes each.
404
+ * no alignment guarantees are made for this pointer.
405
+ * n_blocks: number of contiguous 64-byte blocks to process.
406
+ */
407
+ typedef void (*nostr_native_secp256k1_sha256_compression_function)(
408
+ uint32_t *state,
409
+ const unsigned char *blocks64,
410
+ size_t n_blocks
411
+ );
412
+
413
+ /**
414
+ * Set a callback function to override the internal SHA256 compression function.
415
+ *
416
+ * This installs a callback to replace the built-in block-compression
417
+ * step used by the library's internal SHA256 implementation.
418
+ * The provided callback must exactly implement the effect of n_blocks
419
+ * repeated applications of the SHA256 compression function.
420
+ *
421
+ * This API exists to support environments that wish to route the
422
+ * SHA256 compression step through a hardware-accelerated or otherwise
423
+ * specialized implementation. It is NOT meant for replacing SHA256
424
+ * with a different hash function.
425
+ *
426
+ * Since auxiliary functions exposed by the library via a function
427
+ * pointer such as nostr_native_secp256k1_nonce_function_default do not take a
428
+ * context object, they will not use the callback when called directly
429
+ * from user code. (But they will use the callback when called from
430
+ * other library functions that do take a context object, e.g., when
431
+ * noncefp==NULL or noncefp==nostr_native_secp256k1_nonce_function_default is passed
432
+ * as an argument to nostr_native_secp256k1_ecdsa_sign.)
433
+ *
434
+ * Note: The provided function is tested against a set of known SHA256
435
+ * digests; invokes the context's illegal callback on any mismatch
436
+ * (which aborts by default), in order to catch basic misbehavior early.
437
+ * It takes well under 2.5 ms on a desktop machine.
438
+ * This is NOT a substitute for having proper test coverage of the
439
+ * supplied function outside this library.
440
+ *
441
+ * Args: ctx: pointer to a context object.
442
+ * In: fn_compression: pointer to a function implementing the compression function;
443
+ * passing NULL restores the default implementation.
444
+ */
445
+ SECP256K1_API void nostr_native_secp256k1_context_set_sha256_compression(
446
+ nostr_native_secp256k1_context *ctx,
447
+ nostr_native_secp256k1_sha256_compression_function fn_compression
448
+ ) SECP256K1_ARG_NONNULL(1);
449
+
450
+ /** Parse a variable-length public key into the pubkey object.
451
+ *
452
+ * Returns: 1 if the public key was fully valid.
453
+ * 0 if the public key could not be parsed or is invalid.
454
+ * Args: ctx: pointer to a context object.
455
+ * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a
456
+ * parsed version of input. If not, its value is undefined.
457
+ * In: input: pointer to a serialized public key
458
+ * inputlen: length of the array pointed to by input
459
+ *
460
+ * This function supports parsing compressed (33 bytes, header byte 0x02 or
461
+ * 0x03), uncompressed (65 bytes, header byte 0x04), or hybrid (65 bytes, header
462
+ * byte 0x06 or 0x07) format public keys.
463
+ */
464
+ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ec_pubkey_parse(
465
+ const nostr_native_secp256k1_context *ctx,
466
+ nostr_native_secp256k1_pubkey *pubkey,
467
+ const unsigned char *input,
468
+ size_t inputlen
469
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
470
+
471
+ /** Serialize a pubkey object into a serialized byte sequence.
472
+ *
473
+ * Returns: 1 always.
474
+ * Args: ctx: pointer to a context object.
475
+ * Out: output: pointer to a 65-byte (if compressed==0) or 33-byte (if
476
+ * compressed==1) byte array to place the serialized key
477
+ * in.
478
+ * In/Out: outputlen: pointer to an integer which is initially set to the
479
+ * size of output, and is overwritten with the written
480
+ * size.
481
+ * In: pubkey: pointer to a nostr_native_secp256k1_pubkey containing an
482
+ * initialized public key.
483
+ * flags: SECP256K1_EC_COMPRESSED if serialization should be in
484
+ * compressed format, otherwise SECP256K1_EC_UNCOMPRESSED.
485
+ */
486
+ SECP256K1_API int nostr_native_secp256k1_ec_pubkey_serialize(
487
+ const nostr_native_secp256k1_context *ctx,
488
+ unsigned char *output,
489
+ size_t *outputlen,
490
+ const nostr_native_secp256k1_pubkey *pubkey,
491
+ unsigned int flags
492
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
493
+
494
+ /** Compare two public keys using lexicographic (of compressed serialization) order
495
+ *
496
+ * Returns: <0 if the first public key is less than the second
497
+ * >0 if the first public key is greater than the second
498
+ * 0 if the two public keys are equal
499
+ * Args: ctx: pointer to a context object
500
+ * In: pubkey1: first public key to compare
501
+ * pubkey2: second public key to compare
502
+ */
503
+ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ec_pubkey_cmp(
504
+ const nostr_native_secp256k1_context *ctx,
505
+ const nostr_native_secp256k1_pubkey *pubkey1,
506
+ const nostr_native_secp256k1_pubkey *pubkey2
507
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
508
+
509
+ /** Sort public keys using lexicographic (of compressed serialization) order
510
+ *
511
+ * Returns: 0 if the arguments are invalid. 1 otherwise.
512
+ *
513
+ * Args: ctx: pointer to a context object
514
+ * In: pubkeys: array of pointers to pubkeys to sort
515
+ * n_pubkeys: number of elements in the pubkeys array
516
+ */
517
+ SECP256K1_API int nostr_native_secp256k1_ec_pubkey_sort(
518
+ const nostr_native_secp256k1_context *ctx,
519
+ const nostr_native_secp256k1_pubkey **pubkeys,
520
+ size_t n_pubkeys
521
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
522
+
523
+ /** Parse an ECDSA signature in compact (64 bytes) format.
524
+ *
525
+ * Returns: 1 when the signature could be parsed, 0 otherwise.
526
+ * Args: ctx: pointer to a context object
527
+ * Out: sig: pointer to a signature object
528
+ * In: input64: pointer to the 64-byte array to parse
529
+ *
530
+ * The signature must consist of a 32-byte big endian R value, followed by a
531
+ * 32-byte big endian S value. If R or S fall outside of [0..order-1], the
532
+ * encoding is invalid. R and S with value 0 are allowed in the encoding.
533
+ *
534
+ * After the call, sig will always be initialized. If parsing failed or R or
535
+ * S are zero, the resulting sig value is guaranteed to fail verification for
536
+ * any message and public key.
537
+ */
538
+ SECP256K1_API int nostr_native_secp256k1_ecdsa_signature_parse_compact(
539
+ const nostr_native_secp256k1_context *ctx,
540
+ nostr_native_secp256k1_ecdsa_signature *sig,
541
+ const unsigned char *input64
542
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
543
+
544
+ /** Parse a DER ECDSA signature.
545
+ *
546
+ * Returns: 1 when the signature could be parsed, 0 otherwise.
547
+ * Args: ctx: pointer to a context object
548
+ * Out: sig: pointer to a signature object
549
+ * In: input: pointer to the signature to be parsed
550
+ * inputlen: the length of the array pointed to be input
551
+ *
552
+ * This function will accept any valid DER encoded signature, even if the
553
+ * encoded numbers are out of range.
554
+ *
555
+ * After the call, sig will always be initialized. If parsing failed or the
556
+ * encoded numbers are out of range, signature verification with it is
557
+ * guaranteed to fail for every message and public key.
558
+ */
559
+ SECP256K1_API int nostr_native_secp256k1_ecdsa_signature_parse_der(
560
+ const nostr_native_secp256k1_context *ctx,
561
+ nostr_native_secp256k1_ecdsa_signature *sig,
562
+ const unsigned char *input,
563
+ size_t inputlen
564
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
565
+
566
+ /** Serialize an ECDSA signature in DER format.
567
+ *
568
+ * Returns: 1 if enough space was available to serialize, 0 otherwise
569
+ * Args: ctx: pointer to a context object
570
+ * Out: output: pointer to an array to store the DER serialization
571
+ * In/Out: outputlen: pointer to a length integer. Initially, this integer
572
+ * should be set to the length of output. After the call
573
+ * it will be set to the length of the serialization (even
574
+ * if 0 was returned).
575
+ * In: sig: pointer to an initialized signature object
576
+ */
577
+ SECP256K1_API int nostr_native_secp256k1_ecdsa_signature_serialize_der(
578
+ const nostr_native_secp256k1_context *ctx,
579
+ unsigned char *output,
580
+ size_t *outputlen,
581
+ const nostr_native_secp256k1_ecdsa_signature *sig
582
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
583
+
584
+ /** Serialize an ECDSA signature in compact (64 byte) format.
585
+ *
586
+ * Returns: 1
587
+ * Args: ctx: pointer to a context object
588
+ * Out: output64: pointer to a 64-byte array to store the compact serialization
589
+ * In: sig: pointer to an initialized signature object
590
+ *
591
+ * See nostr_native_secp256k1_ecdsa_signature_parse_compact for details about the encoding.
592
+ */
593
+ SECP256K1_API int nostr_native_secp256k1_ecdsa_signature_serialize_compact(
594
+ const nostr_native_secp256k1_context *ctx,
595
+ unsigned char *output64,
596
+ const nostr_native_secp256k1_ecdsa_signature *sig
597
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
598
+
599
+ /** Verify an ECDSA signature.
600
+ *
601
+ * Returns: 1: correct signature
602
+ * 0: incorrect or unparseable signature
603
+ * Args: ctx: pointer to a context object
604
+ * In: sig: the signature being verified.
605
+ * msghash32: the 32-byte message hash being verified.
606
+ * The verifier must make sure to apply a cryptographic
607
+ * hash function to the message by itself and not accept an
608
+ * msghash32 value directly. Otherwise, it would be easy to
609
+ * create a "valid" signature without knowledge of the
610
+ * secret key. See also
611
+ * https://bitcoin.stackexchange.com/a/81116/35586 for more
612
+ * background on this topic.
613
+ * pubkey: pointer to an initialized public key to verify with.
614
+ *
615
+ * To avoid accepting malleable signatures, only ECDSA signatures in lower-S
616
+ * form are accepted.
617
+ *
618
+ * If you need to accept ECDSA signatures from sources that do not obey this
619
+ * rule, apply nostr_native_secp256k1_ecdsa_signature_normalize to the signature prior to
620
+ * verification, but be aware that doing so results in malleable signatures.
621
+ *
622
+ * For details, see the comments for that function.
623
+ */
624
+ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ecdsa_verify(
625
+ const nostr_native_secp256k1_context *ctx,
626
+ const nostr_native_secp256k1_ecdsa_signature *sig,
627
+ const unsigned char *msghash32,
628
+ const nostr_native_secp256k1_pubkey *pubkey
629
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
630
+
631
+ /** Convert a signature to a normalized lower-S form.
632
+ *
633
+ * Returns: 1 if sigin was not normalized, 0 if it already was.
634
+ * Args: ctx: pointer to a context object
635
+ * Out: sigout: pointer to a signature to fill with the normalized form,
636
+ * or copy if the input was already normalized. (can be NULL if
637
+ * you're only interested in whether the input was already
638
+ * normalized).
639
+ * In: sigin: pointer to a signature to check/normalize (can be identical to sigout)
640
+ *
641
+ * With ECDSA a third-party can forge a second distinct signature of the same
642
+ * message, given a single initial signature, but without knowing the key. This
643
+ * is done by negating the S value modulo the order of the curve, 'flipping'
644
+ * the sign of the random point R which is not included in the signature.
645
+ *
646
+ * Forgery of the same message isn't universally problematic, but in systems
647
+ * where message malleability or uniqueness of signatures is important this can
648
+ * cause issues. This forgery can be blocked by all verifiers forcing signers
649
+ * to use a normalized form.
650
+ *
651
+ * The lower-S form reduces the size of signatures slightly on average when
652
+ * variable length encodings (such as DER) are used and is cheap to verify,
653
+ * making it a good choice. Security of always using lower-S is assured because
654
+ * anyone can trivially modify a signature after the fact to enforce this
655
+ * property anyway.
656
+ *
657
+ * The lower S value is always between 0x1 and
658
+ * 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
659
+ * inclusive.
660
+ *
661
+ * No other forms of ECDSA malleability are known and none seem likely, but
662
+ * there is no formal proof that ECDSA, even with this additional restriction,
663
+ * is free of other malleability. Commonly used serialization schemes will also
664
+ * accept various non-unique encodings, so care should be taken when this
665
+ * property is required for an application.
666
+ *
667
+ * The nostr_native_secp256k1_ecdsa_sign function will by default create signatures in the
668
+ * lower-S form, and nostr_native_secp256k1_ecdsa_verify will not accept others. In case
669
+ * signatures come from a system that cannot enforce this property,
670
+ * nostr_native_secp256k1_ecdsa_signature_normalize must be called before verification.
671
+ */
672
+ SECP256K1_API int nostr_native_secp256k1_ecdsa_signature_normalize(
673
+ const nostr_native_secp256k1_context *ctx,
674
+ nostr_native_secp256k1_ecdsa_signature *sigout,
675
+ const nostr_native_secp256k1_ecdsa_signature *sigin
676
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3);
677
+
678
+ /** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
679
+ * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
680
+ * extra entropy.
681
+ */
682
+ SECP256K1_API const nostr_native_secp256k1_nonce_function nostr_native_secp256k1_nonce_function_rfc6979;
683
+
684
+ /** A default safe nonce generation function (currently equal to nostr_native_secp256k1_nonce_function_rfc6979). */
685
+ SECP256K1_API const nostr_native_secp256k1_nonce_function nostr_native_secp256k1_nonce_function_default;
686
+
687
+ /** Create an ECDSA signature.
688
+ *
689
+ * Returns: 1: signature created
690
+ * 0: the nonce generation function failed, or the secret key was invalid.
691
+ * Args: ctx: pointer to a context object (not nostr_native_secp256k1_context_static).
692
+ * Out: sig: pointer to a signature object.
693
+ * In: msghash32: the 32-byte message hash being signed.
694
+ * seckey: pointer to a 32-byte secret key.
695
+ * noncefp: pointer to a nonce generation function. If NULL,
696
+ * nostr_native_secp256k1_nonce_function_default is used.
697
+ * ndata: pointer to arbitrary data used by the nonce generation function
698
+ * (can be NULL). If it is non-NULL and
699
+ * nostr_native_secp256k1_nonce_function_default is used, then ndata must be a
700
+ * pointer to 32-bytes of additional data.
701
+ *
702
+ * The created signature is always in lower-S form. See
703
+ * nostr_native_secp256k1_ecdsa_signature_normalize for more details.
704
+ */
705
+ SECP256K1_API int nostr_native_secp256k1_ecdsa_sign(
706
+ const nostr_native_secp256k1_context *ctx,
707
+ nostr_native_secp256k1_ecdsa_signature *sig,
708
+ const unsigned char *msghash32,
709
+ const unsigned char *seckey,
710
+ nostr_native_secp256k1_nonce_function noncefp,
711
+ const void *ndata
712
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
713
+
714
+ /** Verify an elliptic curve secret key.
715
+ *
716
+ * A secret key is valid if it is not 0 and less than the secp256k1 curve order
717
+ * when interpreted as an integer (most significant byte first). The
718
+ * probability of choosing a 32-byte string uniformly at random which is an
719
+ * invalid secret key is negligible. However, if it does happen it should
720
+ * be assumed that the randomness source is severely broken and there should
721
+ * be no retry.
722
+ *
723
+ * Returns: 1: secret key is valid
724
+ * 0: secret key is invalid
725
+ * Args: ctx: pointer to a context object.
726
+ * In: seckey: pointer to a 32-byte secret key.
727
+ */
728
+ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ec_seckey_verify(
729
+ const nostr_native_secp256k1_context *ctx,
730
+ const unsigned char *seckey
731
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
732
+
733
+ /** Compute the public key for a secret key.
734
+ *
735
+ * Returns: 1: secret was valid, public key stores.
736
+ * 0: secret was invalid, try again.
737
+ * Args: ctx: pointer to a context object (not nostr_native_secp256k1_context_static).
738
+ * Out: pubkey: pointer to the created public key.
739
+ * In: seckey: pointer to a 32-byte secret key.
740
+ */
741
+ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ec_pubkey_create(
742
+ const nostr_native_secp256k1_context *ctx,
743
+ nostr_native_secp256k1_pubkey *pubkey,
744
+ const unsigned char *seckey
745
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
746
+
747
+ /** Negates a secret key in place.
748
+ *
749
+ * Returns: 0 if the given secret key is invalid according to
750
+ * nostr_native_secp256k1_ec_seckey_verify. 1 otherwise
751
+ * Args: ctx: pointer to a context object
752
+ * In/Out: seckey: pointer to the 32-byte secret key to be negated. If the
753
+ * secret key is invalid according to
754
+ * nostr_native_secp256k1_ec_seckey_verify, this function returns 0 and
755
+ * seckey will be set to some unspecified value.
756
+ */
757
+ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ec_seckey_negate(
758
+ const nostr_native_secp256k1_context *ctx,
759
+ unsigned char *seckey
760
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
761
+
762
+ /** Negates a public key in place.
763
+ *
764
+ * Returns: 1 always
765
+ * Args: ctx: pointer to a context object
766
+ * In/Out: pubkey: pointer to the public key to be negated.
767
+ */
768
+ SECP256K1_API int nostr_native_secp256k1_ec_pubkey_negate(
769
+ const nostr_native_secp256k1_context *ctx,
770
+ nostr_native_secp256k1_pubkey *pubkey
771
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
772
+
773
+ /** Tweak a secret key by adding tweak to it.
774
+ *
775
+ * Returns: 0 if the arguments are invalid or the resulting secret key would be
776
+ * invalid (only when the tweak is the negation of the secret key). 1
777
+ * otherwise.
778
+ * Args: ctx: pointer to a context object.
779
+ * In/Out: seckey: pointer to a 32-byte secret key. If the secret key is
780
+ * invalid according to nostr_native_secp256k1_ec_seckey_verify, this
781
+ * function returns 0. seckey will be set to some unspecified
782
+ * value if this function returns 0.
783
+ * In: tweak32: pointer to a 32-byte tweak, which must be valid according to
784
+ * nostr_native_secp256k1_ec_seckey_verify or 32 zero bytes. For uniformly
785
+ * random 32-byte tweaks, the chance of being invalid is
786
+ * negligible (around 1 in 2^128).
787
+ */
788
+ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ec_seckey_tweak_add(
789
+ const nostr_native_secp256k1_context *ctx,
790
+ unsigned char *seckey,
791
+ const unsigned char *tweak32
792
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
793
+
794
+ /** Tweak a public key by adding tweak times the generator to it.
795
+ *
796
+ * Returns: 0 if the arguments are invalid or the resulting public key would be
797
+ * invalid (only when the tweak is the negation of the corresponding
798
+ * secret key). 1 otherwise.
799
+ * Args: ctx: pointer to a context object.
800
+ * In/Out: pubkey: pointer to a public key object. pubkey will be set to an
801
+ * invalid value if this function returns 0.
802
+ * In: tweak32: pointer to a 32-byte tweak, which must be valid according to
803
+ * nostr_native_secp256k1_ec_seckey_verify or 32 zero bytes. For uniformly
804
+ * random 32-byte tweaks, the chance of being invalid is
805
+ * negligible (around 1 in 2^128).
806
+ */
807
+ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ec_pubkey_tweak_add(
808
+ const nostr_native_secp256k1_context *ctx,
809
+ nostr_native_secp256k1_pubkey *pubkey,
810
+ const unsigned char *tweak32
811
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
812
+
813
+ /** Tweak a secret key by multiplying it by a tweak.
814
+ *
815
+ * Returns: 0 if the arguments are invalid. 1 otherwise.
816
+ * Args: ctx: pointer to a context object.
817
+ * In/Out: seckey: pointer to a 32-byte secret key. If the secret key is
818
+ * invalid according to nostr_native_secp256k1_ec_seckey_verify, this
819
+ * function returns 0. seckey will be set to some unspecified
820
+ * value if this function returns 0.
821
+ * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
822
+ * nostr_native_secp256k1_ec_seckey_verify, this function returns 0. For
823
+ * uniformly random 32-byte arrays the chance of being invalid
824
+ * is negligible (around 1 in 2^128).
825
+ */
826
+ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ec_seckey_tweak_mul(
827
+ const nostr_native_secp256k1_context *ctx,
828
+ unsigned char *seckey,
829
+ const unsigned char *tweak32
830
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
831
+
832
+ /** Tweak a public key by multiplying it by a tweak value.
833
+ *
834
+ * Returns: 0 if the arguments are invalid. 1 otherwise.
835
+ * Args: ctx: pointer to a context object.
836
+ * In/Out: pubkey: pointer to a public key object. pubkey will be set to an
837
+ * invalid value if this function returns 0.
838
+ * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
839
+ * nostr_native_secp256k1_ec_seckey_verify, this function returns 0. For
840
+ * uniformly random 32-byte arrays the chance of being invalid
841
+ * is negligible (around 1 in 2^128).
842
+ */
843
+ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ec_pubkey_tweak_mul(
844
+ const nostr_native_secp256k1_context *ctx,
845
+ nostr_native_secp256k1_pubkey *pubkey,
846
+ const unsigned char *tweak32
847
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
848
+
849
+ /** Randomizes the context to provide enhanced protection against side-channel leakage.
850
+ *
851
+ * Returns: 1: randomization successful
852
+ * 0: error
853
+ * Args: ctx: pointer to a context object (not nostr_native_secp256k1_context_static).
854
+ * In: seed32: pointer to a 32-byte random seed (NULL resets to initial state).
855
+ *
856
+ * While secp256k1 code is written and tested to be constant-time no matter what
857
+ * secret values are, it is possible that a compiler may output code which is not,
858
+ * and also that the CPU may not emit the same radio frequencies or draw the same
859
+ * amount of power for all values. Randomization of the context shields against
860
+ * side-channel observations which aim to exploit secret-dependent behaviour in
861
+ * certain computations which involve secret keys.
862
+ *
863
+ * It is highly recommended to call this function on contexts returned from
864
+ * nostr_native_secp256k1_context_create or nostr_native_secp256k1_context_clone (or from the corresponding
865
+ * functions in nostr_native_secp256k1_preallocated.h) before using these contexts to call API
866
+ * functions that perform computations involving secret keys, e.g., signing and
867
+ * public key generation. It is possible to call this function more than once on
868
+ * the same context, and doing so before every few computations involving secret
869
+ * keys is recommended as a defense-in-depth measure. Randomization of the static
870
+ * context nostr_native_secp256k1_context_static is not supported.
871
+ *
872
+ * Currently, the random seed is mainly used for blinding multiplications of a
873
+ * secret scalar with the elliptic curve base point. Multiplications of this
874
+ * kind are performed by exactly those API functions which are documented to
875
+ * require a context that is not nostr_native_secp256k1_context_static. As a rule of thumb,
876
+ * these are all functions which take a secret key (or a keypair) as an input.
877
+ * A notable exception to that rule is the ECDH module, which relies on a different
878
+ * kind of elliptic curve point multiplication and thus does not benefit from
879
+ * enhanced protection against side-channel leakage currently.
880
+ */
881
+ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_context_randomize(
882
+ nostr_native_secp256k1_context *ctx,
883
+ const unsigned char *seed32
884
+ ) SECP256K1_ARG_NONNULL(1);
885
+
886
+ /** Add a number of public keys together.
887
+ *
888
+ * Returns: 1: the sum of the public keys is valid.
889
+ * 0: the sum of the public keys is not valid.
890
+ * Args: ctx: pointer to a context object.
891
+ * Out: out: pointer to a public key object for placing the resulting public key.
892
+ * In: ins: pointer to array of pointers to public keys.
893
+ * n: the number of public keys to add together (must be at least 1).
894
+ */
895
+ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ec_pubkey_combine(
896
+ const nostr_native_secp256k1_context *ctx,
897
+ nostr_native_secp256k1_pubkey *out,
898
+ const nostr_native_secp256k1_pubkey * const *ins,
899
+ size_t n
900
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
901
+
902
+ /** Compute a tagged hash as defined in BIP-340.
903
+ *
904
+ * This is useful for creating a message hash and achieving domain separation
905
+ * through an application-specific tag. This function returns
906
+ * SHA256(SHA256(tag)||SHA256(tag)||msg). Therefore, tagged hash
907
+ * implementations optimized for a specific tag can precompute the SHA256 state
908
+ * after hashing the tag hashes.
909
+ *
910
+ * Returns: 1 always.
911
+ * Args: ctx: pointer to a context object
912
+ * Out: hash32: pointer to a 32-byte array to store the resulting hash
913
+ * In: tag: pointer to an array containing the tag
914
+ * taglen: length of the tag array
915
+ * msg: pointer to an array containing the message
916
+ * msglen: length of the message array
917
+ */
918
+ SECP256K1_API int nostr_native_secp256k1_tagged_sha256(
919
+ const nostr_native_secp256k1_context *ctx,
920
+ unsigned char *hash32,
921
+ const unsigned char *tag,
922
+ size_t taglen,
923
+ const unsigned char *msg,
924
+ size_t msglen
925
+ ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5);
926
+
927
+ #ifdef __cplusplus
928
+ }
929
+ #endif
930
+
931
+ #endif /* SECP256K1_H */