0

When developing (I'm using Apple Swift), what steps are needed in order to independently create my own pair of private-public keys (with the ability to restore a private key from a mnemonic phrase)?

There's no need to describe each step in detail, I would like to understand just the general idea.

2

2 Answers 2

1
+50

I’ll omit some details to keep the answer compact but the general idea is this:

An elliptic curve point is a two dimensional point (x, y) where the x and y coordinates follow the curve equation y^2 = x^3 + 7

A private key is just an integer in the range 1.. 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141

A public key is an EC point (two coordinates)

G is a special point with coordinates (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798, 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)

Steps to create private/public keys

  1. Implement Elliptic curve point addition (addition of two points)
  2. Implement Elliptic curve point multiplication (multiplication between an EC point and an integer)
  3. Generate a random integer in the range above, that’s your private key
  4. Multiply your private key with the point G using step 2, that’s your public key

For generating keys from mnemonics there are two BIPs that describe the process.

  • BIP-39 describes how to turn a mnemonic phrase into a 512-bit seed
  • BIP-32 describes how to turn your seed and a derivation path into an EC key
1
  • Thanks a lot Mike!
    – user125245
    Sep 2, 2021 at 18:40
2

This is written in swift, it's open source and you can check out the source code to get an idea of what's happening:

https://github.com/BlockchainCommons/GordianSeedTool-iOS

But as I'm coming to understand it myself, I'll attempt to summarize. You need to have a seed (a random number as a starting point) of sufficient entropy, or randomness, that's 2^256 bits long or 10^77, so pretty hard to repeat. Not every random number generator is actually random, for example some kinds of random are repeatable and are great for applications where you need to replicate results. But in this case you want to make it difficult to replicate. Every OS would have it's own set of random number generator syscalls that may or may not be random enough to be cryptographically secure, so you'd have to check the docs on whatever you use to ensure it's secure enough.

https://en.wikipedia.org/wiki/Cryptographically-secure_pseudorandom_number_generator

Generating mnemonic phrases is a little trickier. BIP-39 is the standard for implementing mnemonics in Bitcoin. This methods inserts bits of randomness, and then pulls some of the numbers out and selects the number pulled from an index of a vocabulary wordlist, hence generating the mnemonic phrase. It seems as if this is not quite as secure as a completely random key generated from sufficient entropy, as the possibilities is reduced from 2^256 to 2^132. Still pretty difficult to repeat though. See the following to understand how difficult this is to crack:

https://github.com/BlockchainCommons/SmartCustodyBook/blob/master/manuscript/00-randomness.md

So to generate a private key that can be restored via mnemonic phrase, it must be BIP-39 compatible. https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki

Rather than trying to recreate all this functionality from scratch, which I wouldn't be able to explain succinctly, you could take advantage of libwally, or utilize the BC libraries or peruse them for your own learning/replication.

https://github.com/Sjors/libwally-swift

https://github.com/BlockchainCommons/seedtool-cli

https://github.com/BlockchainCommons/Learning-Bitcoin-from-the-Command-Line/blob/266d7e13a3ba1ce40d29c83a5d63a8d805c06621/src/16_2_genmnemonic.c

https://github.com/BlockchainCommons/Learning-Bitcoin-from-the-Command-Line/blob/45ccb82661b2f4086b9b3bcb062f0c738023aa4b/18_6_Accessing_Bitcoind_with_Swift.md

1
  • Hi Ian! Thank you for your answer. I know how to "calculate" a mnemonic phrase using BIP39's predefined 2048 words' list and math 2^128+4. But what's after that?
    – user125245
    Aug 27, 2021 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy