Sunday, June 17, 2018

How SHA256 works

SHA-256 has the input message size < 2^64-bits. Block size is 512-bits and has a word size of 32-bits. The output is a 256-bit digest.

The compression function processes a 512-bit message block and a 256-bit intermediate hash value. There are two main components of this function: compression function and a message schedule.

The algorithm works as follows:


1.   Padding of the message, which is used to make the length of a block to 512-bits if it is smaller than the required block size of 512-bits.
2.   Parsing the message into message blocks that ensure that the message and its padding is divided into equal blocks of 512-bits.
3.   Setting up the initial hash value, which is the eight 32-bit words obtained by taking the first 32-bits of the fractional parts of the square roots of the first eight prime numbers. These initial values are randomly chosen to initialize the process and gives a level of confidence that no backdoor exists in the algorithm.

Hash computation:

1.   Each message block is processed in a sequence and requires 64 rounds to compute the full hash output. Each round uses slightly different constants to ensure that no two rounds are the same.
2.  First, the message schedule is prepared.
3.  Then, eight working variables are initialized.
4.  Then, the intermediate hash value is calculated.
5.  Finally, the message is processed, and the output hash is produced:

one round of SHA256 compression function

In the preceding diagram, abc,defg, and are the registers. Maj and Ch are applied bitwise.   performs the bitwise rotation. Round constants are Wjand Kj, which are added mod 2^32.

Saturday, June 16, 2018

How Advanced Encryption Standard (AES) works

Advanced Encryption Standard

In 2001 after an open competition an encryption algorithm named Rijndael that was invented by cryptographers Joan Daemen and Vincent Rijmen was standardized as AES with minor modifications by NIST in 2001. So far there is no attack found against AES that is better than brute force method.  Original Rijandaeil allows different key and block sizes of 128,192 and 256 bits but in AES standard only 128-bit block size is allowed. However key sizes of 128, 192 and 256 bit are allowed.

AES Steps

During AES Algorithm processing a 4 by 4 array of bytes knows as state is modified using multiple rounds. Full encryption requires 10 to 14 rounds depending on the size of the key. Following table shows the key sizes and required number of rounds.
Key Size
Number of rounds required
128 bit
10 rounds
192 bit
12 rounds
256 bit
14 rounds

Once the state is initialized with the input to the cipher, four operations are performed in four stages in order to encrypt the input. These stages are AddRoundKey, SubBytes, ShiftRows, and MixColumns.
1.    In AddRoundKey step, the state array is XORed with a subkey which is derived from the master key.
2.    This is the substitution step where a lookup table (S-box) is used to replace all bytes of the state array.
3.    This step is used to shift each row except the first one in the state array to the left in a cyclic and incremental manner.
4.    Finally, all bytes are mixed in this step in a linear fashion column-wise.

The steps above describe one round of AES. In the Final round (either 10, 12 or 14 depending on the key size), Stage 4 is replaced with Addroundkey to ensure that first three steps cannot just be simply inverted back.
AES block diagram, showing 1stround, in last round mixing step is not performed
Various cryptocurrency wallets use AES encryption to encrypt locally stored data. Especially in bitcoin wallet, AES 256 in CBC mode is used.

Saturday, June 2, 2018

How Ethereum Virtual Machine (EVM) works

The diagram below shows the design of the EVM where virtual ROM (right) stores the program code which is copied into main memory by using CODECOPY. Main memory is then read by the EVM by referring to the Program counter and executes instructions step by step. Program counter and EVM stack are updated accordingly with each instruction execution.
EVM is an entirely isolated and sandboxed runtime environment. The code that runs on the EVM does not have access to any external resources such as network or file system.
As discussed above EVM is based on a stack-based architecture. EVM is big endian by design, and it uses 256 bit wide words. This word size allows for Keccak 256 bit hash and Elliptic curve cryptography computations.
There are two types of storage available to contracts and EVM. First is called memory which is a byte array. When a smart contract finishes the code execution, the memory is cleared. It is akin to the concept of RAM. The other type called storage is permanently stored on the blockchain. It is a key-value store. 
Memory is unlimited but constrained by gas fee requirements. The storage associated with the virtual machine is a word addressable word array which is non-volatile and is maintained as part of the system state. Keys and value are 32 bytes in size in storage. The program code is stored in a virtual read-only memory (Virtual ROM) which is accessible by using CODECOPY instruction. CODECOPY instruction is used to copy the program code into main memory. Initially, all storage and memory are set to zero in the EVM.

Friday, June 1, 2018

Symmetric cryptography - Cipher Block chaining mode (CBC)

In this mode, each block of plaintext is XORed with the previous encrypted block. CBC mode uses initialization vector IV to encrypt the first block. It is recommended that IV is randomly chosen.

Thrilled to see that my book Mastering blockchain - the third edition is a best-seller at Amazon!