@qntx/nostr-native 0.0.0-stage → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-APACHE +201 -0
- package/LICENSE-MIT +21 -0
- package/NostrNative.podspec +37 -0
- package/README.md +30 -2
- package/android/CMakeLists.txt +38 -0
- package/android/build.gradle +136 -0
- package/android/gradle.properties +4 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/cpp-adapter.cpp +7 -0
- package/android/src/main/java/com/margelo/nitro/nostrnative/NostrNativePackage.java +29 -0
- package/cpp/HybridNostrNative.cpp +52 -0
- package/cpp/HybridNostrNative.hpp +22 -0
- package/cpp/nostr_core.c +128 -0
- package/cpp/nostr_core.h +65 -0
- package/cpp/nostr_native.c +54 -0
- package/cpp/nostr_native.h +47 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.mjs +26 -0
- package/dist/index.mjs.map +1 -0
- package/nitro.json +19 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/NostrNative+autolinking.cmake +81 -0
- package/nitrogen/generated/android/NostrNative+autolinking.gradle +27 -0
- package/nitrogen/generated/android/NostrNativeOnLoad.cpp +49 -0
- package/nitrogen/generated/android/NostrNativeOnLoad.hpp +34 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nostrnative/NostrNativeOnLoad.kt +35 -0
- package/nitrogen/generated/ios/NostrNative+autolinking.rb +62 -0
- package/nitrogen/generated/ios/NostrNative-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/NostrNative-Swift-Cxx-Bridge.hpp +27 -0
- package/nitrogen/generated/ios/NostrNative-Swift-Cxx-Umbrella.hpp +38 -0
- package/nitrogen/generated/ios/NostrNativeAutolinking.mm +35 -0
- package/nitrogen/generated/ios/NostrNativeAutolinking.swift +16 -0
- package/nitrogen/generated/shared/c++/HybridNostrNativeSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridNostrNativeSpec.hpp +64 -0
- package/package.json +86 -4
- package/react-native.config.js +12 -0
- package/vendor/secp256k1/COPYING +19 -0
- package/vendor/secp256k1/include/secp256k1.h +931 -0
- package/vendor/secp256k1/include/secp256k1_ecdh.h +63 -0
- package/vendor/secp256k1/include/secp256k1_ellswift.h +200 -0
- package/vendor/secp256k1/include/secp256k1_extrakeys.h +250 -0
- package/vendor/secp256k1/include/secp256k1_musig.h +588 -0
- package/vendor/secp256k1/include/secp256k1_preallocated.h +134 -0
- package/vendor/secp256k1/include/secp256k1_recovery.h +123 -0
- package/vendor/secp256k1/include/secp256k1_schnorrsig.h +179 -0
- package/vendor/secp256k1/include/secp256k1_silentpayments.h +396 -0
- package/vendor/secp256k1/src/assumptions.h +87 -0
- package/vendor/secp256k1/src/checkmem.h +117 -0
- package/vendor/secp256k1/src/ecdsa.h +21 -0
- package/vendor/secp256k1/src/ecdsa_impl.h +309 -0
- package/vendor/secp256k1/src/eckey.h +28 -0
- package/vendor/secp256k1/src/eckey_impl.h +94 -0
- package/vendor/secp256k1/src/ecmult.h +64 -0
- package/vendor/secp256k1/src/ecmult_compute_table.h +16 -0
- package/vendor/secp256k1/src/ecmult_compute_table_impl.h +49 -0
- package/vendor/secp256k1/src/ecmult_const.h +38 -0
- package/vendor/secp256k1/src/ecmult_const_impl.h +402 -0
- package/vendor/secp256k1/src/ecmult_gen.h +145 -0
- package/vendor/secp256k1/src/ecmult_gen_compute_table.h +14 -0
- package/vendor/secp256k1/src/ecmult_gen_compute_table_impl.h +108 -0
- package/vendor/secp256k1/src/ecmult_gen_impl.h +347 -0
- package/vendor/secp256k1/src/ecmult_impl.h +869 -0
- package/vendor/secp256k1/src/field.h +351 -0
- package/vendor/secp256k1/src/field_10x26.h +57 -0
- package/vendor/secp256k1/src/field_10x26_impl.h +1210 -0
- package/vendor/secp256k1/src/field_5x52.h +62 -0
- package/vendor/secp256k1/src/field_5x52_impl.h +496 -0
- package/vendor/secp256k1/src/field_5x52_int128_impl.h +274 -0
- package/vendor/secp256k1/src/field_impl.h +457 -0
- package/vendor/secp256k1/src/group.h +218 -0
- package/vendor/secp256k1/src/group_impl.h +1014 -0
- package/vendor/secp256k1/src/hash.h +56 -0
- package/vendor/secp256k1/src/hash_impl.h +397 -0
- package/vendor/secp256k1/src/hsort.h +33 -0
- package/vendor/secp256k1/src/hsort_impl.h +125 -0
- package/vendor/secp256k1/src/int128.h +90 -0
- package/vendor/secp256k1/src/int128_impl.h +18 -0
- package/vendor/secp256k1/src/int128_native.h +19 -0
- package/vendor/secp256k1/src/int128_native_impl.h +94 -0
- package/vendor/secp256k1/src/int128_struct.h +14 -0
- package/vendor/secp256k1/src/int128_struct_impl.h +205 -0
- package/vendor/secp256k1/src/modinv32.h +43 -0
- package/vendor/secp256k1/src/modinv32_impl.h +725 -0
- package/vendor/secp256k1/src/modinv64.h +47 -0
- package/vendor/secp256k1/src/modinv64_impl.h +780 -0
- package/vendor/secp256k1/src/modules/extrakeys/main_impl.h +292 -0
- package/vendor/secp256k1/src/modules/schnorrsig/main_impl.h +256 -0
- package/vendor/secp256k1/src/precomputed_ecmult.c +16456 -0
- package/vendor/secp256k1/src/precomputed_ecmult.h +40 -0
- package/vendor/secp256k1/src/precomputed_ecmult_gen.c +1779 -0
- package/vendor/secp256k1/src/precomputed_ecmult_gen.h +28 -0
- package/vendor/secp256k1/src/scalar.h +105 -0
- package/vendor/secp256k1/src/scalar_4x64.h +19 -0
- package/vendor/secp256k1/src/scalar_4x64_impl.h +1005 -0
- package/vendor/secp256k1/src/scalar_8x32.h +19 -0
- package/vendor/secp256k1/src/scalar_8x32_impl.h +820 -0
- package/vendor/secp256k1/src/scalar_impl.h +321 -0
- package/vendor/secp256k1/src/scalar_low.h +24 -0
- package/vendor/secp256k1/src/scalar_low_impl.h +217 -0
- package/vendor/secp256k1/src/scratch.h +44 -0
- package/vendor/secp256k1/src/scratch_impl.h +99 -0
- package/vendor/secp256k1/src/secp256k1.c +855 -0
- package/vendor/secp256k1/src/selftest.h +41 -0
- package/vendor/secp256k1/src/util.h +479 -0
- package/vendor/secp256k1/src/util_local_visibility.h +12 -0
- package/vendor/secp256k1/vendored.json +6 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
#ifndef SECP256K1_ECDH_H
|
|
2
|
+
#define SECP256K1_ECDH_H
|
|
3
|
+
|
|
4
|
+
#include "secp256k1.h"
|
|
5
|
+
|
|
6
|
+
#ifdef __cplusplus
|
|
7
|
+
extern "C" {
|
|
8
|
+
#endif
|
|
9
|
+
|
|
10
|
+
/** A pointer to a function that hashes an EC point to obtain an ECDH secret
|
|
11
|
+
*
|
|
12
|
+
* Returns: 1 if the point was successfully hashed.
|
|
13
|
+
* 0 will cause nostr_native_secp256k1_ecdh to fail and return 0.
|
|
14
|
+
* Other return values are not allowed, and the behaviour of
|
|
15
|
+
* nostr_native_secp256k1_ecdh is undefined for other return values.
|
|
16
|
+
* Out: output: pointer to an array to be filled by the function
|
|
17
|
+
* In: x32: pointer to a 32-byte x coordinate
|
|
18
|
+
* y32: pointer to a 32-byte y coordinate
|
|
19
|
+
* data: arbitrary data pointer that is passed through
|
|
20
|
+
*/
|
|
21
|
+
typedef int (*nostr_native_secp256k1_ecdh_hash_function)(
|
|
22
|
+
unsigned char *output,
|
|
23
|
+
const unsigned char *x32,
|
|
24
|
+
const unsigned char *y32,
|
|
25
|
+
void *data
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
/** An implementation of SHA256 hash function that applies to compressed public key.
|
|
29
|
+
* Populates the output parameter with 32 bytes. */
|
|
30
|
+
SECP256K1_API const nostr_native_secp256k1_ecdh_hash_function nostr_native_secp256k1_ecdh_hash_function_sha256;
|
|
31
|
+
|
|
32
|
+
/** A default ECDH hash function (currently equal to nostr_native_secp256k1_ecdh_hash_function_sha256).
|
|
33
|
+
* Populates the output parameter with 32 bytes. */
|
|
34
|
+
SECP256K1_API const nostr_native_secp256k1_ecdh_hash_function nostr_native_secp256k1_ecdh_hash_function_default;
|
|
35
|
+
|
|
36
|
+
/** Compute an EC Diffie-Hellman secret in constant time
|
|
37
|
+
*
|
|
38
|
+
* Returns: 1: exponentiation was successful
|
|
39
|
+
* 0: scalar was invalid (zero or overflow) or hashfp returned 0
|
|
40
|
+
* Args: ctx: pointer to a context object.
|
|
41
|
+
* Out: output: pointer to an array to be filled by hashfp.
|
|
42
|
+
* In: pubkey: pointer to a nostr_native_secp256k1_pubkey containing an initialized public key.
|
|
43
|
+
* seckey: a 32-byte scalar with which to multiply the point.
|
|
44
|
+
* hashfp: pointer to a hash function. If NULL,
|
|
45
|
+
* nostr_native_secp256k1_ecdh_hash_function_sha256 is used
|
|
46
|
+
* (in which case, 32 bytes will be written to output).
|
|
47
|
+
* data: arbitrary data pointer that is passed through to hashfp
|
|
48
|
+
* (can be NULL for nostr_native_secp256k1_ecdh_hash_function_sha256).
|
|
49
|
+
*/
|
|
50
|
+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ecdh(
|
|
51
|
+
const nostr_native_secp256k1_context *ctx,
|
|
52
|
+
unsigned char *output,
|
|
53
|
+
const nostr_native_secp256k1_pubkey *pubkey,
|
|
54
|
+
const unsigned char *seckey,
|
|
55
|
+
nostr_native_secp256k1_ecdh_hash_function hashfp,
|
|
56
|
+
void *data
|
|
57
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
|
|
58
|
+
|
|
59
|
+
#ifdef __cplusplus
|
|
60
|
+
}
|
|
61
|
+
#endif
|
|
62
|
+
|
|
63
|
+
#endif /* SECP256K1_ECDH_H */
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
#ifndef SECP256K1_ELLSWIFT_H
|
|
2
|
+
#define SECP256K1_ELLSWIFT_H
|
|
3
|
+
|
|
4
|
+
#include "secp256k1.h"
|
|
5
|
+
|
|
6
|
+
#ifdef __cplusplus
|
|
7
|
+
extern "C" {
|
|
8
|
+
#endif
|
|
9
|
+
|
|
10
|
+
/* This module provides an implementation of ElligatorSwift as well as a
|
|
11
|
+
* version of x-only ECDH using it (including compatibility with BIP324).
|
|
12
|
+
*
|
|
13
|
+
* ElligatorSwift is described in https://eprint.iacr.org/2022/759 by
|
|
14
|
+
* Chavez-Saab, Rodriguez-Henriquez, and Tibouchi. It permits encoding
|
|
15
|
+
* uniformly chosen public keys as 64-byte arrays which are indistinguishable
|
|
16
|
+
* from uniformly random arrays.
|
|
17
|
+
*
|
|
18
|
+
* Let f be the function from pairs of field elements to point X coordinates,
|
|
19
|
+
* defined as follows (all operations modulo p = 2^256 - 2^32 - 977)
|
|
20
|
+
* f(u,t):
|
|
21
|
+
* - Let C = 0xa2d2ba93507f1df233770c2a797962cc61f6d15da14ecd47d8d27ae1cd5f852,
|
|
22
|
+
* a square root of -3.
|
|
23
|
+
* - If u=0, set u=1 instead.
|
|
24
|
+
* - If t=0, set t=1 instead.
|
|
25
|
+
* - If u^3 + t^2 + 7 = 0, multiply t by 2.
|
|
26
|
+
* - Let X = (u^3 + 7 - t^2) / (2 * t)
|
|
27
|
+
* - Let Y = (X + t) / (C * u)
|
|
28
|
+
* - Return the first in [u + 4 * Y^2, (-X/Y - u) / 2, (X/Y - u) / 2] that is an
|
|
29
|
+
* X coordinate on the curve (at least one of them is, for any u and t).
|
|
30
|
+
*
|
|
31
|
+
* Then an ElligatorSwift encoding of x consists of the 32-byte big-endian
|
|
32
|
+
* encodings of field elements u and t concatenated, where f(u,t) = x.
|
|
33
|
+
* The encoding algorithm is described in the paper, and effectively picks a
|
|
34
|
+
* uniformly random pair (u,t) among those which encode x.
|
|
35
|
+
*
|
|
36
|
+
* If the Y coordinate is relevant, it is given the same parity as t.
|
|
37
|
+
*
|
|
38
|
+
* Changes w.r.t. the paper:
|
|
39
|
+
* - The u=0, t=0, and u^3+t^2+7=0 conditions result in decoding to the point
|
|
40
|
+
* at infinity in the paper. Here they are remapped to finite points.
|
|
41
|
+
* - The paper uses an additional encoding bit for the parity of y. Here the
|
|
42
|
+
* parity of t is used (negating t does not affect the decoded x coordinate,
|
|
43
|
+
* so this is possible).
|
|
44
|
+
*
|
|
45
|
+
* For mathematical background about the scheme, see the doc/ellswift.md file.
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/** A pointer to a function used by nostr_native_secp256k1_ellswift_xdh to hash the shared X
|
|
49
|
+
* coordinate along with the encoded public keys to a uniform shared secret.
|
|
50
|
+
*
|
|
51
|
+
* Returns: 1 if a shared secret was successfully computed.
|
|
52
|
+
* 0 will cause nostr_native_secp256k1_ellswift_xdh to fail and return 0.
|
|
53
|
+
* Other return values are not allowed, and the behaviour of
|
|
54
|
+
* nostr_native_secp256k1_ellswift_xdh is undefined for other return values.
|
|
55
|
+
* Out: output: pointer to an array to be filled by the function
|
|
56
|
+
* In: x32: pointer to the 32-byte serialized X coordinate
|
|
57
|
+
* of the resulting shared point (will not be NULL)
|
|
58
|
+
* ell_a64: pointer to the 64-byte encoded public key of party A
|
|
59
|
+
* (will not be NULL)
|
|
60
|
+
* ell_b64: pointer to the 64-byte encoded public key of party B
|
|
61
|
+
* (will not be NULL)
|
|
62
|
+
* data: arbitrary data pointer that is passed through
|
|
63
|
+
*/
|
|
64
|
+
typedef int (*nostr_native_secp256k1_ellswift_xdh_hash_function)(
|
|
65
|
+
unsigned char *output,
|
|
66
|
+
const unsigned char *x32,
|
|
67
|
+
const unsigned char *ell_a64,
|
|
68
|
+
const unsigned char *ell_b64,
|
|
69
|
+
void *data
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
/** An implementation of an nostr_native_secp256k1_ellswift_xdh_hash_function which uses
|
|
73
|
+
* SHA256(prefix64 || ell_a64 || ell_b64 || x32), where prefix64 is the 64-byte
|
|
74
|
+
* array pointed to by data. */
|
|
75
|
+
SECP256K1_API const nostr_native_secp256k1_ellswift_xdh_hash_function nostr_native_secp256k1_ellswift_xdh_hash_function_prefix;
|
|
76
|
+
|
|
77
|
+
/** An implementation of an nostr_native_secp256k1_ellswift_xdh_hash_function compatible with
|
|
78
|
+
* BIP324. It returns H_tag(ell_a64 || ell_b64 || x32), where H_tag is the
|
|
79
|
+
* BIP340 tagged hash function with tag "bip324_ellswift_xonly_ecdh". Equivalent
|
|
80
|
+
* to nostr_native_secp256k1_ellswift_xdh_hash_function_prefix with prefix64 set to
|
|
81
|
+
* SHA256("bip324_ellswift_xonly_ecdh")||SHA256("bip324_ellswift_xonly_ecdh").
|
|
82
|
+
* The data argument is ignored. */
|
|
83
|
+
SECP256K1_API const nostr_native_secp256k1_ellswift_xdh_hash_function nostr_native_secp256k1_ellswift_xdh_hash_function_bip324;
|
|
84
|
+
|
|
85
|
+
/** Construct a 64-byte ElligatorSwift encoding of a given pubkey.
|
|
86
|
+
*
|
|
87
|
+
* Returns: 1 always.
|
|
88
|
+
* Args: ctx: pointer to a context object
|
|
89
|
+
* Out: ell64: pointer to a 64-byte array to be filled
|
|
90
|
+
* In: pubkey: pointer to a nostr_native_secp256k1_pubkey containing an
|
|
91
|
+
* initialized public key
|
|
92
|
+
* rnd32: pointer to 32 bytes of randomness
|
|
93
|
+
*
|
|
94
|
+
* It is recommended that rnd32 consists of 32 uniformly random bytes, not
|
|
95
|
+
* known to any adversary trying to detect whether public keys are being
|
|
96
|
+
* encoded, though 16 bytes of randomness (padded to an array of 32 bytes,
|
|
97
|
+
* e.g., with zeros) suffice to make the result indistinguishable from
|
|
98
|
+
* uniform. The randomness in rnd32 must not be a deterministic function of
|
|
99
|
+
* the pubkey (it can be derived from the private key, though).
|
|
100
|
+
*
|
|
101
|
+
* It is not guaranteed that the computed encoding is stable across versions
|
|
102
|
+
* of the library, even if all arguments to this function (including rnd32)
|
|
103
|
+
* are the same.
|
|
104
|
+
*
|
|
105
|
+
* This function runs in variable time.
|
|
106
|
+
*/
|
|
107
|
+
SECP256K1_API int nostr_native_secp256k1_ellswift_encode(
|
|
108
|
+
const nostr_native_secp256k1_context *ctx,
|
|
109
|
+
unsigned char *ell64,
|
|
110
|
+
const nostr_native_secp256k1_pubkey *pubkey,
|
|
111
|
+
const unsigned char *rnd32
|
|
112
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
|
|
113
|
+
|
|
114
|
+
/** Decode a 64-bytes ElligatorSwift encoded public key.
|
|
115
|
+
*
|
|
116
|
+
* Returns: always 1
|
|
117
|
+
* Args: ctx: pointer to a context object
|
|
118
|
+
* Out: pubkey: pointer to a nostr_native_secp256k1_pubkey that will be filled
|
|
119
|
+
* In: ell64: pointer to a 64-byte array to decode
|
|
120
|
+
*
|
|
121
|
+
* This function runs in variable time.
|
|
122
|
+
*/
|
|
123
|
+
SECP256K1_API int nostr_native_secp256k1_ellswift_decode(
|
|
124
|
+
const nostr_native_secp256k1_context *ctx,
|
|
125
|
+
nostr_native_secp256k1_pubkey *pubkey,
|
|
126
|
+
const unsigned char *ell64
|
|
127
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
|
128
|
+
|
|
129
|
+
/** Compute an ElligatorSwift public key for a secret key.
|
|
130
|
+
*
|
|
131
|
+
* Returns: 1: secret was valid, public key was stored.
|
|
132
|
+
* 0: secret was invalid, try again.
|
|
133
|
+
* Args: ctx: pointer to a context object (not nostr_native_secp256k1_context_static)
|
|
134
|
+
* Out: ell64: pointer to a 64-byte array to receive the ElligatorSwift
|
|
135
|
+
* public key
|
|
136
|
+
* In: seckey32: pointer to a 32-byte secret key
|
|
137
|
+
* auxrnd32: (optional) pointer to 32 bytes of randomness
|
|
138
|
+
*
|
|
139
|
+
* Constant time in seckey and auxrnd32, but not in the resulting public key.
|
|
140
|
+
*
|
|
141
|
+
* It is recommended that auxrnd32 contains 32 uniformly random bytes, though
|
|
142
|
+
* it is optional (and does result in encodings that are indistinguishable from
|
|
143
|
+
* uniform even without any auxrnd32). It differs from the (mandatory) rnd32
|
|
144
|
+
* argument to nostr_native_secp256k1_ellswift_encode in this regard.
|
|
145
|
+
*
|
|
146
|
+
* This function can be used instead of calling nostr_native_secp256k1_ec_pubkey_create
|
|
147
|
+
* followed by nostr_native_secp256k1_ellswift_encode. It is safer, as it uses the secret
|
|
148
|
+
* key as entropy for the encoding (supplemented with auxrnd32, if provided).
|
|
149
|
+
*
|
|
150
|
+
* Like nostr_native_secp256k1_ellswift_encode, this function does not guarantee that the
|
|
151
|
+
* computed encoding is stable across versions of the library, even if all
|
|
152
|
+
* arguments (including auxrnd32) are the same.
|
|
153
|
+
*/
|
|
154
|
+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ellswift_create(
|
|
155
|
+
const nostr_native_secp256k1_context *ctx,
|
|
156
|
+
unsigned char *ell64,
|
|
157
|
+
const unsigned char *seckey32,
|
|
158
|
+
const unsigned char *auxrnd32
|
|
159
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
|
160
|
+
|
|
161
|
+
/** Given a private key, and ElligatorSwift public keys sent in both directions,
|
|
162
|
+
* compute a shared secret using x-only Elliptic Curve Diffie-Hellman (ECDH).
|
|
163
|
+
*
|
|
164
|
+
* Returns: 1: shared secret was successfully computed
|
|
165
|
+
* 0: secret was invalid or hashfp returned 0
|
|
166
|
+
* Args: ctx: pointer to a context object.
|
|
167
|
+
* Out: output: pointer to an array to be filled by hashfp.
|
|
168
|
+
* In: ell_a64: pointer to the 64-byte encoded public key of party A
|
|
169
|
+
* (will not be NULL)
|
|
170
|
+
* ell_b64: pointer to the 64-byte encoded public key of party B
|
|
171
|
+
* (will not be NULL)
|
|
172
|
+
* seckey32: pointer to our 32-byte secret key
|
|
173
|
+
* party: boolean indicating which party we are: zero if we are
|
|
174
|
+
* party A, non-zero if we are party B. seckey32 must be
|
|
175
|
+
* the private key corresponding to that party's ell_?64.
|
|
176
|
+
* This correspondence is not checked.
|
|
177
|
+
* hashfp: pointer to a hash function.
|
|
178
|
+
* data: arbitrary data pointer passed through to hashfp.
|
|
179
|
+
*
|
|
180
|
+
* Constant time in seckey32.
|
|
181
|
+
*
|
|
182
|
+
* This function is more efficient than decoding the public keys, and performing
|
|
183
|
+
* ECDH on them.
|
|
184
|
+
*/
|
|
185
|
+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_ellswift_xdh(
|
|
186
|
+
const nostr_native_secp256k1_context *ctx,
|
|
187
|
+
unsigned char *output,
|
|
188
|
+
const unsigned char *ell_a64,
|
|
189
|
+
const unsigned char *ell_b64,
|
|
190
|
+
const unsigned char *seckey32,
|
|
191
|
+
int party,
|
|
192
|
+
nostr_native_secp256k1_ellswift_xdh_hash_function hashfp,
|
|
193
|
+
void *data
|
|
194
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(7);
|
|
195
|
+
|
|
196
|
+
#ifdef __cplusplus
|
|
197
|
+
}
|
|
198
|
+
#endif
|
|
199
|
+
|
|
200
|
+
#endif /* SECP256K1_ELLSWIFT_H */
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
#ifndef SECP256K1_EXTRAKEYS_H
|
|
2
|
+
#define SECP256K1_EXTRAKEYS_H
|
|
3
|
+
|
|
4
|
+
#include "secp256k1.h"
|
|
5
|
+
|
|
6
|
+
#ifdef __cplusplus
|
|
7
|
+
extern "C" {
|
|
8
|
+
#endif
|
|
9
|
+
|
|
10
|
+
/** Opaque data structure that holds a parsed and valid "x-only" public key.
|
|
11
|
+
* An x-only pubkey encodes a point whose Y coordinate is even. It is
|
|
12
|
+
* serialized using only its X coordinate (32 bytes). See BIP-340 for more
|
|
13
|
+
* information about x-only pubkeys.
|
|
14
|
+
*
|
|
15
|
+
* The exact representation of data inside is implementation defined and not
|
|
16
|
+
* guaranteed to be portable between different platforms or versions. It is
|
|
17
|
+
* however guaranteed to be 64 bytes in size, and can be safely copied/moved.
|
|
18
|
+
* If you need to convert to a format suitable for storage, transmission, use
|
|
19
|
+
* use nostr_native_secp256k1_xonly_pubkey_serialize and nostr_native_secp256k1_xonly_pubkey_parse. To
|
|
20
|
+
* compare keys, use nostr_native_secp256k1_xonly_pubkey_cmp.
|
|
21
|
+
*/
|
|
22
|
+
typedef struct nostr_native_secp256k1_xonly_pubkey {
|
|
23
|
+
unsigned char data[64];
|
|
24
|
+
} nostr_native_secp256k1_xonly_pubkey;
|
|
25
|
+
|
|
26
|
+
/** Opaque data structure that holds a keypair consisting of a secret and a
|
|
27
|
+
* public key.
|
|
28
|
+
*
|
|
29
|
+
* The exact representation of data inside is implementation defined and not
|
|
30
|
+
* guaranteed to be portable between different platforms or versions. It is
|
|
31
|
+
* however guaranteed to be 96 bytes in size, and can be safely copied/moved.
|
|
32
|
+
*/
|
|
33
|
+
typedef struct nostr_native_secp256k1_keypair {
|
|
34
|
+
unsigned char data[96];
|
|
35
|
+
} nostr_native_secp256k1_keypair;
|
|
36
|
+
|
|
37
|
+
/** Parse a 32-byte sequence into a xonly_pubkey object.
|
|
38
|
+
*
|
|
39
|
+
* Returns: 1 if the public key was fully valid.
|
|
40
|
+
* 0 if the public key could not be parsed or is invalid.
|
|
41
|
+
*
|
|
42
|
+
* Args: ctx: pointer to a context object.
|
|
43
|
+
* Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a
|
|
44
|
+
* parsed version of input. If not, it's set to an invalid value.
|
|
45
|
+
* In: input32: pointer to a serialized xonly_pubkey.
|
|
46
|
+
*/
|
|
47
|
+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_xonly_pubkey_parse(
|
|
48
|
+
const nostr_native_secp256k1_context *ctx,
|
|
49
|
+
nostr_native_secp256k1_xonly_pubkey *pubkey,
|
|
50
|
+
const unsigned char *input32
|
|
51
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
|
52
|
+
|
|
53
|
+
/** Serialize an xonly_pubkey object into a 32-byte sequence.
|
|
54
|
+
*
|
|
55
|
+
* Returns: 1 always.
|
|
56
|
+
*
|
|
57
|
+
* Args: ctx: pointer to a context object.
|
|
58
|
+
* Out: output32: pointer to a 32-byte array to place the serialized key in.
|
|
59
|
+
* In: pubkey: pointer to a nostr_native_secp256k1_xonly_pubkey containing an initialized public key.
|
|
60
|
+
*/
|
|
61
|
+
SECP256K1_API int nostr_native_secp256k1_xonly_pubkey_serialize(
|
|
62
|
+
const nostr_native_secp256k1_context *ctx,
|
|
63
|
+
unsigned char *output32,
|
|
64
|
+
const nostr_native_secp256k1_xonly_pubkey *pubkey
|
|
65
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
|
66
|
+
|
|
67
|
+
/** Compare two x-only public keys using lexicographic order
|
|
68
|
+
*
|
|
69
|
+
* Returns: <0 if the first public key is less than the second
|
|
70
|
+
* >0 if the first public key is greater than the second
|
|
71
|
+
* 0 if the two public keys are equal
|
|
72
|
+
* Args: ctx: pointer to a context object.
|
|
73
|
+
* In: pubkey1: first public key to compare
|
|
74
|
+
* pubkey2: second public key to compare
|
|
75
|
+
*/
|
|
76
|
+
SECP256K1_API int nostr_native_secp256k1_xonly_pubkey_cmp(
|
|
77
|
+
const nostr_native_secp256k1_context *ctx,
|
|
78
|
+
const nostr_native_secp256k1_xonly_pubkey *pk1,
|
|
79
|
+
const nostr_native_secp256k1_xonly_pubkey *pk2
|
|
80
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
|
81
|
+
|
|
82
|
+
/** Converts a nostr_native_secp256k1_pubkey into a nostr_native_secp256k1_xonly_pubkey.
|
|
83
|
+
*
|
|
84
|
+
* Returns: 1 always.
|
|
85
|
+
*
|
|
86
|
+
* Args: ctx: pointer to a context object.
|
|
87
|
+
* Out: xonly_pubkey: pointer to an x-only public key object for placing the converted public key.
|
|
88
|
+
* pk_parity: Ignored if NULL. Otherwise, pointer to an integer that
|
|
89
|
+
* will be set to 1 if the point encoded by xonly_pubkey is
|
|
90
|
+
* the negation of the pubkey and set to 0 otherwise.
|
|
91
|
+
* In: pubkey: pointer to a public key that is converted.
|
|
92
|
+
*/
|
|
93
|
+
SECP256K1_API int nostr_native_secp256k1_xonly_pubkey_from_pubkey(
|
|
94
|
+
const nostr_native_secp256k1_context *ctx,
|
|
95
|
+
nostr_native_secp256k1_xonly_pubkey *xonly_pubkey,
|
|
96
|
+
int *pk_parity,
|
|
97
|
+
const nostr_native_secp256k1_pubkey *pubkey
|
|
98
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4);
|
|
99
|
+
|
|
100
|
+
/** Tweak an x-only public key by adding the generator multiplied with tweak32
|
|
101
|
+
* to it.
|
|
102
|
+
*
|
|
103
|
+
* Note that the resulting point can not in general be represented by an x-only
|
|
104
|
+
* pubkey because it may have an odd Y coordinate. Instead, the output_pubkey
|
|
105
|
+
* is a normal nostr_native_secp256k1_pubkey.
|
|
106
|
+
*
|
|
107
|
+
* Returns: 0 if the arguments are invalid or the resulting public key would be
|
|
108
|
+
* invalid (only when the tweak is the negation of the corresponding
|
|
109
|
+
* secret key). 1 otherwise.
|
|
110
|
+
*
|
|
111
|
+
* Args: ctx: pointer to a context object.
|
|
112
|
+
* Out: output_pubkey: pointer to a public key to store the result. Will be set
|
|
113
|
+
* to an invalid value if this function returns 0.
|
|
114
|
+
* In: internal_pubkey: pointer to an x-only pubkey to apply the tweak to.
|
|
115
|
+
* tweak32: pointer to a 32-byte tweak, which must be valid
|
|
116
|
+
* according to nostr_native_secp256k1_ec_seckey_verify or 32 zero
|
|
117
|
+
* bytes. For uniformly random 32-byte tweaks, the chance of
|
|
118
|
+
* being invalid is negligible (around 1 in 2^128).
|
|
119
|
+
*/
|
|
120
|
+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_xonly_pubkey_tweak_add(
|
|
121
|
+
const nostr_native_secp256k1_context *ctx,
|
|
122
|
+
nostr_native_secp256k1_pubkey *output_pubkey,
|
|
123
|
+
const nostr_native_secp256k1_xonly_pubkey *internal_pubkey,
|
|
124
|
+
const unsigned char *tweak32
|
|
125
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
|
|
126
|
+
|
|
127
|
+
/** Checks that a tweaked pubkey is the result of calling
|
|
128
|
+
* nostr_native_secp256k1_xonly_pubkey_tweak_add with internal_pubkey and tweak32.
|
|
129
|
+
*
|
|
130
|
+
* The tweaked pubkey is represented by its 32-byte x-only serialization and
|
|
131
|
+
* its pk_parity, which can both be obtained by converting the result of
|
|
132
|
+
* tweak_add to a nostr_native_secp256k1_xonly_pubkey.
|
|
133
|
+
*
|
|
134
|
+
* Note that this alone does _not_ verify that the tweaked pubkey is a
|
|
135
|
+
* commitment. If the tweak is not chosen in a specific way, the tweaked pubkey
|
|
136
|
+
* can easily be the result of a different internal_pubkey and tweak.
|
|
137
|
+
*
|
|
138
|
+
* Returns: 0 if the arguments are invalid or the tweaked pubkey is not the
|
|
139
|
+
* result of tweaking the internal_pubkey with tweak32. 1 otherwise.
|
|
140
|
+
* Args: ctx: pointer to a context object.
|
|
141
|
+
* In: tweaked_pubkey32: pointer to a serialized xonly_pubkey.
|
|
142
|
+
* tweaked_pk_parity: the parity of the tweaked pubkey (whose serialization
|
|
143
|
+
* is passed in as tweaked_pubkey32). This must match the
|
|
144
|
+
* pk_parity value that is returned when calling
|
|
145
|
+
* nostr_native_secp256k1_xonly_pubkey with the tweaked pubkey, or
|
|
146
|
+
* this function will fail.
|
|
147
|
+
* internal_pubkey: pointer to an x-only public key object to apply the tweak to.
|
|
148
|
+
* tweak32: pointer to a 32-byte tweak.
|
|
149
|
+
*/
|
|
150
|
+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_xonly_pubkey_tweak_add_check(
|
|
151
|
+
const nostr_native_secp256k1_context *ctx,
|
|
152
|
+
const unsigned char *tweaked_pubkey32,
|
|
153
|
+
int tweaked_pk_parity,
|
|
154
|
+
const nostr_native_secp256k1_xonly_pubkey *internal_pubkey,
|
|
155
|
+
const unsigned char *tweak32
|
|
156
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5);
|
|
157
|
+
|
|
158
|
+
/** Compute the keypair for a valid secret key.
|
|
159
|
+
*
|
|
160
|
+
* See the documentation of `nostr_native_secp256k1_ec_seckey_verify` for more information
|
|
161
|
+
* about the validity of secret keys.
|
|
162
|
+
*
|
|
163
|
+
* Returns: 1: secret key is valid
|
|
164
|
+
* 0: secret key is invalid
|
|
165
|
+
* Args: ctx: pointer to a context object (not nostr_native_secp256k1_context_static).
|
|
166
|
+
* Out: keypair: pointer to the created keypair.
|
|
167
|
+
* In: seckey: pointer to a 32-byte secret key.
|
|
168
|
+
*/
|
|
169
|
+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_keypair_create(
|
|
170
|
+
const nostr_native_secp256k1_context *ctx,
|
|
171
|
+
nostr_native_secp256k1_keypair *keypair,
|
|
172
|
+
const unsigned char *seckey
|
|
173
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
|
174
|
+
|
|
175
|
+
/** Get the secret key from a keypair.
|
|
176
|
+
*
|
|
177
|
+
* Returns: 1 always.
|
|
178
|
+
* Args: ctx: pointer to a context object.
|
|
179
|
+
* Out: seckey: pointer to a 32-byte buffer for the secret key.
|
|
180
|
+
* In: keypair: pointer to a keypair.
|
|
181
|
+
*/
|
|
182
|
+
SECP256K1_API int nostr_native_secp256k1_keypair_sec(
|
|
183
|
+
const nostr_native_secp256k1_context *ctx,
|
|
184
|
+
unsigned char *seckey,
|
|
185
|
+
const nostr_native_secp256k1_keypair *keypair
|
|
186
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
|
187
|
+
|
|
188
|
+
/** Get the public key from a keypair.
|
|
189
|
+
*
|
|
190
|
+
* Returns: 1 always.
|
|
191
|
+
* Args: ctx: pointer to a context object.
|
|
192
|
+
* Out: pubkey: pointer to a pubkey object, set to the keypair public key.
|
|
193
|
+
* In: keypair: pointer to a keypair.
|
|
194
|
+
*/
|
|
195
|
+
SECP256K1_API int nostr_native_secp256k1_keypair_pub(
|
|
196
|
+
const nostr_native_secp256k1_context *ctx,
|
|
197
|
+
nostr_native_secp256k1_pubkey *pubkey,
|
|
198
|
+
const nostr_native_secp256k1_keypair *keypair
|
|
199
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
|
200
|
+
|
|
201
|
+
/** Get the x-only public key from a keypair.
|
|
202
|
+
*
|
|
203
|
+
* This is the same as calling nostr_native_secp256k1_keypair_pub and then
|
|
204
|
+
* nostr_native_secp256k1_xonly_pubkey_from_pubkey.
|
|
205
|
+
*
|
|
206
|
+
* Returns: 1 always.
|
|
207
|
+
* Args: ctx: pointer to a context object.
|
|
208
|
+
* Out: pubkey: pointer to an xonly_pubkey object, set to the keypair
|
|
209
|
+
* public key after converting it to an xonly_pubkey.
|
|
210
|
+
* pk_parity: Ignored if NULL. Otherwise, pointer to an integer that will be set to the
|
|
211
|
+
* pk_parity argument of nostr_native_secp256k1_xonly_pubkey_from_pubkey.
|
|
212
|
+
* In: keypair: pointer to a keypair.
|
|
213
|
+
*/
|
|
214
|
+
SECP256K1_API int nostr_native_secp256k1_keypair_xonly_pub(
|
|
215
|
+
const nostr_native_secp256k1_context *ctx,
|
|
216
|
+
nostr_native_secp256k1_xonly_pubkey *pubkey,
|
|
217
|
+
int *pk_parity,
|
|
218
|
+
const nostr_native_secp256k1_keypair *keypair
|
|
219
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4);
|
|
220
|
+
|
|
221
|
+
/** Tweak a keypair by adding tweak32 to the secret key and updating the public
|
|
222
|
+
* key accordingly.
|
|
223
|
+
*
|
|
224
|
+
* Calling this function and then nostr_native_secp256k1_keypair_pub results in the same
|
|
225
|
+
* public key as calling nostr_native_secp256k1_keypair_xonly_pub and then
|
|
226
|
+
* nostr_native_secp256k1_xonly_pubkey_tweak_add.
|
|
227
|
+
*
|
|
228
|
+
* Returns: 0 if the arguments are invalid or the resulting keypair would be
|
|
229
|
+
* invalid (only when the tweak is the negation of the keypair's
|
|
230
|
+
* secret key). 1 otherwise.
|
|
231
|
+
*
|
|
232
|
+
* Args: ctx: pointer to a context object.
|
|
233
|
+
* In/Out: keypair: pointer to a keypair to apply the tweak to. Will be set to
|
|
234
|
+
* an invalid value if this function returns 0.
|
|
235
|
+
* In: tweak32: pointer to a 32-byte tweak, which must be valid according to
|
|
236
|
+
* nostr_native_secp256k1_ec_seckey_verify or 32 zero bytes. For uniformly
|
|
237
|
+
* random 32-byte tweaks, the chance of being invalid is
|
|
238
|
+
* negligible (around 1 in 2^128).
|
|
239
|
+
*/
|
|
240
|
+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int nostr_native_secp256k1_keypair_xonly_tweak_add(
|
|
241
|
+
const nostr_native_secp256k1_context *ctx,
|
|
242
|
+
nostr_native_secp256k1_keypair *keypair,
|
|
243
|
+
const unsigned char *tweak32
|
|
244
|
+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
|
245
|
+
|
|
246
|
+
#ifdef __cplusplus
|
|
247
|
+
}
|
|
248
|
+
#endif
|
|
249
|
+
|
|
250
|
+
#endif /* SECP256K1_EXTRAKEYS_H */
|