threshold-elgamal 0.1.12 → 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/.editorconfig +0 -10
- package/.gitattributes +0 -1
- package/.nvmrc +0 -1
- package/.prettierignore +0 -3
- package/.vscode/extensions.json +0 -7
- package/.vscode/settings.json +0 -50
- package/docs/_config.yml +0 -1
- package/docs/index.md +0 -303
- package/docs/modules.md +0 -381
- package/eslint.config.js +0 -141
- package/src/constants.ts +0 -44
- package/src/elgamal.test.ts +0 -42
- package/src/elgamal.ts +0 -85
- package/src/index.ts +0 -19
- package/src/testUtils.ts +0 -177
- package/src/thresholdElgamal.test.ts +0 -289
- package/src/thresholdElgamal.ts +0 -157
- package/src/types/random-bigint.d.ts +0 -13
- package/src/types.ts +0 -21
- package/src/utils.ts +0 -61
- package/tsconfig.build.json +0 -17
- package/tsconfig.json +0 -29
package/package.json
CHANGED
package/.editorconfig
DELETED
package/.gitattributes
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
* -text
|
package/.nvmrc
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
20.11.0
|
package/.prettierignore
DELETED
package/.vscode/extensions.json
DELETED
package/.vscode/settings.json
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"editor.formatOnSave": true,
|
|
3
|
-
"editor.linkedEditing": true,
|
|
4
|
-
"files.autoSave": "off",
|
|
5
|
-
"javascript.updateImportsOnFileMove.enabled": "prompt",
|
|
6
|
-
"typescript.tsdk": "node_modules/typescript/lib",
|
|
7
|
-
"editor.bracketPairColorization.enabled": true,
|
|
8
|
-
"editor.guides.bracketPairs": true,
|
|
9
|
-
|
|
10
|
-
"css.validate": false,
|
|
11
|
-
"scss.validate": false,
|
|
12
|
-
"javascript.validate.enable": false,
|
|
13
|
-
|
|
14
|
-
"eslint.format.enable": true,
|
|
15
|
-
"eslint.experimental.useFlatConfig": true,
|
|
16
|
-
"[javascript]": {
|
|
17
|
-
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
|
|
18
|
-
},
|
|
19
|
-
"[typescript]": {
|
|
20
|
-
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
|
|
21
|
-
},
|
|
22
|
-
"[json]": {
|
|
23
|
-
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
|
24
|
-
},
|
|
25
|
-
"prettier.htmlWhitespaceSensitivity": "ignore",
|
|
26
|
-
"eslint.validate": ["javascript", "typescript"],
|
|
27
|
-
"editor.codeActionsOnSave": {
|
|
28
|
-
"source.fixAll.eslint": "never"
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
"workbench.iconTheme": "vscode-icons",
|
|
32
|
-
"cSpell.ignorePaths": ["node_modules", ".git"],
|
|
33
|
-
"cSpell.words": [
|
|
34
|
-
"Bleichenbacher's",
|
|
35
|
-
"ciphertext",
|
|
36
|
-
"dbaeumer",
|
|
37
|
-
"docdash",
|
|
38
|
-
"esbenp",
|
|
39
|
-
"Homomorphically",
|
|
40
|
-
"isqrt",
|
|
41
|
-
"jsbn",
|
|
42
|
-
"Khadir's",
|
|
43
|
-
"Longname",
|
|
44
|
-
"pretypedoc",
|
|
45
|
-
"PRIMALITY",
|
|
46
|
-
"Shamir's",
|
|
47
|
-
"shamirs",
|
|
48
|
-
"typedefs"
|
|
49
|
-
]
|
|
50
|
-
}
|
package/docs/_config.yml
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
theme: jekyll-theme-midnight
|
package/docs/index.md
DELETED
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
threshold-elgamal / [Exports](modules.md)
|
|
2
|
-
|
|
3
|
-
# Threshold ElGamal
|
|
4
|
-
|
|
5
|
-
This project is a collection of functions implementing the ElGamal encryption algorithm in TypeScript on top of native JavaScript BigInteger. Its core includes ElGamal functions for key generation, encryption, and decryption. It is extended with support for threshold encryption.
|
|
6
|
-
|
|
7
|
-
**WIP: Early version. Thresholds when set below the number of scheme participants don't behave as expected.**
|
|
8
|
-
However, it works correctly with `threshold == participantsCount`, which is its main use case for myself for now.
|
|
9
|
-
|
|
10
|
-
It was written as clearly as possible, modularized, and with long, explicit variable names. It includes out-of-the-box VS Code configuration, including recommended extensions for working with the library and/or contributing.
|
|
11
|
-
|
|
12
|
-
**This is not a cryptographically audited library, make sure you know what you are doing before using it.**
|
|
13
|
-
|
|
14
|
-
## Documentation
|
|
15
|
-
|
|
16
|
-
For a detailed list of exported types and functions, [click here](https://tenemo.github.io/threshold-elgamal/modules.html).
|
|
17
|
-
|
|
18
|
-
## Contributing
|
|
19
|
-
|
|
20
|
-
The JavaScript/TypeScript ecosystem seems to be lacking in modern, functional ElGamal libraries that work out of the box with reasonable default (this library isn't at that point yet). All PRs are welcome.
|
|
21
|
-
|
|
22
|
-
## Libraries/tools used
|
|
23
|
-
|
|
24
|
-
- TypeScript
|
|
25
|
-
- Vitest
|
|
26
|
-
- ESLint + Prettier
|
|
27
|
-
- Typedoc
|
|
28
|
-
|
|
29
|
-
## Production dependencies
|
|
30
|
-
|
|
31
|
-
- [bigint-mod-arith](https://www.npmjs.com/package/bigint-mod-arith)
|
|
32
|
-
- [random-bigint](https://www.npmjs.com/package/random-bigint)
|
|
33
|
-
|
|
34
|
-
It has no other production dependencies apart from these two. They could be inlined easily, if needed.
|
|
35
|
-
|
|
36
|
-
## TODO
|
|
37
|
-
|
|
38
|
-
- Hashing messages
|
|
39
|
-
- Support for additive property of exponents, not just native ElGamal multiplication
|
|
40
|
-
- consider using {} function params for better readability and consistency in param naming
|
|
41
|
-
|
|
42
|
-
## Installation
|
|
43
|
-
|
|
44
|
-
To use it in your project, install it first:
|
|
45
|
-
|
|
46
|
-
`npm install threshold-elgamal`
|
|
47
|
-
|
|
48
|
-
## Examples
|
|
49
|
-
|
|
50
|
-
First, import the whatever functions you need from the library:
|
|
51
|
-
|
|
52
|
-
```typescript
|
|
53
|
-
import {
|
|
54
|
-
generateParameters,
|
|
55
|
-
encrypt,
|
|
56
|
-
decrypt,
|
|
57
|
-
generateKeyShares,
|
|
58
|
-
combinePublicKeys,
|
|
59
|
-
thresholdDecrypt,
|
|
60
|
-
} from "threshold-elgamal";
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
### Generating Keys
|
|
64
|
-
|
|
65
|
-
Generate a public/private key pair:
|
|
66
|
-
|
|
67
|
-
```typescript
|
|
68
|
-
const { publicKey, privateKey, prime, generator } = generateParameters();
|
|
69
|
-
console.log(publicKey, privateKey, prime, generator); // ffdhe2048 group by default
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Encrypting a Message
|
|
73
|
-
|
|
74
|
-
Encrypt a message using the public key:
|
|
75
|
-
|
|
76
|
-
```typescript
|
|
77
|
-
const secret = 42;
|
|
78
|
-
const encryptedMessage = encrypt(secret, prime, generator, publicKey);
|
|
79
|
-
console.log(encryptedMessage);
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
### Decrypting a Message
|
|
83
|
-
|
|
84
|
-
Decrypt a message using the private key:
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
const decryptedMessage = decrypt(encryptedMessage, prime, privateKey);
|
|
88
|
-
console.log(decryptedMessage); // 42
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### Single secret for shared with 3 participants
|
|
92
|
-
|
|
93
|
-
Threshold scheme for generating a common public key, sharing a secret to 3 participants using that key and requiring all three participants to decrypt it.
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
import {
|
|
97
|
-
getGroup,
|
|
98
|
-
encrypt,
|
|
99
|
-
generateSingleKeyShare,
|
|
100
|
-
combinePublicKeys,
|
|
101
|
-
createDecryptionShare,
|
|
102
|
-
combineDecryptionShares,
|
|
103
|
-
thresholdDecrypt,
|
|
104
|
-
multiplyEncryptedValues,
|
|
105
|
-
generateParameters,
|
|
106
|
-
} from "threshold-elgamal";
|
|
107
|
-
|
|
108
|
-
const primeBits = 2048; // Bit length of the prime modulus
|
|
109
|
-
const threshold = 3; // A scenario for 3 participants with a threshold of 3
|
|
110
|
-
const { prime, generator } = getGroup(2048);
|
|
111
|
-
|
|
112
|
-
// Each participant generates their public key share and private key individually
|
|
113
|
-
const participant1KeyShare: PartyKeyPair = generateSingleKeyShare(
|
|
114
|
-
1,
|
|
115
|
-
threshold,
|
|
116
|
-
primeBits,
|
|
117
|
-
);
|
|
118
|
-
const participant2KeyShare: PartyKeyPair = generateSingleKeyShare(
|
|
119
|
-
2,
|
|
120
|
-
threshold,
|
|
121
|
-
primeBits,
|
|
122
|
-
);
|
|
123
|
-
const participant3KeyShare: PartyKeyPair = generateSingleKeyShare(
|
|
124
|
-
3,
|
|
125
|
-
threshold,
|
|
126
|
-
primeBits,
|
|
127
|
-
);
|
|
128
|
-
|
|
129
|
-
// Combine the public keys to form a single public key
|
|
130
|
-
const combinedPublicKey = combinePublicKeys(
|
|
131
|
-
[
|
|
132
|
-
participant1KeyShare.partyPublicKey,
|
|
133
|
-
participant2KeyShare.partyPublicKey,
|
|
134
|
-
participant3KeyShare.partyPublicKey,
|
|
135
|
-
],
|
|
136
|
-
prime,
|
|
137
|
-
);
|
|
138
|
-
|
|
139
|
-
// Encrypt a message using the combined public key
|
|
140
|
-
const secret = 42;
|
|
141
|
-
const encryptedMessage = encrypt(secret, prime, generator, combinedPublicKey);
|
|
142
|
-
|
|
143
|
-
// Decryption shares
|
|
144
|
-
const decryptionShares = [
|
|
145
|
-
createDecryptionShare(
|
|
146
|
-
encryptedMessage,
|
|
147
|
-
participant1KeyShare.partyPrivateKey,
|
|
148
|
-
prime,
|
|
149
|
-
),
|
|
150
|
-
createDecryptionShare(
|
|
151
|
-
encryptedMessage,
|
|
152
|
-
participant2KeyShare.partyPrivateKey,
|
|
153
|
-
prime,
|
|
154
|
-
),
|
|
155
|
-
createDecryptionShare(
|
|
156
|
-
encryptedMessage,
|
|
157
|
-
participant3KeyShare.partyPrivateKey,
|
|
158
|
-
prime,
|
|
159
|
-
),
|
|
160
|
-
];
|
|
161
|
-
// Combining the decryption shares into one, used to decrypt the message
|
|
162
|
-
const combinedDecryptionShares = combineDecryptionShares(
|
|
163
|
-
decryptionShares,
|
|
164
|
-
prime,
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
// Decrypting the message using the combined decryption shares
|
|
168
|
-
const thresholdDecryptedMessage = thresholdDecrypt(
|
|
169
|
-
encryptedMessage,
|
|
170
|
-
combinedDecryptionShares,
|
|
171
|
-
prime,
|
|
172
|
-
);
|
|
173
|
-
console.log(thresholdDecryptedMessage); // 42
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
### Voting and Multiplication with Threshold Scheme for 3 Participants
|
|
177
|
-
|
|
178
|
-
This example demonstrates a 1 to 10 voting scenario where 3 participants cast encrypted votes on two options. The encrypted votes are aggregated, multiplied with each other and then require all three participants to decrypt the final tally. The decryption does not work on individual votes, meaning that it is impossible to decrypt their votes even after decrypting the result.
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
import {
|
|
182
|
-
encrypt,
|
|
183
|
-
generateSingleKeyShare,
|
|
184
|
-
combinePublicKeys,
|
|
185
|
-
createDecryptionShare,
|
|
186
|
-
combineDecryptionShares,
|
|
187
|
-
thresholdDecrypt,
|
|
188
|
-
multiplyEncryptedValues,
|
|
189
|
-
generateParameters,
|
|
190
|
-
} from "threshold-elgamal";
|
|
191
|
-
|
|
192
|
-
const primeBits = 2048; // Bit length of the prime modulus
|
|
193
|
-
const threshold = 3; // A scenario for 3 participants with a threshold of 3
|
|
194
|
-
const { prime, generator } = getGroup(2048);
|
|
195
|
-
|
|
196
|
-
// Each participant generates their public key share and private key individually
|
|
197
|
-
const participant1KeyShare = generateSingleKeyShare(1, threshold, primeBits);
|
|
198
|
-
const participant2KeyShare = generateSingleKeyShare(2, threshold, primeBits);
|
|
199
|
-
const participant3KeyShare = generateSingleKeyShare(3, threshold, primeBits);
|
|
200
|
-
|
|
201
|
-
// Combine the public keys to form a single public key
|
|
202
|
-
const combinedPublicKey = combinePublicKeys(
|
|
203
|
-
[
|
|
204
|
-
participant1KeyShare.partyPublicKey,
|
|
205
|
-
participant2KeyShare.partyPublicKey,
|
|
206
|
-
participant3KeyShare.partyPublicKey,
|
|
207
|
-
],
|
|
208
|
-
prime,
|
|
209
|
-
);
|
|
210
|
-
|
|
211
|
-
// Participants cast their encrypted votes for two options
|
|
212
|
-
const voteOption1 = [6, 7, 1]; // Votes for option 1 by participants 1, 2, and 3
|
|
213
|
-
const voteOption2 = [10, 7, 4]; // Votes for option 2 by participants 1, 2, and 3
|
|
214
|
-
|
|
215
|
-
// Encrypt votes for both options
|
|
216
|
-
const encryptedVotesOption1 = voteOption1.map((vote) =>
|
|
217
|
-
encrypt(vote, prime, generator, combinedPublicKey),
|
|
218
|
-
);
|
|
219
|
-
const encryptedVotesOption2 = voteOption2.map((vote) =>
|
|
220
|
-
encrypt(vote, prime, generator, combinedPublicKey),
|
|
221
|
-
);
|
|
222
|
-
|
|
223
|
-
// Multiply encrypted votes together to aggregate
|
|
224
|
-
const aggregatedEncryptedVoteOption1 = encryptedVotesOption1.reduce(
|
|
225
|
-
(acc, current) => multiplyEncryptedValues(acc, current, prime),
|
|
226
|
-
{ c1: 1n, c2: 1n },
|
|
227
|
-
);
|
|
228
|
-
const aggregatedEncryptedVoteOption2 = encryptedVotesOption2.reduce(
|
|
229
|
-
(acc, current) => multiplyEncryptedValues(acc, current, prime),
|
|
230
|
-
{ c1: 1n, c2: 1n },
|
|
231
|
-
);
|
|
232
|
-
|
|
233
|
-
// Each participant creates a decryption share for both options.
|
|
234
|
-
// Notice that the shares are created for the aggregated, multiplied tally specifically,
|
|
235
|
-
// not the individual votes. This means that they can be used ONLY for decrypting the aggregated votes.
|
|
236
|
-
const decryptionSharesOption1 = [
|
|
237
|
-
createDecryptionShare(
|
|
238
|
-
aggregatedEncryptedVoteOption1,
|
|
239
|
-
participant1KeyShare.partyPrivateKey,
|
|
240
|
-
prime,
|
|
241
|
-
),
|
|
242
|
-
createDecryptionShare(
|
|
243
|
-
aggregatedEncryptedVoteOption1,
|
|
244
|
-
participant2KeyShare.partyPrivateKey,
|
|
245
|
-
prime,
|
|
246
|
-
),
|
|
247
|
-
createDecryptionShare(
|
|
248
|
-
aggregatedEncryptedVoteOption1,
|
|
249
|
-
participant3KeyShare.partyPrivateKey,
|
|
250
|
-
prime,
|
|
251
|
-
),
|
|
252
|
-
];
|
|
253
|
-
const decryptionSharesOption2 = [
|
|
254
|
-
createDecryptionShare(
|
|
255
|
-
aggregatedEncryptedVoteOption2,
|
|
256
|
-
participant1KeyShare.partyPrivateKey,
|
|
257
|
-
prime,
|
|
258
|
-
),
|
|
259
|
-
createDecryptionShare(
|
|
260
|
-
aggregatedEncryptedVoteOption2,
|
|
261
|
-
participant2KeyShare.partyPrivateKey,
|
|
262
|
-
prime,
|
|
263
|
-
),
|
|
264
|
-
createDecryptionShare(
|
|
265
|
-
aggregatedEncryptedVoteOption2,
|
|
266
|
-
participant3KeyShare.partyPrivateKey,
|
|
267
|
-
prime,
|
|
268
|
-
),
|
|
269
|
-
];
|
|
270
|
-
|
|
271
|
-
// Combine decryption shares and decrypt the aggregated votes for both options.
|
|
272
|
-
// Notice that the private keys of the participants never leave their possession.
|
|
273
|
-
// Only the decryption shares are shared with other participants.
|
|
274
|
-
const combinedDecryptionSharesOption1 = combineDecryptionShares(
|
|
275
|
-
decryptionSharesOption1,
|
|
276
|
-
prime,
|
|
277
|
-
);
|
|
278
|
-
const combinedDecryptionSharesOption2 = combineDecryptionShares(
|
|
279
|
-
decryptionSharesOption2,
|
|
280
|
-
prime,
|
|
281
|
-
);
|
|
282
|
-
|
|
283
|
-
const finalTallyOption1 = thresholdDecrypt(
|
|
284
|
-
aggregatedEncryptedVoteOption1,
|
|
285
|
-
combinedDecryptionSharesOption1,
|
|
286
|
-
prime,
|
|
287
|
-
);
|
|
288
|
-
const finalTallyOption2 = thresholdDecrypt(
|
|
289
|
-
aggregatedEncryptedVoteOption2,
|
|
290
|
-
combinedDecryptionSharesOption2,
|
|
291
|
-
prime,
|
|
292
|
-
);
|
|
293
|
-
|
|
294
|
-
console.log(
|
|
295
|
-
`Final tally for Option 1: ${finalTallyOption1}, Option 2: ${finalTallyOption2}`,
|
|
296
|
-
); // 42, 280
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
This example can be extended with calculating a geometric mean for the candidate options to better present the results.
|
|
300
|
-
|
|
301
|
-
## License
|
|
302
|
-
|
|
303
|
-
This project is licensed under the MIT License - see the LICENSE file for details.
|