bip39/language/
mod.rs

1use core::fmt;
2
3#[cfg(feature = "chinese-simplified")]
4mod chinese_simplified;
5#[cfg(feature = "chinese-traditional")]
6mod chinese_traditional;
7#[cfg(feature = "czech")]
8mod czech;
9mod english;
10#[cfg(feature = "french")]
11mod french;
12#[cfg(feature = "italian")]
13mod italian;
14#[cfg(feature = "japanese")]
15mod japanese;
16#[cfg(feature = "korean")]
17mod korean;
18#[cfg(feature = "spanish")]
19mod spanish;
20
21/// The maximum number of languages enabled.
22pub(crate) const MAX_NB_LANGUAGES: usize = 9;
23
24/// Language to be used for the mnemonic phrase.
25///
26/// The English language is always available, other languages are enabled using
27/// the compilation features.
28#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
29pub enum Language {
30	/// The English language.
31	English,
32	#[cfg(feature = "chinese-simplified")]
33	/// The Simplified Chinese language.
34	SimplifiedChinese,
35	#[cfg(feature = "chinese-traditional")]
36	/// The Traditional Chinese language.
37	TraditionalChinese,
38	#[cfg(feature = "czech")]
39	/// The Czech language.
40	Czech,
41	#[cfg(feature = "french")]
42	/// The French language.
43	French,
44	#[cfg(feature = "italian")]
45	/// The Italian language.
46	Italian,
47	#[cfg(feature = "japanese")]
48	/// The Japanese language.
49	Japanese,
50	#[cfg(feature = "korean")]
51	/// The Korean language.
52	Korean,
53	#[cfg(feature = "spanish")]
54	/// The Spanish language.
55	Spanish,
56}
57
58impl Language {
59	/// The list of supported languages.
60	/// Language support is managed by compile features.
61	pub fn all() -> &'static [Language] {
62		&[
63			Language::English,
64			#[cfg(feature = "chinese-simplified")]
65			Language::SimplifiedChinese,
66			#[cfg(feature = "chinese-traditional")]
67			Language::TraditionalChinese,
68			#[cfg(feature = "czech")]
69			Language::Czech,
70			#[cfg(feature = "french")]
71			Language::French,
72			#[cfg(feature = "italian")]
73			Language::Italian,
74			#[cfg(feature = "japanese")]
75			Language::Japanese,
76			#[cfg(feature = "korean")]
77			Language::Korean,
78			#[cfg(feature = "spanish")]
79			Language::Spanish,
80		]
81	}
82
83	/// The word list for this language.
84	#[inline]
85	pub(crate) fn word_list(self) -> &'static [&'static str; 2048] {
86		match self {
87			Language::English => &english::WORDS,
88			#[cfg(feature = "chinese-simplified")]
89			Language::SimplifiedChinese => &chinese_simplified::WORDS,
90			#[cfg(feature = "chinese-traditional")]
91			Language::TraditionalChinese => &chinese_traditional::WORDS,
92			#[cfg(feature = "czech")]
93			Language::Czech => &czech::WORDS,
94			#[cfg(feature = "french")]
95			Language::French => &french::WORDS,
96			#[cfg(feature = "italian")]
97			Language::Italian => &italian::WORDS,
98			#[cfg(feature = "japanese")]
99			Language::Japanese => &japanese::WORDS,
100			#[cfg(feature = "korean")]
101			Language::Korean => &korean::WORDS,
102			#[cfg(feature = "spanish")]
103			Language::Spanish => &spanish::WORDS,
104		}
105	}
106
107	/// Returns true if all words in the list are guaranteed to
108	/// only be in this list and not in any other.
109	#[inline]
110	pub(crate) fn unique_words(self) -> bool {
111		match self {
112			Language::English => false,
113			#[cfg(feature = "chinese-simplified")]
114			Language::SimplifiedChinese => false,
115			#[cfg(feature = "chinese-traditional")]
116			Language::TraditionalChinese => false,
117			#[cfg(feature = "czech")]
118			Language::Czech => true,
119			#[cfg(feature = "french")]
120			Language::French => false,
121			#[cfg(feature = "italian")]
122			Language::Italian => true,
123			#[cfg(feature = "japanese")]
124			Language::Japanese => true,
125			#[cfg(feature = "korean")]
126			Language::Korean => true,
127			#[cfg(feature = "spanish")]
128			Language::Spanish => true,
129		}
130	}
131
132	/// Get words from the word list that start with the given prefix.
133	pub fn words_by_prefix(self, prefix: &str) -> &[&'static str] {
134		// The words in the word list are ordered lexicographically. This means
135		// that we cannot use `binary_search` to find words more efficiently,
136		// because the Rust ordering is based on the byte values. However, it
137		// does mean that words that share a prefix will follow each other.
138
139		let first = match self.word_list().iter().position(|w| w.starts_with(prefix)) {
140			Some(i) => i,
141			None => return &[],
142		};
143		let count = self.word_list()[first..].iter().take_while(|w| w.starts_with(prefix)).count();
144		&self.word_list()[first..first + count]
145	}
146
147	/// Get the index of the word in the word list.
148	#[inline]
149	pub(crate) fn find_word(self, word: &str) -> Option<u16> {
150		self.word_list().iter().position(|w| *w == word).map(|i| i as u16)
151	}
152}
153
154impl fmt::Display for Language {
155	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
156		fmt::Debug::fmt(self, f)
157	}
158}
159
160#[cfg(test)]
161mod tests {
162	use super::*;
163
164	#[cfg(all(
165		feature = "chinese-simplified",
166		feature = "chinese-traditional",
167		feature = "czech",
168		feature = "french",
169		feature = "italian",
170		feature = "japanese",
171		feature = "korean",
172		feature = "spanish"
173	))]
174	#[test]
175	fn validate_word_list_checksums() {
176		//! In this test, we ensure that the word lists are identical.
177		//!
178		//! They are as follows in the bips repository:
179		//! 5c5942792bd8340cb8b27cd592f1015edf56a8c5b26276ee18a482428e7c5726  chinese_simplified.txt
180		//! 417b26b3d8500a4ae3d59717d7011952db6fc2fb84b807f3f94ac734e89c1b5f  chinese_traditional.txt
181		//! 7e80e161c3e93d9554c2efb78d4e3cebf8fc727e9c52e03b83b94406bdcc95fc  czech.txt
182		//! 2f5eed53a4727b4bf8880d8f3f199efc90e58503646d9ff8eff3a2ed3b24dbda  english.txt
183		//! ebc3959ab7801a1df6bac4fa7d970652f1df76b683cd2f4003c941c63d517e59  french.txt
184		//! d392c49fdb700a24cd1fceb237c1f65dcc128f6b34a8aacb58b59384b5c648c2  italian.txt
185		//! 2eed0aef492291e061633d7ad8117f1a2b03eb80a29d0e4e3117ac2528d05ffd  japanese.txt
186		//! 9e95f86c167de88f450f0aaf89e87f6624a57f973c67b516e338e8e8b8897f60  korean.txt
187		//! 46846a5a0139d1e3cb77293e521c2865f7bcdb82c44e8d0a06a2cd0ecba48c0b  spanish.txt
188
189		use bitcoin_hashes::{sha256, Hash, HashEngine};
190
191		let checksums = [
192			(
193				"5c5942792bd8340cb8b27cd592f1015edf56a8c5b26276ee18a482428e7c5726",
194				Language::SimplifiedChinese,
195			),
196			(
197				"417b26b3d8500a4ae3d59717d7011952db6fc2fb84b807f3f94ac734e89c1b5f",
198				Language::TraditionalChinese,
199			),
200			("7e80e161c3e93d9554c2efb78d4e3cebf8fc727e9c52e03b83b94406bdcc95fc", Language::Czech),
201			("2f5eed53a4727b4bf8880d8f3f199efc90e58503646d9ff8eff3a2ed3b24dbda", Language::English),
202			("ebc3959ab7801a1df6bac4fa7d970652f1df76b683cd2f4003c941c63d517e59", Language::French),
203			("d392c49fdb700a24cd1fceb237c1f65dcc128f6b34a8aacb58b59384b5c648c2", Language::Italian),
204			(
205				"2eed0aef492291e061633d7ad8117f1a2b03eb80a29d0e4e3117ac2528d05ffd",
206				Language::Japanese,
207			),
208			("9e95f86c167de88f450f0aaf89e87f6624a57f973c67b516e338e8e8b8897f60", Language::Korean),
209			("46846a5a0139d1e3cb77293e521c2865f7bcdb82c44e8d0a06a2cd0ecba48c0b", Language::Spanish),
210		];
211		assert_eq!(MAX_NB_LANGUAGES, checksums.len());
212
213		for &(_sum, lang) in &checksums {
214			let mut digest = sha256::Hash::engine();
215			for (_idx, word) in lang.word_list().iter().enumerate() {
216				#[cfg(feature = "std")]
217				assert!(::unicode_normalization::is_nfkd(&word));
218				digest.input(word.as_bytes());
219				digest.input("\n".as_bytes());
220			}
221			#[cfg(feature = "std")]
222			assert_eq!(
223				sha256::Hash::from_engine(digest).to_string(),
224				_sum,
225				"word list for language {} failed checksum check",
226				lang,
227			);
228		}
229	}
230
231	#[test]
232	fn words_by_prefix() {
233		let lang = Language::English;
234
235		let res = lang.words_by_prefix("woo");
236		assert_eq!(res, ["wood", "wool"]);
237
238		let res = lang.words_by_prefix("");
239		assert_eq!(res.len(), 2048);
240
241		let res = lang.words_by_prefix("woof");
242		assert!(res.is_empty());
243	}
244
245	#[cfg(all(
246		feature = "chinese-simplified",
247		feature = "chinese-traditional",
248		feature = "czech",
249		feature = "french",
250		feature = "italian",
251		feature = "japanese",
252		feature = "korean",
253		feature = "spanish"
254	))]
255	#[test]
256	fn words_overlaps() {
257		use std::collections::HashMap;
258
259		// We keep a map of all words and the languages they occur in.
260		// Afterwards, we make sure that no word maps to multiple languages
261		// if either of those is guaranteed to have unique words.
262		let mut words: HashMap<&str, Vec<Language>> = HashMap::new();
263		for lang in Language::all().iter() {
264			for word in lang.word_list().iter() {
265				words.entry(word).or_insert(Vec::new()).push(*lang);
266			}
267		}
268
269		let mut ok = true;
270		for (word, langs) in words.into_iter() {
271			if langs.len() == 1 {
272				continue;
273			}
274			if langs.iter().any(|l| l.unique_words()) {
275				println!("Word {} is not unique: {:?}", word, langs);
276				ok = false;
277			}
278		}
279		assert!(ok);
280	}
281}