What's next? What will I have to write on my main csharp file to run that backwards (mnemonic->hex) function?
I don't really get what you're trying to make but based on your other topic (public key to mnemonic) I assume you don't want the checksum validation and you want to enter a mnemonic and get the 264 bit entropy out. With that assumption the code would be this:
using Autarkysoft.Bitcoin;
using Autarkysoft.Bitcoin.Encoders;
using Autarkysoft.Bitcoin.ImprovementProposals;
using System;
using System.Linq;
using System.Text;
public string MnToHex(string mnemonic, BIP0039.WordLists wl = BIP0039.WordLists.English)
{
if (string.IsNullOrWhiteSpace(mnemonic))
throw new ArgumentNullException(nameof(mnemonic), "Seed can not be null or empty!");
string[] allWords = BIP0039.GetAllWords(wl);
string[] words = mnemonic.Normalize(NormalizationForm.FormKD)
.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (!words.All(x => allWords.Contains(x)))
{
throw new ArgumentException(nameof(mnemonic), "Seed has invalid words.");
}
if (!new int[] { 12, 15, 18, 21, 24 }.Contains(words.Length))
{
throw new FormatException("Invalid seed length. It should be ∈{12, 15, 18, 21, 24}");
}
uint[] wordIndexes = new uint[words.Length];
for (int i = 0; i < words.Length; i++)
{
wordIndexes[i] = (uint)Array.IndexOf(allWords, words[i]);
}
int ENTCS = words.Length * 11;
int CS = ENTCS % 32;
int ENT = ENTCS - CS;
byte[] entropy = new byte[(ENT / 8) + 1];
int itemIndex = 0;
int bitIndex = 0;
for (int i = 0; i < entropy.Length - 1; i++)
{
if (bitIndex + 8 <= 11)
{
entropy[i] = (byte)(wordIndexes[itemIndex] >> (3 - bitIndex));
}
else
{
entropy[i] = (byte)(((wordIndexes[itemIndex] << (bitIndex - 3)) & 0xff) |
(wordIndexes[itemIndex + 1] >> (14 - bitIndex)));
}
bitIndex += 8;
if (bitIndex >= 11)
{
bitIndex -= 11;
itemIndex++;
}
}
uint mask = (1U << CS) - 1;
entropy[^1] = (byte)(wordIndexes[itemIndex] & mask);
return entropy.ToBase16();
}
HexToMn works only with 33 byte entropy (66 char hex):
public string HexToMn(string hex, BIP0039.WordLists wl = BIP0039.WordLists.English)
{
byte[] entropy = Base16.Decode(hex);
if (entropy.Length != 33)
throw new ArgumentOutOfRangeException();
byte[] ba = entropy.ConcatFast(new byte[3]);
uint[] bits = new uint[9];
for (int i = 0, j = 0; i < ba.Length - 1; i += 4, j++)
{
bits[j] = (uint)(ba[i + 3] | (ba[i + 2] << 8) | (ba[i + 1] << 16) | (ba[i] << 24));
}
int itemIndex = 0;
int bitIndex = 0;
uint[] wordIndexes = new uint[24];
for (int i = 0; i < 24; i++)
{
if (bitIndex + 11 <= 32)
{
wordIndexes[i] = (bits[itemIndex] << bitIndex) >> 21;
}
else
{
wordIndexes[i] = ((bits[itemIndex] << bitIndex) >> 21) |
(bits[itemIndex + 1] >> (53 - bitIndex));
}
bitIndex += 11;
if (bitIndex >= 32)
{
bitIndex -= 32;
itemIndex++;
}
}
StringBuilder sb = new StringBuilder(wordIndexes.Length * 8);
string[] allWords = BIP0039.GetAllWords(wl);
for (int i = 0; i < wordIndexes.Length; i++)
{
sb.Append($"{allWords[wordIndexes[i]]} ");
}
sb.Length--;
return sb.ToString();
}