diff --git a/src/calculate_bucket.hpp b/src/calculate_bucket.hpp index 9c22c5df8..ec00ea190 100644 --- a/src/calculate_bucket.hpp +++ b/src/calculate_bucket.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include "util.hpp" #include "bits.hpp" @@ -60,20 +61,22 @@ std::map kVectorLens = { {8, 0} }; -// Precomputed shifts that specify which entries match with which other entries -// in adjacent buckets. -uint16_t matching_shifts_c[2][kC]; - -// Performs the precomputation of shifts. -void precompute_shifts() { +uint32_t L_targets[2][kBC][kExtraBitsPow]; +bool initialized = false; +void load_tables() +{ for (uint8_t parity = 0; parity < 2; parity++) { - for (uint16_t r = 0; r < kExtraBitsPow; r++) { - uint16_t v = (uint16_t)pow((2 * r + parity), 2) % kC; - matching_shifts_c[parity][r] = v; + for (uint16_t i = 0; i < kBC; i++) { + uint16_t indJ = i / kC; + for (uint16_t m = 0; m < kExtraBitsPow; m++) { + uint16_t yr = ((indJ + m) % kB) * kC + (((2*m + parity) * (2*m + parity) + i) % kC); + L_targets[parity][i][m] = yr; + } } } } + // Class to evaluate F1 class F1Calculator { public: @@ -87,9 +90,6 @@ class F1Calculator { // Loads the key into the global AES context aes_load_key(this->aes_key_, 32); - - // Precomputes the shifts, this is only done once - precompute_shifts(); } inline ~F1Calculator() { @@ -243,15 +243,13 @@ class FxCalculator { // for these f functions (as opposed to f1, which uses a 32 byte key). Note that, however, // block sizes are still 128 bits (32 bytes). aes_load_key(this->aes_key_, 16); - - // One time precomputation of the shifts - precompute_shifts(); - - // Preallocates vector to be used for matching - for (uint16_t i = 0; i < kC; i++) { + for (uint16_t i = 0; i < kBC; i++) { std::vector new_vec; - this->R_positions.push_back(new_vec); - this->R_bids.push_back(new_vec); + this->rmap.push_back(new_vec); + } + if(!initialized) { + initialized = true; + load_tables(); } } @@ -352,43 +350,32 @@ class FxCalculator { inline std::vector> FindMatches(const std::vector& bucket_L, const std::vector& bucket_R) { std::vector> matches; - for (uint16_t i = 0; i < kC; i++) { - this->R_bids[i].clear(); - this->R_positions[i].clear(); - } uint16_t parity = (bucket_L[0].y / kBC) % 2; + for (uint16_t i = 0; i < rmap_clean.size(); i++) { + uint16_t yl = rmap_clean[i]; + this->rmap[yl].clear(); + } + rmap_clean.clear(); + + uint64_t remove = (bucket_R[0].y / kBC) * kBC; for (uint16_t pos_R = 0; pos_R < bucket_R.size(); pos_R++) { - R_bids[bucket_R[pos_R].y % kC].push_back((bucket_R[pos_R].y % kBC) / kC); - R_positions[bucket_R[pos_R].y % kC].push_back(pos_R); + uint64_t r_y = bucket_R[pos_R].y - remove; + rmap[r_y].push_back(pos_R); + rmap_clean.push_back(r_y); } + uint64_t remove_y = remove - kBC; for (uint16_t pos_L = 0; pos_L < bucket_L.size(); pos_L++) { - uint16_t yl_bid = (bucket_L[pos_L].y % kBC) / kC; - uint16_t yl_cid = bucket_L[pos_L].y % kC; - for (uint8_t m = 0; m < kExtraBitsPow; m++) { - uint16_t target_bid = (yl_bid + m); - uint16_t target_cid = yl_cid + matching_shifts_c[parity][m]; - - // This is faster than % - if (target_bid >= kB) { - target_bid -= kB; - } - if (target_cid >= kC) { - target_cid -= kC; - } - - for (uint32_t i = 0; i < R_bids[target_cid].size(); i++) { - uint16_t R_bid = R_bids[target_cid][i]; - if (target_bid == R_bid) { - uint64_t yl_bucket = bucket_L[pos_L].y / kBC; - if (yl_bucket + 1 == bucket_R[R_positions[target_cid][i]].y / kBC) { - matches.push_back(std::make_pair(pos_L, R_positions[target_cid][i])); - } - } + uint64_t r = bucket_L[pos_L].y - remove_y; + for (uint8_t i = 0; i < kExtraBitsPow; i++) { + uint16_t r_target = L_targets[parity][r][i]; + for (uint8_t j = 0; j < rmap[r_target].size(); j++) { + matches.push_back(std::make_pair(pos_L, rmap[r_target][j])); } } } + return matches; } @@ -402,8 +389,8 @@ class FxCalculator { uint8_t block_3[kBlockSizeBits/8]; uint8_t block_4[kBlockSizeBits/8]; uint8_t ciphertext[kBlockSizeBits/8]; - std::vector > R_positions; - std::vector > R_bids; + std::vector> rmap; + std::vector rmap_clean; }; #endif // SRC_CPP_CALCULATE_BUCKET_HPP_ diff --git a/src/verifier.hpp b/src/verifier.hpp index 350bbdd6a..b809affe5 100644 --- a/src/verifier.hpp +++ b/src/verifier.hpp @@ -79,9 +79,13 @@ class Verifier { vector bucket_R = {r_plot_entry}; // If there is no match, fails. - if (f.FindMatches(bucket_L, bucket_R).size() != 1) { + uint64_t cdiff = r_plot_entry.y / kBC - l_plot_entry.y / kBC; + if (cdiff != 1) { + return LargeBits(); + } else if (f.FindMatches(bucket_L, bucket_R).size() != 1) { return LargeBits(); } + std::pair results = f.CalculateBucket(ys[i], ys[i+1], metadata[i], metadata[i+1]); new_ys.push_back(std::get<0>(results));