diff --git a/blockset/src/app.rs b/blockset/src/app.rs index 4b71dd0b..2c6ba7b7 100644 --- a/blockset/src/app.rs +++ b/blockset/src/app.rs @@ -1,7 +1,7 @@ use crate::{ base32::{StrEx, ToBase32}, - digest224::Digest224, io::Io, + u224::U224, }; pub fn run(io: &mut impl Io) -> Result<(), &str> { @@ -11,7 +11,7 @@ pub fn run(io: &mut impl Io) -> Result<(), &str> { match command.as_str() { "validate" => { let b32 = a.next().ok_or("missing address")?; - let d = b32.from_base32::().ok_or("invalid address")?; + let d = b32.from_base32::().ok_or("invalid address")?; io.print("valid: "); io.println(&d.to_base32()); Ok(()) diff --git a/blockset/src/lib.rs b/blockset/src/lib.rs index 0a82e4f9..11bcb87f 100644 --- a/blockset/src/lib.rs +++ b/blockset/src/lib.rs @@ -2,14 +2,14 @@ mod app; mod ascii; mod base32; mod bit_vec; -mod digest224; mod io; mod overflow32; mod sha224; mod sigma32; -mod u32x16; -mod u32x4; -mod u32x8; +mod u128; +mod u224; +mod u256; +mod u512; #[cfg(test)] mod static_assert; diff --git a/blockset/src/sha224.rs b/blockset/src/sha224.rs index b7022874..37c6c51a 100644 --- a/blockset/src/sha224.rs +++ b/blockset/src/sha224.rs @@ -1,9 +1,10 @@ use crate::{ overflow32::{add, add3, add4}, sigma32::{BIG0, BIG1, SMALL0, SMALL1}, - u32x16::U512, - u32x4::{get_u32, to_u128, to_u32x4}, - u32x8::{u32x8_add, U256}, + u128::{from_u32x4, get_u32, to_u32x4}, + u224::U224, + u256::{to_u32x8, u32x8_add, U256}, + u512::U512, }; const fn round([s0, s1]: U256, i: usize, w: u128, k: u128) -> U256 { @@ -94,7 +95,7 @@ const fn w_round4(w: &U512, i: usize) -> u128 { w0[1] = w_round(w0[1], w0[2], w22, w33); w0[2] = w_round(w0[2], w0[3], w23, w0[0]); w0[3] = w_round(w0[3], w10, w30, w0[1]); - to_u128(w0) + from_u32x4(w0) } const fn w_round16(mut w: U512) -> U512 { @@ -110,7 +111,7 @@ pub const INIT: U256 = [ 0xbefa4fa4_64f98fa7_68581511_ffc00b31, ]; -pub const fn compress(mut w: U512) -> U256 { +pub const fn compress(mut w: U512) -> U224 { let mut x: U256 = INIT; x = round16(x, &w, 0); w = w_round16(w); @@ -120,24 +121,23 @@ pub const fn compress(mut w: U512) -> U256 { w = w_round16(w); x = round16(x, &w, 3); x = u32x8_add(&x, &INIT); - x[1] |= 0xFFFF_FFFF << 96; - x + let [x0, x1, x2, x3, x4, x5, x6, _] = to_u32x8(&x); + [x0, x1, x2, x3, x4, x5, x6] } #[cfg(test)] mod test { - use super::{compress, U256}; + use crate::u224::U224; - const A: U256 = compress([0x8000_0000, 0, 0, 0]); + use super::compress; + + const A: U224 = compress([0x8000_0000, 0, 0, 0]); #[test] fn test() { assert_eq!( A, - [ - 0x288234c4_476102bb_2a3a2bc9_d14a028c, - 0xFFFFFFFF_c5b3e42f_828ea62a_15a2b01f - ] + [0xd14a028c, 0x2a3a2bc9, 0x476102bb, 0x288234c4, 0x15a2b01f, 0x828ea62a, 0xc5b3e42f] ); } @@ -145,10 +145,7 @@ mod test { fn runtime_test() { assert_eq!( compress([0x8000_0000, 0, 0, 0]), - [ - 0x288234c4_476102bb_2a3a2bc9_d14a028c, - 0xFFFFFFFF_c5b3e42f_828ea62a_15a2b01f - ] + [0xd14a028c, 0x2a3a2bc9, 0x476102bb, 0x288234c4, 0x15a2b01f, 0x828ea62a, 0xc5b3e42f] ); } } diff --git a/blockset/src/u32x4.rs b/blockset/src/u128.rs similarity index 80% rename from blockset/src/u32x4.rs rename to blockset/src/u128.rs index 161f406b..eded843b 100644 --- a/blockset/src/u32x4.rs +++ b/blockset/src/u128.rs @@ -11,7 +11,7 @@ pub const fn to_u32x4(v: u128) -> [u32; 4] { } #[inline(always)] -pub const fn to_u128([w0, w1, w2, w3]: [u32; 4]) -> u128 { +pub const fn from_u32x4([w0, w1, w2, w3]: [u32; 4]) -> u128 { w0 as u128 | ((w1 as u128) << 32) | ((w2 as u128) << 64) | ((w3 as u128) << 96) } @@ -24,5 +24,5 @@ pub const fn get_u32(v: u128, i: usize) -> u32 { pub const fn u32x4_add(a: u128, b: u128) -> u128 { let [a0, a1, a2, a3] = to_u32x4(a); let [b0, b1, b2, b3] = to_u32x4(b); - to_u128([add(a0, b0), add(a1, b1), add(a2, b2), add(a3, b3)]) + from_u32x4([add(a0, b0), add(a1, b1), add(a2, b2), add(a3, b3)]) } diff --git a/blockset/src/digest224.rs b/blockset/src/u224.rs similarity index 94% rename from blockset/src/digest224.rs rename to blockset/src/u224.rs index 858a76c3..10bf0c4e 100644 --- a/blockset/src/digest224.rs +++ b/blockset/src/u224.rs @@ -5,19 +5,19 @@ use crate::{ bit_vec::BitVec, }; -pub type Digest224 = [u32; 7]; +pub type U224 = [u32; 7]; -pub trait Digest224Ex { +pub trait U224Ex { fn parity_bit(self) -> u8; } -impl Digest224Ex for &Digest224 { +impl U224Ex for &U224 { fn parity_bit(self) -> u8 { self.iter().fold(0, |a, b| a ^ b.count_ones()) as u8 & 1 } } -impl ToBase32 for &Digest224 { +impl ToBase32 for &U224 { fn to_base32(self) -> String { let (result, BitVec { value, len }) = self .iter() @@ -31,7 +31,7 @@ impl ToBase32 for &Digest224 { } } -impl FromBase32 for Digest224 { +impl FromBase32 for U224 { fn from_base32(i: &str) -> Option { let (vec, BitVec { value, len }) = i.from_base32()?; if vec.len() != 7 { @@ -39,7 +39,7 @@ impl FromBase32 for Digest224 { } assert_eq!(len, 1); assert_eq!(value | 1, 1); - let mut result = Digest224::default(); + let mut result = U224::default(); result.copy_from_slice(&vec); if value != result.parity_bit() as u64 { return None; @@ -52,7 +52,7 @@ impl FromBase32 for Digest224 { mod tests { use crate::{ base32::{StrEx, ToBase32}, - digest224::{Digest224, Digest224Ex}, + u224::{U224Ex, U224}, }; #[test] @@ -219,12 +219,12 @@ mod tests { #[test] fn invalid_str_test() { - assert_eq!("01".from_base32::(), None); + assert_eq!("01".from_base32::(), None); assert!("3v1d4j94scaseqgcyzr0ha5dxa9rx6ppnfbndck971ac0" - .from_base32::() + .from_base32::() .is_none()); assert!("1v1d4j94scaseqgcyzr0ha5dxa9rx6ppnfbndck971ac0" - .from_base32::() + .from_base32::() .is_some()); } } diff --git a/blockset/src/u256.rs b/blockset/src/u256.rs new file mode 100644 index 00000000..fc0708fd --- /dev/null +++ b/blockset/src/u256.rs @@ -0,0 +1,14 @@ +use crate::u128::{to_u32x4, u32x4_add}; + +pub type U256 = [u128; 2]; + +#[inline(always)] +pub const fn u32x8_add(&[a0, a1]: &U256, &[b0, b1]: &U256) -> U256 { + [u32x4_add(a0, b0), u32x4_add(a1, b1)] +} + +pub const fn to_u32x8([a, b]: &U256) -> [u32; 8] { + let [a0, a1, a2, a3] = to_u32x4(*a); + let [b0, b1, b2, b3] = to_u32x4(*b); + [a0, a1, a2, a3, b0, b1, b2, b3] +} diff --git a/blockset/src/u32x8.rs b/blockset/src/u32x8.rs deleted file mode 100644 index 8dacafc2..00000000 --- a/blockset/src/u32x8.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::u32x4::u32x4_add; - -pub type U256 = [u128; 2]; - -#[inline(always)] -pub const fn u32x8_add(&[a0, a1]: &U256, &[b0, b1]: &U256) -> U256 { - [u32x4_add(a0, b0), u32x4_add(a1, b1)] -} diff --git a/blockset/src/u32x16.rs b/blockset/src/u512.rs similarity index 100% rename from blockset/src/u32x16.rs rename to blockset/src/u512.rs