Post
Topic
Board Bitcoin Technical Support
Merits 1 from 1 user
Re: How to generate public and private key.
by
AdolfinWolf
on 30/05/2020, 16:57:24 UTC
⭐ Merited by Last of the V8s (1)
Blockchain does not verify addresses, so if we mistake it for the right address, we will lose our money.
Every adress has a checksum so it's pretty hard (impossible) to send to an adress with a typo.

https://gobittest.appspot.com/Address

Quote
Now suppose I choose a 99 random bytes to generate the private key, it is not a perfect idea but I want to know. How is the addreess, public and private key generated?
Needs to be below
Code:
0xFFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4140
due to the curvature

then you ecsda it (get a public key), so you take a scalar (private key below ^) and repeat that along the the curve.
That results in a public key on which you perform the necessary operations.

for a standard address, see https://gobittest.appspot.com/Address

So let's say we have scalar
Code:
97668528691423002059228747364507275544785515506959736128404960720826822156012n
which is valid, because it's below
Code:
115792089237316195423570985008687907852837564279074904382605163141518161494337

and hex
Code:
d7ee6da182177c6be9cf2a14e6c41fb6a692663849f09cb947c3b27c6cdddeec


to get a public key, you simply multiply it (the starting point of the curve) by the scalar.  (You should watch a video on how exactly this is done, for example: https://www.youtube.com/watch?v=F3zzNa42-tQ

so then you get a point x,y , in my case
Code:
 x: 37429941216543900232094877115525389998977649641483690331264124109562838279185n,
  y: 30224260574053827691919187729967576172484851453209067507073176932097443187763n
Now you have an uncompressed and compressed public key, but really, you should never use uncompressed keys (due to them not working well with nested segwit et al?)

So to get the compressed public key, we take y, and if it's odd, we add 0x02 in front of the x, otherwise 0x03,

so buffer x, append 0x02 if even, 0x03 if odd
and we get
Code:
0352c09891cd7fe1f9eed7776651805973986a9e8e6457e1095634ab72c8346411

Now to get this to an address we can perform a couple operations, for example for P2WSH we can do

we take the buffer of above compressed key
Code:
[
      3,  82, 192, 152, 145, 205, 127, 225,
    249, 238, 215, 119, 102,  81, 128,  89,
    115, 152, 106, 158, 142, 100,  87, 225,
      9,  86,  52, 171, 114, 200,  52, 100,
     17
  ]

RMDSHA256 it.
 we get:
Code:
d9351dcbad5b8f3b8bfa2f2cdc85c28118ca9326

Then we create the redeemscript by adding 0x00 0x14 to the above result, and RDM160SHA256 it again.
Code:
7db6766dce18d816eaac1198391e8bdcf70b253a

now paste a prefix in front of it. for mainnet we use 0x05 so we get
Code:
057db6766dce18d816eaac1198391e8bdcf70b253a

calculate the checksum by double SHA256 hashing the redeemscript plus the added bytes, so we hash
Code:
057db6766dce18d816eaac1198391e8bdcf70b253a
result:
Code:
b3e8e9dd2be918e9bf078b4166180cae14a717be79d222debc3f8d50210f6596
& then take the first 4 bytes,
so
Code:
b3e8e9dd

add those to the back of the previous step, so we get
Code:
057db6766dce18d816eaac1198391e8bdcf70b253ab3e8e9dd

base58 the result ^ and you should get  
Code:
3D9iyFHi1Zs9KoyynUfrL82rGhJfYTfSG4

I came across Code enthousiast's topic half the way through, so definitely check that out as well. https://bitcointalk.org/index.php?topic=5229211.0