From f71c3ccd420e5bd6f019cbdcae5c432ebcd0a48e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 22 Sep 2023 16:03:12 +0300 Subject: [PATCH] md5: minor code tweaks, bump to v0.10.6 --- .github/workflows/md5.yml | 12 ++++++++++++ Cargo.lock | 2 +- md5/CHANGELOG.md | 6 ++++++ md5/Cargo.toml | 2 +- md5/src/compress/loongarch64_asm.rs | 18 +++++++----------- md5/src/compress/soft.rs | 5 +---- md5/src/{compress => }/consts.rs | 6 +++++- md5/src/lib.rs | 11 +++++++---- 8 files changed, 40 insertions(+), 22 deletions(-) rename md5/src/{compress => }/consts.rs (80%) diff --git a/.github/workflows/md5.yml b/.github/workflows/md5.yml index d95499886..a1d2ce88d 100644 --- a/.github/workflows/md5.yml +++ b/.github/workflows/md5.yml @@ -64,6 +64,18 @@ jobs: - uses: RustCrypto/actions/cargo-hack-install@master - run: cargo hack test --feature-powerset + # Build-only test of the LoongArch64 assembly backend + loongarch64_asm: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: RustCrypto/actions/cargo-cache@master + - uses: dtolnay/rust-toolchain@master + with: + toolchain: 1.72 + targets: loongarch64-unknown-linux-gnu + - run: cargo build --target loongarch64-unknown-linux-gnu --features loongarch64_asm + minimal-versions: uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master with: diff --git a/Cargo.lock b/Cargo.lock index 361eac726..0afe260d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -149,7 +149,7 @@ checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "md-5" -version = "0.10.5" +version = "0.10.6" dependencies = [ "cfg-if", "digest", diff --git a/md5/CHANGELOG.md b/md5/CHANGELOG.md index 76e592b7e..65c30dd8f 100644 --- a/md5/CHANGELOG.md +++ b/md5/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.10.6 (2023-09-22) +### Added +- `asm!`-based backend for LoongArch64 targets gated behind `loongarch64_asm` feature [#505] + +[#505]: https://github.com/RustCrypto/hashes/pull/505 + ## 0.10.5 (2022-09-22) ### Added - Feature-gated OID support ([#413]) diff --git a/md5/Cargo.toml b/md5/Cargo.toml index e8f89ad1d..8103cd8a4 100644 --- a/md5/Cargo.toml +++ b/md5/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "md-5" -version = "0.10.5" +version = "0.10.6" description = "MD5 hash function" authors = ["RustCrypto Developers"] license = "MIT OR Apache-2.0" diff --git a/md5/src/compress/loongarch64_asm.rs b/md5/src/compress/loongarch64_asm.rs index 2af53af35..3bbd8523e 100644 --- a/md5/src/compress/loongarch64_asm.rs +++ b/md5/src/compress/loongarch64_asm.rs @@ -1,11 +1,5 @@ //! LoongArch64 assembly backend -use core::arch::asm; - -#[path = "consts.rs"] -mod consts; -use consts::*; - macro_rules! c { ($($l:expr)*) => { concat!($($l ,)*) @@ -74,7 +68,7 @@ pub fn compress(state: &mut [u32; 4], blocks: &[[u8; 64]]) { } unsafe { - asm!( + core::arch::asm!( "42:", "move $t0, $a4", @@ -99,6 +93,7 @@ pub fn compress(state: &mut [u32; 4], blocks: &[[u8; 64]]) { round0!("$t3", "$t0", "$t1", "$t2", 13, 12, 13), round0!("$t2", "$t3", "$t0", "$t1", 14, 17, 14), round0!("$t1", "$t2", "$t3", "$t0", 15, 22, 15), + round1!("$t0", "$t1", "$t2", "$t3", 1, 5, 16), round1!("$t3", "$t0", "$t1", "$t2", 6, 9, 17), round1!("$t2", "$t3", "$t0", "$t1", 11, 14, 18), @@ -114,6 +109,7 @@ pub fn compress(state: &mut [u32; 4], blocks: &[[u8; 64]]) { round1!("$t0", "$t1", "$t2", "$t3", 13, 5, 28), round1!("$t3", "$t0", "$t1", "$t2", 2, 9, 29), round1!("$t2", "$t3", "$t0", "$t1", 7, 14, 30), + round1!("$t1", "$t2", "$t3", "$t0", 12, 20, 31), round2!("$t0", "$t1", "$t2", "$t3", 5, 4, 32), round2!("$t3", "$t0", "$t1", "$t2", 8, 11, 33), @@ -131,6 +127,7 @@ pub fn compress(state: &mut [u32; 4], blocks: &[[u8; 64]]) { round2!("$t3", "$t0", "$t1", "$t2", 12, 11, 45), round2!("$t2", "$t3", "$t0", "$t1", 15, 16, 46), round2!("$t1", "$t2", "$t3", "$t0", 2, 23, 47), + round3!("$t0", "$t1", "$t2", "$t3", 0, 6, 48), round3!("$t3", "$t0", "$t1", "$t2", 7, 10, 49), round3!("$t2", "$t3", "$t0", "$t1", 14, 15, 50), @@ -158,14 +155,13 @@ pub fn compress(state: &mut [u32; 4], blocks: &[[u8; 64]]) { "addi.d $a2, $a2, -1", "bnez $a2, 42b", + inout("$a1") blocks.as_ptr() => _, + inout("$a2") blocks.len() => _, + in("$a3") crate::consts::RC.as_ptr(), inout("$a4") state[0], inout("$a5") state[1], inout("$a6") state[2], inout("$a7") state[3], - inout("$a1") blocks.as_ptr() => _, - inout("$a2") blocks.len() => _, - - in("$a3") RC.as_ptr(), // Clobbers out("$t0") _, diff --git a/md5/src/compress/soft.rs b/md5/src/compress/soft.rs index c41f2bb41..40630a121 100644 --- a/md5/src/compress/soft.rs +++ b/md5/src/compress/soft.rs @@ -1,10 +1,7 @@ #![allow(clippy::many_single_char_names, clippy::unreadable_literal)] +use crate::consts::RC; use core::convert::TryInto; -#[path = "consts.rs"] -mod consts; -use consts::*; - #[inline(always)] fn op_f(w: u32, x: u32, y: u32, z: u32, m: u32, c: u32, s: u32) -> u32 { ((x & y) | (!x & z)) diff --git a/md5/src/compress/consts.rs b/md5/src/consts.rs similarity index 80% rename from md5/src/compress/consts.rs rename to md5/src/consts.rs index 2b6d13042..67ce879a7 100644 --- a/md5/src/compress/consts.rs +++ b/md5/src/consts.rs @@ -1,4 +1,8 @@ -pub const RC: [u32; 64] = [ +pub(crate) const STATE_INIT: [u32; 4] = [0x6745_2301, 0xEFCD_AB89, 0x98BA_DCFE, 0x1032_5476]; + +// TODO: remove `allow` on deprecation of `md5-asm` +#[allow(dead_code)] +pub(crate) static RC: [u32; 64] = [ // round 1 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, diff --git a/md5/src/lib.rs b/md5/src/lib.rs index 085275ba9..a45de2f11 100644 --- a/md5/src/lib.rs +++ b/md5/src/lib.rs @@ -33,7 +33,7 @@ pub use digest::{self, Digest}; mod compress; -use compress::compress; +pub(crate) mod consts; use core::{fmt, slice::from_ref}; #[cfg(feature = "oid")] @@ -47,6 +47,7 @@ use digest::{ typenum::{Unsigned, U16, U64}, HashMarker, Output, }; + /// Core MD5 hasher state. #[derive(Clone)] pub struct Md5Core { @@ -72,7 +73,7 @@ impl UpdateCore for Md5Core { #[inline] fn update_blocks(&mut self, blocks: &[Block]) { self.block_len = self.block_len.wrapping_add(blocks.len() as u64); - compress(&mut self.state, convert(blocks)) + compress::compress(&mut self.state, convert(blocks)) } } @@ -85,7 +86,9 @@ impl FixedOutputCore for Md5Core { .wrapping_add(buffer.get_pos() as u64) .wrapping_mul(8); let mut s = self.state; - buffer.len64_padding_le(bit_len, |b| compress(&mut s, convert(from_ref(b)))); + buffer.len64_padding_le(bit_len, |b| { + compress::compress(&mut s, convert(from_ref(b))) + }); for (chunk, v) in out.chunks_exact_mut(4).zip(s.iter()) { chunk.copy_from_slice(&v.to_le_bytes()); } @@ -97,7 +100,7 @@ impl Default for Md5Core { fn default() -> Self { Self { block_len: 0, - state: [0x6745_2301, 0xEFCD_AB89, 0x98BA_DCFE, 0x1032_5476], + state: consts::STATE_INIT, } } }