@umbra-privacy/sdk 1.0.0 → 2.0.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.
Files changed (181) hide show
  1. package/README.md +104 -25
  2. package/dist/{addresses-Brzgurv_.d.ts → addresses-B7HybtbJ.d.ts} +2 -1
  3. package/dist/{addresses-D_0YAS6B.d.cts → addresses-CTVY1oi7.d.cts} +2 -1
  4. package/dist/arcium-BXXlryfe.d.cts +20 -0
  5. package/dist/arcium-BXXlryfe.d.ts +20 -0
  6. package/dist/chunk-3LS5P32X.cjs +10892 -0
  7. package/dist/chunk-3LS5P32X.cjs.map +1 -0
  8. package/dist/chunk-4RHXVBNI.js +203 -0
  9. package/dist/chunk-4RHXVBNI.js.map +1 -0
  10. package/dist/chunk-4TZVXB5G.js +324 -0
  11. package/dist/chunk-4TZVXB5G.js.map +1 -0
  12. package/dist/chunk-5GUSMQ74.cjs +549 -0
  13. package/dist/chunk-5GUSMQ74.cjs.map +1 -0
  14. package/dist/chunk-5KPQXPQM.js +36 -0
  15. package/dist/chunk-5KPQXPQM.js.map +1 -0
  16. package/dist/chunk-AXD7LXYY.cjs +405 -0
  17. package/dist/chunk-AXD7LXYY.cjs.map +1 -0
  18. package/dist/{chunk-HOEXDXRC.cjs → chunk-BL6WXLPV.cjs} +32 -360
  19. package/dist/chunk-BL6WXLPV.cjs.map +1 -0
  20. package/dist/chunk-CFFLOE7D.cjs +598 -0
  21. package/dist/chunk-CFFLOE7D.cjs.map +1 -0
  22. package/dist/{chunk-BM7N6N7E.js → chunk-CFTW5WNG.js} +3 -325
  23. package/dist/chunk-CFTW5WNG.js.map +1 -0
  24. package/dist/chunk-DD2WCK4C.js +327 -0
  25. package/dist/chunk-DD2WCK4C.js.map +1 -0
  26. package/dist/chunk-DMPMQ74B.cjs +246 -0
  27. package/dist/chunk-DMPMQ74B.cjs.map +1 -0
  28. package/dist/{chunk-2Q75CQQJ.js → chunk-EEKF4553.js} +2 -2
  29. package/dist/chunk-EEKF4553.js.map +1 -0
  30. package/dist/chunk-ENVYYEM4.cjs +113 -0
  31. package/dist/chunk-ENVYYEM4.cjs.map +1 -0
  32. package/dist/chunk-FQX6ZYGJ.js +500 -0
  33. package/dist/chunk-FQX6ZYGJ.js.map +1 -0
  34. package/dist/chunk-FSK2ICMB.cjs +39 -0
  35. package/dist/chunk-FSK2ICMB.cjs.map +1 -0
  36. package/dist/chunk-FZYWLQAF.cjs +355 -0
  37. package/dist/chunk-FZYWLQAF.cjs.map +1 -0
  38. package/dist/chunk-GP26R377.js +436 -0
  39. package/dist/chunk-GP26R377.js.map +1 -0
  40. package/dist/chunk-HA5FLM63.js +393 -0
  41. package/dist/chunk-HA5FLM63.js.map +1 -0
  42. package/dist/chunk-INJ73LXQ.js +1107 -0
  43. package/dist/chunk-INJ73LXQ.js.map +1 -0
  44. package/dist/chunk-KMRROOME.js +10750 -0
  45. package/dist/chunk-KMRROOME.js.map +1 -0
  46. package/dist/{chunk-MDFSBU5W.cjs → chunk-LTCKPTZC.cjs} +2 -351
  47. package/dist/chunk-LTCKPTZC.cjs.map +1 -0
  48. package/dist/chunk-MKNCBUFA.js +564 -0
  49. package/dist/chunk-MKNCBUFA.js.map +1 -0
  50. package/dist/chunk-NKVMSABR.cjs +207 -0
  51. package/dist/chunk-NKVMSABR.cjs.map +1 -0
  52. package/dist/chunk-OFDWNWCL.js +70 -0
  53. package/dist/chunk-OFDWNWCL.js.map +1 -0
  54. package/dist/chunk-QJAUUYZU.cjs +331 -0
  55. package/dist/chunk-QJAUUYZU.cjs.map +1 -0
  56. package/dist/chunk-TLR7A64G.js +103 -0
  57. package/dist/chunk-TLR7A64G.js.map +1 -0
  58. package/dist/{chunk-MVKTV3FT.cjs → chunk-TQQZGNOI.cjs} +2 -2
  59. package/dist/chunk-TQQZGNOI.cjs.map +1 -0
  60. package/dist/chunk-UOFYS6M3.js +219 -0
  61. package/dist/chunk-UOFYS6M3.js.map +1 -0
  62. package/dist/chunk-UXMQI6B7.js +2406 -0
  63. package/dist/chunk-UXMQI6B7.js.map +1 -0
  64. package/dist/chunk-WN75ORDT.js +571 -0
  65. package/dist/chunk-WN75ORDT.js.map +1 -0
  66. package/dist/chunk-Y55PYKXH.cjs +595 -0
  67. package/dist/chunk-Y55PYKXH.cjs.map +1 -0
  68. package/dist/chunk-YEZBTYCP.cjs +77 -0
  69. package/dist/chunk-YEZBTYCP.cjs.map +1 -0
  70. package/dist/chunk-ZQOIYCGA.cjs +1126 -0
  71. package/dist/chunk-ZQOIYCGA.cjs.map +1 -0
  72. package/dist/chunk-ZY3TSHMJ.cjs +2665 -0
  73. package/dist/chunk-ZY3TSHMJ.cjs.map +1 -0
  74. package/dist/client-DkVBHMWb.d.cts +2613 -0
  75. package/dist/client-V4AF6Bz9.d.ts +2613 -0
  76. package/dist/common/pda/index.cjs +145 -0
  77. package/dist/common/pda/index.cjs.map +1 -0
  78. package/dist/common/pda/index.d.cts +1250 -0
  79. package/dist/common/pda/index.d.ts +1250 -0
  80. package/dist/common/pda/index.js +8 -0
  81. package/dist/common/pda/index.js.map +1 -0
  82. package/dist/constants/index.cjs +38 -164
  83. package/dist/constants/index.cjs.map +1 -1
  84. package/dist/constants/index.d.cts +8 -425
  85. package/dist/constants/index.d.ts +8 -425
  86. package/dist/constants/index.js +15 -124
  87. package/dist/constants/index.js.map +1 -1
  88. package/dist/crypto/index.cjs +583 -0
  89. package/dist/crypto/index.cjs.map +1 -0
  90. package/dist/crypto/index.d.cts +6731 -0
  91. package/dist/crypto/index.d.ts +6731 -0
  92. package/dist/crypto/index.js +14 -0
  93. package/dist/crypto/index.js.map +1 -0
  94. package/dist/{cryptography-BTGC72u-.d.ts → cryptography-BFSJcvi6.d.ts} +3 -2465
  95. package/dist/{cryptography-BTGC72u-.d.cts → cryptography-D6tPDh-Y.d.cts} +3 -2465
  96. package/dist/errors/index.cjs +64 -54
  97. package/dist/errors/index.d.cts +7 -797
  98. package/dist/errors/index.d.ts +7 -797
  99. package/dist/errors/index.js +3 -1
  100. package/dist/errors-B9EoPeWV.d.cts +593 -0
  101. package/dist/errors-B9EoPeWV.d.ts +593 -0
  102. package/dist/errors-DAIrstEL.d.cts +300 -0
  103. package/dist/errors-DPNMfyh0.d.ts +300 -0
  104. package/dist/index-BG0yjL7C.d.cts +6006 -0
  105. package/dist/index-ByynoyBO.d.ts +6006 -0
  106. package/dist/index.cjs +5133 -16116
  107. package/dist/index.cjs.map +1 -1
  108. package/dist/index.d.cts +1031 -7685
  109. package/dist/index.d.ts +1031 -7685
  110. package/dist/index.js +3228 -14905
  111. package/dist/index.js.map +1 -1
  112. package/dist/interfaces/index.d.cts +14 -6
  113. package/dist/interfaces/index.d.ts +14 -6
  114. package/dist/interfaces-43cReBcS.d.cts +3346 -0
  115. package/dist/interfaces-B8xKNl_6.d.ts +997 -0
  116. package/dist/interfaces-D2NO6kDD.d.cts +997 -0
  117. package/dist/interfaces-z_xYJlgV.d.ts +3346 -0
  118. package/dist/math/index.cjs +115 -0
  119. package/dist/math/index.cjs.map +1 -0
  120. package/dist/math/index.d.cts +1327 -0
  121. package/dist/math/index.d.ts +1327 -0
  122. package/dist/math/index.js +10 -0
  123. package/dist/math/index.js.map +1 -0
  124. package/dist/networks-RMd3abPE.d.ts +44 -0
  125. package/dist/networks-yAoO8peQ.d.cts +44 -0
  126. package/dist/relayer-NRRMSMNB.js +4 -0
  127. package/dist/relayer-NRRMSMNB.js.map +1 -0
  128. package/dist/relayer-RJHEIXJG.cjs +21 -0
  129. package/dist/relayer-RJHEIXJG.cjs.map +1 -0
  130. package/dist/solana/index.cjs +56 -0
  131. package/dist/solana/index.cjs.map +1 -0
  132. package/dist/solana/index.d.cts +105 -0
  133. package/dist/solana/index.d.ts +105 -0
  134. package/dist/solana/index.js +7 -0
  135. package/dist/solana/index.js.map +1 -0
  136. package/dist/{index-CLj_zWSD.d.ts → temporal-BbRaEPoO.d.ts} +1 -1
  137. package/dist/{index-CX6_pIRS.d.cts → temporal-oUj7iCaq.d.cts} +1 -1
  138. package/dist/transaction-forwarder-5mAMTjw6.d.ts +1155 -0
  139. package/dist/transaction-forwarder-C6gMUG7a.d.cts +1155 -0
  140. package/dist/types/index.cjs +232 -231
  141. package/dist/types/index.d.cts +15 -1485
  142. package/dist/types/index.d.ts +15 -1485
  143. package/dist/types/index.js +2 -1
  144. package/dist/types-BohhvPth.d.cts +87 -0
  145. package/dist/types-CW0oTT0j.d.ts +87 -0
  146. package/dist/types-C_V_CaKK.d.cts +2468 -0
  147. package/dist/types-C_V_CaKK.d.ts +2468 -0
  148. package/dist/types-Ca7frykr.d.ts +793 -0
  149. package/dist/types-CuKeoI19.d.cts +1296 -0
  150. package/dist/types-CxfTIpN9.d.ts +1052 -0
  151. package/dist/{types-n-sHFcgr.d.ts → types-D1jDUjfN.d.ts} +2 -2
  152. package/dist/types-DKEDUlH9.d.ts +1296 -0
  153. package/dist/types-EKuIfxTz.d.cts +1052 -0
  154. package/dist/{types-BBuELtY8.d.cts → types-IMGYmlv-.d.cts} +2 -2
  155. package/dist/types-PwNLi_2k.d.cts +793 -0
  156. package/dist/utils/index.cjs +823 -525
  157. package/dist/utils/index.d.cts +1711 -4021
  158. package/dist/utils/index.d.ts +1711 -4021
  159. package/dist/utils/index.js +9 -3
  160. package/dist/{versions-D9PqsEvj.d.cts → versions-BRlR36EA.d.cts} +1 -0
  161. package/dist/{versions-D9PqsEvj.d.ts → versions-BRlR36EA.d.ts} +1 -0
  162. package/package.json +79 -18
  163. package/dist/chunk-2Q75CQQJ.js.map +0 -1
  164. package/dist/chunk-BM7N6N7E.js.map +0 -1
  165. package/dist/chunk-GXKSUB2U.cjs +0 -4416
  166. package/dist/chunk-GXKSUB2U.cjs.map +0 -1
  167. package/dist/chunk-HOEXDXRC.cjs.map +0 -1
  168. package/dist/chunk-MDFSBU5W.cjs.map +0 -1
  169. package/dist/chunk-MQY7HDIA.js +0 -600
  170. package/dist/chunk-MQY7HDIA.js.map +0 -1
  171. package/dist/chunk-MVKTV3FT.cjs.map +0 -1
  172. package/dist/chunk-PG2J6V6Y.js +0 -4094
  173. package/dist/chunk-PG2J6V6Y.js.map +0 -1
  174. package/dist/chunk-VEGLTTYQ.cjs +0 -621
  175. package/dist/chunk-VEGLTTYQ.cjs.map +0 -1
  176. package/dist/chunk-WVHQ46DD.js +0 -758
  177. package/dist/chunk-WVHQ46DD.js.map +0 -1
  178. package/dist/index-B9pDY73x.d.ts +0 -12933
  179. package/dist/index-D33yo0qB.d.cts +0 -12933
  180. package/dist/networks-C-orpSFW.d.ts +0 -65
  181. package/dist/networks-FxYERGD1.d.cts +0 -65
@@ -0,0 +1,997 @@
1
+ import { i as U512BeBytes, S as SubSubBrandedType, f as U256 } from './types-C_V_CaKK.cjs';
2
+
3
+ /**
4
+ * BN254 Field Arithmetic Interfaces
5
+ *
6
+ * This module defines TypeScript function-type interfaces that abstract modular
7
+ * arithmetic operations over the BN254 scalar prime field F_r. These interfaces
8
+ * decouple callers from specific implementations, allowing constant-time,
9
+ * optimized, or platform-specific variants to be substituted without changes to
10
+ * calling code.
11
+ *
12
+ * @remarks
13
+ * ## Overview
14
+ *
15
+ * Modular arithmetic is the mathematical foundation of elliptic curve cryptography
16
+ * and zero-knowledge proof systems. Every field operation — addition, subtraction,
17
+ * multiplication, negation, and inversion — produces a result that is reduced
18
+ * modulo the field prime `p` and lies in `[0, p-1]`.
19
+ *
20
+ * The interfaces defined here are function types (not class or object interfaces),
21
+ * making them composable and easy to pass as dependencies (dependency injection).
22
+ * Concrete implementations are provided in `field-arithmetic.ts`.
23
+ *
24
+ * ## Constant-Time Considerations
25
+ *
26
+ * In cryptographic contexts, arithmetic functions must execute in *constant time*
27
+ * to prevent timing side-channel attacks. An adversary who can precisely measure
28
+ * how long an operation takes may recover secret inputs (e.g., private keys or
29
+ * witness values) if the runtime depends on those inputs.
30
+ *
31
+ * Implementations conforming to these interfaces should:
32
+ * - Avoid data-dependent branching (`if`/`else` on secret data)
33
+ * - Use uniform execution paths regardless of input values
34
+ * - Employ bitwise masking instead of conditional selection
35
+ * - Not short-circuit on special values (e.g., zero or one)
36
+ *
37
+ * Note: JavaScript's `bigint` type does not provide hardware-level constant-time
38
+ * guarantees due to JIT optimization and platform differences. The implementations
39
+ * in `field-arithmetic.ts` are *constant-operation-count*, which is the practical
40
+ * standard for safe JavaScript cryptography.
41
+ *
42
+ * ## Limb Representation
43
+ *
44
+ * The concrete implementations in `field-arithmetic.ts` operate on 256-bit field
45
+ * elements decomposed into 4 x 64-bit limbs in little-endian order. This
46
+ * representation enables efficient constant-time arithmetic via native 64-bit
47
+ * operations and avoids variable-time branches in the BigInt layer.
48
+ *
49
+ * See `Bn254FieldElementLimb` for the canonical limb tuple type.
50
+ *
51
+ * ## Field Element Sampling
52
+ *
53
+ * This module also defines the interface and dependency injection types for the
54
+ * U512-based BN254 field element sampler, which reduces a 512-bit byte array to
55
+ * a uniformly distributed field element.
56
+ *
57
+ * @see {@link ./field-arithmetic} Concrete constant-time implementations
58
+ * @see {@link ./types} BN254 field element types and validation
59
+ * @see {@link ./field-element-sampler} Concrete sampler implementation
60
+ *
61
+ * @packageDocumentation
62
+ * @module math/bn254/interfaces
63
+ */
64
+
65
+ /**
66
+ * The 4-limb (64-bit little-endian) internal representation of a BN254 field element.
67
+ *
68
+ * A 256-bit field element is split into four 64-bit `bigint` limbs stored in
69
+ * little-endian order (least significant limb first):
70
+ * - `limbs[0]`: bits 0–63 (least significant)
71
+ * - `limbs[1]`: bits 64–127
72
+ * - `limbs[2]`: bits 128–191
73
+ * - `limbs[3]`: bits 192–255 (most significant)
74
+ *
75
+ * @remarks
76
+ * ## Why 4-Limb Representation?
77
+ *
78
+ * Native JavaScript `bigint` operations are not constant-time — their runtime
79
+ * depends on the magnitude of the operands. The 4-limb representation allows
80
+ * constant-time arithmetic to be implemented by operating on fixed-size 64-bit
81
+ * chunks independently, using bitwise masking for conditional selection and
82
+ * carry propagation for addition/subtraction.
83
+ *
84
+ * ## Reconstruction Formula
85
+ *
86
+ * The bigint value `v` represented by a limb tuple `[l0, l1, l2, l3]` is:
87
+ * ```
88
+ * v = l0 + l1 * 2^64 + l2 * 2^128 + l3 * 2^192
89
+ * ```
90
+ *
91
+ * ## Readonly Constraint
92
+ *
93
+ * The tuple is `readonly` to prevent accidental in-place mutation of limb arrays,
94
+ * which could introduce subtle state corruption bugs in arithmetic pipelines.
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * import { type Bn254FieldElementLimb } from "@umbra/sdk";
99
+ *
100
+ * // The number 1 in limb representation
101
+ * const one: Bn254FieldElementLimb = [1n, 0n, 0n, 0n];
102
+ *
103
+ * // A 65-bit value (spans limbs 0 and 1)
104
+ * const large: Bn254FieldElementLimb = [
105
+ * 0xFFFFFFFFFFFFFFFFn, // bits 0-63: all set
106
+ * 0x0000000000000001n, // bits 64-127: only bit 64 set
107
+ * 0n,
108
+ * 0n,
109
+ * ];
110
+ * // large represents 2^64 + (2^64 - 1) = 2^65 - 1
111
+ * ```
112
+ *
113
+ * @see {@link ModuloAddFunction} Uses this type internally
114
+ * @see {@link ./field-arithmetic} The module that uses this representation
115
+ *
116
+ * @public
117
+ */
118
+ type Bn254FieldElementLimb = readonly [bigint, bigint, bigint, bigint];
119
+ /**
120
+ * A function type for constant-time modular addition over the BN254 scalar field.
121
+ *
122
+ * Computes `(a + b) mod p` where `p` is `BN254_FIELD_PRIME`.
123
+ *
124
+ * @param a - First operand. Must be in the canonical range `[0, p-1]`.
125
+ * @param b - Second operand. Must be in the canonical range `[0, p-1]`.
126
+ * @returns The sum `(a + b) mod p` in the canonical range `[0, p-1]`.
127
+ *
128
+ * @remarks
129
+ * ## Algorithm (Constant-Time)
130
+ *
131
+ * A constant-time implementation proceeds as follows, operating on 64-bit limbs:
132
+ *
133
+ * 1. **Raw addition**: compute `t = a + b` with carry propagation across all 4 limbs.
134
+ * Record whether the sum overflowed 256 bits (`tCarry`).
135
+ * 2. **Trial subtraction**: compute `d = t - p` with borrow propagation.
136
+ * Record whether the subtraction underflowed (`borrow`).
137
+ * 3. **Constant-time selection**: return `d` if `tCarry == 1` (overflow occurred,
138
+ * so `t >= p`) or if `borrow == 0` (no underflow, so `t >= p`); otherwise return `t`.
139
+ *
140
+ * No data-dependent branch is taken — the selection is performed using bitwise masking.
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * import { bn254ModuloAdd, BN254_FIELD_PRIME } from "@umbra/sdk";
145
+ *
146
+ * // Basic addition
147
+ * const sum = bn254ModuloAdd(10n, 20n); // 30n
148
+ *
149
+ * // Wrap-around at the field boundary
150
+ * const nearPrime = BN254_FIELD_PRIME - 1n;
151
+ * const wrapped = bn254ModuloAdd(nearPrime, 2n); // 1n
152
+ * ```
153
+ *
154
+ * @see {@link ModuloSubFunction} For modular subtraction
155
+ * @see {@link ModuloMulFunction} For modular multiplication
156
+ * @see {@link ./field-arithmetic.bn254ModuloAdd} The concrete implementation
157
+ *
158
+ * @public
159
+ */
160
+ type ModuloAddFunction = (a: bigint, b: bigint) => bigint;
161
+ /**
162
+ * A function type for constant-time modular subtraction over the BN254 scalar field.
163
+ *
164
+ * Computes `(a - b) mod p` where `p` is `BN254_FIELD_PRIME`.
165
+ *
166
+ * @param a - Minuend. Must be in the canonical range `[0, p-1]`.
167
+ * @param b - Subtrahend. Must be in the canonical range `[0, p-1]`.
168
+ * @returns The difference `(a - b) mod p` in the canonical range `[0, p-1]`.
169
+ *
170
+ * @remarks
171
+ * ## Algorithm (Constant-Time)
172
+ *
173
+ * A constant-time implementation proceeds as follows, operating on 64-bit limbs:
174
+ *
175
+ * 1. **Raw subtraction**: compute `diff = a - b` with borrow propagation across
176
+ * all 4 limbs. Record whether underflow occurred (`borrow`).
177
+ * 2. **Conditional correction**: if `borrow == 1` (underflow), the result is
178
+ * negative — add `p` back. The addition of `p` is gated by a bitmask derived
179
+ * from `borrow`, so it executes unconditionally in both branches (the mask is
180
+ * `0xFFF...F` when correction is needed, `0x000...0` otherwise).
181
+ *
182
+ * No data-dependent branch is taken — the correction is performed via bitwise masking.
183
+ *
184
+ * @example
185
+ * ```typescript
186
+ * import { bn254ModuloSub, BN254_FIELD_PRIME } from "@umbra/sdk";
187
+ *
188
+ * // Basic subtraction
189
+ * const diff = bn254ModuloSub(30n, 10n); // 20n
190
+ *
191
+ * // Underflow wraps around via field addition
192
+ * const wrapped = bn254ModuloSub(5n, 10n); // BN254_FIELD_PRIME - 5n
193
+ * ```
194
+ *
195
+ * @see {@link ModuloAddFunction} For modular addition
196
+ * @see {@link ModuloNegFunction} For modular negation (special case of subtraction)
197
+ * @see {@link ./field-arithmetic.bn254ModuloSub} The concrete implementation
198
+ *
199
+ * @public
200
+ */
201
+ type ModuloSubFunction = (a: bigint, b: bigint) => bigint;
202
+ /**
203
+ * A function type for constant-time modular multiplication over the BN254 scalar field.
204
+ *
205
+ * Computes `(a * b) mod p` where `p` is `BN254_FIELD_PRIME`.
206
+ *
207
+ * @param a - First factor. Must be in the canonical range `[0, p-1]`.
208
+ * @param b - Second factor. Must be in the canonical range `[0, p-1]`.
209
+ * @returns The product `(a * b) mod p` in the canonical range `[0, p-1]`.
210
+ *
211
+ * @remarks
212
+ * ## Why Not `(a * b) % p` Directly?
213
+ *
214
+ * The naive `(a * b) % p` is NOT constant-time: JavaScript's `bigint` modulo
215
+ * operation has variable latency that depends on the operand sizes and potentially
216
+ * their values. For secret-key operations, this leaks timing information.
217
+ *
218
+ * ## Montgomery Multiplication
219
+ *
220
+ * The standard constant-time approach is Montgomery multiplication. It transforms
221
+ * values into "Montgomery domain" (`a -> aR mod p`, where `R = 2^256`), performs
222
+ * multiplication, and transforms back. Within the Montgomery domain, one reduction
223
+ * step replaces the full modulo operation:
224
+ *
225
+ * 1. Convert: `aR = a * R^2 * R^(-1) mod p = MontMul(a, R^2)`
226
+ * 2. Multiply in Montgomery domain: `abR = MontMul(aR, bR)`
227
+ * 3. Convert back: `ab = MontMul(abR, 1) mod p`
228
+ *
229
+ * The Montgomery reduction step (REDC or CIOS) works exclusively with 64-bit limb
230
+ * arithmetic and a precomputed constant `p' = -p^(-1) mod 2^64`, making it
231
+ * constant-time.
232
+ *
233
+ * @example
234
+ * ```typescript
235
+ * import { bn254ModuloMul, BN254_FIELD_PRIME } from "@umbra/sdk";
236
+ *
237
+ * const product = bn254ModuloMul(7n, 8n); // 56n
238
+ *
239
+ * // All results are reduced mod p
240
+ * const result = bn254ModuloMul(BN254_FIELD_PRIME - 1n, 2n); // p - 2n
241
+ * ```
242
+ *
243
+ * @see {@link ModuloAddFunction} For modular addition
244
+ * @see {@link ModuloInvFunction} For multiplicative inverse
245
+ * @see {@link ./field-arithmetic.bn254ModuloMul} The concrete Montgomery implementation
246
+ *
247
+ * @public
248
+ */
249
+ type ModuloMulFunction = (a: bigint, b: bigint) => bigint;
250
+ /**
251
+ * A function type for constant-time modular negation over the BN254 scalar field.
252
+ *
253
+ * Computes `-a mod p`, which equals `p - a` for non-zero `a` and `0` when `a == 0`.
254
+ *
255
+ * @param a - The value to negate. Must be in the canonical range `[0, p-1]`.
256
+ * @returns `-a mod p` in the canonical range `[0, p-1]`.
257
+ *
258
+ * @remarks
259
+ * ## Formula
260
+ *
261
+ * ```
262
+ * result = (a == 0) ? 0 : (p - a)
263
+ * ```
264
+ *
265
+ * The additive inverse of a field element `a` is the unique element `-a` such that
266
+ * `a + (-a) ≡ 0 (mod p)`.
267
+ *
268
+ * ## Constant-Time Zero Handling
269
+ *
270
+ * The special case `a == 0` (where the naive `p - a = p` would be out of range)
271
+ * must be handled without a data-dependent branch. A constant-time implementation
272
+ * computes `p - a` unconditionally and then uses bitwise masking to zero out the
273
+ * result when `a` is zero:
274
+ * ```
275
+ * isZero = (a[0] | a[1] | a[2] | a[3]) == 0 ? 1 : 0
276
+ * result = ctSelect(isZero, 0, p - a)
277
+ * ```
278
+ *
279
+ * @example
280
+ * ```typescript
281
+ * import { bn254ModuloNeg, bn254ModuloAdd, BN254_FIELD_PRIME } from "@umbra/sdk";
282
+ *
283
+ * const a = 10n;
284
+ * const negA = bn254ModuloNeg(a); // BN254_FIELD_PRIME - 10n
285
+ *
286
+ * // Verify: a + (-a) = 0 (mod p)
287
+ * const sum = bn254ModuloAdd(a, negA); // 0n
288
+ *
289
+ * // Special case: negation of zero is zero
290
+ * console.log(bn254ModuloNeg(0n) === 0n); // true
291
+ * ```
292
+ *
293
+ * @see {@link ModuloSubFunction} Subtraction generalizes negation
294
+ * @see {@link ./field-arithmetic.bn254ModuloNeg} The concrete implementation
295
+ *
296
+ * @public
297
+ */
298
+ type ModuloNegFunction = (a: bigint) => bigint;
299
+ /**
300
+ * A function type for constant-time modular multiplicative inverse over the BN254 field.
301
+ *
302
+ * Computes `a^(-1) mod p`, the unique field element satisfying `a * a^(-1) ≡ 1 (mod p)`.
303
+ *
304
+ * @param a - The value to invert. Must be in the canonical range `[1, p-1]`.
305
+ * Passing `0` is undefined behavior and implementations should throw.
306
+ * @returns `a^(-1) mod p` in the canonical range `[1, p-1]`.
307
+ * @throws {Error} If `a === 0n`. Zero has no multiplicative inverse in any field.
308
+ *
309
+ * @remarks
310
+ * ## Mathematical Background
311
+ *
312
+ * For a prime field F_p, every non-zero element `a` has a unique multiplicative
313
+ * inverse by Fermat's Little Theorem:
314
+ * ```
315
+ * a^(p-1) ≡ 1 (mod p) => a^(-1) ≡ a^(p-2) (mod p)
316
+ * ```
317
+ *
318
+ * ## Implementation Strategy
319
+ *
320
+ * Rather than the Extended Euclidean Algorithm (which has variable-time
321
+ * data-dependent branches), implementations conforming to this interface should
322
+ * use Fermat's Little Theorem with square-and-multiply in Montgomery form:
323
+ *
324
+ * 1. Convert `a` to Montgomery form: `aM = MontMul(a, R^2)`
325
+ * 2. Compute `aM^(p-2)` via square-and-multiply (using `montgomeryMulLimbs`)
326
+ * 3. Convert result back from Montgomery form
327
+ *
328
+ * The Montgomery square-and-multiply is NOT constant-time with respect to the
329
+ * exponent because it branches on bits of `p-2`. However, since `p` is a public
330
+ * constant (not secret), this is acceptable.
331
+ *
332
+ * ## Use Cases
333
+ *
334
+ * - Polynomial division (multiply by the inverse of the divisor)
335
+ * - Computing Lagrange basis polynomials
336
+ * - Batch inversion via Montgomery's trick
337
+ *
338
+ * @example
339
+ * ```typescript
340
+ * import { bn254ModuloInv, bn254ModuloMul } from "@umbra/sdk";
341
+ *
342
+ * const a = 7n;
343
+ * const invA = bn254ModuloInv(a);
344
+ *
345
+ * // Verify: a * a^(-1) = 1 (mod p)
346
+ * const product = bn254ModuloMul(a, invA); // 1n
347
+ *
348
+ * // Division: b / a = b * a^(-1) mod p
349
+ * const b = 42n;
350
+ * const quotient = bn254ModuloMul(b, invA); // 6n (42 / 7)
351
+ * ```
352
+ *
353
+ * @see {@link ModuloMulFunction} For multiplication (used to verify inverse)
354
+ * @see {@link ./field-arithmetic.bn254ModuloInv} The concrete Fermat-based implementation
355
+ * @see {@link ./operations.computeBn254ModularInverse} Higher-level branded-type inverse
356
+ *
357
+ * @public
358
+ */
359
+ type ModuloInvFunction = (a: bigint) => bigint;
360
+ /**
361
+ * Optional dependency injection container for the U512-based BN254 field element sampler.
362
+ *
363
+ * Callers may inject custom `modAdd` and `modMul` implementations to override the
364
+ * default constant-time arithmetic used by `getBn254FieldElementSampler`.
365
+ * This is useful for:
366
+ * - **Testing**: inject mock arithmetic that counts invocations or logs values.
367
+ * - **Optimization**: inject a faster (potentially non-constant-time) implementation
368
+ * for non-secret contexts such as test vector generation.
369
+ * - **Benchmarking**: compare performance of different implementation strategies.
370
+ *
371
+ * @remarks
372
+ * All fields are optional. When a field is omitted, the factory function falls back
373
+ * to the production constant-time implementations from `field-arithmetic.ts`:
374
+ * - `modAdd` defaults to `bn254ModuloAdd`
375
+ * - `modMul` defaults to `bn254ModuloMul`
376
+ *
377
+ * @example
378
+ * ```typescript
379
+ * import { getBn254FieldElementSampler } from "@umbra/sdk";
380
+ *
381
+ * // Override only modMul for testing
382
+ * let mulCallCount = 0;
383
+ * const sampler = getBn254FieldElementSampler({
384
+ * modMul: (a, b) => {
385
+ * mulCallCount++;
386
+ * return (a * b) % BN254_FIELD_PRIME; // simple non-CT version for tests
387
+ * },
388
+ * });
389
+ *
390
+ * sampler(someInput);
391
+ * console.log(mulCallCount); // 1 (one multiplication per sampler call)
392
+ * ```
393
+ *
394
+ * @see {@link U512BasedBn254FieldElementSamplerFunction} The sampler function type
395
+ * @see {@link ModuloAddFunction} The addition function interface
396
+ * @see {@link ModuloMulFunction} The multiplication function interface
397
+ *
398
+ * @public
399
+ */
400
+ interface U512BasedBn254FieldElementSamplerDeps {
401
+ /**
402
+ * Custom modular addition function for `(a + b) mod p`.
403
+ *
404
+ * If not provided, defaults to the constant-time `bn254ModuloAdd` implementation
405
+ * from `field-arithmetic.ts`.
406
+ *
407
+ * @see {@link ModuloAddFunction}
408
+ */
409
+ readonly modAdd?: ModuloAddFunction;
410
+ /**
411
+ * Custom modular multiplication function for `(a * b) mod p`.
412
+ *
413
+ * If not provided, defaults to the constant-time `bn254ModuloMul` (Montgomery
414
+ * multiplication) implementation from `field-arithmetic.ts`.
415
+ *
416
+ * @see {@link ModuloMulFunction}
417
+ */
418
+ readonly modMul?: ModuloMulFunction;
419
+ }
420
+ /**
421
+ * A function type for reducing a 512-bit big-endian byte array to a uniformly
422
+ * distributed BN254 scalar field element.
423
+ *
424
+ * @param input - A 512-bit (64-byte) byte array in big-endian order (`U512BeBytes`).
425
+ * The caller is responsible for ensuring the input has exactly 64 bytes.
426
+ * @returns A BN254 scalar field element (as `bigint`) in the canonical range `[0, p-1]`.
427
+ *
428
+ * @remarks
429
+ * ## Purpose
430
+ *
431
+ * Cryptographic protocols frequently need to derive field elements from hash outputs
432
+ * or random byte strings. A 256-bit input would introduce non-negligible bias because
433
+ * `2^256 mod p != 0` (the top few values of the 256-bit range would be over-represented).
434
+ * Using a 512-bit input reduces the statistical bias to `2^(-256)`, which is
435
+ * cryptographically negligible.
436
+ *
437
+ * ## Algorithm
438
+ *
439
+ * The 512-bit input `h` is split into two 256-bit halves in big-endian byte order:
440
+ * ```
441
+ * high = h[0..31] (most significant 256 bits)
442
+ * low = h[32..63] (least significant 256 bits)
443
+ * ```
444
+ *
445
+ * Each half is independently reduced to `[0, p-1]` using constant-time trial
446
+ * subtraction. Then the field element is computed as:
447
+ * ```
448
+ * result = (high * R + low) mod p
449
+ * ```
450
+ * where `R = 2^256 mod p` is a precomputed constant
451
+ * (`6350874878119819312338956282401532410528162663560392320966563075034087161851n`).
452
+ *
453
+ * ## Uniformity Analysis
454
+ *
455
+ * The 512-bit input space has `2^512` values; the field has `p ≈ 2^254` elements.
456
+ * The number of inputs mapping to each field element differs by at most 1, giving
457
+ * maximum bias of `1 / p ≈ 2^(-254)`, well within cryptographic security bounds.
458
+ *
459
+ * ## Constant-Time Guarantees
460
+ *
461
+ * Implementations conforming to this interface should use constant-time modular
462
+ * arithmetic for both the `reduce256` step and the final `high * R + low` computation.
463
+ *
464
+ * @example
465
+ * ```typescript
466
+ * import {
467
+ * getBn254FieldElementSampler,
468
+ * bn254FieldElementSampler,
469
+ * } from "@umbra/sdk";
470
+ *
471
+ * // Using the pre-instantiated default sampler
472
+ * const input = new Uint8Array(64); // 512 zero bits
473
+ * assertU512BeBytes(input);
474
+ * const zero = bn254FieldElementSampler(input);
475
+ * // zero is 0n (the additive identity of the field)
476
+ *
477
+ * // Using the factory for custom arithmetic
478
+ * const sampler = getBn254FieldElementSampler();
479
+ * const randomBytes = crypto.getRandomValues(new Uint8Array(64));
480
+ * assertU512BeBytes(randomBytes);
481
+ * const fieldElement = sampler(randomBytes);
482
+ * // fieldElement is a uniformly random element of F_r
483
+ * ```
484
+ *
485
+ * @see {@link U512BasedBn254FieldElementSamplerDeps} Optional dependency injection
486
+ * @see {@link ./field-element-sampler.getBn254FieldElementSampler} The factory
487
+ * @see {@link ./field-element-sampler.bn254FieldElementSampler} Pre-instantiated sampler
488
+ *
489
+ * @public
490
+ */
491
+ type U512BasedBn254FieldElementSamplerFunction = (input: U512BeBytes) => bigint;
492
+
493
+ /**
494
+ * Curve25519 Base Field Types
495
+ *
496
+ * This module defines the branded type and validation primitives for elements
497
+ * of the Curve25519 base field, whose prime modulus is p = 2^255 - 19.
498
+ *
499
+ * ## What is Curve25519?
500
+ *
501
+ * Curve25519 is a Montgomery-form elliptic curve designed by Daniel J. Bernstein
502
+ * for high-performance, side-channel-resistant Diffie-Hellman key exchange. It is
503
+ * specified in RFC 7748 and underlies two widely deployed cryptographic primitives:
504
+ *
505
+ * - **X25519** — Diffie-Hellman key agreement. Used in Umbra for encrypted token
506
+ * account key registration (users register an X25519 public key so MXE-encrypted
507
+ * balances can be shared with them).
508
+ * - **Ed25519** — Digital signatures (uses the twisted-Edwards form of the same
509
+ * underlying field).
510
+ *
511
+ * ## The Base Field GF(p)
512
+ *
513
+ * All Curve25519 point coordinates are elements of the prime field GF(p) where:
514
+ * ```
515
+ * p = 2^255 - 19
516
+ * = 57896044618658097711785492504343953926634992332820282019728792003956564819949
517
+ * ```
518
+ *
519
+ * The near-power-of-two structure of p makes modular reduction especially
520
+ * efficient: because p ≡ -19 (mod 2^255), any reduction can be expressed as a
521
+ * small multiply-and-add rather than a full division.
522
+ *
523
+ * ## Rescue-XLIX Cipher
524
+ *
525
+ * Within the Umbra SDK, field arithmetic over GF(p) is used primarily by the
526
+ * **Rescue-XLIX block cipher** (see `crypto/rescue-cipher/`). Rescue-XLIX operates
527
+ * over this same field and is the symmetric cipher used inside Arcium MPC
528
+ * computations to produce and verify ciphertexts for encrypted Umbra token accounts.
529
+ *
530
+ * ## Contrast with BN254
531
+ *
532
+ * The SDK also provides `math/bn254/` for the BN254 scalar field
533
+ * (p ≈ 2^254, ~100-bit security, pairing-friendly). That field is used for
534
+ * Groth16 ZK proofs and Poseidon hashes. The two fields are **not interchangeable**:
535
+ * elements of one field are not valid elements of the other.
536
+ *
537
+ * ## Branded Types
538
+ *
539
+ * `Curve25519FieldElement` is a branded `bigint` subtype. The branding enforces
540
+ * at compile time that only validated, in-range values are passed to field
541
+ * arithmetic functions, preventing silent truncation bugs.
542
+ *
543
+ * @module math/curve25519/types
544
+ * @packageDocumentation
545
+ */
546
+
547
+ /**
548
+ * Branded type representing a validated element of the Curve25519 base field
549
+ * GF(2^255 - 19).
550
+ *
551
+ * @remarks
552
+ * A `Curve25519FieldElement` is a `bigint` that has been verified to lie in the
553
+ * canonical range [0, p-1] where p = 2^255 - 19. The branding is a
554
+ * compile-time phantom that prevents unvalidated bigints from being passed
555
+ * to functions that expect field elements.
556
+ *
557
+ * The type sits at the third level of the branded-type hierarchy:
558
+ * ```
559
+ * bigint
560
+ * └── U256 (branded bigint: 0 ≤ value < 2^256)
561
+ * └── Curve25519FieldElement (branded U256: 0 ≤ value < p)
562
+ * ```
563
+ *
564
+ * Unlike BN254FieldElement (used for Groth16 proofs / Poseidon hashes),
565
+ * Curve25519FieldElement operates over a **different prime** and must not be
566
+ * mixed with BN254 arithmetic. Field elements from the two curves are
567
+ * structurally identical at runtime (both are `bigint`) but semantically
568
+ * incompatible; the branded type hierarchy enforces this distinction
569
+ * statically.
570
+ *
571
+ * ## Security Properties
572
+ *
573
+ * - Provides approximately 128 bits of security for discrete-logarithm problems.
574
+ * - Curve25519 is designed to resist timing attacks: the prime's structure
575
+ * permits implementations with uniform execution paths.
576
+ * - Used in X25519 (ECDH) for Umbra encrypted token account key registration
577
+ * and in Rescue-XLIX for MPC-layer symmetric encryption.
578
+ *
579
+ * @example
580
+ * ```typescript
581
+ * import {
582
+ * assertCurve25519FieldElement,
583
+ * type Curve25519FieldElement,
584
+ * } from "@umbra-privacy/sdk";
585
+ *
586
+ * // Obtain a branded field element after validation
587
+ * const raw = 12345n;
588
+ * assertCurve25519FieldElement(raw, "raw");
589
+ * const element: Curve25519FieldElement = raw;
590
+ *
591
+ * // Type error: plain bigint is not assignable to Curve25519FieldElement
592
+ * // const bad: Curve25519FieldElement = 12345n; // compile error
593
+ * ```
594
+ *
595
+ * @see {@link assertCurve25519FieldElement} — runtime validator that narrows `bigint` to this type
596
+ * @see {@link CURVE25519_FIELD_PRIME} — the field prime p = 2^255 - 19
597
+ * @see https://cr.yp.to/ecdh/curve25519-20060209.pdf — original Curve25519 specification
598
+ * @see https://www.rfc-editor.org/rfc/rfc7748 — X25519 / X448 RFC
599
+ *
600
+ * @public
601
+ */
602
+ type Curve25519FieldElement = SubSubBrandedType<U256, "Curve25519FieldElement">;
603
+
604
+ /**
605
+ * Curve25519 Base Field Arithmetic — Function Type Interfaces
606
+ *
607
+ * This module defines TypeScript function-type aliases and dependency-injection
608
+ * interfaces for modular arithmetic operations over the Curve25519 prime field
609
+ * GF(p), where p = 2^255 - 19. The interfaces are consumed by the
610
+ * `math/curve25519/field-arithmetic` implementation module and by the
611
+ * Rescue-XLIX cipher (`crypto/rescue-cipher/`) which operates over this field.
612
+ *
613
+ * ## Purpose of Interface Separation
614
+ *
615
+ * Defining arithmetic operations as named function types (rather than
616
+ * inline signatures) provides three benefits:
617
+ *
618
+ * - **Substitutability** — callers can inject alternative implementations
619
+ * (e.g., a faster platform-native library, or a simplified test double)
620
+ * without changing call sites.
621
+ * - **Testability** — test suites can supply deterministic stubs.
622
+ * - **Documentation clarity** — each operation has an unambiguous name and
623
+ * a single authoritative description.
624
+ *
625
+ * ## Background: Modular Arithmetic over GF(p)
626
+ *
627
+ * A **prime field** GF(p) is the set {0, 1, …, p-1} equipped with addition
628
+ * and multiplication defined modulo p. For the Curve25519 field:
629
+ * ```
630
+ * p = 2^255 - 19
631
+ * ≈ 5.79 × 10^76
632
+ * ```
633
+ *
634
+ * Every non-zero element has a unique multiplicative inverse; the field
635
+ * is closed under all four arithmetic operations (with division by zero
636
+ * being the sole undefined case).
637
+ *
638
+ * ## Constant-Time Considerations
639
+ *
640
+ * In cryptographic contexts, it is critical that arithmetic operations execute
641
+ * in constant time to prevent timing side-channel attacks. Implementations
642
+ * conforming to these interfaces should:
643
+ *
644
+ * - Avoid data-dependent branching (if/else on secret data).
645
+ * - Use uniform execution paths regardless of input values.
646
+ * - Employ bitwise masking instead of conditional selection.
647
+ *
648
+ * Note: true hardware-level constant time cannot be guaranteed in JavaScript
649
+ * due to JIT compilation and GC pauses. The SDK's implementations are
650
+ * **constant-operation-count**, which is the practical standard for JS crypto.
651
+ *
652
+ * ## 4-Limb Little-Endian Representation
653
+ *
654
+ * Because JavaScript `BigInt` operations are not constant-time and can vary
655
+ * in cost with the magnitude of their operands, the implementations behind
656
+ * these interfaces decompose each 256-bit field element into four 64-bit
657
+ * **limbs** stored in little-endian order:
658
+ *
659
+ * | Limb index | Bit range |
660
+ * | ---------- | --------------- |
661
+ * | 0 | 0 – 63 (LSB) |
662
+ * | 1 | 64 – 127 |
663
+ * | 2 | 128 – 191 |
664
+ * | 3 | 192 – 255 (MSB) |
665
+ *
666
+ * Operating on fixed-width 64-bit chunks produces uniform BigInt sizes and
667
+ * therefore uniform execution time, regardless of the numeric magnitude of
668
+ * the field element being processed.
669
+ *
670
+ * ## Relationship to Other Modules
671
+ *
672
+ * - **`math/curve25519/types`** — defines `Curve25519FieldElement` (the
673
+ * branded output type) and `CURVE25519_FIELD_PRIME`.
674
+ * - **`math/curve25519/field-arithmetic`** — provides the concrete
675
+ * constant-time implementations of the function types defined here.
676
+ * - **`crypto/rescue-cipher/`** — the Rescue-XLIX block cipher that operates
677
+ * over GF(2^255 - 19) and uses these interfaces for field arithmetic.
678
+ * - **`math/bn254/`** — analogous interfaces for the BN254 scalar field
679
+ * (a different prime, different use case — Groth16 proofs and Poseidon hashes).
680
+ *
681
+ * @module math/curve25519/interfaces
682
+ * @packageDocumentation
683
+ */
684
+
685
+ /**
686
+ * 4-limb little-endian representation of a 256-bit Curve25519 field element.
687
+ *
688
+ * @remarks
689
+ * A `Curve25519FieldElementLimb` is a fixed-size tuple `[limb0, limb1, limb2, limb3]`
690
+ * where each entry is a non-negative `bigint` in the range [0, 2^64 - 1]. The
691
+ * full 256-bit value is reconstructed as:
692
+ *
693
+ * ```
694
+ * value = limb0
695
+ * + limb1 × 2^64
696
+ * + limb2 × 2^128
697
+ * + limb3 × 2^192
698
+ * ```
699
+ *
700
+ * Little-endian ordering means the **least significant** limb occupies index 0.
701
+ * This convention matches x86/x64 multi-precision integer layouts and simplifies
702
+ * carry propagation in hand-rolled addition and subtraction loops.
703
+ *
704
+ * ## Why 4 × 64-bit Limbs?
705
+ *
706
+ * The Curve25519 prime p = 2^255 - 19 fits in 255 bits, which requires at
707
+ * least 4 × 64-bit = 256 bits of storage. The fourth limb (index 3) always
708
+ * has its top bit clear for valid field elements (its maximum value is
709
+ * 0x7FFFFFFFFFFFFFFF). Using exactly 4 limbs avoids the overhead of a 5-limb
710
+ * representation while still covering the full field.
711
+ *
712
+ * ## Constant-Time Use
713
+ *
714
+ * Decomposing a field element into same-sized 64-bit limbs ensures that
715
+ * BigInt arithmetic on each limb operates on identically-sized values,
716
+ * keeping per-operation cost uniform regardless of the element's magnitude.
717
+ *
718
+ * @example
719
+ * ```typescript
720
+ * import type { Curve25519FieldElementLimb } from "@umbra-privacy/sdk";
721
+ *
722
+ * // The multiplicative identity 1 in limb representation
723
+ * const one: Curve25519FieldElementLimb = [1n, 0n, 0n, 0n];
724
+ *
725
+ * // A value that uses all four limbs, e.g. 2^192
726
+ * const highValue: Curve25519FieldElementLimb = [0n, 0n, 0n, 1n];
727
+ * // Reconstructed: 0 + 0 + 0 + 1 × 2^192 = 2^192
728
+ * ```
729
+ *
730
+ * @public
731
+ */
732
+ type Curve25519FieldElementLimb = readonly [bigint, bigint, bigint, bigint];
733
+ /**
734
+ * Function type for constant-time modular addition over GF(2^255 - 19).
735
+ *
736
+ * @remarks
737
+ * Computes the field sum:
738
+ * ```
739
+ * result ≡ a + b (mod p), where p = 2^255 - 19
740
+ * ```
741
+ *
742
+ * The result always lies in the canonical range [0, p-1]. Implementations
743
+ * must not return values outside this range, even when a + b overflows 255 bits.
744
+ *
745
+ * A constant-time implementation performs the addition unconditionally and
746
+ * then conditionally subtracts p using bitwise masking — never an `if` branch
747
+ * on whether the sum exceeded p.
748
+ *
749
+ * @param a - First field element. Must be in the canonical range [0, p-1].
750
+ * @param b - Second field element. Must be in the canonical range [0, p-1].
751
+ * @returns The canonical field sum `(a + b) mod p`, in range [0, p-1].
752
+ *
753
+ * @see {@link Curve25519ModuloSubFunction} — the complementary subtraction type
754
+ * @see {@link Curve25519ModuloMulFunction} — multiplication type
755
+ *
756
+ * @public
757
+ */
758
+ type Curve25519ModuloAddFunction = (a: bigint, b: bigint) => bigint;
759
+ /**
760
+ * Function type for constant-time modular subtraction over GF(2^255 - 19).
761
+ *
762
+ * @remarks
763
+ * Computes the field difference:
764
+ * ```
765
+ * result ≡ a - b (mod p), where p = 2^255 - 19
766
+ * ```
767
+ *
768
+ * When a < b the result would be negative in ordinary arithmetic; in the field
769
+ * the result wraps around: (a - b) mod p = a - b + p, which is still in [0, p-1].
770
+ *
771
+ * A constant-time implementation always computes the subtraction and conditionally
772
+ * adds back p via a borrow-driven bitmask — never branches on whether a borrow
773
+ * occurred.
774
+ *
775
+ * @param a - Minuend. Must be in the canonical range [0, p-1].
776
+ * @param b - Subtrahend. Must be in the canonical range [0, p-1].
777
+ * @returns The canonical field difference `(a - b) mod p`, in range [0, p-1].
778
+ *
779
+ * @see {@link Curve25519ModuloAddFunction} — the complementary addition type
780
+ *
781
+ * @public
782
+ */
783
+ type Curve25519ModuloSubFunction = (a: bigint, b: bigint) => bigint;
784
+ /**
785
+ * Function type for constant-time modular multiplication over GF(2^255 - 19).
786
+ *
787
+ * @remarks
788
+ * Computes the field product:
789
+ * ```
790
+ * result ≡ a × b (mod p), where p = 2^255 - 19
791
+ * ```
792
+ *
793
+ * Multiplication of two 255-bit numbers produces a 510-bit intermediate product
794
+ * which must be reduced modulo p before the result is returned. The SDK's
795
+ * concrete implementation uses schoolbook multiplication followed by Barrett or
796
+ * direct reduction, rather than Montgomery form (whose conversion overhead is
797
+ * not amortized in this context).
798
+ *
799
+ * @param a - First factor. Must be in the canonical range [0, p-1].
800
+ * @param b - Second factor. Must be in the canonical range [0, p-1].
801
+ * @returns The canonical field product `(a × b) mod p`, in range [0, p-1].
802
+ *
803
+ * @see {@link Curve25519ModuloPowFunction} — repeated squaring using this operation
804
+ * @see {@link Curve25519ModuloInvFunction} — inversion via Fermat's little theorem
805
+ *
806
+ * @public
807
+ */
808
+ type Curve25519ModuloMulFunction = (a: bigint, b: bigint) => bigint;
809
+ /**
810
+ * Function type for modular multiplicative inverse over GF(2^255 - 19).
811
+ *
812
+ * @remarks
813
+ * Computes the multiplicative inverse:
814
+ * ```
815
+ * result ≡ a^(-1) (mod p) such that a × result ≡ 1 (mod p)
816
+ * ```
817
+ *
818
+ * The inverse exists for every non-zero field element. By **Fermat's Little
819
+ * Theorem**, since p is prime:
820
+ * ```
821
+ * a^(-1) ≡ a^(p-2) (mod p)
822
+ * ```
823
+ *
824
+ * Implementations may therefore compute the inverse via modular exponentiation
825
+ * with exponent p - 2, which costs O(log p) multiplications (≈ 255 squarings
826
+ * and ~128 multiplications for this specific prime). Alternatively, an extended
827
+ * Euclidean algorithm can be used if a constant-time variant is available.
828
+ *
829
+ * @param a - The field element to invert. Must be in range [1, p-1].
830
+ * **Zero has no multiplicative inverse.**
831
+ * @returns The unique inverse `a^(-1) mod p`, in range [1, p-1].
832
+ * @throws {Error} When `a === 0n`. Zero has no multiplicative inverse in any field.
833
+ *
834
+ * @see {@link Curve25519ModuloPowFunction} — generalized exponentiation used internally
835
+ *
836
+ * @public
837
+ */
838
+ type Curve25519ModuloInvFunction = (a: bigint) => bigint;
839
+ /**
840
+ * Function type for modular exponentiation over GF(2^255 - 19).
841
+ *
842
+ * @remarks
843
+ * Computes the field power:
844
+ * ```
845
+ * result ≡ base^exp (mod p), where p = 2^255 - 19
846
+ * ```
847
+ *
848
+ * The standard algorithm is **square-and-multiply** (also known as
849
+ * binary exponentiation):
850
+ * 1. Start with `result = 1`.
851
+ * 2. For each bit of `exp` from LSB to MSB:
852
+ * - If the bit is 1, multiply `result` by the current power of `base`.
853
+ * - Square the current power of `base`.
854
+ * 3. Return `result`.
855
+ *
856
+ * This runs in O(log exp) field multiplications. For the full exponent range
857
+ * of 0 to p-1, that is at most 255 squarings.
858
+ *
859
+ * Special cases:
860
+ * - `base^0 ≡ 1 (mod p)` for all `base` (including `base = 0`).
861
+ * - `0^exp ≡ 0 (mod p)` for all `exp > 0`.
862
+ *
863
+ * @param base - The base element. Must be in range [0, p-1].
864
+ * @param exp - The exponent. Must be a non-negative `bigint`.
865
+ * @returns `base^exp mod p`, in range [0, p-1].
866
+ *
867
+ * @see {@link Curve25519ModuloInvFunction} — uses this with exponent p-2
868
+ *
869
+ * @public
870
+ */
871
+ type Curve25519ModuloPowFunction = (base: bigint, exp: bigint) => bigint;
872
+ /**
873
+ * Optional dependency-injection bag for {@link U512BasedCurve25519FieldElementSamplerFunction}
874
+ * factory functions.
875
+ *
876
+ * @remarks
877
+ * The sampler algorithm requires two field operations: modular addition and
878
+ * modular multiplication. By default it uses the SDK's constant-time
879
+ * implementations from `math/curve25519/field-arithmetic`. Providing custom
880
+ * implementations here allows:
881
+ *
882
+ * - **Testing** — inject deterministic stubs to isolate sampler logic.
883
+ * - **Optimization** — swap in a faster native-bigint implementation when
884
+ * side-channel resistance is not required (e.g., server-side tooling).
885
+ * - **Alternative backends** — use a WASM-compiled field library.
886
+ *
887
+ * Both fields are optional; any combination of overrides is valid. Fields not
888
+ * provided fall back to the default SDK implementation.
889
+ *
890
+ * @example
891
+ * ```typescript
892
+ * import {
893
+ * getCurve25519FieldElementSampler,
894
+ * type U512BasedCurve25519FieldElementSamplerDeps,
895
+ * } from "@umbra-privacy/sdk";
896
+ *
897
+ * // Override only multiplication with a naive implementation for testing
898
+ * const deps: U512BasedCurve25519FieldElementSamplerDeps = {
899
+ * modMul: (a, b) => (a * b) % ((1n << 255n) - 19n),
900
+ * };
901
+ * const sampler = getCurve25519FieldElementSampler(deps);
902
+ * ```
903
+ *
904
+ * @see {@link getCurve25519FieldElementSampler} — the factory that accepts these deps
905
+ *
906
+ * @public
907
+ */
908
+ interface U512BasedCurve25519FieldElementSamplerDeps {
909
+ /**
910
+ * Custom modular addition function to use in place of the SDK default.
911
+ *
912
+ * @remarks
913
+ * If omitted, the factory uses `curve25519ModuloAdd` from
914
+ * `math/curve25519/field-arithmetic`, which is the constant-time
915
+ * 4-limb implementation.
916
+ */
917
+ readonly modAdd?: Curve25519ModuloAddFunction;
918
+ /**
919
+ * Custom modular multiplication function to use in place of the SDK default.
920
+ *
921
+ * @remarks
922
+ * If omitted, the factory uses `curve25519ModuloMul` from
923
+ * `math/curve25519/field-arithmetic`, which delegates to JavaScript BigInt
924
+ * multiplication followed by direct modular reduction.
925
+ */
926
+ readonly modMul?: Curve25519ModuloMulFunction;
927
+ }
928
+ /**
929
+ * Function type for sampling a Curve25519 field element from a 512-bit
930
+ * big-endian byte array.
931
+ *
932
+ * @remarks
933
+ * ## Purpose
934
+ *
935
+ * Cryptographic key-derivation and hash-to-field algorithms often produce
936
+ * 512-bit (64-byte) outputs to ensure statistical uniformity when mapping
937
+ * into a prime field. Naively taking the output modulo p introduces bias
938
+ * because 2^512 is not a multiple of p; this function uses a two-step
939
+ * technique that keeps the bias negligible (below 2^-256).
940
+ *
941
+ * ## Algorithm
942
+ *
943
+ * Given a 512-bit big-endian byte array `input`:
944
+ *
945
+ * 1. **Split** into two 256-bit halves in big-endian order:
946
+ * - `high = input[0..31]` (most significant 256 bits)
947
+ * - `low = input[32..63]` (least significant 256 bits)
948
+ *
949
+ * 2. **Reduce** each half independently to the canonical range [0, p) using
950
+ * constant-time trial subtraction (a single conditional subtract of p).
951
+ *
952
+ * 3. **Combine** using the precomputed constant R = 2^256 mod p:
953
+ * ```
954
+ * result = (high × R + low) mod p
955
+ * ```
956
+ * For Curve25519, R = 38 (since 2^256 = 2p + 38, so 2^256 mod p = 38).
957
+ *
958
+ * ## Uniformity Analysis
959
+ *
960
+ * The input space has 2^512 distinct values. After the hash-and-reduce step,
961
+ * each of the p ≈ 2^255 field elements is represented by exactly ⌊2^512/p⌋ or
962
+ * ⌊2^512/p⌋ + 1 input values. The resulting bias is at most 1/p < 2^-254,
963
+ * which is cryptographically negligible.
964
+ *
965
+ * ## Constant-Time Guarantees
966
+ *
967
+ * The default implementation is constant-operation-count: reduction and
968
+ * combination use bitwise masking rather than data-dependent branches.
969
+ *
970
+ * @param input - A 512-bit (64-byte) big-endian byte array (`U512BeBytes`).
971
+ * @returns A `Curve25519FieldElement` in the canonical range [0, p-1].
972
+ *
973
+ * @example
974
+ * ```typescript
975
+ * import {
976
+ * curve25519FieldElementSampler,
977
+ * type U512BasedCurve25519FieldElementSamplerFunction,
978
+ * } from "@umbra-privacy/sdk";
979
+ *
980
+ * // 64 zero bytes → deterministic field element
981
+ * const input = new Uint8Array(64) as U512BeBytes;
982
+ * const element = curve25519FieldElementSampler(input);
983
+ * // element is a valid Curve25519FieldElement (== 0n for all-zero input)
984
+ *
985
+ * // Typical use: pass the 64-byte output of SHA-512 or BLAKE2b-512
986
+ * const hashOutput: U512BeBytes = sha512(someData);
987
+ * const fieldElement = curve25519FieldElementSampler(hashOutput);
988
+ * ```
989
+ *
990
+ * @see {@link getCurve25519FieldElementSampler} — factory that creates instances of this type
991
+ * @see {@link curve25519FieldElementSampler} — default pre-instantiated sampler
992
+ *
993
+ * @public
994
+ */
995
+ type U512BasedCurve25519FieldElementSamplerFunction = (input: U512BeBytes) => Curve25519FieldElement;
996
+
997
+ export type { Bn254FieldElementLimb as B, Curve25519FieldElement as C, ModuloInvFunction as M, U512BasedBn254FieldElementSamplerFunction as U, U512BasedCurve25519FieldElementSamplerFunction as a, ModuloSubFunction as b, ModuloAddFunction as c, Curve25519FieldElementLimb as d, Curve25519ModuloAddFunction as e, Curve25519ModuloInvFunction as f, Curve25519ModuloMulFunction as g, Curve25519ModuloPowFunction as h, Curve25519ModuloSubFunction as i, ModuloMulFunction as j, ModuloNegFunction as k, U512BasedBn254FieldElementSamplerDeps as l, U512BasedCurve25519FieldElementSamplerDeps as m };