Skip to content

Commit

Permalink
0
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Jul 18, 2024
1 parent 6b7d3ba commit 1b0611c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
24 changes: 15 additions & 9 deletions blockset-lib/src/sha2/hash_state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::uint::{
u256x::U256,
u32x,
u512x::{self, U512},
u128x, u256x::U256, u32x, u512x::{self, U512}
};

use super::compress::compress;
Expand Down Expand Up @@ -29,7 +27,7 @@ impl HashState {
}
self.len += len as u64;
if len < 511 - 64 {
data[0][0] |= self.len as u128;
data[1][1] |= u128x::swap32(self.len as u128);
self.hash = compress(self.hash, data);
} else {
self.hash = compress(self.hash, data);
Expand All @@ -45,22 +43,22 @@ mod tests {

use crate::{
sha2::{sha224::SHA224, sha256::SHA256},
uint::u512x,
uint::{u256x, u512x},
};

use super::HashState;

#[test]
#[wasm_bindgen_test]
fn test() {
let f = |init| {
let f = |init, k, len| {
let state = HashState::new(init);
state.end(u512x::ZERO, 0)
state.end(k, len)
};
// d14a028c_2a3a2bc9_476102bb_288234c4
// 15a2b01f_828ea62a_c5b3e42f
{
let mut h = f(SHA224);
let mut h = f(SHA224, u512x::ZERO, 0);
h[1] |= 0xFFFF_FFFF << 96;
assert_eq!(
h,
Expand All @@ -73,11 +71,19 @@ mod tests {
// e3b0c442_98fc1c14_9afbf4c8_996fb924
// 27ae41e4_649b934c_a495991b_7852b855
assert_eq!(
f(SHA256),
f(SHA256, u512x::ZERO, 0),
[
0x996fb924_9afbf4c8_98fc1c14_e3b0c442,
0x7852b855_a495991b_649b934c_27ae41e4,
],
);
// 5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9
assert_eq!(
f(SHA256, [[0x3000_0000, 0], [0, 0]], 8),
u256x::swap32([
0xc2dbc23_9dd4e91b4_6729d73a_27fb57e9,
0x5feceb6_6ffc86f38_d952786c_6d696c79,
])
);
}
}
11 changes: 11 additions & 0 deletions blockset-lib/src/uint/u128x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ pub const fn u32x4_wadd(a: u128, b: u128) -> u128 {
from_u32x4([wadd(a0, b0), wadd(a1, b1), wadd(a2, b2), wadd(a3, b3)])
}

#[inline(always)]
pub const fn swap64(a: u128) -> u128 {
(a >> 64) | (a << 64)
}

#[inline(always)]
pub const fn swap32(a: u128) -> u128 {
const MASK: u128 = 0xFFFF_FFFF_0000_0000_FFFF_FFFF;
swap64(((a >> 32) & MASK) | ((a & MASK) << 32))
}

#[inline(always)]
pub const fn shl(u: u128, i: i32) -> u128 {
match i {
Expand Down
4 changes: 4 additions & 0 deletions blockset-lib/src/uint/u256x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ pub const fn get_bit([a0, a1]: U256, i: u32) -> bool {
(x & 1) != 0
}

pub const fn swap32([a0, a1]: U256) -> U256 {
[u128x::swap32(a1), u128x::swap32(a0)]
}

#[cfg(test)]
mod test {
use wasm_bindgen_test::wasm_bindgen_test;
Expand Down
9 changes: 8 additions & 1 deletion blockset-lib/src/uint/u64x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ pub const fn div_rem(a: u64, b: u64) -> (u64, u64) {
(a / b, a % b)
}

#[inline(always)]
pub const fn swap32(a: u64) -> u64 {
(a >> 32) | (a << 32)
}

#[cfg(test)]
mod test {
use wasm_bindgen_test::wasm_bindgen_test;

use super::swap32;

const fn byte_swap(mut a: u64) -> u64 {
a =(a >> 32) | (a << 32);
a = swap32(a);
a = ((a >> 16) & 0x0000FFFF_0000FFFF) | ((a & 0x0000FFFF_0000FFFF) << 16);
((a >> 8) & 0x00FF00FF_00FF00FF) | ((a & 0x00FF00FF_00FF00FF) << 8)
}
Expand Down

0 comments on commit 1b0611c

Please sign in to comment.