First of all, I know absolutely nothing about c++. I develop with Python and I complained about speed, someone suggested I should use c++, so I went to chatgpt and got a c++ script but it is slower than my python script. My python script does 1400 iterations/s while the c++ script does 60 iterations/s.
It is a read and write script that loads a text file containing mnemonics, calculates the valid ones and write them out to another text file.
If i can get 5x the speed of my python script, I am ok. Any help would be appreciated.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <bitset>
#include <vector>
#include <openssl/sha.h>
#include <algorithm>
#include <stdexcept>
#include <unordered_map>
std::string normalize_string(const std::string& mnemonic) {
std::string normalized = mnemonic;
std::transform(normalized.begin(), normalized.end(), normalized.begin(), ::tolower);
return normalized;
}
std::string calculate_checksum(const std::string& binary_string) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, binary_string.c_str(), binary_string.size());
SHA256_Final(hash, &sha256);
std::string checksum;
for (int i = 0; i <SHA256_DIGEST_LENGTH; ++i) {
checksum += std::bitset<8>(hash[i]).to_string();
}
return checksum.substr(0, binary_string.size() / 32);;
}
int get_word_index(const std::string& word) {
std::unordered_map<std::string, int> wordlist = {
{"abandon", 0}, {"ability", 1}.... //all 2048 bip39 english words
};
auto it = wordlist.find(word);
if (it != wordlist.end()) {
return it->second;
} else {
return -1;
}
}
bool is_mnemonic_valid(const std::string& mnemonic) {
std::string normalized_mnemonic = normalize_string(mnemonic);
std::istringstream iss(normalized_mnemonic);
std::vector<std::string> words;
std::string word;
while (iss >> word) {
words.push_back(word);
}
int num_words = words.size();
if (num_words % 3 != 0 || num_words < 12 || num_words > 24) {
return false;
}
std::string binary_string;
for (const auto& word : words) {
int index = get_word_index(word);
if (index == -1) {
return false;
}
std::string binary_index = std::bitset<11>(index).to_string();
binary_string += binary_index;
}
std::string checksum = calculate_checksum(binary_string);
int entropy_size = num_words * 11 - num_words / 3;
std::string stored_checksum = binary_string.substr(entropy_size);
return checksum == stored_checksum;
}
void process_mnemonics(const std::string& input_file_name, const std::string& output_file_name) {
std::ifstream input_file(input_file_name);
std::ofstream output_file(output_file_name);
if (!input_file.is_open()) {
std::cerr << "Error opening input file." << input_file_name << std::endl;
return;
}
if (!output_file.is_open()) {
std::cerr << "Error opening output file." << output_file_name << std::endl;
return;
}
std::string mnemonic;
int total_mnemonics = 0;
int valid_mnemonics = 0;
while (std::getline(input_file, mnemonic)) {
total_mnemonics++;
std::string normalized_mnemonic = normalize_string(mnemonic);
bool is_valid = is_mnemonic_valid(normalized_mnemonic);
if (is_valid) {
output_file << normalized_mnemonic << std::endl;
valid_mnemonics++;
}
std::cout << " Processed " << total_mnemonics << " mnemonics. Found " << valid_mnemonics << " valid. " << std::endl;
}
std::cout << "Processing complete. Total: " << total_mnemonics << ", Valid: " << valid_mnemonics << std::endl;
input_file.close();
output_file.close();
}
int main() {
std::string input_file_name = "gain1.txt";
std::string output_file_name = "valid.txt";
process_mnemonics(input_file_name, output_file_name);
return 0;
}
Sirkastiq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.