system void generate_mnemonic(curandState *state, char *mnemonic) {
int word_indices[12]; // Now we’d like 12 phrases
// 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 technology
}
// Calculate the SHA-256 hash of the entropy
uint8_t hash[32];
sha256((const char*)entropy, 16, (char*)hash);
// The checksum is the primary (entropy_length / 32) bits of the hash.
// For 128-bit entropy, the checksum is the primary 4 bits of the hash.
uint8_t checksum_bits = hash[0] >> 4; // Extract first 4 bits for checksum
// Mix entropy and checksum for the mnemonic technology
// Now we've to take 128 bits of entropy + 4 bits of checksum (whole 132 bits)
uint8_t bits[16]; // To retailer the mixed 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, cut up the 132 bits into 11-bit teams and discover the corresponding phrases
for (int i = 0; i < 12; i++) {
int bit_pos = i * 11; // Begin bit place for the 11-bit phrase
int byte_pos = bit_pos / 8; // Discover the byte place
int shift = bit_pos % 8; // Bit shift throughout the byte
// Extract 11 bits (that is the important thing half for getting the phrase index)
int word_index = 0;
for (int j = 0; j < 11; j++) = ((bits[byte_offset] >> (7 - bit_offset)) & 1) << (10 - j);
// Map the 11-bit phrase index to a phrase from the wordlist
word_indices[i] = word_index % WORDS_COUNT; // WORDS_COUNT is 2048
}
// Assemble the mnemonic string
mnemonic[0] = ' '; // Begin with an empty string
for (int i = 0; i < 12; i++) {
my_strcat(mnemonic, WORDS[word_indices[i]]);
my_strcat(mnemonic, " ");
}
}