Bitshala / BitcoinCore-PR-Review-Club

Bitcoin Core PR Review Organising repo
10 stars 2 forks source link

crypto: more Span<std::byte> modernization & follow-ups #42

Closed rajarshimaitra closed 10 months ago

rajarshimaitra commented 10 months ago

Session Details

Learning

stratospher commented 10 months ago

Summary

Questions

  1. Did you review the PR? Concept ACK, approach ACK, tested ACK, or NACK? What was your review approach?
  2. What is ChaCha20 encryption algorithm? Why are there 2 interfaces - ChaCha20 and ChaCha20Aligned? (hint: Check comments on src/crypto/chacha20.h)
  3. Can you pick out some examples of modern c++ code used in this PR? what's the advantage of using these in ChaCha20?(hint: auto, span, std::byte, noexcept, std::array, nodiscard, template, optional, constexpr. You can search up C++ features in https://en.cppreference.com/ and get the C++ version in which it was introduced/updates.)
  4. Which version of C++ does bitcoin core support? What's the disadvantage of updating to newer C++ versions?
  5. What does ChaCha20() noexcept = delete; do?
stratospher commented 10 months ago

Summary

1. Did you review the PR? Concept ACK, approach ACK, tested ACK, or NACK? What was your review approach?

Concept/tested ACK by the participants who went through the code.

2. What is ChaCha20 encryption algorithm? Why are there 2 interfaces - ChaCha20 and ChaCha20Aligned? (hint: Check comments on src/crypto/chacha20.h)

  1. Chacha20 is a secure, fast, and amazingly simple encryption algorithm. ChaCha20 is a hash function which mixes the key, number used once(nonce) and block counter producing a random keystream. This is used to xor and encrypt our data. plaintext xor keystream = ciphertext and ciphertext xor keystream = plaintext

  2. It applies 20 round functions to scrambles the 64 byte blocks. 1 round function consists of 4 quarter rounds and the operations in a quarter round consist of AND, XOR and ROTATE operations.

    QUARTERROUND(a, b, c, d)
    a += b;  d ^= a;  d <<<= 16;
    c += d;  b ^= c;  b <<<= 12;
    a += b;  d ^= a;  d <<<=  8;
    c += d;  b ^= c;  b <<<=  7; 
  3. 4 words - a, b, c and d are picked either from a column or from a diagonal.

    1. Quarter round operations are performed on a, b, c, d picked from each of the 4 columns one by one (4 quarter rounds = 1 round).
    2. Then Quarter round operations are performed on a, b, c, d picked from each of the 4 diagonals one by one (4 quarter rounds = 1 round).
    3. Now we've completed 2 rounds. If we repeat this 10 times, we perform 20 round functions and that's where the 20 in ChaCha20 comes from.
  4. Daniel J Bernstein, the creator of ChaCha20 put a lot of thought in this quarter-round, for both performance and scrambling quality.

    1. it works on 4 words at a time to minimise memory access.
    2. It is very cache friendly.
    3. It also scrambles data a bit better than his earlier Salsa20 quarter-round, because each word is updated twice here, and every word has a chance to influence the three others. This makes Chacha20 a little stronger than Salsa20 in practice.
  5. The first block gets the block counter initialised to 0. The difference between 2 blocks is often only one bit. But after the scrambling, that single bit will have wrecked so much havoc it won’t matter - the scrambled blocks will look unrelated to the attacker.

  6. ChaCha20Aligned only works on 64 byte blocks. ChaCha20 can encrypt any length message. We interact with the algorithm using ChaCha20 interface. ChaCha20 uses ChaCha20Aligned interface internally to perform the computation.

3. Can you pick out some examples of modern c++ code used in this PR? what's the advantage of using these in ChaCha20?(hint: auto, span, std::byte, noexcept, std::array, nodiscard, template, optional, constexpr. You can search up C++ features in https://en.cppreference.com/ and get the C++ version in which it was introduced/updates.)

References