-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
145 lines (137 loc) · 6.34 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Fair-coin-flip by devm33</title>
<link rel="stylesheet" href="stylesheets/styles.css">
<link rel="stylesheet" href="stylesheets/pygment_trac.css">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="stylesheets/jquery-ui-1.10.4.custom.min.css" rel="stylesheet">
</head>
<body>
<div class="wrapper">
<header>
<h1>Fair-coin-flip</h1>
<p>Electronically flips a fair coin between two people</p>
<p class="view"><a href="https://github.com/devm33/fair-coin-flip">View the Project on GitHub <small>devm33/fair-coin-flip</small></a></p>
</header>
<section id="content">
<p class="title">Bob and Alice will simulate a fair coin flip through a short exchange.</p>
<div id="start">
<p>Alice slide to pick the number of digits for your primes.</p>
<div id="digit-slider" class="slider"></div>
<p>Then click here to generate primes of <input type="button" id="compute-primes" value="5"></input> digits.</p>
</div>
<div id="primes" style="display:none;">
<p><code>n = <span class="n"></span></code> is the product of Alice's primes which is now given to Bob</p>
<p>Bob, choose a positive integer less than <code>n</code> to use as your root.</p>
<p>Slide to choose a root, and then, click here to send its square to Alice <code>x<sup>2</sup> = </code><input type="button" id="bobs-square" value="4" /></p>
<div id="root-slider" class="slider"></div>
</div>
<div id="roots" style="display:none;">
<p>Bob's chosen square is <code>a = <span class="a"></span></code>.</p>
<p>Alice using her prime factors of <code>n</code> is able to solve <code>a = x<sup>2</sup> (mod n)</code> for the four possible values of <code>x</code>.</p>
<p>Alice, choose one of these square roots to send back to Bob</p>
<input type="button" id="root0" class="root" /> or <input type="button" id="root1" class="root" /> or
<input type="button" id="root2" class="root" /> or <input type="button" id="root3" class="root" />
<p>(Note: two of these are simply negations of each other)</p>
</div>
<div id="finish" style="display:none;">
<p id="bob-wins">Since Alice chose a root of <code>a</code> that Bob did not know, Bob now knows all roots and is able to factor <code>n</code> quickly, so Bob wins the coin flip.</p>
<p id="alice-wins">Since Alice chose a root of <code>a</code> that Bob already knew, Bob does not have enough information to factor <code>n</code> quickly, so Alice wins the coin flip.</p>
<p>Alice's prime factors were <code>p = <span id="p"></span>, q = <span id="q"></span></code></p>
<p>Bob's root was <code>x = <span id="x"></span></code></p>
<input type="button" id="restart" value="Click here to flip again" />
</div>
</form>
</section>
<footer>
<p>This project is maintained by <a href="https://github.com/devm33">devm33</a></p>
<p><small>Hosted on GitHub Pages — Theme by <a href="https://github.com/orderedlist">orderedlist</a></small></p>
</footer>
</div>
<script src="javascripts/scale.fix.js"></script>
<script src="fair-coin-flip.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="javascripts/jquery-ui-1.10.4.custom.min.js"></script>
<script>
jQuery(function($){
/* Sections to be displayed */
var start = $('#start');
var primes = $('#primes');
var roots = $('#roots');
var finish = $('#finish');
/* Variables for the coin flip */
var d, p, q, n, a, x, r, b, i;
/* Initialize the two sliders */
$('#digit-slider').slider({ min: 2, value: 5,
max: 8, /* Since it is not well-supported to go over 16 digit integers in browsers, prime factors must not exceed 8 digits */
slide: function(e, ui) { /* might want slide here? */
$('#compute-primes').val(ui.value);
}
});
$('#root-slider').slider({ min: 1, value: 2,
slide: function(e, ui) {
x = +(ui.value);
a = (x * x) % n;
$('#bobs-square').val(a);
}
});
/* Bind listeners */
$('#content').on('click', '#compute-primes', compute_primes)
.on('click', '#bobs-square', bob_choose_root)
.on('click', 'input.root', alice_choose_root)
.on('click', '#restart', restart);
function compute_primes() {
start.hide();
d = +(this.value);
p = generate_prime_of_length(d);
q = generate_prime_of_length(d);
while(p == q) {
q = generate_prime_of_length(d);
}
n = p * q;
$('#p').text(p);
$('#q').text(q);
$('.n').text(n);
$('#root-slider').slider('option', {'max': n - 1, 'value': 2});
$('#bobs-square').val(4);
primes.show();
}
function bob_choose_root() {
primes.hide();
x = +(this.value);
a = (x * x) % n;
$('#x').text(x);
$('.a').text(a);
r = square_root_modpq(a, p, q);
for(i = 0; i < 4; i++) {
$('#root'+i).val(r[i]);
}
roots.show();
}
function alice_choose_root() {
roots.hide();
b = +(this.value);
if(x == b || n - x == b) {
$('#bob-wins').hide();
$('#alice-wins').show();
}
else {
$('#alice-wins').hide();
$('#bob-wins').show();
}
finish.show();
}
function restart() {
finish.hide();
start.show();
}
});
</script>
</body>
</html>