Reading a process and Slicing a string line

Today I had to write a quick function to read the VmSize of a linux process, in C++;
For simplicity lets define some invariants for our function:-

  • The linux process are avaialbe in /proc/
  • The PID will be passed to the function
  • VmSize is avaialbe in 'status' file

Our goal can be easily achieved by reading the 'status' file and searching for VmSize value converting it to GB.

Continue reading "Reading a process and Slicing a string line"

Elementary Sorting Algorithms – Insertion Sort

Sorts while moving a pointer i from left to right, elements on the right are sorted by comparing the element at the pointer index with elements to its left and exchange their position if the element to the left is larger. Elements to the left of the pointer have not been visited yet.

The Invariants

  1. Elements to the right are sorted
  2. Elements to the left are not visited

Continue reading "Elementary Sorting Algorithms – Insertion Sort"

Seed-to-Mnemonic

BIP39

https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki

Motivation

The bitcoin reference client offered a JBOK (just a punch of keys) wallet which required backing up of all the independent private keys and no support for regenerating/restoring the wallet state without them.

Individuals are no more required to backup their private keys in hexadicimal/binary formats rather they can store them as human readable memorable words in multiple languages (EN,CN,KR,...)

The introduction of BIP39 aids in implementing hierarchical deterministic wallets to overcome the above limitation.

Continue reading "Seed-to-Mnemonic"

Spending bitcoin UTXO using libbitcoin (bx)

The Times

1- Generate random seed and hd master private key

hd_master_private=$(bx seed --bit_length 512 | bx hd-new)

2- generate hd master public key

hd_master_public=$(bx hd-to-public $hd_master_private)

3- generate private key for spending account 0

m/44'/1'/0'/0

we first fund to this account for future spends

hd_m_44h_1h_0h_0=$(bx hd-private --hard --index 44 $hd_master_private | bx hd-private --hard --index 1 | bx hd-private --hard --index 0 | bx hd-private --index 0)
  • 44 : purpose always set to 44 BIP44 hardened
  • 0/1 : 0 for mainnet, 1 for testnet hardened
  • 0` First account/Individual wallet account key
  • 0/1 unhardened : Keys of receiving address/keys of change address

Continue reading "Spending bitcoin UTXO using libbitcoin (bx)"

Playing with bits

Here is a few handy tricks to manipulate bits in multibit values

  • Setting a bit number n in a value x
    int val = 122;
    std::bitset<8> a(val);
    std::cout << a << std::endl; //01111010
    // setting bit number 2 to 1
    a |= (1 << 2);
    std::cout << a << std::endl; //01111110
  • Twiddling a bit number n in a value x
    std::bitset<8> a(val); //01111010
    a &= ~(1 << 2); //01111010