diff --git a/SConstruct b/SConstruct index aaf15cc70..150ed3b89 100644 --- a/SConstruct +++ b/SConstruct @@ -60,8 +60,9 @@ for language, tools in languages_to_import.items(): Export('env') -env['CFLAGS'] = '-Wall -Wextra -Werror' -env['CXXFLAGS'] = '-std=c++17' +env['CCFLAGS'] = '-Wall -Wextra -Werror -pedantic -Wconversion' +env['CFLAGS'] = '-std=gnu99' +env['CXXFLAGS'] = '-std=c++17 -Wold-style-cast' env['ASFLAGS'] = '--64' env['COCONUTFLAGS'] = '--target 3.8' diff --git a/contents/IFS/code/c/IFS.c b/contents/IFS/code/c/IFS.c index e4aab3b50..ffd56f6fe 100644 --- a/contents/IFS/code/c/IFS.c +++ b/contents/IFS/code/c/IFS.c @@ -29,18 +29,18 @@ void chaos_game(struct point *in, size_t in_n, struct point *out, } int main() { - const int point_count = 10000; + const size_t point_count = 10000; struct point shape_points [3] = {{0.0,0.0}, {0.5,sqrt(0.75)}, {1.0,0.0}}; struct point out_points[point_count]; - srand(time(NULL)); + srand((unsigned int)time(NULL)); chaos_game(shape_points, 3, out_points, point_count); FILE *fp = fopen("sierpinksi.dat", "w+"); - for (int i = 0; i < point_count; ++i) { + for (size_t i = 0; i < point_count; ++i) { fprintf(fp, "%f\t%f\n", out_points[i].x, out_points[i].y); } diff --git a/contents/approximate_counting/code/c/approximate_counting.c b/contents/approximate_counting/code/c/approximate_counting.c index 2be6ffaf8..f372b0953 100644 --- a/contents/approximate_counting/code/c/approximate_counting.c +++ b/contents/approximate_counting/code/c/approximate_counting.c @@ -40,7 +40,7 @@ double increment(double v, double a) // It returns n(v, a), the approximate count double approximate_count(size_t n_items, double a) { - int v = 0; + double v = 0; for (size_t i = 0; i < n_items; ++i) { v = increment(v, a); } @@ -61,9 +61,10 @@ void test_approximation_count(size_t n_trials, size_t n_items, double a, for (size_t i = 0; i < n_trials; ++i) { sum += approximate_count(n_items, a); } - double avg = sum / n_trials; + double avg = sum / (double)n_trials; - if (fabs((avg - n_items) / n_items) < threshold){ + double items = (double)n_items; + if (fabs((avg - items) / items) < threshold){ printf("passed\n"); } else{ @@ -73,7 +74,7 @@ void test_approximation_count(size_t n_trials, size_t n_items, double a, int main() { - srand(time(NULL)); + srand((unsigned int)time(NULL)); printf("[#]\nCounting Tests, 100 trials\n"); printf("[#]\ntesting 1,000, a = 30, 10%% error\n"); diff --git a/contents/cooley_tukey/code/c/fft.c b/contents/cooley_tukey/code/c/fft.c index f87e12afd..212b272b1 100644 --- a/contents/cooley_tukey/code/c/fft.c +++ b/contents/cooley_tukey/code/c/fft.c @@ -11,7 +11,7 @@ void fft(double complex *x, size_t n) { memset(y, 0, sizeof(y)); fftw_plan p; - p = fftw_plan_dft_1d(n, (fftw_complex*)x, (fftw_complex*)y, + p = fftw_plan_dft_1d((int)n, (fftw_complex*)x, (fftw_complex*)y, FFTW_FORWARD, FFTW_ESTIMATE); fftw_execute(p); @@ -27,7 +27,7 @@ void dft(double complex *X, const size_t N) { for (size_t i = 0; i < N; ++i) { tmp[i] = 0; for (size_t j = 0; j < N; ++j) { - tmp[i] += X[j] * cexp(-2.0 * M_PI * I * j * i / N); + tmp[i] += X[j] * cexp(-2.0 * M_PI * I * (double)j * (double)i / (double)N); } } @@ -49,7 +49,7 @@ void cooley_tukey(double complex *X, const size_t N) { cooley_tukey(X + N / 2, N / 2); for (size_t i = 0; i < N / 2; ++i) { - X[i + N / 2] = X[i] - cexp(-2.0 * I * M_PI * i / N) * X[i + N / 2]; + X[i + N / 2] = X[i] - cexp(-2.0 * I * M_PI * (double)i / (double)N) * X[i + N / 2]; X[i] -= (X[i + N / 2]-X[i]); } } @@ -58,7 +58,7 @@ void cooley_tukey(double complex *X, const size_t N) { void bit_reverse(double complex *X, size_t N) { for (size_t i = 0; i < N; ++i) { size_t n = i; - int a = i; + size_t a = i; int count = (int)log2((double)N) - 1; n >>= 1; @@ -67,7 +67,7 @@ void bit_reverse(double complex *X, size_t N) { count--; n >>= 1; } - n = (a << count) & ((1 << (int)log2((double)N)) - 1); + n = (a << count) & (size_t)((1 << (size_t)log2((double)N)) - 1); if (n > i) { double complex tmp = X[i]; @@ -81,8 +81,8 @@ void iterative_cooley_tukey(double complex *X, size_t N) { bit_reverse(X, N); for (int i = 1; i <= log2((double)N); ++i) { - size_t stride = pow(2, i); - double complex w = cexp(-2.0 * I * M_PI / stride); + size_t stride = (size_t)pow(2, i); + double complex w = cexp(-2.0 * I * M_PI / (double)stride); for (size_t j = 0; j < N; j += stride) { double complex v = 1.0; for (size_t k = 0; k < stride / 2; ++k) { @@ -105,7 +105,7 @@ void approx(double complex *X, double complex *Y, size_t N) { } int main() { - srand(time(NULL)); + srand((unsigned int)time(NULL)); double complex x[64], y[64], z[64]; for (size_t i = 0; i < 64; ++i) { x[i] = rand() / (double) RAND_MAX; diff --git a/contents/cooley_tukey/code/cpp/fft.cpp b/contents/cooley_tukey/code/cpp/fft.cpp index 5d4772f10..d4697d1df 100644 --- a/contents/cooley_tukey/code/cpp/fft.cpp +++ b/contents/cooley_tukey/code/cpp/fft.cpp @@ -55,7 +55,7 @@ void cooley_tukey(Iter first, Iter last) { // now combine each of those halves with the butterflies for (int k = 0; k < size / 2; ++k) { - auto w = std::exp(complex(0, -2.0 * pi * k / size)); + auto w = std::exp(complex(0, -2.0 * pi * k / static_cast(size))); auto& bottom = first[k]; auto& top = first[k + size / 2]; @@ -78,7 +78,7 @@ void sort_by_bit_reverse(Iter first, Iter last) { b = (((b & 0xcccccccc) >> 2) | ((b & 0x33333333) << 2)); b = (((b & 0xf0f0f0f0) >> 4) | ((b & 0x0f0f0f0f) << 4)); b = (((b & 0xff00ff00) >> 8) | ((b & 0x00ff00ff) << 8)); - b = ((b >> 16) | (b << 16)) >> (32 - std::uint32_t(log2(size))); + b = ((b >> 16) | (b << 16)) >> (32 - std::uint32_t(log2(static_cast(size)))); if (b > i) { swap(first[b], first[i]); } diff --git a/contents/cryptography/cryptography.md b/contents/cryptography/cryptography.md index 80daba747..dd136dae8 100644 --- a/contents/cryptography/cryptography.md +++ b/contents/cryptography/cryptography.md @@ -2,7 +2,7 @@ Humans have almost always been interested in sending secret messages that only the sender and receiver understand. The reason for this is obvious: secret messages should remain secret. -The easiest way for this to happen is to talk behind closed doors, but that simply doesn't work if the the sender and receiver are separated by a significant distance. +The easiest way for this to happen is to talk behind closed doors, but that simply doesn't work if the sender and receiver are separated by a significant distance. In this case, they need to rely on a messenger or mailman to send the message. For simplicity, let's assume they are sending a written letter for the purpose of negotiating war tactics in ancient Greece or Rome. @@ -17,17 +17,38 @@ In this way, the message would seem like utter gobbledygook to anyone other than It doesn't matter if the messenger is evil. They cannot read the message anyway. It's also fine if the message is replaced, because then the receiver won't be able to properly decode the message and can just ask for another message to be sent (probably on another path with a different messenger). -Unsurprisingly, a very early method of encryption was supposedly developed by Julius Caeser and called the "Caesar Cipher." +Unsurprisingly, a very early method of encryption was supposedly developed by Julius Caeser and called the "Caesar Cipher" {{ "ceasar_cipher_wiki" | cite }}. Here, every character in the message is replaced by another character based on some pre-defined table or chart that only the sender and receiver have. The table is created by simply rotating the alphabet by $$n$$ spaces, where $$n$$ is chosen in a discussion between the sender and receiver before-hand. -| $$n$$ | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | -|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| -| 0 | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | -| 2 | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | a | b | -| 14 | o | p | q | r | s | t | u | v | w | x | y | z | a | b | c | d | e | f | g | h | i | j | k | l | m | n | -| 18 | s | t | u | v | w | x | y | z | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | -| 21 | v | w | x | y | z | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | -| 24 | y | z | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | + + | $$n$$ | 0 | 2 | 14 | 18 | 21 | 24 | + | ----- | - | - | -- | -- | -- | -- | + | a | a | c | o | s | v | y | + | b | b | d | p | t | w | z | + | c | c | e | q | u | x | a | + | d | d | f | r | v | y | b | + | e | e | g | s | w | z | c | + | f | f | h | t | x | a | d | + | g | g | i | u | y | b | e | + | h | h | j | v | z | c | f | + | i | i | k | w | a | d | g | + | j | j | l | x | b | e | h | + | k | k | m | y | c | f | i | + | l | l | n | z | d | g | j | + | m | m | o | a | e | h | k | + | n | n | p | b | f | i | l | + | o | o | q | c | g | j | m | + | p | p | r | d | h | k | n | + | q | q | s | e | i | l | o | + | r | r | t | f | j | m | p | + | s | s | u | g | k | n | q | + | t | t | v | h | l | o | r | + | u | u | w | i | m | p | s | + | v | v | x | j | n | q | t | + | w | w | y | k | o | r | u | + | x | x | z | l | p | s | v | + | y | y | a | m | q | t | w | + | z | z | b | n | r | u | x | It is certainly not the most complicated scheme out there, but it is generally the first encryption scheme people come up with when trying to encode secret messages to one another. Honestly, I remember sending messages back and forth to friends in elementary school, but we would never provide the necessary table to decode the message. @@ -35,7 +56,8 @@ Instead, we would provide enough text that they could find the table themselves If a bunch of elementary school kids can figure out how to break this encryption scheme, it cannot be too robust. In fact, it's interesting to see how the field of cryptography has grown since the Caesar cipher was developed. In the cryptographic literature, there is always a sender, receiver, and eavesdropper. -For some reason beyond my own comprehension, these three people are almost always given the names Alice (sender), Bob (receiver), and Eve (attacker or eavesdropper). +For some reason beyond my own comprehension, the first two people are almost always given the names Alice (sender) and Bob (receiver). +Meanwhile, the attacker or eavesdropper is usually called either Eve or Charlie These names are consistent even with quantum cryptography, so they are here to stay. In general, there are two different types of encryption: symmetric and asymmetric. @@ -44,32 +66,31 @@ Both of which are described in the following sections. Cryptographic systems are a cornerstone to modern information technology and lie at the heart of everything from WiFi to online banking. If an attacker manages to crack modern cryptographic algorithms, they could cause serious damage. For this reason, it is important to keep a few things in mind: -* Because cryptography has become such an advanced field cryptography systems should be analyzed by trained professionals and have to go under extensive testing and vetting. - Meaning whenever possible use a widely accepted cryptography library instead of writing your own cypher. -* Kerckhoffs's principle says that when determining the robustness of a cryptography system it should be assumed that the attacker knows the encryption and decryption algorithm. +* Because cryptography has become such an advanced field cryptographic systems should be analyzed by trained professionals and have undergo extensive testing and vetting. + This means that whenever possible, one should use a widely accepted cryptography library instead of writing their own cypher. +* Kerckhoffs's principle says that when determining the robustness of a cryptographic system it should be assumed that the attacker knows the encryption and decryption algorithm {{ "Kerckhoffs_principle_wiki" | cite }}. This does not include any pre-shared or secret keys. -* With the advances in technology cryptography often hits its limits. +* With the advances in technology, cryptography often hits its limits. Many formerly state-of-the-art hashing algorithms have become obsolete because the computers used to crack them have gotten faster and better. - The keys for RSA have to be longer or different and stronger algorithms have to be used. - Another field that cryptography will have to face is Quantum Computing. + Another field that cryptography will have to face is [quantum computing](../quantum_information/quantum_information.md). Quantum computers will have a big impact on cryptography and especially asymmetric cryptography. - This whole set of problems is summarized in the field of post-quantum cryptography. + This whole set of problems is summarized in the field of post-quantum cryptography {{ "post-quantum_crypto_wiki" | cite }}. ## Symmetric Cryptography Symmetric cryptography is called symmetric because the key that is used is the same for encrypting and decrypting. For this to work Alice and Bob both need the same key, which they have to share before communicating. Some examples for symmetric cryptography are: -* **Ceasar Cipher**: Alice and Bob rotate the alphabet by $$n$$ characters and use that as a table to encode and decode their message. -* **Rot13**: This is a special case of the Caeser Cipher where the alphabet is rotated by 13, hence the name "Rot13." -* **Permutation Cipher**: Here you choose a permutation $$\pi$$ (i.e. $$\pi=(3,1,2,4)$$) and reorder the the letters according to that $$\pi$$ which is the key. -* **XOR encryption**: This method works on bit strings and combines the message and a key of equal bit length with the XOR operator. +* **Ceasar Cipher**: Alice and Bob rotate the alphabet by $$n$$ characters and use that as a table to encode and decode their message {{ "ceasar_cipher_wiki" | cite }}. +* **Rot13**: This is a special case of the Caeser Cipher where the alphabet is rotated by 13, hence the name "Rot13" {{ "rot13_wiki" | cite }} +* **Permutation Cipher**: Here we choose a permutation $$\pi$$ (i.e. $$\pi=(3,1,2,4)$$) and reorder the the letters according to that $$\pi$$ which is the key {{ "CC_permutation" | cite }}. +* **XOR encryption**: This method works on bit strings and combines the message and a key of equal bit length with the XOR operator {{ "xor_cipher_wiki" | cite }}. To decrypt, simply XOR again with the same key. -* **DES or Data Encryption Standard**: This is a newer encryption algorithm which was standardized in 1977. +* **DES or Data Encryption Standard**: This is a newer encryption algorithm which was standardized in 1977 {{ "DES_wiki" | cite }}. It has since been deemed insecure and is superseded by AES. -* **AES or Advanced Encryption Standard**: The actual algorithm is called "Rijndael". - Like with XOR or DES you generate a bit string (depending on which AES you use 128/192 or 256 bit long) which is your key. -* **Blowfish**: This algorithm also was a good contender for the AES but lost to Rijndael. +* **AES or Advanced Encryption Standard**: The actual algorithm is called "Rijndael" {{ "AES_wiki" | cite }}. + Like with XOR or DES we generate a bit string (depending on which AES you use 128/192 or 256 bit long) which is your key. +* **Blowfish**: This algorithm was also a good contender for the AES but lost to Rijndael {{ "blowfish_cipher_wiki" | cite }}. This section is currently a work-in-progress, and all of these methods will have corresponding chapters in the near future. @@ -77,19 +98,24 @@ This section is currently a work-in-progress, and all of these methods will have Asymmetric cryptography is sometimes called "public key cryptography" (or PK cryptography in short) because Bob and Alice both need a shared public key and a private key they keep to themselves. These algorithms are called asymmetric because what is encrypted with the public key can only be decrypted with the private key and vice versa. -This can be used for a number of different applications, like digital signing, encrypted communication or secretly sharing keys. +This can be used for a number of different applications, like digital signing, encrypted communication, or secretly sharing keys. For example, if Alice wants to send a message to Bob and this message has to be kept private, Alice will encrypt the message with Bob's public key. Now only Bob can decrypt the message again and read it. If Charlie were to alter Alice's message, Bob couldn't decrypt it anymore. If Bob wants to make sure the message is actually from Alice, Alice can encrypt the already encrypted message with her private key again. This is to keep Charlie from sending forged or altered messages since Bob couldn't decrypt that layer with Alice's public key. Some examples for public key cryptography: -* **RSA**: This algorithm calculates a public and a private key from two very large primes. +* **RSA**: This algorithm calculates a public and a private key from two very large primes {{ "RSA_wiki" | cite }}. It is (hopefully) near impossible to factor the product of two such primes in a feasible amount of time. -* **ECC or Elliptic-curve cryptography**: Here you calculate the private and public key from two points on an elliptic curve. +* **ECC or Elliptic-curve cryptography**: Here you calculate the private and public key from two points on an elliptic curve {{ "ECC_crypto_wiki" | cite }}. This has the positive side effect that you need smaller numbers than non-ECC algorithms like RSA to achieve the same level of security. This section is currently a work-in-progress. These methods will also have corresponding chapters in the near future. + +### Bibliography + +{% references %} {% endreferences %} + diff --git a/contents/cryptography/res/table.jl b/contents/cryptography/res/table.jl new file mode 100644 index 000000000..314c238c4 --- /dev/null +++ b/contents/cryptography/res/table.jl @@ -0,0 +1,33 @@ +function print_table(a::Array{T, 2}, + header::Vector{String}) where T <: Union{Char, String} + print(" | ") + for i = 1:length(header) + print(string(header[i]), " | ") + end + println() + + print(" | ") + for i = 1:length(header) + print("---", " | ") + end + println() + + for i = 1:size(a)[1] + print(" | ") + for j = 1:size(a)[2] + print(string(a[i,j]), " | ") + end + println() + end +end + +alphabet = [char for char = 'a':'z'] +offsets = Int.([0, 0, 2, 14, 18, 21, 24]) +alphabet_array = Array{Char}(undef, 26, length(offsets)) + +for i = 1:length(offsets) + alphabet_array[:,i] = vcat(alphabet[offsets[i]+1:26],alphabet[1:offsets[i]]) +end + +header = vcat(string.(offsets)) +print_table(alphabet_array, header) diff --git a/contents/forward_euler_method/code/c/euler.c b/contents/forward_euler_method/code/c/euler.c index 9ce095930..211e2f614 100644 --- a/contents/forward_euler_method/code/c/euler.c +++ b/contents/forward_euler_method/code/c/euler.c @@ -13,7 +13,7 @@ void solve_euler(double timestep, double *result, size_t n) { int check_result(double *result, size_t n, double threshold, double timestep) { int is_approx = 1; for (size_t i = 0; i < n; ++i) { - double solution = exp(-3.0 * i * timestep); + double solution = exp(-3.0 * (double)i * timestep); if (fabs(result[i] - solution) > threshold) { printf("%f %f\n", result[i], solution); is_approx = 0; diff --git a/contents/forward_euler_method/code/cpp/euler.cpp b/contents/forward_euler_method/code/cpp/euler.cpp index a341655f4..0fbeb8426 100644 --- a/contents/forward_euler_method/code/cpp/euler.cpp +++ b/contents/forward_euler_method/code/cpp/euler.cpp @@ -27,7 +27,7 @@ template bool check_result(Iter first, Iter last, double threshold, double timestep) { auto it = first; for (size_t idx = 0; it != last; ++idx, ++it) { - double solution = std::exp(-3.0 * idx * timestep); + double solution = std::exp(-3.0 * static_cast(idx) * timestep); if (std::abs(*it - solution) > threshold) { std::cout << "We found a value outside the threshold; the " << idx << "-th value was " << *it << ", but the expected solution was " diff --git a/contents/gaussian_elimination/code/cpp/gaussian_elimination.cpp b/contents/gaussian_elimination/code/cpp/gaussian_elimination.cpp index 843392490..45047a9d4 100644 --- a/contents/gaussian_elimination/code/cpp/gaussian_elimination.cpp +++ b/contents/gaussian_elimination/code/cpp/gaussian_elimination.cpp @@ -7,12 +7,12 @@ void gaussianElimination(std::vector > &eqns) { // 'eqns' is the matrix, 'rows' is no. of vars - int rows = eqns.size(), cols = eqns[0].size(); + std::size_t rows = eqns.size(), cols = eqns[0].size(); - for (int i = 0; i < rows - 1; i++) { - int pivot = i; + for (std::size_t i = 0; i < rows - 1; i++) { + std::size_t pivot = i; - for (int j = i + 1; j < rows; j++) { + for (std::size_t j = i + 1; j < rows; j++) { if (fabs(eqns[j][i]) > fabs(eqns[pivot][i])) pivot = j; } @@ -22,10 +22,10 @@ void gaussianElimination(std::vector > &eqns) { if (i != pivot) // Swapping the rows if new row with higher maxVals is found std::swap(eqns[pivot], eqns[i]); // C++ swap function - for (int j = i + 1; j < rows; j++) { + for (std::size_t j = i + 1; j < rows; j++) { double scale = eqns[j][i] / eqns[i][i]; - for (int k = i + 1; k < cols; k++) // k doesn't start at 0, since + for (std::size_t k = i + 1; k < cols; k++) // k doesn't start at 0, since eqns[j][k] -= scale * eqns[i][k]; // values before from 0 to i // are already 0 eqns[j][i] = 0.0; @@ -35,9 +35,9 @@ void gaussianElimination(std::vector > &eqns) { void gaussJordan(std::vector > &eqns) { // 'eqns' is the (Row-echelon) matrix, 'rows' is no. of vars - int rows = eqns.size(); + std::size_t rows = eqns.size(); - for (int i = rows - 1; i >= 0; i--) { + for (std::size_t i = rows - 1; i < rows; i--) { if (eqns[i][i] != 0) { @@ -45,7 +45,7 @@ void gaussJordan(std::vector > &eqns) { eqns[i][i] = 1; // We know that the only entry in this row is 1 // subtracting rows from below - for (int j = i - 1; j >= 0; j--) { + for (std::size_t j = i - 1; j < i; j--) { eqns[j][rows] -= eqns[j][i] * eqns[i][rows]; eqns[j][i] = 0; // We also set all the other values in row to 0 directly } @@ -55,13 +55,13 @@ void gaussJordan(std::vector > &eqns) { std::vector backSubs(const std::vector > &eqns) { // 'eqns' is matrix, 'rows' is no. of variables - int rows = eqns.size(); + std::size_t rows = eqns.size(); std::vector ans(rows); - for (int i = rows - 1; i >= 0; i--) { + for (std::size_t i = rows - 1; i < rows; i--) { double sum = 0.0; - for (int j = i + 1; j < rows; j++) sum += eqns[i][j] * ans[j]; + for (std::size_t j = i + 1; j < rows; j++) sum += eqns[i][j] * ans[j]; if (eqns[i][i] != 0) ans[i] = (eqns[i][rows] - sum) / eqns[i][i]; @@ -73,10 +73,10 @@ std::vector backSubs(const std::vector > &eqns) { void printMatrix(const std::vector > &matrix) { - for (int row = 0; row < matrix.size(); row++) { + for (std::size_t row = 0; row < matrix.size(); row++) { std::cout << "["; - for (int col = 0; col < matrix[row].size() - 1; col++) + for (std::size_t col = 0; col < matrix[row].size() - 1; col++) std::cout << std::setw(8) << std::fixed << std::setprecision(3) << matrix[row][col]; diff --git a/contents/graham_scan/code/c/graham.c b/contents/graham_scan/code/c/graham.c index ecd15f867..a3cdb1190 100644 --- a/contents/graham_scan/code/c/graham.c +++ b/contents/graham_scan/code/c/graham.c @@ -36,8 +36,8 @@ void polar_angles_sort(struct point *points, struct point origin, size_t size) { double pivot_angle = polar_angle(origin, points[size / 2]); - int i = 0; - int j = size - 1; + size_t i = 0; + size_t j = size - 1; while (1) { while (polar_angle(origin, points[i]) < pivot_angle) { i++; diff --git a/contents/huffman_encoding/code/c/huffman.c b/contents/huffman_encoding/code/c/huffman.c index 4f60f5da6..87f5fb2c9 100644 --- a/contents/huffman_encoding/code/c/huffman.c +++ b/contents/huffman_encoding/code/c/huffman.c @@ -23,8 +23,8 @@ struct codebook { struct heap { struct tree** data; - int length; - int capacity; + size_t length; + size_t capacity; }; bool is_leaf(const struct tree* t) { @@ -39,21 +39,21 @@ void swap(struct tree** lhs, struct tree** rhs) { /* The two concat functions are horribly inefficient */ void concat(char** dst, const char* src) { - int dst_len = strlen(*dst); - int src_len = strlen(src); + size_t dst_len = strlen(*dst); + size_t src_len = strlen(src); *dst = realloc(*dst, src_len + dst_len + 1); strcat(*dst, src); } void concat_char(char** dst, char c) { - int len = strlen(*dst); + size_t len = strlen(*dst); *dst = realloc(*dst, len + 2); (*dst)[len] = c; (*dst)[len + 1] = '\0'; } char* duplicate(const char* src) { - int length = strlen(src); + size_t length = strlen(src); char* dst = malloc(length + 1); memcpy(dst, src, length + 1); return dst; @@ -66,9 +66,9 @@ void heap_push(struct heap* heap, struct tree* value) { } heap->data[heap->length++] = value; - int index = heap->length - 1; + size_t index = heap->length - 1; while (index) { - int parent_index = (index - 1) / 2; + size_t parent_index = (index - 1) / 2; if (heap->data[parent_index]->count <= heap->data[index]->count) { break; } @@ -86,11 +86,11 @@ struct tree* heap_pop(struct heap* heap) { struct tree* result = heap->data[0]; swap(&heap->data[0], &heap->data[--heap->length]); - int index = 0; + size_t index = 0; for (;;) { - int target = index; - int left = 2 * index + 1; - int right = left + 1; + size_t target = index; + size_t left = 2 * index + 1; + size_t right = left + 1; if (left < heap->length && heap->data[left]->count < heap->data[target]->count) { diff --git a/contents/monte_carlo_integration/code/c/monte_carlo.c b/contents/monte_carlo_integration/code/c/monte_carlo.c index 9920ff55c..14f823fe4 100644 --- a/contents/monte_carlo_integration/code/c/monte_carlo.c +++ b/contents/monte_carlo_integration/code/c/monte_carlo.c @@ -24,7 +24,7 @@ double monte_carlo(unsigned int samples) { } int main() { - srand(time(NULL)); + srand((unsigned int)time(NULL)); double estimate = monte_carlo(1000000); diff --git a/contents/monte_carlo_integration/code/cpp/monte_carlo.cpp b/contents/monte_carlo_integration/code/cpp/monte_carlo.cpp index f38d83f45..4a600d72e 100644 --- a/contents/monte_carlo_integration/code/cpp/monte_carlo.cpp +++ b/contents/monte_carlo_integration/code/cpp/monte_carlo.cpp @@ -37,8 +37,6 @@ double monte_carlo_pi(unsigned samples) { } int main() { - unsigned samples; - double pi_estimate = monte_carlo_pi(10000000); std::cout << "Pi = " << pi_estimate << '\n'; std::cout << "Percent error is: " << 100 * std::abs(pi_estimate - PI) / PI << " %\n"; diff --git a/contents/split-operator_method/code/c/split_op.c b/contents/split-operator_method/code/c/split_op.c index ecf48e027..fd5a84001 100644 --- a/contents/split-operator_method/code/c/split_op.c +++ b/contents/split-operator_method/code/c/split_op.c @@ -34,10 +34,10 @@ void fft(double complex *x, size_t n, bool inverse) { fftw_plan p; if (inverse) { - p = fftw_plan_dft_1d(n, (fftw_complex*)x, (fftw_complex*)y, + p = fftw_plan_dft_1d((int)n, (fftw_complex*)x, (fftw_complex*)y, FFTW_BACKWARD, FFTW_ESTIMATE); } else { - p = fftw_plan_dft_1d(n, (fftw_complex*)x, (fftw_complex*)y, + p = fftw_plan_dft_1d((int)n, (fftw_complex*)x, (fftw_complex*)y, FFTW_FORWARD, FFTW_ESTIMATE); } @@ -63,9 +63,9 @@ void init_params(struct params *par, double xmax, unsigned int res, double dt, par->im_time = im; for (size_t i = 0; i < res; ++i) { - par->x[i] = xmax / res - xmax + i * (2.0 * xmax / res); + par->x[i] = xmax / res - xmax + (double)i * (2.0 * xmax / res); if (i < res / 2) { - par->k[i] = i * M_PI / xmax; + par->k[i] = (double)i * M_PI / xmax; } else { par->k[i] = ((double)i - res) * M_PI / xmax; } diff --git a/contents/split-operator_method/code/cpp/split_op.cpp b/contents/split-operator_method/code/cpp/split_op.cpp index 74f8df2b7..bb160c901 100644 --- a/contents/split-operator_method/code/cpp/split_op.cpp +++ b/contents/split-operator_method/code/cpp/split_op.cpp @@ -28,9 +28,9 @@ struct Params { im_time = im; for (size_t i = 0; i < res; ++i) { - x.emplace_back(xmax / res - xmax + i * (2.0 * xmax / res)); + x.emplace_back(xmax / res - xmax + static_cast(i) * (2.0 * xmax / res)); if (i < res / 2) { - k.push_back(i * M_PI / xmax); + k.push_back(static_cast(i) * M_PI / xmax); } else { k.push_back((static_cast(i) - res) * M_PI / xmax); } @@ -85,7 +85,7 @@ void fft(vector_complex &x, bool inverse) { fftw_complex *in = reinterpret_cast(x.data()); fftw_complex *out = reinterpret_cast(y.data()); - p = fftw_plan_dft_1d(x.size(), in, out, + p = fftw_plan_dft_1d(static_cast(x.size()), in, out, (inverse ? FFTW_BACKWARD : FFTW_FORWARD), FFTW_ESTIMATE); fftw_execute(p); @@ -97,7 +97,7 @@ void fft(vector_complex &x, bool inverse) { } void split_op(Params &par, Operators &opr) { - double density[opr.size]; + auto density = std::vector(opr.size, 0); for (size_t i = 0; i < par.timesteps; ++i) { for (size_t j = 0; j < opr.size; ++j) { @@ -142,7 +142,7 @@ void split_op(Params &par, Operators &opr) { std::ofstream fstream = std::ofstream(filename_stream.str()); if (fstream) { - for (int i = 0; i < opr.size; ++i) { + for (std::size_t i = 0; i < opr.size; ++i) { std::stringstream data_stream; data_stream << i << "\t" << density[i] << "\t" << real(opr.v[i]) << "\n"; diff --git a/contents/stable_marriage_problem/code/c/stable_marriage.c b/contents/stable_marriage_problem/code/c/stable_marriage.c index e4f4ad2fd..8537e82f2 100644 --- a/contents/stable_marriage_problem/code/c/stable_marriage.c +++ b/contents/stable_marriage_problem/code/c/stable_marriage.c @@ -5,7 +5,7 @@ #include struct person { - int id; + size_t id; struct person *partner; size_t *prefers; size_t index; @@ -13,7 +13,7 @@ struct person { void shuffle(size_t *array, size_t size) { for (size_t i = size - 1; i > 0; --i) { - size_t j = rand() % (i + 1); + size_t j = (size_t)rand() % (i + 1); size_t tmp = array[i]; array[i] = array[j]; array[j] = tmp; @@ -82,7 +82,7 @@ void free_group(struct person *group, size_t size) { } int main() { - srand(time(NULL)); + srand((unsigned int)time(NULL)); struct person men[5], women[5]; @@ -114,7 +114,7 @@ int main() { printf("\n"); for (size_t i = 0; i < 5; ++i) { - printf("the partner of man %zu is woman %d\n", i, men[i].partner->id); + printf("the partner of man %zu is woman %ld\n", i, men[i].partner->id); } free_group(men, 5); diff --git a/contents/thomas_algorithm/code/c/thomas.c b/contents/thomas_algorithm/code/c/thomas.c index 3aed94e8e..415d31d97 100644 --- a/contents/thomas_algorithm/code/c/thomas.c +++ b/contents/thomas_algorithm/code/c/thomas.c @@ -16,7 +16,7 @@ void thomas(double * const a, double * const b, double * const c, x[i] = (x[i] - a[i] * x[i - 1]) * scale; } - for (int i = size - 2; i >= 0; --i) { + for (size_t i = size - 2; i < size - 1; --i) { x[i] -= y[i] * x[i + 1]; } } diff --git a/literature.bib b/literature.bib index b1a8e154e..1ef1e5c12 100644 --- a/literature.bib +++ b/literature.bib @@ -447,3 +447,74 @@ @misc{rmsd_wiki year={2021} } + +#------------------------------------------------------------------------------# +# Cryptography +#------------------------------------------------------------------------------# + +@misc{ceasar_cipher_wiki, + title={Wikipedia: Ceasar Cipher}, + url={https://en.wikipedia.org/wiki/Caesar_cipher}, + year={2022} +} + +@misc{post-quantum_crypto_wiki, + title={Wikipedia: Post-quantum Cryptography}, + url={https://en.wikipedia.org/wiki/Post-quantum_cryptography}, + year={2022} +} + +@misc{Kerckhoffs_principle_wiki, + title={Wikipedia: Kerckhoffs's principle}, + url={https://en.wikipedia.org/wiki/Kerckhoffs%27s_principle}, + year={2022} +} + +@misc{rot13_wiki, + title={Wikipedia: ROT13}, + url={https://en.wikipedia.org/wiki/ROT13}, + year={2022} +} + +@misc{CC_permutation, + title={Crypto Corner: Permutation Cipher}, + url={https://crypto.interactive-maths.com/permutation-cipher.html}, + year={2022} +} + +@misc{xor_cipher_wiki, + title={Wikipedia: XOR cipher}, + url={https://en.wikipedia.org/wiki/XOR_cipher}, + year={2022} +} + +@misc{DES_wiki, + title={Wikipedia: Data Encryption Standard}, + url={https://en.wikipedia.org/wiki/Data_Encryption_Standard}, + year={2022} +} + +@misc{AES_wiki, + title={Wikipedia: Advanced Encryption Standard}, + url={https://en.wikipedia.org/wiki/Advanced_Encryption_Standard}, + year={2022} +} + +@misc{blowfish_cipher_wiki, + title={Wikipedia: Blowfish (cipher)}, + url={https://en.wikipedia.org/wiki/Blowfish_(cipher)}, + year={2022} +} + +@misc{RSA_wiki, + title={Wikipedia: RSA (cryptosystem)}, + url={https://en.wikipedia.org/wiki/RSA_(cryptosystem)}, + year={2022} +} + +@misc{ECC_crypto_wiki, + title={Wikipedia: Elliptic-curve cryptography}, + url={https://en.wikipedia.org/wiki/Elliptic-curve_cryptography}, + year={2022} +} +