Skip to content

Commit

Permalink
Supprt to define search space - will enable jobs to be split across m…
Browse files Browse the repository at this point in the history
…ultiple 'containers'
  • Loading branch information
martynp committed Sep 11, 2016
1 parent e7d3e04 commit 5145102
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
19 changes: 14 additions & 5 deletions bf_crc.cc
Expand Up @@ -155,7 +155,7 @@ void bf_crc::print_settings(void)
if (polynomial_ > 0)
std::cout << ": 0x" << std::hex << polynomial_ << std::dec << std::endl;
else
std::cout << ": 0x0 to 0x" << std::hex << max_value(crc_width_) << std::dec << std::endl;
std::cout << ": 0x" << std::hex << polynomial_start_ << " to 0x" << std::hex << polynomial_end_ << std::dec << std::endl;

if (probe_initial_)
std::cout << "Initial value : 0x0 to 0x" << std::hex << max_value(crc_width_) << std::dec << std::endl;
Expand Down Expand Up @@ -307,20 +307,29 @@ int bf_crc::do_brute_force(int num_threads, std::vector<test_vector_t> test_vect
int thread_number = 0;

// Polystep is how the search polynomials are spread betweeen threads
int poly_step = polynomial_ > 0 ? 1 : max_value(crc_width_)/num_threads;
int poly_count = polynomial_end_ - polynomial_start_;
int poly_step = polynomial_ > 0 ? 1 : poly_count/num_threads;

// Handle low polynomial count
if (poly_step == 0) poly_step = 1;

for(int thread_number = 0; thread_number < num_threads; thread_number++) {

uint32_t search_end = polynomial_ > 0 ? polynomial_ : (thread_number + 1) * poly_step - 1;
uint32_t search_end = 0;
if (polynomial_ > 0)
search_end = polynomial_;
else
search_end = polynomial_start_ + (thread_number + 1) * poly_step - 1;

// Due to math the last caluclate will wrap to zero?
if (thread_number == num_threads-1 && polynomial_ == 0)
search_end = max_value(crc_width_);
search_end = polynomial_end_;

uint32_t search_start = polynomial_ > 0 ? polynomial_ : thread_number * poly_step;
uint32_t search_start = 0;
if (polynomial_ > 0)
search_start = polynomial_;
else
search_start = polynomial_start_ + thread_number * poly_step;

if (verbose_) {
std::cout << "Starting Thread " << thread_number << ", searching from ";
Expand Down
47 changes: 37 additions & 10 deletions bf_crc.hpp
Expand Up @@ -46,8 +46,7 @@ class bf_crc {
bool probe_initial,
uint32_t initial,
bool probe_reflected_input,
bool probe_reflected_output)
{
bool probe_reflected_output) {
set_parameters( crc_width,
polynomial,
probe_final_xor,
Expand All @@ -59,11 +58,14 @@ class bf_crc {
crc_model_match_.clear();
}

void print_settings(void);
void print_settings(void);

private:
uint16_t crc_width_;
uint32_t polynomial_;
uint32_t polynomial_;
bool polynomial_range_;
uint32_t polynomial_start_;
uint32_t polynomial_end_;
bool probe_final_xor_;
uint32_t final_xor_;
bool probe_initial_;
Expand All @@ -77,10 +79,31 @@ class bf_crc {
std::vector<crc_model_t> crc_model_match_;

public:
void set_crc_width(uint16_t var) { crc_width_ = var; update_test_vector_count(); }
void set_crc_width(uint16_t var) {
crc_width_ = var;
update_test_vector_count();
polynomial_range_ = false;
polynomial_start_ = 0;
polynomial_end_ = max_value(crc_width_);
update_test_vector_count();
}
uint16_t crc_width() const { return crc_width_; }
void set_polynomial(uint32_t var) { polynomial_ = var; update_test_vector_count(); }
uint32_t polynomial() const { return polynomial_; }
void set_polynomial_range(bool var) { polynomial_range_ = var; set_crc_width(crc_width_); }
bool polynomial_range() const { return polynomial_range_; }
void set_polynomial_start(uint32_t var) {
polynomial_start_ = var;
polynomial_range_ = true;
update_test_vector_count();
}
uint32_t polynomial_start() const { return polynomial_start_; }
void set_polynomial_end(uint32_t var) {
polynomial_end_ = var;
polynomial_range_ = true;
update_test_vector_count();
}
uint32_t polynomial_end() const { return polynomial_end_; }
void set_probe_final_xor(bool var) { probe_final_xor_ = var; update_test_vector_count(); }
bool probe_final_xor() const { return probe_final_xor_; }
void set_final_xor(uint32_t var) { final_xor_ = var; }
Expand Down Expand Up @@ -115,16 +138,20 @@ class bf_crc {
{
test_vector_count_ = 0;

if (polynomial_ > 0)
// TODO: Check polynomial range and throw exception if too large
if (polynomial_ > 0) {
test_vector_count_ = 1;
else
test_vector_count_ = (uint64_t)1+max_value(crc_width_);
} else if (polynomial_range_) {
test_vector_count_ = polynomial_end_ - polynomial_start_;
} else {
test_vector_count_ = (uint64_t)max_value(crc_width_);
}

if (probe_final_xor_)
test_vector_count_ *= (uint64_t)1+max_value(crc_width_);
test_vector_count_ *= (uint64_t)max_value(crc_width_);

if (probe_initial_)
test_vector_count_ *= (uint64_t)1+max_value(crc_width_);
test_vector_count_ *= (uint64_t)max_value(crc_width_);

if (probe_reflected_input_)
test_vector_count_ *= 2;
Expand Down
13 changes: 13 additions & 0 deletions bruteforce-crc.cc
Expand Up @@ -150,6 +150,8 @@ int main(int argc, char *argv[]) {
("final-xor", po::value<uint32_t>(), "Final xor (default: 0)")
("probe-final-xor", po::value<bool>(), "Bruteforce the final-xor, overrides final-xor (default: false)")
("poly", po::value<uint32_t>(), "Truncated polynomial (default: bruteforced)")
("poly-start", po::value<uint32_t>(), "Start of polynomial search space (default: 0)")
("poly-end", po::value<uint32_t>(), "End of polynomial search space (default (2^width - 1))")
("probe-reflected-input", po::value<bool>(), "Probe for reflect input (default: false)")
("probe-reflected-output", po::value<bool>(), "Probe for reflect remainder output (default: false)")
;
Expand Down Expand Up @@ -208,6 +210,17 @@ int main(int argc, char *argv[]) {
reflected_input, // Probe Reflected Input?
reflected_output); // Probe Reflected Output?

// The command line input can limit the search range
if (vm.count("poly-start")) {
uint32_t poly_start = vm["poly-start"].as<uint32_t>();
crc_bruteforce->set_polynomial_start(poly_start);
}

if (vm.count("poly-end")) {
uint32_t poly_end = vm["poly-end"].as<uint32_t>();
crc_bruteforce->set_polynomial_end(poly_end);
}

crc_bruteforce->set_verbose(verbose);

int found = crc_bruteforce->do_brute_force(num_threads, test_vectors);
Expand Down
2 changes: 1 addition & 1 deletion test.sh
@@ -1,3 +1,3 @@
#!/bin/sh

./bruteforce-crc --verbose true --width 10 --start 0 --end 49 --offs-crc 49 --file testmessage_1.bits --probe-reflected-input true --probe-reflected-output true --probe-initial true --output testresult.csv
./bruteforce-crc --verbose true --width 10 --start 0 --end 49 --offs-crc 49 --file testmessage_1.bits --probe-reflected-input true --probe-reflected-output true --probe-initial true --output testresult.csv --poly-start 100 --poly-end 200

0 comments on commit 5145102

Please sign in to comment.