@digitaldefiance/ecies-lib 4.17.2 → 4.17.3

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 (169) hide show
  1. package/package.json +1 -1
  2. package/src/i18n-setup.d.ts +7 -1
  3. package/src/i18n-setup.d.ts.map +1 -1
  4. package/src/i18n-setup.js +12 -0
  5. package/src/i18n-setup.js.map +1 -1
  6. package/src/index.d.ts +1 -1
  7. package/src/index.d.ts.map +1 -1
  8. package/src/index.js +2 -1
  9. package/src/index.js.map +1 -1
  10. package/src/lib/voting/index.d.ts +1 -0
  11. package/src/lib/voting/index.d.ts.map +1 -1
  12. package/src/lib/voting/index.js +2 -0
  13. package/src/lib/voting/index.js.map +1 -1
  14. package/src/lib/voting/threshold/decryption-combiner.d.ts +141 -0
  15. package/src/lib/voting/threshold/decryption-combiner.d.ts.map +1 -0
  16. package/src/lib/voting/threshold/decryption-combiner.js +343 -0
  17. package/src/lib/voting/threshold/decryption-combiner.js.map +1 -0
  18. package/src/lib/voting/threshold/enumerations/ceremony-status.d.ts +19 -0
  19. package/src/lib/voting/threshold/enumerations/ceremony-status.d.ts.map +1 -0
  20. package/src/lib/voting/threshold/enumerations/ceremony-status.js +23 -0
  21. package/src/lib/voting/threshold/enumerations/ceremony-status.js.map +1 -0
  22. package/src/lib/voting/threshold/enumerations/guardian-status.d.ts +18 -0
  23. package/src/lib/voting/threshold/enumerations/guardian-status.d.ts.map +1 -0
  24. package/src/lib/voting/threshold/enumerations/guardian-status.js +22 -0
  25. package/src/lib/voting/threshold/enumerations/guardian-status.js.map +1 -0
  26. package/src/lib/voting/threshold/enumerations/index.d.ts +8 -0
  27. package/src/lib/voting/threshold/enumerations/index.d.ts.map +1 -0
  28. package/src/lib/voting/threshold/enumerations/index.js +11 -0
  29. package/src/lib/voting/threshold/enumerations/index.js.map +1 -0
  30. package/src/lib/voting/threshold/enumerations/interval-trigger-type.d.ts +15 -0
  31. package/src/lib/voting/threshold/enumerations/interval-trigger-type.d.ts.map +1 -0
  32. package/src/lib/voting/threshold/enumerations/interval-trigger-type.js +19 -0
  33. package/src/lib/voting/threshold/enumerations/interval-trigger-type.js.map +1 -0
  34. package/src/lib/voting/threshold/enumerations/threshold-audit-event-type.d.ts +29 -0
  35. package/src/lib/voting/threshold/enumerations/threshold-audit-event-type.d.ts.map +1 -0
  36. package/src/lib/voting/threshold/enumerations/threshold-audit-event-type.js +33 -0
  37. package/src/lib/voting/threshold/enumerations/threshold-audit-event-type.js.map +1 -0
  38. package/src/lib/voting/threshold/guardian-registry.d.ts +120 -0
  39. package/src/lib/voting/threshold/guardian-registry.d.ts.map +1 -0
  40. package/src/lib/voting/threshold/guardian-registry.js +227 -0
  41. package/src/lib/voting/threshold/guardian-registry.js.map +1 -0
  42. package/src/lib/voting/threshold/index.d.ts +49 -0
  43. package/src/lib/voting/threshold/index.d.ts.map +1 -0
  44. package/src/lib/voting/threshold/index.js +69 -0
  45. package/src/lib/voting/threshold/index.js.map +1 -0
  46. package/src/lib/voting/threshold/interfaces/ceremony-coordinator.d.ts +19 -0
  47. package/src/lib/voting/threshold/interfaces/ceremony-coordinator.d.ts.map +1 -0
  48. package/src/lib/voting/threshold/interfaces/ceremony-coordinator.js +3 -0
  49. package/src/lib/voting/threshold/interfaces/ceremony-coordinator.js.map +1 -0
  50. package/src/lib/voting/threshold/interfaces/ceremony.d.ts +33 -0
  51. package/src/lib/voting/threshold/interfaces/ceremony.d.ts.map +1 -0
  52. package/src/lib/voting/threshold/interfaces/ceremony.js +3 -0
  53. package/src/lib/voting/threshold/interfaces/ceremony.js.map +1 -0
  54. package/src/lib/voting/threshold/interfaces/combined-decryption.d.ts +20 -0
  55. package/src/lib/voting/threshold/interfaces/combined-decryption.d.ts.map +1 -0
  56. package/src/lib/voting/threshold/interfaces/combined-decryption.js +3 -0
  57. package/src/lib/voting/threshold/interfaces/combined-decryption.js.map +1 -0
  58. package/src/lib/voting/threshold/interfaces/combined-zk-proof.d.ts +16 -0
  59. package/src/lib/voting/threshold/interfaces/combined-zk-proof.d.ts.map +1 -0
  60. package/src/lib/voting/threshold/interfaces/combined-zk-proof.js +3 -0
  61. package/src/lib/voting/threshold/interfaces/combined-zk-proof.js.map +1 -0
  62. package/src/lib/voting/threshold/interfaces/decryption-combiner.d.ts +14 -0
  63. package/src/lib/voting/threshold/interfaces/decryption-combiner.d.ts.map +1 -0
  64. package/src/lib/voting/threshold/interfaces/decryption-combiner.js +3 -0
  65. package/src/lib/voting/threshold/interfaces/decryption-combiner.js.map +1 -0
  66. package/src/lib/voting/threshold/interfaces/guardian-registry.d.ts +28 -0
  67. package/src/lib/voting/threshold/interfaces/guardian-registry.d.ts.map +1 -0
  68. package/src/lib/voting/threshold/interfaces/guardian-registry.js +3 -0
  69. package/src/lib/voting/threshold/interfaces/guardian-registry.js.map +1 -0
  70. package/src/lib/voting/threshold/interfaces/guardian-status-change-event.d.ts +16 -0
  71. package/src/lib/voting/threshold/interfaces/guardian-status-change-event.d.ts.map +1 -0
  72. package/src/lib/voting/threshold/interfaces/guardian-status-change-event.js +3 -0
  73. package/src/lib/voting/threshold/interfaces/guardian-status-change-event.js.map +1 -0
  74. package/src/lib/voting/threshold/interfaces/guardian.d.ts +25 -0
  75. package/src/lib/voting/threshold/interfaces/guardian.d.ts.map +1 -0
  76. package/src/lib/voting/threshold/interfaces/guardian.js +3 -0
  77. package/src/lib/voting/threshold/interfaces/guardian.js.map +1 -0
  78. package/src/lib/voting/threshold/interfaces/index.d.ts +32 -0
  79. package/src/lib/voting/threshold/interfaces/index.d.ts.map +1 -0
  80. package/src/lib/voting/threshold/interfaces/index.js +6 -0
  81. package/src/lib/voting/threshold/interfaces/index.js.map +1 -0
  82. package/src/lib/voting/threshold/interfaces/interval-config.d.ts +17 -0
  83. package/src/lib/voting/threshold/interfaces/interval-config.d.ts.map +1 -0
  84. package/src/lib/voting/threshold/interfaces/interval-config.js +3 -0
  85. package/src/lib/voting/threshold/interfaces/interval-config.js.map +1 -0
  86. package/src/lib/voting/threshold/interfaces/interval-scheduler.d.ts +23 -0
  87. package/src/lib/voting/threshold/interfaces/interval-scheduler.d.ts.map +1 -0
  88. package/src/lib/voting/threshold/interfaces/interval-scheduler.js +3 -0
  89. package/src/lib/voting/threshold/interfaces/interval-scheduler.js.map +1 -0
  90. package/src/lib/voting/threshold/interfaces/interval-tally.d.ts +30 -0
  91. package/src/lib/voting/threshold/interfaces/interval-tally.d.ts.map +1 -0
  92. package/src/lib/voting/threshold/interfaces/interval-tally.js +3 -0
  93. package/src/lib/voting/threshold/interfaces/interval-tally.js.map +1 -0
  94. package/src/lib/voting/threshold/interfaces/interval-trigger-event.d.ts +20 -0
  95. package/src/lib/voting/threshold/interfaces/interval-trigger-event.d.ts.map +1 -0
  96. package/src/lib/voting/threshold/interfaces/interval-trigger-event.js +3 -0
  97. package/src/lib/voting/threshold/interfaces/interval-trigger-event.js.map +1 -0
  98. package/src/lib/voting/threshold/interfaces/key-share.d.ts +15 -0
  99. package/src/lib/voting/threshold/interfaces/key-share.d.ts.map +1 -0
  100. package/src/lib/voting/threshold/interfaces/key-share.js +3 -0
  101. package/src/lib/voting/threshold/interfaces/key-share.js.map +1 -0
  102. package/src/lib/voting/threshold/interfaces/partial-decryption-service.d.ts +17 -0
  103. package/src/lib/voting/threshold/interfaces/partial-decryption-service.d.ts.map +1 -0
  104. package/src/lib/voting/threshold/interfaces/partial-decryption-service.js +3 -0
  105. package/src/lib/voting/threshold/interfaces/partial-decryption-service.js.map +1 -0
  106. package/src/lib/voting/threshold/interfaces/partial-decryption.d.ts +20 -0
  107. package/src/lib/voting/threshold/interfaces/partial-decryption.d.ts.map +1 -0
  108. package/src/lib/voting/threshold/interfaces/partial-decryption.js +3 -0
  109. package/src/lib/voting/threshold/interfaces/partial-decryption.js.map +1 -0
  110. package/src/lib/voting/threshold/interfaces/public-tally-feed.d.ts +19 -0
  111. package/src/lib/voting/threshold/interfaces/public-tally-feed.d.ts.map +1 -0
  112. package/src/lib/voting/threshold/interfaces/public-tally-feed.js +3 -0
  113. package/src/lib/voting/threshold/interfaces/public-tally-feed.js.map +1 -0
  114. package/src/lib/voting/threshold/interfaces/tally-subscription.d.ts +16 -0
  115. package/src/lib/voting/threshold/interfaces/tally-subscription.d.ts.map +1 -0
  116. package/src/lib/voting/threshold/interfaces/tally-subscription.js +3 -0
  117. package/src/lib/voting/threshold/interfaces/tally-subscription.js.map +1 -0
  118. package/src/lib/voting/threshold/interfaces/tally-verifier.d.ts +12 -0
  119. package/src/lib/voting/threshold/interfaces/tally-verifier.d.ts.map +1 -0
  120. package/src/lib/voting/threshold/interfaces/tally-verifier.js +3 -0
  121. package/src/lib/voting/threshold/interfaces/tally-verifier.js.map +1 -0
  122. package/src/lib/voting/threshold/interfaces/threshold-aggregator.d.ts +15 -0
  123. package/src/lib/voting/threshold/interfaces/threshold-aggregator.d.ts.map +1 -0
  124. package/src/lib/voting/threshold/interfaces/threshold-aggregator.js +3 -0
  125. package/src/lib/voting/threshold/interfaces/threshold-aggregator.js.map +1 -0
  126. package/src/lib/voting/threshold/interfaces/threshold-audit-entry.d.ts +28 -0
  127. package/src/lib/voting/threshold/interfaces/threshold-audit-entry.d.ts.map +1 -0
  128. package/src/lib/voting/threshold/interfaces/threshold-audit-entry.js +3 -0
  129. package/src/lib/voting/threshold/interfaces/threshold-audit-entry.js.map +1 -0
  130. package/src/lib/voting/threshold/interfaces/threshold-key-config.d.ts +12 -0
  131. package/src/lib/voting/threshold/interfaces/threshold-key-config.d.ts.map +1 -0
  132. package/src/lib/voting/threshold/interfaces/threshold-key-config.js +3 -0
  133. package/src/lib/voting/threshold/interfaces/threshold-key-config.js.map +1 -0
  134. package/src/lib/voting/threshold/interfaces/threshold-key-generator.d.ts +12 -0
  135. package/src/lib/voting/threshold/interfaces/threshold-key-generator.d.ts.map +1 -0
  136. package/src/lib/voting/threshold/interfaces/threshold-key-generator.js +3 -0
  137. package/src/lib/voting/threshold/interfaces/threshold-key-generator.js.map +1 -0
  138. package/src/lib/voting/threshold/interfaces/threshold-key-pair.d.ts +30 -0
  139. package/src/lib/voting/threshold/interfaces/threshold-key-pair.d.ts.map +1 -0
  140. package/src/lib/voting/threshold/interfaces/threshold-key-pair.js +3 -0
  141. package/src/lib/voting/threshold/interfaces/threshold-key-pair.js.map +1 -0
  142. package/src/lib/voting/threshold/interfaces/threshold-poll-config.d.ts +19 -0
  143. package/src/lib/voting/threshold/interfaces/threshold-poll-config.d.ts.map +1 -0
  144. package/src/lib/voting/threshold/interfaces/threshold-poll-config.js +3 -0
  145. package/src/lib/voting/threshold/interfaces/threshold-poll-config.js.map +1 -0
  146. package/src/lib/voting/threshold/interfaces/threshold-poll-factory.d.ts +16 -0
  147. package/src/lib/voting/threshold/interfaces/threshold-poll-factory.d.ts.map +1 -0
  148. package/src/lib/voting/threshold/interfaces/threshold-poll-factory.js +3 -0
  149. package/src/lib/voting/threshold/interfaces/threshold-poll-factory.js.map +1 -0
  150. package/src/lib/voting/threshold/interfaces/threshold-poll.d.ts +25 -0
  151. package/src/lib/voting/threshold/interfaces/threshold-poll.d.ts.map +1 -0
  152. package/src/lib/voting/threshold/interfaces/threshold-poll.js +3 -0
  153. package/src/lib/voting/threshold/interfaces/threshold-poll.js.map +1 -0
  154. package/src/lib/voting/threshold/interfaces/verification-result.d.ts +17 -0
  155. package/src/lib/voting/threshold/interfaces/verification-result.d.ts.map +1 -0
  156. package/src/lib/voting/threshold/interfaces/verification-result.js +3 -0
  157. package/src/lib/voting/threshold/interfaces/verification-result.js.map +1 -0
  158. package/src/lib/voting/threshold/interfaces/zk-proof.d.ts +15 -0
  159. package/src/lib/voting/threshold/interfaces/zk-proof.d.ts.map +1 -0
  160. package/src/lib/voting/threshold/interfaces/zk-proof.js +3 -0
  161. package/src/lib/voting/threshold/interfaces/zk-proof.js.map +1 -0
  162. package/src/lib/voting/threshold/partial-decryption-service.d.ts +130 -0
  163. package/src/lib/voting/threshold/partial-decryption-service.d.ts.map +1 -0
  164. package/src/lib/voting/threshold/partial-decryption-service.js +288 -0
  165. package/src/lib/voting/threshold/partial-decryption-service.js.map +1 -0
  166. package/src/lib/voting/threshold/threshold-key-generator.d.ts +114 -0
  167. package/src/lib/voting/threshold/threshold-key-generator.d.ts.map +1 -0
  168. package/src/lib/voting/threshold/threshold-key-generator.js +275 -0
  169. package/src/lib/voting/threshold/threshold-key-generator.js.map +1 -0
@@ -0,0 +1,275 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ThresholdKeyGenerator = exports.KeyGenerationFailedError = exports.InvalidThresholdConfigError = void 0;
4
+ /**
5
+ * Error thrown when threshold configuration is invalid.
6
+ */
7
+ class InvalidThresholdConfigError extends Error {
8
+ constructor(message) {
9
+ super(message);
10
+ this.name = 'InvalidThresholdConfigError';
11
+ }
12
+ }
13
+ exports.InvalidThresholdConfigError = InvalidThresholdConfigError;
14
+ /**
15
+ * Error thrown when key generation fails.
16
+ */
17
+ class KeyGenerationFailedError extends Error {
18
+ constructor(message) {
19
+ super(message);
20
+ this.name = 'KeyGenerationFailedError';
21
+ }
22
+ }
23
+ exports.KeyGenerationFailedError = KeyGenerationFailedError;
24
+ /**
25
+ * Default key bit length for Paillier keys.
26
+ */
27
+ const DEFAULT_KEY_BIT_LENGTH = 2048;
28
+ /**
29
+ * Generates threshold Paillier keys with n shares and threshold k.
30
+ *
31
+ * Uses Shamir's Secret Sharing to split the private key into n shares,
32
+ * where any k shares can reconstruct decryption capability.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const generator = new ThresholdKeyGenerator();
37
+ * const keyPair = await generator.generate({
38
+ * totalShares: 9,
39
+ * threshold: 5,
40
+ * keyBitLength: 2048
41
+ * });
42
+ *
43
+ * // Distribute keyPair.keyShares to Guardians
44
+ * // Use keyPair.publicKey for encryption
45
+ * ```
46
+ */
47
+ class ThresholdKeyGenerator {
48
+ /**
49
+ * Validate a threshold configuration.
50
+ *
51
+ * @param config - The threshold configuration to validate
52
+ * @throws InvalidThresholdConfigError if configuration is invalid
53
+ */
54
+ validateConfig(config) {
55
+ const { totalShares: n, threshold: k } = config;
56
+ if (!Number.isInteger(n) || n < 2) {
57
+ throw new InvalidThresholdConfigError(`Total shares (n) must be an integer >= 2, got ${n}`);
58
+ }
59
+ if (!Number.isInteger(k) || k < 2) {
60
+ throw new InvalidThresholdConfigError(`Threshold (k) must be an integer >= 2, got ${k}`);
61
+ }
62
+ if (k > n) {
63
+ throw new InvalidThresholdConfigError(`Threshold (k=${k}) cannot exceed total shares (n=${n})`);
64
+ }
65
+ if (config.keyBitLength !== undefined) {
66
+ if (!Number.isInteger(config.keyBitLength) || config.keyBitLength < 512) {
67
+ throw new InvalidThresholdConfigError(`Key bit length must be an integer >= 512, got ${config.keyBitLength}`);
68
+ }
69
+ }
70
+ }
71
+ /**
72
+ * Generate a new threshold key pair.
73
+ *
74
+ * @param config - The threshold configuration
75
+ * @returns A promise that resolves to the threshold key pair
76
+ * @throws InvalidThresholdConfigError if configuration is invalid
77
+ * @throws KeyGenerationFailedError if key generation fails
78
+ */
79
+ async generate(config) {
80
+ this.validateConfig(config);
81
+ const { totalShares: n, threshold: k } = config;
82
+ const keyBitLength = config.keyBitLength ?? DEFAULT_KEY_BIT_LENGTH;
83
+ try {
84
+ // Dynamically import paillier-bigint to support both browser and Node.js
85
+ const paillier = await import('paillier-bigint');
86
+ // Generate the base Paillier key pair
87
+ const baseKeyPair = await paillier.generateRandomKeys(keyBitLength);
88
+ const publicKey = baseKeyPair.publicKey;
89
+ // Get the private key components
90
+ // In Paillier, the private key contains lambda and mu
91
+ // For threshold, we need to split lambda using Shamir's Secret Sharing
92
+ const lambda = baseKeyPair.privateKey.lambda;
93
+ const n2 = publicKey.n * publicKey.n;
94
+ // The Shamir modulus must be a multiple of the group order of Z*_{n²}.
95
+ // The order of Z*_{n²} divides n·λ, so we split modulo n·λ.
96
+ // This ensures that Lagrange reconstruction in the exponent is correct:
97
+ // Σ λ_i'·s_i ≡ Δ·λ (mod n·λ) implies c^(Σ ...) ≡ c^(Δ·λ) (mod n²).
98
+ const shamirModulus = publicKey.n * lambda;
99
+ // Generate key shares using Shamir's Secret Sharing
100
+ const { shares, coefficients } = this.shamirSplit(lambda, k, n, shamirModulus);
101
+ // Generate verification keys for each share
102
+ // The verification key allows ZK proof verification without revealing the share
103
+ const verificationKeys = await this.generateVerificationKeys(shares, publicKey, coefficients[0]);
104
+ // Create KeyShare objects
105
+ const keyShares = shares.map((share, i) => ({
106
+ index: i + 1, // 1-indexed
107
+ share: share,
108
+ verificationKey: verificationKeys[i],
109
+ }));
110
+ // Compute the threshold decryption constant theta.
111
+ // θ = L(g^(4·Δ·λ) mod n²) mod n, where Δ = n! (totalShares factorial).
112
+ // The combiner uses θ⁻¹ to convert combined partial decryptions
113
+ // into the final plaintext. This value is public and safe to share.
114
+ const delta = this.factorial(BigInt(n));
115
+ const g = publicKey.g;
116
+ const thetaBase = this.modPow(g, 4n * delta * lambda, n2);
117
+ const theta = this.mod((thetaBase - 1n) / publicKey.n, publicKey.n);
118
+ return {
119
+ publicKey,
120
+ verificationKeys,
121
+ keyShares,
122
+ config: {
123
+ totalShares: n,
124
+ threshold: k,
125
+ keyBitLength,
126
+ },
127
+ theta,
128
+ };
129
+ }
130
+ catch (error) {
131
+ if (error instanceof InvalidThresholdConfigError ||
132
+ error instanceof KeyGenerationFailedError) {
133
+ throw error;
134
+ }
135
+ throw new KeyGenerationFailedError(`Failed to generate threshold keys: ${error instanceof Error ? error.message : String(error)}`);
136
+ }
137
+ }
138
+ /**
139
+ * Split a secret using Shamir's Secret Sharing.
140
+ *
141
+ * Creates a random polynomial of degree k-1 where the constant term is the secret.
142
+ * Evaluates the polynomial at points 1, 2, ..., n to generate n shares.
143
+ *
144
+ * @param secret - The secret to split
145
+ * @param k - The threshold (minimum shares needed to reconstruct)
146
+ * @param n - The total number of shares
147
+ * @param modulus - The modulus for arithmetic operations
148
+ * @returns The shares and polynomial coefficients
149
+ */
150
+ shamirSplit(secret, k, n, modulus) {
151
+ // Generate random coefficients for polynomial of degree k-1
152
+ // f(x) = secret + a1*x + a2*x^2 + ... + a(k-1)*x^(k-1)
153
+ const coefficients = [secret];
154
+ for (let i = 1; i < k; i++) {
155
+ coefficients.push(this.randomBigInt(modulus));
156
+ }
157
+ // Evaluate polynomial at points 1, 2, ..., n
158
+ const shares = [];
159
+ for (let i = 1; i <= n; i++) {
160
+ const x = BigInt(i);
161
+ let y = 0n;
162
+ let xPower = 1n;
163
+ for (const coeff of coefficients) {
164
+ y = this.mod(y + this.mod(coeff * xPower, modulus), modulus);
165
+ xPower = this.mod(xPower * x, modulus);
166
+ }
167
+ shares.push(y);
168
+ }
169
+ return { shares, coefficients };
170
+ }
171
+ /**
172
+ * Generate verification keys for each share.
173
+ *
174
+ * The verification key is used to verify ZK proofs of correct partial decryption.
175
+ * It's computed as g^share mod n^2 where g is the Paillier generator.
176
+ *
177
+ * @param shares - The key shares
178
+ * @param publicKey - The Paillier public key
179
+ * @param _secret - The original secret (for verification)
180
+ * @returns Array of verification keys as Uint8Array
181
+ */
182
+ async generateVerificationKeys(shares, publicKey, _secret) {
183
+ const n2 = publicKey.n * publicKey.n;
184
+ const g = publicKey.g;
185
+ const verificationKeys = [];
186
+ for (const share of shares) {
187
+ // Verification key: v_i = g^(share_i) mod n^2
188
+ const vk = this.modPow(g, share, n2);
189
+ verificationKeys.push(this.bigintToUint8Array(vk));
190
+ }
191
+ return verificationKeys;
192
+ }
193
+ /**
194
+ * Generate a cryptographically secure random bigint less than max.
195
+ *
196
+ * @param max - The upper bound (exclusive)
197
+ * @returns A random bigint in [0, max)
198
+ */
199
+ randomBigInt(max) {
200
+ const byteLength = Math.ceil(max.toString(2).length / 8) + 8;
201
+ const randomBytes = new Uint8Array(byteLength);
202
+ crypto.getRandomValues(randomBytes);
203
+ let result = 0n;
204
+ for (const byte of randomBytes) {
205
+ result = (result << 8n) | BigInt(byte);
206
+ }
207
+ return this.mod(result, max);
208
+ }
209
+ /**
210
+ * Compute a mod m, handling negative numbers correctly.
211
+ *
212
+ * @param a - The dividend
213
+ * @param m - The modulus
214
+ * @returns a mod m (always non-negative)
215
+ */
216
+ mod(a, m) {
217
+ const result = a % m;
218
+ return result >= 0n ? result : result + m;
219
+ }
220
+ /**
221
+ * Compute base^exp mod m using square-and-multiply.
222
+ *
223
+ * @param base - The base
224
+ * @param exp - The exponent
225
+ * @param m - The modulus
226
+ * @returns base^exp mod m
227
+ */
228
+ modPow(base, exp, m) {
229
+ if (m === 1n)
230
+ return 0n;
231
+ let result = 1n;
232
+ base = this.mod(base, m);
233
+ while (exp > 0n) {
234
+ if (exp % 2n === 1n) {
235
+ result = this.mod(result * base, m);
236
+ }
237
+ exp = exp >> 1n;
238
+ base = this.mod(base * base, m);
239
+ }
240
+ return result;
241
+ }
242
+ /**
243
+ * Convert a bigint to Uint8Array.
244
+ *
245
+ * @param value - The bigint to convert
246
+ * @returns The Uint8Array representation
247
+ */
248
+ bigintToUint8Array(value) {
249
+ if (value === 0n) {
250
+ return new Uint8Array([0]);
251
+ }
252
+ const hex = value.toString(16);
253
+ const paddedHex = hex.length % 2 === 0 ? hex : '0' + hex;
254
+ const bytes = new Uint8Array(paddedHex.length / 2);
255
+ for (let i = 0; i < bytes.length; i++) {
256
+ bytes[i] = parseInt(paddedHex.slice(i * 2, i * 2 + 2), 16);
257
+ }
258
+ return bytes;
259
+ }
260
+ /**
261
+ * Compute n! (factorial).
262
+ *
263
+ * @param n - The number to compute factorial of
264
+ * @returns n!
265
+ */
266
+ factorial(n) {
267
+ let result = 1n;
268
+ for (let i = 2n; i <= n; i++) {
269
+ result *= i;
270
+ }
271
+ return result;
272
+ }
273
+ }
274
+ exports.ThresholdKeyGenerator = ThresholdKeyGenerator;
275
+ //# sourceMappingURL=threshold-key-generator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"threshold-key-generator.js","sourceRoot":"","sources":["../../../../../../../packages/digitaldefiance-ecies-lib/src/lib/voting/threshold/threshold-key-generator.ts"],"names":[],"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