@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,457 @@
|
|
|
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_IMPL_H
|
|
8
|
+
#define SECP256K1_FIELD_IMPL_H
|
|
9
|
+
|
|
10
|
+
#include "field.h"
|
|
11
|
+
#include "util.h"
|
|
12
|
+
|
|
13
|
+
#if defined(SECP256K1_WIDEMUL_INT128)
|
|
14
|
+
#include "field_5x52_impl.h"
|
|
15
|
+
#elif defined(SECP256K1_WIDEMUL_INT64)
|
|
16
|
+
#include "field_10x26_impl.h"
|
|
17
|
+
#else
|
|
18
|
+
#error "Please select wide multiplication implementation"
|
|
19
|
+
#endif
|
|
20
|
+
|
|
21
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_clear(nostr_native_secp256k1_fe *a) {
|
|
22
|
+
nostr_native_secp256k1_memclear_explicit(a, sizeof(nostr_native_secp256k1_fe));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
SECP256K1_INLINE static int nostr_native_secp256k1_fe_equal(const nostr_native_secp256k1_fe *a, const nostr_native_secp256k1_fe *b) {
|
|
26
|
+
nostr_native_secp256k1_fe na;
|
|
27
|
+
SECP256K1_FE_VERIFY(a);
|
|
28
|
+
SECP256K1_FE_VERIFY(b);
|
|
29
|
+
SECP256K1_FE_VERIFY_MAGNITUDE(a, 1);
|
|
30
|
+
SECP256K1_FE_VERIFY_MAGNITUDE(b, 30);
|
|
31
|
+
|
|
32
|
+
nostr_native_secp256k1_fe_negate(&na, a, 1);
|
|
33
|
+
nostr_native_secp256k1_fe_add(&na, b);
|
|
34
|
+
return nostr_native_secp256k1_fe_normalizes_to_zero(&na);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static int nostr_native_secp256k1_fe_sqrt(nostr_native_secp256k1_fe * SECP256K1_RESTRICT r, const nostr_native_secp256k1_fe * SECP256K1_RESTRICT a) {
|
|
38
|
+
/** Given that p is congruent to 3 mod 4, we can compute the square root of
|
|
39
|
+
* a mod p as the (p+1)/4'th power of a.
|
|
40
|
+
*
|
|
41
|
+
* As (p+1)/4 is an even number, it will have the same result for a and for
|
|
42
|
+
* (-a). Only one of these two numbers actually has a square root however,
|
|
43
|
+
* so we test at the end by squaring and comparing to the input.
|
|
44
|
+
* Also because (p+1)/4 is an even number, the computed square root is
|
|
45
|
+
* itself always a square (a ** ((p+1)/4) is the square of a ** ((p+1)/8)).
|
|
46
|
+
*/
|
|
47
|
+
nostr_native_secp256k1_fe x2, x3, x6, x9, x11, x22, x44, x88, x176, x220, x223, t1;
|
|
48
|
+
int j, ret;
|
|
49
|
+
|
|
50
|
+
VERIFY_CHECK(r != a);
|
|
51
|
+
SECP256K1_FE_VERIFY(a);
|
|
52
|
+
SECP256K1_FE_VERIFY_MAGNITUDE(a, 8);
|
|
53
|
+
|
|
54
|
+
/** The binary representation of (p + 1)/4 has 3 blocks of 1s, with lengths in
|
|
55
|
+
* { 2, 22, 223 }. Use an addition chain to calculate 2^n - 1 for each block:
|
|
56
|
+
* 1, [2], 3, 6, 9, 11, [22], 44, 88, 176, 220, [223]
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
nostr_native_secp256k1_fe_sqr(&x2, a);
|
|
60
|
+
nostr_native_secp256k1_fe_mul(&x2, &x2, a);
|
|
61
|
+
|
|
62
|
+
nostr_native_secp256k1_fe_sqr(&x3, &x2);
|
|
63
|
+
nostr_native_secp256k1_fe_mul(&x3, &x3, a);
|
|
64
|
+
|
|
65
|
+
x6 = x3;
|
|
66
|
+
for (j=0; j<3; j++) {
|
|
67
|
+
nostr_native_secp256k1_fe_sqr(&x6, &x6);
|
|
68
|
+
}
|
|
69
|
+
nostr_native_secp256k1_fe_mul(&x6, &x6, &x3);
|
|
70
|
+
|
|
71
|
+
x9 = x6;
|
|
72
|
+
for (j=0; j<3; j++) {
|
|
73
|
+
nostr_native_secp256k1_fe_sqr(&x9, &x9);
|
|
74
|
+
}
|
|
75
|
+
nostr_native_secp256k1_fe_mul(&x9, &x9, &x3);
|
|
76
|
+
|
|
77
|
+
x11 = x9;
|
|
78
|
+
for (j=0; j<2; j++) {
|
|
79
|
+
nostr_native_secp256k1_fe_sqr(&x11, &x11);
|
|
80
|
+
}
|
|
81
|
+
nostr_native_secp256k1_fe_mul(&x11, &x11, &x2);
|
|
82
|
+
|
|
83
|
+
x22 = x11;
|
|
84
|
+
for (j=0; j<11; j++) {
|
|
85
|
+
nostr_native_secp256k1_fe_sqr(&x22, &x22);
|
|
86
|
+
}
|
|
87
|
+
nostr_native_secp256k1_fe_mul(&x22, &x22, &x11);
|
|
88
|
+
|
|
89
|
+
x44 = x22;
|
|
90
|
+
for (j=0; j<22; j++) {
|
|
91
|
+
nostr_native_secp256k1_fe_sqr(&x44, &x44);
|
|
92
|
+
}
|
|
93
|
+
nostr_native_secp256k1_fe_mul(&x44, &x44, &x22);
|
|
94
|
+
|
|
95
|
+
x88 = x44;
|
|
96
|
+
for (j=0; j<44; j++) {
|
|
97
|
+
nostr_native_secp256k1_fe_sqr(&x88, &x88);
|
|
98
|
+
}
|
|
99
|
+
nostr_native_secp256k1_fe_mul(&x88, &x88, &x44);
|
|
100
|
+
|
|
101
|
+
x176 = x88;
|
|
102
|
+
for (j=0; j<88; j++) {
|
|
103
|
+
nostr_native_secp256k1_fe_sqr(&x176, &x176);
|
|
104
|
+
}
|
|
105
|
+
nostr_native_secp256k1_fe_mul(&x176, &x176, &x88);
|
|
106
|
+
|
|
107
|
+
x220 = x176;
|
|
108
|
+
for (j=0; j<44; j++) {
|
|
109
|
+
nostr_native_secp256k1_fe_sqr(&x220, &x220);
|
|
110
|
+
}
|
|
111
|
+
nostr_native_secp256k1_fe_mul(&x220, &x220, &x44);
|
|
112
|
+
|
|
113
|
+
x223 = x220;
|
|
114
|
+
for (j=0; j<3; j++) {
|
|
115
|
+
nostr_native_secp256k1_fe_sqr(&x223, &x223);
|
|
116
|
+
}
|
|
117
|
+
nostr_native_secp256k1_fe_mul(&x223, &x223, &x3);
|
|
118
|
+
|
|
119
|
+
/* The final result is then assembled using a sliding window over the blocks. */
|
|
120
|
+
|
|
121
|
+
t1 = x223;
|
|
122
|
+
for (j=0; j<23; j++) {
|
|
123
|
+
nostr_native_secp256k1_fe_sqr(&t1, &t1);
|
|
124
|
+
}
|
|
125
|
+
nostr_native_secp256k1_fe_mul(&t1, &t1, &x22);
|
|
126
|
+
for (j=0; j<6; j++) {
|
|
127
|
+
nostr_native_secp256k1_fe_sqr(&t1, &t1);
|
|
128
|
+
}
|
|
129
|
+
nostr_native_secp256k1_fe_mul(&t1, &t1, &x2);
|
|
130
|
+
nostr_native_secp256k1_fe_sqr(&t1, &t1);
|
|
131
|
+
nostr_native_secp256k1_fe_sqr(r, &t1);
|
|
132
|
+
|
|
133
|
+
/* Check that a square root was actually calculated */
|
|
134
|
+
|
|
135
|
+
nostr_native_secp256k1_fe_sqr(&t1, r);
|
|
136
|
+
ret = nostr_native_secp256k1_fe_equal(&t1, a);
|
|
137
|
+
|
|
138
|
+
#ifdef VERIFY
|
|
139
|
+
if (!ret) {
|
|
140
|
+
nostr_native_secp256k1_fe_negate(&t1, &t1, 1);
|
|
141
|
+
nostr_native_secp256k1_fe_normalize_var(&t1);
|
|
142
|
+
VERIFY_CHECK(nostr_native_secp256k1_fe_equal(&t1, a));
|
|
143
|
+
}
|
|
144
|
+
#endif
|
|
145
|
+
return ret;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
#ifndef VERIFY
|
|
149
|
+
static void nostr_native_secp256k1_fe_verify(const nostr_native_secp256k1_fe *a) { (void)a; }
|
|
150
|
+
static void nostr_native_secp256k1_fe_verify_magnitude(const nostr_native_secp256k1_fe *a, int m) { (void)a; (void)m; }
|
|
151
|
+
#else
|
|
152
|
+
static void nostr_native_secp256k1_fe_impl_verify(const nostr_native_secp256k1_fe *a);
|
|
153
|
+
static void nostr_native_secp256k1_fe_verify(const nostr_native_secp256k1_fe *a) {
|
|
154
|
+
/* Magnitude between 0 and 32. */
|
|
155
|
+
SECP256K1_FE_VERIFY_MAGNITUDE(a, 32);
|
|
156
|
+
/* Normalized is 0 or 1. */
|
|
157
|
+
VERIFY_CHECK((a->normalized == 0) || (a->normalized == 1));
|
|
158
|
+
/* If normalized, magnitude must be 0 or 1. */
|
|
159
|
+
if (a->normalized) SECP256K1_FE_VERIFY_MAGNITUDE(a, 1);
|
|
160
|
+
/* Invoke implementation-specific checks. */
|
|
161
|
+
nostr_native_secp256k1_fe_impl_verify(a);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
static void nostr_native_secp256k1_fe_verify_magnitude(const nostr_native_secp256k1_fe *a, int m) {
|
|
165
|
+
VERIFY_CHECK(m >= 0);
|
|
166
|
+
VERIFY_CHECK(m <= 32);
|
|
167
|
+
VERIFY_CHECK(a->magnitude <= m);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
static void nostr_native_secp256k1_fe_impl_normalize(nostr_native_secp256k1_fe *r);
|
|
171
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_normalize(nostr_native_secp256k1_fe *r) {
|
|
172
|
+
SECP256K1_FE_VERIFY(r);
|
|
173
|
+
|
|
174
|
+
nostr_native_secp256k1_fe_impl_normalize(r);
|
|
175
|
+
r->magnitude = 1;
|
|
176
|
+
r->normalized = 1;
|
|
177
|
+
|
|
178
|
+
SECP256K1_FE_VERIFY(r);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
static void nostr_native_secp256k1_fe_impl_normalize_weak(nostr_native_secp256k1_fe *r);
|
|
182
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_normalize_weak(nostr_native_secp256k1_fe *r) {
|
|
183
|
+
SECP256K1_FE_VERIFY(r);
|
|
184
|
+
|
|
185
|
+
nostr_native_secp256k1_fe_impl_normalize_weak(r);
|
|
186
|
+
r->magnitude = 1;
|
|
187
|
+
|
|
188
|
+
SECP256K1_FE_VERIFY(r);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static void nostr_native_secp256k1_fe_impl_normalize_var(nostr_native_secp256k1_fe *r);
|
|
192
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_normalize_var(nostr_native_secp256k1_fe *r) {
|
|
193
|
+
SECP256K1_FE_VERIFY(r);
|
|
194
|
+
|
|
195
|
+
nostr_native_secp256k1_fe_impl_normalize_var(r);
|
|
196
|
+
r->magnitude = 1;
|
|
197
|
+
r->normalized = 1;
|
|
198
|
+
|
|
199
|
+
SECP256K1_FE_VERIFY(r);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
static int nostr_native_secp256k1_fe_impl_normalizes_to_zero(const nostr_native_secp256k1_fe *r);
|
|
203
|
+
SECP256K1_INLINE static int nostr_native_secp256k1_fe_normalizes_to_zero(const nostr_native_secp256k1_fe *r) {
|
|
204
|
+
SECP256K1_FE_VERIFY(r);
|
|
205
|
+
|
|
206
|
+
return nostr_native_secp256k1_fe_impl_normalizes_to_zero(r);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
static int nostr_native_secp256k1_fe_impl_normalizes_to_zero_var(const nostr_native_secp256k1_fe *r);
|
|
210
|
+
SECP256K1_INLINE static int nostr_native_secp256k1_fe_normalizes_to_zero_var(const nostr_native_secp256k1_fe *r) {
|
|
211
|
+
SECP256K1_FE_VERIFY(r);
|
|
212
|
+
|
|
213
|
+
return nostr_native_secp256k1_fe_impl_normalizes_to_zero_var(r);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
static void nostr_native_secp256k1_fe_impl_set_int(nostr_native_secp256k1_fe *r, int a);
|
|
217
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_set_int(nostr_native_secp256k1_fe *r, int a) {
|
|
218
|
+
VERIFY_CHECK(0 <= a && a <= 0x7FFF);
|
|
219
|
+
|
|
220
|
+
nostr_native_secp256k1_fe_impl_set_int(r, a);
|
|
221
|
+
r->magnitude = (a != 0);
|
|
222
|
+
r->normalized = 1;
|
|
223
|
+
|
|
224
|
+
SECP256K1_FE_VERIFY(r);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
static void nostr_native_secp256k1_fe_impl_add_int(nostr_native_secp256k1_fe *r, int a);
|
|
228
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_add_int(nostr_native_secp256k1_fe *r, int a) {
|
|
229
|
+
VERIFY_CHECK(0 <= a && a <= 0x7FFF);
|
|
230
|
+
SECP256K1_FE_VERIFY(r);
|
|
231
|
+
|
|
232
|
+
nostr_native_secp256k1_fe_impl_add_int(r, a);
|
|
233
|
+
r->magnitude += 1;
|
|
234
|
+
r->normalized = 0;
|
|
235
|
+
|
|
236
|
+
SECP256K1_FE_VERIFY(r);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
static int nostr_native_secp256k1_fe_impl_is_zero(const nostr_native_secp256k1_fe *a);
|
|
240
|
+
SECP256K1_INLINE static int nostr_native_secp256k1_fe_is_zero(const nostr_native_secp256k1_fe *a) {
|
|
241
|
+
SECP256K1_FE_VERIFY(a);
|
|
242
|
+
VERIFY_CHECK(a->normalized);
|
|
243
|
+
|
|
244
|
+
return nostr_native_secp256k1_fe_impl_is_zero(a);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
static int nostr_native_secp256k1_fe_impl_is_odd(const nostr_native_secp256k1_fe *a);
|
|
248
|
+
SECP256K1_INLINE static int nostr_native_secp256k1_fe_is_odd(const nostr_native_secp256k1_fe *a) {
|
|
249
|
+
SECP256K1_FE_VERIFY(a);
|
|
250
|
+
VERIFY_CHECK(a->normalized);
|
|
251
|
+
|
|
252
|
+
return nostr_native_secp256k1_fe_impl_is_odd(a);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
static int nostr_native_secp256k1_fe_impl_cmp_var(const nostr_native_secp256k1_fe *a, const nostr_native_secp256k1_fe *b);
|
|
256
|
+
SECP256K1_INLINE static int nostr_native_secp256k1_fe_cmp_var(const nostr_native_secp256k1_fe *a, const nostr_native_secp256k1_fe *b) {
|
|
257
|
+
SECP256K1_FE_VERIFY(a);
|
|
258
|
+
SECP256K1_FE_VERIFY(b);
|
|
259
|
+
VERIFY_CHECK(a->normalized);
|
|
260
|
+
VERIFY_CHECK(b->normalized);
|
|
261
|
+
|
|
262
|
+
return nostr_native_secp256k1_fe_impl_cmp_var(a, b);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
static void nostr_native_secp256k1_fe_impl_set_b32_mod(nostr_native_secp256k1_fe *r, const unsigned char *a);
|
|
266
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_set_b32_mod(nostr_native_secp256k1_fe *r, const unsigned char *a) {
|
|
267
|
+
nostr_native_secp256k1_fe_impl_set_b32_mod(r, a);
|
|
268
|
+
r->magnitude = 1;
|
|
269
|
+
r->normalized = 0;
|
|
270
|
+
|
|
271
|
+
SECP256K1_FE_VERIFY(r);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
static int nostr_native_secp256k1_fe_impl_set_b32_limit(nostr_native_secp256k1_fe *r, const unsigned char *a);
|
|
275
|
+
SECP256K1_INLINE static int nostr_native_secp256k1_fe_set_b32_limit(nostr_native_secp256k1_fe *r, const unsigned char *a) {
|
|
276
|
+
if (nostr_native_secp256k1_fe_impl_set_b32_limit(r, a)) {
|
|
277
|
+
r->magnitude = 1;
|
|
278
|
+
r->normalized = 1;
|
|
279
|
+
SECP256K1_FE_VERIFY(r);
|
|
280
|
+
return 1;
|
|
281
|
+
} else {
|
|
282
|
+
/* Mark the output field element as invalid. */
|
|
283
|
+
r->magnitude = -1;
|
|
284
|
+
return 0;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
static void nostr_native_secp256k1_fe_impl_get_b32(unsigned char *r, const nostr_native_secp256k1_fe *a);
|
|
289
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_get_b32(unsigned char *r, const nostr_native_secp256k1_fe *a) {
|
|
290
|
+
SECP256K1_FE_VERIFY(a);
|
|
291
|
+
VERIFY_CHECK(a->normalized);
|
|
292
|
+
|
|
293
|
+
nostr_native_secp256k1_fe_impl_get_b32(r, a);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
static void nostr_native_secp256k1_fe_impl_negate_unchecked(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a, int m);
|
|
297
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_negate_unchecked(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a, int m) {
|
|
298
|
+
SECP256K1_FE_VERIFY(a);
|
|
299
|
+
VERIFY_CHECK(m >= 0 && m <= 31);
|
|
300
|
+
SECP256K1_FE_VERIFY_MAGNITUDE(a, m);
|
|
301
|
+
|
|
302
|
+
nostr_native_secp256k1_fe_impl_negate_unchecked(r, a, m);
|
|
303
|
+
r->magnitude = m + 1;
|
|
304
|
+
r->normalized = 0;
|
|
305
|
+
|
|
306
|
+
SECP256K1_FE_VERIFY(r);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
static void nostr_native_secp256k1_fe_impl_mul_int_unchecked(nostr_native_secp256k1_fe *r, int a);
|
|
310
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_mul_int_unchecked(nostr_native_secp256k1_fe *r, int a) {
|
|
311
|
+
SECP256K1_FE_VERIFY(r);
|
|
312
|
+
|
|
313
|
+
VERIFY_CHECK(a >= 0 && a <= 32);
|
|
314
|
+
VERIFY_CHECK(a*r->magnitude <= 32);
|
|
315
|
+
nostr_native_secp256k1_fe_impl_mul_int_unchecked(r, a);
|
|
316
|
+
r->magnitude *= a;
|
|
317
|
+
r->normalized = 0;
|
|
318
|
+
|
|
319
|
+
SECP256K1_FE_VERIFY(r);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
static void nostr_native_secp256k1_fe_impl_add(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a);
|
|
323
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_add(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a) {
|
|
324
|
+
SECP256K1_FE_VERIFY(r);
|
|
325
|
+
SECP256K1_FE_VERIFY(a);
|
|
326
|
+
VERIFY_CHECK(r->magnitude + a->magnitude <= 32);
|
|
327
|
+
|
|
328
|
+
nostr_native_secp256k1_fe_impl_add(r, a);
|
|
329
|
+
r->magnitude += a->magnitude;
|
|
330
|
+
r->normalized = 0;
|
|
331
|
+
|
|
332
|
+
SECP256K1_FE_VERIFY(r);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
static void nostr_native_secp256k1_fe_impl_mul(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a, const nostr_native_secp256k1_fe * SECP256K1_RESTRICT b);
|
|
336
|
+
SECP256K1_INLINE 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) {
|
|
337
|
+
SECP256K1_FE_VERIFY(a);
|
|
338
|
+
SECP256K1_FE_VERIFY(b);
|
|
339
|
+
SECP256K1_FE_VERIFY_MAGNITUDE(a, 8);
|
|
340
|
+
SECP256K1_FE_VERIFY_MAGNITUDE(b, 8);
|
|
341
|
+
VERIFY_CHECK(r != b);
|
|
342
|
+
VERIFY_CHECK(a != b);
|
|
343
|
+
|
|
344
|
+
nostr_native_secp256k1_fe_impl_mul(r, a, b);
|
|
345
|
+
r->magnitude = 1;
|
|
346
|
+
r->normalized = 0;
|
|
347
|
+
|
|
348
|
+
SECP256K1_FE_VERIFY(r);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
static void nostr_native_secp256k1_fe_impl_sqr(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a);
|
|
352
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_sqr(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a) {
|
|
353
|
+
SECP256K1_FE_VERIFY(a);
|
|
354
|
+
SECP256K1_FE_VERIFY_MAGNITUDE(a, 8);
|
|
355
|
+
|
|
356
|
+
nostr_native_secp256k1_fe_impl_sqr(r, a);
|
|
357
|
+
r->magnitude = 1;
|
|
358
|
+
r->normalized = 0;
|
|
359
|
+
|
|
360
|
+
SECP256K1_FE_VERIFY(r);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
static void nostr_native_secp256k1_fe_impl_cmov(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a, int flag);
|
|
364
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_cmov(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *a, int flag) {
|
|
365
|
+
VERIFY_CHECK(flag == 0 || flag == 1);
|
|
366
|
+
SECP256K1_FE_VERIFY(a);
|
|
367
|
+
SECP256K1_FE_VERIFY(r);
|
|
368
|
+
|
|
369
|
+
nostr_native_secp256k1_fe_impl_cmov(r, a, flag);
|
|
370
|
+
if (a->magnitude > r->magnitude) r->magnitude = a->magnitude;
|
|
371
|
+
if (!a->normalized) r->normalized = 0;
|
|
372
|
+
|
|
373
|
+
SECP256K1_FE_VERIFY(r);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
static void nostr_native_secp256k1_fe_impl_to_storage(nostr_native_secp256k1_fe_storage *r, const nostr_native_secp256k1_fe *a);
|
|
377
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_to_storage(nostr_native_secp256k1_fe_storage *r, const nostr_native_secp256k1_fe *a) {
|
|
378
|
+
SECP256K1_FE_VERIFY(a);
|
|
379
|
+
VERIFY_CHECK(a->normalized);
|
|
380
|
+
|
|
381
|
+
nostr_native_secp256k1_fe_impl_to_storage(r, a);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
static void nostr_native_secp256k1_fe_impl_from_storage(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe_storage *a);
|
|
385
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_from_storage(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe_storage *a) {
|
|
386
|
+
nostr_native_secp256k1_fe_impl_from_storage(r, a);
|
|
387
|
+
r->magnitude = 1;
|
|
388
|
+
r->normalized = 1;
|
|
389
|
+
|
|
390
|
+
SECP256K1_FE_VERIFY(r);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
static void nostr_native_secp256k1_fe_impl_inv(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *x);
|
|
394
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_inv(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *x) {
|
|
395
|
+
int input_is_zero = nostr_native_secp256k1_fe_normalizes_to_zero(x);
|
|
396
|
+
SECP256K1_FE_VERIFY(x);
|
|
397
|
+
|
|
398
|
+
nostr_native_secp256k1_fe_impl_inv(r, x);
|
|
399
|
+
r->magnitude = x->magnitude > 0;
|
|
400
|
+
r->normalized = 1;
|
|
401
|
+
|
|
402
|
+
VERIFY_CHECK(nostr_native_secp256k1_fe_normalizes_to_zero(r) == input_is_zero);
|
|
403
|
+
SECP256K1_FE_VERIFY(r);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
static void nostr_native_secp256k1_fe_impl_inv_var(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *x);
|
|
407
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_inv_var(nostr_native_secp256k1_fe *r, const nostr_native_secp256k1_fe *x) {
|
|
408
|
+
int input_is_zero = nostr_native_secp256k1_fe_normalizes_to_zero(x);
|
|
409
|
+
SECP256K1_FE_VERIFY(x);
|
|
410
|
+
|
|
411
|
+
nostr_native_secp256k1_fe_impl_inv_var(r, x);
|
|
412
|
+
r->magnitude = x->magnitude > 0;
|
|
413
|
+
r->normalized = 1;
|
|
414
|
+
|
|
415
|
+
VERIFY_CHECK(nostr_native_secp256k1_fe_normalizes_to_zero(r) == input_is_zero);
|
|
416
|
+
SECP256K1_FE_VERIFY(r);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
static int nostr_native_secp256k1_fe_impl_is_square_var(const nostr_native_secp256k1_fe *x);
|
|
420
|
+
SECP256K1_INLINE static int nostr_native_secp256k1_fe_is_square_var(const nostr_native_secp256k1_fe *x) {
|
|
421
|
+
int ret;
|
|
422
|
+
nostr_native_secp256k1_fe tmp = *x, sqrt;
|
|
423
|
+
SECP256K1_FE_VERIFY(x);
|
|
424
|
+
|
|
425
|
+
ret = nostr_native_secp256k1_fe_impl_is_square_var(x);
|
|
426
|
+
nostr_native_secp256k1_fe_normalize_weak(&tmp);
|
|
427
|
+
VERIFY_CHECK(ret == nostr_native_secp256k1_fe_sqrt(&sqrt, &tmp));
|
|
428
|
+
return ret;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
static void nostr_native_secp256k1_fe_impl_get_bounds(nostr_native_secp256k1_fe* r, int m);
|
|
432
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_get_bounds(nostr_native_secp256k1_fe* r, int m) {
|
|
433
|
+
VERIFY_CHECK(m >= 0);
|
|
434
|
+
VERIFY_CHECK(m <= 32);
|
|
435
|
+
|
|
436
|
+
nostr_native_secp256k1_fe_impl_get_bounds(r, m);
|
|
437
|
+
r->magnitude = m;
|
|
438
|
+
r->normalized = (m == 0);
|
|
439
|
+
|
|
440
|
+
SECP256K1_FE_VERIFY(r);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
static void nostr_native_secp256k1_fe_impl_half(nostr_native_secp256k1_fe *r);
|
|
444
|
+
SECP256K1_INLINE static void nostr_native_secp256k1_fe_half(nostr_native_secp256k1_fe *r) {
|
|
445
|
+
SECP256K1_FE_VERIFY(r);
|
|
446
|
+
SECP256K1_FE_VERIFY_MAGNITUDE(r, 31);
|
|
447
|
+
|
|
448
|
+
nostr_native_secp256k1_fe_impl_half(r);
|
|
449
|
+
r->magnitude = (r->magnitude >> 1) + 1;
|
|
450
|
+
r->normalized = 0;
|
|
451
|
+
|
|
452
|
+
SECP256K1_FE_VERIFY(r);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
#endif /* defined(VERIFY) */
|
|
456
|
+
|
|
457
|
+
#endif /* SECP256K1_FIELD_IMPL_H */
|
|
@@ -0,0 +1,218 @@
|
|
|
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_GROUP_H
|
|
8
|
+
#define SECP256K1_GROUP_H
|
|
9
|
+
|
|
10
|
+
#include "field.h"
|
|
11
|
+
|
|
12
|
+
/** A group element in affine coordinates on the secp256k1 curve,
|
|
13
|
+
* or occasionally on an isomorphic curve of the form y^2 = x^3 + 7*t^6.
|
|
14
|
+
* Note: For exhaustive test mode, secp256k1 is replaced by a small subgroup of a different curve.
|
|
15
|
+
*/
|
|
16
|
+
typedef struct {
|
|
17
|
+
nostr_native_secp256k1_fe x;
|
|
18
|
+
nostr_native_secp256k1_fe y;
|
|
19
|
+
int infinity; /* whether this represents the point at infinity */
|
|
20
|
+
} nostr_native_secp256k1_ge;
|
|
21
|
+
|
|
22
|
+
#define SECP256K1_GE_CONST(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) {SECP256K1_FE_CONST((a),(b),(c),(d),(e),(f),(g),(h)), SECP256K1_FE_CONST((i),(j),(k),(l),(m),(n),(o),(p)), 0}
|
|
23
|
+
#define SECP256K1_GE_CONST_INFINITY {SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), 1}
|
|
24
|
+
|
|
25
|
+
/** A group element of the secp256k1 curve, in jacobian coordinates.
|
|
26
|
+
* Note: For exhastive test mode, secp256k1 is replaced by a small subgroup of a different curve.
|
|
27
|
+
*/
|
|
28
|
+
typedef struct {
|
|
29
|
+
nostr_native_secp256k1_fe x; /* actual X: x/z^2 */
|
|
30
|
+
nostr_native_secp256k1_fe y; /* actual Y: y/z^3 */
|
|
31
|
+
nostr_native_secp256k1_fe z;
|
|
32
|
+
int infinity; /* whether this represents the point at infinity */
|
|
33
|
+
} nostr_native_secp256k1_gej;
|
|
34
|
+
|
|
35
|
+
#define SECP256K1_GEJ_CONST(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) {SECP256K1_FE_CONST((a),(b),(c),(d),(e),(f),(g),(h)), SECP256K1_FE_CONST((i),(j),(k),(l),(m),(n),(o),(p)), SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1), 0}
|
|
36
|
+
#define SECP256K1_GEJ_CONST_INFINITY {SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), 1}
|
|
37
|
+
|
|
38
|
+
typedef struct {
|
|
39
|
+
nostr_native_secp256k1_fe_storage x;
|
|
40
|
+
nostr_native_secp256k1_fe_storage y;
|
|
41
|
+
} nostr_native_secp256k1_ge_storage;
|
|
42
|
+
|
|
43
|
+
#define SECP256K1_GE_STORAGE_CONST(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) {SECP256K1_FE_STORAGE_CONST((a),(b),(c),(d),(e),(f),(g),(h)), SECP256K1_FE_STORAGE_CONST((i),(j),(k),(l),(m),(n),(o),(p))}
|
|
44
|
+
|
|
45
|
+
#define SECP256K1_GE_STORAGE_CONST_GET(t) SECP256K1_FE_STORAGE_CONST_GET(t.x), SECP256K1_FE_STORAGE_CONST_GET(t.y)
|
|
46
|
+
|
|
47
|
+
/** Maximum allowed magnitudes for group element coordinates
|
|
48
|
+
* in affine (x, y) and jacobian (x, y, z) representation. */
|
|
49
|
+
#define SECP256K1_GE_X_MAGNITUDE_MAX 4
|
|
50
|
+
#define SECP256K1_GE_Y_MAGNITUDE_MAX 3
|
|
51
|
+
#define SECP256K1_GEJ_X_MAGNITUDE_MAX 4
|
|
52
|
+
#define SECP256K1_GEJ_Y_MAGNITUDE_MAX 4
|
|
53
|
+
#define SECP256K1_GEJ_Z_MAGNITUDE_MAX 1
|
|
54
|
+
|
|
55
|
+
/** Set a group element equal to the point with given X and Y coordinates */
|
|
56
|
+
static void nostr_native_secp256k1_ge_set_xy(nostr_native_secp256k1_ge *r, const nostr_native_secp256k1_fe *x, const nostr_native_secp256k1_fe *y);
|
|
57
|
+
|
|
58
|
+
/** Set a group element (affine) equal to the point with the given X coordinate, and given oddness
|
|
59
|
+
* for Y. Return value indicates whether the result is valid. */
|
|
60
|
+
static int nostr_native_secp256k1_ge_set_xo_var(nostr_native_secp256k1_ge *r, const nostr_native_secp256k1_fe *x, int odd);
|
|
61
|
+
|
|
62
|
+
/** Determine whether x is a valid X coordinate on the curve. */
|
|
63
|
+
static int nostr_native_secp256k1_ge_x_on_curve_var(const nostr_native_secp256k1_fe *x);
|
|
64
|
+
|
|
65
|
+
/** Determine whether fraction xn/xd is a valid X coordinate on the curve (xd != 0). */
|
|
66
|
+
static int nostr_native_secp256k1_ge_x_frac_on_curve_var(const nostr_native_secp256k1_fe *xn, const nostr_native_secp256k1_fe *xd);
|
|
67
|
+
|
|
68
|
+
/** Check whether a group element is the point at infinity. */
|
|
69
|
+
static int nostr_native_secp256k1_ge_is_infinity(const nostr_native_secp256k1_ge *a);
|
|
70
|
+
|
|
71
|
+
/** Check whether a group element is valid (i.e., on the curve). */
|
|
72
|
+
static int nostr_native_secp256k1_ge_is_valid_var(const nostr_native_secp256k1_ge *a);
|
|
73
|
+
|
|
74
|
+
/** Set r equal to the inverse of a (i.e., mirrored around the X axis) */
|
|
75
|
+
static void nostr_native_secp256k1_ge_neg(nostr_native_secp256k1_ge *r, const nostr_native_secp256k1_ge *a);
|
|
76
|
+
|
|
77
|
+
/** Set a group element equal to another which is given in jacobian coordinates. Constant time. */
|
|
78
|
+
static void nostr_native_secp256k1_ge_set_gej(nostr_native_secp256k1_ge *r, nostr_native_secp256k1_gej *a);
|
|
79
|
+
|
|
80
|
+
/** Set a group element equal to another which is given in jacobian coordinates. */
|
|
81
|
+
static void nostr_native_secp256k1_ge_set_gej_var(nostr_native_secp256k1_ge *r, nostr_native_secp256k1_gej *a);
|
|
82
|
+
|
|
83
|
+
/** Set group elements r[0:len] (affine) equal to group elements a[0:len] (jacobian).
|
|
84
|
+
* None of the group elements in a[0:len] may be infinity. Constant time. */
|
|
85
|
+
static void nostr_native_secp256k1_ge_set_all_gej(nostr_native_secp256k1_ge *r, const nostr_native_secp256k1_gej *a, size_t len);
|
|
86
|
+
|
|
87
|
+
/** Set group elements r[0:len] (affine) equal to group elements a[0:len] (jacobian). */
|
|
88
|
+
static void nostr_native_secp256k1_ge_set_all_gej_var(nostr_native_secp256k1_ge *r, const nostr_native_secp256k1_gej *a, size_t len);
|
|
89
|
+
|
|
90
|
+
/** Bring a batch of inputs to the same global z "denominator", based on ratios between
|
|
91
|
+
* (omitted) z coordinates of adjacent elements.
|
|
92
|
+
*
|
|
93
|
+
* Although the elements a[i] are _ge rather than _gej, they actually represent elements
|
|
94
|
+
* in Jacobian coordinates with their z coordinates omitted.
|
|
95
|
+
*
|
|
96
|
+
* Using the notation z(b) to represent the omitted z coordinate of b, the array zr of
|
|
97
|
+
* z coordinate ratios must satisfy zr[i] == z(a[i]) / z(a[i-1]) for 0 < 'i' < len.
|
|
98
|
+
* The zr[0] value is unused.
|
|
99
|
+
*
|
|
100
|
+
* This function adjusts the coordinates of 'a' in place so that for all 'i', z(a[i]) == z(a[len-1]).
|
|
101
|
+
* In other words, the initial value of z(a[len-1]) becomes the global z "denominator". Only the
|
|
102
|
+
* a[i].x and a[i].y coordinates are explicitly modified; the adjustment of the omitted z coordinate is
|
|
103
|
+
* implicit.
|
|
104
|
+
*
|
|
105
|
+
* The coordinates of the final element a[len-1] are not changed.
|
|
106
|
+
*/
|
|
107
|
+
static void nostr_native_secp256k1_ge_table_set_globalz(size_t len, nostr_native_secp256k1_ge *a, const nostr_native_secp256k1_fe *zr);
|
|
108
|
+
|
|
109
|
+
/** Check two group elements (affine) for equality in variable time. */
|
|
110
|
+
static int nostr_native_secp256k1_ge_eq_var(const nostr_native_secp256k1_ge *a, const nostr_native_secp256k1_ge *b);
|
|
111
|
+
|
|
112
|
+
/** Set a group element (affine) equal to the point at infinity. */
|
|
113
|
+
static void nostr_native_secp256k1_ge_set_infinity(nostr_native_secp256k1_ge *r);
|
|
114
|
+
|
|
115
|
+
/** Set a group element (jacobian) equal to the point at infinity. */
|
|
116
|
+
static void nostr_native_secp256k1_gej_set_infinity(nostr_native_secp256k1_gej *r);
|
|
117
|
+
|
|
118
|
+
/** Set a group element (jacobian) equal to another which is given in affine coordinates. */
|
|
119
|
+
static void nostr_native_secp256k1_gej_set_ge(nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_ge *a);
|
|
120
|
+
|
|
121
|
+
/** Check two group elements (jacobian) for equality in variable time. */
|
|
122
|
+
static int nostr_native_secp256k1_gej_eq_var(const nostr_native_secp256k1_gej *a, const nostr_native_secp256k1_gej *b);
|
|
123
|
+
|
|
124
|
+
/** Check two group elements (jacobian and affine) for equality in variable time. */
|
|
125
|
+
static int nostr_native_secp256k1_gej_eq_ge_var(const nostr_native_secp256k1_gej *a, const nostr_native_secp256k1_ge *b);
|
|
126
|
+
|
|
127
|
+
/** Compare the X coordinate of a group element (jacobian).
|
|
128
|
+
* The magnitude of the group element's X coordinate must not exceed 31. */
|
|
129
|
+
static int nostr_native_secp256k1_gej_eq_x_var(const nostr_native_secp256k1_fe *x, const nostr_native_secp256k1_gej *a);
|
|
130
|
+
|
|
131
|
+
/** Set r equal to the inverse of a (i.e., mirrored around the X axis) */
|
|
132
|
+
static void nostr_native_secp256k1_gej_neg(nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_gej *a);
|
|
133
|
+
|
|
134
|
+
/** Check whether a group element is the point at infinity. */
|
|
135
|
+
static int nostr_native_secp256k1_gej_is_infinity(const nostr_native_secp256k1_gej *a);
|
|
136
|
+
|
|
137
|
+
/** Set r equal to the double of a. Constant time. */
|
|
138
|
+
static void nostr_native_secp256k1_gej_double(nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_gej *a);
|
|
139
|
+
|
|
140
|
+
/** Set r equal to the double of a. If rzr is not-NULL this sets *rzr such that r->z == a->z * *rzr (where infinity means an implicit z = 0). */
|
|
141
|
+
static void nostr_native_secp256k1_gej_double_var(nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_gej *a, nostr_native_secp256k1_fe *rzr);
|
|
142
|
+
|
|
143
|
+
/** Set r equal to the sum of a and b. If rzr is non-NULL this sets *rzr such that r->z == a->z * *rzr (a cannot be infinity in that case). */
|
|
144
|
+
static void nostr_native_secp256k1_gej_add_var(nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_gej *a, const nostr_native_secp256k1_gej *b, nostr_native_secp256k1_fe *rzr);
|
|
145
|
+
|
|
146
|
+
/** Set r equal to the sum of a and b (with b given in affine coordinates, and not infinity). */
|
|
147
|
+
static void nostr_native_secp256k1_gej_add_ge(nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_gej *a, const nostr_native_secp256k1_ge *b);
|
|
148
|
+
|
|
149
|
+
/** Set r equal to the sum of a and b (with b given in affine coordinates). This is more efficient
|
|
150
|
+
than nostr_native_secp256k1_gej_add_var. It is identical to nostr_native_secp256k1_gej_add_ge but without constant-time
|
|
151
|
+
guarantee, and b is allowed to be infinity. If rzr is non-NULL this sets *rzr such that r->z == a->z * *rzr (a cannot be infinity in that case). */
|
|
152
|
+
static void nostr_native_secp256k1_gej_add_ge_var(nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_gej *a, const nostr_native_secp256k1_ge *b, nostr_native_secp256k1_fe *rzr);
|
|
153
|
+
|
|
154
|
+
/** Set r equal to the sum of a and b (with the inverse of b's Z coordinate passed as bzinv). */
|
|
155
|
+
static void nostr_native_secp256k1_gej_add_zinv_var(nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_gej *a, const nostr_native_secp256k1_ge *b, const nostr_native_secp256k1_fe *bzinv);
|
|
156
|
+
|
|
157
|
+
/** Set r to be equal to lambda times a, where lambda is chosen in a way such that this is very fast. */
|
|
158
|
+
static void nostr_native_secp256k1_ge_mul_lambda(nostr_native_secp256k1_ge *r, const nostr_native_secp256k1_ge *a);
|
|
159
|
+
|
|
160
|
+
/** Clear a nostr_native_secp256k1_gej to prevent leaking sensitive information. */
|
|
161
|
+
static void nostr_native_secp256k1_gej_clear(nostr_native_secp256k1_gej *r);
|
|
162
|
+
|
|
163
|
+
/** Clear a nostr_native_secp256k1_ge to prevent leaking sensitive information. */
|
|
164
|
+
static void nostr_native_secp256k1_ge_clear(nostr_native_secp256k1_ge *r);
|
|
165
|
+
|
|
166
|
+
/** Convert a group element to the storage type. */
|
|
167
|
+
static void nostr_native_secp256k1_ge_to_storage(nostr_native_secp256k1_ge_storage *r, const nostr_native_secp256k1_ge *a);
|
|
168
|
+
|
|
169
|
+
/** Convert a group element back from the storage type. */
|
|
170
|
+
static void nostr_native_secp256k1_ge_from_storage(nostr_native_secp256k1_ge *r, const nostr_native_secp256k1_ge_storage *a);
|
|
171
|
+
|
|
172
|
+
/** If flag is 1, set *r equal to *a; if flag is 0, leave it. Constant-time.
|
|
173
|
+
* Both *r and *a must be initialized. Flag must be 0 or 1. */
|
|
174
|
+
static void nostr_native_secp256k1_gej_cmov(nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_gej *a, int flag);
|
|
175
|
+
|
|
176
|
+
/** If flag is 1, set *r equal to *a; if flag is 0, leave it. Constant-time.
|
|
177
|
+
* Both *r and *a must be initialized. Flag must be 0 or 1. */
|
|
178
|
+
static void nostr_native_secp256k1_ge_storage_cmov(nostr_native_secp256k1_ge_storage *r, const nostr_native_secp256k1_ge_storage *a, int flag);
|
|
179
|
+
|
|
180
|
+
/** Rescale a jacobian point by b which must be non-zero. Constant-time. */
|
|
181
|
+
static void nostr_native_secp256k1_gej_rescale(nostr_native_secp256k1_gej *r, const nostr_native_secp256k1_fe *b);
|
|
182
|
+
|
|
183
|
+
/** Convert a group element that is not infinity to a 64-byte array. The output
|
|
184
|
+
* array is platform-dependent. */
|
|
185
|
+
static void nostr_native_secp256k1_ge_to_bytes(unsigned char *buf, const nostr_native_secp256k1_ge *a);
|
|
186
|
+
|
|
187
|
+
/** Convert a 64-byte array into group element. This function assumes that the
|
|
188
|
+
* provided buffer correctly encodes a group element. */
|
|
189
|
+
static void nostr_native_secp256k1_ge_from_bytes(nostr_native_secp256k1_ge *r, const unsigned char *buf);
|
|
190
|
+
|
|
191
|
+
/** Convert a group element (that is allowed to be infinity) to a 64-byte
|
|
192
|
+
* array. The output array is platform-dependent. */
|
|
193
|
+
static void nostr_native_secp256k1_ge_to_bytes_ext(unsigned char *data, const nostr_native_secp256k1_ge *ge);
|
|
194
|
+
|
|
195
|
+
/** Convert a 64-byte array into a group element. This function assumes that the
|
|
196
|
+
* provided buffer is the output of nostr_native_secp256k1_ge_to_bytes_ext. */
|
|
197
|
+
static void nostr_native_secp256k1_ge_from_bytes_ext(nostr_native_secp256k1_ge *ge, const unsigned char *data);
|
|
198
|
+
|
|
199
|
+
/** Determine if a point (which is assumed to be on the curve) is in the correct (sub)group of the curve.
|
|
200
|
+
*
|
|
201
|
+
* In normal mode, the used group is secp256k1, which has cofactor=1 meaning that every point on the curve is in the
|
|
202
|
+
* group, and this function returns always true.
|
|
203
|
+
*
|
|
204
|
+
* When compiling in exhaustive test mode, a slightly different curve equation is used, leading to a group with a
|
|
205
|
+
* (very) small subgroup, and that subgroup is what is used for all cryptographic operations. In that mode, this
|
|
206
|
+
* function checks whether a point that is on the curve is in fact also in that subgroup.
|
|
207
|
+
*/
|
|
208
|
+
static int nostr_native_secp256k1_ge_is_in_correct_subgroup(const nostr_native_secp256k1_ge* ge);
|
|
209
|
+
|
|
210
|
+
/** Check invariants on an affine group element (no-op unless VERIFY is enabled). */
|
|
211
|
+
static void nostr_native_secp256k1_ge_verify(const nostr_native_secp256k1_ge *a);
|
|
212
|
+
#define SECP256K1_GE_VERIFY(a) nostr_native_secp256k1_ge_verify(a)
|
|
213
|
+
|
|
214
|
+
/** Check invariants on a Jacobian group element (no-op unless VERIFY is enabled). */
|
|
215
|
+
static void nostr_native_secp256k1_gej_verify(const nostr_native_secp256k1_gej *a);
|
|
216
|
+
#define SECP256K1_GEJ_VERIFY(a) nostr_native_secp256k1_gej_verify(a)
|
|
217
|
+
|
|
218
|
+
#endif /* SECP256K1_GROUP_H */
|