Uncompressed Bitcoin Public Key Generator

Generate private and public keys.

by Jun Wang

HTML

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Test</title>
    <link rel="stylesheet" href="test.css">
  </head>
  <body>
    <p>Choose your private key:</p>
    <form name="bitcoinPrivateKeyForm1">
        <input type="range" name="privateKeyInput1" id="privateKeyInputId1"
          value="5974818946285"
          min="0" max="11580000000000"
          oninput="privateKeyOutputId1.value = bigInt(privateKeyInputId1.value)"
          onchange="document.getElementById('privateKey1').innerHTML = privateKeyInputId1.value"
          class="slider">
        <output name="privateKeyOutput1" id="privateKeyOutputId1">
          5974818946285
        </output>
    </form>
    <form name="bitcoinPrivateKeyForm2">
        <input type="range" name="privateKeyInput2" id="privateKeyInputId2"
          value="0"
          min="0" max="9999999999999999"
          oninput="privateKeyOutputId2.value = bigInt(privateKeyInputId2.value)"
          onchange="document.getElementById('privateKey2').innerHTML = privateKeyInputId2.value.toString().padStart(16, '0')"
          class="slider">
        <output name="privateKeyOutput2" id="privateKeyOutputId2">
          0
        </output>
    </form>
    <form name="bitcoinPrivateKeyForm3">
        <input type="range" name="privateKeyInput3" id="privateKeyInputId3"
          value="0"
          min="0" max="9999999999999999"
          oninput="privateKeyOutputId3.value = bigInt(privateKeyInputId3.value)"
          onchange="document.getElementById('privateKey3').innerHTML = privateKeyInputId3.value.toString().padStart(16, '0')"
          class="slider">
        <output name="privateKeyOutput3" id="privateKeyOutputId3">
          0
        </output>
    </form>
    <form name="bitcoinPrivateKeyForm4">
        <input type="range" name="privateKeyInput4" id="privateKeyInputId4"
          value="0"
          min="0" max="9999999999999999"
          oninput="privateKeyOutputId4.value =...

CSS

.slider {
  width: 30%; /* Full-width */
}

.keys {
  color: blue;
}

#errorMessage{
  color: red;
}

/* 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: Import [BigInteger.js](https://github.com/peterolson/BigInteger.js).
// It has a function mod(number) and modInv(mod).

function ECdouble(G,a,b,P){
	// G = (x1,y1) where x1,y1 are integers.
  var lambda_mod = bigInt(bigInt(bigInt(bigInt(bigInt(G[0]).pow(2)).multiply(3)).add(a)).multiply(bigInt(bigInt(G[1]).multiply(2)).modInv(P))).mod(P);
  var x2 = bigInt(bigInt(bigInt(bigInt(lambda_mod).square()).minus(G[0])).minus(G[0])).mod(P);
  var y2 = bigInt(bigInt(bigInt(lambda_mod).multiply(bigInt(G[0]).minus(x2))).minus(G[1])).mod(P);
  if (bigInt(x2).lesser(0)){
    x2 = bigInt(x2).add(P);
  }
  if (bigInt(y2).lesser(0)){
    y2 = bigInt(y2).add(P);
  }
  var double = [x2,y2];
  return double
}

function ECadd(A,B,P){
	var lambda_mod2 = bigInt(bigInt(bigInt(B[1]).minus(A[1])).multiply(bigInt(bigInt(B[0]).minus(A[0])).modInv(P))).mod(P);
  var x3 = bigInt(bigInt(bigInt(bigInt(lambda_mod2).square()).minus(A[0])).minus(B[0])).mod(P);
  var y3 = bigInt(bigInt(bigInt(lambda_mod2).multiply(bigInt(bigInt(A[0]).minus(x3)))).minus(A[1])).mod(P);
  if (bigInt(x3).lesser(0)){
    x3 = bigInt(x3).add(P);
  }
  if (bigInt(y3).lesser(0)){
    y3 = bigInt(y3).add(P);
  }
  var add2 = [x3,y3];
  return add2
}

function ECMultiplication(G,a,b,P,N,privateKey){
	/* if (privateKey < 1 || privateKey >= N) {
	  return "ECMultiplication(G,privateKey,a,b,P), privateKey should >0 and <N."
	} */
  var n_binary = bigInt(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);  
    }
	}
  return bigInt(D)
}


 // Feed the button
function feedTheButton() {
  // Set Bitcoin parameters
  var a = 0; var b = 7;
  var P =...