From 5970a455d0671cdde4793350be073ad308884f45 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Fri, 3 Nov 2023 06:43:34 -0400 Subject: [PATCH] Replace crc dependency with our own crc implementation It's ~30 lines to remove 2 crates in our tree. --- Cargo.lock | 16 ----------- coins/monero/Cargo.toml | 1 - coins/monero/src/wallet/seed/classic.rs | 37 +++++++++++++++++++++---- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bbfca3301..0ff8880ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1313,21 +1313,6 @@ dependencies = [ "wasmtime-types", ] -[[package]] -name = "crc" -version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86ec7a15cbe22e59248fc7eadb1907dab5ba09372595da4d73dd805ed4417dfe" -dependencies = [ - "crc-catalog", -] - -[[package]] -name = "crc-catalog" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cace84e55f07e7301bae1c519df89cdad8cc3cd868413d3fdbdeca9ff3db484" - [[package]] name = "crc32fast" version = "1.3.2" @@ -4913,7 +4898,6 @@ version = "0.1.4-alpha" dependencies = [ "async-trait", "base58-monero", - "crc", "curve25519-dalek", "dalek-ff-group", "digest_auth", diff --git a/coins/monero/Cargo.toml b/coins/monero/Cargo.toml index 701f7ebbe..8725f62fa 100644 --- a/coins/monero/Cargo.toml +++ b/coins/monero/Cargo.toml @@ -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 } diff --git a/coins/monero/src/wallet/seed/classic.rs b/coins/monero/src/wallet/seed/classic.rs index b4b4c83a0..78c4f2099 100644 --- a/coins/monero/src/wallet/seed/classic.rs +++ b/coins/monero/src/wallet/seed/classic.rs @@ -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}; @@ -101,11 +99,38 @@ fn checksum_index(words: &[Zeroizing], lang: &WordList) -> usize { *trimmed_words += &trim(w, lang.unique_prefix_length); } - let crc = Crc::::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