@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,855 @@
1
+ /***********************************************************************
2
+ * Copyright (c) 2013-2015 Pieter Wuille *
3
+ * Distributed under the MIT software license, see the accompanying *
4
+ * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5
+ ***********************************************************************/
6
+
7
+ /* This is a C project. It should not be compiled with a C++ compiler,
8
+ * and we error out if we detect one.
9
+ *
10
+ * We still want to be able to test the project with a C++ compiler
11
+ * because it is still good to know if this will lead to real trouble, so
12
+ * there is a possibility to override the check. But be warned that
13
+ * compiling with a C++ compiler is not supported. */
14
+ #if defined(__cplusplus) && !defined(SECP256K1_CPLUSPLUS_TEST_OVERRIDE)
15
+ #error Trying to compile a C project with a C++ compiler.
16
+ #endif
17
+
18
+ #define SECP256K1_BUILD
19
+
20
+ #include "../include/secp256k1.h"
21
+ #include "../include/secp256k1_preallocated.h"
22
+
23
+ #include "assumptions.h"
24
+ #include "checkmem.h"
25
+ #include "util.h"
26
+
27
+ #include "field_impl.h"
28
+ #include "scalar_impl.h"
29
+ #include "group_impl.h"
30
+ #include "ecmult_impl.h"
31
+ #include "ecmult_const_impl.h"
32
+ #include "ecmult_gen_impl.h"
33
+ #include "ecdsa_impl.h"
34
+ #include "eckey_impl.h"
35
+ #include "hash_impl.h"
36
+ #include "int128_impl.h"
37
+ #include "scratch_impl.h"
38
+ #include "selftest.h"
39
+ #include "hsort_impl.h"
40
+
41
+ #ifdef SECP256K1_NO_BUILD
42
+ # error "secp256k1.h processed without SECP256K1_BUILD defined while building secp256k1.c"
43
+ #endif
44
+
45
+ #define ARG_CHECK(cond) do { \
46
+ if (EXPECT(!(cond), 0)) { \
47
+ nostr_native_secp256k1_callback_call(&ctx->illegal_callback, #cond); \
48
+ return 0; \
49
+ } \
50
+ } while(0)
51
+
52
+ #define ARG_CHECK_VOID(cond) do { \
53
+ if (EXPECT(!(cond), 0)) { \
54
+ nostr_native_secp256k1_callback_call(&ctx->illegal_callback, #cond); \
55
+ return; \
56
+ } \
57
+ } while(0)
58
+
59
+ /* Note that whenever you change the context struct, you must also change the
60
+ * context_eq function. */
61
+ struct nostr_native_secp256k1_context_struct {
62
+ nostr_native_secp256k1_ecmult_gen_context ecmult_gen_ctx;
63
+ nostr_native_secp256k1_hash_ctx hash_ctx;
64
+ nostr_native_secp256k1_callback illegal_callback;
65
+ nostr_native_secp256k1_callback error_callback;
66
+ int declassify;
67
+ };
68
+
69
+ static const nostr_native_secp256k1_context nostr_native_secp256k1_context_static_ = {
70
+ { 0 },
71
+ { nostr_native_secp256k1_sha256_transform },
72
+ { nostr_native_secp256k1_default_illegal_callback_fn, 0 },
73
+ { nostr_native_secp256k1_default_error_callback_fn, 0 },
74
+ 0
75
+ };
76
+ const nostr_native_secp256k1_context * const nostr_native_secp256k1_context_static = &nostr_native_secp256k1_context_static_;
77
+
78
+ /* Helper function that determines if a context is proper, i.e., is not the static context or a copy thereof.
79
+ *
80
+ * This is intended for "context" functions such as nostr_native_secp256k1_context_clone. Functions that need specific
81
+ * features of a context should still check for these features directly. For example, a function that needs
82
+ * ecmult_gen should directly check for the existence of the ecmult_gen context. */
83
+ static int nostr_native_secp256k1_context_is_proper(const nostr_native_secp256k1_context* ctx) {
84
+ return nostr_native_secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx);
85
+ }
86
+
87
+ void nostr_native_secp256k1_selftest(void) {
88
+ if (!nostr_native_secp256k1_selftest_passes()) {
89
+ nostr_native_secp256k1_callback_call(&default_error_callback, "self test failed");
90
+ }
91
+ }
92
+
93
+ size_t nostr_native_secp256k1_context_preallocated_size(unsigned int flags) {
94
+ size_t ret = sizeof(nostr_native_secp256k1_context);
95
+ /* A return value of 0 is reserved as an indicator for errors when we call this function internally. */
96
+ VERIFY_CHECK(ret != 0);
97
+
98
+ if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
99
+ nostr_native_secp256k1_callback_call(&default_illegal_callback,
100
+ "Invalid flags");
101
+ return 0;
102
+ }
103
+
104
+ if (EXPECT(!SECP256K1_CHECKMEM_RUNNING() && (flags & SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY), 0)) {
105
+ nostr_native_secp256k1_callback_call(&default_illegal_callback,
106
+ "Declassify flag requires running with memory checking");
107
+ return 0;
108
+ }
109
+
110
+ return ret;
111
+ }
112
+
113
+ size_t nostr_native_secp256k1_context_preallocated_clone_size(const nostr_native_secp256k1_context* ctx) {
114
+ VERIFY_CHECK(ctx != NULL);
115
+ ARG_CHECK(nostr_native_secp256k1_context_is_proper(ctx));
116
+ return sizeof(nostr_native_secp256k1_context);
117
+ }
118
+
119
+ nostr_native_secp256k1_context* nostr_native_secp256k1_context_preallocated_create(void* prealloc, unsigned int flags) {
120
+ size_t prealloc_size;
121
+ nostr_native_secp256k1_context* ret;
122
+
123
+ nostr_native_secp256k1_selftest();
124
+
125
+ prealloc_size = nostr_native_secp256k1_context_preallocated_size(flags);
126
+ if (prealloc_size == 0) {
127
+ return NULL;
128
+ }
129
+ VERIFY_CHECK(prealloc != NULL);
130
+ ret = (nostr_native_secp256k1_context*)prealloc;
131
+ ret->illegal_callback = default_illegal_callback;
132
+ ret->error_callback = default_error_callback;
133
+ nostr_native_secp256k1_hash_ctx_init(&ret->hash_ctx);
134
+
135
+ /* Flags have been checked by nostr_native_secp256k1_context_preallocated_size. */
136
+ VERIFY_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_CONTEXT);
137
+ nostr_native_secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &ret->hash_ctx);
138
+ ret->declassify = !!(flags & SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY);
139
+
140
+ return ret;
141
+ }
142
+
143
+ nostr_native_secp256k1_context* nostr_native_secp256k1_context_create(unsigned int flags) {
144
+ size_t const prealloc_size = nostr_native_secp256k1_context_preallocated_size(flags);
145
+ nostr_native_secp256k1_context* ctx = checked_malloc(&default_error_callback, prealloc_size);
146
+ if (EXPECT(nostr_native_secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
147
+ free(ctx);
148
+ return NULL;
149
+ }
150
+
151
+ return ctx;
152
+ }
153
+
154
+ nostr_native_secp256k1_context* nostr_native_secp256k1_context_preallocated_clone(const nostr_native_secp256k1_context* ctx, void* prealloc) {
155
+ nostr_native_secp256k1_context* ret;
156
+ VERIFY_CHECK(ctx != NULL);
157
+ ARG_CHECK(prealloc != NULL);
158
+ ARG_CHECK(nostr_native_secp256k1_context_is_proper(ctx));
159
+
160
+ ret = (nostr_native_secp256k1_context*)prealloc;
161
+ *ret = *ctx;
162
+ return ret;
163
+ }
164
+
165
+ nostr_native_secp256k1_context* nostr_native_secp256k1_context_clone(const nostr_native_secp256k1_context* ctx) {
166
+ nostr_native_secp256k1_context* ret;
167
+ size_t prealloc_size;
168
+
169
+ VERIFY_CHECK(ctx != NULL);
170
+ ARG_CHECK(nostr_native_secp256k1_context_is_proper(ctx));
171
+
172
+ prealloc_size = nostr_native_secp256k1_context_preallocated_clone_size(ctx);
173
+ ret = checked_malloc(&ctx->error_callback, prealloc_size);
174
+ ret = nostr_native_secp256k1_context_preallocated_clone(ctx, ret);
175
+ return ret;
176
+ }
177
+
178
+ void nostr_native_secp256k1_context_preallocated_destroy(nostr_native_secp256k1_context* ctx) {
179
+ ARG_CHECK_VOID(ctx == NULL || nostr_native_secp256k1_context_is_proper(ctx));
180
+
181
+ /* Defined as noop */
182
+ if (ctx == NULL) {
183
+ return;
184
+ }
185
+
186
+ nostr_native_secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
187
+ }
188
+
189
+ void nostr_native_secp256k1_context_destroy(nostr_native_secp256k1_context* ctx) {
190
+ ARG_CHECK_VOID(ctx == NULL || nostr_native_secp256k1_context_is_proper(ctx));
191
+
192
+ /* Defined as noop */
193
+ if (ctx == NULL) {
194
+ return;
195
+ }
196
+
197
+ nostr_native_secp256k1_context_preallocated_destroy(ctx);
198
+ free(ctx);
199
+ }
200
+
201
+ void nostr_native_secp256k1_context_set_illegal_callback(nostr_native_secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
202
+ /* We compare pointers instead of checking nostr_native_secp256k1_context_is_proper() here
203
+ because setting callbacks is allowed on *copies* of the static context:
204
+ it's harmless and makes testing easier. */
205
+ ARG_CHECK_VOID(ctx != nostr_native_secp256k1_context_static);
206
+ if (fun == NULL) {
207
+ fun = nostr_native_secp256k1_default_illegal_callback_fn;
208
+ }
209
+ ctx->illegal_callback.fn = fun;
210
+ ctx->illegal_callback.data = data;
211
+ }
212
+
213
+ void nostr_native_secp256k1_context_set_error_callback(nostr_native_secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
214
+ /* We compare pointers instead of checking nostr_native_secp256k1_context_is_proper() here
215
+ because setting callbacks is allowed on *copies* of the static context:
216
+ it's harmless and makes testing easier. */
217
+ ARG_CHECK_VOID(ctx != nostr_native_secp256k1_context_static);
218
+ if (fun == NULL) {
219
+ fun = nostr_native_secp256k1_default_error_callback_fn;
220
+ }
221
+ ctx->error_callback.fn = fun;
222
+ ctx->error_callback.data = data;
223
+ }
224
+
225
+ void nostr_native_secp256k1_context_set_sha256_compression(nostr_native_secp256k1_context *ctx, nostr_native_secp256k1_sha256_compression_function fn_compression) {
226
+ VERIFY_CHECK(ctx != NULL);
227
+ ARG_CHECK_VOID(nostr_native_secp256k1_context_is_proper(ctx));
228
+ if (!fn_compression) { /* Reset hash context */
229
+ nostr_native_secp256k1_hash_ctx_init(&ctx->hash_ctx);
230
+ return;
231
+ }
232
+ /* Check and set */
233
+ ARG_CHECK_VOID(nostr_native_secp256k1_selftest_sha256(fn_compression));
234
+ ctx->hash_ctx.fn_sha256_compression = fn_compression;
235
+ }
236
+
237
+ static SECP256K1_INLINE const nostr_native_secp256k1_hash_ctx* nostr_native_secp256k1_get_hash_context(const nostr_native_secp256k1_context *ctx) {
238
+ return &ctx->hash_ctx;
239
+ }
240
+
241
+ static nostr_native_secp256k1_scratch_space* nostr_native_secp256k1_scratch_space_create(const nostr_native_secp256k1_context* ctx, size_t max_size) {
242
+ VERIFY_CHECK(ctx != NULL);
243
+ return nostr_native_secp256k1_scratch_create(&ctx->error_callback, max_size);
244
+ }
245
+
246
+ static void nostr_native_secp256k1_scratch_space_destroy(const nostr_native_secp256k1_context *ctx, nostr_native_secp256k1_scratch_space* scratch) {
247
+ VERIFY_CHECK(ctx != NULL);
248
+ nostr_native_secp256k1_scratch_destroy(&ctx->error_callback, scratch);
249
+ }
250
+
251
+ /* Mark memory as no-longer-secret for the purpose of analysing constant-time behaviour
252
+ * of the software.
253
+ */
254
+ static SECP256K1_INLINE void nostr_native_secp256k1_declassify(const nostr_native_secp256k1_context* ctx, const void *p, size_t len) {
255
+ if (EXPECT(ctx->declassify, 0)) SECP256K1_CHECKMEM_DEFINE(p, len);
256
+ }
257
+
258
+ static int nostr_native_secp256k1_pubkey_load(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_ge* ge, const nostr_native_secp256k1_pubkey* pubkey) {
259
+ nostr_native_secp256k1_ge_from_bytes(ge, pubkey->data);
260
+ ARG_CHECK(!nostr_native_secp256k1_fe_is_zero(&ge->x));
261
+ return 1;
262
+ }
263
+
264
+ static void nostr_native_secp256k1_pubkey_save(nostr_native_secp256k1_pubkey* pubkey, nostr_native_secp256k1_ge* ge) {
265
+ nostr_native_secp256k1_ge_to_bytes(pubkey->data, ge);
266
+ }
267
+
268
+ int nostr_native_secp256k1_ec_pubkey_parse(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
269
+ nostr_native_secp256k1_ge Q;
270
+
271
+ VERIFY_CHECK(ctx != NULL);
272
+ ARG_CHECK(pubkey != NULL);
273
+ memset(pubkey, 0, sizeof(*pubkey));
274
+ ARG_CHECK(input != NULL);
275
+ if (!nostr_native_secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
276
+ return 0;
277
+ }
278
+ if (!nostr_native_secp256k1_ge_is_in_correct_subgroup(&Q)) {
279
+ return 0;
280
+ }
281
+ nostr_native_secp256k1_pubkey_save(pubkey, &Q);
282
+ nostr_native_secp256k1_ge_clear(&Q);
283
+ return 1;
284
+ }
285
+
286
+ int nostr_native_secp256k1_ec_pubkey_serialize(const nostr_native_secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const nostr_native_secp256k1_pubkey* pubkey, unsigned int flags) {
287
+ nostr_native_secp256k1_ge Q;
288
+ size_t len;
289
+
290
+ VERIFY_CHECK(ctx != NULL);
291
+ ARG_CHECK(outputlen != NULL);
292
+ ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33u : 65u));
293
+ len = *outputlen;
294
+ *outputlen = 0;
295
+ ARG_CHECK(output != NULL);
296
+ memset(output, 0, len);
297
+ ARG_CHECK(pubkey != NULL);
298
+ ARG_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_COMPRESSION);
299
+ if (nostr_native_secp256k1_pubkey_load(ctx, &Q, pubkey)) {
300
+ if (flags & SECP256K1_FLAGS_BIT_COMPRESSION) {
301
+ nostr_native_secp256k1_eckey_pubkey_serialize33(&Q, output);
302
+ *outputlen = 33;
303
+ } else {
304
+ nostr_native_secp256k1_eckey_pubkey_serialize65(&Q, output);
305
+ *outputlen = 65;
306
+ }
307
+ return 1;
308
+ }
309
+ return 0;
310
+ }
311
+
312
+ int nostr_native_secp256k1_ec_pubkey_cmp(const nostr_native_secp256k1_context* ctx, const nostr_native_secp256k1_pubkey* pubkey0, const nostr_native_secp256k1_pubkey* pubkey1) {
313
+ unsigned char out[2][33];
314
+ const nostr_native_secp256k1_pubkey* pk[2];
315
+ int i;
316
+
317
+ VERIFY_CHECK(ctx != NULL);
318
+ pk[0] = pubkey0; pk[1] = pubkey1;
319
+ for (i = 0; i < 2; i++) {
320
+ size_t out_size = sizeof(out[i]);
321
+ /* If the public key is NULL or invalid, ec_pubkey_serialize will call
322
+ * the illegal_callback and return 0. In that case we will serialize the
323
+ * key as all zeros which is less than any valid public key. This
324
+ * results in consistent comparisons even if NULL or invalid pubkeys are
325
+ * involved and prevents edge cases such as sorting algorithms that use
326
+ * this function and do not terminate as a result. */
327
+ if (!nostr_native_secp256k1_ec_pubkey_serialize(ctx, out[i], &out_size, pk[i], SECP256K1_EC_COMPRESSED)) {
328
+ /* Note that ec_pubkey_serialize should already set the output to
329
+ * zero in that case, but it's not guaranteed by the API, we can't
330
+ * test it and writing a VERIFY_CHECK is more complex than
331
+ * explicitly memsetting (again). */
332
+ memset(out[i], 0, sizeof(out[i]));
333
+ }
334
+ }
335
+ return nostr_native_secp256k1_memcmp_var(out[0], out[1], sizeof(out[0]));
336
+ }
337
+
338
+ static int nostr_native_secp256k1_ec_pubkey_sort_cmp(const void* pk1, const void* pk2, void *ctx) {
339
+ return nostr_native_secp256k1_ec_pubkey_cmp((nostr_native_secp256k1_context *)ctx,
340
+ *(nostr_native_secp256k1_pubkey **)pk1,
341
+ *(nostr_native_secp256k1_pubkey **)pk2);
342
+ }
343
+
344
+ int nostr_native_secp256k1_ec_pubkey_sort(const nostr_native_secp256k1_context* ctx, const nostr_native_secp256k1_pubkey **pubkeys, size_t n_pubkeys) {
345
+ size_t i;
346
+
347
+ VERIFY_CHECK(ctx != NULL);
348
+ ARG_CHECK(pubkeys != NULL);
349
+ for (i = 0; i < n_pubkeys; i++) {
350
+ ARG_CHECK(pubkeys[i] != NULL);
351
+ }
352
+
353
+ /* Suppress wrong warning (fixed in MSVC 19.33) */
354
+ #if defined(_MSC_VER) && (_MSC_VER < 1933)
355
+ #pragma warning(push)
356
+ #pragma warning(disable: 4090)
357
+ #endif
358
+
359
+ /* Casting away const is fine because neither nostr_native_secp256k1_hsort nor
360
+ * nostr_native_secp256k1_ec_pubkey_sort_cmp modify the data pointed to by the cmp_data
361
+ * argument. */
362
+ nostr_native_secp256k1_hsort(pubkeys, n_pubkeys, sizeof(*pubkeys), nostr_native_secp256k1_ec_pubkey_sort_cmp, (void *)ctx);
363
+
364
+ #if defined(_MSC_VER) && (_MSC_VER < 1933)
365
+ #pragma warning(pop)
366
+ #endif
367
+
368
+ return 1;
369
+ }
370
+
371
+ static void nostr_native_secp256k1_ecdsa_signature_load(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_scalar* r, nostr_native_secp256k1_scalar* s, const nostr_native_secp256k1_ecdsa_signature* sig) {
372
+ (void)ctx;
373
+ if (sizeof(nostr_native_secp256k1_scalar) == 32) {
374
+ /* When the nostr_native_secp256k1_scalar type is exactly 32 byte, use its
375
+ * representation inside nostr_native_secp256k1_ecdsa_signature, as conversion is very fast.
376
+ * Note that nostr_native_secp256k1_ecdsa_signature_save must use the same representation. */
377
+ memcpy(r, &sig->data[0], 32);
378
+ memcpy(s, &sig->data[32], 32);
379
+ } else {
380
+ nostr_native_secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
381
+ nostr_native_secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
382
+ }
383
+ }
384
+
385
+ static void nostr_native_secp256k1_ecdsa_signature_save(nostr_native_secp256k1_ecdsa_signature* sig, const nostr_native_secp256k1_scalar* r, const nostr_native_secp256k1_scalar* s) {
386
+ if (sizeof(nostr_native_secp256k1_scalar) == 32) {
387
+ memcpy(&sig->data[0], r, 32);
388
+ memcpy(&sig->data[32], s, 32);
389
+ } else {
390
+ nostr_native_secp256k1_scalar_get_b32(&sig->data[0], r);
391
+ nostr_native_secp256k1_scalar_get_b32(&sig->data[32], s);
392
+ }
393
+ }
394
+
395
+ int nostr_native_secp256k1_ecdsa_signature_parse_der(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
396
+ nostr_native_secp256k1_scalar r, s;
397
+
398
+ VERIFY_CHECK(ctx != NULL);
399
+ ARG_CHECK(sig != NULL);
400
+ ARG_CHECK(input != NULL);
401
+
402
+ if (nostr_native_secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
403
+ nostr_native_secp256k1_ecdsa_signature_save(sig, &r, &s);
404
+ return 1;
405
+ } else {
406
+ memset(sig, 0, sizeof(*sig));
407
+ return 0;
408
+ }
409
+ }
410
+
411
+ int nostr_native_secp256k1_ecdsa_signature_parse_compact(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_ecdsa_signature* sig, const unsigned char *input64) {
412
+ nostr_native_secp256k1_scalar r, s;
413
+ int ret = 1;
414
+ int overflow = 0;
415
+
416
+ VERIFY_CHECK(ctx != NULL);
417
+ ARG_CHECK(sig != NULL);
418
+ ARG_CHECK(input64 != NULL);
419
+
420
+ nostr_native_secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
421
+ ret &= !overflow;
422
+ nostr_native_secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
423
+ ret &= !overflow;
424
+ if (ret) {
425
+ nostr_native_secp256k1_ecdsa_signature_save(sig, &r, &s);
426
+ } else {
427
+ memset(sig, 0, sizeof(*sig));
428
+ }
429
+ return ret;
430
+ }
431
+
432
+ int nostr_native_secp256k1_ecdsa_signature_serialize_der(const nostr_native_secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const nostr_native_secp256k1_ecdsa_signature* sig) {
433
+ nostr_native_secp256k1_scalar r, s;
434
+
435
+ VERIFY_CHECK(ctx != NULL);
436
+ ARG_CHECK(output != NULL);
437
+ ARG_CHECK(outputlen != NULL);
438
+ ARG_CHECK(sig != NULL);
439
+
440
+ nostr_native_secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
441
+ return nostr_native_secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
442
+ }
443
+
444
+ int nostr_native_secp256k1_ecdsa_signature_serialize_compact(const nostr_native_secp256k1_context* ctx, unsigned char *output64, const nostr_native_secp256k1_ecdsa_signature* sig) {
445
+ nostr_native_secp256k1_scalar r, s;
446
+
447
+ VERIFY_CHECK(ctx != NULL);
448
+ ARG_CHECK(output64 != NULL);
449
+ ARG_CHECK(sig != NULL);
450
+
451
+ nostr_native_secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
452
+ nostr_native_secp256k1_scalar_get_b32(&output64[0], &r);
453
+ nostr_native_secp256k1_scalar_get_b32(&output64[32], &s);
454
+ return 1;
455
+ }
456
+
457
+ int nostr_native_secp256k1_ecdsa_signature_normalize(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_ecdsa_signature *sigout, const nostr_native_secp256k1_ecdsa_signature *sigin) {
458
+ nostr_native_secp256k1_scalar r, s;
459
+ int ret = 0;
460
+
461
+ VERIFY_CHECK(ctx != NULL);
462
+ ARG_CHECK(sigin != NULL);
463
+
464
+ nostr_native_secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
465
+ ret = nostr_native_secp256k1_scalar_is_high(&s);
466
+ if (sigout != NULL) {
467
+ if (ret) {
468
+ nostr_native_secp256k1_scalar_negate(&s, &s);
469
+ }
470
+ nostr_native_secp256k1_ecdsa_signature_save(sigout, &r, &s);
471
+ }
472
+
473
+ return ret;
474
+ }
475
+
476
+ int nostr_native_secp256k1_ecdsa_verify(const nostr_native_secp256k1_context* ctx, const nostr_native_secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const nostr_native_secp256k1_pubkey *pubkey) {
477
+ nostr_native_secp256k1_ge q;
478
+ nostr_native_secp256k1_scalar r, s;
479
+ nostr_native_secp256k1_scalar m;
480
+ VERIFY_CHECK(ctx != NULL);
481
+ ARG_CHECK(msghash32 != NULL);
482
+ ARG_CHECK(sig != NULL);
483
+ ARG_CHECK(pubkey != NULL);
484
+
485
+ nostr_native_secp256k1_scalar_set_b32(&m, msghash32, NULL);
486
+ nostr_native_secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
487
+ return (!nostr_native_secp256k1_scalar_is_high(&s) &&
488
+ nostr_native_secp256k1_pubkey_load(ctx, &q, pubkey) &&
489
+ nostr_native_secp256k1_ecdsa_sig_verify(&r, &s, &q, &m));
490
+ }
491
+
492
+ static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) {
493
+ memcpy(buf + *offset, data, len);
494
+ *offset += len;
495
+ }
496
+
497
+ static int nonce_function_rfc6979_impl(const nostr_native_secp256k1_hash_ctx *hash_ctx, unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
498
+ unsigned char keydata[112];
499
+ unsigned int offset = 0;
500
+ nostr_native_secp256k1_rfc6979_hmac_sha256 rng;
501
+ unsigned int i;
502
+ nostr_native_secp256k1_scalar msg;
503
+ unsigned char msgmod32[32];
504
+ nostr_native_secp256k1_scalar_set_b32(&msg, msg32, NULL);
505
+ nostr_native_secp256k1_scalar_get_b32(msgmod32, &msg);
506
+ /* We feed a byte array to the PRNG as input, consisting of:
507
+ * - the private key (32 bytes) and reduced message (32 bytes), see RFC 6979 3.2d.
508
+ * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
509
+ * - optionally 16 extra bytes with the algorithm name.
510
+ * Because the arguments have distinct fixed lengths it is not possible for
511
+ * different argument mixtures to emulate each other and result in the same
512
+ * nonces.
513
+ */
514
+ buffer_append(keydata, &offset, key32, 32);
515
+ buffer_append(keydata, &offset, msgmod32, 32);
516
+ if (data != NULL) {
517
+ buffer_append(keydata, &offset, data, 32);
518
+ }
519
+ if (algo16 != NULL) {
520
+ buffer_append(keydata, &offset, algo16, 16);
521
+ }
522
+ nostr_native_secp256k1_rfc6979_hmac_sha256_initialize(hash_ctx, &rng, keydata, offset);
523
+ for (i = 0; ; i++) {
524
+ nostr_native_secp256k1_rfc6979_hmac_sha256_generate(hash_ctx, &rng, nonce32, 32);
525
+ if (i == counter) break;
526
+ }
527
+ nostr_native_secp256k1_rfc6979_hmac_sha256_finalize(&rng);
528
+
529
+ nostr_native_secp256k1_memclear_explicit(keydata, sizeof(keydata));
530
+ nostr_native_secp256k1_rfc6979_hmac_sha256_clear(&rng);
531
+ return 1;
532
+ }
533
+
534
+ static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
535
+ return nonce_function_rfc6979_impl(nostr_native_secp256k1_get_hash_context(nostr_native_secp256k1_context_static), nonce32, msg32, key32, algo16, data, counter);
536
+ }
537
+
538
+ const nostr_native_secp256k1_nonce_function nostr_native_secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979;
539
+ const nostr_native_secp256k1_nonce_function nostr_native_secp256k1_nonce_function_default = nonce_function_rfc6979;
540
+
541
+ static int nostr_native_secp256k1_ecdsa_sign_inner(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_scalar* r, nostr_native_secp256k1_scalar* s, int* recid, const unsigned char *msg32, const unsigned char *seckey, nostr_native_secp256k1_nonce_function noncefp, const void* noncedata) {
542
+ nostr_native_secp256k1_scalar sec, non, msg;
543
+ int ret = 0;
544
+ int is_sec_valid;
545
+ unsigned char nonce32[32];
546
+ unsigned int count = 0;
547
+ /* Default initialization here is important so we won't pass uninit values to the cmov in the end */
548
+ *r = nostr_native_secp256k1_scalar_zero;
549
+ *s = nostr_native_secp256k1_scalar_zero;
550
+ if (recid) {
551
+ *recid = 0;
552
+ }
553
+
554
+ /* Fail if the secret key is invalid. */
555
+ is_sec_valid = nostr_native_secp256k1_scalar_set_b32_seckey(&sec, seckey);
556
+ nostr_native_secp256k1_scalar_cmov(&sec, &nostr_native_secp256k1_scalar_one, !is_sec_valid);
557
+ nostr_native_secp256k1_scalar_set_b32(&msg, msg32, NULL);
558
+ while (1) {
559
+ int is_nonce_valid;
560
+
561
+ if (noncefp == NULL || noncefp == nostr_native_secp256k1_nonce_function_rfc6979) {
562
+ /* Use ctx-aware function by default */
563
+ ret = nonce_function_rfc6979_impl(nostr_native_secp256k1_get_hash_context(ctx), nonce32, msg32, seckey, NULL, (void*)noncedata, count);
564
+ } else {
565
+ ret = !!noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
566
+ }
567
+
568
+ if (!ret) {
569
+ break;
570
+ }
571
+ is_nonce_valid = nostr_native_secp256k1_scalar_set_b32_seckey(&non, nonce32);
572
+ /* The nonce is still secret here, but it being invalid is less likely than 1:2^255. */
573
+ nostr_native_secp256k1_declassify(ctx, &is_nonce_valid, sizeof(is_nonce_valid));
574
+ if (is_nonce_valid) {
575
+ ret = nostr_native_secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, r, s, &sec, &msg, &non, recid);
576
+ /* The final signature is no longer a secret, nor is the fact that we were successful or not. */
577
+ nostr_native_secp256k1_declassify(ctx, &ret, sizeof(ret));
578
+ if (ret) {
579
+ break;
580
+ }
581
+ }
582
+ count++;
583
+ }
584
+ /* We don't want to declassify is_sec_valid and therefore the range of
585
+ * seckey. As a result is_sec_valid is included in ret only after ret was
586
+ * used as a branching variable. */
587
+ ret &= is_sec_valid;
588
+ nostr_native_secp256k1_memclear_explicit(nonce32, sizeof(nonce32));
589
+ nostr_native_secp256k1_scalar_clear(&msg);
590
+ nostr_native_secp256k1_scalar_clear(&non);
591
+ nostr_native_secp256k1_scalar_clear(&sec);
592
+ nostr_native_secp256k1_scalar_cmov(r, &nostr_native_secp256k1_scalar_zero, !ret);
593
+ nostr_native_secp256k1_scalar_cmov(s, &nostr_native_secp256k1_scalar_zero, !ret);
594
+ if (recid) {
595
+ const int zero = 0;
596
+ nostr_native_secp256k1_int_cmov(recid, &zero, !ret);
597
+ }
598
+ return ret;
599
+ }
600
+
601
+ int nostr_native_secp256k1_ecdsa_sign(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_ecdsa_signature *signature, const unsigned char *msghash32, const unsigned char *seckey, nostr_native_secp256k1_nonce_function noncefp, const void* noncedata) {
602
+ nostr_native_secp256k1_scalar r, s;
603
+ int ret;
604
+ VERIFY_CHECK(ctx != NULL);
605
+ ARG_CHECK(nostr_native_secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
606
+ ARG_CHECK(msghash32 != NULL);
607
+ ARG_CHECK(signature != NULL);
608
+ ARG_CHECK(seckey != NULL);
609
+
610
+ ret = nostr_native_secp256k1_ecdsa_sign_inner(ctx, &r, &s, NULL, msghash32, seckey, noncefp, noncedata);
611
+ nostr_native_secp256k1_ecdsa_signature_save(signature, &r, &s);
612
+ return ret;
613
+ }
614
+
615
+ int nostr_native_secp256k1_ec_seckey_verify(const nostr_native_secp256k1_context* ctx, const unsigned char *seckey) {
616
+ nostr_native_secp256k1_scalar sec;
617
+ int ret;
618
+ VERIFY_CHECK(ctx != NULL);
619
+ ARG_CHECK(seckey != NULL);
620
+
621
+ ret = nostr_native_secp256k1_scalar_set_b32_seckey(&sec, seckey);
622
+ nostr_native_secp256k1_scalar_clear(&sec);
623
+ return ret;
624
+ }
625
+
626
+ static int nostr_native_secp256k1_ec_pubkey_create_helper(const nostr_native_secp256k1_ecmult_gen_context *ecmult_gen_ctx, nostr_native_secp256k1_scalar *seckey_scalar, nostr_native_secp256k1_ge *p, const unsigned char *seckey) {
627
+ int ret;
628
+
629
+ ret = nostr_native_secp256k1_scalar_set_b32_seckey(seckey_scalar, seckey);
630
+ nostr_native_secp256k1_scalar_cmov(seckey_scalar, &nostr_native_secp256k1_scalar_one, !ret);
631
+
632
+ nostr_native_secp256k1_ecmult_gen_ge(ecmult_gen_ctx, p, seckey_scalar);
633
+ return ret;
634
+ }
635
+
636
+ int nostr_native_secp256k1_ec_pubkey_create(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_pubkey *pubkey, const unsigned char *seckey) {
637
+ nostr_native_secp256k1_ge p;
638
+ nostr_native_secp256k1_scalar seckey_scalar;
639
+ int ret = 0;
640
+ VERIFY_CHECK(ctx != NULL);
641
+ ARG_CHECK(pubkey != NULL);
642
+ memset(pubkey, 0, sizeof(*pubkey));
643
+ ARG_CHECK(nostr_native_secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
644
+ ARG_CHECK(seckey != NULL);
645
+
646
+ ret = nostr_native_secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &seckey_scalar, &p, seckey);
647
+ nostr_native_secp256k1_pubkey_save(pubkey, &p);
648
+ nostr_native_secp256k1_memczero(pubkey, sizeof(*pubkey), !ret);
649
+
650
+ nostr_native_secp256k1_scalar_clear(&seckey_scalar);
651
+ return ret;
652
+ }
653
+
654
+ int nostr_native_secp256k1_ec_seckey_negate(const nostr_native_secp256k1_context* ctx, unsigned char *seckey) {
655
+ nostr_native_secp256k1_scalar sec;
656
+ int ret = 0;
657
+ VERIFY_CHECK(ctx != NULL);
658
+ ARG_CHECK(seckey != NULL);
659
+
660
+ ret = nostr_native_secp256k1_scalar_set_b32_seckey(&sec, seckey);
661
+ nostr_native_secp256k1_scalar_cmov(&sec, &nostr_native_secp256k1_scalar_zero, !ret);
662
+ nostr_native_secp256k1_scalar_negate(&sec, &sec);
663
+ nostr_native_secp256k1_scalar_get_b32(seckey, &sec);
664
+
665
+ nostr_native_secp256k1_scalar_clear(&sec);
666
+ return ret;
667
+ }
668
+
669
+ int nostr_native_secp256k1_ec_pubkey_negate(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_pubkey *pubkey) {
670
+ int ret = 0;
671
+ nostr_native_secp256k1_ge p;
672
+ VERIFY_CHECK(ctx != NULL);
673
+ ARG_CHECK(pubkey != NULL);
674
+
675
+ ret = nostr_native_secp256k1_pubkey_load(ctx, &p, pubkey);
676
+ memset(pubkey, 0, sizeof(*pubkey));
677
+ if (ret) {
678
+ nostr_native_secp256k1_ge_neg(&p, &p);
679
+ nostr_native_secp256k1_pubkey_save(pubkey, &p);
680
+ }
681
+ return ret;
682
+ }
683
+
684
+
685
+ static int nostr_native_secp256k1_ec_seckey_tweak_add_helper(nostr_native_secp256k1_scalar *sec, const unsigned char *tweak32) {
686
+ nostr_native_secp256k1_scalar term;
687
+ int overflow = 0;
688
+ int ret = 0;
689
+
690
+ nostr_native_secp256k1_scalar_set_b32(&term, tweak32, &overflow);
691
+ ret = (!overflow) & nostr_native_secp256k1_eckey_privkey_tweak_add(sec, &term);
692
+ nostr_native_secp256k1_scalar_clear(&term);
693
+ return ret;
694
+ }
695
+
696
+ int nostr_native_secp256k1_ec_seckey_tweak_add(const nostr_native_secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
697
+ nostr_native_secp256k1_scalar sec;
698
+ int ret = 0;
699
+ VERIFY_CHECK(ctx != NULL);
700
+ ARG_CHECK(seckey != NULL);
701
+ ARG_CHECK(tweak32 != NULL);
702
+
703
+ ret = nostr_native_secp256k1_scalar_set_b32_seckey(&sec, seckey);
704
+ ret &= nostr_native_secp256k1_ec_seckey_tweak_add_helper(&sec, tweak32);
705
+ nostr_native_secp256k1_scalar_cmov(&sec, &nostr_native_secp256k1_scalar_zero, !ret);
706
+ nostr_native_secp256k1_scalar_get_b32(seckey, &sec);
707
+
708
+ nostr_native_secp256k1_scalar_clear(&sec);
709
+ return ret;
710
+ }
711
+
712
+ static int nostr_native_secp256k1_ec_pubkey_tweak_add_helper(nostr_native_secp256k1_ge *p, const unsigned char *tweak32) {
713
+ nostr_native_secp256k1_scalar term;
714
+ int overflow = 0;
715
+ nostr_native_secp256k1_scalar_set_b32(&term, tweak32, &overflow);
716
+ return !overflow && nostr_native_secp256k1_eckey_pubkey_tweak_add(p, &term);
717
+ }
718
+
719
+ int nostr_native_secp256k1_ec_pubkey_tweak_add(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_pubkey *pubkey, const unsigned char *tweak32) {
720
+ nostr_native_secp256k1_ge p;
721
+ int ret = 0;
722
+ VERIFY_CHECK(ctx != NULL);
723
+ ARG_CHECK(pubkey != NULL);
724
+ ARG_CHECK(tweak32 != NULL);
725
+
726
+ ret = nostr_native_secp256k1_pubkey_load(ctx, &p, pubkey);
727
+ memset(pubkey, 0, sizeof(*pubkey));
728
+ ret = ret && nostr_native_secp256k1_ec_pubkey_tweak_add_helper(&p, tweak32);
729
+ if (ret) {
730
+ nostr_native_secp256k1_pubkey_save(pubkey, &p);
731
+ }
732
+
733
+ return ret;
734
+ }
735
+
736
+ int nostr_native_secp256k1_ec_seckey_tweak_mul(const nostr_native_secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
737
+ nostr_native_secp256k1_scalar factor;
738
+ nostr_native_secp256k1_scalar sec;
739
+ int ret = 0;
740
+ int overflow = 0;
741
+ VERIFY_CHECK(ctx != NULL);
742
+ ARG_CHECK(seckey != NULL);
743
+ ARG_CHECK(tweak32 != NULL);
744
+
745
+ nostr_native_secp256k1_scalar_set_b32(&factor, tweak32, &overflow);
746
+ ret = nostr_native_secp256k1_scalar_set_b32_seckey(&sec, seckey);
747
+ ret &= (!overflow) & nostr_native_secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
748
+ nostr_native_secp256k1_scalar_cmov(&sec, &nostr_native_secp256k1_scalar_zero, !ret);
749
+ nostr_native_secp256k1_scalar_get_b32(seckey, &sec);
750
+
751
+ nostr_native_secp256k1_scalar_clear(&sec);
752
+ nostr_native_secp256k1_scalar_clear(&factor);
753
+ return ret;
754
+ }
755
+
756
+ int nostr_native_secp256k1_ec_pubkey_tweak_mul(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_pubkey *pubkey, const unsigned char *tweak32) {
757
+ nostr_native_secp256k1_ge p;
758
+ nostr_native_secp256k1_scalar factor;
759
+ int ret = 0;
760
+ int overflow = 0;
761
+ VERIFY_CHECK(ctx != NULL);
762
+ ARG_CHECK(pubkey != NULL);
763
+ ARG_CHECK(tweak32 != NULL);
764
+
765
+ nostr_native_secp256k1_scalar_set_b32(&factor, tweak32, &overflow);
766
+ ret = !overflow && nostr_native_secp256k1_pubkey_load(ctx, &p, pubkey);
767
+ memset(pubkey, 0, sizeof(*pubkey));
768
+ if (ret) {
769
+ if (nostr_native_secp256k1_eckey_pubkey_tweak_mul(&p, &factor)) {
770
+ nostr_native_secp256k1_pubkey_save(pubkey, &p);
771
+ } else {
772
+ ret = 0;
773
+ }
774
+ }
775
+
776
+ return ret;
777
+ }
778
+
779
+ int nostr_native_secp256k1_context_randomize(nostr_native_secp256k1_context* ctx, const unsigned char *seed32) {
780
+ VERIFY_CHECK(ctx != NULL);
781
+ ARG_CHECK(nostr_native_secp256k1_context_is_proper(ctx));
782
+
783
+ if (nostr_native_secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
784
+ nostr_native_secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, nostr_native_secp256k1_get_hash_context(ctx), seed32);
785
+ }
786
+ return 1;
787
+ }
788
+
789
+ int nostr_native_secp256k1_ec_pubkey_combine(const nostr_native_secp256k1_context* ctx, nostr_native_secp256k1_pubkey *pubnonce, const nostr_native_secp256k1_pubkey * const *pubnonces, size_t n) {
790
+ size_t i;
791
+ nostr_native_secp256k1_gej Qj;
792
+ nostr_native_secp256k1_ge Q;
793
+
794
+ VERIFY_CHECK(ctx != NULL);
795
+ ARG_CHECK(pubnonce != NULL);
796
+ memset(pubnonce, 0, sizeof(*pubnonce));
797
+ ARG_CHECK(n >= 1);
798
+ ARG_CHECK(pubnonces != NULL);
799
+
800
+ nostr_native_secp256k1_gej_set_infinity(&Qj);
801
+
802
+ for (i = 0; i < n; i++) {
803
+ ARG_CHECK(pubnonces[i] != NULL);
804
+ nostr_native_secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
805
+ nostr_native_secp256k1_gej_add_ge(&Qj, &Qj, &Q);
806
+ }
807
+ if (nostr_native_secp256k1_gej_is_infinity(&Qj)) {
808
+ return 0;
809
+ }
810
+ nostr_native_secp256k1_ge_set_gej(&Q, &Qj);
811
+ nostr_native_secp256k1_pubkey_save(pubnonce, &Q);
812
+ return 1;
813
+ }
814
+
815
+ int nostr_native_secp256k1_tagged_sha256(const nostr_native_secp256k1_context* ctx, unsigned char *hash32, const unsigned char *tag, size_t taglen, const unsigned char *msg, size_t msglen) {
816
+ nostr_native_secp256k1_sha256 sha;
817
+ VERIFY_CHECK(ctx != NULL);
818
+ ARG_CHECK(hash32 != NULL);
819
+ ARG_CHECK(tag != NULL);
820
+ ARG_CHECK(msg != NULL);
821
+
822
+ nostr_native_secp256k1_sha256_initialize_tagged(nostr_native_secp256k1_get_hash_context(ctx), &sha, tag, taglen);
823
+ nostr_native_secp256k1_sha256_write(nostr_native_secp256k1_get_hash_context(ctx), &sha, msg, msglen);
824
+ nostr_native_secp256k1_sha256_finalize(nostr_native_secp256k1_get_hash_context(ctx), &sha, hash32);
825
+ nostr_native_secp256k1_sha256_clear(&sha);
826
+ return 1;
827
+ }
828
+
829
+ #ifdef ENABLE_MODULE_ECDH
830
+ # include "modules/ecdh/main_impl.h"
831
+ #endif
832
+
833
+ #ifdef ENABLE_MODULE_RECOVERY
834
+ # include "modules/recovery/main_impl.h"
835
+ #endif
836
+
837
+ #ifdef ENABLE_MODULE_EXTRAKEYS
838
+ # include "modules/extrakeys/main_impl.h"
839
+ #endif
840
+
841
+ #ifdef ENABLE_MODULE_SCHNORRSIG
842
+ # include "modules/schnorrsig/main_impl.h"
843
+ #endif
844
+
845
+ #ifdef ENABLE_MODULE_MUSIG
846
+ # include "modules/musig/main_impl.h"
847
+ #endif
848
+
849
+ #ifdef ENABLE_MODULE_ELLSWIFT
850
+ # include "modules/ellswift/main_impl.h"
851
+ #endif
852
+
853
+ #ifdef ENABLE_MODULE_SILENTPAYMENTS
854
+ # include "modules/silentpayments/main_impl.h"
855
+ #endif