threshold-elgamal 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +10 -0
- package/.gitattributes +1 -0
- package/.nvmrc +1 -0
- package/.prettierignore +3 -0
- package/.vscode/extensions.json +7 -0
- package/.vscode/settings.json +50 -0
- package/LICENSE +7 -0
- package/README.md +287 -0
- package/docs/.nojekyll +1 -0
- package/docs/README.md +289 -0
- package/docs/modules.md +381 -0
- package/eslint.config.js +161 -0
- package/package.json +82 -0
- package/src/constants.ts +44 -0
- package/src/elgamal.test.ts +42 -0
- package/src/elgamal.ts +85 -0
- package/src/index.ts +19 -0
- package/src/testUtils.ts +177 -0
- package/src/thresholdElgamal.test.ts +289 -0
- package/src/thresholdElgamal.ts +157 -0
- package/src/types/random-bigint.d.ts +13 -0
- package/src/types.ts +21 -0
- package/src/utils.ts +61 -0
- package/tsconfig.build.json +17 -0
- package/tsconfig.json +29 -0
package/.editorconfig
ADDED
package/.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* -text
|
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
20.11.0
|
package/.prettierignore
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
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/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2024 Piotr Piech piotr@piech.dev
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
# Threshold ElGamal
|
|
2
|
+
|
|
3
|
+
This project is a collection of functions implementing the ElGamal encryption algorithm in TypeScript. Its core includes ElGamal functions for key generation, encryption, and decryption. It is extended with support for threshold encryption.
|
|
4
|
+
|
|
5
|
+
**WIP: Early version. Thresholds when set below the number of scheme participants don't behave as expected.**
|
|
6
|
+
However, it works correctly with `threshold == participantsCount`, which is its main use case for myself for now.
|
|
7
|
+
|
|
8
|
+
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.
|
|
9
|
+
|
|
10
|
+
## Contributing
|
|
11
|
+
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
## TODO
|
|
15
|
+
|
|
16
|
+
- Hashing messages
|
|
17
|
+
- Support for additive property of exponents, not just native ElGamal multiplication
|
|
18
|
+
- consider using {} function params for better readability and consistency in param naming
|
|
19
|
+
|
|
20
|
+
## Setup
|
|
21
|
+
|
|
22
|
+
Ensure you have Node.js installed on your system and then install the required dependencies by running:
|
|
23
|
+
|
|
24
|
+
`npm install`
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
To use it in your project, install it first:
|
|
29
|
+
|
|
30
|
+
`npm install threshold-elgamal`
|
|
31
|
+
|
|
32
|
+
## Examples
|
|
33
|
+
|
|
34
|
+
First, import the whatever functions you need from the library:
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import {
|
|
38
|
+
generateParameters,
|
|
39
|
+
encrypt,
|
|
40
|
+
decrypt,
|
|
41
|
+
generateKeyShares,
|
|
42
|
+
combinePublicKeys,
|
|
43
|
+
thresholdDecrypt,
|
|
44
|
+
} from "threshold-elgamal";
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Generating Keys
|
|
48
|
+
|
|
49
|
+
Generate a public/private key pair:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const { publicKey, privateKey, prime, generator } = generateParameters();
|
|
53
|
+
console.log(publicKey, privateKey, prime, generator); // ffdhe2048 group by default
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Encrypting a Message
|
|
57
|
+
|
|
58
|
+
Encrypt a message using the public key:
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
const secret = 42;
|
|
62
|
+
const encryptedMessage = encrypt(secret, prime, generator, publicKey);
|
|
63
|
+
console.log(encryptedMessage);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Decrypting a Message
|
|
67
|
+
|
|
68
|
+
Decrypt a message using the private key:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
const decryptedMessage = decrypt(encryptedMessage, prime, privateKey);
|
|
72
|
+
console.log(decryptedMessage); // 42
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Single secret for shared with 3 participants
|
|
76
|
+
|
|
77
|
+
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.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import {
|
|
81
|
+
getGroup,
|
|
82
|
+
encrypt,
|
|
83
|
+
generateSingleKeyShare,
|
|
84
|
+
combinePublicKeys,
|
|
85
|
+
createDecryptionShare,
|
|
86
|
+
combineDecryptionShares,
|
|
87
|
+
thresholdDecrypt,
|
|
88
|
+
multiplyEncryptedValues,
|
|
89
|
+
generateParameters,
|
|
90
|
+
} from "threshold-elgamal";
|
|
91
|
+
|
|
92
|
+
const primeBits = 2048; // Bit length of the prime modulus
|
|
93
|
+
const threshold = 3; // A scenario for 3 participants with a threshold of 3
|
|
94
|
+
const { prime, generator } = getGroup(2048);
|
|
95
|
+
|
|
96
|
+
// Each participant generates their public key share and private key individually
|
|
97
|
+
const participant1KeyShare: PartyKeyPair = generateSingleKeyShare(
|
|
98
|
+
1,
|
|
99
|
+
threshold,
|
|
100
|
+
primeBits,
|
|
101
|
+
);
|
|
102
|
+
const participant2KeyShare: PartyKeyPair = generateSingleKeyShare(
|
|
103
|
+
2,
|
|
104
|
+
threshold,
|
|
105
|
+
primeBits,
|
|
106
|
+
);
|
|
107
|
+
const participant3KeyShare: PartyKeyPair = generateSingleKeyShare(
|
|
108
|
+
3,
|
|
109
|
+
threshold,
|
|
110
|
+
primeBits,
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
// Combine the public keys to form a single public key
|
|
114
|
+
const combinedPublicKey = combinePublicKeys(
|
|
115
|
+
[
|
|
116
|
+
participant1KeyShare.partyPublicKey,
|
|
117
|
+
participant2KeyShare.partyPublicKey,
|
|
118
|
+
participant3KeyShare.partyPublicKey,
|
|
119
|
+
],
|
|
120
|
+
prime,
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
// Encrypt a message using the combined public key
|
|
124
|
+
const secret = 42;
|
|
125
|
+
const encryptedMessage = encrypt(secret, prime, generator, combinedPublicKey);
|
|
126
|
+
|
|
127
|
+
// Decryption shares
|
|
128
|
+
const decryptionShares = [
|
|
129
|
+
createDecryptionShare(
|
|
130
|
+
encryptedMessage,
|
|
131
|
+
participant1KeyShare.partyPrivateKey,
|
|
132
|
+
prime,
|
|
133
|
+
),
|
|
134
|
+
createDecryptionShare(
|
|
135
|
+
encryptedMessage,
|
|
136
|
+
participant2KeyShare.partyPrivateKey,
|
|
137
|
+
prime,
|
|
138
|
+
),
|
|
139
|
+
createDecryptionShare(
|
|
140
|
+
encryptedMessage,
|
|
141
|
+
participant3KeyShare.partyPrivateKey,
|
|
142
|
+
prime,
|
|
143
|
+
),
|
|
144
|
+
];
|
|
145
|
+
// Combining the decryption shares into one, used to decrypt the message
|
|
146
|
+
const combinedDecryptionShares = combineDecryptionShares(
|
|
147
|
+
decryptionShares,
|
|
148
|
+
prime,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
// Decrypting the message using the combined decryption shares
|
|
152
|
+
const thresholdDecryptedMessage = thresholdDecrypt(
|
|
153
|
+
encryptedMessage,
|
|
154
|
+
combinedDecryptionShares,
|
|
155
|
+
prime,
|
|
156
|
+
);
|
|
157
|
+
console.log(thresholdDecryptedMessage); // 42
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Voting and Multiplication with Threshold Scheme for 3 Participants
|
|
161
|
+
|
|
162
|
+
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.
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
import {
|
|
166
|
+
encrypt,
|
|
167
|
+
generateSingleKeyShare,
|
|
168
|
+
combinePublicKeys,
|
|
169
|
+
createDecryptionShare,
|
|
170
|
+
combineDecryptionShares,
|
|
171
|
+
thresholdDecrypt,
|
|
172
|
+
multiplyEncryptedValues,
|
|
173
|
+
generateParameters,
|
|
174
|
+
} from "threshold-elgamal";
|
|
175
|
+
|
|
176
|
+
const primeBits = 2048; // Bit length of the prime modulus
|
|
177
|
+
const threshold = 3; // A scenario for 3 participants with a threshold of 3
|
|
178
|
+
const { prime, generator } = getGroup(2048);
|
|
179
|
+
|
|
180
|
+
// Each participant generates their public key share and private key individually
|
|
181
|
+
const participant1KeyShare = generateSingleKeyShare(1, threshold, primeBits);
|
|
182
|
+
const participant2KeyShare = generateSingleKeyShare(2, threshold, primeBits);
|
|
183
|
+
const participant3KeyShare = generateSingleKeyShare(3, threshold, primeBits);
|
|
184
|
+
|
|
185
|
+
// Combine the public keys to form a single public key
|
|
186
|
+
const combinedPublicKey = combinePublicKeys(
|
|
187
|
+
[
|
|
188
|
+
participant1KeyShare.partyPublicKey,
|
|
189
|
+
participant2KeyShare.partyPublicKey,
|
|
190
|
+
participant3KeyShare.partyPublicKey,
|
|
191
|
+
],
|
|
192
|
+
prime,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
// Participants cast their encrypted votes for two options
|
|
196
|
+
const voteOption1 = [6, 7, 1]; // Votes for option 1 by participants 1, 2, and 3
|
|
197
|
+
const voteOption2 = [10, 7, 4]; // Votes for option 2 by participants 1, 2, and 3
|
|
198
|
+
|
|
199
|
+
// Encrypt votes for both options
|
|
200
|
+
const encryptedVotesOption1 = voteOption1.map((vote) =>
|
|
201
|
+
encrypt(vote, prime, generator, combinedPublicKey),
|
|
202
|
+
);
|
|
203
|
+
const encryptedVotesOption2 = voteOption2.map((vote) =>
|
|
204
|
+
encrypt(vote, prime, generator, combinedPublicKey),
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
// Multiply encrypted votes together to aggregate
|
|
208
|
+
const aggregatedEncryptedVoteOption1 = encryptedVotesOption1.reduce(
|
|
209
|
+
(acc, current) => multiplyEncryptedValues(acc, current, prime),
|
|
210
|
+
{ c1: 1n, c2: 1n },
|
|
211
|
+
);
|
|
212
|
+
const aggregatedEncryptedVoteOption2 = encryptedVotesOption2.reduce(
|
|
213
|
+
(acc, current) => multiplyEncryptedValues(acc, current, prime),
|
|
214
|
+
{ c1: 1n, c2: 1n },
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
// Each participant creates a decryption share for both options.
|
|
218
|
+
// Notice that the shares are created for the aggregated, multiplied tally specifically,
|
|
219
|
+
// not the individual votes. This means that they can be used ONLY for decrypting the aggregated votes.
|
|
220
|
+
const decryptionSharesOption1 = [
|
|
221
|
+
createDecryptionShare(
|
|
222
|
+
aggregatedEncryptedVoteOption1,
|
|
223
|
+
participant1KeyShare.partyPrivateKey,
|
|
224
|
+
prime,
|
|
225
|
+
),
|
|
226
|
+
createDecryptionShare(
|
|
227
|
+
aggregatedEncryptedVoteOption1,
|
|
228
|
+
participant2KeyShare.partyPrivateKey,
|
|
229
|
+
prime,
|
|
230
|
+
),
|
|
231
|
+
createDecryptionShare(
|
|
232
|
+
aggregatedEncryptedVoteOption1,
|
|
233
|
+
participant3KeyShare.partyPrivateKey,
|
|
234
|
+
prime,
|
|
235
|
+
),
|
|
236
|
+
];
|
|
237
|
+
const decryptionSharesOption2 = [
|
|
238
|
+
createDecryptionShare(
|
|
239
|
+
aggregatedEncryptedVoteOption2,
|
|
240
|
+
participant1KeyShare.partyPrivateKey,
|
|
241
|
+
prime,
|
|
242
|
+
),
|
|
243
|
+
createDecryptionShare(
|
|
244
|
+
aggregatedEncryptedVoteOption2,
|
|
245
|
+
participant2KeyShare.partyPrivateKey,
|
|
246
|
+
prime,
|
|
247
|
+
),
|
|
248
|
+
createDecryptionShare(
|
|
249
|
+
aggregatedEncryptedVoteOption2,
|
|
250
|
+
participant3KeyShare.partyPrivateKey,
|
|
251
|
+
prime,
|
|
252
|
+
),
|
|
253
|
+
];
|
|
254
|
+
|
|
255
|
+
// Combine decryption shares and decrypt the aggregated votes for both options.
|
|
256
|
+
// Notice that the private keys of the participants never leave their possession.
|
|
257
|
+
// Only the decryption shares are shared with other participants.
|
|
258
|
+
const combinedDecryptionSharesOption1 = combineDecryptionShares(
|
|
259
|
+
decryptionSharesOption1,
|
|
260
|
+
prime,
|
|
261
|
+
);
|
|
262
|
+
const combinedDecryptionSharesOption2 = combineDecryptionShares(
|
|
263
|
+
decryptionSharesOption2,
|
|
264
|
+
prime,
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
const finalTallyOption1 = thresholdDecrypt(
|
|
268
|
+
aggregatedEncryptedVoteOption1,
|
|
269
|
+
combinedDecryptionSharesOption1,
|
|
270
|
+
prime,
|
|
271
|
+
);
|
|
272
|
+
const finalTallyOption2 = thresholdDecrypt(
|
|
273
|
+
aggregatedEncryptedVoteOption2,
|
|
274
|
+
combinedDecryptionSharesOption2,
|
|
275
|
+
prime,
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
console.log(
|
|
279
|
+
`Final tally for Option 1: ${finalTallyOption1}, Option 2: ${finalTallyOption2}`,
|
|
280
|
+
); // 42, 280
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
This example can be extended with calculating a geometric mean for the candidate options to better present the results.
|
|
284
|
+
|
|
285
|
+
## License
|
|
286
|
+
|
|
287
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
package/docs/.nojekyll
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false.
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
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. 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
|
+
## Contributing
|
|
13
|
+
|
|
14
|
+
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.
|
|
15
|
+
|
|
16
|
+
## TODO
|
|
17
|
+
|
|
18
|
+
- Hashing messages
|
|
19
|
+
- Support for additive property of exponents, not just native ElGamal multiplication
|
|
20
|
+
- consider using {} function params for better readability and consistency in param naming
|
|
21
|
+
|
|
22
|
+
## Setup
|
|
23
|
+
|
|
24
|
+
Ensure you have Node.js installed on your system and then install the required dependencies by running:
|
|
25
|
+
|
|
26
|
+
`npm install`
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
To use it in your project, install it first:
|
|
31
|
+
|
|
32
|
+
`npm install threshold-elgamal`
|
|
33
|
+
|
|
34
|
+
## Examples
|
|
35
|
+
|
|
36
|
+
First, import the whatever functions you need from the library:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import {
|
|
40
|
+
generateParameters,
|
|
41
|
+
encrypt,
|
|
42
|
+
decrypt,
|
|
43
|
+
generateKeyShares,
|
|
44
|
+
combinePublicKeys,
|
|
45
|
+
thresholdDecrypt,
|
|
46
|
+
} from "threshold-elgamal";
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Generating Keys
|
|
50
|
+
|
|
51
|
+
Generate a public/private key pair:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
const { publicKey, privateKey, prime, generator } = generateParameters();
|
|
55
|
+
console.log(publicKey, privateKey, prime, generator); // ffdhe2048 group by default
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Encrypting a Message
|
|
59
|
+
|
|
60
|
+
Encrypt a message using the public key:
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
const secret = 42;
|
|
64
|
+
const encryptedMessage = encrypt(secret, prime, generator, publicKey);
|
|
65
|
+
console.log(encryptedMessage);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Decrypting a Message
|
|
69
|
+
|
|
70
|
+
Decrypt a message using the private key:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
const decryptedMessage = decrypt(encryptedMessage, prime, privateKey);
|
|
74
|
+
console.log(decryptedMessage); // 42
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Single secret for shared with 3 participants
|
|
78
|
+
|
|
79
|
+
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.
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import {
|
|
83
|
+
getGroup,
|
|
84
|
+
encrypt,
|
|
85
|
+
generateSingleKeyShare,
|
|
86
|
+
combinePublicKeys,
|
|
87
|
+
createDecryptionShare,
|
|
88
|
+
combineDecryptionShares,
|
|
89
|
+
thresholdDecrypt,
|
|
90
|
+
multiplyEncryptedValues,
|
|
91
|
+
generateParameters,
|
|
92
|
+
} from "threshold-elgamal";
|
|
93
|
+
|
|
94
|
+
const primeBits = 2048; // Bit length of the prime modulus
|
|
95
|
+
const threshold = 3; // A scenario for 3 participants with a threshold of 3
|
|
96
|
+
const { prime, generator } = getGroup(2048);
|
|
97
|
+
|
|
98
|
+
// Each participant generates their public key share and private key individually
|
|
99
|
+
const participant1KeyShare: PartyKeyPair = generateSingleKeyShare(
|
|
100
|
+
1,
|
|
101
|
+
threshold,
|
|
102
|
+
primeBits,
|
|
103
|
+
);
|
|
104
|
+
const participant2KeyShare: PartyKeyPair = generateSingleKeyShare(
|
|
105
|
+
2,
|
|
106
|
+
threshold,
|
|
107
|
+
primeBits,
|
|
108
|
+
);
|
|
109
|
+
const participant3KeyShare: PartyKeyPair = generateSingleKeyShare(
|
|
110
|
+
3,
|
|
111
|
+
threshold,
|
|
112
|
+
primeBits,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
// Combine the public keys to form a single public key
|
|
116
|
+
const combinedPublicKey = combinePublicKeys(
|
|
117
|
+
[
|
|
118
|
+
participant1KeyShare.partyPublicKey,
|
|
119
|
+
participant2KeyShare.partyPublicKey,
|
|
120
|
+
participant3KeyShare.partyPublicKey,
|
|
121
|
+
],
|
|
122
|
+
prime,
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
// Encrypt a message using the combined public key
|
|
126
|
+
const secret = 42;
|
|
127
|
+
const encryptedMessage = encrypt(secret, prime, generator, combinedPublicKey);
|
|
128
|
+
|
|
129
|
+
// Decryption shares
|
|
130
|
+
const decryptionShares = [
|
|
131
|
+
createDecryptionShare(
|
|
132
|
+
encryptedMessage,
|
|
133
|
+
participant1KeyShare.partyPrivateKey,
|
|
134
|
+
prime,
|
|
135
|
+
),
|
|
136
|
+
createDecryptionShare(
|
|
137
|
+
encryptedMessage,
|
|
138
|
+
participant2KeyShare.partyPrivateKey,
|
|
139
|
+
prime,
|
|
140
|
+
),
|
|
141
|
+
createDecryptionShare(
|
|
142
|
+
encryptedMessage,
|
|
143
|
+
participant3KeyShare.partyPrivateKey,
|
|
144
|
+
prime,
|
|
145
|
+
),
|
|
146
|
+
];
|
|
147
|
+
// Combining the decryption shares into one, used to decrypt the message
|
|
148
|
+
const combinedDecryptionShares = combineDecryptionShares(
|
|
149
|
+
decryptionShares,
|
|
150
|
+
prime,
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
// Decrypting the message using the combined decryption shares
|
|
154
|
+
const thresholdDecryptedMessage = thresholdDecrypt(
|
|
155
|
+
encryptedMessage,
|
|
156
|
+
combinedDecryptionShares,
|
|
157
|
+
prime,
|
|
158
|
+
);
|
|
159
|
+
console.log(thresholdDecryptedMessage); // 42
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Voting and Multiplication with Threshold Scheme for 3 Participants
|
|
163
|
+
|
|
164
|
+
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.
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
import {
|
|
168
|
+
encrypt,
|
|
169
|
+
generateSingleKeyShare,
|
|
170
|
+
combinePublicKeys,
|
|
171
|
+
createDecryptionShare,
|
|
172
|
+
combineDecryptionShares,
|
|
173
|
+
thresholdDecrypt,
|
|
174
|
+
multiplyEncryptedValues,
|
|
175
|
+
generateParameters,
|
|
176
|
+
} from "threshold-elgamal";
|
|
177
|
+
|
|
178
|
+
const primeBits = 2048; // Bit length of the prime modulus
|
|
179
|
+
const threshold = 3; // A scenario for 3 participants with a threshold of 3
|
|
180
|
+
const { prime, generator } = getGroup(2048);
|
|
181
|
+
|
|
182
|
+
// Each participant generates their public key share and private key individually
|
|
183
|
+
const participant1KeyShare = generateSingleKeyShare(1, threshold, primeBits);
|
|
184
|
+
const participant2KeyShare = generateSingleKeyShare(2, threshold, primeBits);
|
|
185
|
+
const participant3KeyShare = generateSingleKeyShare(3, threshold, primeBits);
|
|
186
|
+
|
|
187
|
+
// Combine the public keys to form a single public key
|
|
188
|
+
const combinedPublicKey = combinePublicKeys(
|
|
189
|
+
[
|
|
190
|
+
participant1KeyShare.partyPublicKey,
|
|
191
|
+
participant2KeyShare.partyPublicKey,
|
|
192
|
+
participant3KeyShare.partyPublicKey,
|
|
193
|
+
],
|
|
194
|
+
prime,
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
// Participants cast their encrypted votes for two options
|
|
198
|
+
const voteOption1 = [6, 7, 1]; // Votes for option 1 by participants 1, 2, and 3
|
|
199
|
+
const voteOption2 = [10, 7, 4]; // Votes for option 2 by participants 1, 2, and 3
|
|
200
|
+
|
|
201
|
+
// Encrypt votes for both options
|
|
202
|
+
const encryptedVotesOption1 = voteOption1.map((vote) =>
|
|
203
|
+
encrypt(vote, prime, generator, combinedPublicKey),
|
|
204
|
+
);
|
|
205
|
+
const encryptedVotesOption2 = voteOption2.map((vote) =>
|
|
206
|
+
encrypt(vote, prime, generator, combinedPublicKey),
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
// Multiply encrypted votes together to aggregate
|
|
210
|
+
const aggregatedEncryptedVoteOption1 = encryptedVotesOption1.reduce(
|
|
211
|
+
(acc, current) => multiplyEncryptedValues(acc, current, prime),
|
|
212
|
+
{ c1: 1n, c2: 1n },
|
|
213
|
+
);
|
|
214
|
+
const aggregatedEncryptedVoteOption2 = encryptedVotesOption2.reduce(
|
|
215
|
+
(acc, current) => multiplyEncryptedValues(acc, current, prime),
|
|
216
|
+
{ c1: 1n, c2: 1n },
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
// Each participant creates a decryption share for both options.
|
|
220
|
+
// Notice that the shares are created for the aggregated, multiplied tally specifically,
|
|
221
|
+
// not the individual votes. This means that they can be used ONLY for decrypting the aggregated votes.
|
|
222
|
+
const decryptionSharesOption1 = [
|
|
223
|
+
createDecryptionShare(
|
|
224
|
+
aggregatedEncryptedVoteOption1,
|
|
225
|
+
participant1KeyShare.partyPrivateKey,
|
|
226
|
+
prime,
|
|
227
|
+
),
|
|
228
|
+
createDecryptionShare(
|
|
229
|
+
aggregatedEncryptedVoteOption1,
|
|
230
|
+
participant2KeyShare.partyPrivateKey,
|
|
231
|
+
prime,
|
|
232
|
+
),
|
|
233
|
+
createDecryptionShare(
|
|
234
|
+
aggregatedEncryptedVoteOption1,
|
|
235
|
+
participant3KeyShare.partyPrivateKey,
|
|
236
|
+
prime,
|
|
237
|
+
),
|
|
238
|
+
];
|
|
239
|
+
const decryptionSharesOption2 = [
|
|
240
|
+
createDecryptionShare(
|
|
241
|
+
aggregatedEncryptedVoteOption2,
|
|
242
|
+
participant1KeyShare.partyPrivateKey,
|
|
243
|
+
prime,
|
|
244
|
+
),
|
|
245
|
+
createDecryptionShare(
|
|
246
|
+
aggregatedEncryptedVoteOption2,
|
|
247
|
+
participant2KeyShare.partyPrivateKey,
|
|
248
|
+
prime,
|
|
249
|
+
),
|
|
250
|
+
createDecryptionShare(
|
|
251
|
+
aggregatedEncryptedVoteOption2,
|
|
252
|
+
participant3KeyShare.partyPrivateKey,
|
|
253
|
+
prime,
|
|
254
|
+
),
|
|
255
|
+
];
|
|
256
|
+
|
|
257
|
+
// Combine decryption shares and decrypt the aggregated votes for both options.
|
|
258
|
+
// Notice that the private keys of the participants never leave their possession.
|
|
259
|
+
// Only the decryption shares are shared with other participants.
|
|
260
|
+
const combinedDecryptionSharesOption1 = combineDecryptionShares(
|
|
261
|
+
decryptionSharesOption1,
|
|
262
|
+
prime,
|
|
263
|
+
);
|
|
264
|
+
const combinedDecryptionSharesOption2 = combineDecryptionShares(
|
|
265
|
+
decryptionSharesOption2,
|
|
266
|
+
prime,
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
const finalTallyOption1 = thresholdDecrypt(
|
|
270
|
+
aggregatedEncryptedVoteOption1,
|
|
271
|
+
combinedDecryptionSharesOption1,
|
|
272
|
+
prime,
|
|
273
|
+
);
|
|
274
|
+
const finalTallyOption2 = thresholdDecrypt(
|
|
275
|
+
aggregatedEncryptedVoteOption2,
|
|
276
|
+
combinedDecryptionSharesOption2,
|
|
277
|
+
prime,
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
console.log(
|
|
281
|
+
`Final tally for Option 1: ${finalTallyOption1}, Option 2: ${finalTallyOption2}`,
|
|
282
|
+
); // 42, 280
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
This example can be extended with calculating a geometric mean for the candidate options to better present the results.
|
|
286
|
+
|
|
287
|
+
## License
|
|
288
|
+
|
|
289
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|