@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,351 @@
|
|
|
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_FIELD_H
|
|
8
|
+
#define SECP256K1_FIELD_H
|
|
9
|
+
|
|
10
|
+
#include "util.h"
|
|
11
|
+
|
|
12
|
+
/* This file defines the generic interface for working with nostr_native_secp256k1_fe
|
|
13
|
+
* objects, which represent field elements (integers modulo 2^256 - 2^32 - 977).
|
|
14
|
+
*
|
|
15
|
+
* The actual definition of the nostr_native_secp256k1_fe type depends on the chosen field
|
|
16
|
+
* implementation; see the field_5x52.h and field_10x26.h files for details.
|
|
17
|
+
*
|
|
18
|
+
* All nostr_native_secp256k1_fe objects have implicit properties that determine what
|
|
19
|
+
* operations are permitted on it. These are purely a function of what
|
|
20
|
+
* nostr_native_secp256k1_fe_ operations are applied on it, generally (implicitly) fixed at
|
|
21
|
+
* compile time, and do not depend on the chosen field implementation. Despite
|
|
22
|
+
* that, what these properties actually entail for the field representation
|
|
23
|
+
* values depends on the chosen field implementation. These properties are:
|
|
24
|
+
* - magnitude: an integer in [0,32]
|
|
25
|
+
* - normalized: 0 or 1; normalized=1 implies magnitude <= 1.
|
|
26
|
+
*
|
|
27
|
+
* In VERIFY mode, they are materialized explicitly as fields in the struct,
|
|
28
|
+
* allowing run-time verification of these properties. In that case, the field
|
|
29
|
+
* implementation also provides a nostr_native_secp256k1_fe_verify routine to verify that
|
|
30
|
+
* these fields match the run-time value and perform internal consistency
|
|
31
|
+
* checks. */
|
|
32
|
+
#ifdef VERIFY
|
|
33
|
+
# define SECP256K1_FE_VERIFY_FIELDS \
|
|
34
|
+
int magnitude; \
|
|
35
|
+
int normalized;
|
|
36
|
+
#else
|
|
37
|
+
# define SECP256K1_FE_VERIFY_FIELDS
|
|
38
|
+
#endif
|
|
39
|
+
|
|
40
|
+
#if defined(SECP256K1_WIDEMUL_INT128)
|
|
41
|
+
#include "field_5x52.h"
|
|
42
|
+
#elif defined(SECP256K1_WIDEMUL_INT64)
|
|
43
|
+
#include "field_10x26.h"
|
|
44
|
+
#else
|
|
45
|
+
#error "Please select wide multiplication implementation"
|
|
46
|
+
#endif
|
|
47
|
+
|
|
48
|
+
#ifdef VERIFY
|
|
49
|
+
/* Magnitude and normalized value for constants. */
|
|
50
|
+
#define SECP256K1_FE_VERIFY_CONST(d7, d6, d5, d4, d3, d2, d1, d0) \
|
|
51
|
+
/* Magnitude is 0 for constant 0; 1 otherwise. */ \
|
|
52
|
+
, (((d7) | (d6) | (d5) | (d4) | (d3) | (d2) | (d1) | (d0)) != 0) \
|
|
53
|
+
/* Normalized is 1 unless sum(d_i<<(32*i) for i=0..7) exceeds field modulus. */ \
|
|
54
|
+
, (!(((d7) & (d6) & (d5) & (d4) & (d3) & (d2)) == 0xfffffffful && ((d1) == 0xfffffffful || ((d1) == 0xfffffffe && (d0 >= 0xfffffc2f)))))
|
|
55
|
+
#else
|
|
56
|
+
#define SECP256K1_FE_VERIFY_CONST(d7, d6, d5, d4, d3, d2, d1, d0)
|
|
57
|
+
#endif
|
|
58
|
+
|
|
59
|
+
/** This expands to an initializer for a nostr_native_secp256k1_fe valued sum((i*32) * d_i, i=0..7) mod p.
|
|
60
|
+
*
|
|
61
|
+
* It has magnitude 1, unless d_i are all 0, in which case the magnitude is 0.
|
|
62
|
+
* It is normalized, unless sum(2^(i*32) * d_i, i=0..7) >= p.
|
|
63
|
+
*
|
|
64
|
+
* SECP256K1_FE_CONST_INNER is provided by the implementation.
|
|
65
|
+
*/
|
|
66
|
+
#define SECP256K1_FE_CONST(d7, d6, d5, d4, d3, d2, d1, d0) {SECP256K1_FE_CONST_INNER((d7), (d6), (d5), (d4), (d3), (d2), (d1), (d0)) SECP256K1_FE_VERIFY_CONST((d7), (d6), (d5), (d4), (d3), (d2), (d1), (d0)) }
|
|
67
|
+
|
|
68
|
+
static const nostr_native_secp256k1_fe nostr_native_secp256k1_fe_one = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
|
|
69
|
+
static const nostr_native_secp256k1_fe nostr_native_secp256k1_const_beta = SECP256K1_FE_CONST(
|
|
70
|
+
0x7ae96a2bul, 0x657c0710ul, 0x6e64479eul, 0xac3434e9ul,
|
|
71
|
+
0x9cf04975ul, 0x12f58995ul, 0xc1396c28ul, 0x719501eeul
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
#ifndef VERIFY
|
|
75
|
+
/* In non-VERIFY mode, we #define the fe operations to be identical to their
|
|
76
|
+
* internal field implementation, to avoid the potential overhead of a
|
|
77
|
+
* function call (even though presumably inlinable). */
|
|
78
|
+
# define nostr_native_secp256k1_fe_normalize nostr_native_secp256k1_fe_impl_normalize
|
|
79
|
+
# define nostr_native_secp256k1_fe_normalize_weak nostr_native_secp256k1_fe_impl_normalize_weak
|
|
80
|
+
# define nostr_native_secp256k1_fe_normalize_var nostr_native_secp256k1_fe_impl_normalize_var
|
|
81
|
+
# define nostr_native_secp256k1_fe_normalizes_to_zero nostr_native_secp256k1_fe_impl_normalizes_to_zero
|
|
82
|
+
# define nostr_native_secp256k1_fe_normalizes_to_zero_var nostr_native_secp256k1_fe_impl_normalizes_to_zero_var
|
|
83
|
+
# define nostr_native_secp256k1_fe_set_int nostr_native_secp256k1_fe_impl_set_int
|
|
84
|
+
# define nostr_native_secp256k1_fe_is_zero nostr_native_secp256k1_fe_impl_is_zero
|
|
85
|
+
# define nostr_native_secp256k1_fe_is_odd nostr_native_secp256k1_fe_impl_is_odd
|
|
86
|
+
# define nostr_native_secp256k1_fe_cmp_var nostr_native_secp256k1_fe_impl_cmp_var
|
|
87
|
+
# define nostr_native_secp256k1_fe_set_b32_mod nostr_native_secp256k1_fe_impl_set_b32_mod
|
|
88
|
+
# define nostr_native_secp256k1_fe_set_b32_limit nostr_native_secp256k1_fe_impl_set_b32_limit
|
|
89
|
+
# define nostr_native_secp256k1_fe_get_b32 nostr_native_secp256k1_fe_impl_get_b32
|
|
90
|
+
# define nostr_native_secp256k1_fe_negate_unchecked nostr_native_secp256k1_fe_impl_negate_unchecked
|
|
91
|
+
# define nostr_native_secp256k1_fe_mul_int_unchecked nostr_native_secp256k1_fe_impl_mul_int_unchecked
|
|
92
|
+
# define nostr_native_secp256k1_fe_add nostr_native_secp256k1_fe_impl_add
|
|
93
|
+
# define nostr_native_secp256k1_fe_mul nostr_native_secp256k1_fe_impl_mul
|
|
94
|
+
# define nostr_native_secp256k1_fe_sqr nostr_native_secp256k1_fe_impl_sqr
|
|
95
|
+
# define nostr_native_secp256k1_fe_cmov nostr_native_secp256k1_fe_impl_cmov
|
|
96
|
+
# define nostr_native_secp256k1_fe_to_storage nostr_native_secp256k1_fe_impl_to_storage
|
|
97
|
+
# define nostr_native_secp256k1_fe_from_storage nostr_native_secp256k1_fe_impl_from_storage
|
|
98
|
+
# define nostr_native_secp256k1_fe_inv nostr_native_secp256k1_fe_impl_inv
|
|
99
|
+
# define nostr_native_secp256k1_fe_inv_var nostr_native_secp256k1_fe_impl_inv_var
|
|
100
|
+
# define nostr_native_secp256k1_fe_get_bounds nostr_native_secp256k1_fe_impl_get_bounds
|
|
101
|
+
# define nostr_native_secp256k1_fe_half nostr_native_secp256k1_fe_impl_half
|
|
102
|
+
# define nostr_native_secp256k1_fe_add_int nostr_native_secp256k1_fe_impl_add_int
|
|
103
|
+
# define nostr_native_secp256k1_fe_is_square_var nostr_native_secp256k1_fe_impl_is_square_var
|
|
104
|
+
#endif /* !defined(VERIFY) */
|
|
105
|
+
|
|
106
|
+
/** Normalize a field element.
|
|
107
|
+
*
|
|
108
|
+
* On input, r must be a valid field element.
|
|
109
|
+
* On output, r represents the same value but has normalized=1 and magnitude=1.
|
|
110
|
+
*/
|
|
111
|
+
static void nostr_native_secp256k1_fe_normalize(nostr_native_secp256k1_fe *r);
|
|
112
|
+
|
|
113
|
+
/** Give a field element magnitude 1.
|
|
114
|
+
*
|
|
115
|
+
* On input, r must be a valid field element.
|
|
116
|
+
* On output, r represents the same value but has magnitude=1. Normalized is unchanged.
|
|
117
|
+
*/
|
|
118
|
+
static void nostr_native_secp256k1_fe_normalize_weak(nostr_native_secp256k1_fe *r);
|
|
119
|
+
|
|
120
|
+
/** Normalize a field element, without constant-time guarantee.
|
|
121
|
+
*
|
|
122
|
+
* Identical in behavior to nostr_native_secp256k1_fe_normalize, but not constant time in r.
|
|
123
|
+
*/
|
|
124
|
+
static void nostr_native_secp256k1_fe_normalize_var(nostr_native_secp256k1_fe *r);
|
|
125
|
+
|
|
126
|
+
/** Determine whether r represents field element 0.
|
|
127
|
+
*
|
|
128
|
+
* On input, r must be a valid field element.
|
|
129
|
+
* Returns whether r = 0 (mod p).
|
|
130
|
+
*/
|
|
131
|
+
static int nostr_native_secp256k1_fe_normalizes_to_zero(const nostr_native_secp256k1_fe *r);
|
|
132
|
+
|
|
133
|
+
/** Determine whether r represents field element 0, without constant-time guarantee.
|
|
134
|
+
*
|
|
135
|
+
* Identical in behavior to nostr_native_secp256k1_normalizes_to_zero, but not constant time in r.
|
|
136
|
+
*/
|
|
137
|
+
static int nostr_native_secp256k1_fe_normalizes_to_zero_var(const nostr_native_secp256k1_fe *r);
|
|
138
|
+
|
|
139
|
+
/** Set a field element to an integer in range [0,0x7FFF].
|
|
140
|
+
*
|
|
141
|
+
* On input, r does not need to be initialized, a must be in [0,0x7FFF].
|
|
142
|
+
* On output, r represents value a, is normalized and has magnitude (a!=0).
|
|
143
|
+
*/
|
|
144
|
+
static void nostr_native_secp256k1_fe_set_int(nostr_native_secp256k1_fe *r, int a);
|
|
145
|
+
|
|
146
|
+
/** Clear a field element to prevent leaking sensitive information. */
|
|
147
|
+
static void nostr_native_secp256k1_fe_clear(nostr_native_secp256k1_fe *a);
|
|
148
|
+
|
|
149
|
+
/** Determine whether a represents field element 0.
|
|
150
|
+
*
|
|
151
|
+
* On input, a must be a valid normalized field element.
|
|
152
|
+
* Returns whether a = 0 (mod p).
|
|
153
|
+
*
|
|
154
|
+
* This behaves identical to nostr_native_secp256k1_normalizes_to_zero{,_var}, but requires
|
|
155
|
+
* normalized input (and is much faster).
|
|
156
|
+
*/
|
|
157
|
+
static int nostr_native_secp256k1_fe_is_zero(const nostr_native_secp256k1_fe *a);
|
|
158
|
+
|
|
159
|
+
/** Determine whether a (mod p) is odd.
|
|
160
|
+
*
|
|
161
|
+
* On input, a must be a valid normalized field element.
|
|
162
|
+
* Returns (int(a) mod p) & 1.
|
|
163
|
+
*/
|
|
164
|
+
static int nostr_native_secp256k1_fe_is_odd(const nostr_native_secp256k1_fe *a);
|
|
165
|
+
|
|
166
|
+
/** Determine whether two field elements are equal.
|
|
167
|
+
*
|
|
168
|
+
* On input, a and b must be valid field elements with magnitudes not exceeding
|
|
169
|
+
* 1 and 30, respectively.
|
|
170
|
+
* Returns a = b (mod p).
|
|
171
|
+
*/
|
|
172
|
+
static int nostr_native_secp256k1_fe_equal(const nostr_native_secp256k1_fe *a, const nostr_native_secp256k1_fe *b);
|
|
173
|
+
|
|
174
|
+
/** Compare the values represented by 2 field elements, without constant-time guarantee.
|
|
175
|
+
*
|
|
176
|
+
* On input, a and b must be valid normalized field elements.
|
|
177
|
+
* Returns 1 if a > b, -1 if a < b, and 0 if a = b (comparisons are done as integers
|
|
178
|
+
* in range 0..p-1).
|
|
179
|
+
*/
|
|
180
|
+
static int nostr_native_secp256k1_fe_cmp_var(const nostr_native_secp256k1_fe *a, const nostr_native_secp256k1_fe *b);
|
|
181
|
+
|
|
182
|
+
/** Set a field element equal to the element represented by a provided 32-byte big endian value
|
|
183
|
+
* interpreted modulo p.
|
|
184
|
+
*
|
|
185
|
+
* On input, r does not need to be initialized. a must be a pointer to an initialized 32-byte array.
|
|
186
|
+
* On output, r = a (mod p). It will have magnitude 1, and not be normalized.
|
|
187
|
+
*/
|
|
188
|
+
static void nostr_native_secp256k1_fe_set_b32_mod(nostr_native_secp256k1_fe *r, const unsigned char *a);
|
|
189
|
+
|
|
190
|
+
/** Set a field element equal to a provided 32-byte big endian value, checking for overflow.
|
|
191
|
+
*
|
|
192
|
+
* On input, r does not need to be initialized. a must be a pointer to an initialized 32-byte array.
|
|
193
|
+
* On output, r = a if (a < p), it will be normalized with magnitude 1, and 1 is returned.
|
|
194
|
+
* If a >= p, 0 is returned, and r will be made invalid (and must not be used without overwriting).
|
|
195
|
+
*/
|
|
196
|
+
static int nostr_native_secp256k1_fe_set_b32_limit(nostr_native_secp256k1_fe *r, const unsigned char *a);
|
|
197
|
+
|
|
198
|
+
/** Convert a field element to 32-byte big endian byte array.
|
|
199
|
+
* On input, a must be a valid normalized field element, and r a pointer to a 32-byte array.
|
|
200
|
+
* On output, r = a (mod p).
|
|
201
|
+
*/
|
|
202
|
+
static void nostr_native_secp256k1_fe_get_b32(unsigned char *r, const nostr_native_secp256k1_fe *a);
|
|
203
|
+
|
|
204
|
+
/** Negate a field element.
|
|
205
|
+
*
|
|
206
|
+
* On input, r does not need to be initialized. a must be a valid field element with
|
|
207
|
+
* magnitude not exceeding m. m must be an integer constant expression in [0,31].
|
|
208
|
+
* Performs {r = -a}.
|
|
209
|
+
* On output, r will not be normalized, and will have magnitude m+1.
|
|
210
|
+
*/
|
|
211
|
+
#define nostr_native_secp256k1_fe_negate(r, a, m) ASSERT_INT_CONST_AND_DO(m, nostr_native_secp256k1_fe_negate_unchecked(r, a, m))
|
|
212
|
+
|
|
213
|
+
/** Like nostr_native_secp256k1_fe_negate_unchecked but m is not checked to be an integer constant expression.
|
|
214
|
+
*
|
|
215
|
+
* Should not be called directly outside of tests.
|
|
216
|
+
*/
|
|
217
|
+
static void nostr_native_secp256k1_fe_negate_unchecked(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a, int m);
|
|
218
|
+
|
|
219
|
+
/** Add a small integer to a field element.
|
|
220
|
+
*
|
|
221
|
+
* Performs {r += a}. The magnitude of r increases by 1, and normalized is cleared.
|
|
222
|
+
* a must be in range [0,0x7FFF].
|
|
223
|
+
*/
|
|
224
|
+
static void nostr_native_secp256k1_fe_add_int(nostr_native_secp256k1_fe *r, int a);
|
|
225
|
+
|
|
226
|
+
/** Multiply a field element with a small integer.
|
|
227
|
+
*
|
|
228
|
+
* On input, r must be a valid field element. a must be an integer constant expression in [0,32].
|
|
229
|
+
* The magnitude of r times a must not exceed 32.
|
|
230
|
+
* Performs {r *= a}.
|
|
231
|
+
* On output, r's magnitude is multiplied by a, and r will not be normalized.
|
|
232
|
+
*/
|
|
233
|
+
#define nostr_native_secp256k1_fe_mul_int(r, a) ASSERT_INT_CONST_AND_DO(a, nostr_native_secp256k1_fe_mul_int_unchecked(r, a))
|
|
234
|
+
|
|
235
|
+
/** Like nostr_native_secp256k1_fe_mul_int but a is not checked to be an integer constant expression.
|
|
236
|
+
*
|
|
237
|
+
* Should not be called directly outside of tests.
|
|
238
|
+
*/
|
|
239
|
+
static void nostr_native_secp256k1_fe_mul_int_unchecked(nostr_native_secp256k1_fe *r, int a);
|
|
240
|
+
|
|
241
|
+
/** Increment a field element by another.
|
|
242
|
+
*
|
|
243
|
+
* On input, r and a must be valid field elements, not necessarily normalized.
|
|
244
|
+
* The sum of their magnitudes must not exceed 32.
|
|
245
|
+
* Performs {r += a}.
|
|
246
|
+
* On output, r will not be normalized, and will have magnitude incremented by a's.
|
|
247
|
+
*/
|
|
248
|
+
static void nostr_native_secp256k1_fe_add(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a);
|
|
249
|
+
|
|
250
|
+
/** Multiply two field elements.
|
|
251
|
+
*
|
|
252
|
+
* On input, a and b must be valid field elements; r does not need to be initialized.
|
|
253
|
+
* r and a may point to the same object, but neither may point to the object pointed
|
|
254
|
+
* to by b. The magnitudes of a and b must not exceed 8.
|
|
255
|
+
* Performs {r = a * b}
|
|
256
|
+
* On output, r will have magnitude 1, but won't be normalized.
|
|
257
|
+
*/
|
|
258
|
+
static void nostr_native_secp256k1_fe_mul(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a, const nostr_native_secp256k1_fe * SECP256K1_RESTRICT b);
|
|
259
|
+
|
|
260
|
+
/** Square a field element.
|
|
261
|
+
*
|
|
262
|
+
* On input, a must be a valid field element; r does not need to be initialized. The magnitude
|
|
263
|
+
* of a must not exceed 8.
|
|
264
|
+
* Performs {r = a**2}
|
|
265
|
+
* On output, r will have magnitude 1, but won't be normalized.
|
|
266
|
+
*/
|
|
267
|
+
static void nostr_native_secp256k1_fe_sqr(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a);
|
|
268
|
+
|
|
269
|
+
/** Compute a square root of a field element.
|
|
270
|
+
*
|
|
271
|
+
* On input, a must be a valid field element with magnitude<=8; r need not be initialized.
|
|
272
|
+
* If sqrt(a) exists, performs {r = sqrt(a)} and returns 1.
|
|
273
|
+
* Otherwise, sqrt(-a) exists. The function performs {r = sqrt(-a)} and returns 0.
|
|
274
|
+
* The resulting value represented by r will be a square itself.
|
|
275
|
+
* Variables r and a must not point to the same object.
|
|
276
|
+
* On output, r will have magnitude 1 but will not be normalized.
|
|
277
|
+
*/
|
|
278
|
+
static int nostr_native_secp256k1_fe_sqrt(nostr_native_secp256k1_fe * SECP256K1_RESTRICT r, const nostr_native_secp256k1_fe * SECP256K1_RESTRICT a);
|
|
279
|
+
|
|
280
|
+
/** Compute the modular inverse of a field element.
|
|
281
|
+
*
|
|
282
|
+
* On input, a must be a valid field element; r need not be initialized.
|
|
283
|
+
* Performs {r = a**(p-2)} (which maps 0 to 0, and every other element to its
|
|
284
|
+
* inverse).
|
|
285
|
+
* On output, r will have magnitude (a.magnitude != 0) and be normalized.
|
|
286
|
+
*/
|
|
287
|
+
static void nostr_native_secp256k1_fe_inv(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a);
|
|
288
|
+
|
|
289
|
+
/** Compute the modular inverse of a field element, without constant-time guarantee.
|
|
290
|
+
*
|
|
291
|
+
* Behaves identically to nostr_native_secp256k1_fe_inv, but is not constant-time in a.
|
|
292
|
+
*/
|
|
293
|
+
static void nostr_native_secp256k1_fe_inv_var(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a);
|
|
294
|
+
|
|
295
|
+
/** Convert a field element to nostr_native_secp256k1_fe_storage.
|
|
296
|
+
*
|
|
297
|
+
* On input, a must be a valid normalized field element.
|
|
298
|
+
* Performs {r = a}.
|
|
299
|
+
*/
|
|
300
|
+
static void nostr_native_secp256k1_fe_to_storage(nostr_native_secp256k1_fe_storage *r, const nostr_native_secp256k1_fe *a);
|
|
301
|
+
|
|
302
|
+
/** Convert a field element back from nostr_native_secp256k1_fe_storage.
|
|
303
|
+
*
|
|
304
|
+
* On input, r need not be initialized.
|
|
305
|
+
* Performs {r = a}.
|
|
306
|
+
* On output, r will be normalized and will have magnitude 1.
|
|
307
|
+
*/
|
|
308
|
+
static void nostr_native_secp256k1_fe_from_storage(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe_storage *a);
|
|
309
|
+
|
|
310
|
+
/** If flag is 1, set *r equal to *a; if flag is 0, leave it. Constant-time.
|
|
311
|
+
* Both *r and *a must be initialized. Flag must be 0 or 1. */
|
|
312
|
+
static void nostr_native_secp256k1_fe_storage_cmov(nostr_native_secp256k1_fe_storage *r, const nostr_native_secp256k1_fe_storage *a, int flag);
|
|
313
|
+
|
|
314
|
+
/** Conditionally move a field element in constant time.
|
|
315
|
+
*
|
|
316
|
+
* On input, both r and a must be valid field elements. Flag must be 0 or 1.
|
|
317
|
+
* Performs {r = flag ? a : r}.
|
|
318
|
+
*
|
|
319
|
+
* On output, r's magnitude will be the maximum of both input magnitudes.
|
|
320
|
+
* It will be normalized if and only if both inputs were normalized.
|
|
321
|
+
*/
|
|
322
|
+
static void nostr_native_secp256k1_fe_cmov(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a, int flag);
|
|
323
|
+
|
|
324
|
+
/** Halve the value of a field element modulo the field prime in constant-time.
|
|
325
|
+
*
|
|
326
|
+
* On input, r must be a valid field element.
|
|
327
|
+
* On output, r will be normalized and have magnitude floor(m/2) + 1 where m is
|
|
328
|
+
* the magnitude of r on input.
|
|
329
|
+
*/
|
|
330
|
+
static void nostr_native_secp256k1_fe_half(nostr_native_secp256k1_fe *r);
|
|
331
|
+
|
|
332
|
+
/** Sets r to a field element with magnitude m, normalized if (and only if) m==0.
|
|
333
|
+
* The value is chosen so that it is likely to trigger edge cases related to
|
|
334
|
+
* internal overflows. */
|
|
335
|
+
static void nostr_native_secp256k1_fe_get_bounds(nostr_native_secp256k1_fe *r, int m);
|
|
336
|
+
|
|
337
|
+
/** Determine whether a is a square (modulo p).
|
|
338
|
+
*
|
|
339
|
+
* On input, a must be a valid field element.
|
|
340
|
+
*/
|
|
341
|
+
static int nostr_native_secp256k1_fe_is_square_var(const nostr_native_secp256k1_fe *a);
|
|
342
|
+
|
|
343
|
+
/** Check invariants on a field element (no-op unless VERIFY is enabled). */
|
|
344
|
+
static void nostr_native_secp256k1_fe_verify(const nostr_native_secp256k1_fe *a);
|
|
345
|
+
#define SECP256K1_FE_VERIFY(a) nostr_native_secp256k1_fe_verify(a)
|
|
346
|
+
|
|
347
|
+
/** Check that magnitude of a is at most m (no-op unless VERIFY is enabled). */
|
|
348
|
+
static void nostr_native_secp256k1_fe_verify_magnitude(const nostr_native_secp256k1_fe *a, int m);
|
|
349
|
+
#define SECP256K1_FE_VERIFY_MAGNITUDE(a, m) nostr_native_secp256k1_fe_verify_magnitude(a, m)
|
|
350
|
+
|
|
351
|
+
#endif /* SECP256K1_FIELD_H */
|
|
@@ -0,0 +1,57 @@
|
|
|
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_FIELD_REPR_H
|
|
8
|
+
#define SECP256K1_FIELD_REPR_H
|
|
9
|
+
|
|
10
|
+
#include <stdint.h>
|
|
11
|
+
|
|
12
|
+
/** This field implementation represents the value as 10 uint32_t limbs in base
|
|
13
|
+
* 2^26. */
|
|
14
|
+
typedef struct {
|
|
15
|
+
/* A field element f represents the sum(i=0..9, f.n[i] << (i*26)) mod p,
|
|
16
|
+
* where p is the field modulus, 2^256 - 2^32 - 977.
|
|
17
|
+
*
|
|
18
|
+
* The individual limbs f.n[i] can exceed 2^26; the field's magnitude roughly
|
|
19
|
+
* corresponds to how much excess is allowed. The value
|
|
20
|
+
* sum(i=0..9, f.n[i] << (i*26)) may exceed p, unless the field element is
|
|
21
|
+
* normalized. */
|
|
22
|
+
uint32_t n[10];
|
|
23
|
+
/*
|
|
24
|
+
* Magnitude m requires:
|
|
25
|
+
* n[i] <= 2 * m * (2^26 - 1) for i=0..8
|
|
26
|
+
* n[9] <= 2 * m * (2^22 - 1)
|
|
27
|
+
*
|
|
28
|
+
* Normalized requires:
|
|
29
|
+
* n[i] <= (2^26 - 1) for i=0..8
|
|
30
|
+
* sum(i=0..9, n[i] << (i*26)) < p
|
|
31
|
+
* (together these imply n[9] <= 2^22 - 1)
|
|
32
|
+
*/
|
|
33
|
+
SECP256K1_FE_VERIFY_FIELDS
|
|
34
|
+
} nostr_native_secp256k1_fe;
|
|
35
|
+
|
|
36
|
+
/* Unpacks a constant into a overlapping multi-limbed FE element. */
|
|
37
|
+
#define SECP256K1_FE_CONST_INNER(d7, d6, d5, d4, d3, d2, d1, d0) { \
|
|
38
|
+
(d0) & 0x3FFFFFFUL, \
|
|
39
|
+
(((uint32_t)d0) >> 26) | (((uint32_t)(d1) & 0xFFFFFUL) << 6), \
|
|
40
|
+
(((uint32_t)d1) >> 20) | (((uint32_t)(d2) & 0x3FFFUL) << 12), \
|
|
41
|
+
(((uint32_t)d2) >> 14) | (((uint32_t)(d3) & 0xFFUL) << 18), \
|
|
42
|
+
(((uint32_t)d3) >> 8) | (((uint32_t)(d4) & 0x3UL) << 24), \
|
|
43
|
+
(((uint32_t)d4) >> 2) & 0x3FFFFFFUL, \
|
|
44
|
+
(((uint32_t)d4) >> 28) | (((uint32_t)(d5) & 0x3FFFFFUL) << 4), \
|
|
45
|
+
(((uint32_t)d5) >> 22) | (((uint32_t)(d6) & 0xFFFFUL) << 10), \
|
|
46
|
+
(((uint32_t)d6) >> 16) | (((uint32_t)(d7) & 0x3FFUL) << 16), \
|
|
47
|
+
(((uint32_t)d7) >> 10) \
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
typedef struct {
|
|
51
|
+
uint32_t n[8];
|
|
52
|
+
} nostr_native_secp256k1_fe_storage;
|
|
53
|
+
|
|
54
|
+
#define SECP256K1_FE_STORAGE_CONST(d7, d6, d5, d4, d3, d2, d1, d0) {{ (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }}
|
|
55
|
+
#define SECP256K1_FE_STORAGE_CONST_GET(d) d.n[7], d.n[6], d.n[5], d.n[4],d.n[3], d.n[2], d.n[1], d.n[0]
|
|
56
|
+
|
|
57
|
+
#endif /* SECP256K1_FIELD_REPR_H */
|