Skip to content

Commit

Permalink
Replace crc dependency with our own crc implementation
Browse files Browse the repository at this point in the history
It's ~30 lines to remove 2 crates in our tree.
  • Loading branch information
kayabaNerve committed Nov 3, 2023
1 parent 4c9e3b0 commit 5970a45
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
16 changes: 0 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion coins/monero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ rand_chacha = { version = "0.3", default-features = false }
# Used to select decoys
rand_distr = { version = "0.4", default-features = false }

crc = { version = "3", default-features = false }
sha3 = { version = "0.10", default-features = false }
pbkdf2 = { version = "0.12", features = ["simple"], default-features = false }

Expand Down
37 changes: 31 additions & 6 deletions coins/monero/src/wallet/seed/classic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use std_shims::{
use zeroize::{Zeroize, Zeroizing};
use rand_core::{RngCore, CryptoRng};

use crc::{Crc, CRC_32_ISO_HDLC};

use curve25519_dalek::scalar::Scalar;

use crate::{random_scalar, wallet::seed::SeedError};
Expand Down Expand Up @@ -101,11 +99,38 @@ fn checksum_index(words: &[Zeroizing<String>], lang: &WordList) -> usize {
*trimmed_words += &trim(w, lang.unique_prefix_length);
}

let crc = Crc::<u32>::new(&CRC_32_ISO_HDLC);
let mut digest = crc.digest();
digest.update(trimmed_words.as_bytes());
const fn crc32_table() -> [u32; 256] {
let poly = 0xedb88320u32;

let mut res = [0; 256];
let mut i = 0;
while i < 256 {
let mut entry = i;
let mut b = 0;
while b < 8 {
let trigger = entry & 1;
entry >>= 1;
if trigger == 1 {
entry ^= poly;
}
b += 1;
}
res[i as usize] = entry;
i += 1;
}

res
}
const CRC32_TABLE: [u32; 256] = crc32_table();

let trimmed_words = trimmed_words.as_bytes();
let mut checksum = u32::MAX;
for i in 0 .. trimmed_words.len() {
checksum = CRC32_TABLE[usize::from(u8::try_from(checksum % 256).unwrap() ^ trimmed_words[i])] ^
(checksum >> 8);
}

usize::try_from(digest.finalize()).unwrap() % words.len()
usize::try_from(!checksum).unwrap() % words.len()
}

// Convert a private key to a seed
Expand Down

0 comments on commit 5970a45

Please sign in to comment.