@authon/react-native-wallet 0.0.0-stage → 0.1.0
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/README.md +221 -2
- package/android/build.gradle +40 -0
- package/android/consumer-rules.pro +10 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/dev/authon/reactnative/wallet/AuthonWalletModule.kt +181 -0
- package/android/src/vendor/GENERATED.md +4 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/AuthonWallet.kt +191 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Bytes.kt +74 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/CredentialManagerPasskeys.kt +192 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/DeviceStore.kt +170 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/DialogWalletApprover.kt +141 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Eip712.kt +176 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/EvmAccount.kt +45 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/EvmTransaction.kt +180 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/GfTables.kt +44 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/HdWallet.kt +130 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Http.kt +82 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/JsValues.kt +83 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/KeystoreDeviceStore.kt +232 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Prf.kt +111 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Proof.kt +134 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Shamir.kt +106 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/SignSummary.kt +335 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletApi.kt +353 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletError.kt +58 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletOps.kt +449 -0
- package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletTypes.kt +71 -0
- package/build/authonContext.d.ts +19 -0
- package/build/authonContext.d.ts.map +1 -0
- package/build/authonContext.js +20 -0
- package/build/authonContext.js.map +1 -0
- package/build/client.d.ts +76 -0
- package/build/client.d.ts.map +1 -0
- package/build/client.js +349 -0
- package/build/client.js.map +1 -0
- package/build/errors.d.ts +18 -0
- package/build/errors.d.ts.map +1 -0
- package/build/errors.js +9 -0
- package/build/errors.js.map +1 -0
- package/build/index.d.ts +7 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +4 -0
- package/build/index.js.map +1 -0
- package/build/native.d.ts +39 -0
- package/build/native.d.ts.map +1 -0
- package/build/native.js +12 -0
- package/build/native.js.map +1 -0
- package/build/types.d.ts +121 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +3 -0
- package/build/types.js.map +1 -0
- package/build/useAuthonWallet.d.ts +25 -0
- package/build/useAuthonWallet.d.ts.map +1 -0
- package/build/useAuthonWallet.js +77 -0
- package/build/useAuthonWallet.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/AuthonReactNativeWallet.podspec +37 -0
- package/ios/AuthonWalletModule.swift +190 -0
- package/ios/vendor/AuthonWallet/AuthonWallet.swift +212 -0
- package/ios/vendor/AuthonWallet/Crypto/BIP39Wordlist.swift +262 -0
- package/ios/vendor/AuthonWallet/Crypto/HDKey.swift +108 -0
- package/ios/vendor/AuthonWallet/Crypto/Secp256k1.swift +104 -0
- package/ios/vendor/AuthonWallet/Crypto/Shamir.swift +123 -0
- package/ios/vendor/AuthonWallet/EVM/Address.swift +57 -0
- package/ios/vendor/AuthonWallet/EVM/EvmAccount.swift +48 -0
- package/ios/vendor/AuthonWallet/EVM/Transaction.swift +165 -0
- package/ios/vendor/AuthonWallet/EVM/TypedData.swift +193 -0
- package/ios/vendor/AuthonWallet/Support/BigInteger.swift +220 -0
- package/ios/vendor/AuthonWallet/Support/Bytes.swift +89 -0
- package/ios/vendor/AuthonWallet/Support/JSONValue.swift +341 -0
- package/ios/vendor/AuthonWallet/UI/WalletApprover.swift +250 -0
- package/ios/vendor/AuthonWallet/Wallet/Backup.swift +69 -0
- package/ios/vendor/AuthonWallet/Wallet/DeviceStore.swift +351 -0
- package/ios/vendor/AuthonWallet/Wallet/Passkeys.swift +190 -0
- package/ios/vendor/AuthonWallet/Wallet/Proof.swift +143 -0
- package/ios/vendor/AuthonWallet/Wallet/Shares.swift +116 -0
- package/ios/vendor/AuthonWallet/Wallet/Summary.swift +342 -0
- package/ios/vendor/AuthonWallet/Wallet/WalletAPI.swift +350 -0
- package/ios/vendor/AuthonWallet/Wallet/WalletOps.swift +555 -0
- package/ios/vendor/AuthonWallet/WalletError.swift +54 -0
- package/ios/vendor/CKeccak/LICENSE +22 -0
- package/ios/vendor/CKeccak/README.md +27 -0
- package/ios/vendor/CKeccak/byte_order.h +59 -0
- package/ios/vendor/CKeccak/include/ckeccak.h +9 -0
- package/ios/vendor/CKeccak/memzero.c +75 -0
- package/ios/vendor/CKeccak/memzero.h +8 -0
- package/ios/vendor/CKeccak/options.h +99 -0
- package/ios/vendor/CKeccak/sha3.c +413 -0
- package/ios/vendor/CKeccak/sha3.h +89 -0
- package/ios/vendor/CSecp256k1/COPYING +19 -0
- package/ios/vendor/CSecp256k1/README.md +35 -0
- package/ios/vendor/CSecp256k1/build/precomputed_ecmult.c +3 -0
- package/ios/vendor/CSecp256k1/build/precomputed_ecmult_gen.c +3 -0
- package/ios/vendor/CSecp256k1/build/secp256k1.c +3 -0
- package/ios/vendor/CSecp256k1/include/secp256k1.h +899 -0
- package/ios/vendor/CSecp256k1/include/secp256k1_preallocated.h +134 -0
- package/ios/vendor/CSecp256k1/include/secp256k1_recovery.h +113 -0
- package/ios/vendor/CSecp256k1/src/assumptions.h +87 -0
- package/ios/vendor/CSecp256k1/src/checkmem.h +102 -0
- package/ios/vendor/CSecp256k1/src/ecdsa.h +21 -0
- package/ios/vendor/CSecp256k1/src/ecdsa_impl.h +304 -0
- package/ios/vendor/CSecp256k1/src/eckey.h +25 -0
- package/ios/vendor/CSecp256k1/src/eckey_impl.h +92 -0
- package/ios/vendor/CSecp256k1/src/ecmult.h +61 -0
- package/ios/vendor/CSecp256k1/src/ecmult_const.h +38 -0
- package/ios/vendor/CSecp256k1/src/ecmult_const_impl.h +399 -0
- package/ios/vendor/CSecp256k1/src/ecmult_gen.h +143 -0
- package/ios/vendor/CSecp256k1/src/ecmult_gen_impl.h +341 -0
- package/ios/vendor/CSecp256k1/src/ecmult_impl.h +853 -0
- package/ios/vendor/CSecp256k1/src/field.h +350 -0
- package/ios/vendor/CSecp256k1/src/field_10x26.h +57 -0
- package/ios/vendor/CSecp256k1/src/field_10x26_impl.h +1232 -0
- package/ios/vendor/CSecp256k1/src/field_5x52.h +62 -0
- package/ios/vendor/CSecp256k1/src/field_5x52_impl.h +522 -0
- package/ios/vendor/CSecp256k1/src/field_5x52_int128_impl.h +274 -0
- package/ios/vendor/CSecp256k1/src/field_impl.h +457 -0
- package/ios/vendor/CSecp256k1/src/group.h +212 -0
- package/ios/vendor/CSecp256k1/src/group_impl.h +974 -0
- package/ios/vendor/CSecp256k1/src/hash.h +44 -0
- package/ios/vendor/CSecp256k1/src/hash_impl.h +299 -0
- package/ios/vendor/CSecp256k1/src/hsort.h +33 -0
- package/ios/vendor/CSecp256k1/src/hsort_impl.h +125 -0
- package/ios/vendor/CSecp256k1/src/int128.h +90 -0
- package/ios/vendor/CSecp256k1/src/int128_impl.h +18 -0
- package/ios/vendor/CSecp256k1/src/int128_native.h +19 -0
- package/ios/vendor/CSecp256k1/src/int128_native_impl.h +94 -0
- package/ios/vendor/CSecp256k1/src/int128_struct.h +14 -0
- package/ios/vendor/CSecp256k1/src/int128_struct_impl.h +205 -0
- package/ios/vendor/CSecp256k1/src/modinv32.h +43 -0
- package/ios/vendor/CSecp256k1/src/modinv32_impl.h +725 -0
- package/ios/vendor/CSecp256k1/src/modinv64.h +47 -0
- package/ios/vendor/CSecp256k1/src/modinv64_impl.h +780 -0
- package/ios/vendor/CSecp256k1/src/modules/recovery/main_impl.h +159 -0
- package/ios/vendor/CSecp256k1/src/precomputed_ecmult.c +16456 -0
- package/ios/vendor/CSecp256k1/src/precomputed_ecmult.h +38 -0
- package/ios/vendor/CSecp256k1/src/precomputed_ecmult_gen.c +1779 -0
- package/ios/vendor/CSecp256k1/src/precomputed_ecmult_gen.h +26 -0
- package/ios/vendor/CSecp256k1/src/scalar.h +105 -0
- package/ios/vendor/CSecp256k1/src/scalar_4x64.h +19 -0
- package/ios/vendor/CSecp256k1/src/scalar_4x64_impl.h +1000 -0
- package/ios/vendor/CSecp256k1/src/scalar_8x32.h +19 -0
- package/ios/vendor/CSecp256k1/src/scalar_8x32_impl.h +816 -0
- package/ios/vendor/CSecp256k1/src/scalar_impl.h +321 -0
- package/ios/vendor/CSecp256k1/src/scalar_low.h +24 -0
- package/ios/vendor/CSecp256k1/src/scalar_low_impl.h +206 -0
- package/ios/vendor/CSecp256k1/src/scratch.h +44 -0
- package/ios/vendor/CSecp256k1/src/scratch_impl.h +99 -0
- package/ios/vendor/CSecp256k1/src/secp256k1.c +831 -0
- package/ios/vendor/CSecp256k1/src/selftest.h +32 -0
- package/ios/vendor/CSecp256k1/src/util.h +451 -0
- package/ios/vendor/GENERATED.md +4 -0
- package/package.json +65 -3
- package/src/authonContext.ts +30 -0
- package/src/client.ts +375 -0
- package/src/errors.ts +52 -0
- package/src/index.ts +21 -0
- package/src/native.ts +50 -0
- package/src/types.ts +120 -0
- package/src/useAuthonWallet.ts +103 -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 secp256k1_fe_clear(secp256k1_fe *a) {
|
|
22
|
+
secp256k1_memclear(a, sizeof(secp256k1_fe));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
SECP256K1_INLINE static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b) {
|
|
26
|
+
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, 31);
|
|
31
|
+
|
|
32
|
+
secp256k1_fe_negate(&na, a, 1);
|
|
33
|
+
secp256k1_fe_add(&na, b);
|
|
34
|
+
return secp256k1_fe_normalizes_to_zero(&na);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static int secp256k1_fe_sqrt(secp256k1_fe * SECP256K1_RESTRICT r, const 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
|
+
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
|
+
secp256k1_fe_sqr(&x2, a);
|
|
60
|
+
secp256k1_fe_mul(&x2, &x2, a);
|
|
61
|
+
|
|
62
|
+
secp256k1_fe_sqr(&x3, &x2);
|
|
63
|
+
secp256k1_fe_mul(&x3, &x3, a);
|
|
64
|
+
|
|
65
|
+
x6 = x3;
|
|
66
|
+
for (j=0; j<3; j++) {
|
|
67
|
+
secp256k1_fe_sqr(&x6, &x6);
|
|
68
|
+
}
|
|
69
|
+
secp256k1_fe_mul(&x6, &x6, &x3);
|
|
70
|
+
|
|
71
|
+
x9 = x6;
|
|
72
|
+
for (j=0; j<3; j++) {
|
|
73
|
+
secp256k1_fe_sqr(&x9, &x9);
|
|
74
|
+
}
|
|
75
|
+
secp256k1_fe_mul(&x9, &x9, &x3);
|
|
76
|
+
|
|
77
|
+
x11 = x9;
|
|
78
|
+
for (j=0; j<2; j++) {
|
|
79
|
+
secp256k1_fe_sqr(&x11, &x11);
|
|
80
|
+
}
|
|
81
|
+
secp256k1_fe_mul(&x11, &x11, &x2);
|
|
82
|
+
|
|
83
|
+
x22 = x11;
|
|
84
|
+
for (j=0; j<11; j++) {
|
|
85
|
+
secp256k1_fe_sqr(&x22, &x22);
|
|
86
|
+
}
|
|
87
|
+
secp256k1_fe_mul(&x22, &x22, &x11);
|
|
88
|
+
|
|
89
|
+
x44 = x22;
|
|
90
|
+
for (j=0; j<22; j++) {
|
|
91
|
+
secp256k1_fe_sqr(&x44, &x44);
|
|
92
|
+
}
|
|
93
|
+
secp256k1_fe_mul(&x44, &x44, &x22);
|
|
94
|
+
|
|
95
|
+
x88 = x44;
|
|
96
|
+
for (j=0; j<44; j++) {
|
|
97
|
+
secp256k1_fe_sqr(&x88, &x88);
|
|
98
|
+
}
|
|
99
|
+
secp256k1_fe_mul(&x88, &x88, &x44);
|
|
100
|
+
|
|
101
|
+
x176 = x88;
|
|
102
|
+
for (j=0; j<88; j++) {
|
|
103
|
+
secp256k1_fe_sqr(&x176, &x176);
|
|
104
|
+
}
|
|
105
|
+
secp256k1_fe_mul(&x176, &x176, &x88);
|
|
106
|
+
|
|
107
|
+
x220 = x176;
|
|
108
|
+
for (j=0; j<44; j++) {
|
|
109
|
+
secp256k1_fe_sqr(&x220, &x220);
|
|
110
|
+
}
|
|
111
|
+
secp256k1_fe_mul(&x220, &x220, &x44);
|
|
112
|
+
|
|
113
|
+
x223 = x220;
|
|
114
|
+
for (j=0; j<3; j++) {
|
|
115
|
+
secp256k1_fe_sqr(&x223, &x223);
|
|
116
|
+
}
|
|
117
|
+
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
|
+
secp256k1_fe_sqr(&t1, &t1);
|
|
124
|
+
}
|
|
125
|
+
secp256k1_fe_mul(&t1, &t1, &x22);
|
|
126
|
+
for (j=0; j<6; j++) {
|
|
127
|
+
secp256k1_fe_sqr(&t1, &t1);
|
|
128
|
+
}
|
|
129
|
+
secp256k1_fe_mul(&t1, &t1, &x2);
|
|
130
|
+
secp256k1_fe_sqr(&t1, &t1);
|
|
131
|
+
secp256k1_fe_sqr(r, &t1);
|
|
132
|
+
|
|
133
|
+
/* Check that a square root was actually calculated */
|
|
134
|
+
|
|
135
|
+
secp256k1_fe_sqr(&t1, r);
|
|
136
|
+
ret = secp256k1_fe_equal(&t1, a);
|
|
137
|
+
|
|
138
|
+
#ifdef VERIFY
|
|
139
|
+
if (!ret) {
|
|
140
|
+
secp256k1_fe_negate(&t1, &t1, 1);
|
|
141
|
+
secp256k1_fe_normalize_var(&t1);
|
|
142
|
+
VERIFY_CHECK(secp256k1_fe_equal(&t1, a));
|
|
143
|
+
}
|
|
144
|
+
#endif
|
|
145
|
+
return ret;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
#ifndef VERIFY
|
|
149
|
+
static void secp256k1_fe_verify(const secp256k1_fe *a) { (void)a; }
|
|
150
|
+
static void secp256k1_fe_verify_magnitude(const secp256k1_fe *a, int m) { (void)a; (void)m; }
|
|
151
|
+
#else
|
|
152
|
+
static void secp256k1_fe_impl_verify(const secp256k1_fe *a);
|
|
153
|
+
static void secp256k1_fe_verify(const 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
|
+
secp256k1_fe_impl_verify(a);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
static void secp256k1_fe_verify_magnitude(const 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 secp256k1_fe_impl_normalize(secp256k1_fe *r);
|
|
171
|
+
SECP256K1_INLINE static void secp256k1_fe_normalize(secp256k1_fe *r) {
|
|
172
|
+
SECP256K1_FE_VERIFY(r);
|
|
173
|
+
|
|
174
|
+
secp256k1_fe_impl_normalize(r);
|
|
175
|
+
r->magnitude = 1;
|
|
176
|
+
r->normalized = 1;
|
|
177
|
+
|
|
178
|
+
SECP256K1_FE_VERIFY(r);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
static void secp256k1_fe_impl_normalize_weak(secp256k1_fe *r);
|
|
182
|
+
SECP256K1_INLINE static void secp256k1_fe_normalize_weak(secp256k1_fe *r) {
|
|
183
|
+
SECP256K1_FE_VERIFY(r);
|
|
184
|
+
|
|
185
|
+
secp256k1_fe_impl_normalize_weak(r);
|
|
186
|
+
r->magnitude = 1;
|
|
187
|
+
|
|
188
|
+
SECP256K1_FE_VERIFY(r);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static void secp256k1_fe_impl_normalize_var(secp256k1_fe *r);
|
|
192
|
+
SECP256K1_INLINE static void secp256k1_fe_normalize_var(secp256k1_fe *r) {
|
|
193
|
+
SECP256K1_FE_VERIFY(r);
|
|
194
|
+
|
|
195
|
+
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 secp256k1_fe_impl_normalizes_to_zero(const secp256k1_fe *r);
|
|
203
|
+
SECP256K1_INLINE static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r) {
|
|
204
|
+
SECP256K1_FE_VERIFY(r);
|
|
205
|
+
|
|
206
|
+
return secp256k1_fe_impl_normalizes_to_zero(r);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
static int secp256k1_fe_impl_normalizes_to_zero_var(const secp256k1_fe *r);
|
|
210
|
+
SECP256K1_INLINE static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r) {
|
|
211
|
+
SECP256K1_FE_VERIFY(r);
|
|
212
|
+
|
|
213
|
+
return secp256k1_fe_impl_normalizes_to_zero_var(r);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
static void secp256k1_fe_impl_set_int(secp256k1_fe *r, int a);
|
|
217
|
+
SECP256K1_INLINE static void secp256k1_fe_set_int(secp256k1_fe *r, int a) {
|
|
218
|
+
VERIFY_CHECK(0 <= a && a <= 0x7FFF);
|
|
219
|
+
|
|
220
|
+
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 secp256k1_fe_impl_add_int(secp256k1_fe *r, int a);
|
|
228
|
+
SECP256K1_INLINE static void secp256k1_fe_add_int(secp256k1_fe *r, int a) {
|
|
229
|
+
VERIFY_CHECK(0 <= a && a <= 0x7FFF);
|
|
230
|
+
SECP256K1_FE_VERIFY(r);
|
|
231
|
+
|
|
232
|
+
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 secp256k1_fe_impl_is_zero(const secp256k1_fe *a);
|
|
240
|
+
SECP256K1_INLINE static int secp256k1_fe_is_zero(const secp256k1_fe *a) {
|
|
241
|
+
SECP256K1_FE_VERIFY(a);
|
|
242
|
+
VERIFY_CHECK(a->normalized);
|
|
243
|
+
|
|
244
|
+
return secp256k1_fe_impl_is_zero(a);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
static int secp256k1_fe_impl_is_odd(const secp256k1_fe *a);
|
|
248
|
+
SECP256K1_INLINE static int secp256k1_fe_is_odd(const secp256k1_fe *a) {
|
|
249
|
+
SECP256K1_FE_VERIFY(a);
|
|
250
|
+
VERIFY_CHECK(a->normalized);
|
|
251
|
+
|
|
252
|
+
return secp256k1_fe_impl_is_odd(a);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
static int secp256k1_fe_impl_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b);
|
|
256
|
+
SECP256K1_INLINE static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const 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 secp256k1_fe_impl_cmp_var(a, b);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
static void secp256k1_fe_impl_set_b32_mod(secp256k1_fe *r, const unsigned char *a);
|
|
266
|
+
SECP256K1_INLINE static void secp256k1_fe_set_b32_mod(secp256k1_fe *r, const unsigned char *a) {
|
|
267
|
+
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 secp256k1_fe_impl_set_b32_limit(secp256k1_fe *r, const unsigned char *a);
|
|
275
|
+
SECP256K1_INLINE static int secp256k1_fe_set_b32_limit(secp256k1_fe *r, const unsigned char *a) {
|
|
276
|
+
if (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 secp256k1_fe_impl_get_b32(unsigned char *r, const secp256k1_fe *a);
|
|
289
|
+
SECP256K1_INLINE static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a) {
|
|
290
|
+
SECP256K1_FE_VERIFY(a);
|
|
291
|
+
VERIFY_CHECK(a->normalized);
|
|
292
|
+
|
|
293
|
+
secp256k1_fe_impl_get_b32(r, a);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
static void secp256k1_fe_impl_negate_unchecked(secp256k1_fe *r, const secp256k1_fe *a, int m);
|
|
297
|
+
SECP256K1_INLINE static void secp256k1_fe_negate_unchecked(secp256k1_fe *r, const 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
|
+
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 secp256k1_fe_impl_mul_int_unchecked(secp256k1_fe *r, int a);
|
|
310
|
+
SECP256K1_INLINE static void secp256k1_fe_mul_int_unchecked(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
|
+
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 secp256k1_fe_impl_add(secp256k1_fe *r, const secp256k1_fe *a);
|
|
323
|
+
SECP256K1_INLINE static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a) {
|
|
324
|
+
SECP256K1_FE_VERIFY(r);
|
|
325
|
+
SECP256K1_FE_VERIFY(a);
|
|
326
|
+
VERIFY_CHECK(r->magnitude + a->magnitude <= 32);
|
|
327
|
+
|
|
328
|
+
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 secp256k1_fe_impl_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe * SECP256K1_RESTRICT b);
|
|
336
|
+
SECP256K1_INLINE static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const 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
|
+
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 secp256k1_fe_impl_sqr(secp256k1_fe *r, const secp256k1_fe *a);
|
|
352
|
+
SECP256K1_INLINE static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a) {
|
|
353
|
+
SECP256K1_FE_VERIFY(a);
|
|
354
|
+
SECP256K1_FE_VERIFY_MAGNITUDE(a, 8);
|
|
355
|
+
|
|
356
|
+
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 secp256k1_fe_impl_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag);
|
|
364
|
+
SECP256K1_INLINE static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag) {
|
|
365
|
+
VERIFY_CHECK(flag == 0 || flag == 1);
|
|
366
|
+
SECP256K1_FE_VERIFY(a);
|
|
367
|
+
SECP256K1_FE_VERIFY(r);
|
|
368
|
+
|
|
369
|
+
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 secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a);
|
|
377
|
+
SECP256K1_INLINE static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a) {
|
|
378
|
+
SECP256K1_FE_VERIFY(a);
|
|
379
|
+
VERIFY_CHECK(a->normalized);
|
|
380
|
+
|
|
381
|
+
secp256k1_fe_impl_to_storage(r, a);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
static void secp256k1_fe_impl_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a);
|
|
385
|
+
SECP256K1_INLINE static void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a) {
|
|
386
|
+
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 secp256k1_fe_impl_inv(secp256k1_fe *r, const secp256k1_fe *x);
|
|
394
|
+
SECP256K1_INLINE static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *x) {
|
|
395
|
+
int input_is_zero = secp256k1_fe_normalizes_to_zero(x);
|
|
396
|
+
SECP256K1_FE_VERIFY(x);
|
|
397
|
+
|
|
398
|
+
secp256k1_fe_impl_inv(r, x);
|
|
399
|
+
r->magnitude = x->magnitude > 0;
|
|
400
|
+
r->normalized = 1;
|
|
401
|
+
|
|
402
|
+
VERIFY_CHECK(secp256k1_fe_normalizes_to_zero(r) == input_is_zero);
|
|
403
|
+
SECP256K1_FE_VERIFY(r);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
static void secp256k1_fe_impl_inv_var(secp256k1_fe *r, const secp256k1_fe *x);
|
|
407
|
+
SECP256K1_INLINE static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *x) {
|
|
408
|
+
int input_is_zero = secp256k1_fe_normalizes_to_zero(x);
|
|
409
|
+
SECP256K1_FE_VERIFY(x);
|
|
410
|
+
|
|
411
|
+
secp256k1_fe_impl_inv_var(r, x);
|
|
412
|
+
r->magnitude = x->magnitude > 0;
|
|
413
|
+
r->normalized = 1;
|
|
414
|
+
|
|
415
|
+
VERIFY_CHECK(secp256k1_fe_normalizes_to_zero(r) == input_is_zero);
|
|
416
|
+
SECP256K1_FE_VERIFY(r);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
static int secp256k1_fe_impl_is_square_var(const secp256k1_fe *x);
|
|
420
|
+
SECP256K1_INLINE static int secp256k1_fe_is_square_var(const secp256k1_fe *x) {
|
|
421
|
+
int ret;
|
|
422
|
+
secp256k1_fe tmp = *x, sqrt;
|
|
423
|
+
SECP256K1_FE_VERIFY(x);
|
|
424
|
+
|
|
425
|
+
ret = secp256k1_fe_impl_is_square_var(x);
|
|
426
|
+
secp256k1_fe_normalize_weak(&tmp);
|
|
427
|
+
VERIFY_CHECK(ret == secp256k1_fe_sqrt(&sqrt, &tmp));
|
|
428
|
+
return ret;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
static void secp256k1_fe_impl_get_bounds(secp256k1_fe* r, int m);
|
|
432
|
+
SECP256K1_INLINE static void secp256k1_fe_get_bounds(secp256k1_fe* r, int m) {
|
|
433
|
+
VERIFY_CHECK(m >= 0);
|
|
434
|
+
VERIFY_CHECK(m <= 32);
|
|
435
|
+
|
|
436
|
+
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 secp256k1_fe_impl_half(secp256k1_fe *r);
|
|
444
|
+
SECP256K1_INLINE static void secp256k1_fe_half(secp256k1_fe *r) {
|
|
445
|
+
SECP256K1_FE_VERIFY(r);
|
|
446
|
+
SECP256K1_FE_VERIFY_MAGNITUDE(r, 31);
|
|
447
|
+
|
|
448
|
+
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,212 @@
|
|
|
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
|
+
secp256k1_fe x;
|
|
18
|
+
secp256k1_fe y;
|
|
19
|
+
int infinity; /* whether this represents the point at infinity */
|
|
20
|
+
} 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
|
+
secp256k1_fe x; /* actual X: x/z^2 */
|
|
30
|
+
secp256k1_fe y; /* actual Y: y/z^3 */
|
|
31
|
+
secp256k1_fe z;
|
|
32
|
+
int infinity; /* whether this represents the point at infinity */
|
|
33
|
+
} 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
|
+
secp256k1_fe_storage x;
|
|
40
|
+
secp256k1_fe_storage y;
|
|
41
|
+
} 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 secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const 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 secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd);
|
|
61
|
+
|
|
62
|
+
/** Determine whether x is a valid X coordinate on the curve. */
|
|
63
|
+
static int secp256k1_ge_x_on_curve_var(const secp256k1_fe *x);
|
|
64
|
+
|
|
65
|
+
/** Determine whether fraction xn/xd is a valid X coordinate on the curve (xd != 0). */
|
|
66
|
+
static int secp256k1_ge_x_frac_on_curve_var(const secp256k1_fe *xn, const secp256k1_fe *xd);
|
|
67
|
+
|
|
68
|
+
/** Check whether a group element is the point at infinity. */
|
|
69
|
+
static int secp256k1_ge_is_infinity(const secp256k1_ge *a);
|
|
70
|
+
|
|
71
|
+
/** Check whether a group element is valid (i.e., on the curve). */
|
|
72
|
+
static int secp256k1_ge_is_valid_var(const secp256k1_ge *a);
|
|
73
|
+
|
|
74
|
+
/** Set r equal to the inverse of a (i.e., mirrored around the X axis) */
|
|
75
|
+
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a);
|
|
76
|
+
|
|
77
|
+
/** Set a group element equal to another which is given in jacobian coordinates. Constant time. */
|
|
78
|
+
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a);
|
|
79
|
+
|
|
80
|
+
/** Set a group element equal to another which is given in jacobian coordinates. */
|
|
81
|
+
static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a);
|
|
82
|
+
|
|
83
|
+
/** Set a batch of group elements equal to the inputs given in jacobian coordinates */
|
|
84
|
+
static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len);
|
|
85
|
+
|
|
86
|
+
/** Bring a batch of inputs to the same global z "denominator", based on ratios between
|
|
87
|
+
* (omitted) z coordinates of adjacent elements.
|
|
88
|
+
*
|
|
89
|
+
* Although the elements a[i] are _ge rather than _gej, they actually represent elements
|
|
90
|
+
* in Jacobian coordinates with their z coordinates omitted.
|
|
91
|
+
*
|
|
92
|
+
* Using the notation z(b) to represent the omitted z coordinate of b, the array zr of
|
|
93
|
+
* z coordinate ratios must satisfy zr[i] == z(a[i]) / z(a[i-1]) for 0 < 'i' < len.
|
|
94
|
+
* The zr[0] value is unused.
|
|
95
|
+
*
|
|
96
|
+
* This function adjusts the coordinates of 'a' in place so that for all 'i', z(a[i]) == z(a[len-1]).
|
|
97
|
+
* In other words, the initial value of z(a[len-1]) becomes the global z "denominator". Only the
|
|
98
|
+
* a[i].x and a[i].y coordinates are explicitly modified; the adjustment of the omitted z coordinate is
|
|
99
|
+
* implicit.
|
|
100
|
+
*
|
|
101
|
+
* The coordinates of the final element a[len-1] are not changed.
|
|
102
|
+
*/
|
|
103
|
+
static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr);
|
|
104
|
+
|
|
105
|
+
/** Check two group elements (affine) for equality in variable time. */
|
|
106
|
+
static int secp256k1_ge_eq_var(const secp256k1_ge *a, const secp256k1_ge *b);
|
|
107
|
+
|
|
108
|
+
/** Set a group element (affine) equal to the point at infinity. */
|
|
109
|
+
static void secp256k1_ge_set_infinity(secp256k1_ge *r);
|
|
110
|
+
|
|
111
|
+
/** Set a group element (jacobian) equal to the point at infinity. */
|
|
112
|
+
static void secp256k1_gej_set_infinity(secp256k1_gej *r);
|
|
113
|
+
|
|
114
|
+
/** Set a group element (jacobian) equal to another which is given in affine coordinates. */
|
|
115
|
+
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a);
|
|
116
|
+
|
|
117
|
+
/** Check two group elements (jacobian) for equality in variable time. */
|
|
118
|
+
static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b);
|
|
119
|
+
|
|
120
|
+
/** Check two group elements (jacobian and affine) for equality in variable time. */
|
|
121
|
+
static int secp256k1_gej_eq_ge_var(const secp256k1_gej *a, const secp256k1_ge *b);
|
|
122
|
+
|
|
123
|
+
/** Compare the X coordinate of a group element (jacobian).
|
|
124
|
+
* The magnitude of the group element's X coordinate must not exceed 31. */
|
|
125
|
+
static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a);
|
|
126
|
+
|
|
127
|
+
/** Set r equal to the inverse of a (i.e., mirrored around the X axis) */
|
|
128
|
+
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a);
|
|
129
|
+
|
|
130
|
+
/** Check whether a group element is the point at infinity. */
|
|
131
|
+
static int secp256k1_gej_is_infinity(const secp256k1_gej *a);
|
|
132
|
+
|
|
133
|
+
/** Set r equal to the double of a. Constant time. */
|
|
134
|
+
static void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a);
|
|
135
|
+
|
|
136
|
+
/** 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). */
|
|
137
|
+
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr);
|
|
138
|
+
|
|
139
|
+
/** 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). */
|
|
140
|
+
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr);
|
|
141
|
+
|
|
142
|
+
/** Set r equal to the sum of a and b (with b given in affine coordinates, and not infinity). */
|
|
143
|
+
static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b);
|
|
144
|
+
|
|
145
|
+
/** Set r equal to the sum of a and b (with b given in affine coordinates). This is more efficient
|
|
146
|
+
than secp256k1_gej_add_var. It is identical to secp256k1_gej_add_ge but without constant-time
|
|
147
|
+
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). */
|
|
148
|
+
static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr);
|
|
149
|
+
|
|
150
|
+
/** Set r equal to the sum of a and b (with the inverse of b's Z coordinate passed as bzinv). */
|
|
151
|
+
static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv);
|
|
152
|
+
|
|
153
|
+
/** Set r to be equal to lambda times a, where lambda is chosen in a way such that this is very fast. */
|
|
154
|
+
static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a);
|
|
155
|
+
|
|
156
|
+
/** Clear a secp256k1_gej to prevent leaking sensitive information. */
|
|
157
|
+
static void secp256k1_gej_clear(secp256k1_gej *r);
|
|
158
|
+
|
|
159
|
+
/** Clear a secp256k1_ge to prevent leaking sensitive information. */
|
|
160
|
+
static void secp256k1_ge_clear(secp256k1_ge *r);
|
|
161
|
+
|
|
162
|
+
/** Convert a group element to the storage type. */
|
|
163
|
+
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a);
|
|
164
|
+
|
|
165
|
+
/** Convert a group element back from the storage type. */
|
|
166
|
+
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a);
|
|
167
|
+
|
|
168
|
+
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/
|
|
169
|
+
static void secp256k1_gej_cmov(secp256k1_gej *r, const secp256k1_gej *a, int flag);
|
|
170
|
+
|
|
171
|
+
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/
|
|
172
|
+
static void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag);
|
|
173
|
+
|
|
174
|
+
/** Rescale a jacobian point by b which must be non-zero. Constant-time. */
|
|
175
|
+
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b);
|
|
176
|
+
|
|
177
|
+
/** Convert a group element that is not infinity to a 64-byte array. The output
|
|
178
|
+
* array is platform-dependent. */
|
|
179
|
+
static void secp256k1_ge_to_bytes(unsigned char *buf, const secp256k1_ge *a);
|
|
180
|
+
|
|
181
|
+
/** Convert a 64-byte array into group element. This function assumes that the
|
|
182
|
+
* provided buffer correctly encodes a group element. */
|
|
183
|
+
static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf);
|
|
184
|
+
|
|
185
|
+
/** Convert a group element (that is allowed to be infinity) to a 64-byte
|
|
186
|
+
* array. The output array is platform-dependent. */
|
|
187
|
+
static void secp256k1_ge_to_bytes_ext(unsigned char *data, const secp256k1_ge *ge);
|
|
188
|
+
|
|
189
|
+
/** Convert a 64-byte array into a group element. This function assumes that the
|
|
190
|
+
* provided buffer is the output of secp256k1_ge_to_bytes_ext. */
|
|
191
|
+
static void secp256k1_ge_from_bytes_ext(secp256k1_ge *ge, const unsigned char *data);
|
|
192
|
+
|
|
193
|
+
/** Determine if a point (which is assumed to be on the curve) is in the correct (sub)group of the curve.
|
|
194
|
+
*
|
|
195
|
+
* In normal mode, the used group is secp256k1, which has cofactor=1 meaning that every point on the curve is in the
|
|
196
|
+
* group, and this function returns always true.
|
|
197
|
+
*
|
|
198
|
+
* When compiling in exhaustive test mode, a slightly different curve equation is used, leading to a group with a
|
|
199
|
+
* (very) small subgroup, and that subgroup is what is used for all cryptographic operations. In that mode, this
|
|
200
|
+
* function checks whether a point that is on the curve is in fact also in that subgroup.
|
|
201
|
+
*/
|
|
202
|
+
static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge* ge);
|
|
203
|
+
|
|
204
|
+
/** Check invariants on an affine group element (no-op unless VERIFY is enabled). */
|
|
205
|
+
static void secp256k1_ge_verify(const secp256k1_ge *a);
|
|
206
|
+
#define SECP256K1_GE_VERIFY(a) secp256k1_ge_verify(a)
|
|
207
|
+
|
|
208
|
+
/** Check invariants on a Jacobian group element (no-op unless VERIFY is enabled). */
|
|
209
|
+
static void secp256k1_gej_verify(const secp256k1_gej *a);
|
|
210
|
+
#define SECP256K1_GEJ_VERIFY(a) secp256k1_gej_verify(a)
|
|
211
|
+
|
|
212
|
+
#endif /* SECP256K1_GROUP_H */
|