-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcurve.html
244 lines (221 loc) · 6.27 KB
/
curve.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
231
232
233
234
235
236
237
238
239
240
241
242
243
<html>
<head>
<title>Graphs of small elliptic curves</title>
<style>
.w50 { width:100px; display: inline-block; }
</style>
<meta charset=utf-8 />
<meta http-equiv=“Pragma” content=”no-cache”>
<meta http-equiv=“Expires” content=”-1″>
<meta http-equiv=“CACHE-CONTROL” content=”NO-CACHE”>
<meta name="keywords" content="ellipticcurve,example,calculations,math">
<meta name="author" content="Willem Hengeveld, itsme@xs4all.nl">
<meta name="description" content="Graphs of small elliptic curves.">
<script src="bignum.js"></script>
<script src="primes.js"></script>
<script src="gfp.js"></script>
<script src="ec.js"></script>
<script src="ecdsa.js"></script>
<script src="utils.js"></script>
<script src="jsutils.js"></script>
<script language=javascript>
'use strict';
var g_p; var g_a; var g_b; // the curve parameters.
var ec; // the curve.
var curvepoints = []; // the list of all points on the curve.
var g_mouse_point; // selected with 'mouse-over'
var g_start; // the starting point, selected by hovering over the grid.
var g_nrlines=10; // how many lines to draw from the starting point
var pointlookup = {};
var primelist = Array.from(genprimes(100000));
function loadec(p, a, b)
{
var F = new GaloisField(p);
var newec = new EllipticCurve(F, a, b);
if (!ec || !ec.equalscurve(newec)) {
g_start = undefined;
curvepoints = [];
pointlookup = {};
if (ec)
console.log("curve changed", ec.field.p, ec.a, ec.b, " -> ", newec.field.p, newec.a, newec.b);
ec = newec;
calcpoints();
}
else if (g_mouse_point) {
g_start = g_mouse_point;
console.log("using mouse point");
}
drawgrid();
drawpath();
//loadpoints();
}
function calcpoints()
{
curvepoints = [ec.infinity()];
for (var x = 0 ; x<g_p ; x++)
{
var ysquared = x*x*x+g_a*x+g_b;
var y0 = ec.field.value(ysquared).sqrt(0);
var y1 = ec.field.value(ysquared).sqrt(1);
if (typeof(y0)!="undefined")
{
curvepoints.push(ec.point(x, y0));
if (!y0.equals(y1))
curvepoints.push(ec.point(x, y1));
}
}
g_start = curvepoints[Math.floor(curvepoints.length/2)];
for (var pt of curvepoints)
pointlookup[indexkey(pt)] = pt;
}
function indexkey(p)
{
if (p.isinf())
return "(*,*)";
return "("+Math.round(p.x.uint()/5).toString(10)+","+Math.round(p.y.uint()/5).toString(10)+")";
}
function findpoint(x, y)
{
var ix = "("+Math.round(x/5).toString(10)+","+Math.round(y/5).toString(10)+")";
return pointlookup[ix];
}
function drawpoint(ctx, pt)
{
if (pt.isinf())
return;
ctx.fillRect(pt.x.uint()-1, pt.y.uint()-1, 3, 3);
}
function startline(ctx, p)
{
if (p.isinf())
return;
ctx.moveTo(p.x.uint(), p.y.uint());
}
function drawlineto(ctx, p)
{
if (p.isinf())
return;
ctx.lineTo(p.x.uint(), p.y.uint());
}
function drawgrid()
{
setnrpoints(curvepoints.length);
var canvas = document.getElementById("points");
canvas.width = g_p;
canvas.height = g_p;
var ctx = canvas.getContext("2d")
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var pt of curvepoints)
{
drawpoint(ctx, pt);
}
}
function drawpath()
{
if (!g_start) return;
if (!ec.isoncurve(g_start))
console.log("g_start is not on curve");
var canvas = document.getElementById("points");
var ctx = canvas.getContext("2d")
ctx.beginPath();
var p = g_start;
startline(ctx, p);
for (var i=0 ; i<g_nrlines ; i++)
{
p = p.add(g_start);
drawlineto(ctx, p);
if (p.equals(g_start)) {
set_cycle_length(i);
break;
}
}
ctx.stroke();
}
function loadpoints()
{
for (var a of curvepoints)
for (var b of curvepoints)
{
}
}
function update()
{
loadec(g_p, g_a, g_b);
set_ab_check((4*g_a**3+27*g_b**2)%g_p);
}
function makesliders()
{
return [
makeslider("prime:", "prime", 0, 200, 30, t => { g_p = primelist[Number(t)]; }, ()=>g_p),
makeslider("a:", "a", -16, 16, 0, t=>{ g_a = Number(t); }, ()=>g_a),
makeslider("b:", "b", -16, 16, 7, t=>{ g_b = Number(t); }, ()=>g_b),
makeslider("nlines:", "n", 0, 1000, 10, t=>{ g_nrlines = Number(t); }, ()=>g_nrlines),
];
}
function start()
{
var div = document.getElementById("controlsdiv");
div.append(...makesliders());
var fp = document.getElementById("points");
fp.onmousemove = function(e) {
var pt = findpoint(e.offsetX, e.offsetY);
if (pt) {
g_mouse_point = pt;
update();
}
};
update();
}
function setnrpoints(n)
{
var av =document.getElementById("order.value");
if (primelist.indexOf(n)>=0)
av.innerHTML = n + " (prime)";
else
av.innerHTML = n;
}
function set_cycle_length(n)
{
var av =document.getElementById("cycle.value");
av.innerHTML = n;
}
function set_ab_check(n)
{
var av =document.getElementById("abcheck.value");
av.innerHTML = n;
}
</script>
</head>
<body onLoad="start()">
Menu:
<a href="ecdsacrack.html">crack demo</a>
<a href="linearequations.html">using linear algebra</a>
<a href="calculator.html">curve calculator</a>
<a href="curve.html">curve demo</a>
<a href="transaction.html">bitcoin transaction</a>
<a href="unittest.html">unittest</a><br>
Author: Willem Hengeveld, <a href="mailto:itsme@xs4all.nl">itsme@xs4all.nl</a>,
Source: <a href="https://github.com/nlitsme/bitcoinexplainer">on github</a>.
<p>
This demonstrates what a finite field elliptic curve looks like.<br>
The curve formula is: <code>y<sup>2</sup> = x<sup>3</sup> + a x + b</code>. The 'ab-check' should be non-zero for a valid curve.
The lines are generated by starting at an arbitrary point, and keep adding it to itself.
<div id="controlsdiv"></div>
<div>
<span>order:</span> <span class=w50 id="order.value"></span>
<span>cycle:</span> <span class=w50 id="cycle.value"></span>
<span>ab-check:</span> <span class=w50 id="abcheck.value"></span>
</div>
<table>
<tr>
<td>
<canvas id="points"></canvas>
</td>
<td>
</td>
</tr>
</table>
<div id="xygrid"></div>
<div id="ptgrid"></div>
</body>
</html>