cuda function generate invalid mnemonic checksum
device void generate_mnemonic(curandState *state, char *mnemonic) { int word_indices[12]; // Now we need 12 words
// Generate 128 bits of entropy for a 12-word mnemonic
uint8_t entropy[16]; // 128 bits of entropy (16 bytes)
for (int i = 0; i < 16; i++) {
entropy[i] = curand(state) % 256; // Random byte generation
}
// Calculate the SHA-256 hash of the entropy
uint8_t hash[32];
sha256((const char*)entropy, 16, (char*)hash);
// The checksum is the first (entropy_length / 32) bits of the hash.
// For 128-bit entropy, the checksum is the first 4 bits of the hash.
uint8_t checksum_bits = hash[0] >> 4; // Extract first 4 bits for checksum
// Combine entropy and checksum for the mnemonic generation
// Now we have to take 128 bits of entropy + 4 bits of checksum (total 132 bits)
uint8_t bits[16]; // To store the combined bits of entropy and checksum
for (int i = 0; i < 16; i++) {
bits[i] = entropy[i]; // Copy entropy to bits
}
bits[15] = (bits[15] & 0xF0) | (checksum_bits & 0x0F); // Append checksum
// Now, split the 132 bits into 11-bit groups and find the corresponding words
for (int i = 0; i < 12; i++) {
int bit_pos = i * 11; // Start bit position for the 11-bit word
int byte_pos = bit_pos / 8; // Find the byte position
int shift = bit_pos % 8; // Bit shift within the byte
// Extract 11 bits (this is the key part for getting the word index)
int word_index = 0;
for (int j = 0; j < 11; j++) {
int bit_offset = (bit_pos + j) % 8;
int byte_offset = (bit_pos + j) / 8;
word_index |= ((bits[byte_offset] >> (7 - bit_offset)) & 1) << (10 - j);
}
// Map the 11-bit word index to a word from the wordlist
word_indices[i] = word_index % WORDS_COUNT; // WORDS_COUNT is 2048
}
// Construct the mnemonic string
mnemonic[0] = '\0'; // Start with an empty string
for (int i = 0; i < 12; i++) {
my_strcat(mnemonic, WORDS[word_indices[i]]);
my_strcat(mnemonic, " ");
}
}
from Recent Questions - Bitcoin Stack Exchange https://ift.tt/AZdS3ER
via IFTTT