Before we get deeper into the concept of Ethereum Blockchain, it is important to understand how Public Key, Private Key and Address are generated. To make this easier for beginners, I am trying to explain the technical concept with an example.
On EC ecp256k1 , any number between 1 to 2^256-1 is a valid private key. A good library generate a private key with taking sufficient randomness into account. For this exercise we will take "1" as private key which is an acceptable private key because it lies in specified range mentioned above. Ethereum requires private key to be 256 bit long. Here is the sample code for nodejs.
var privateKey=Buffer.alloc(32, 0); privateKey=1; console.log("PK::"+privateKey.toString('hex'))
This will print private key in hex which is
Public key is described as follows in yellow paper.
Where pu is the public key, assumed to be a byte array of size 64 (formed from the concatenation of two positive integers each < 2256) and pr is the private key, a byte array of size 32 (or a single positive integer in the aforementioned range).
This is done using group operation of EC cryptography. To derive public key, private key is multiplied by G.Multiplication used to derive public is EC multiplication which is entirely different from normal multiplication for which I am going to use JS library . G is called generator point which is one of the domain parameters of EC cryptography. G has fixed value for ecp256k1, which is recommended by experts. you can read more here.
var EC = require('elliptic').ec; var BN = require('bn.js'); var ec = new EC('secp256k1'); var G = ec.g; // Generator point var pk = new BN('1'); // private key as big number var pubPoint=G.mul(pk); // EC multiplication to determine public point var x = pubPoint.getX().toBuffer(); //32 bit x co-ordinate of public point var y = pubPoint.getY().toBuffer(); //32 bit y co-ordinate of public point var publicKey =Buffer.concat([x,y]) console.log("pub key::"+publicKey.toString('hex'))
Ethereum address is described as follows in yellow paper
For a given private key, pr, the Ethereum address A(pr) (a 160-bit value) to which it corresponds is defined as the right most 160-bits of the Keccak hash of the corresponding ECDSA public key.
To generate Ethereum address, take Keccak-256 hash of public key. Right most 20 bytes is your Ethereum address.
var EC = require('elliptic').ec; var BN = require('bn.js'); var ec = new EC('secp256k1'); const keccak256 = require('js-sha3').keccak256; var privateKey=Buffer.alloc(32, 0); privateKey=1; console.log("PK::"+privateKey.toString('hex')) var G = ec.g; // Generator point var pk = new BN('1'); // private key as big number var pubPoint=G.mul(pk); // EC multiplication to determine public point var x = pubPoint.getX().toBuffer(); //32 bit x co-ordinate of public point var y = pubPoint.getY().toBuffer(); //32 bit y co-ordinate of public point var publicKey =Buffer.concat([x,y]) console.log("public key::"+publicKey.toString('hex')) const address = keccak256(publicKey) // keccak256 hash of publicKey const buf2 = Buffer.from(address, 'hex'); console.log("Ethereum Adress:::"+"0x"+buf2.slice(-20).toString('hex')) // take lat 20 bytes as ethereum adress
This is just an example to understand the concept. Please do not use it to generate real wallet to hold ETH.
If you find this article helpful, you may show your appreciation by sharing it. Also, you may reach me at email@example.com with your comments, questions or suggestions of any other topic that you would want to be covered at EtherWorld.co.
Read similar articles
- Reading Ethereum Geth database (LEVELDB)
- ÐΞVp2p Wire Protocol (Kademlia Distributed Hash Table)
- Introducing web3 commands to browser
- Understanding Ethereum Light Node
- Certificate on IPFS and Ethereum
- Running Ethereum testnet using Geth
- Installing geth on ubuntu 16.10
- Understanding Bitcoin transaction details
- Concept of Merkle Tree in Ethereum
EtherWorld's collection of Good Read on Blockchain & Cryptocurrency.______________________________________________________________________
Disclaimer: The information contained on this web page is for education purpose only. Readers are suggested to conduct their own research, review, analyze and verify the content before relying on them.
To publish press releases, project updates and guest posts with us, please email at firstname.lastname@example.org.
Subscribe to EtherWorld YouTube channel for easy digestable content.
Support us at Gitcoin
You've something to share with the blockchain community, join us on Discord!