Closed rajarshimaitra closed 10 months ago
ChaCha20
and ChaCha20Aligned
? (hint: Check comments on src/crypto/chacha20.h)ChaCha20() noexcept = delete;
do?Concept/tested ACK by the participants who went through the code.
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
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;
4 words - a
, b
, c
and d
are picked either from a column or from a diagonal.
a
, b
, c
, d
picked from each of the 4 columns one by one (4 quarter rounds = 1 round
). a
, b
, c
, d
picked from each of the 4 diagonals one by one (4 quarter rounds = 1 round
). Daniel J Bernstein, the creator of ChaCha20 put a lot of thought in this quarter-round, for both performance and scrambling quality.
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.
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.
auto
- the c++ compiler would complain about type errors and so the compiler already know what the type is. auto
makes the compiler assign the type by itself since the compiler already knows it.span
- a view for a contiguous sequence of objects (has information about the pointer + size). it's a C++20 feature. Bitcoin core had a custom implementation in src/span.h with API structures compatible with C++20.std::byte
- std::byte
models a collection of bits and it's what is best for interacting with raw memory. It supports only bitwise and comparison operations - unlike char
which can perform arithmetic operations too.noexcept
- noexcept
is used in cases where exception is guaranteed to be not thrown . Just because a function is marked noexcept
doesn't mean it wont possibly throw an exception, it just means that your program will terminate rather than let the exception escape from the function..std::array
- a fixed size vector. it's a container with all the good stuff vector brings with it - cache friendly, compact + efficient.nodiscard
- used when the return value of a function should not be ignored.template
- templates are expanded at compiler time. we pass the data type as a parameter so that we don’t need to write the same code for different data types.optional
- the return value of a function that may not return anything.constexpr
- both c++14 and 17 made constexpr
have better performance for functions at compile time.
We're currently in C++17. It takes time for C++ version to be available on all common platform/linux distributions. We don't generally update it till it's stable + commonly available. For example, Backports like this can take time to be commonly available.
ChaCha20() noexcept = delete;
do?This prevents objects of the ChaCha20 class from being initialized without a key. So initializing with a zero key isn't allowed by the interface of the class.
Session Details
[Crypto][C++]
[c++]
Learning