@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,309 @@
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
+
8
+ #ifndef SECP256K1_ECDSA_IMPL_H
9
+ #define SECP256K1_ECDSA_IMPL_H
10
+
11
+ #include "scalar.h"
12
+ #include "field.h"
13
+ #include "group.h"
14
+ #include "ecmult.h"
15
+ #include "ecmult_gen.h"
16
+ #include "ecdsa.h"
17
+
18
+ /** Group order for secp256k1 defined as 'n' in "Standards for Efficient Cryptography" (SEC2) 2.7.1
19
+ * $ sage -c 'load("nostr_native_secp256k1_params.sage"); print(hex(N))'
20
+ * 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
21
+ */
22
+ static const nostr_native_secp256k1_fe nostr_native_secp256k1_ecdsa_const_order_as_fe = SECP256K1_FE_CONST(
23
+ 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
24
+ 0xBAAEDCE6UL, 0xAF48A03BUL, 0xBFD25E8CUL, 0xD0364141UL
25
+ );
26
+
27
+ /** Difference between field and order, values 'p' and 'n' values defined in
28
+ * "Standards for Efficient Cryptography" (SEC2) 2.7.1.
29
+ * $ sage -c 'load("nostr_native_secp256k1_params.sage"); print(hex(P-N))'
30
+ * 0x14551231950b75fc4402da1722fc9baee
31
+ */
32
+ static const nostr_native_secp256k1_fe nostr_native_secp256k1_ecdsa_const_p_minus_order = SECP256K1_FE_CONST(
33
+ 0, 0, 0, 1, 0x45512319UL, 0x50B75FC4UL, 0x402DA172UL, 0x2FC9BAEEUL
34
+ );
35
+
36
+ static int nostr_native_secp256k1_der_read_len(size_t *len, const unsigned char **sigp, const unsigned char *sigend) {
37
+ size_t lenleft;
38
+ unsigned char b1;
39
+ VERIFY_CHECK(len != NULL);
40
+ *len = 0;
41
+ if (*sigp >= sigend) {
42
+ return 0;
43
+ }
44
+ b1 = *((*sigp)++);
45
+ if (b1 == 0xFF) {
46
+ /* X.690-0207 8.1.3.5.c the value 0xFF shall not be used. */
47
+ return 0;
48
+ }
49
+ if ((b1 & 0x80) == 0) {
50
+ /* X.690-0207 8.1.3.4 short form length octets */
51
+ *len = b1;
52
+ return 1;
53
+ }
54
+ if (b1 == 0x80) {
55
+ /* Indefinite length is not allowed in DER. */
56
+ return 0;
57
+ }
58
+ /* X.690-207 8.1.3.5 long form length octets */
59
+ lenleft = b1 & 0x7F; /* lenleft is at least 1 */
60
+ if (lenleft > (size_t)(sigend - *sigp)) {
61
+ return 0;
62
+ }
63
+ if (**sigp == 0) {
64
+ /* Not the shortest possible length encoding. */
65
+ return 0;
66
+ }
67
+ if (lenleft > sizeof(size_t)) {
68
+ /* The resulting length would exceed the range of a size_t, so
69
+ * it is certainly longer than the passed array size. */
70
+ return 0;
71
+ }
72
+ while (lenleft > 0) {
73
+ *len = (*len << 8) | **sigp;
74
+ (*sigp)++;
75
+ lenleft--;
76
+ }
77
+ if (*len > (size_t)(sigend - *sigp)) {
78
+ /* Result exceeds the length of the passed array.
79
+ (Checking this is the responsibility of the caller but it
80
+ can't hurt do it here, too.) */
81
+ return 0;
82
+ }
83
+ if (*len < 128) {
84
+ /* Not the shortest possible length encoding. */
85
+ return 0;
86
+ }
87
+ return 1;
88
+ }
89
+
90
+ static int nostr_native_secp256k1_der_parse_integer(nostr_native_secp256k1_scalar *r, const unsigned char **sig, const unsigned char *sigend) {
91
+ int overflow = 0;
92
+ unsigned char ra[32] = {0};
93
+ size_t rlen;
94
+
95
+ if (*sig == sigend || **sig != 0x02) {
96
+ /* Not a primitive integer (X.690-0207 8.3.1). */
97
+ return 0;
98
+ }
99
+ (*sig)++;
100
+ if (nostr_native_secp256k1_der_read_len(&rlen, sig, sigend) == 0) {
101
+ return 0;
102
+ }
103
+ if (rlen == 0 || rlen > (size_t)(sigend - *sig)) {
104
+ /* Exceeds bounds or not at least length 1 (X.690-0207 8.3.1). */
105
+ return 0;
106
+ }
107
+ if (**sig == 0x00 && rlen > 1 && (((*sig)[1]) & 0x80) == 0x00) {
108
+ /* Excessive 0x00 padding. */
109
+ return 0;
110
+ }
111
+ if (**sig == 0xFF && rlen > 1 && (((*sig)[1]) & 0x80) == 0x80) {
112
+ /* Excessive 0xFF padding. */
113
+ return 0;
114
+ }
115
+ if ((**sig & 0x80) == 0x80) {
116
+ /* Negative. */
117
+ overflow = 1;
118
+ }
119
+ /* There is at most one leading zero byte:
120
+ * if there were two leading zero bytes, we would have failed and returned 0
121
+ * because of excessive 0x00 padding already. */
122
+ if (rlen > 0 && **sig == 0) {
123
+ /* Skip leading zero byte */
124
+ rlen--;
125
+ (*sig)++;
126
+ }
127
+ if (rlen > 32) {
128
+ overflow = 1;
129
+ }
130
+ if (!overflow) {
131
+ if (rlen) memcpy(ra + 32 - rlen, *sig, rlen);
132
+ nostr_native_secp256k1_scalar_set_b32(r, ra, &overflow);
133
+ }
134
+ if (overflow) {
135
+ nostr_native_secp256k1_scalar_set_int(r, 0);
136
+ }
137
+ (*sig) += rlen;
138
+ return 1;
139
+ }
140
+
141
+ static int nostr_native_secp256k1_ecdsa_sig_parse(nostr_native_secp256k1_scalar *rr, nostr_native_secp256k1_scalar *rs, const unsigned char *sig, size_t size) {
142
+ const unsigned char *sigend = sig + size;
143
+ size_t rlen;
144
+ if (sig == sigend || *(sig++) != 0x30) {
145
+ /* The encoding doesn't start with a constructed sequence (X.690-0207 8.9.1). */
146
+ return 0;
147
+ }
148
+ if (nostr_native_secp256k1_der_read_len(&rlen, &sig, sigend) == 0) {
149
+ return 0;
150
+ }
151
+ if (rlen != (size_t)(sigend - sig)) {
152
+ /* Tuple exceeds bounds or garage after tuple. */
153
+ return 0;
154
+ }
155
+
156
+ if (!nostr_native_secp256k1_der_parse_integer(rr, &sig, sigend)) {
157
+ return 0;
158
+ }
159
+ if (!nostr_native_secp256k1_der_parse_integer(rs, &sig, sigend)) {
160
+ return 0;
161
+ }
162
+
163
+ if (sig != sigend) {
164
+ /* Trailing garbage inside tuple. */
165
+ return 0;
166
+ }
167
+
168
+ return 1;
169
+ }
170
+
171
+ static int nostr_native_secp256k1_ecdsa_sig_serialize(unsigned char *sig, size_t *size, const nostr_native_secp256k1_scalar* ar, const nostr_native_secp256k1_scalar* as) {
172
+ unsigned char r[33] = {0}, s[33] = {0};
173
+ unsigned char *rp = r, *sp = s;
174
+ size_t lenR = 33, lenS = 33;
175
+ nostr_native_secp256k1_scalar_get_b32(&r[1], ar);
176
+ nostr_native_secp256k1_scalar_get_b32(&s[1], as);
177
+ while (lenR > 1 && rp[0] == 0 && rp[1] < 0x80) { lenR--; rp++; }
178
+ while (lenS > 1 && sp[0] == 0 && sp[1] < 0x80) { lenS--; sp++; }
179
+ if (*size < 6+lenS+lenR) {
180
+ *size = 6 + lenS + lenR;
181
+ return 0;
182
+ }
183
+ *size = 6 + lenS + lenR;
184
+ sig[0] = 0x30;
185
+ sig[1] = 4 + lenS + lenR;
186
+ sig[2] = 0x02;
187
+ sig[3] = lenR;
188
+ memcpy(sig+4, rp, lenR);
189
+ sig[4+lenR] = 0x02;
190
+ sig[5+lenR] = lenS;
191
+ memcpy(sig+lenR+6, sp, lenS);
192
+ return 1;
193
+ }
194
+
195
+ static int nostr_native_secp256k1_ecdsa_sig_verify(const nostr_native_secp256k1_scalar *sigr, const nostr_native_secp256k1_scalar *sigs, const nostr_native_secp256k1_ge *pubkey, const nostr_native_secp256k1_scalar *message) {
196
+ unsigned char c[32];
197
+ nostr_native_secp256k1_scalar sn, u1, u2;
198
+ #if !defined(EXHAUSTIVE_TEST_ORDER)
199
+ int range;
200
+ nostr_native_secp256k1_fe xr;
201
+ #endif
202
+ nostr_native_secp256k1_gej pubkeyj;
203
+ nostr_native_secp256k1_gej pr;
204
+
205
+ if (nostr_native_secp256k1_scalar_is_zero(sigr) || nostr_native_secp256k1_scalar_is_zero(sigs)) {
206
+ return 0;
207
+ }
208
+
209
+ nostr_native_secp256k1_scalar_inverse_var(&sn, sigs);
210
+ nostr_native_secp256k1_scalar_mul(&u1, &sn, message);
211
+ nostr_native_secp256k1_scalar_mul(&u2, &sn, sigr);
212
+ nostr_native_secp256k1_gej_set_ge(&pubkeyj, pubkey);
213
+ nostr_native_secp256k1_ecmult(&pr, &pubkeyj, &u2, &u1);
214
+ if (nostr_native_secp256k1_gej_is_infinity(&pr)) {
215
+ return 0;
216
+ }
217
+
218
+ #if defined(EXHAUSTIVE_TEST_ORDER)
219
+ {
220
+ nostr_native_secp256k1_scalar computed_r;
221
+ nostr_native_secp256k1_ge pr_ge;
222
+ nostr_native_secp256k1_ge_set_gej(&pr_ge, &pr);
223
+ nostr_native_secp256k1_fe_normalize(&pr_ge.x);
224
+
225
+ nostr_native_secp256k1_fe_get_b32(c, &pr_ge.x);
226
+ nostr_native_secp256k1_scalar_set_b32(&computed_r, c, NULL);
227
+ return nostr_native_secp256k1_scalar_eq(sigr, &computed_r);
228
+ }
229
+ #else
230
+
231
+ /* Interpret sigr as a field element xr */
232
+ nostr_native_secp256k1_scalar_get_b32(c, sigr);
233
+ range = nostr_native_secp256k1_fe_set_b32_limit(&xr, c);
234
+ #ifdef VERIFY
235
+ /* We know that c is in range; it comes from a scalar. */
236
+ VERIFY_CHECK(range);
237
+ #else
238
+ (void)range;
239
+ #endif
240
+
241
+ /** We now have the recomputed R point in pr, and its claimed x coordinate (modulo n)
242
+ * in xr. Naively, we would extract the x coordinate from pr (requiring a inversion modulo p),
243
+ * compute the remainder modulo n, and compare it to xr. However:
244
+ *
245
+ * xr == X(pr) mod n
246
+ * <=> exists h. (xr + h * n < p && xr + h * n == X(pr))
247
+ * [Since 2 * n > p, h can only be 0 or 1]
248
+ * <=> (xr == X(pr)) || (xr + n < p && xr + n == X(pr))
249
+ * [In Jacobian coordinates, X(pr) is pr.x / pr.z^2 mod p]
250
+ * <=> (xr == pr.x / pr.z^2 mod p) || (xr + n < p && xr + n == pr.x / pr.z^2 mod p)
251
+ * [Multiplying both sides of the equations by pr.z^2 mod p]
252
+ * <=> (xr * pr.z^2 mod p == pr.x) || (xr + n < p && (xr + n) * pr.z^2 mod p == pr.x)
253
+ *
254
+ * Thus, we can avoid the inversion, but we have to check both cases separately.
255
+ * nostr_native_secp256k1_gej_eq_x implements the (xr * pr.z^2 mod p == pr.x) test.
256
+ */
257
+ if (nostr_native_secp256k1_gej_eq_x_var(&xr, &pr)) {
258
+ /* xr * pr.z^2 mod p == pr.x, so the signature is valid. */
259
+ return 1;
260
+ }
261
+ if (nostr_native_secp256k1_fe_cmp_var(&xr, &nostr_native_secp256k1_ecdsa_const_p_minus_order) >= 0) {
262
+ /* xr + n >= p, so we can skip testing the second case. */
263
+ return 0;
264
+ }
265
+ nostr_native_secp256k1_fe_add(&xr, &nostr_native_secp256k1_ecdsa_const_order_as_fe);
266
+ if (nostr_native_secp256k1_gej_eq_x_var(&xr, &pr)) {
267
+ /* (xr + n) * pr.z^2 mod p == pr.x, so the signature is valid. */
268
+ return 1;
269
+ }
270
+ return 0;
271
+ #endif
272
+ }
273
+
274
+ static int nostr_native_secp256k1_ecdsa_sig_sign(const nostr_native_secp256k1_ecmult_gen_context *ctx, nostr_native_secp256k1_scalar *sigr, nostr_native_secp256k1_scalar *sigs, const nostr_native_secp256k1_scalar *seckey, const nostr_native_secp256k1_scalar *message, const nostr_native_secp256k1_scalar *nonce, int *recid) {
275
+ unsigned char b[32];
276
+ nostr_native_secp256k1_ge r;
277
+ nostr_native_secp256k1_scalar n;
278
+ int overflow = 0;
279
+ int high;
280
+
281
+ nostr_native_secp256k1_ecmult_gen_ge(ctx, &r, nonce);
282
+ nostr_native_secp256k1_fe_normalize(&r.x);
283
+ nostr_native_secp256k1_fe_normalize(&r.y);
284
+ nostr_native_secp256k1_fe_get_b32(b, &r.x);
285
+ nostr_native_secp256k1_scalar_set_b32(sigr, b, &overflow);
286
+ if (recid) {
287
+ /* The overflow condition is cryptographically unreachable as hitting it requires finding the discrete log
288
+ * of some P where P.x >= order, and only 1 in about 2^127 points meet this criteria.
289
+ */
290
+ *recid = (overflow << 1) | nostr_native_secp256k1_fe_is_odd(&r.y);
291
+ }
292
+ nostr_native_secp256k1_scalar_mul(&n, sigr, seckey);
293
+ nostr_native_secp256k1_scalar_add(&n, &n, message);
294
+ nostr_native_secp256k1_scalar_inverse(sigs, nonce);
295
+ nostr_native_secp256k1_scalar_mul(sigs, sigs, &n);
296
+ nostr_native_secp256k1_scalar_clear(&n);
297
+ nostr_native_secp256k1_ge_clear(&r);
298
+ high = nostr_native_secp256k1_scalar_is_high(sigs);
299
+ nostr_native_secp256k1_scalar_cond_negate(sigs, high);
300
+ if (recid) {
301
+ *recid ^= high;
302
+ }
303
+ /* P.x = order is on the curve, so technically sig->r could end up being zero, which would be an invalid signature.
304
+ * This is cryptographically unreachable as hitting it requires finding the discrete log of P.x = N.
305
+ */
306
+ return (int)(!nostr_native_secp256k1_scalar_is_zero(sigr)) & (int)(!nostr_native_secp256k1_scalar_is_zero(sigs));
307
+ }
308
+
309
+ #endif /* SECP256K1_ECDSA_IMPL_H */
@@ -0,0 +1,28 @@
1
+ /***********************************************************************
2
+ * Copyright (c) 2013, 2014 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
+ #ifndef SECP256K1_ECKEY_H
8
+ #define SECP256K1_ECKEY_H
9
+
10
+ #include <stddef.h>
11
+
12
+ #include "group.h"
13
+ #include "scalar.h"
14
+ #include "ecmult.h"
15
+ #include "ecmult_gen.h"
16
+
17
+ static int nostr_native_secp256k1_eckey_pubkey_parse(nostr_native_secp256k1_ge *elem, const unsigned char *pub, size_t size);
18
+ /** Serialize a group element (that is not allowed to be infinity) to a compressed public key (33 bytes). */
19
+ static void nostr_native_secp256k1_eckey_pubkey_serialize33(nostr_native_secp256k1_ge *elem, unsigned char *pub33);
20
+ /** Serialize a group element (that is not allowed to be infinity) to an uncompressed public key (65 bytes). */
21
+ static void nostr_native_secp256k1_eckey_pubkey_serialize65(nostr_native_secp256k1_ge *elem, unsigned char *pub65);
22
+
23
+ static int nostr_native_secp256k1_eckey_privkey_tweak_add(nostr_native_secp256k1_scalar *key, const nostr_native_secp256k1_scalar *tweak);
24
+ static int nostr_native_secp256k1_eckey_pubkey_tweak_add(nostr_native_secp256k1_ge *key, const nostr_native_secp256k1_scalar *tweak);
25
+ static int nostr_native_secp256k1_eckey_privkey_tweak_mul(nostr_native_secp256k1_scalar *key, const nostr_native_secp256k1_scalar *tweak);
26
+ static int nostr_native_secp256k1_eckey_pubkey_tweak_mul(nostr_native_secp256k1_ge *key, const nostr_native_secp256k1_scalar *tweak);
27
+
28
+ #endif /* SECP256K1_ECKEY_H */
@@ -0,0 +1,94 @@
1
+ /***********************************************************************
2
+ * Copyright (c) 2013, 2014 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
+ #ifndef SECP256K1_ECKEY_IMPL_H
8
+ #define SECP256K1_ECKEY_IMPL_H
9
+
10
+ #include "eckey.h"
11
+
12
+ #include "util.h"
13
+ #include "scalar.h"
14
+ #include "field.h"
15
+ #include "group.h"
16
+ #include "ecmult_gen.h"
17
+
18
+ static int nostr_native_secp256k1_eckey_pubkey_parse(nostr_native_secp256k1_ge *elem, const unsigned char *pub, size_t size) {
19
+ if (size == 33 && (pub[0] == SECP256K1_TAG_PUBKEY_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_ODD)) {
20
+ nostr_native_secp256k1_fe x;
21
+ return nostr_native_secp256k1_fe_set_b32_limit(&x, pub+1) && nostr_native_secp256k1_ge_set_xo_var(elem, &x, pub[0] == SECP256K1_TAG_PUBKEY_ODD);
22
+ } else if (size == 65 && (pub[0] == SECP256K1_TAG_PUBKEY_UNCOMPRESSED || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD)) {
23
+ nostr_native_secp256k1_fe x, y;
24
+ if (!nostr_native_secp256k1_fe_set_b32_limit(&x, pub+1) || !nostr_native_secp256k1_fe_set_b32_limit(&y, pub+33)) {
25
+ return 0;
26
+ }
27
+ nostr_native_secp256k1_ge_set_xy(elem, &x, &y);
28
+ if ((pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD) &&
29
+ nostr_native_secp256k1_fe_is_odd(&y) != (pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD)) {
30
+ return 0;
31
+ }
32
+ return nostr_native_secp256k1_ge_is_valid_var(elem);
33
+ } else {
34
+ return 0;
35
+ }
36
+ }
37
+
38
+ static void nostr_native_secp256k1_eckey_pubkey_serialize33(nostr_native_secp256k1_ge *elem, unsigned char *pub33) {
39
+ VERIFY_CHECK(!nostr_native_secp256k1_ge_is_infinity(elem));
40
+
41
+ nostr_native_secp256k1_fe_normalize_var(&elem->x);
42
+ nostr_native_secp256k1_fe_normalize_var(&elem->y);
43
+ pub33[0] = nostr_native_secp256k1_fe_is_odd(&elem->y) ? SECP256K1_TAG_PUBKEY_ODD : SECP256K1_TAG_PUBKEY_EVEN;
44
+ nostr_native_secp256k1_fe_get_b32(&pub33[1], &elem->x);
45
+ }
46
+
47
+ static void nostr_native_secp256k1_eckey_pubkey_serialize65(nostr_native_secp256k1_ge *elem, unsigned char *pub65) {
48
+ VERIFY_CHECK(!nostr_native_secp256k1_ge_is_infinity(elem));
49
+
50
+ nostr_native_secp256k1_fe_normalize_var(&elem->x);
51
+ nostr_native_secp256k1_fe_normalize_var(&elem->y);
52
+ pub65[0] = SECP256K1_TAG_PUBKEY_UNCOMPRESSED;
53
+ nostr_native_secp256k1_fe_get_b32(&pub65[1], &elem->x);
54
+ nostr_native_secp256k1_fe_get_b32(&pub65[33], &elem->y);
55
+ }
56
+
57
+ static int nostr_native_secp256k1_eckey_privkey_tweak_add(nostr_native_secp256k1_scalar *key, const nostr_native_secp256k1_scalar *tweak) {
58
+ nostr_native_secp256k1_scalar_add(key, key, tweak);
59
+ return !nostr_native_secp256k1_scalar_is_zero(key);
60
+ }
61
+
62
+ static int nostr_native_secp256k1_eckey_pubkey_tweak_add(nostr_native_secp256k1_ge *key, const nostr_native_secp256k1_scalar *tweak) {
63
+ nostr_native_secp256k1_gej pt;
64
+ nostr_native_secp256k1_gej_set_ge(&pt, key);
65
+ nostr_native_secp256k1_ecmult(&pt, &pt, &nostr_native_secp256k1_scalar_one, tweak);
66
+
67
+ if (nostr_native_secp256k1_gej_is_infinity(&pt)) {
68
+ return 0;
69
+ }
70
+ nostr_native_secp256k1_ge_set_gej(key, &pt);
71
+ return 1;
72
+ }
73
+
74
+ static int nostr_native_secp256k1_eckey_privkey_tweak_mul(nostr_native_secp256k1_scalar *key, const nostr_native_secp256k1_scalar *tweak) {
75
+ int ret;
76
+ ret = !nostr_native_secp256k1_scalar_is_zero(tweak);
77
+
78
+ nostr_native_secp256k1_scalar_mul(key, key, tweak);
79
+ return ret;
80
+ }
81
+
82
+ static int nostr_native_secp256k1_eckey_pubkey_tweak_mul(nostr_native_secp256k1_ge *key, const nostr_native_secp256k1_scalar *tweak) {
83
+ nostr_native_secp256k1_gej pt;
84
+ if (nostr_native_secp256k1_scalar_is_zero(tweak)) {
85
+ return 0;
86
+ }
87
+
88
+ nostr_native_secp256k1_gej_set_ge(&pt, key);
89
+ nostr_native_secp256k1_ecmult(&pt, &pt, tweak, NULL);
90
+ nostr_native_secp256k1_ge_set_gej(key, &pt);
91
+ return 1;
92
+ }
93
+
94
+ #endif /* SECP256K1_ECKEY_IMPL_H */
@@ -0,0 +1,64 @@
1
+ /***********************************************************************
2
+ * Copyright (c) 2013, 2014, 2017 Pieter Wuille, Andrew Poelstra *
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
+ #ifndef SECP256K1_ECMULT_H
8
+ #define SECP256K1_ECMULT_H
9
+
10
+ #include "group.h"
11
+ #include "scalar.h"
12
+ #include "scratch.h"
13
+
14
+ #ifndef ECMULT_WINDOW_SIZE
15
+ # define ECMULT_WINDOW_SIZE 15
16
+ # ifdef DEBUG_CONFIG
17
+ # pragma message DEBUG_CONFIG_MSG("ECMULT_WINDOW_SIZE undefined, assuming default value")
18
+ # endif
19
+ #endif
20
+
21
+ #ifdef DEBUG_CONFIG
22
+ # pragma message DEBUG_CONFIG_DEF(ECMULT_WINDOW_SIZE)
23
+ #endif
24
+
25
+ /* No one will ever need more than a window size of 24. The code might
26
+ * be correct for larger values of ECMULT_WINDOW_SIZE but this is not
27
+ * tested.
28
+ *
29
+ * The following limitations are known, and there are probably more:
30
+ * If WINDOW_G > 27 and size_t has 32 bits, then the code is incorrect
31
+ * because the size of the memory object that we allocate (in bytes)
32
+ * will not fit in a size_t.
33
+ * If WINDOW_G > 31 and int has 32 bits, then the code is incorrect
34
+ * because certain expressions will overflow.
35
+ */
36
+ #if ECMULT_WINDOW_SIZE < 2 || ECMULT_WINDOW_SIZE > 24
37
+ # error Set ECMULT_WINDOW_SIZE to an integer in range [2..24].
38
+ #endif
39
+
40
+ /** The number of entries a table with precomputed multiples needs to have. */
41
+ #define ECMULT_TABLE_SIZE(w) ((size_t)1 << ((w)-2))
42
+
43
+ /** Double multiply: R = na*A + ng*G
44
+ *
45
+ * Passing NULL as ng is equivalent to the zero scalar but a tiny bit faster.
46
+ */
47
+ static void nostr_native_secp256k1_ecmult(nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_gej *a, const nostr_native_secp256k1_scalar *na, const nostr_native_secp256k1_scalar *ng);
48
+
49
+ typedef int (nostr_native_secp256k1_ecmult_multi_callback)(nostr_native_secp256k1_scalar *sc, nostr_native_secp256k1_ge *pt, size_t idx, void *data);
50
+
51
+ /**
52
+ * Multi-multiply: R = inp_g_sc * G + sum_i ni * Ai.
53
+ * Chooses the right algorithm for a given number of points and scratch space
54
+ * size. Resets and overwrites the given scratch space. If the points do not
55
+ * fit in the scratch space the algorithm is repeatedly run with batches of
56
+ * points. If no scratch space is given then a simple algorithm is used that
57
+ * simply multiplies the points with the corresponding scalars and adds them up.
58
+ * Returns: 1 on success (including when inp_g_sc is NULL and n is 0)
59
+ * 0 if there is not enough scratch space for a single point or
60
+ * callback returns 0
61
+ */
62
+ static int nostr_native_secp256k1_ecmult_multi_var(const nostr_native_secp256k1_callback* error_callback, nostr_native_secp256k1_scratch *scratch, nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_scalar *inp_g_sc, nostr_native_secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n);
63
+
64
+ #endif /* SECP256K1_ECMULT_H */
@@ -0,0 +1,16 @@
1
+ /*****************************************************************************************************
2
+ * Copyright (c) 2013, 2014, 2017, 2021 Pieter Wuille, Andrew Poelstra, Jonas Nick, Russell O'Connor *
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
+ #ifndef SECP256K1_ECMULT_COMPUTE_TABLE_H
8
+ #define SECP256K1_ECMULT_COMPUTE_TABLE_H
9
+
10
+ /* Construct table of all odd multiples of gen in range 1..(2**(window_g-1)-1). */
11
+ static void nostr_native_secp256k1_ecmult_compute_table(nostr_native_secp256k1_ge_storage* table, int window_g, const nostr_native_secp256k1_gej* gen);
12
+
13
+ /* Like nostr_native_secp256k1_ecmult_compute_table, but one for both gen and gen*2^128. */
14
+ static void nostr_native_secp256k1_ecmult_compute_two_tables(nostr_native_secp256k1_ge_storage* table, nostr_native_secp256k1_ge_storage* table_128, int window_g, const nostr_native_secp256k1_ge* gen);
15
+
16
+ #endif /* SECP256K1_ECMULT_COMPUTE_TABLE_H */
@@ -0,0 +1,49 @@
1
+ /*****************************************************************************************************
2
+ * Copyright (c) 2013, 2014, 2017, 2021 Pieter Wuille, Andrew Poelstra, Jonas Nick, Russell O'Connor *
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
+ #ifndef SECP256K1_ECMULT_COMPUTE_TABLE_IMPL_H
8
+ #define SECP256K1_ECMULT_COMPUTE_TABLE_IMPL_H
9
+
10
+ #include "ecmult_compute_table.h"
11
+ #include "group_impl.h"
12
+ #include "field_impl.h"
13
+ #include "ecmult.h"
14
+ #include "util.h"
15
+
16
+ static void nostr_native_secp256k1_ecmult_compute_table(nostr_native_secp256k1_ge_storage* table, int window_g, const nostr_native_secp256k1_gej* gen) {
17
+ nostr_native_secp256k1_gej gj;
18
+ nostr_native_secp256k1_ge ge, dgen;
19
+ size_t j;
20
+
21
+ gj = *gen;
22
+ nostr_native_secp256k1_ge_set_gej_var(&ge, &gj);
23
+ nostr_native_secp256k1_ge_to_storage(&table[0], &ge);
24
+
25
+ nostr_native_secp256k1_gej_double_var(&gj, gen, NULL);
26
+ nostr_native_secp256k1_ge_set_gej_var(&dgen, &gj);
27
+
28
+ for (j = 1; j < ECMULT_TABLE_SIZE(window_g); ++j) {
29
+ nostr_native_secp256k1_gej_set_ge(&gj, &ge);
30
+ nostr_native_secp256k1_gej_add_ge_var(&gj, &gj, &dgen, NULL);
31
+ nostr_native_secp256k1_ge_set_gej_var(&ge, &gj);
32
+ nostr_native_secp256k1_ge_to_storage(&table[j], &ge);
33
+ }
34
+ }
35
+
36
+ /* Like nostr_native_secp256k1_ecmult_compute_table, but one for both gen and gen*2^128. */
37
+ static void nostr_native_secp256k1_ecmult_compute_two_tables(nostr_native_secp256k1_ge_storage* table, nostr_native_secp256k1_ge_storage* table_128, int window_g, const nostr_native_secp256k1_ge* gen) {
38
+ nostr_native_secp256k1_gej gj;
39
+ int i;
40
+
41
+ nostr_native_secp256k1_gej_set_ge(&gj, gen);
42
+ nostr_native_secp256k1_ecmult_compute_table(table, window_g, &gj);
43
+ for (i = 0; i < 128; ++i) {
44
+ nostr_native_secp256k1_gej_double_var(&gj, &gj, NULL);
45
+ }
46
+ nostr_native_secp256k1_ecmult_compute_table(table_128, window_g, &gj);
47
+ }
48
+
49
+ #endif /* SECP256K1_ECMULT_COMPUTE_TABLE_IMPL_H */
@@ -0,0 +1,38 @@
1
+ /***********************************************************************
2
+ * Copyright (c) 2015 Andrew Poelstra *
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
+ #ifndef SECP256K1_ECMULT_CONST_H
8
+ #define SECP256K1_ECMULT_CONST_H
9
+
10
+ #include "scalar.h"
11
+ #include "group.h"
12
+
13
+ /**
14
+ * Multiply: R = q*A (in constant-time for q)
15
+ */
16
+ static void nostr_native_secp256k1_ecmult_const(nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_ge *a, const nostr_native_secp256k1_scalar *q);
17
+
18
+ /**
19
+ * Same as nostr_native_secp256k1_ecmult_const, but takes in an x coordinate of the base point
20
+ * only, specified as fraction n/d (numerator/denominator). Only the x coordinate of the result is
21
+ * returned.
22
+ *
23
+ * If known_on_curve is 0, a verification is performed that n/d is a valid X
24
+ * coordinate, and 0 is returned if not. Otherwise, 1 is returned.
25
+ *
26
+ * d being NULL is interpreted as d=1. If non-NULL, d must not be zero. q must not be zero.
27
+ *
28
+ * Constant time in the value of q, but not any other inputs.
29
+ */
30
+ static int nostr_native_secp256k1_ecmult_const_xonly(
31
+ nostr_native_secp256k1_fe *r,
32
+ const nostr_native_secp256k1_fe *n,
33
+ const nostr_native_secp256k1_fe *d,
34
+ const nostr_native_secp256k1_scalar *q,
35
+ int known_on_curve
36
+ );
37
+
38
+ #endif /* SECP256K1_ECMULT_CONST_H */