Uncompressed Bitcoin Public Key Generator(small numbers)
Generate uncompressed public key(small numbers).
by Jun Wang
HTML
<p>Choose your private key:</p>
<form name="privateKeyForm">
<input type="range" name="privateKeyInput" id="privateKeyInputId" value="13" min="1" max="27" oninput="privateKeyOutputId.value = privateKeyInputId.value">
<output name="privateKeyOutput" id="privateKeyOutputId">13</output>
</form>
<br>
<button id="b" class="myButton">Get your public key</button>
<p>Your public key is: <span id="publicKey"></span></p>
<p><strong>Note:</strong> This is just a demo.
CSS
#publicKey {
color: blue;
}
/* insane button CSS from http://www.bestcssbuttongenerator.com/ */
.myButton {
-moz-box-shadow: 0px 1px 0px 0px #f0f7fa;
-webkit-box-shadow: 0px 1px 0px 0px #f0f7fa;
box-shadow: 0px 1px 0px 0px #f0f7fa;
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #33bdef), color-stop(1, #019ad2));
background:-moz-linear-gradient(top, #33bdef 5%, #019ad2 100%);
background:-webkit-linear-gradient(top, #33bdef 5%, #019ad2 100%);
background:-o-linear-gradient(top, #33bdef 5%, #019ad2 100%);
background:-ms-linear-gradient(top, #33bdef 5%, #019ad2 100%);
background:linear-gradient(to bottom, #33bdef 5%, #019ad2 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#33bdef', endColorstr='#019ad2',GradientType=0);
background-color:#33bdef;
-moz-border-radius:6px;
-webkit-border-radius:6px;
border-radius:6px;
border:1px solid #057fd0;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:Arial;
font-size:15px;
font-weight:bold;
padding:6px 24px;
text-decoration:none;
text-shadow:0px -1px 0px #5b6178;
}
.myButton:hover {
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #019ad2), color-stop(1, #33bdef));
background:-moz-linear-gradient(top, #019ad2 5%, #33bdef 100%);
background:-webkit-linear-gradient(top, #019ad2 5%, #33bdef 100%);
background:-o-linear-gradient(top, #019ad2 5%, #33bdef 100%);
background:-ms-linear-gradient(top, #019ad2 5%, #33bdef 100%);
background:linear-gradient(to bottom, #019ad2 5%, #33bdef 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#019ad2', endColorstr='#33bdef',GradientType=0);
background-color:#019ad2;
}
.myButton:active {
position:relative;
top:1px;
}
JavaScript
// get the button and make it respond to a click
var theButton = document.getElementById("b");
theButton.onclick = feedTheButton;
// Note: in Java -1 mod 23=-24 mod 23 = -1; in Python -1 mod 23=-24 mod 23 = 22. So we need to define a mod function here.
function mod(c1,c2){
m = (((c1 % c2) + c2) % c2);
return m
}
// Note: A and P must share no factor greater than 1.
function modular_inverse(A,P) {
var p0 = 1; var a0 = 0;
var p1 = 0; var a1 = 1;
var R0 = P; var R1 = A % P;
var n = 0; var R2 = 0;
var p2 = 0; var a2 = 0;
while (R1 != 1 && R1 != 0){
n = Math.floor(R0/R1); R2 = mod(R0,R1);
p2 = p0 - n*p1 ; a2 = a0 - n*a1;
p0 = p1; a0 = a1; p1 = p2; a1 = a2;
R0 = R1; R1 = R2;
}
return mod(a1,P)
}
function ECdouble(G,a,b,P){
// G = (x1,y1) where x1,y1 are integers.
var lambda_mod = mod(((3*G[0]** 2 + a) * modular_inverse(2*G[1],P)), P);
var x2 = mod((lambda_mod * lambda_mod - G[0] - G[0]), P);
var y2 = mod((lambda_mod * (G[0]-x2)-G[1]), P);
var double = [x2,y2];
return double
}
function ECadd(A,B,P){
var lambda_mod = (mod((B[1]-A[1]), P)) * modular_inverse(B[0]-A[0], P);
var x3 = mod((lambda_mod * lambda_mod - A[0] - B[0]), P);
var y3 = mod((lambda_mod * (A[0] - x3) - A[1]), P);
var add = [x3,y3];
return add
}
function ECMultiplication(G,a,b,P,N,privateKey){
var n_binary = privateKey.toString(2);
var D = G;
for (i = 1; i < n_binary.length; i++) {
D = ECdouble(D,a,b,P);
if (n_binary.charAt(i) == 1){
D = ECadd(G,D,P); //Don't use ECadd(D, G, P), I don't know why yet.
}
}
return D
}
// Feed the button
function feedTheButton() {
// An example of small parameters.
var a=1; var b=1; var P=23;
var x1=3; var y1=10; var G = [x1,y1];
var N = 27;
var privateKey = parseInt(privateKeyOutputId.value);
var result = ECMultiplication(G,a,b,P,N,privateKey);
var place = document.getElementById("publicKey");
place.innerHTML = result;
}