-
Notifications
You must be signed in to change notification settings - Fork 2
/
voter.html
230 lines (227 loc) · 8.07 KB
/
voter.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebSocket demo</title>
<script>
function borda_handleDragStart(e) {
this.style.opacity = '0.4';
dragSrcEl = this;
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/html', this.innerHTML);
}
function borda_handleDrop(e) {
e.stopPropagation(); // stops the browser from redirecting.
if (dragSrcEl !== this) {
dragSrcEl.innerHTML = this.innerHTML;
this.innerHTML = e.dataTransfer.getData('text/html');
let temp = this.getAttribute('value');
this.setAttribute('value', dragSrcEl.getAttribute('value'));
dragSrcEl.setAttribute('value', temp);
}
return false;
}
function borda_handleDragEnd(e) {
this.style.opacity = '1';
document.querySelectorAll('.box').forEach((item) => {
item.classList.remove('over');
});
}
function borda_handleDragOver(e) {
if (e.preventDefault)
e.preventDefault();
return false;
}
function borda_handleDragEnter(e) {
this.classList.add('over');
}
function borda_handleDragLeave(e) {
this.classList.remove('over');
}
function addmod(x, y, n) {
if (x + y <= x) x = x - (n - y) % n;
else x = (x + y) % n;
return x;
}
function sqrmod(a, n) {
var b, sum = 0;
a = a % n;
for (b = a; b != 0; b >>= 1) {
if (b & 1) {
sum = addmod(sum, a, n);
}
a = addmod(a, a, n);
}
return sum;
}
function expmod( base, exp, mod ){
if(exp === 0)
return 1;
else if(exp % 2 === 0)
return sqrmod(expmod(base, exp / 2, mod) % mod, mod);
else
return (base * expmod(base, exp - 1, mod)) % mod;
}
function gen_shamir(value, key_count, threshold, p) {
const coeef = [value].concat(Array.from(Array(threshold - 1).keys(), () => Math.floor(Math.random() * p)));
const sum_func = (accumulator, currentValue) => addmod(accumulator, currentValue, p);
return Array.from(Array(key_count).keys(), (x) => Array.from(coeef, (a, i) => (a * expmod(x + 1, i, p)) % p).reduce(sum_func));
}
function votesystem_range(name, idx) {
const row = document.createElement('tr');
let cell = document.createElement('td');
const range = document.createElement('input');
range.type = 'range';
range.min = 0;
range.setAttribute('value', 0);
range.value = 0;
range.max = config.L;
range.name = range.id = `value${idx}`;
cell.appendChild(range);
const label = document.createElement('label');
label.htmlFor = `value${idx}`;
label.textContent = `${name} (0)`;
range.addEventListener('change', (event) => {
label.textContent = `${name} (${event.target.value})`;
event.target.setAttribute('value', event.target.value);
});
cell.appendChild(label);
row.appendChild(cell);
return row;
}
function votesystem_radio(name, idx, inverse) {
const row = document.createElement('tr');
let cell = document.createElement('input');
cell.type = 'radio';
cell.id = `value${idx}`;
cell.name = 'vote';
cell.addEventListener('change', (event) => {
event.target.value = (event.target.checked != inverse ? 1 : 0);
});
cell.checked = (idx == 0);
cell.value = (cell.checked != inverse ? 1 : 0);
row.appendChild(cell);
cell = document.createElement('label');
cell.htmlFor = `value${idx}`;
cell.appendChild(document.createTextNode(name));
row.appendChild(cell);
return row;
}
function votesystem_checkbox(name, idx) {
const row = document.createElement('tr');
let cell = document.createElement('input');
cell.type = 'checkbox';
cell.name = cell.id = `value${idx}`;
cell.value = 0;
cell.addEventListener('change', (event) => {
event.target.value = (event.target.checked ? 1 : 0);
});
row.appendChild(cell);
cell = document.createElement('label');
cell.htmlFor = `value${idx}`;
cell.appendChild(document.createTextNode(name));
row.appendChild(cell);
return row;
}
function votesystem_borda(name, idx) {
const row = document.createElement('tr');
const box = document.createElement('div');
box.draggable = true;
box.classList.add('box');
box.id = `value${idx}`;
box.setAttribute('value', idx);
box.addEventListener('dragstart', borda_handleDragStart, false);
box.addEventListener('dragover', borda_handleDragOver, false);
box.addEventListener('dragenter', borda_handleDragEnter, false);
box.addEventListener('dragleave', borda_handleDragLeave, false);
box.addEventListener('dragend', borda_handleDragEnd, false);
box.addEventListener('drop', borda_handleDrop, false);
box.appendChild(document.createTextNode(name));
row.appendChild(box);
return row;
}
function send_vector(votes) {
const D = config.T.length, t = (D + 1) / 2;
const shares = Array.from(votes, (vote) => gen_shamir(vote, D, t, config.p));
let cnt = 0;
config.T.forEach((addr, idx) => {
let conn = new WebSocket(addr);
conn.onopen = function(e) {
conn.send(JSON.stringify({
U: config.U,
V: Array.from(shares, (x) => x[idx])
}));
cnt++;
if (cnt == 3)
console.log("fin");
};
});
}
function send_vote() {
send_vector(Array.from(config.C, (name, idx) => parseInt(document.getElementById(`value${idx}`).getAttribute('value'))));
}
function abstain() {
send_vector(Array.from(config.C, (name, idx) => 0));
}
</script>
<style>
.container {
display: grid;
grid-template-columns: fit-content(60%);
gap: 5px;
}
.box {
border: 1px solid #666;
background-color: #eee;
border-radius: .5em;
padding: 5px;
cursor: move;
}
.box.over {
border: 2px dotted #666;
}
</style>
</head>
<body>
<form>
<div>
<h3>Candidates:</h3>
<p>
<span>Voter ID:</span>
<input type="text" disabled="disabled" id="voter_id" />
</p>
<table><tbody id="candidates"></tbody></table>
</div>
<input type="button" value="Abstain" onclick="abstain()" />
<input type="button" value="Send" onclick="send_vote()" />
<input type="reset" />
</form>
<p>
<a href="voter.html?data=eyJDIjpbIkFydGh1ciIsIlRhbWlyIiwiTGloaSIsIkF2aXNoYWkiXSwiVCI6WyJ3czovLzEyNy4wLjAuMTo1MDAwLyIsIndzOi8vMTI3LjAuMC4xOjUwMDEvIiwid3M6Ly8xMjcuMC4wLjE6NTAwMi8iXSwiVSI6InJZTm9nUTVWUWk2VkRxdEZQMDNRbUEiLCJMIjoxMCwicCI6MjE0NzQ4MzY0NywiciI6MH0=">Plurality</a>
<a href="voter.html?data=eyJDIjpbIkFydGh1ciIsIlRhbWlyIiwiTGloaSIsIkF2aXNoYWkiXSwiVCI6WyJ3czovLzEyNy4wLjAuMTo1MDAwLyIsIndzOi8vMTI3LjAuMC4xOjUwMDEvIiwid3M6Ly8xMjcuMC4wLjE6NTAwMi8iXSwiVSI6InJZTm9nUTVWUWk2VkRxdEZQMDNRbUEiLCJMIjoxMCwicCI6MjE0NzQ4MzY0NywiciI6MX0=">Range</a>
<a href="voter.html?data=eyJDIjpbIkFydGh1ciIsIlRhbWlyIiwiTGloaSIsIkF2aXNoYWkiXSwiVCI6WyJ3czovLzEyNy4wLjAuMTo1MDAwLyIsIndzOi8vMTI3LjAuMC4xOjUwMDEvIiwid3M6Ly8xMjcuMC4wLjE6NTAwMi8iXSwiVSI6InJZTm9nUTVWUWk2VkRxdEZQMDNRbUEiLCJMIjoxMCwicCI6MjE0NzQ4MzY0NywiciI6Mn0=">Approval</a>
<a href="voter.html?data=eyJDIjpbIkFydGh1ciIsIlRhbWlyIiwiTGloaSIsIkF2aXNoYWkiXSwiVCI6WyJ3czovLzEyNy4wLjAuMTo1MDAwLyIsIndzOi8vMTI3LjAuMC4xOjUwMDEvIiwid3M6Ly8xMjcuMC4wLjE6NTAwMi8iXSwiVSI6InJZTm9nUTVWUWk2VkRxdEZQMDNRbUEiLCJMIjoxMCwicCI6MjE0NzQ4MzY0NywiciI6M30=">Veto</a>
<a href="voter.html?data=eyJDIjpbIkFydGh1ciIsIlRhbWlyIiwiTGloaSIsIkF2aXNoYWkiXSwiVCI6WyJ3czovLzEyNy4wLjAuMTo1MDAwLyIsIndzOi8vMTI3LjAuMC4xOjUwMDEvIiwid3M6Ly8xMjcuMC4wLjE6NTAwMi8iXSwiVSI6InJZTm9nUTVWUWk2VkRxdEZQMDNRbUEiLCJMIjoxMCwicCI6MjE0NzQ4MzY0NywiciI6NH0=">Borda</a>
</p>
<script>
const url = new URL(window.location.href);
let data = url.searchParams.get("data");
if (data == null) {
} else {
config = JSON.parse(atob(data));
document.getElementById('voter_id').value = config.U;
const table = document.getElementById('candidates');
const gen_func = [
(n, i) => votesystem_radio(n, i, false),
votesystem_range,
votesystem_checkbox,
(n, i) => votesystem_radio(n, i, true),
votesystem_borda
][config.r || 0];
config.C.forEach((name, idx) => {
table.appendChild(gen_func(name, idx, 0));
});
}
</script>
</body>
</html>