@authon/react-native-wallet 0.0.0-stage → 0.4.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.
Files changed (183) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +251 -2
  3. package/android/build.gradle +40 -0
  4. package/android/consumer-rules.pro +10 -0
  5. package/android/src/main/AndroidManifest.xml +8 -0
  6. package/android/src/main/java/dev/authon/reactnative/wallet/AuthonWalletModule.kt +235 -0
  7. package/android/src/vendor/GENERATED.md +4 -0
  8. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/AndroidCapabilityProbe.kt +86 -0
  9. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/AuthonWallet.kt +218 -0
  10. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Bitcoin.kt +296 -0
  11. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Bytes.kt +74 -0
  12. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Chains.kt +157 -0
  13. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/CredentialManagerPasskeys.kt +192 -0
  14. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Der.kt +17 -0
  15. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/DeviceStore.kt +170 -0
  16. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/DialogWalletApprover.kt +141 -0
  17. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Eip712.kt +176 -0
  18. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Encodings.kt +133 -0
  19. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/EvmAccount.kt +45 -0
  20. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/EvmTransaction.kt +180 -0
  21. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/GfTables.kt +44 -0
  22. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/HdWallet.kt +130 -0
  23. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Http.kt +86 -0
  24. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/JsValues.kt +83 -0
  25. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/KeystoreDeviceStore.kt +236 -0
  26. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Prf.kt +111 -0
  27. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Proof.kt +134 -0
  28. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Shamir.kt +106 -0
  29. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/SignSummary.kt +335 -0
  30. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Solana.kt +195 -0
  31. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Tron.kt +184 -0
  32. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletApi.kt +361 -0
  33. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletCapabilities.kt +91 -0
  34. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletError.kt +74 -0
  35. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletOps.kt +551 -0
  36. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletTypes.kt +78 -0
  37. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Xrp.kt +193 -0
  38. package/build/authonContext.d.ts +19 -0
  39. package/build/authonContext.d.ts.map +1 -0
  40. package/build/authonContext.js +20 -0
  41. package/build/authonContext.js.map +1 -0
  42. package/build/client.d.ts +92 -0
  43. package/build/client.d.ts.map +1 -0
  44. package/build/client.js +431 -0
  45. package/build/client.js.map +1 -0
  46. package/build/errors.d.ts +25 -0
  47. package/build/errors.d.ts.map +1 -0
  48. package/build/errors.js +15 -0
  49. package/build/errors.js.map +1 -0
  50. package/build/index.d.ts +7 -0
  51. package/build/index.d.ts.map +1 -0
  52. package/build/index.js +4 -0
  53. package/build/index.js.map +1 -0
  54. package/build/native.d.ts +43 -0
  55. package/build/native.d.ts.map +1 -0
  56. package/build/native.js +12 -0
  57. package/build/native.js.map +1 -0
  58. package/build/types.d.ts +167 -0
  59. package/build/types.d.ts.map +1 -0
  60. package/build/types.js +3 -0
  61. package/build/types.js.map +1 -0
  62. package/build/useAuthonWallet.d.ts +25 -0
  63. package/build/useAuthonWallet.d.ts.map +1 -0
  64. package/build/useAuthonWallet.js +77 -0
  65. package/build/useAuthonWallet.js.map +1 -0
  66. package/expo-module.config.json +9 -0
  67. package/ios/AuthonReactNativeWallet.podspec +41 -0
  68. package/ios/AuthonWalletModule.swift +233 -0
  69. package/ios/vendor/AuthonWallet/AuthonWallet.swift +232 -0
  70. package/ios/vendor/AuthonWallet/Capabilities.swift +149 -0
  71. package/ios/vendor/AuthonWallet/Chains/Bitcoin.swift +296 -0
  72. package/ios/vendor/AuthonWallet/Chains/Solana.swift +207 -0
  73. package/ios/vendor/AuthonWallet/Chains/Tron.swift +181 -0
  74. package/ios/vendor/AuthonWallet/Chains/XRP.swift +191 -0
  75. package/ios/vendor/AuthonWallet/Crypto/BIP39Wordlist.swift +262 -0
  76. package/ios/vendor/AuthonWallet/Crypto/Chains.swift +278 -0
  77. package/ios/vendor/AuthonWallet/Crypto/HDKey.swift +108 -0
  78. package/ios/vendor/AuthonWallet/Crypto/Secp256k1.swift +128 -0
  79. package/ios/vendor/AuthonWallet/Crypto/Shamir.swift +123 -0
  80. package/ios/vendor/AuthonWallet/EVM/Address.swift +57 -0
  81. package/ios/vendor/AuthonWallet/EVM/EvmAccount.swift +48 -0
  82. package/ios/vendor/AuthonWallet/EVM/Transaction.swift +165 -0
  83. package/ios/vendor/AuthonWallet/EVM/TypedData.swift +193 -0
  84. package/ios/vendor/AuthonWallet/Support/BigInteger.swift +220 -0
  85. package/ios/vendor/AuthonWallet/Support/Bytes.swift +89 -0
  86. package/ios/vendor/AuthonWallet/Support/JSONValue.swift +341 -0
  87. package/ios/vendor/AuthonWallet/UI/WalletApprover.swift +250 -0
  88. package/ios/vendor/AuthonWallet/Wallet/Backup.swift +69 -0
  89. package/ios/vendor/AuthonWallet/Wallet/DeviceStore.swift +351 -0
  90. package/ios/vendor/AuthonWallet/Wallet/Passkeys.swift +190 -0
  91. package/ios/vendor/AuthonWallet/Wallet/Proof.swift +143 -0
  92. package/ios/vendor/AuthonWallet/Wallet/Shares.swift +116 -0
  93. package/ios/vendor/AuthonWallet/Wallet/Summary.swift +342 -0
  94. package/ios/vendor/AuthonWallet/Wallet/WalletAPI.swift +361 -0
  95. package/ios/vendor/AuthonWallet/Wallet/WalletOps.swift +654 -0
  96. package/ios/vendor/AuthonWallet/WalletError.swift +84 -0
  97. package/ios/vendor/CKeccak/LICENSE +22 -0
  98. package/ios/vendor/CKeccak/README.md +30 -0
  99. package/ios/vendor/CKeccak/byte_order.h +59 -0
  100. package/ios/vendor/CKeccak/include/ckeccak.h +13 -0
  101. package/ios/vendor/CKeccak/memzero.c +75 -0
  102. package/ios/vendor/CKeccak/memzero.h +8 -0
  103. package/ios/vendor/CKeccak/options.h +99 -0
  104. package/ios/vendor/CKeccak/ripemd160.c +343 -0
  105. package/ios/vendor/CKeccak/ripemd160.h +22 -0
  106. package/ios/vendor/CKeccak/sha3.c +413 -0
  107. package/ios/vendor/CKeccak/sha3.h +89 -0
  108. package/ios/vendor/CMonocypher/LICENCE.md +173 -0
  109. package/ios/vendor/CMonocypher/README.md +27 -0
  110. package/ios/vendor/CMonocypher/include/monocypher-ed25519.h +140 -0
  111. package/ios/vendor/CMonocypher/include/monocypher.h +321 -0
  112. package/ios/vendor/CMonocypher/monocypher-ed25519.c +500 -0
  113. package/ios/vendor/CMonocypher/monocypher.c +2956 -0
  114. package/ios/vendor/CSecp256k1/COPYING +19 -0
  115. package/ios/vendor/CSecp256k1/README.md +35 -0
  116. package/ios/vendor/CSecp256k1/build/precomputed_ecmult.c +3 -0
  117. package/ios/vendor/CSecp256k1/build/precomputed_ecmult_gen.c +3 -0
  118. package/ios/vendor/CSecp256k1/build/secp256k1.c +3 -0
  119. package/ios/vendor/CSecp256k1/include/secp256k1.h +899 -0
  120. package/ios/vendor/CSecp256k1/include/secp256k1_preallocated.h +134 -0
  121. package/ios/vendor/CSecp256k1/include/secp256k1_recovery.h +113 -0
  122. package/ios/vendor/CSecp256k1/src/assumptions.h +87 -0
  123. package/ios/vendor/CSecp256k1/src/checkmem.h +102 -0
  124. package/ios/vendor/CSecp256k1/src/ecdsa.h +21 -0
  125. package/ios/vendor/CSecp256k1/src/ecdsa_impl.h +304 -0
  126. package/ios/vendor/CSecp256k1/src/eckey.h +25 -0
  127. package/ios/vendor/CSecp256k1/src/eckey_impl.h +92 -0
  128. package/ios/vendor/CSecp256k1/src/ecmult.h +61 -0
  129. package/ios/vendor/CSecp256k1/src/ecmult_const.h +38 -0
  130. package/ios/vendor/CSecp256k1/src/ecmult_const_impl.h +399 -0
  131. package/ios/vendor/CSecp256k1/src/ecmult_gen.h +143 -0
  132. package/ios/vendor/CSecp256k1/src/ecmult_gen_impl.h +341 -0
  133. package/ios/vendor/CSecp256k1/src/ecmult_impl.h +853 -0
  134. package/ios/vendor/CSecp256k1/src/field.h +350 -0
  135. package/ios/vendor/CSecp256k1/src/field_10x26.h +57 -0
  136. package/ios/vendor/CSecp256k1/src/field_10x26_impl.h +1232 -0
  137. package/ios/vendor/CSecp256k1/src/field_5x52.h +62 -0
  138. package/ios/vendor/CSecp256k1/src/field_5x52_impl.h +522 -0
  139. package/ios/vendor/CSecp256k1/src/field_5x52_int128_impl.h +274 -0
  140. package/ios/vendor/CSecp256k1/src/field_impl.h +457 -0
  141. package/ios/vendor/CSecp256k1/src/group.h +212 -0
  142. package/ios/vendor/CSecp256k1/src/group_impl.h +974 -0
  143. package/ios/vendor/CSecp256k1/src/hash.h +44 -0
  144. package/ios/vendor/CSecp256k1/src/hash_impl.h +299 -0
  145. package/ios/vendor/CSecp256k1/src/hsort.h +33 -0
  146. package/ios/vendor/CSecp256k1/src/hsort_impl.h +125 -0
  147. package/ios/vendor/CSecp256k1/src/int128.h +90 -0
  148. package/ios/vendor/CSecp256k1/src/int128_impl.h +18 -0
  149. package/ios/vendor/CSecp256k1/src/int128_native.h +19 -0
  150. package/ios/vendor/CSecp256k1/src/int128_native_impl.h +94 -0
  151. package/ios/vendor/CSecp256k1/src/int128_struct.h +14 -0
  152. package/ios/vendor/CSecp256k1/src/int128_struct_impl.h +205 -0
  153. package/ios/vendor/CSecp256k1/src/modinv32.h +43 -0
  154. package/ios/vendor/CSecp256k1/src/modinv32_impl.h +725 -0
  155. package/ios/vendor/CSecp256k1/src/modinv64.h +47 -0
  156. package/ios/vendor/CSecp256k1/src/modinv64_impl.h +780 -0
  157. package/ios/vendor/CSecp256k1/src/modules/recovery/main_impl.h +159 -0
  158. package/ios/vendor/CSecp256k1/src/precomputed_ecmult.c +16456 -0
  159. package/ios/vendor/CSecp256k1/src/precomputed_ecmult.h +38 -0
  160. package/ios/vendor/CSecp256k1/src/precomputed_ecmult_gen.c +1779 -0
  161. package/ios/vendor/CSecp256k1/src/precomputed_ecmult_gen.h +26 -0
  162. package/ios/vendor/CSecp256k1/src/scalar.h +105 -0
  163. package/ios/vendor/CSecp256k1/src/scalar_4x64.h +19 -0
  164. package/ios/vendor/CSecp256k1/src/scalar_4x64_impl.h +1000 -0
  165. package/ios/vendor/CSecp256k1/src/scalar_8x32.h +19 -0
  166. package/ios/vendor/CSecp256k1/src/scalar_8x32_impl.h +816 -0
  167. package/ios/vendor/CSecp256k1/src/scalar_impl.h +321 -0
  168. package/ios/vendor/CSecp256k1/src/scalar_low.h +24 -0
  169. package/ios/vendor/CSecp256k1/src/scalar_low_impl.h +206 -0
  170. package/ios/vendor/CSecp256k1/src/scratch.h +44 -0
  171. package/ios/vendor/CSecp256k1/src/scratch_impl.h +99 -0
  172. package/ios/vendor/CSecp256k1/src/secp256k1.c +831 -0
  173. package/ios/vendor/CSecp256k1/src/selftest.h +32 -0
  174. package/ios/vendor/CSecp256k1/src/util.h +451 -0
  175. package/ios/vendor/GENERATED.md +4 -0
  176. package/package.json +66 -3
  177. package/src/authonContext.ts +30 -0
  178. package/src/client.ts +459 -0
  179. package/src/errors.ts +58 -0
  180. package/src/index.ts +26 -0
  181. package/src/native.ts +52 -0
  182. package/src/types.ts +171 -0
  183. package/src/useAuthonWallet.ts +103 -0
@@ -0,0 +1,350 @@
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 secp256k1_fe
13
+ * objects, which represent field elements (integers modulo 2^256 - 2^32 - 977).
14
+ *
15
+ * The actual definition of the 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 secp256k1_fe objects have implicit properties that determine what
19
+ * operations are permitted on it. These are purely a function of what
20
+ * 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 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 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 secp256k1_fe secp256k1_fe_one = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
69
+ static const secp256k1_fe 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 secp256k1_fe_normalize secp256k1_fe_impl_normalize
79
+ # define secp256k1_fe_normalize_weak secp256k1_fe_impl_normalize_weak
80
+ # define secp256k1_fe_normalize_var secp256k1_fe_impl_normalize_var
81
+ # define secp256k1_fe_normalizes_to_zero secp256k1_fe_impl_normalizes_to_zero
82
+ # define secp256k1_fe_normalizes_to_zero_var secp256k1_fe_impl_normalizes_to_zero_var
83
+ # define secp256k1_fe_set_int secp256k1_fe_impl_set_int
84
+ # define secp256k1_fe_is_zero secp256k1_fe_impl_is_zero
85
+ # define secp256k1_fe_is_odd secp256k1_fe_impl_is_odd
86
+ # define secp256k1_fe_cmp_var secp256k1_fe_impl_cmp_var
87
+ # define secp256k1_fe_set_b32_mod secp256k1_fe_impl_set_b32_mod
88
+ # define secp256k1_fe_set_b32_limit secp256k1_fe_impl_set_b32_limit
89
+ # define secp256k1_fe_get_b32 secp256k1_fe_impl_get_b32
90
+ # define secp256k1_fe_negate_unchecked secp256k1_fe_impl_negate_unchecked
91
+ # define secp256k1_fe_mul_int_unchecked secp256k1_fe_impl_mul_int_unchecked
92
+ # define secp256k1_fe_add secp256k1_fe_impl_add
93
+ # define secp256k1_fe_mul secp256k1_fe_impl_mul
94
+ # define secp256k1_fe_sqr secp256k1_fe_impl_sqr
95
+ # define secp256k1_fe_cmov secp256k1_fe_impl_cmov
96
+ # define secp256k1_fe_to_storage secp256k1_fe_impl_to_storage
97
+ # define secp256k1_fe_from_storage secp256k1_fe_impl_from_storage
98
+ # define secp256k1_fe_inv secp256k1_fe_impl_inv
99
+ # define secp256k1_fe_inv_var secp256k1_fe_impl_inv_var
100
+ # define secp256k1_fe_get_bounds secp256k1_fe_impl_get_bounds
101
+ # define secp256k1_fe_half secp256k1_fe_impl_half
102
+ # define secp256k1_fe_add_int secp256k1_fe_impl_add_int
103
+ # define secp256k1_fe_is_square_var 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 secp256k1_fe_normalize(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 secp256k1_fe_normalize_weak(secp256k1_fe *r);
119
+
120
+ /** Normalize a field element, without constant-time guarantee.
121
+ *
122
+ * Identical in behavior to secp256k1_fe_normalize, but not constant time in r.
123
+ */
124
+ static void secp256k1_fe_normalize_var(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 secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r);
132
+
133
+ /** Determine whether r represents field element 0, without constant-time guarantee.
134
+ *
135
+ * Identical in behavior to secp256k1_normalizes_to_zero, but not constant time in r.
136
+ */
137
+ static int secp256k1_fe_normalizes_to_zero_var(const 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 secp256k1_fe_set_int(secp256k1_fe *r, int a);
145
+
146
+ /** Clear a field element to prevent leaking sensitive information. */
147
+ static void secp256k1_fe_clear(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 secp256k1_normalizes_to_zero{,_var}, but requires
155
+ * normalized input (and is much faster).
156
+ */
157
+ static int secp256k1_fe_is_zero(const 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 secp256k1_fe_is_odd(const 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 31, respectively.
170
+ * Returns a = b (mod p).
171
+ */
172
+ static int secp256k1_fe_equal(const secp256k1_fe *a, const 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 secp256k1_fe_cmp_var(const secp256k1_fe *a, const 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 secp256k1_fe_set_b32_mod(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 secp256k1_fe_set_b32_limit(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 secp256k1_fe_get_b32(unsigned char *r, const 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 secp256k1_fe_negate(r, a, m) ASSERT_INT_CONST_AND_DO(m, secp256k1_fe_negate_unchecked(r, a, m))
212
+
213
+ /** Like 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 secp256k1_fe_negate_unchecked(secp256k1_fe *r, const 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 secp256k1_fe_add_int(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 secp256k1_fe_mul_int(r, a) ASSERT_INT_CONST_AND_DO(a, secp256k1_fe_mul_int_unchecked(r, a))
234
+
235
+ /** Like 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 secp256k1_fe_mul_int_unchecked(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 secp256k1_fe_add(secp256k1_fe *r, const 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 secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const 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 secp256k1_fe_sqr(secp256k1_fe *r, const 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 secp256k1_fe_sqrt(secp256k1_fe * SECP256K1_RESTRICT r, const 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 secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *a);
288
+
289
+ /** Compute the modular inverse of a field element, without constant-time guarantee.
290
+ *
291
+ * Behaves identically to secp256k1_fe_inv, but is not constant-time in a.
292
+ */
293
+ static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *a);
294
+
295
+ /** Convert a field element to secp256k1_fe_storage.
296
+ *
297
+ * On input, a must be a valid normalized field element.
298
+ * Performs {r = a}.
299
+ */
300
+ static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a);
301
+
302
+ /** Convert a field element back from 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 secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a);
309
+
310
+ /** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/
311
+ static void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag);
312
+
313
+ /** Conditionally move a field element in constant time.
314
+ *
315
+ * On input, both r and a must be valid field elements. Flag must be 0 or 1.
316
+ * Performs {r = flag ? a : r}.
317
+ *
318
+ * On output, r's magnitude will be the maximum of both input magnitudes.
319
+ * It will be normalized if and only if both inputs were normalized.
320
+ */
321
+ static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag);
322
+
323
+ /** Halve the value of a field element modulo the field prime in constant-time.
324
+ *
325
+ * On input, r must be a valid field element.
326
+ * On output, r will be normalized and have magnitude floor(m/2) + 1 where m is
327
+ * the magnitude of r on input.
328
+ */
329
+ static void secp256k1_fe_half(secp256k1_fe *r);
330
+
331
+ /** Sets r to a field element with magnitude m, normalized if (and only if) m==0.
332
+ * The value is chosen so that it is likely to trigger edge cases related to
333
+ * internal overflows. */
334
+ static void secp256k1_fe_get_bounds(secp256k1_fe *r, int m);
335
+
336
+ /** Determine whether a is a square (modulo p).
337
+ *
338
+ * On input, a must be a valid field element.
339
+ */
340
+ static int secp256k1_fe_is_square_var(const secp256k1_fe *a);
341
+
342
+ /** Check invariants on a field element (no-op unless VERIFY is enabled). */
343
+ static void secp256k1_fe_verify(const secp256k1_fe *a);
344
+ #define SECP256K1_FE_VERIFY(a) secp256k1_fe_verify(a)
345
+
346
+ /** Check that magnitude of a is at most m (no-op unless VERIFY is enabled). */
347
+ static void secp256k1_fe_verify_magnitude(const secp256k1_fe *a, int m);
348
+ #define SECP256K1_FE_VERIFY_MAGNITUDE(a, m) secp256k1_fe_verify_magnitude(a, m)
349
+
350
+ #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
+ } 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
+ } 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 */