@things-factory/survey 0.0.0-stage → 10.1.316
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/dist-client/index.d.ts +1 -0
- package/dist-client/index.js +2 -0
- package/dist-client/pages/my-surveys.d.ts +24 -0
- package/dist-client/pages/my-surveys.js +256 -0
- package/dist-client/pages/survey-editor.d.ts +62 -0
- package/dist-client/pages/survey-editor.js +668 -0
- package/dist-client/pages/survey-list.d.ts +15 -0
- package/dist-client/pages/survey-list.js +150 -0
- package/dist-client/pages/survey-trustee.d.ts +21 -0
- package/dist-client/pages/survey-trustee.js +170 -0
- package/dist-client/route.d.ts +1 -0
- package/dist-client/route.js +17 -0
- package/dist-client/utils/survey-api.d.ts +16 -0
- package/dist-client/utils/survey-api.js +46 -0
- package/dist-client/utils/survey-result.d.ts +4 -0
- package/dist-client/utils/survey-result.js +79 -0
- package/dist-client/utils/survey-secret.d.ts +40 -0
- package/dist-client/utils/survey-secret.js +154 -0
- package/dist-server/index.d.ts +2 -0
- package/dist-server/index.js +27 -0
- package/dist-server/role-tasks.d.ts +2 -0
- package/dist-server/role-tasks.js +16 -0
- package/dist-server/service/index.d.ts +16 -0
- package/dist-server/service/index.js +30 -0
- package/dist-server/service/refusal.d.ts +57 -0
- package/dist-server/service/refusal.js +66 -0
- package/dist-server/service/survey/ballot-route.d.ts +1 -0
- package/dist-server/service/survey/ballot-route.js +30 -0
- package/dist-server/service/survey/ballot-worker.d.ts +1 -0
- package/dist-server/service/survey/ballot-worker.js +19 -0
- package/dist-server/service/survey/index.d.ts +10 -0
- package/dist-server/service/survey/index.js +14 -0
- package/dist-server/service/survey/survey-answer.d.ts +23 -0
- package/dist-server/service/survey/survey-answer.js +99 -0
- package/dist-server/service/survey/survey-attributes.d.ts +16 -0
- package/dist-server/service/survey/survey-attributes.js +39 -0
- package/dist-server/service/survey/survey-ballot.d.ts +14 -0
- package/dist-server/service/survey/survey-ballot.js +61 -0
- package/dist-server/service/survey/survey-class-key.d.ts +20 -0
- package/dist-server/service/survey/survey-class-key.js +92 -0
- package/dist-server/service/survey/survey-question.d.ts +27 -0
- package/dist-server/service/survey/survey-question.js +113 -0
- package/dist-server/service/survey/survey-resolver.d.ts +37 -0
- package/dist-server/service/survey/survey-resolver.js +492 -0
- package/dist-server/service/survey/survey-respondent.d.ts +17 -0
- package/dist-server/service/survey/survey-respondent.js +81 -0
- package/dist-server/service/survey/survey-rules.d.ts +169 -0
- package/dist-server/service/survey/survey-rules.js +299 -0
- package/dist-server/service/survey/survey-secret.d.ts +133 -0
- package/dist-server/service/survey/survey-secret.js +347 -0
- package/dist-server/service/survey/survey-service.d.ts +83 -0
- package/dist-server/service/survey/survey-service.js +384 -0
- package/dist-server/service/survey/survey-trustee.d.ts +25 -0
- package/dist-server/service/survey/survey-trustee.js +119 -0
- package/dist-server/service/survey/survey-types.d.ts +91 -0
- package/dist-server/service/survey/survey-types.js +319 -0
- package/dist-server/service/survey/survey.d.ts +47 -0
- package/dist-server/service/survey/survey.js +214 -0
- package/dist-shared/ballot.d.ts +54 -0
- package/dist-shared/ballot.js +146 -0
- package/dist-shared/blind-rsa.d.ts +18 -0
- package/dist-shared/blind-rsa.js +127 -0
- package/dist-shared/elgamal.d.ts +20 -0
- package/dist-shared/elgamal.js +54 -0
- package/dist-shared/group.d.ts +22 -0
- package/dist-shared/group.js +112 -0
- package/dist-shared/proofs.d.ts +20 -0
- package/dist-shared/proofs.js +94 -0
- package/dist-shared/threshold.d.ts +39 -0
- package/dist-shared/threshold.js +98 -0
- package/dist-shared/verify.d.ts +13 -0
- package/dist-shared/verify.js +73 -0
- package/package.json +63 -4
- package/things-factory.config.js +15 -0
- package/translations/en.json +202 -0
- package/translations/es.json +202 -0
- package/translations/ja.json +202 -0
- package/translations/ko.json +202 -0
- package/translations/ms.json +202 -0
- package/translations/zh.json +202 -0
- package/README.md +0 -3
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cellsOf = cellsOf;
|
|
4
|
+
exports.valuesOf = valuesOf;
|
|
5
|
+
exports.sealBallot = sealBallot;
|
|
6
|
+
exports.openedCells = openedCells;
|
|
7
|
+
exports.weightedSums = weightedSums;
|
|
8
|
+
exports.boundOf = boundOf;
|
|
9
|
+
exports.cellsOfChecked = cellsOfChecked;
|
|
10
|
+
/*
|
|
11
|
+
* A secret ballot (operato-survey design §6-2): every answer becomes ciphertexts with proofs that they are a valid
|
|
12
|
+
* answer, and nothing else. The server checks the proofs and keeps the ciphertexts; the count multiplies them.
|
|
13
|
+
*
|
|
14
|
+
* - pick one / scale: one 0-or-1 per choice (a scale's choices are its values), and their sum is 1;
|
|
15
|
+
* - pick several: one 0-or-1 per choice, and their sum is between 1 and the most allowed;
|
|
16
|
+
* - split a total: each share in bits, each bit 0 or 1, and the shares sum to the total - so none is negative or more.
|
|
17
|
+
*/
|
|
18
|
+
const elgamal_1 = require("./elgamal");
|
|
19
|
+
const group_1 = require("./group");
|
|
20
|
+
const proofs_1 = require("./proofs");
|
|
21
|
+
/** The cells of a question - its choices' keys, or a scale's values. */
|
|
22
|
+
function cellsOf(question) {
|
|
23
|
+
if (question.kind === 'SCALE') {
|
|
24
|
+
const out = [];
|
|
25
|
+
for (let v = question.scaleMin ?? 0; v <= (question.scaleMax ?? -1); v++)
|
|
26
|
+
out.push(String(v));
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
return (question.options ?? []).map(one => one.key);
|
|
30
|
+
}
|
|
31
|
+
const bitsOf = (total) => Math.max(1, Math.ceil(Math.log2(total + 1)));
|
|
32
|
+
const range = (from, to) => Array.from({ length: to - from + 1 }, (_, i) => from + i);
|
|
33
|
+
/** The sums a question's cells may add to. */
|
|
34
|
+
function sumsOf(question) {
|
|
35
|
+
switch (question.kind) {
|
|
36
|
+
case 'SINGLE':
|
|
37
|
+
case 'SCALE':
|
|
38
|
+
return [1];
|
|
39
|
+
case 'MULTI':
|
|
40
|
+
return range(1, question.maxPicks ?? 1);
|
|
41
|
+
case 'ALLOCATE':
|
|
42
|
+
return [question.allocateTotal ?? 0];
|
|
43
|
+
}
|
|
44
|
+
throw new Error(`no secret ballot for ${question.kind}`);
|
|
45
|
+
}
|
|
46
|
+
/** The value of each cell for an answer - the answer was checked against the question before (survey-rules). */
|
|
47
|
+
function valuesOf(question, answer) {
|
|
48
|
+
const cells = cellsOf(question);
|
|
49
|
+
switch (question.kind) {
|
|
50
|
+
case 'SINGLE':
|
|
51
|
+
return cells.map(key => (key === answer.pick ? 1 : 0));
|
|
52
|
+
case 'SCALE':
|
|
53
|
+
return cells.map(key => (key === String(answer.value) ? 1 : 0));
|
|
54
|
+
case 'MULTI':
|
|
55
|
+
return cells.map(key => ((answer.picks ?? []).includes(key) ? 1 : 0));
|
|
56
|
+
case 'ALLOCATE':
|
|
57
|
+
return cells.map(key => answer.shares?.[key] ?? 0);
|
|
58
|
+
}
|
|
59
|
+
throw new Error(`no secret ballot for ${question.kind}`);
|
|
60
|
+
}
|
|
61
|
+
const contextOf = (base, question, cell, bit) => `${base}|q:${question}|c:${cell}${bit === undefined ? '' : `|b:${bit}`}`;
|
|
62
|
+
function sealBallot(h, questions, answers, context) {
|
|
63
|
+
return questions.map(question => {
|
|
64
|
+
const values = valuesOf(question, answers[question.id]);
|
|
65
|
+
const most = question.kind === 'ALLOCATE' ? (question.allocateTotal ?? 0) : 1;
|
|
66
|
+
if (values.some(value => !Number.isInteger(value) || value < 0 || value > most))
|
|
67
|
+
throw new Error('an answer outside its question');
|
|
68
|
+
let sumR = 0n;
|
|
69
|
+
const cells = values.map((value, index) => {
|
|
70
|
+
if (question.kind !== 'ALLOCATE') {
|
|
71
|
+
const r = (0, group_1.randomScalar)();
|
|
72
|
+
sumR = (0, group_1.mod)(sumR + r, group_1.Q);
|
|
73
|
+
const c = (0, elgamal_1.encrypt)(h, BigInt(value), r);
|
|
74
|
+
return { ...(0, elgamal_1.cipherJson)(c), p: (0, proofs_1.proveOneOf)(h, c, r, [0, 1], value, contextOf(context, question.id, index)) };
|
|
75
|
+
}
|
|
76
|
+
const bits = [];
|
|
77
|
+
for (let i = 0; i < bitsOf(question.allocateTotal ?? 0); i++) {
|
|
78
|
+
const bit = (value >> i) & 1;
|
|
79
|
+
const r = (0, group_1.randomScalar)();
|
|
80
|
+
sumR = (0, group_1.mod)(sumR + r * (1n << BigInt(i)), group_1.Q);
|
|
81
|
+
const c = (0, elgamal_1.encrypt)(h, BigInt(bit), r);
|
|
82
|
+
bits.push({ ...(0, elgamal_1.cipherJson)(c), p: (0, proofs_1.proveOneOf)(h, c, r, [0, 1], bit, contextOf(context, question.id, index, i)) });
|
|
83
|
+
}
|
|
84
|
+
return { ...(0, elgamal_1.cipherJson)(cellOfBits(bits.map(elgamal_1.cipherOf))), bits };
|
|
85
|
+
});
|
|
86
|
+
const total = values.reduce((sum, one) => sum + one, 0);
|
|
87
|
+
const sum = (0, elgamal_1.product)(cells.map(elgamal_1.cipherOf));
|
|
88
|
+
return { question: question.id, cells, sum: (0, proofs_1.proveOneOf)(h, sum, sumR, sumsOf(question), total, contextOf(context, question.id, 'sum')) };
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
const cellOfBits = (bits) => bits.reduce((out, bit, i) => (0, elgamal_1.multiply)(out, (0, elgamal_1.power)(bit, 1n << BigInt(i))), elgamal_1.ONE);
|
|
92
|
+
/**
|
|
93
|
+
* The cells of a sealed ballot, every proof checked - or why it is refused. A share's cell is computed from its bits,
|
|
94
|
+
* never taken as sent.
|
|
95
|
+
*/
|
|
96
|
+
function openedCells(h, questions, ballot, context) {
|
|
97
|
+
try {
|
|
98
|
+
if (!Array.isArray(ballot) || ballot.length !== questions.length)
|
|
99
|
+
return { problem: 'ballot-shape' };
|
|
100
|
+
const out = [];
|
|
101
|
+
for (const [n, question] of questions.entries()) {
|
|
102
|
+
const part = ballot[n];
|
|
103
|
+
const keys = cellsOf(question);
|
|
104
|
+
if (part?.question !== question.id || part.cells?.length !== keys.length)
|
|
105
|
+
return { problem: 'ballot-shape' };
|
|
106
|
+
const cells = [];
|
|
107
|
+
for (const [index, cell] of part.cells.entries()) {
|
|
108
|
+
if (question.kind !== 'ALLOCATE') {
|
|
109
|
+
const c = (0, elgamal_1.cipherOf)(cell);
|
|
110
|
+
if (!(0, proofs_1.verifyOneOf)(h, c, [0, 1], cell.p, contextOf(context, question.id, index)))
|
|
111
|
+
return { problem: 'ballot-proof' };
|
|
112
|
+
cells.push(c);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (cell.bits?.length !== bitsOf(question.allocateTotal ?? 0))
|
|
116
|
+
return { problem: 'ballot-shape' };
|
|
117
|
+
const bits = cell.bits.map(elgamal_1.cipherOf);
|
|
118
|
+
for (const [i, bit] of bits.entries())
|
|
119
|
+
if (!(0, proofs_1.verifyOneOf)(h, bit, [0, 1], cell.bits[i].p, contextOf(context, question.id, index, i)))
|
|
120
|
+
return { problem: 'ballot-proof' };
|
|
121
|
+
cells.push(cellOfBits(bits));
|
|
122
|
+
}
|
|
123
|
+
if (!(0, proofs_1.verifyOneOf)(h, (0, elgamal_1.product)(cells), sumsOf(question), part.sum, contextOf(context, question.id, 'sum')))
|
|
124
|
+
return { problem: 'ballot-proof' };
|
|
125
|
+
out.push(cells);
|
|
126
|
+
}
|
|
127
|
+
return { cells: out };
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return { problem: 'ballot-shape' };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/** The weighted sum of every ballot's cells - a ballot's cells raised to its class's rate (design §6-1). */
|
|
134
|
+
function weightedSums(ballots, questions) {
|
|
135
|
+
return questions.map((question, n) => cellsOf(question).map((_, index) => (0, elgamal_1.product)(ballots.map(one => (0, elgamal_1.power)(one.cells[n][index], BigInt(one.rate))))));
|
|
136
|
+
}
|
|
137
|
+
/** The most a cell's sum can be - what the discrete log is searched up to. */
|
|
138
|
+
function boundOf(question, ballots, maxRate) {
|
|
139
|
+
const most = question.kind === 'ALLOCATE' ? (question.allocateTotal ?? 0) : 1;
|
|
140
|
+
return ballots * maxRate * most;
|
|
141
|
+
}
|
|
142
|
+
/** The cells of a ballot whose proofs were checked when it came in - for the count, which does not check them again. */
|
|
143
|
+
function cellsOfChecked(questions, ballot) {
|
|
144
|
+
return questions.map((question, n) => ballot[n].cells.map(cell => (question.kind === 'ALLOCATE' ? cellOfBits((cell.bits ?? []).map(elgamal_1.cipherOf)) : (0, elgamal_1.cipherOf)(cell))));
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=ballot.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface RsaPublic {
|
|
2
|
+
n: bigint;
|
|
3
|
+
e: bigint;
|
|
4
|
+
}
|
|
5
|
+
export declare const tokenMessage: (survey: string, serial: string) => Uint8Array<ArrayBuffer>;
|
|
6
|
+
/** The browser's step one: a blinded message and what unblinds it. */
|
|
7
|
+
export declare function blind(key: RsaPublic, message: Uint8Array): {
|
|
8
|
+
blinded: bigint;
|
|
9
|
+
inverse: bigint;
|
|
10
|
+
};
|
|
11
|
+
/** The server's step: the blinded message signed - and checked, so a fault never leaks the key (RFC 9474 §4.3). */
|
|
12
|
+
export declare function blindSign(key: RsaPublic & {
|
|
13
|
+
d: bigint;
|
|
14
|
+
}, blinded: bigint): bigint;
|
|
15
|
+
/** The browser's step two: the signature unblinded and checked. */
|
|
16
|
+
export declare function finalize(key: RsaPublic, message: Uint8Array, blindSignature: bigint, inverseR: bigint): bigint;
|
|
17
|
+
/** Whether s is a signature of the message under the key. */
|
|
18
|
+
export declare function verifyToken(key: RsaPublic, message: Uint8Array, s: bigint): boolean;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tokenMessage = void 0;
|
|
4
|
+
exports.blind = blind;
|
|
5
|
+
exports.blindSign = blindSign;
|
|
6
|
+
exports.finalize = finalize;
|
|
7
|
+
exports.verifyToken = verifyToken;
|
|
8
|
+
/*
|
|
9
|
+
* A one-time ballot token the server signs without seeing (operato-survey design §6-4) - RSA blind signatures as RFC 9474
|
|
10
|
+
* sets them out (RSABSSA-SHA384-PSS-Deterministic: SHA-384, MGF1-SHA-384, a 48-byte salt, no message prefix). The
|
|
11
|
+
* browser blinds a random serial, the server signs the blinded value with the key of the respondent's weight class,
|
|
12
|
+
* the browser unblinds - and the result is an ordinary RSA-PSS signature on a serial the server has never seen.
|
|
13
|
+
*/
|
|
14
|
+
const sha512_1 = require("@noble/hashes/sha512");
|
|
15
|
+
const group_1 = require("./group");
|
|
16
|
+
const H_LEN = 48;
|
|
17
|
+
const S_LEN = 48;
|
|
18
|
+
const bitLength = (n) => n.toString(2).length;
|
|
19
|
+
const byteLength = (n) => Math.ceil(bitLength(n) / 8);
|
|
20
|
+
function gcd(a, b) {
|
|
21
|
+
while (b)
|
|
22
|
+
[a, b] = [b, a % b];
|
|
23
|
+
return a;
|
|
24
|
+
}
|
|
25
|
+
function inverse(a, m) {
|
|
26
|
+
let [oldR, r] = [((a % m) + m) % m, m];
|
|
27
|
+
let [oldS, s] = [1n, 0n];
|
|
28
|
+
while (r) {
|
|
29
|
+
const q = oldR / r;
|
|
30
|
+
[oldR, r] = [r, oldR - q * r];
|
|
31
|
+
[oldS, s] = [s, oldS - q * s];
|
|
32
|
+
}
|
|
33
|
+
if (oldR !== 1n)
|
|
34
|
+
throw new Error('not invertible');
|
|
35
|
+
return ((oldS % m) + m) % m;
|
|
36
|
+
}
|
|
37
|
+
function mgf1(seed, length) {
|
|
38
|
+
const out = new Uint8Array(length);
|
|
39
|
+
for (let counter = 0, at = 0; at < length; counter++) {
|
|
40
|
+
const block = (0, sha512_1.sha384)(new Uint8Array([...seed, (counter >>> 24) & 255, (counter >>> 16) & 255, (counter >>> 8) & 255, counter & 255]));
|
|
41
|
+
out.set(block.slice(0, Math.min(block.length, length - at)), at);
|
|
42
|
+
at += block.length;
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
/** EMSA-PSS-ENCODE (RFC 8017 §9.1.1). */
|
|
47
|
+
function pssEncode(message, emBits, salt) {
|
|
48
|
+
const emLen = Math.ceil(emBits / 8);
|
|
49
|
+
const mHash = (0, sha512_1.sha384)(message);
|
|
50
|
+
if (emLen < H_LEN + S_LEN + 2)
|
|
51
|
+
throw new Error('key too small');
|
|
52
|
+
const h = (0, sha512_1.sha384)(new Uint8Array([...new Uint8Array(8), ...mHash, ...salt]));
|
|
53
|
+
const db = new Uint8Array(emLen - H_LEN - 1);
|
|
54
|
+
db[emLen - S_LEN - H_LEN - 2] = 1;
|
|
55
|
+
db.set(salt, emLen - S_LEN - H_LEN - 1);
|
|
56
|
+
const mask = mgf1(h, db.length);
|
|
57
|
+
for (let i = 0; i < db.length; i++)
|
|
58
|
+
db[i] ^= mask[i];
|
|
59
|
+
db[0] &= 0xff >>> (8 * emLen - emBits);
|
|
60
|
+
return new Uint8Array([...db, ...h, 0xbc]);
|
|
61
|
+
}
|
|
62
|
+
/** EMSA-PSS-VERIFY (RFC 8017 §9.1.2). */
|
|
63
|
+
function pssVerify(message, em, emBits) {
|
|
64
|
+
const emLen = Math.ceil(emBits / 8);
|
|
65
|
+
if (em.length !== emLen || emLen < H_LEN + S_LEN + 2 || em[emLen - 1] !== 0xbc)
|
|
66
|
+
return false;
|
|
67
|
+
const masked = em.slice(0, emLen - H_LEN - 1);
|
|
68
|
+
const h = em.slice(emLen - H_LEN - 1, emLen - 1);
|
|
69
|
+
const top = 0xff >>> (8 * emLen - emBits);
|
|
70
|
+
if ((masked[0] & ~top) !== 0)
|
|
71
|
+
return false;
|
|
72
|
+
const mask = mgf1(h, masked.length);
|
|
73
|
+
const db = masked.map((byte, i) => byte ^ mask[i]);
|
|
74
|
+
db[0] &= top;
|
|
75
|
+
const pad = emLen - H_LEN - S_LEN - 2;
|
|
76
|
+
for (let i = 0; i < pad; i++)
|
|
77
|
+
if (db[i] !== 0)
|
|
78
|
+
return false;
|
|
79
|
+
if (db[pad] !== 1)
|
|
80
|
+
return false;
|
|
81
|
+
const salt = db.slice(db.length - S_LEN);
|
|
82
|
+
const expected = (0, sha512_1.sha384)(new Uint8Array([...new Uint8Array(8), ...(0, sha512_1.sha384)(message), ...salt]));
|
|
83
|
+
return expected.every((byte, i) => byte === h[i]);
|
|
84
|
+
}
|
|
85
|
+
const tokenMessage = (survey, serial) => new TextEncoder().encode(`survey-ballot|${survey}|${serial}`);
|
|
86
|
+
exports.tokenMessage = tokenMessage;
|
|
87
|
+
/** The browser's step one: a blinded message and what unblinds it. */
|
|
88
|
+
function blind(key, message) {
|
|
89
|
+
const em = pssEncode(message, bitLength(key.n) - 1, (0, group_1.randomBytes)(S_LEN));
|
|
90
|
+
const m = (0, group_1.bytesToBig)(em);
|
|
91
|
+
if (gcd(m, key.n) !== 1n)
|
|
92
|
+
throw new Error('blinding failed - try again');
|
|
93
|
+
for (;;) {
|
|
94
|
+
const r = (0, group_1.bytesToBig)((0, group_1.randomBytes)(byteLength(key.n))) % key.n;
|
|
95
|
+
if (r < 2n || gcd(r, key.n) !== 1n)
|
|
96
|
+
continue;
|
|
97
|
+
return { blinded: (m * (0, group_1.modPow)(r, key.e, key.n)) % key.n, inverse: inverse(r, key.n) };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** The server's step: the blinded message signed - and checked, so a fault never leaks the key (RFC 9474 §4.3). */
|
|
101
|
+
function blindSign(key, blinded) {
|
|
102
|
+
if (blinded <= 0n || blinded >= key.n)
|
|
103
|
+
throw new Error('blinded message out of range');
|
|
104
|
+
const s = (0, group_1.modPow)(blinded, key.d, key.n);
|
|
105
|
+
if ((0, group_1.modPow)(s, key.e, key.n) !== blinded)
|
|
106
|
+
throw new Error('signing failed');
|
|
107
|
+
return s;
|
|
108
|
+
}
|
|
109
|
+
/** The browser's step two: the signature unblinded and checked. */
|
|
110
|
+
function finalize(key, message, blindSignature, inverseR) {
|
|
111
|
+
const s = (blindSignature * inverseR) % key.n;
|
|
112
|
+
if (!verifyToken(key, message, s))
|
|
113
|
+
throw new Error('the signature does not verify');
|
|
114
|
+
return s;
|
|
115
|
+
}
|
|
116
|
+
/** Whether s is a signature of the message under the key. */
|
|
117
|
+
function verifyToken(key, message, s) {
|
|
118
|
+
if (s <= 0n || s >= key.n)
|
|
119
|
+
return false;
|
|
120
|
+
const emBits = bitLength(key.n) - 1;
|
|
121
|
+
const whole = (0, group_1.bigToBytes)((0, group_1.modPow)(s, key.e, key.n), byteLength(key.n));
|
|
122
|
+
const cut = byteLength(key.n) - Math.ceil(emBits / 8);
|
|
123
|
+
if (whole.slice(0, cut).some(byte => byte !== 0))
|
|
124
|
+
return false;
|
|
125
|
+
return pssVerify(message, whole.slice(cut), emBits);
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=blind-rsa.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface Cipher {
|
|
2
|
+
a: bigint;
|
|
3
|
+
b: bigint;
|
|
4
|
+
}
|
|
5
|
+
export type CipherJson = {
|
|
6
|
+
a: string;
|
|
7
|
+
b: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const ONE: Cipher;
|
|
10
|
+
export declare const encrypt: (h: bigint, m: bigint, r: bigint) => Cipher;
|
|
11
|
+
export declare const multiply: (x: Cipher, y: Cipher) => Cipher;
|
|
12
|
+
export declare const power: (x: Cipher, k: bigint) => Cipher;
|
|
13
|
+
export declare const product: (list: readonly Cipher[]) => Cipher;
|
|
14
|
+
/** g^m from a ciphertext and the whole secret key - for tests; a real survey never holds the whole key. */
|
|
15
|
+
export declare const openWithKey: (c: Cipher, x: bigint) => bigint;
|
|
16
|
+
/** m from g^m, for 0 ≤ m ≤ bound - baby-step giant-step. */
|
|
17
|
+
export declare function discreteLog(gm: bigint, bound: number): number | undefined;
|
|
18
|
+
export declare const cipherJson: (c: Cipher) => CipherJson;
|
|
19
|
+
/** A ciphertext from what someone sent - both parts must be elements of the group. */
|
|
20
|
+
export declare function cipherOf(json: unknown): Cipher;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cipherJson = exports.openWithKey = exports.product = exports.power = exports.multiply = exports.encrypt = exports.ONE = void 0;
|
|
4
|
+
exports.discreteLog = discreteLog;
|
|
5
|
+
exports.cipherOf = cipherOf;
|
|
6
|
+
/*
|
|
7
|
+
* Exponential ElGamal (operato-survey design §6-1): a value m under the public key h is (g^r, g^m · h^r). Ciphertexts
|
|
8
|
+
* multiply into the encryption of the sum; raised to k, into k times the value - the weighted sum is computed without
|
|
9
|
+
* opening a single ballot.
|
|
10
|
+
*/
|
|
11
|
+
const group_1 = require("./group");
|
|
12
|
+
exports.ONE = { a: 1n, b: 1n };
|
|
13
|
+
const encrypt = (h, m, r) => ({ a: (0, group_1.gPow)(r), b: ((0, group_1.gPow)(m) * (0, group_1.modPow)(h, r, group_1.P)) % group_1.P });
|
|
14
|
+
exports.encrypt = encrypt;
|
|
15
|
+
const multiply = (x, y) => ({ a: (x.a * y.a) % group_1.P, b: (x.b * y.b) % group_1.P });
|
|
16
|
+
exports.multiply = multiply;
|
|
17
|
+
const power = (x, k) => ({ a: (0, group_1.modPow)(x.a, (0, group_1.mod)(k, group_1.Q), group_1.P), b: (0, group_1.modPow)(x.b, (0, group_1.mod)(k, group_1.Q), group_1.P) });
|
|
18
|
+
exports.power = power;
|
|
19
|
+
const product = (list) => list.reduce(exports.multiply, exports.ONE);
|
|
20
|
+
exports.product = product;
|
|
21
|
+
/** g^m from a ciphertext and the whole secret key - for tests; a real survey never holds the whole key. */
|
|
22
|
+
const openWithKey = (c, x) => (c.b * (0, group_1.invP)((0, group_1.modPow)(c.a, x, group_1.P))) % group_1.P;
|
|
23
|
+
exports.openWithKey = openWithKey;
|
|
24
|
+
/** m from g^m, for 0 ≤ m ≤ bound - baby-step giant-step. */
|
|
25
|
+
function discreteLog(gm, bound) {
|
|
26
|
+
const step = Math.ceil(Math.sqrt(bound + 1));
|
|
27
|
+
const table = new Map();
|
|
28
|
+
let x = 1n;
|
|
29
|
+
for (let j = 0; j < step; j++) {
|
|
30
|
+
if (!table.has(x))
|
|
31
|
+
table.set(x, j);
|
|
32
|
+
x = (x * 2n) % group_1.P;
|
|
33
|
+
}
|
|
34
|
+
const giant = (0, group_1.invP)((0, group_1.gPow)(BigInt(step)));
|
|
35
|
+
let y = gm;
|
|
36
|
+
for (let i = 0; i <= step; i++) {
|
|
37
|
+
const j = table.get(y);
|
|
38
|
+
if (j !== undefined && i * step + j <= bound)
|
|
39
|
+
return i * step + j;
|
|
40
|
+
y = (y * giant) % group_1.P;
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const cipherJson = (c) => ({ a: (0, group_1.hex)(c.a), b: (0, group_1.hex)(c.b) });
|
|
45
|
+
exports.cipherJson = cipherJson;
|
|
46
|
+
/** A ciphertext from what someone sent - both parts must be elements of the group. */
|
|
47
|
+
function cipherOf(json) {
|
|
48
|
+
const one = json;
|
|
49
|
+
const c = { a: (0, group_1.fromHex)(one?.a), b: (0, group_1.fromHex)(one?.b) };
|
|
50
|
+
if (!(0, group_1.isElement)(c.a) || !(0, group_1.isElement)(c.b))
|
|
51
|
+
throw new Error('not a ciphertext of this group');
|
|
52
|
+
return c;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=elgamal.js.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare const P: bigint;
|
|
2
|
+
export declare const Q: bigint;
|
|
3
|
+
export declare const G = 2n;
|
|
4
|
+
export declare const mod: (x: bigint, m: bigint) => bigint;
|
|
5
|
+
export declare function modPow(base: bigint, exponent: bigint, m: bigint): bigint;
|
|
6
|
+
/** The inverse of a group element - p is prime. */
|
|
7
|
+
export declare const invP: (x: bigint) => bigint;
|
|
8
|
+
/** The inverse of a scalar - q is prime. */
|
|
9
|
+
export declare const invQ: (x: bigint) => bigint;
|
|
10
|
+
/** g to the power k. */
|
|
11
|
+
export declare const gPow: (k: bigint) => bigint;
|
|
12
|
+
/** Whether x is an element of the order-q subgroup - every value an untrusted party sends is checked before use. */
|
|
13
|
+
export declare const isElement: (x: unknown) => x is bigint;
|
|
14
|
+
export declare function randomBytes(length: number): Uint8Array;
|
|
15
|
+
export declare const bytesToBig: (bytes: Uint8Array) => bigint;
|
|
16
|
+
export declare function bigToBytes(x: bigint, length: number): Uint8Array;
|
|
17
|
+
/** A uniform scalar in [1, q). 64 bits more than q, so the reduction leaves no bias worth naming. */
|
|
18
|
+
export declare function randomScalar(): bigint;
|
|
19
|
+
/** A Fiat–Shamir challenge: SHA-256 of the transcript stretched to 512 bits, reduced mod q. */
|
|
20
|
+
export declare function hashToScalar(...parts: (bigint | string | number)[]): bigint;
|
|
21
|
+
export declare const hex: (x: bigint) => string;
|
|
22
|
+
export declare const fromHex: (text: string) => bigint;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fromHex = exports.hex = exports.bytesToBig = exports.isElement = exports.gPow = exports.invQ = exports.invP = exports.mod = exports.G = exports.Q = exports.P = void 0;
|
|
4
|
+
exports.modPow = modPow;
|
|
5
|
+
exports.randomBytes = randomBytes;
|
|
6
|
+
exports.bigToBytes = bigToBytes;
|
|
7
|
+
exports.randomScalar = randomScalar;
|
|
8
|
+
exports.hashToScalar = hashToScalar;
|
|
9
|
+
/*
|
|
10
|
+
* The group every survey computes in (operato-survey design §6) - the 2048-bit MODP group of RFC 3526 (group 14): a
|
|
11
|
+
* safe prime p, its prime subgroup of order q = (p - 1) / 2, generated by 2. Plain BigInt, the same in the browser and
|
|
12
|
+
* on the server - no library beyond a hash. Not constant-time: the secrets it handles are a voter's own randomness in
|
|
13
|
+
* their own browser and a key holder's share in theirs.
|
|
14
|
+
*/
|
|
15
|
+
const sha256_1 = require("@noble/hashes/sha256");
|
|
16
|
+
exports.P = BigInt('0x' +
|
|
17
|
+
'ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74' +
|
|
18
|
+
'020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f1437' +
|
|
19
|
+
'4fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7ed' +
|
|
20
|
+
'ee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf05' +
|
|
21
|
+
'98da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb' +
|
|
22
|
+
'9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3b' +
|
|
23
|
+
'e39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf695581718' +
|
|
24
|
+
'3995497cea956ae515d2261898fa051015728e5a8aacaa68ffffffffffffffff');
|
|
25
|
+
exports.Q = (exports.P - 1n) / 2n;
|
|
26
|
+
exports.G = 2n;
|
|
27
|
+
const mod = (x, m) => ((x % m) + m) % m;
|
|
28
|
+
exports.mod = mod;
|
|
29
|
+
function modPow(base, exponent, m) {
|
|
30
|
+
let result = 1n;
|
|
31
|
+
let b = (0, exports.mod)(base, m);
|
|
32
|
+
let e = exponent;
|
|
33
|
+
if (e < 0n)
|
|
34
|
+
throw new Error('negative exponent');
|
|
35
|
+
while (e > 0n) {
|
|
36
|
+
if (e & 1n)
|
|
37
|
+
result = (result * b) % m;
|
|
38
|
+
b = (b * b) % m;
|
|
39
|
+
e >>= 1n;
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
/** The inverse of a group element - p is prime. */
|
|
44
|
+
const invP = (x) => modPow(x, exports.P - 2n, exports.P);
|
|
45
|
+
exports.invP = invP;
|
|
46
|
+
/** The inverse of a scalar - q is prime. */
|
|
47
|
+
const invQ = (x) => modPow((0, exports.mod)(x, exports.Q), exports.Q - 2n, exports.Q);
|
|
48
|
+
exports.invQ = invQ;
|
|
49
|
+
/** g to the power k. */
|
|
50
|
+
const gPow = (k) => modPow(exports.G, (0, exports.mod)(k, exports.Q), exports.P);
|
|
51
|
+
exports.gPow = gPow;
|
|
52
|
+
/** Whether x is an element of the order-q subgroup - every value an untrusted party sends is checked before use. */
|
|
53
|
+
const isElement = (x) => typeof x === 'bigint' && x > 1n && x < exports.P && modPow(x, exports.Q, exports.P) === 1n;
|
|
54
|
+
exports.isElement = isElement;
|
|
55
|
+
function randomBytes(length) {
|
|
56
|
+
const out = new Uint8Array(length);
|
|
57
|
+
globalThis.crypto.getRandomValues(out);
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
const bytesToBig = (bytes) => (bytes.length ? BigInt('0x' + [...bytes].map(one => one.toString(16).padStart(2, '0')).join('')) : 0n);
|
|
61
|
+
exports.bytesToBig = bytesToBig;
|
|
62
|
+
function bigToBytes(x, length) {
|
|
63
|
+
const hex = x.toString(16).padStart(length * 2, '0');
|
|
64
|
+
if (hex.length > length * 2)
|
|
65
|
+
throw new Error('value too large');
|
|
66
|
+
const out = new Uint8Array(length);
|
|
67
|
+
for (let i = 0; i < length; i++)
|
|
68
|
+
out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
/** A uniform scalar in [1, q). 64 bits more than q, so the reduction leaves no bias worth naming. */
|
|
72
|
+
function randomScalar() {
|
|
73
|
+
for (;;) {
|
|
74
|
+
const x = (0, exports.mod)((0, exports.bytesToBig)(randomBytes(264)), exports.Q);
|
|
75
|
+
if (x !== 0n)
|
|
76
|
+
return x;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const utf8 = (text) => new TextEncoder().encode(text);
|
|
80
|
+
/** Every part written with its kind and length, so two different lists never hash alike. */
|
|
81
|
+
function transcript(parts) {
|
|
82
|
+
const chunks = [];
|
|
83
|
+
for (const part of parts) {
|
|
84
|
+
const body = typeof part === 'bigint' ? utf8(part.toString(16)) : utf8(String(part));
|
|
85
|
+
const head = utf8(`${typeof part === 'bigint' ? 'n' : 's'}${body.length}:`);
|
|
86
|
+
chunks.push(head, body);
|
|
87
|
+
}
|
|
88
|
+
const total = chunks.reduce((sum, one) => sum + one.length, 0);
|
|
89
|
+
const out = new Uint8Array(total);
|
|
90
|
+
let at = 0;
|
|
91
|
+
for (const one of chunks) {
|
|
92
|
+
out.set(one, at);
|
|
93
|
+
at += one.length;
|
|
94
|
+
}
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
/** A Fiat–Shamir challenge: SHA-256 of the transcript stretched to 512 bits, reduced mod q. */
|
|
98
|
+
function hashToScalar(...parts) {
|
|
99
|
+
const body = transcript(parts);
|
|
100
|
+
const first = (0, sha256_1.sha256)(new Uint8Array([...utf8('survey-h0|'), ...body]));
|
|
101
|
+
const second = (0, sha256_1.sha256)(new Uint8Array([...utf8('survey-h1|'), ...body]));
|
|
102
|
+
return (0, exports.mod)((0, exports.bytesToBig)(new Uint8Array([...first, ...second])), exports.Q);
|
|
103
|
+
}
|
|
104
|
+
const hex = (x) => x.toString(16);
|
|
105
|
+
exports.hex = hex;
|
|
106
|
+
const fromHex = (text) => {
|
|
107
|
+
if (typeof text !== 'string' || !/^[0-9a-f]+$/i.test(text))
|
|
108
|
+
throw new Error('not hex');
|
|
109
|
+
return BigInt('0x' + text);
|
|
110
|
+
};
|
|
111
|
+
exports.fromHex = fromHex;
|
|
112
|
+
//# sourceMappingURL=group.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Cipher } from './elgamal';
|
|
2
|
+
export type OneOfProof = {
|
|
3
|
+
e: string[];
|
|
4
|
+
z: string[];
|
|
5
|
+
};
|
|
6
|
+
export declare function proveOneOf(h: bigint, c: Cipher, r: bigint, values: readonly number[], actual: number, context: string): OneOfProof;
|
|
7
|
+
export declare function verifyOneOf(h: bigint, c: Cipher, values: readonly number[], proof: OneOfProof, context: string): boolean;
|
|
8
|
+
export type SameLogProof = {
|
|
9
|
+
e: string;
|
|
10
|
+
z: string;
|
|
11
|
+
};
|
|
12
|
+
/** That d = a^s for the s with v = g^s. */
|
|
13
|
+
export declare function proveSameLog(v: bigint, a: bigint, d: bigint, s: bigint, context: string): SameLogProof;
|
|
14
|
+
export declare function verifySameLog(v: bigint, a: bigint, d: bigint, proof: SameLogProof, context: string): boolean;
|
|
15
|
+
export type KnowsLogProof = {
|
|
16
|
+
e: string;
|
|
17
|
+
z: string;
|
|
18
|
+
};
|
|
19
|
+
export declare function proveKnowsLog(y: bigint, x: bigint, context: string): KnowsLogProof;
|
|
20
|
+
export declare function verifyKnowsLog(y: bigint, proof: KnowsLogProof, context: string): boolean;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.proveOneOf = proveOneOf;
|
|
4
|
+
exports.verifyOneOf = verifyOneOf;
|
|
5
|
+
exports.proveSameLog = proveSameLog;
|
|
6
|
+
exports.verifySameLog = verifySameLog;
|
|
7
|
+
exports.proveKnowsLog = proveKnowsLog;
|
|
8
|
+
exports.verifyKnowsLog = verifyKnowsLog;
|
|
9
|
+
const group_1 = require("./group");
|
|
10
|
+
function proveOneOf(h, c, r, values, actual, context) {
|
|
11
|
+
const k = values.indexOf(actual);
|
|
12
|
+
if (k < 0)
|
|
13
|
+
throw new Error('the value is not one of the list');
|
|
14
|
+
const e = [];
|
|
15
|
+
const z = [];
|
|
16
|
+
const commits = [];
|
|
17
|
+
const w = (0, group_1.randomScalar)();
|
|
18
|
+
for (let j = 0; j < values.length; j++) {
|
|
19
|
+
if (j === k) {
|
|
20
|
+
e.push(0n);
|
|
21
|
+
z.push(0n);
|
|
22
|
+
commits.push((0, group_1.gPow)(w), (0, group_1.modPow)(h, w, group_1.P));
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const ej = (0, group_1.randomScalar)();
|
|
26
|
+
const zj = (0, group_1.randomScalar)();
|
|
27
|
+
e.push(ej);
|
|
28
|
+
z.push(zj);
|
|
29
|
+
const bj = (c.b * (0, group_1.invP)((0, group_1.gPow)(BigInt(values[j])))) % group_1.P;
|
|
30
|
+
commits.push(((0, group_1.gPow)(zj) * (0, group_1.invP)((0, group_1.modPow)(c.a, ej, group_1.P))) % group_1.P, ((0, group_1.modPow)(h, zj, group_1.P) * (0, group_1.invP)((0, group_1.modPow)(bj, ej, group_1.P))) % group_1.P);
|
|
31
|
+
}
|
|
32
|
+
const challenge = (0, group_1.hashToScalar)('one-of', context, h, c.a, c.b, ...values.map(String), ...commits);
|
|
33
|
+
e[k] = (0, group_1.mod)(challenge - e.reduce((sum, one, j) => (j === k ? sum : sum + one), 0n), group_1.Q);
|
|
34
|
+
z[k] = (0, group_1.mod)(w + e[k] * r, group_1.Q);
|
|
35
|
+
return { e: e.map(group_1.hex), z: z.map(group_1.hex) };
|
|
36
|
+
}
|
|
37
|
+
function verifyOneOf(h, c, values, proof, context) {
|
|
38
|
+
try {
|
|
39
|
+
if (!proof || proof.e?.length !== values.length || proof.z?.length !== values.length)
|
|
40
|
+
return false;
|
|
41
|
+
const e = proof.e.map(group_1.fromHex);
|
|
42
|
+
const z = proof.z.map(group_1.fromHex);
|
|
43
|
+
if ([...e, ...z].some(one => one >= group_1.Q))
|
|
44
|
+
return false;
|
|
45
|
+
const commits = [];
|
|
46
|
+
for (let j = 0; j < values.length; j++) {
|
|
47
|
+
const bj = (c.b * (0, group_1.invP)((0, group_1.gPow)(BigInt(values[j])))) % group_1.P;
|
|
48
|
+
commits.push(((0, group_1.gPow)(z[j]) * (0, group_1.invP)((0, group_1.modPow)(c.a, e[j], group_1.P))) % group_1.P, ((0, group_1.modPow)(h, z[j], group_1.P) * (0, group_1.invP)((0, group_1.modPow)(bj, e[j], group_1.P))) % group_1.P);
|
|
49
|
+
}
|
|
50
|
+
const challenge = (0, group_1.hashToScalar)('one-of', context, h, c.a, c.b, ...values.map(String), ...commits);
|
|
51
|
+
return (0, group_1.mod)(e.reduce((sum, one) => sum + one, 0n), group_1.Q) === challenge;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** That d = a^s for the s with v = g^s. */
|
|
58
|
+
function proveSameLog(v, a, d, s, context) {
|
|
59
|
+
const w = (0, group_1.randomScalar)();
|
|
60
|
+
const e = (0, group_1.hashToScalar)('same-log', context, v, a, d, (0, group_1.gPow)(w), (0, group_1.modPow)(a, w, group_1.P));
|
|
61
|
+
return { e: (0, group_1.hex)(e), z: (0, group_1.hex)((0, group_1.mod)(w + e * s, group_1.Q)) };
|
|
62
|
+
}
|
|
63
|
+
function verifySameLog(v, a, d, proof, context) {
|
|
64
|
+
try {
|
|
65
|
+
const e = (0, group_1.fromHex)(proof.e);
|
|
66
|
+
const z = (0, group_1.fromHex)(proof.z);
|
|
67
|
+
if (e >= group_1.Q || z >= group_1.Q)
|
|
68
|
+
return false;
|
|
69
|
+
const one = ((0, group_1.gPow)(z) * (0, group_1.invP)((0, group_1.modPow)(v, e, group_1.P))) % group_1.P;
|
|
70
|
+
const two = ((0, group_1.modPow)(a, z, group_1.P) * (0, group_1.invP)((0, group_1.modPow)(d, e, group_1.P))) % group_1.P;
|
|
71
|
+
return (0, group_1.hashToScalar)('same-log', context, v, a, d, one, two) === e;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function proveKnowsLog(y, x, context) {
|
|
78
|
+
const w = (0, group_1.randomScalar)();
|
|
79
|
+
const e = (0, group_1.hashToScalar)('knows-log', context, group_1.G, y, (0, group_1.gPow)(w));
|
|
80
|
+
return { e: (0, group_1.hex)(e), z: (0, group_1.hex)((0, group_1.mod)(w + e * x, group_1.Q)) };
|
|
81
|
+
}
|
|
82
|
+
function verifyKnowsLog(y, proof, context) {
|
|
83
|
+
try {
|
|
84
|
+
const e = (0, group_1.fromHex)(proof.e);
|
|
85
|
+
const z = (0, group_1.fromHex)(proof.z);
|
|
86
|
+
if (e >= group_1.Q || z >= group_1.Q)
|
|
87
|
+
return false;
|
|
88
|
+
return (0, group_1.hashToScalar)('knows-log', context, group_1.G, y, ((0, group_1.gPow)(z) * (0, group_1.invP)((0, group_1.modPow)(y, e, group_1.P))) % group_1.P) === e;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=proofs.js.map
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type Cipher } from './elgamal';
|
|
2
|
+
import { type KnowsLogProof, type SameLogProof } from './proofs';
|
|
3
|
+
export interface Dealing {
|
|
4
|
+
/** a_0 … a_{t-1} - the dealer's own polynomial; never leaves their browser. */
|
|
5
|
+
coefficients: bigint[];
|
|
6
|
+
/** g^{a_k} - posted. */
|
|
7
|
+
commitments: bigint[];
|
|
8
|
+
/** That the dealer knows a_0 - against a key posted to cancel the others'. */
|
|
9
|
+
proof: KnowsLogProof;
|
|
10
|
+
}
|
|
11
|
+
export declare function deal(threshold: number, context: string): Dealing;
|
|
12
|
+
/** f(j) - the share for key holder j (1-based). */
|
|
13
|
+
export declare function shareFor(coefficients: readonly bigint[], j: number): bigint;
|
|
14
|
+
/** g^{f(j)} from a dealer's commitments - what a share must match. */
|
|
15
|
+
export declare function committedShare(commitments: readonly bigint[], j: number): bigint;
|
|
16
|
+
export declare const shareMatches: (commitments: readonly bigint[], j: number, share: bigint) => boolean;
|
|
17
|
+
export declare function dealingStands(commitments: readonly bigint[], proof: KnowsLogProof, threshold: number, context: string): boolean;
|
|
18
|
+
/** The survey's public key - the product of every dealer's g^{a_0}. */
|
|
19
|
+
export declare const publicKeyOf: (dealers: readonly (readonly bigint[])[]) => bigint;
|
|
20
|
+
/** Key holder j's verification key g^{s_j}, s_j being the sum of the shares dealt to j. */
|
|
21
|
+
export declare const verificationKeyOf: (dealers: readonly (readonly bigint[])[], j: number) => bigint;
|
|
22
|
+
export type SealedShare = {
|
|
23
|
+
u: string;
|
|
24
|
+
v: string;
|
|
25
|
+
};
|
|
26
|
+
export declare function sealShare(transportKey: bigint, share: bigint, context: string): SealedShare;
|
|
27
|
+
export declare function openShare(transportSecret: bigint, sealed: SealedShare, context: string): bigint;
|
|
28
|
+
export type Partial = {
|
|
29
|
+
index: number;
|
|
30
|
+
d: string;
|
|
31
|
+
proof: SameLogProof;
|
|
32
|
+
};
|
|
33
|
+
/** Key holder j's part of decrypting a ciphertext, with the proof it is theirs to give. */
|
|
34
|
+
export declare function partialOf(c: Cipher, index: number, secretShare: bigint, context: string): Partial;
|
|
35
|
+
export declare function partialStands(c: Cipher, partial: Partial, verificationKey: bigint, context: string): boolean;
|
|
36
|
+
/** λ_j at 0 for the holders in `indices`. */
|
|
37
|
+
export declare function lagrangeAtZero(indices: readonly number[], j: number): bigint;
|
|
38
|
+
/** g^m from t partial decryptions - the sum is opened, nothing else. */
|
|
39
|
+
export declare function combine(c: Cipher, partials: readonly Partial[]): bigint;
|