@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,90 @@
|
|
|
1
|
+
#ifndef SECP256K1_INT128_H
|
|
2
|
+
#define SECP256K1_INT128_H
|
|
3
|
+
|
|
4
|
+
#include "util.h"
|
|
5
|
+
|
|
6
|
+
#if defined(SECP256K1_WIDEMUL_INT128)
|
|
7
|
+
# if defined(SECP256K1_INT128_NATIVE)
|
|
8
|
+
# include "int128_native.h"
|
|
9
|
+
# elif defined(SECP256K1_INT128_STRUCT)
|
|
10
|
+
# include "int128_struct.h"
|
|
11
|
+
# else
|
|
12
|
+
# error "Please select int128 implementation"
|
|
13
|
+
# endif
|
|
14
|
+
|
|
15
|
+
/* Construct an unsigned 128-bit value from a high and a low 64-bit value. */
|
|
16
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_load(nostr_native_secp256k1_uint128 *r, uint64_t hi, uint64_t lo);
|
|
17
|
+
|
|
18
|
+
/* Multiply two unsigned 64-bit values a and b and write the result to r. */
|
|
19
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_mul(nostr_native_secp256k1_uint128 *r, uint64_t a, uint64_t b);
|
|
20
|
+
|
|
21
|
+
/* Multiply two unsigned 64-bit values a and b and add the result to r.
|
|
22
|
+
* The final result is taken modulo 2^128.
|
|
23
|
+
*/
|
|
24
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_accum_mul(nostr_native_secp256k1_uint128 *r, uint64_t a, uint64_t b);
|
|
25
|
+
|
|
26
|
+
/* Add an unsigned 64-bit value a to r.
|
|
27
|
+
* The final result is taken modulo 2^128.
|
|
28
|
+
*/
|
|
29
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_accum_u64(nostr_native_secp256k1_uint128 *r, uint64_t a);
|
|
30
|
+
|
|
31
|
+
/* Unsigned (logical) right shift.
|
|
32
|
+
* Non-constant time in n.
|
|
33
|
+
*/
|
|
34
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_rshift(nostr_native_secp256k1_uint128 *r, unsigned int n);
|
|
35
|
+
|
|
36
|
+
/* Return the low 64-bits of a 128-bit value as an unsigned 64-bit value. */
|
|
37
|
+
static SECP256K1_INLINE uint64_t nostr_native_secp256k1_u128_to_u64(const nostr_native_secp256k1_uint128 *a);
|
|
38
|
+
|
|
39
|
+
/* Return the high 64-bits of a 128-bit value as an unsigned 64-bit value. */
|
|
40
|
+
static SECP256K1_INLINE uint64_t nostr_native_secp256k1_u128_hi_u64(const nostr_native_secp256k1_uint128 *a);
|
|
41
|
+
|
|
42
|
+
/* Write an unsigned 64-bit value to r. */
|
|
43
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_from_u64(nostr_native_secp256k1_uint128 *r, uint64_t a);
|
|
44
|
+
|
|
45
|
+
/* Tests if r is strictly less than to 2^n.
|
|
46
|
+
* n must be strictly less than 128.
|
|
47
|
+
*/
|
|
48
|
+
static SECP256K1_INLINE int nostr_native_secp256k1_u128_check_bits(const nostr_native_secp256k1_uint128 *r, unsigned int n);
|
|
49
|
+
|
|
50
|
+
/* Construct an signed 128-bit value from a high and a low 64-bit value. */
|
|
51
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_load(nostr_native_secp256k1_int128 *r, int64_t hi, uint64_t lo);
|
|
52
|
+
|
|
53
|
+
/* Multiply two signed 64-bit values a and b and write the result to r. */
|
|
54
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_mul(nostr_native_secp256k1_int128 *r, int64_t a, int64_t b);
|
|
55
|
+
|
|
56
|
+
/* Multiply two signed 64-bit values a and b and add the result to r.
|
|
57
|
+
* Overflow or underflow from the addition is undefined behaviour.
|
|
58
|
+
*/
|
|
59
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_accum_mul(nostr_native_secp256k1_int128 *r, int64_t a, int64_t b);
|
|
60
|
+
|
|
61
|
+
/* Compute a*d - b*c from signed 64-bit values and write the result to r. */
|
|
62
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_det(nostr_native_secp256k1_int128 *r, int64_t a, int64_t b, int64_t c, int64_t d);
|
|
63
|
+
|
|
64
|
+
/* Signed (arithmetic) right shift.
|
|
65
|
+
* Non-constant time in b.
|
|
66
|
+
*/
|
|
67
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_rshift(nostr_native_secp256k1_int128 *r, unsigned int b);
|
|
68
|
+
|
|
69
|
+
/* Return the input value modulo 2^64. */
|
|
70
|
+
static SECP256K1_INLINE uint64_t nostr_native_secp256k1_i128_to_u64(const nostr_native_secp256k1_int128 *a);
|
|
71
|
+
|
|
72
|
+
/* Return the value as a signed 64-bit value.
|
|
73
|
+
* Requires the input to be between INT64_MIN and INT64_MAX.
|
|
74
|
+
*/
|
|
75
|
+
static SECP256K1_INLINE int64_t nostr_native_secp256k1_i128_to_i64(const nostr_native_secp256k1_int128 *a);
|
|
76
|
+
|
|
77
|
+
/* Write a signed 64-bit value to r. */
|
|
78
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_from_i64(nostr_native_secp256k1_int128 *r, int64_t a);
|
|
79
|
+
|
|
80
|
+
/* Compare two 128-bit values for equality. */
|
|
81
|
+
static SECP256K1_INLINE int nostr_native_secp256k1_i128_eq_var(const nostr_native_secp256k1_int128 *a, const nostr_native_secp256k1_int128 *b);
|
|
82
|
+
|
|
83
|
+
/* Tests if r is equal to sign*2^n (sign must be 1 or -1).
|
|
84
|
+
* n must be strictly less than 127.
|
|
85
|
+
*/
|
|
86
|
+
static SECP256K1_INLINE int nostr_native_secp256k1_i128_check_pow2(const nostr_native_secp256k1_int128 *r, unsigned int n, int sign);
|
|
87
|
+
|
|
88
|
+
#endif
|
|
89
|
+
|
|
90
|
+
#endif
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#ifndef SECP256K1_INT128_IMPL_H
|
|
2
|
+
#define SECP256K1_INT128_IMPL_H
|
|
3
|
+
|
|
4
|
+
#include "util.h"
|
|
5
|
+
|
|
6
|
+
#include "int128.h"
|
|
7
|
+
|
|
8
|
+
#if defined(SECP256K1_WIDEMUL_INT128)
|
|
9
|
+
# if defined(SECP256K1_INT128_NATIVE)
|
|
10
|
+
# include "int128_native_impl.h"
|
|
11
|
+
# elif defined(SECP256K1_INT128_STRUCT)
|
|
12
|
+
# include "int128_struct_impl.h"
|
|
13
|
+
# else
|
|
14
|
+
# error "Please select int128 implementation"
|
|
15
|
+
# endif
|
|
16
|
+
#endif
|
|
17
|
+
|
|
18
|
+
#endif
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#ifndef SECP256K1_INT128_NATIVE_H
|
|
2
|
+
#define SECP256K1_INT128_NATIVE_H
|
|
3
|
+
|
|
4
|
+
#include <stdint.h>
|
|
5
|
+
#include "util.h"
|
|
6
|
+
|
|
7
|
+
#if !defined(UINT128_MAX) && defined(__SIZEOF_INT128__)
|
|
8
|
+
SECP256K1_GNUC_EXT typedef unsigned __int128 uint128_t;
|
|
9
|
+
SECP256K1_GNUC_EXT typedef __int128 int128_t;
|
|
10
|
+
# define UINT128_MAX ((uint128_t)(-1))
|
|
11
|
+
# define INT128_MAX ((int128_t)(UINT128_MAX >> 1))
|
|
12
|
+
# define INT128_MIN (-INT128_MAX - 1)
|
|
13
|
+
/* No (U)INT128_C macros because compilers providing __int128 do not support 128-bit literals. */
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
typedef uint128_t nostr_native_secp256k1_uint128;
|
|
17
|
+
typedef int128_t nostr_native_secp256k1_int128;
|
|
18
|
+
|
|
19
|
+
#endif
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
#ifndef SECP256K1_INT128_NATIVE_IMPL_H
|
|
2
|
+
#define SECP256K1_INT128_NATIVE_IMPL_H
|
|
3
|
+
|
|
4
|
+
#include "int128.h"
|
|
5
|
+
#include "util.h"
|
|
6
|
+
|
|
7
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_load(nostr_native_secp256k1_uint128 *r, uint64_t hi, uint64_t lo) {
|
|
8
|
+
*r = (((uint128_t)hi) << 64) + lo;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_mul(nostr_native_secp256k1_uint128 *r, uint64_t a, uint64_t b) {
|
|
12
|
+
*r = (uint128_t)a * b;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_accum_mul(nostr_native_secp256k1_uint128 *r, uint64_t a, uint64_t b) {
|
|
16
|
+
*r += (uint128_t)a * b;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_accum_u64(nostr_native_secp256k1_uint128 *r, uint64_t a) {
|
|
20
|
+
*r += a;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_rshift(nostr_native_secp256k1_uint128 *r, unsigned int n) {
|
|
24
|
+
VERIFY_CHECK(n < 128);
|
|
25
|
+
*r >>= n;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static SECP256K1_INLINE uint64_t nostr_native_secp256k1_u128_to_u64(const nostr_native_secp256k1_uint128 *a) {
|
|
29
|
+
return (uint64_t)(*a);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static SECP256K1_INLINE uint64_t nostr_native_secp256k1_u128_hi_u64(const nostr_native_secp256k1_uint128 *a) {
|
|
33
|
+
return (uint64_t)(*a >> 64);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_from_u64(nostr_native_secp256k1_uint128 *r, uint64_t a) {
|
|
37
|
+
*r = a;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static SECP256K1_INLINE int nostr_native_secp256k1_u128_check_bits(const nostr_native_secp256k1_uint128 *r, unsigned int n) {
|
|
41
|
+
VERIFY_CHECK(n < 128);
|
|
42
|
+
return (*r >> n == 0);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_load(nostr_native_secp256k1_int128 *r, int64_t hi, uint64_t lo) {
|
|
46
|
+
*r = (((uint128_t)(uint64_t)hi) << 64) + lo;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_mul(nostr_native_secp256k1_int128 *r, int64_t a, int64_t b) {
|
|
50
|
+
*r = (int128_t)a * b;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_accum_mul(nostr_native_secp256k1_int128 *r, int64_t a, int64_t b) {
|
|
54
|
+
int128_t ab = (int128_t)a * b;
|
|
55
|
+
VERIFY_CHECK(0 <= ab ? *r <= INT128_MAX - ab : INT128_MIN - ab <= *r);
|
|
56
|
+
*r += ab;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_det(nostr_native_secp256k1_int128 *r, int64_t a, int64_t b, int64_t c, int64_t d) {
|
|
60
|
+
int128_t ad = (int128_t)a * d;
|
|
61
|
+
int128_t bc = (int128_t)b * c;
|
|
62
|
+
VERIFY_CHECK(0 <= bc ? INT128_MIN + bc <= ad : ad <= INT128_MAX + bc);
|
|
63
|
+
*r = ad - bc;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_rshift(nostr_native_secp256k1_int128 *r, unsigned int n) {
|
|
67
|
+
VERIFY_CHECK(n < 128);
|
|
68
|
+
*r >>= n;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static SECP256K1_INLINE uint64_t nostr_native_secp256k1_i128_to_u64(const nostr_native_secp256k1_int128 *a) {
|
|
72
|
+
return (uint64_t)*a;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static SECP256K1_INLINE int64_t nostr_native_secp256k1_i128_to_i64(const nostr_native_secp256k1_int128 *a) {
|
|
76
|
+
VERIFY_CHECK(INT64_MIN <= *a && *a <= INT64_MAX);
|
|
77
|
+
return *a;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_from_i64(nostr_native_secp256k1_int128 *r, int64_t a) {
|
|
81
|
+
*r = a;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static SECP256K1_INLINE int nostr_native_secp256k1_i128_eq_var(const nostr_native_secp256k1_int128 *a, const nostr_native_secp256k1_int128 *b) {
|
|
85
|
+
return *a == *b;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static SECP256K1_INLINE int nostr_native_secp256k1_i128_check_pow2(const nostr_native_secp256k1_int128 *r, unsigned int n, int sign) {
|
|
89
|
+
VERIFY_CHECK(n < 127);
|
|
90
|
+
VERIFY_CHECK(sign == 1 || sign == -1);
|
|
91
|
+
return (*r == (int128_t)((uint128_t)sign << n));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#endif
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#ifndef SECP256K1_INT128_STRUCT_H
|
|
2
|
+
#define SECP256K1_INT128_STRUCT_H
|
|
3
|
+
|
|
4
|
+
#include <stdint.h>
|
|
5
|
+
#include "util.h"
|
|
6
|
+
|
|
7
|
+
typedef struct {
|
|
8
|
+
uint64_t lo;
|
|
9
|
+
uint64_t hi;
|
|
10
|
+
} nostr_native_secp256k1_uint128;
|
|
11
|
+
|
|
12
|
+
typedef nostr_native_secp256k1_uint128 nostr_native_secp256k1_int128;
|
|
13
|
+
|
|
14
|
+
#endif
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
#ifndef SECP256K1_INT128_STRUCT_IMPL_H
|
|
2
|
+
#define SECP256K1_INT128_STRUCT_IMPL_H
|
|
3
|
+
|
|
4
|
+
#include "int128.h"
|
|
5
|
+
#include "util.h"
|
|
6
|
+
|
|
7
|
+
#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM64)) /* MSVC */
|
|
8
|
+
# include <intrin.h>
|
|
9
|
+
# if defined(_M_ARM64) || defined(SECP256K1_MSVC_MULH_TEST_OVERRIDE)
|
|
10
|
+
/* On ARM64 MSVC, use __(u)mulh for the upper half of 64x64 multiplications.
|
|
11
|
+
(Define SECP256K1_MSVC_MULH_TEST_OVERRIDE to test this code path on X64,
|
|
12
|
+
which supports both __(u)mulh and _umul128.) */
|
|
13
|
+
# if defined(SECP256K1_MSVC_MULH_TEST_OVERRIDE)
|
|
14
|
+
# pragma message(__FILE__ ": SECP256K1_MSVC_MULH_TEST_OVERRIDE is defined, forcing use of __(u)mulh.")
|
|
15
|
+
# endif
|
|
16
|
+
static SECP256K1_INLINE uint64_t nostr_native_secp256k1_umul128(uint64_t a, uint64_t b, uint64_t* hi) {
|
|
17
|
+
*hi = __umulh(a, b);
|
|
18
|
+
return a * b;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static SECP256K1_INLINE int64_t nostr_native_secp256k1_mul128(int64_t a, int64_t b, int64_t* hi) {
|
|
22
|
+
*hi = __mulh(a, b);
|
|
23
|
+
return (uint64_t)a * (uint64_t)b;
|
|
24
|
+
}
|
|
25
|
+
# else
|
|
26
|
+
/* On x84_64 MSVC, use native _(u)mul128 for 64x64->128 multiplications. */
|
|
27
|
+
# define nostr_native_secp256k1_umul128 _umul128
|
|
28
|
+
# define nostr_native_secp256k1_mul128 _mul128
|
|
29
|
+
# endif
|
|
30
|
+
#else
|
|
31
|
+
/* On other systems, emulate 64x64->128 multiplications using 32x32->64 multiplications. */
|
|
32
|
+
static SECP256K1_INLINE uint64_t nostr_native_secp256k1_umul128(uint64_t a, uint64_t b, uint64_t* hi) {
|
|
33
|
+
uint64_t ll = (uint64_t)(uint32_t)a * (uint32_t)b;
|
|
34
|
+
uint64_t lh = (uint32_t)a * (b >> 32);
|
|
35
|
+
uint64_t hl = (a >> 32) * (uint32_t)b;
|
|
36
|
+
uint64_t hh = (a >> 32) * (b >> 32);
|
|
37
|
+
uint64_t mid34 = (ll >> 32) + (uint32_t)lh + (uint32_t)hl;
|
|
38
|
+
*hi = hh + (lh >> 32) + (hl >> 32) + (mid34 >> 32);
|
|
39
|
+
return (mid34 << 32) + (uint32_t)ll;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static SECP256K1_INLINE int64_t nostr_native_secp256k1_mul128(int64_t a, int64_t b, int64_t* hi) {
|
|
43
|
+
uint64_t ll = (uint64_t)(uint32_t)a * (uint32_t)b;
|
|
44
|
+
int64_t lh = (uint32_t)a * (b >> 32);
|
|
45
|
+
int64_t hl = (a >> 32) * (uint32_t)b;
|
|
46
|
+
int64_t hh = (a >> 32) * (b >> 32);
|
|
47
|
+
uint64_t mid34 = (ll >> 32) + (uint32_t)lh + (uint32_t)hl;
|
|
48
|
+
*hi = hh + (lh >> 32) + (hl >> 32) + (mid34 >> 32);
|
|
49
|
+
return (mid34 << 32) + (uint32_t)ll;
|
|
50
|
+
}
|
|
51
|
+
#endif
|
|
52
|
+
|
|
53
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_load(nostr_native_secp256k1_uint128 *r, uint64_t hi, uint64_t lo) {
|
|
54
|
+
r->hi = hi;
|
|
55
|
+
r->lo = lo;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_mul(nostr_native_secp256k1_uint128 *r, uint64_t a, uint64_t b) {
|
|
59
|
+
r->lo = nostr_native_secp256k1_umul128(a, b, &r->hi);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_accum_mul(nostr_native_secp256k1_uint128 *r, uint64_t a, uint64_t b) {
|
|
63
|
+
uint64_t lo, hi;
|
|
64
|
+
lo = nostr_native_secp256k1_umul128(a, b, &hi);
|
|
65
|
+
r->lo += lo;
|
|
66
|
+
r->hi += hi + (r->lo < lo);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_accum_u64(nostr_native_secp256k1_uint128 *r, uint64_t a) {
|
|
70
|
+
r->lo += a;
|
|
71
|
+
r->hi += r->lo < a;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/* Unsigned (logical) right shift.
|
|
75
|
+
* Non-constant time in n.
|
|
76
|
+
*/
|
|
77
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_rshift(nostr_native_secp256k1_uint128 *r, unsigned int n) {
|
|
78
|
+
VERIFY_CHECK(n < 128);
|
|
79
|
+
if (n >= 64) {
|
|
80
|
+
r->lo = r->hi >> (n-64);
|
|
81
|
+
r->hi = 0;
|
|
82
|
+
} else if (n > 0) {
|
|
83
|
+
#if defined(_MSC_VER) && defined(_M_X64)
|
|
84
|
+
VERIFY_CHECK(n < 64);
|
|
85
|
+
r->lo = __shiftright128(r->lo, r->hi, n);
|
|
86
|
+
#else
|
|
87
|
+
r->lo = ((1U * r->hi) << (64-n)) | r->lo >> n;
|
|
88
|
+
#endif
|
|
89
|
+
r->hi >>= n;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
static SECP256K1_INLINE uint64_t nostr_native_secp256k1_u128_to_u64(const nostr_native_secp256k1_uint128 *a) {
|
|
94
|
+
return a->lo;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
static SECP256K1_INLINE uint64_t nostr_native_secp256k1_u128_hi_u64(const nostr_native_secp256k1_uint128 *a) {
|
|
98
|
+
return a->hi;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_u128_from_u64(nostr_native_secp256k1_uint128 *r, uint64_t a) {
|
|
102
|
+
r->hi = 0;
|
|
103
|
+
r->lo = a;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static SECP256K1_INLINE int nostr_native_secp256k1_u128_check_bits(const nostr_native_secp256k1_uint128 *r, unsigned int n) {
|
|
107
|
+
VERIFY_CHECK(n < 128);
|
|
108
|
+
return n >= 64 ? r->hi >> (n - 64) == 0
|
|
109
|
+
: r->hi == 0 && r->lo >> n == 0;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_load(nostr_native_secp256k1_int128 *r, int64_t hi, uint64_t lo) {
|
|
113
|
+
r->hi = hi;
|
|
114
|
+
r->lo = lo;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_mul(nostr_native_secp256k1_int128 *r, int64_t a, int64_t b) {
|
|
118
|
+
int64_t hi;
|
|
119
|
+
r->lo = (uint64_t)nostr_native_secp256k1_mul128(a, b, &hi);
|
|
120
|
+
r->hi = (uint64_t)hi;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_accum_mul(nostr_native_secp256k1_int128 *r, int64_t a, int64_t b) {
|
|
124
|
+
int64_t hi;
|
|
125
|
+
uint64_t lo = (uint64_t)nostr_native_secp256k1_mul128(a, b, &hi);
|
|
126
|
+
r->lo += lo;
|
|
127
|
+
hi += r->lo < lo;
|
|
128
|
+
/* Verify no overflow.
|
|
129
|
+
* If r represents a positive value (the sign bit is not set) and the value we are adding is a positive value (the sign bit is not set),
|
|
130
|
+
* then we require that the resulting value also be positive (the sign bit is not set).
|
|
131
|
+
* Note that (X <= Y) means (X implies Y) when X and Y are boolean values (i.e. 0 or 1).
|
|
132
|
+
*/
|
|
133
|
+
VERIFY_CHECK((r->hi <= 0x7fffffffffffffffu && (uint64_t)hi <= 0x7fffffffffffffffu) <= (r->hi + (uint64_t)hi <= 0x7fffffffffffffffu));
|
|
134
|
+
/* Verify no underflow.
|
|
135
|
+
* If r represents a negative value (the sign bit is set) and the value we are adding is a negative value (the sign bit is set),
|
|
136
|
+
* then we require that the resulting value also be negative (the sign bit is set).
|
|
137
|
+
*/
|
|
138
|
+
VERIFY_CHECK((r->hi > 0x7fffffffffffffffu && (uint64_t)hi > 0x7fffffffffffffffu) <= (r->hi + (uint64_t)hi > 0x7fffffffffffffffu));
|
|
139
|
+
r->hi += hi;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_dissip_mul(nostr_native_secp256k1_int128 *r, int64_t a, int64_t b) {
|
|
143
|
+
int64_t hi;
|
|
144
|
+
uint64_t lo = (uint64_t)nostr_native_secp256k1_mul128(a, b, &hi);
|
|
145
|
+
hi += r->lo < lo;
|
|
146
|
+
/* Verify no overflow.
|
|
147
|
+
* If r represents a positive value (the sign bit is not set) and the value we are subtracting is a negative value (the sign bit is set),
|
|
148
|
+
* then we require that the resulting value also be positive (the sign bit is not set).
|
|
149
|
+
*/
|
|
150
|
+
VERIFY_CHECK((r->hi <= 0x7fffffffffffffffu && (uint64_t)hi > 0x7fffffffffffffffu) <= (r->hi - (uint64_t)hi <= 0x7fffffffffffffffu));
|
|
151
|
+
/* Verify no underflow.
|
|
152
|
+
* If r represents a negative value (the sign bit is set) and the value we are subtracting is a positive value (the sign sign bit is not set),
|
|
153
|
+
* then we require that the resulting value also be negative (the sign bit is set).
|
|
154
|
+
*/
|
|
155
|
+
VERIFY_CHECK((r->hi > 0x7fffffffffffffffu && (uint64_t)hi <= 0x7fffffffffffffffu) <= (r->hi - (uint64_t)hi > 0x7fffffffffffffffu));
|
|
156
|
+
r->hi -= hi;
|
|
157
|
+
r->lo -= lo;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_det(nostr_native_secp256k1_int128 *r, int64_t a, int64_t b, int64_t c, int64_t d) {
|
|
161
|
+
nostr_native_secp256k1_i128_mul(r, a, d);
|
|
162
|
+
nostr_native_secp256k1_i128_dissip_mul(r, b, c);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/* Signed (arithmetic) right shift.
|
|
166
|
+
* Non-constant time in n.
|
|
167
|
+
*/
|
|
168
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_rshift(nostr_native_secp256k1_int128 *r, unsigned int n) {
|
|
169
|
+
VERIFY_CHECK(n < 128);
|
|
170
|
+
if (n >= 64) {
|
|
171
|
+
r->lo = (uint64_t)((int64_t)(r->hi) >> (n-64));
|
|
172
|
+
r->hi = (uint64_t)((int64_t)(r->hi) >> 63);
|
|
173
|
+
} else if (n > 0) {
|
|
174
|
+
r->lo = ((1U * r->hi) << (64-n)) | r->lo >> n;
|
|
175
|
+
r->hi = (uint64_t)((int64_t)(r->hi) >> n);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
static SECP256K1_INLINE uint64_t nostr_native_secp256k1_i128_to_u64(const nostr_native_secp256k1_int128 *a) {
|
|
180
|
+
return a->lo;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
static SECP256K1_INLINE int64_t nostr_native_secp256k1_i128_to_i64(const nostr_native_secp256k1_int128 *a) {
|
|
184
|
+
/* Verify that a represents a 64 bit signed value by checking that the high bits are a sign extension of the low bits. */
|
|
185
|
+
VERIFY_CHECK(a->hi == -(a->lo >> 63));
|
|
186
|
+
return (int64_t)nostr_native_secp256k1_i128_to_u64(a);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
static SECP256K1_INLINE void nostr_native_secp256k1_i128_from_i64(nostr_native_secp256k1_int128 *r, int64_t a) {
|
|
190
|
+
r->hi = (uint64_t)(a >> 63);
|
|
191
|
+
r->lo = (uint64_t)a;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
static SECP256K1_INLINE int nostr_native_secp256k1_i128_eq_var(const nostr_native_secp256k1_int128 *a, const nostr_native_secp256k1_int128 *b) {
|
|
195
|
+
return a->hi == b->hi && a->lo == b->lo;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
static SECP256K1_INLINE int nostr_native_secp256k1_i128_check_pow2(const nostr_native_secp256k1_int128 *r, unsigned int n, int sign) {
|
|
199
|
+
VERIFY_CHECK(n < 127);
|
|
200
|
+
VERIFY_CHECK(sign == 1 || sign == -1);
|
|
201
|
+
return n >= 64 ? r->hi == (uint64_t)sign << (n - 64) && r->lo == 0
|
|
202
|
+
: r->hi == (uint64_t)(sign >> 1) && r->lo == (uint64_t)sign << n;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
#endif
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/***********************************************************************
|
|
2
|
+
* Copyright (c) 2020 Peter Dettman *
|
|
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_MODINV32_H
|
|
8
|
+
#define SECP256K1_MODINV32_H
|
|
9
|
+
|
|
10
|
+
#include "util.h"
|
|
11
|
+
|
|
12
|
+
/* A signed 30-bit limb representation of integers.
|
|
13
|
+
*
|
|
14
|
+
* Its value is sum(v[i] * 2^(30*i), i=0..8). */
|
|
15
|
+
typedef struct {
|
|
16
|
+
int32_t v[9];
|
|
17
|
+
} nostr_native_secp256k1_modinv32_signed30;
|
|
18
|
+
|
|
19
|
+
typedef struct {
|
|
20
|
+
/* The modulus in signed30 notation, must be odd and in [3, 2^256]. */
|
|
21
|
+
nostr_native_secp256k1_modinv32_signed30 modulus;
|
|
22
|
+
|
|
23
|
+
/* modulus^{-1} mod 2^30 */
|
|
24
|
+
uint32_t modulus_inv30;
|
|
25
|
+
} nostr_native_secp256k1_modinv32_modinfo;
|
|
26
|
+
|
|
27
|
+
/* Replace x with its modular inverse mod modinfo->modulus. x must be in range [0, modulus).
|
|
28
|
+
* If x is zero, the result will be zero as well. If not, the inverse must exist (i.e., the gcd of
|
|
29
|
+
* x and modulus must be 1). These rules are automatically satisfied if the modulus is prime.
|
|
30
|
+
*
|
|
31
|
+
* On output, all of x's limbs will be in [0, 2^30).
|
|
32
|
+
*/
|
|
33
|
+
static void nostr_native_secp256k1_modinv32_var(nostr_native_secp256k1_modinv32_signed30 *x, const nostr_native_secp256k1_modinv32_modinfo *modinfo);
|
|
34
|
+
|
|
35
|
+
/* Same as nostr_native_secp256k1_modinv32_var, but constant time in x (not in the modulus). */
|
|
36
|
+
static void nostr_native_secp256k1_modinv32(nostr_native_secp256k1_modinv32_signed30 *x, const nostr_native_secp256k1_modinv32_modinfo *modinfo);
|
|
37
|
+
|
|
38
|
+
/* Compute the Jacobi symbol for (x | modinfo->modulus). x must be coprime with modulus (and thus
|
|
39
|
+
* cannot be 0, as modulus >= 3). All limbs of x must be non-negative. Returns 0 if the result
|
|
40
|
+
* cannot be computed. */
|
|
41
|
+
static int nostr_native_secp256k1_jacobi32_maybe_var(const nostr_native_secp256k1_modinv32_signed30 *x, const nostr_native_secp256k1_modinv32_modinfo *modinfo);
|
|
42
|
+
|
|
43
|
+
#endif /* SECP256K1_MODINV32_H */
|