-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
712 lines (577 loc) · 23.3 KB
/
index.js
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
console.log("--- ZERO KNOWLEDGE SIM ---");
console.log("Written by Luca D'Angelo and Jenny Long");
console.log("McGill University 2020 \n");
/*
Global Variables
*/
// Input graph parameters
let graph_params = {
graph: {
V: [0,1,2,3,4,5,6,7,8,9,10,11],
//E: [[0,6], [0,7], [7,6], [7,1], [7,8], [1,8], [1,4], [8,2], [8,9], [2,9], [2,5], [9,10], [9,3], [3,10], [10,11], [10,4], [4,11], [11,5], [11,6], [5,6]],
E: [[0,6], [0,7], [6,7], [1,7], [7,8], [1,8], [1,4], [2,8], [8,9], [2,9], [2,5], [9,10], [3,9], [3,10], [10,11], [4,10], [4,11], [5,11], [6,11], [5,6]],
compute_E: [[0,6], [0,7], [6,7], [1,7], [7,8], [1,8], [1,4], [2,8], [8,9], [2,9], [2,5], [9,10], [3,9], [3,10], [10,11], [4,10], [4,11], [5,11], [6,11], [5,6]]
},
graph_colors: {0: "brown", 1: "yellow", 2: "blue"}
};
// characters
let characters = [];
let provers = [];
let verifiers = [];
const char_diam = 30;
// verifiers
let v1, v2;
const v1_x = 100;
const v1_y = 100;
const v2_x = 450;
const v2_y = 450;
// provers
let p1, p2;
const p1_x = 150;
const p1_y = 150;
const p2_x = 400;
const p2_y = 400;
// requests
let request_sent_toggle = false;
let automatic_request_sent_toggle = false;
const info_diam = 0;
const info_speed = 3;
const user_request_name = "User Request";
// Simulation Instance and DisplayGraph Instance
let sim_reset_toggle = false;
let iterations = 100;
let frameRate = 30;
let zerosim = new Simulation("Zero-Knowledge-Sim", 0, graph_params.graph, graph_params.graph_colors);
let displaygraph = new DisplayGraph("Star Graph", 0, "Star", 86.6, 250, 86.6, 50, graph_params.graph, graph_params.graph_colors);
// Log color code
console.log("COLOR CODE: 0 => RED, 1 => GREEN, 2 => BLUE");
// Buttons
let buttons = {
cases: {
honest_case: null,
dishonest_case: null,
},
modes: {
regular_mode: null,
automatic_mode: null
},
tests: {
request: null,
edge_verification: null,
well_definition: null,
},
user_select: {
node_i_r: null,
node_j_s: null
}
};
// Text
let text = {
instructions: {
instruction_1: null,
instruction_2: null,
instruction_3: null,
instruction_4: null
},
user_select: {
node_i_r: null,
node_j_s: null
},
table: {
prover_1: null,
prover_2: null,
node_i: null,
node_j: null,
node_ir: null,
node_js: null,
node_ip: null,
node_jp: null,
node_ip_r: null,
node_jp_s: null,
result: null,
init: "-",
commits: {
node_i: null,
node_j: null,
node_ip: null,
node_jp: null
}
}
};
/*
User Options Canvas
*/
let options = new p5(s1 => {
s1.setup = () => {
//console.log(text.table.init);
let canvUser = s1.createCanvas(1300, 300);
s1.frameRate(frameRate);
// Instructions to user
{
text.instructions.instruction_1 = s1.createElement("h5", "1. Choose a case:");
text.instructions.instruction_1.position(20, 0);
text.instructions.instruction_2 = s1.createElement("h5", "2. Choose a mode:");
text.instructions.instruction_2.position(text.instructions.instruction_1.x, text.instructions.instruction_1.y + 50);
text.instructions.instruction_3 = s1.createElement("h5", "3. Regular Mode - Choose 2 nodes with r and s values:");
text.instructions.instruction_3.position(text.instructions.instruction_2.x, text.instructions.instruction_2.y + 50);
text.instructions.instruction_4 = s1.createElement("h5", "4. Regular Mode - Action:");
text.instructions.instruction_4.position(text.instructions.instruction_3.x, text.instructions.instruction_3.y + 50);
}
// Honest prover button
{
buttons.cases.honest_case = s1.createButton('Honest Prover');
buttons.cases.honest_case.position(150, 20);
buttons.cases.honest_case.style('font-size', '14px');
buttons.cases.honest_case.style('background-color', s1.color(255));
buttons.cases.honest_case.style('color: black');
buttons.cases.honest_case.mouseClicked(() => {
caseButtonClicked("HONEST-CASE");
});
}
// Dishonest prover button
{
buttons.cases.dishonest_case = s1.createButton('Dishonest Prover');
buttons.cases.dishonest_case.position(265, buttons.cases.honest_case.y);
buttons.cases.dishonest_case.style('font-size', '14px');
buttons.cases.dishonest_case.style('background-color', s1.color(255));
buttons.cases.dishonest_case.style('color: black');
buttons.cases.dishonest_case.mouseClicked(() => {
caseButtonClicked("DISHONEST-CASE");
});
}
// Regular Mode button
{
buttons.modes.regular_mode = s1.createButton('Regular');
buttons.modes.regular_mode.position(150, 70);
buttons.modes.regular_mode.style('font-size', '14px');
buttons.modes.regular_mode.style('background-color', s1.color(255));
buttons.modes.regular_mode.style('color: black');
buttons.modes.regular_mode.mouseClicked(() => {
modeButtonClicked("REGULAR-MODE");
});
}
// Automatic Mode button
{
buttons.modes.automatic_mode = s1.createButton('Automatic');
buttons.modes.automatic_mode.position(buttons.modes.regular_mode.x + 75, buttons.modes.regular_mode.y);
buttons.modes.automatic_mode.style('font-size', '14px');
buttons.modes.automatic_mode.style('background-color', s1.color(255));
buttons.modes.automatic_mode.style('color: black');
buttons.modes.automatic_mode.mouseClicked(() => {
modeButtonClicked("AUTOMATIC-MODE");
});
}
// Randomness options text
{
text.user_select.node_i_r = s1.createElement("h5", "r");
text.user_select.node_j_s = s1.createElement("h5", "s");
text.user_select.node_i_r.position(375, 100);
text.user_select.node_j_s.position(425, 100);
}
// Randomness select buttons
{
buttons.user_select.node_i_r = s1.createSelect();
buttons.user_select.node_j_s = s1.createSelect();
buttons.user_select.node_i_r.position(385, 120);
buttons.user_select.node_j_s.position(435, 120);
buttons.user_select.node_i_r.option('1');
buttons.user_select.node_i_r.option('2');
buttons.user_select.node_j_s.option('1');
buttons.user_select.node_j_s.option('2');
}
// Request button
{
buttons.tests.request = s1.createButton('Request');
buttons.tests.request.position(190, 170);
buttons.tests.request.style('font-size', '14px');
buttons.tests.request.style('background-color', s1.color(255));
buttons.tests.request.style('color: black');
buttons.tests.request.id('comButton');
buttons.tests.request.mouseClicked(() => {
testButtonClicked("REQUEST");
});
}
// Edge verification button
{
buttons.tests.edge_verification = s1.createButton('Edge Verification Test');
buttons.tests.edge_verification.position(190, buttons.tests.request.y + buttons.tests.request.height + 10);
buttons.tests.edge_verification.style('font-size', '14px');
buttons.tests.edge_verification.style('background-color', s1.color(255));
buttons.tests.edge_verification.style('color: black');
buttons.tests.edge_verification.mouseClicked(() => {
testButtonClicked("FORCED-EDGE-VERIFICATION");
});
}
// Well definition button
{
buttons.tests.well_definition = s1.createButton('Well-Definition Test');
buttons.tests.well_definition.position(190, buttons.tests.edge_verification.y + buttons.tests.edge_verification.height + 10);
buttons.tests.well_definition.style('font-size', '14px');
buttons.tests.well_definition.style('background-color', s1.color(255));
buttons.tests.well_definition.style('color: black');
buttons.tests.well_definition.mouseClicked(() => {
testButtonClicked("FORCED-WELL-DEFINITION");
});
}
// Output table
{
text.table.prover_1 = s1.createElement('h5', "Prover 1");
text.table.prover_1.position(600, 40);
text.table.prover_2 = s1.createElement('h5', "Prover 2");
text.table.prover_2.position(600, text.table.prover_1.y + text.table.prover_1.height * 2);
}
// Output table node names
{
text.table.node_i = s1.createElement('h5', "Node i");
text.table.node_j = s1.createElement('h5', "Node j");
text.table.node_ip = s1.createElement('h5', "Node i'");
text.table.node_jp = s1.createElement('h5', "Node j'");
text.table.node_i_r = s1.createElement('h5', "r");
text.table.node_j_s = s1.createElement('h5', "s");
text.table.node_ip_r = s1.createElement('h5', "r'");
text.table.node_jp_s = s1.createElement('h5', "s'");
text.table.node_i.position(700, 5);
text.table.node_j.position(800, 5);
text.table.node_ip.position(900, 5);
text.table.node_jp.position(1000, 5);
text.table.node_i_r.position(1100, 5);
text.table.node_j_s.position(1150, 5);
text.table.node_ip_r.position(1200, 5);
text.table.node_jp_s.position(1250, 5);
}
// Output table commit values for each node
{
text.table.commits.node_i = s1.createElement('h5', text.table.init);
text.table.commits.node_i.position(text.table.node_i.x, text.table.prover_1.y);
text.table.commits.node_j = s1.createElement('h5', text.table.init);
text.table.commits.node_j.position(text.table.node_j.x, text.table.prover_1.y);
text.table.commits.node_ip = s1.createElement('h5', text.table.init);
text.table.commits.node_ip.position(text.table.node_ip.x, text.table.prover_2.y);
text.table.commits.node_jp = s1.createElement('h5', text.table.init);
text.table.commits.node_jp.position(text.table.node_jp.x, text.table.prover_2.y);
}
};
s1.draw = () => {
s1.background(220);
}
}, "user-canvas-container");
/*
Graph Canvas
*/
let graph = new p5(s2 => {
let show_3col;
s2.setup = () => {
let graph_canv = s2.createCanvas(500, 500);
show_3col = s2.createButton("Show 3 Coloring");
show_3col.position(10, 475);
show_3col.mouseClicked(() => {
show3Col();
});
};
s2.draw = () => {
s2.background(220);
displaygraph.renderDisplayGraph(s2);
};
/**
* Function called on mouse click event.
*/
s2.mouseClicked = function() {
// Change node color and log selected nodes when clicking
displaygraph.checkCellClicking(s2);
};
}, "graph-canvas-container");
let simulation = new p5(s3 => {
let reset_button;
s3.setup = function() {
s3.createCanvas(500, 500);
s3.frameRate(frameRate);
s3.textSize(30);
// Defining the characters
v1 = new Verifier("V1", 0, v1_x, v1_y, char_diam, "purple", "P1", "V2", s3);
v2 = new Verifier("V2", 1, v2_x, v2_y, char_diam, "pink", "P2", "V1", s3);
p1 = new Prover("P1", 0, p1_x, p1_y, char_diam, "orange", "V1", "P2", s3);
p2 = new Prover("P2", 1, p2_x, p2_y, char_diam, "black", "V2", "P1", s3);
verifiers = [v1, v2];
provers = [p1, p2];
characters = verifiers.concat(provers);
reset_button = s3.createButton("RESET");
reset_button.position(10, 475);
reset_button.mouseClicked(resetSimulation);
//request_button.mouseClicked(addInfoFromRequestButton());
};
s3.draw = function() {
s3.background(220);
s3.text(s3.frameCount, 500, 50);
// Display characters
displayVerifiers();
displayProvers();
// Check test has been chosen by user
if ( request_sent_toggle ) {
// Add request to verifiers
addInfoFromTestButton();
// Display the node information data to the table
displayNodeInfoToTable(zerosim.simulation_params);
// Display colors on display graph
displayNodeInfoToGraph(zerosim.simulation_params);
// Change the toggle
request_sent_toggle = false;
}
// Check if automatic mode was chosen by user
if ( automatic_request_sent_toggle ) {
for(let i = 0; i < iterations; i++) {
if (Verifier.sim_stopped_toggle === true || sim_reset_toggle === true) {
automatic_request_sent_toggle = false;
break;
}
// Add request to verifiers
addInfoFromTestButton();
// Display the node information data to the table
setTimeout(function() {
// Display the node information data to the table
displayNodeInfoToTable(zerosim.simulations[i]);
// Display colors on display graph
displayNodeInfoToGraph(zerosim.simulations[i]);
}, 1000 * i);
}
automatic_request_sent_toggle = false;
}
};
/**
* Function to called when mouse click is pressed.
*/
s3.mousePressed = function() {
if (characters.length > 0) {
for(let i in characters) {
characters[i].characterIsPressed();
}
}
}
/**
* Function called when mouse click is released.
*/
s3.mouseReleased = function() {
for(let i in characters) {
characters[i].characterIsReleased();
}
}
}, "simulation-canvas-container");
/*
Helper Methods
*/
/**
* Display node information to table
* @param {Object} simulation_params Object that includes all of the simulation parameters.
*/
function displayNodeInfoToTable( simulation_params ) {
// Display nodes chosen from both verifiers
text.table.node_i.html("Node i = " + simulation_params.characters.verifiers.automated_v1.request.selected_edge[0]);
text.table.node_j.html("Node j = " + simulation_params.characters.verifiers.automated_v1.request.selected_edge[1]);
text.table.node_ip.html("Node i' = " + simulation_params.characters.verifiers.automated_v2.request.selected_edge[0]);
text.table.node_jp.html("Node j' = " + simulation_params.characters.verifiers.automated_v2.request.selected_edge[1]);
// Display randomness values for automated_verifier_2
text.table.node_i_r.html("r = " + simulation_params.characters.verifiers.automated_v1.request.edge_randomness[0]);
text.table.node_j_s.html("s = " + simulation_params.characters.verifiers.automated_v1.request.edge_randomness[1]);
text.table.node_ip_r.html("r' = " + simulation_params.characters.verifiers.automated_v2.request.edge_randomness[0]);
text.table.node_jp_s.html("s' = " + simulation_params.characters.verifiers.automated_v2.request.edge_randomness[1]);
// Display verifiers accept or decline commit values based on edge-verification and well-definition tests
if (simulation_params.result.edge_verification_status || simulation_params.result.well_definition_status) {
text.table.commits.node_i.style('color', 'green');
text.table.commits.node_j.style('color', 'green');
text.table.commits.node_ip.style('color', 'green');
text.table.commits.node_jp.style('color', 'green');
} else {
text.table.commits.node_i.style('color', 'red');
text.table.commits.node_j.style('color', 'red');
text.table.commits.node_ip.style('color', 'red');
text.table.commits.node_jp.style('color', 'red');
}
// Display commit values
text.table.commits.node_i.html(simulation_params.characters.provers.automated_p1.commit.node_i);
text.table.commits.node_j.html(simulation_params.characters.provers.automated_p1.commit.node_j);
text.table.commits.node_ip.html(simulation_params.characters.provers.automated_p2.commit.node_ip);
text.table.commits.node_jp.html(simulation_params.characters.provers.automated_p2.commit.node_jp);
}
/**
* Display node information to graph
* @param {Object} simulation_params Object that includes all of the simulation parameters.
*/
function displayNodeInfoToGraph( simulation_params ) {
// Reset the display graph
displaygraph.resetDisplayGraphProperties();
displaygraph.resetDisplayGraphCells();
// Display node color if possible
let colorings = simulation_params.result.node_colorings;
if ( colorings ) {
for (let i = 0; i < colorings.length; i++) {
let cell = colorings[i][0];
let color = colorings[i][1];
displaygraph.revealCellColor(cell, color);
}
}
}
/**
* Method called when case button is clicked prover case button is clicked
* @param {string} CASE Case type, one of: "HONEST-CASE" | "DISHONEST-CASE"
*/
function caseButtonClicked( CASE ) {
switch ( CASE ) {
case "HONEST-CASE":
zerosim.simulation_case = "HONEST-CASE";
zerosim.removeDishonestEdgeSTAR();
displaygraph.removeDishonestEdgeSTAR();
console.log("HONEST PROVER CASE");
break;
case "DISHONEST-CASE":
zerosim.simulation_case = "DISHONEST-CASE";
zerosim.addDishonestEdgeSTAR();
displaygraph.addDishonestEdgeSTAR();
console.log("DISHONEST PROVER CASE");
break;
default:
console.log("Please pick a case!");
alert("Please pick a case!");
}
}
/**
* Function called when mode button is clicked.
* @param {string} MODE Mode type, one of: "REGULAR-MODE" | "AUTOMATIC-MODE"
*/
function modeButtonClicked( MODE ) {
switch ( MODE ) {
case "REGULAR-MODE":
zerosim.simulation_mode = "REGULAR-MODE";
console.log("REGULAR-MODE");
break;
case "AUTOMATIC-MODE":
zerosim.simulation_mode = "AUTOMATIC-MODE";
console.log("AUTOMATIC MODE");
// Run the simulation
zerosim.runAutomaticSimulation(iterations);
console.log(zerosim.simulations);
// Change the automatic request toggle
automatic_request_sent_toggle = true;
break;
default:
console.log("Mode error!");
alert("Mode error!");
}
}
/**
* Method called when one of the three test buttons are clicked.
* @param {string} TEST Test type, one of: "REQUEST" | "FORCED-EDGE-VERIFICATION" | "FORCED-WELL-DEFINITION"
*/
function testButtonClicked( TEST ) {
// Check if and edge has been selected
if (displaygraph.selected_cells.length === 0) {
console.log("Please select an edge!");
alert("Please select an edge!");
} else {
// Get the user request information
let user_selected_request = {
edge: Simulation.sort_ascending(displaygraph.selected_cells_id),
edge_randomness: [parseInt(buttons.user_select.node_i_r.value()), parseInt(buttons.user_select.node_j_s.value())]
};
// Run the simulation
zerosim.runSingleSimulation(TEST, user_selected_request);
console.log(zerosim.simulation_params);
// Change the request toggle
request_sent_toggle = true;
}
}
/**
* Method to reveal the graph 3 coloring when show_3col button is clicked.
*/
function show3Col() {
// Display node colors
if (zerosim.simulation_params.coloring !== null) {
for (let i = 0; i < zerosim.simulation_params.coloring.length; i++) {
let cell = i;
let color = zerosim.simulation_params.graph_colors[zerosim.simulation_params.coloring[i]];
displaygraph.revealCellColor(cell, color);
}
} else {
console.log("Send a request first!");
alert("Send a request first!");
}
}
/**
* Generate and emit Information from Character instance when clicking on one of the test buttons.
*/
function addInfoFromTestButton() {
v1.addSingleInformationFromUser(generateInformation(v1));
v2.addSingleInformationFromUser(generateInformation(v2));
}
/**
* Generate some dummy RequestInfo instances.
* @param {Character} character
* @param {string} color
* @returns {RequestInfo}
*/
function generateInformation(character) {
return new RequestInfo(user_request_name, Math.random(), [Math.random(), Math.random(), Math.random(), Math.random()], character.getCenterX(), character.getCenterY(), info_diam, info_speed, character.getColor(), simulation);
}
/**
* Reset the simulation.
*/
function resetSimulation() {
simulation.noLoop();
for(let i in characters) {
let char = characters[i];
// Reset the position.
char.setCenterX(char.getInitCenterX());
char.setCenterY(char.getInitCenterY());
// Reset the information.
char.resetQueuedInformation();
char.resetDisplayedInformation();
}
simulation.loop();
sim_reset_toggle = true;
}
/**
* Display all of the verifiers required for the simulation.
*/
function displayVerifiers() {
if (verifiers.length > 0) {
for(let i in verifiers) {
let verifier = verifiers[i];
verifier.displayCharacter(); // Display prover on the screen every frame.
// Check for commits from paired provers.
for(let j in provers) {
let prover = provers[j];
// Check if verifier and prover are linked together.
if (verifier.isProverLinked(prover)) {
verifier.scanForCommits(prover.getDisplayedInformations());
}
}
// Check for requests from paired verifiers.
for(let k in verifiers) {
let compare_verifier = verifiers[k];
// Check if verifier pairs are linked together.
if (verifier.isVerifierLinked(compare_verifier)) {
verifier.scanForPairedRequests(compare_verifier.getDisplayedInformations());
}
}
verifier.emitInformation();
}
}
}
/**
* Display all provers required for the simulation.
*/
function displayProvers() {
if(provers.length > 0) {
for(let i in provers) {
let prover = provers[i];
prover.displayCharacter(); // Display prover on the screen every frame.
// Check for requests from paired verifiers.
for(let j in verifiers) {
let verifier = verifiers[j];
// Check if prover and verifier are linked together.
if (prover.isVerifierLinked(verifier)) {
prover.scanForRequests(verifier.getDisplayedInformations()); // Scan for requests from linked verifier.
}
}
prover.emitInformation(); // Emit any corresponding commits.
}
}
}